-
Notifications
You must be signed in to change notification settings - Fork 2
/
ejsappbooking_model.class.php
512 lines (407 loc) · 20 KB
/
ejsappbooking_model.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
use availability_date\condition;
use core_availability\info_module;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/filter/multilang/filter.php');
require_once($CFG->dirroot . '/calendar/lib.php');
require_once($CFG->dirroot . '/user/profile/lib.php'); // userprofile
require_once($CFG->dirroot . '/filter/multilang/filter.php');
class ejsappbooking_model
{
private $cm;
private $course;
private $ejsappbooking;
private $context;
private $multilang;
private $remlabs;
public function __construct($id, $n) {
global $DB, $CFG, $PAGE, $OUTPUT;
if ($id) {
$this->cm = get_coursemodule_from_id('ejsappbooking', $id, 0, false, MUST_EXIST);
$this->course = $DB->get_record('course', array('id' => $this->cm->course), '*', MUST_EXIST);
$this->ejsappbooking = $DB->get_record('ejsappbooking', array('id' => $this->cm->instance), '*', MUST_EXIST);
} else if ($n) {
$this->ejsappbooking = $DB->get_record('ejsappbooking', array('id' => $n), '*', MUST_EXIST);
$this->course = $DB->get_record('course', array('id' => $this->ejsappbooking->course), '*', MUST_EXIST);
$this->cm = get_coursemodule_from_instance('ejsappbooking',
$this->ejsappbooking->id, $this->course->id, false, MUST_EXIST);
} else {
print_error('You must specify a course_module ID or an instance ID');
}
require_login($this->course, true, $this->cm);
$this->context = context_module::instance($this->cm->id);
$PAGE->set_context($this->context);
$PAGE->set_cm($this->cm, $this->course, $this->ejsappbooking); // Set's up global $COURSE.
$this->multilang = new filter_multilang($this->context, array('filter_multilang_force_old' => 0));
if ($CFG->version < 2013111899) { // Moodle 2.6 or inferior.
add_to_log($this->course->id, 'ejsappbooking', 'view', "view.php?id={$this->cm->id}", $this->ejsappbooking->name, $this->cm->id);
} else {
$event = \mod_ejsappbooking\event\ejsappbooking_viewed::create(array(
'objectid' => $this->ejsappbooking->id,
'context' => $this->context
));
$event->add_record_snapshot('course_modules', $this->cm);
$event->add_record_snapshot('course', $this->course);
$event->add_record_snapshot('ejsappbooking', $this->ejsappbooking);
$event->trigger();
}
if ($CFG->version < 2016090100) {
$PAGE->set_button($OUTPUT->update_module_button($this->cm->id, 'ejsappbooking'));
}
// Check if the user has the capability to view the page - used when an assignment is set to hidden.
require_capability('mod/ejsappbooking:view', $this->context);
}
public function get_mod_url (){
return '/mod/ejsappbooking/view.php' . http_build_query(array('id' => $this->cm->id));
}
public function get_mod_name(){
return format_string($this->ejsappbooking->name);
}
public function get_course_name(){
return format_string($this->course->fullname);
}
public function get_mod_intro() {
$intro = "";
if ( isset($this->ejsappbooking->intro) && ($this->ejsappbooking->intro != null) ){
$intro = format_module_intro('ejsappbooking', $this->ejsappbooking, $this->cm->id);
}
return $intro;
}
public function get_remlabs() {
// Get the remote laboratories in which the user is authorized to make bookings.
global $DB;
$query =
$DB->get_records('ejsapp', array('course' => $this->course->id, 'is_rem_lab' => 1));
$this->remlabs = array();
foreach ($query as $key => $remlab) {
$ejsappcm = get_coursemodule_from_instance('ejsapp', $remlab->id, $this->course->id, false, MUST_EXIST);
$modinfo = get_fast_modinfo($this->course);
$ejsappcm = $modinfo->get_cm($ejsappcm->id);
if ($ejsappcm->uservisible) {
$item = new stdClass();
$item->lid = $remlab->id;
$item->name = $this->translate($remlab->name);
array_push($this->remlabs, $item);
}
}
return $this->remlabs;
}
public function get_practices($labid) {
global $DB;
// Select practices.
$practices = $DB->get_records_sql(
"SELECT id, ejsappid, practiceid, practiceintro
FROM {block_remlab_manager_exp2prc}
WHERE ejsappid = ? ", array($labid));
return $practices;
}
public function get_user_timezone_str() {
global $USER;
if ( $USER->timezone == '99' ){
$tz_str = get_string('time_zone_default', 'ejsappbooking');
} else {
$tz_str = get_string('time_zone', 'ejsappbooking') . ' ' . $USER->timezone;
}
return $tz_str;
}
public function get_user_timezone() {
global $USER;
if( $USER->timezone == '99'){
return $this->get_default_timezone();
} else {
return new DateTimeZone($USER->timezone);
}
}
public function get_default_timezone() {
return new DateTimeZone(date_default_timezone_get()); // server tz
}
public function get_timezone_edit_url() {
global $CFG, $USER;
$tz_edit_url = $CFG->wwwroot . "/user/edit.php?id=".$USER->id."&returnto=profile";
// $tz_edit_url = $CFG->wwwroot . "/user/editadvanced.php?id=".$USER->id; // ."#id_email"
return $tz_edit_url;
}
public function get_current_server_time() {
return new DateTime('NOW', $this->get_default_timezone());
}
public function translate($str) {
return $this->multilang->filter($str);
}
public function get_lab_conf($labid) {
global $DB;
$practiceintro = $DB->get_field('block_remlab_manager_exp2prc', 'practiceintro', array('ejsappid' => $labid));
$labconf = $DB->get_record('block_remlab_manager_conf', array('practiceintro' => $practiceintro));
return $labconf;
}
public function save_booking($labid, $practid, $starttime, $endtime) {
global $USER,$DB;
$bk = new stdClass();
$bk->username = $USER->username;
$bk->ejsappid = $labid;
$bk->practiceid = $practid;
$bk->starttime = $starttime;
$bk->endtime = $endtime;
$bk->valid = 1;
// Save in database.
$bookid = $DB->insert_record('ejsappbooking_remlab_access', $bk, true);
return $bookid;
}
public function create_event($labid, $practid, $starttime, $endtime, $slot_size) {
global $DB, $USER;
// Booking information - Event.
$inittime = DateTime::createFromFormat('Y-m-d H:i:s', $starttime, $this->get_user_timezone());
$finishtime = DateTime::createFromFormat('Y-m-d H:i:s', $endtime, $this->get_user_timezone());
$lab = $DB->get_record('ejsapp', array('id' => $labid));
$prac = $DB->get_record('block_remlab_manager_exp2prc', array('practiceid' => $practid,
'ejsappid' => $labid));
$event = new stdClass();
$event->name = get_string('book_message', 'ejsappbooking') . ' ' . $lab->name .
'. ' . $prac->practiceintro;
$event->description = get_string('bookinginfo', 'ejsappbooking') . '<br><br>';
$event->groupid = 0;
$event->courseid = 0;
$event->userid = $USER->id;
$event->eventtype = 'user';
$event->timestart = make_timestamp($inittime->format('Y'), $inittime->format('m'),
$inittime->format('d'), $inittime->format('H'), $inittime->format('i'));
$event->timeduration = $slot_size*60;
$event->description = $event->description . get_string('plant', 'ejsappbooking') .
': ' . $this->multilang->filter($lab->name) . '. ' . $prac->practiceintro . '<br>';
$event->description = $event->description . get_string('date', 'ejsappbooking') .
': ' . $inittime->format("Y-m-d"). '<br>';
$event->description = $event->description . get_string('hour', 'ejsappbooking') .
': ' . $inittime->format("H:i") . '-' . $finishtime->format("H:i");
// Create the event on the calendar.
calendar_event::create($event);
}
public function send_message($labid, $practid, $starttime, $endtime) {
global $DB, $USER, $CFG;
// Check message delivery.
$init = explode(" ",$starttime);
$finish = explode(" ",$endtime);
$msgbody = get_string('bookinginfo', 'ejsappbooking') . '<br><br>';
$msgbody = $msgbody . get_string('plant', 'ejsappbooking') . ': ' .
$this->multilang->filter($labid) . '. ' . $practid . '<br>';
$msgbody = $msgbody . get_string('date', 'ejsappbooking') . ': ' .
$init[0] . '<br>';
$msgbody = $msgbody . get_string('hour', 'ejsappbooking') . ': ' .
$init[1] . '-' . $finish[1] . '<br><br>';
if (empty($CFG->messaging)) {
// $out .= '<p align="center"> <strong>' . get_string('messagingdisabled', 'message') . '</strong></p>';
return;
}
// Format and send the message by the Admin user.
$format = FORMAT_HTML;
$cuser = $DB->get_record('user', array('id' => 2));
@message_post_message($cuser, $USER, $msgbody, $format);
}
public function booking_exists($labid, $practid, $date) {
global $DB;
$server_tz = $this->get_default_timezone();
$sdate = clone $date;
$sdate->setTimeZone($server_tz);
$query = $DB->record_exists('ejsappbooking_remlab_access', array(
'starttime' => $sdate->format("Y-m-d H:i:s"),
'ejsappid' => $labid, 'practiceid' => $practid
)
);
return ($query != null );
}
public function delete_booking($bookid) { // Check and delete booking
global $DB, $USER;
$record = $DB->get_record('ejsappbooking_remlab_access', array('id' => $bookid));
if ( ! $record ){
return -1; // not found
} else {
$lab = $DB->get_record('ejsapp', array('id' => $record->ejsappid));
$prac = $DB->get_record('block_remlab_manager_exp2prc', array('practiceid' => $record->practiceid,
'ejsappid' => $record->ejsappid));
$name = get_string('book_message', 'ejsappbooking') . ' ' . $lab->name .
'. ' . $prac->practiceintro;
$inittime = DateTime::createFromFormat('Y-m-d H:i:s', $record->starttime, $this->get_user_timezone());
//$inittime = DateTime::createFromFormat('Y-m-d H:i:s', "2019-11-18 22:15:00", $this->get_user_timezone());
$time = make_timestamp($inittime->format('Y'), $inittime->format('m'),
$inittime->format('d'), $inittime->format('H'), $inittime->format('i'));
$event = $DB->get_record_sql('SELECT * FROM {event} WHERE userid = ? AND name = ? AND timestart = ?',
array($USER->id, $name, $time));
// Delete calendar´s event.
if ($event) {
$event = calendar_event::load($event->id);
$event->delete($deleterepeated = false);
}
$success = $DB->delete_records('ejsappbooking_remlab_access', array('id' => $bookid));
if ( $success ){
return 0; // success
} else {
return -2; // error deleting
}
}
}
public function get_sql_str_to_date_query() {
global $CFG;
if ( $CFG->dbtype == 'pgsql'){
return "to_timestamp( ?, 'YYYY-MM-DD HH24:MI:SS' )";
} if ( $CFG->dbtype == 'mysql' ) {
return "STR_TO_DATE( ?, '%Y-%m-%d %T')";
}
return null;
}
public function get_day_bookings($labid, $date, $checkavailability = false) {
global $DB;
$user_tz = $this->get_user_timezone();
$server_tz = $this->get_default_timezone();
$sdate = clone $date;
$sdate->setTime(0,0,0);
$sdate->setTimeZone($server_tz);
$edate= clone $sdate;
$edate->add(new DateInterval('PT24H'));
$dayaccesses = $DB->get_records_sql("
SELECT starttime
FROM {ejsappbooking_remlab_access}
WHERE ejsappid = ?
AND starttime >= ?
AND starttime <= ?
ORDER BY starttime ASC",
array($labid, $sdate->format('Y-m-d H:i:s'), $edate->format('Y-m-d H:i:s'),
$this->get_sql_str_to_date_query(), $this->get_sql_str_to_date_query()));
$list = [];
foreach ($dayaccesses as $slot) {
$date = DateTime::createFromFormat('Y-m-d H:i:s', $slot->starttime, $server_tz);
$date->setTimeZone($user_tz);
array_push($list, $date->format("H:i"));
}
if ($checkavailability) {
$moduleid = $DB->get_field('modules', 'id', array('name' => 'ejsapp'));
$courseid = $DB->get_field('ejsapp', 'course', array('id' => 1));
$cmid = $DB->get_field('course_modules', 'id', array('instance' => $labid, 'module' => $moduleid,
'course' => $courseid));
$modinfo = get_fast_modinfo($courseid);
$cm = $modinfo->get_cm($cmid);
if ($cm->availability) {
$info = new info_module($cm);
$json = json_decode($cm->availability);
$conditions = $json->c;
if (strlen($json->op) > 1) {
$not = true;
$op = substr($json->op, 1, 1);
} else {
$not = false;
$op = $json->op;
}
$inittime = $sdate->getTimeStamp();
$endtime = $inittime + 24 * 60 * 60;
$practiceintro = $DB->get_field('block_remlab_manager_exp2prc', 'practiceintro',
array('practiceid' => 1, 'ejsappid' => $labid));
$slotduration = $DB->get_field('block_remlab_manager_conf', 'slotsduration',
array('practiceintro' => $practiceintro));
$slotduration = $this->get_slot_size($slotduration) * 60;
for ($slottime = $inittime; $slottime < $endtime; $slottime += $slotduration) {
$available = $this->check_nested_conditions($conditions, $slottime, $not, $info, $op);
if (!$available) $list = $this->append_hours($slottime, $server_tz, $user_tz, $list);
}
}
}
return $list;
}
private function check_nested_conditions($conditions, $slottime, $not, $info, $op) {
($op == '|') ? $available = false : $available = true;
for ($i = 0; $i < count($conditions); $i++) {
if (isset($conditions[$i]->c)) {
$deeperconditions = $conditions[$i]->c;
if (strlen($conditions[$i]->op) > 1) {
$deepernot = true;
$deeperop = substr($conditions[$i]->op, 1, 1);
} else {
$deepernot = false;
$deeperop = $conditions[$i]->op;
}
$available = $this->check_nested_conditions($deeperconditions, $slottime, $deepernot, $info, $deeperop);
if ($not && !$deepernot) $available = !$available;
} else {
$available = $this->check_single_condition($conditions[$i], $slottime, $not, $info, $op, $available);
}
if ($op == '|') {
if ($available) break;
} else {
if (!$available) break;
}
}
return $available;
}
private function check_single_condition($condition, $slottime, $not, $info, $op, $available) {
global $USER;
if ($condition->type == "date") {
$date = new condition((object)array('d' => $condition->d, 't' => $condition->t));
$date->set_current_time_for_test($slottime);
$newavailable = $date->is_available($not, $info, true, $USER->id);
($op == '|') ? $available |= $newavailable : $available &= $newavailable;
}
return $available;
}
private function append_hours($slottime, $server_tz, $user_tz, $list) {
$slot = date('Y-m-d H:i:s', $slottime);
$date = DateTime::createFromFormat('Y-m-d H:i:s', $slot, $server_tz);
$date->setTimeZone($user_tz);
array_push($list, $date->format("H:i"));
return $list;
}
public function get_week_bookings($labid,$sdate) { // Determine user´s bookings of the week.
global $USER, $DB;
$user_tz = $this->get_user_timezone();
$server_tz = $this->get_default_timezone();
$day = $sdate->format('w');
if ($day == 0) {
$monday = 6;
$sunday = 0;
} else {
$monday = $day - 1;
$sunday = 7 - $day;
}
$dmonday = strtotime('-' . $monday . 'day', strtotime($sdate->format('Y-m-d')));
$dsunday = strtotime('+' . $sunday . 'day', strtotime($sdate->format('Y-m-d')));
$dmonday = date('Y-m-d', $dmonday);
$dsunday = date('Y-m-d', $dsunday);
$week_start = DateTime::createFromFormat('Y-m-d H:i:s', $dmonday . ' 00:00:00', $user_tz);
$week_start->setTimeZone($server_tz);
$week_end = DateTime::createFromFormat('Y-m-d H:i:s', $dsunday . ' 23:59:59', $user_tz);
$week_end->setTimeZone($server_tz);
$weekaccesses = $DB->get_records_sql("
SELECT starttime FROM {ejsappbooking_remlab_access}
WHERE username = ? AND ejsappid = ?
AND starttime >= ?
AND starttime <= ?
ORDER BY starttime ASC",
array($USER->username,$labid,$week_start->format('Y-m-d H:i:s'),
$week_end->format('Y-m-d H:i:s'), $this->get_sql_str_to_date_query(), $this->get_sql_str_to_date_query())
);
return $weekaccesses;
}
public function get_total_bookings($labid) { // Retrieving user´s bookings at the DB.
global $USER, $DB;
$start = $this->get_current_server_time();
$useraccess = $DB->get_records_sql("
SELECT starttime
FROM {ejsappbooking_remlab_access}
WHERE username = ? AND ejsappid = ?
AND starttime >= ?
ORDER BY starttime ASC",
array($USER->username, $labid, $start->format('Y-m-d H:i:s'), $this->get_sql_str_to_date_query()));
return $useraccess;
}
public function get_slot_size($slotsduration) {
$durations = array( "0" => 60, "1"=>30, "2"=>15, "3"=>5, "4"=>2 );
return $durations[$slotsduration];
}
public function get_current_user_active_bookings($sdate) {
global $USER, $DB;
$bookings = $DB->get_records_sql("
SELECT a.id, a.username, a.ejsappid, a.practiceid, a.starttime, a.endtime, a.valid, b.name, c.practiceintro
FROM {ejsappbooking_remlab_access} a INNER JOIN {ejsapp} b ON a.ejsappid = b.id
INNER JOIN {block_remlab_manager_exp2prc} c ON a.practiceid = c.practiceid
WHERE a.ejsappid = c.ejsappid AND a.username = ?
AND a.endtime > ?
ORDER BY a.starttime",
array($USER->username, $sdate->format('Y-m-d H:i:s'), $this->get_sql_str_to_date_query()));
return $bookings;
}
}