Presence.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Http\Middleware\TenantMiddleware;
  5. class Presence extends Component
  6. {
  7. public $calendar;
  8. public $records;
  9. public $member_ids = [];
  10. public $court_id, $instructor_id, $motivation_id, $motivation_manual_id, $note, $manual;
  11. public $save_court_id, $save_instructor_id, $save_notes;
  12. public $newMemberFirstName, $newMemberLastName, $newMemberEmail, $newMemberToComplete, $newMemberFiscalCode, $newMemberFiscalCodeExist, $newMemberMotivationId;
  13. public $userName, $userEmail;
  14. public $added = false;
  15. public $filter = '';
  16. public $courts = [];
  17. public $instructors = [];
  18. public $motivations = [];
  19. public $motivations_add = [];
  20. public $insertUser = 'exist';
  21. public $course_limit;
  22. public $motivation_course_id = null;
  23. public $motivation_course_name = 0;
  24. public $motivation_course_level = 0;
  25. public $motivation_course_frequency = 0;
  26. public $course_names = [];
  27. public $course_levels = [];
  28. public $course_frequencies = [];
  29. public $members = [];
  30. public $newMembers = [];
  31. public $ids = [];
  32. public function boot()
  33. {
  34. app(TenantMiddleware::class)->setupTenantConnection();
  35. }
  36. public function mount()
  37. {
  38. setlocale(LC_ALL, 'it_IT');
  39. $this->calendar = \App\Models\Calendar::findOrFail($_GET["calendarId"]);
  40. $this->court_id = $this->calendar->court_id;
  41. $this->instructor_id = $this->calendar->instructor_id;
  42. $this->motivation_manual_id = $this->calendar->motivation_manual_id;
  43. $this->manual = $this->calendar->manual;
  44. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  45. ->where(function ($query) {
  46. $query->where('is_archived', false)
  47. ->orWhereNull('is_archived');
  48. })
  49. ->where(function ($query) {
  50. $query->where('is_deleted', false)
  51. ->orWhereNull('is_deleted');
  52. })->orderBy('last_name')->orderBy('first_name')->get();
  53. $this->note = $this->calendar->note;
  54. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  55. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->orderBy('name', 'asc')->get();
  56. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
  57. $this->motivations_add = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'add')->get();
  58. $this->save_court_id = 0;
  59. $this->save_instructor_id = 0;
  60. $this->save_notes = '';
  61. $this->insertUser = 'exist';
  62. $this->course_limit = now()->endOfDay();
  63. $this->course_names = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->orderBy('name')->groupBy('name')->pluck('name');
  64. $this->course_levels = [];
  65. $this->course_frequencies = [];
  66. }
  67. function updatedMotivationCourseName()
  68. {
  69. if ($this->motivation_course_name > 0 && $this->motivation_course_name != '') {
  70. $this->motivation_course_id = null;
  71. $this->motivation_course_level = 0;
  72. $this->motivation_course_frequency = 0;
  73. $levels_ids = [];
  74. $levels = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->where('name', $this->motivation_course_name)->get();
  75. foreach ($levels as $l) {
  76. $levels_ids[] = $l->course_level_id;
  77. }
  78. $this->course_levels = \App\Models\CourseLevel::where('enabled', true)->whereIn('id', $levels_ids)->orderBy('name')->get();
  79. $this->course_frequencies = [];
  80. } else {
  81. $this->course_levels = [];
  82. $this->course_frequencies = [];
  83. }
  84. }
  85. function updatedMotivationCourseLevel()
  86. {
  87. if ($this->motivation_course_name > 0 && $this->motivation_course_name != '' && $this->motivation_course_level > 0 && $this->motivation_course_level != '') {
  88. $this->motivation_course_id = null;
  89. $this->motivation_course_frequency = 0;
  90. $frequency_ids = [];
  91. $frequencies = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->where('name', $this->motivation_course_name)->where('course_level_id', $this->motivation_course_level)->get();
  92. foreach ($frequencies as $f) {
  93. $frequency_ids[] = $f->course_frequency_id;
  94. }
  95. $this->course_frequencies = \App\Models\CourseFrequency::where('enabled', true)->whereIn('id', $frequency_ids)->orderBy('name')->get();
  96. } else {
  97. $this->course_frequencies = [];
  98. }
  99. }
  100. function updatedMotivationCourseFrequency()
  101. {
  102. if ($this->motivation_course_name > 0 && $this->motivation_course_name != '' && $this->motivation_course_level > 0 && $this->motivation_course_level != '' && $this->motivation_course_frequency > 0 && $this->motivation_course_frequency != '') {
  103. $this->motivation_course_id = null;
  104. $course = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->where('name', $this->motivation_course_name)->where('course_level_id', $this->motivation_course_level)->where('course_frequency_id', $this->motivation_course_frequency)->first();
  105. $this->motivation_course_id = $course->id;
  106. } else {
  107. $this->motivation_course_id = null;
  108. }
  109. }
  110. public function updatedNewMemberMotivationId()
  111. {
  112. $this->emit('reload');
  113. }
  114. public function render()
  115. {
  116. $this->records = [];
  117. setlocale(LC_ALL, 'it_IT');
  118. $presenceMembers = [];
  119. if (!$this->manual) {
  120. // Carco tutti gli iscritti a un corso padre in quel giorno con un range orario simile
  121. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  122. $dow = date('w', strtotime($this->calendar->from));
  123. $d = $days[$dow];
  124. $h = date('H:i', strtotime($this->calendar->from));
  125. // Elenco corsi per tipologia in base al calendario
  126. $courses = \App\Models\Course::query()
  127. ->where('name', $this->calendar->name)
  128. ->where('date_from', '<=', $this->calendar->from)
  129. ->where('date_to', '>=', $this->calendar->to)
  130. ->get(['id', 'when']);
  131. $slotCourseIds = $courses
  132. ->filter(fn($c) => $this->courseMatchesSlot($c->when, $d, $h))
  133. ->pluck('id')
  134. ->all();
  135. if (empty($slotCourseIds)) {
  136. $members_courses = [];
  137. } else {
  138. $members_courses = \App\Models\MemberCourse::query()
  139. ->whereIn('course_id', $slotCourseIds)
  140. ->whereDate('date_from', '<=', $this->calendar->from)
  141. ->whereDate('date_to', '>=', $this->calendar->from)
  142. ->pluck('member_id')
  143. ->toArray();
  144. }
  145. if ($this->filter != '') {
  146. $filter = $this->filter;
  147. $members = \App\Models\Member::whereIn('id', $members_courses)->where(function ($query) use ($filter) {
  148. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  149. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  150. })
  151. ->where(function ($query) {
  152. $query->where('is_archived', false)
  153. ->orWhereNull('is_archived');
  154. })
  155. ->where(function ($query) {
  156. $query->where('is_deleted', false)
  157. ->orWhereNull('is_deleted');
  158. })->orderBy('last_name')->orderBy('first_name')->get();
  159. } else
  160. $members = \App\Models\Member::whereIn('id', $members_courses)
  161. ->where(function ($query) {
  162. $query->where('is_archived', false)
  163. ->orWhereNull('is_archived');
  164. })
  165. ->where(function ($query) {
  166. $query->where('is_deleted', false)
  167. ->orWhereNull('is_deleted');
  168. })->orderBy('last_name')->orderBy('first_name')->get();
  169. // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
  170. // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
  171. foreach ($members as $member) {
  172. $presenceMembers[] = $member->id;
  173. //$this->member_ids[] = $member->id;
  174. $this->records[] = $this->getMember($member);
  175. }
  176. }
  177. // Aggiungo i membri iscritti
  178. $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
  179. if ($this->filter != '') {
  180. $filter = $this->filter;
  181. $members = \App\Models\Member::whereIn('id', $members_presences)->where(function ($query) use ($filter) {
  182. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  183. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  184. })
  185. ->where(function ($query) {
  186. $query->where('is_archived', false)
  187. ->orWhereNull('is_archived');
  188. })
  189. ->where(function ($query) {
  190. $query->where('is_deleted', false)
  191. ->orWhereNull('is_deleted');
  192. })->get();
  193. } else
  194. $members = \App\Models\Member::whereIn('id', $members_presences)
  195. ->where(function ($query) {
  196. $query->where('is_archived', false)
  197. ->orWhereNull('is_archived');
  198. })
  199. ->where(function ($query) {
  200. $query->where('is_deleted', false)
  201. ->orWhereNull('is_deleted');
  202. })->get();
  203. foreach ($members as $member) {
  204. //$this->member_ids[] = $member->id;
  205. $this->records[] = $this->getMember($member);
  206. }
  207. foreach ($this->newMembers as $m) {
  208. $member = \App\Models\Member::findOrFail($m);
  209. //$this->member_ids[] = $member->id;
  210. $this->records[] = $this->getMember($member);
  211. }
  212. /*$calendars = \App\Models\Calendar::get();
  213. foreach($calendars as $c)
  214. {
  215. $this->records[] = array('id' => $c->id, 'title' => $c->course->name, 'start' => $c->from, 'end' => $c->to);
  216. }*/
  217. return view('livewire.presence');
  218. }
  219. private function courseMatchesSlot(?string $whenJson, string $day, string $hhmm): bool
  220. {
  221. if (!$whenJson) return false;
  222. $when = json_decode($whenJson, true);
  223. if (!is_array($when)) return false;
  224. foreach ($when as $period) {
  225. $days = $period['day'] ?? [];
  226. $from = $period['from'] ?? null;
  227. if (!$from || empty($days)) continue;
  228. $from = substr((string)$from, 0, 5);
  229. if ($from === $hhmm && in_array($day, $days, true)) {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. public function getDateX()
  236. {
  237. setlocale(LC_ALL, 'it_IT');
  238. $days = ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'];
  239. $months = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'];
  240. return $days[date('w', strtotime($this->calendar->from))] . " " . date("d", strtotime($this->calendar->from)) . " " . $months[date("n", strtotime($this->calendar->from)) - 1];
  241. }
  242. public function getMember($member)
  243. {
  244. $latestCert = \App\Models\MemberCertificate::where('member_id', $member->id)
  245. ->orderBy('expire_date', 'desc')
  246. ->first();
  247. $certificate = '';
  248. $y = "|";
  249. if ($latestCert) {
  250. $latest_date = $latestCert->expire_date;
  251. $status = '';
  252. if ($latest_date < date("Y-m-d")) {
  253. $status = "0"; // Expired
  254. } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
  255. $status = "1"; // Expiring soon
  256. } else {
  257. $status = "2"; // Valid
  258. }
  259. $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
  260. }
  261. $presence = false;
  262. $my_presence = false;
  263. $motivation = '';
  264. $status = 0;
  265. $court = '';
  266. $instructor = '';
  267. $additional_instructor = '';
  268. $notes = '';
  269. $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
  270. if ($has_presence) {
  271. $presence = true;
  272. $my_presence = $has_presence->user_id == \Auth::user()->id;
  273. if ($has_presence->motivation_id > 0) {
  274. $motivation = \App\Models\Motivation::findOrFail($has_presence->motivation_id)->name;
  275. }
  276. $status = $has_presence->status;
  277. // cerca nel master con ::on('mysql')
  278. $user_instructor = \Illuminate\Foundation\Auth\User::on('mysql')->find($has_presence->user_id);
  279. $instructor = $user_instructor?->name;
  280. if ($has_presence->court_id > 0) {
  281. $court = \App\Models\Court::findOrFail($has_presence->court_id)->name;
  282. }
  283. if ($has_presence->instructor_id > 0 && $has_presence->instructor_id !== $has_presence->user_id) {
  284. $additional_user_instructor = \App\Models\User::find($has_presence->instructor_id);
  285. $additional_instructor = $additional_user_instructor?->name . ' ' . $additional_user_instructor?->cognome;
  286. }
  287. if (!is_null($has_presence->notes)) {
  288. $notes = $has_presence->notes;
  289. }
  290. }
  291. if (in_array($member->id, $this->newMembers)) {
  292. $presence = true;
  293. $my_presence = true;
  294. }
  295. return array(
  296. 'id' => $member->id,
  297. 'first_name' => $member->first_name,
  298. 'last_name' => $member->last_name,
  299. 'certificate' => $y,
  300. 'presence' => $presence,
  301. 'my_presence' => $my_presence,
  302. 'status' => $status,
  303. 'motivation' => $motivation,
  304. 'court' => $court,
  305. 'instructor' => $instructor,
  306. 'additional_instructor' => $additional_instructor,
  307. 'notes' => $notes,
  308. );
  309. }
  310. public function save($ids)
  311. {
  312. $this->saveAndStay($ids);
  313. $last_date = explode(" ", $this->calendar->from)[0];
  314. return redirect()->to("/calendar?last_date={$last_date}");
  315. }
  316. public function saveAndStay($ids)
  317. {
  318. $this->calendar->court_id = $this->court_id;
  319. $this->calendar->instructor_id = $this->instructor_id;
  320. $this->calendar->note = $this->note;
  321. if ($this->motivation_id != "" && $this->motivation_id != null)
  322. $this->calendar->motivation_id = $this->motivation_id;
  323. if ($this->motivation_manual_id != "" && $this->motivation_manual_id != null)
  324. $this->calendar->motivation_manual_id = $this->motivation_manual_id;
  325. $this->calendar->save();
  326. // $x = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->first();
  327. // $mid = null;
  328. // if ($x) {
  329. // $mid = $x->motivation_id;
  330. // $x->delete();
  331. // }
  332. // Mappa degli ultimi motivation_id per ogni member_id dell'utente e calendario correnti
  333. $userId = \Auth::user()->id;
  334. $calendarId = $this->calendar->id;
  335. $motivation_query = \App\Models\Presence::query()
  336. ->select('member_id', 'motivation_id', 'motivation_course_id')
  337. ->where('calendar_id', $calendarId)
  338. ->where('user_id', $userId)
  339. ->where('status', '<>', 99)
  340. ->whereIn('id', function ($q) use ($calendarId, $userId) {
  341. $q->selectRaw('MAX(id)')
  342. ->from('presences')
  343. ->where('calendar_id', $calendarId)
  344. ->where('user_id', $userId)
  345. ->where('status', '<>', 99)
  346. ->groupBy('member_id');
  347. })
  348. ->get()
  349. ->keyBy('member_id');
  350. $motivationMap = $motivation_query->map(fn($row) => $row->motivation_id)->toArray();
  351. $motivationCourseMap = $motivation_query->map(fn($row) => $row->motivation_course_id)->toArray();
  352. // Elimino tutti i dati correnti che devono essere sostituiti
  353. \App\Models\Presence::query()
  354. ->where('calendar_id', $calendarId)
  355. ->where('user_id', $userId)
  356. ->where('status', '<>', 99)
  357. ->delete();
  358. // Ricreo le presenze per ogni membro contenuto in $ids
  359. foreach ($ids as $id) {
  360. $p = new \App\Models\Presence();
  361. $p->member_id = $id;
  362. $p->calendar_id = $calendarId;
  363. // Se per quel membro esisteva un motivation_id, lo riuso, altrimenti lo lascio null
  364. $p->motivation_id = $motivationMap[$id] ?? null;
  365. $p->motivation_course_id = $motivationCourseMap[$id] ?? null;
  366. $p->user_id = $userId;
  367. $p->status = 0;
  368. // Salvo eventuale court_id (se presente e maggiore di 0)
  369. if ($this->save_court_id > 0) {
  370. $p->court_id = $this->save_court_id;
  371. }
  372. // Salvo eventuale instructor_id (se presente e maggiore di 0)
  373. if ($this->save_instructor_id > 0) {
  374. $p->instructor_id = $this->save_instructor_id;
  375. }
  376. // Salvo eventuali note (se non vuote)
  377. if ($this->save_notes != '') {
  378. $p->notes = $this->save_notes;
  379. }
  380. $p->save();
  381. }
  382. $this->emit('setSaving');
  383. }
  384. public function cancel($ids, $motivation_id)
  385. {
  386. $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('member_id', $ids)->get();
  387. foreach ($presences as $presence) {
  388. $presence->motivation_id = $motivation_id;
  389. $presence->status = 99;
  390. $presence->save();
  391. }
  392. return redirect()->to('/presences?calendarId=' . $this->calendar->id);
  393. }
  394. public function addMember($ids)
  395. {
  396. $this->added = true;
  397. //if (!in_array($id, $this->newMembers))
  398. // $this->newMembers[] = $id;
  399. $this->member_ids = $ids;
  400. $this->emit('reload');
  401. }
  402. public function cancelCalendar()
  403. {
  404. $this->calendar->motivation_id = $this->motivation_id;
  405. $this->calendar->status = 99;
  406. $this->calendar->save();
  407. return redirect()->to('/calendar');
  408. }
  409. public function createMember()
  410. {
  411. if (!$this->added) {
  412. $this->newMemberFiscalCodeExist = false;
  413. $this->validate([
  414. "newMemberMotivationId" => 'required',
  415. ]);
  416. /*$this->validate([
  417. // 'newMemberFiscalCode'=>'required|max:16',
  418. 'newMemberFirstName'=>'required',
  419. 'newMemberLastName'=>'required',
  420. //'newMemberEmail'=>'required',
  421. ]);*/
  422. // Check fiscal code exist
  423. $exist = false;
  424. if ($this->newMemberFiscalCode != '') {
  425. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  426. $exist = $check->count() > 0;
  427. }
  428. if (!$exist) {
  429. $member = \App\Models\Member::create([
  430. 'first_name' => strtoupper($this->newMemberFirstName),
  431. 'last_name' => strtoupper($this->newMemberLastName),
  432. 'email' => strtoupper($this->newMemberEmail),
  433. 'to_complete' => true,
  434. 'fiscal_code' => $this->newMemberFiscalCode,
  435. 'status' => true
  436. ]);
  437. //if (!in_array($member->id, $this->newMembers))
  438. // $this->newMembers[] = $member->id;
  439. $p = new \App\Models\Presence();
  440. $p->member_id = $member->id;
  441. $p->calendar_id = $this->calendar->id;
  442. $p->motivation_id = $this->newMemberMotivationId;
  443. $p->motivation_course_id = $this->motivation_course_id;
  444. $p->user_id = \Auth::user()->id;
  445. $p->status = 0;
  446. $p->court_id = null;
  447. $p->instructor_id = null;
  448. $p->notes = null;
  449. $p->save();
  450. $this->emit('reload');
  451. $this->emit('saved');
  452. /*$this->newMemberFirstName = '';
  453. $this->newMemberLastName = '';
  454. $this->newMemberEmail = '';
  455. $this->newMemberFiscalCode = '';*/
  456. } else {
  457. $this->newMemberFiscalCodeExist = true;
  458. }
  459. } else {
  460. if ($this->member_ids != null) {
  461. $this->validate([
  462. "newMemberMotivationId" => 'required',
  463. ]);
  464. foreach ($this->member_ids as $m) {
  465. //if ($this->manual)
  466. //{
  467. //\App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
  468. //foreach($ids as $id)
  469. //{
  470. $p = new \App\Models\Presence();
  471. $p->member_id = $m;
  472. $p->calendar_id = $this->calendar->id;
  473. $p->motivation_id = $this->newMemberMotivationId;
  474. $p->motivation_course_id = $this->motivation_course_id;
  475. $p->user_id = \Auth::user()->id;
  476. $p->status = 0;
  477. $p->court_id = null;
  478. $p->instructor_id = null;
  479. $p->notes = null;
  480. $p->save();
  481. //}
  482. /*}
  483. else
  484. {
  485. if (!in_array($m, $this->newMembers))
  486. $this->newMembers[] = $m;
  487. }*/
  488. }
  489. }
  490. //$this->member_id = 0;
  491. $this->member_ids = [];
  492. $this->added = false;
  493. $this->emit('reload');
  494. $this->emit('saved');
  495. }
  496. $this->resetCreationFields();
  497. }
  498. public function resetCreationFields()
  499. {
  500. $this->insertUser = 'new';
  501. $this->motivation_course_id = null;
  502. $this->motivation_course_name = null;
  503. $this->motivation_course_level = null;
  504. $this->motivation_course_frequency = null;
  505. $this->newMemberMotivationId = null;
  506. $this->newMemberFirstName = null;
  507. $this->newMemberLastName = null;
  508. $this->newMemberEmail = null;
  509. $this->newMemberFiscalCode = null;
  510. $this->emit('resetCreationForm');
  511. }
  512. public function createInstructor()
  513. {
  514. $user = \App\Models\User::create([
  515. 'name' => $this->userName,
  516. 'email' => $this->userEmail,
  517. 'password' => '',
  518. 'level' => 2,
  519. 'enabled' => true
  520. ]);
  521. $this->instructor_id = $user->id;
  522. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->orderBy('name', 'asc')->orderBy('cognome', 'asc')->get();
  523. $this->emit('saved');
  524. }
  525. public function removeSingle($id)
  526. {
  527. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->delete();
  528. $this->emit('reload');
  529. }
  530. public function revert($id)
  531. {
  532. $p = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->first();
  533. $p->motivation_id = null;
  534. $p->status = 0;
  535. $p->save();
  536. $this->emit('reload');
  537. }
  538. }