forked from Wunderbyte-GmbH/moodle-mod_booking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocallib.php
More file actions
executable file
·541 lines (458 loc) · 18.6 KB
/
locallib.php
File metadata and controls
executable file
·541 lines (458 loc) · 18.6 KB
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die();
use mod_booking\booking_option;
use mod_booking\booking_utils;
use mod_booking\event\bookingoptiondate_created;
use mod_booking\singleton_service;
global $CFG;
require_once($CFG->dirroot . '/user/selector/lib.php');
require_once($CFG->dirroot . '/mod/booking/lib.php');
/**
* Abstract class used by booking subscriber selection controls
*
* @package mod_booking
* @copyright 2013 David Bogner
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class booking_user_selector_base extends user_selector_base {
/**
* The id of the booking this selector is being used for.
*
* @var int
*/
protected $bookingid = null;
/**
* The id of the current option
*
* @var int
*/
protected $optionid = null;
/**
* The potential users array
*
* @var array
*/
public $potentialusers = null;
/**
*
* @var array of userids
*/
public $bookedvisibleusers = null;
/**
*
* @var stdClass
*/
public $course;
/**
*
* @var cm_info
*/
public $cm;
/**
* Constructor method
*
* @param string $name
* @param array $options
*/
public function __construct($name, $options) {
$this->maxusersperpage = 50;
parent::__construct($name, $options);
if (isset($options['bookingid'])) {
$this->bookingid = $options['bookingid'];
}
if (isset($options['potentialusers'])) {
$this->potentialusers = $options['potentialusers'];
}
if (isset($options['optionid'])) {
$this->optionid = $options['optionid'];
}
if (isset($options['course'])) {
$this->course = $options['course'];
}
if (isset($options['cm'])) {
$this->cm = $options['cm'];
}
}
protected function get_options() {
$options = parent::get_options();
$options['file'] = 'mod/booking/locallib.php';
$options['bookingid'] = $this->bookingid;
$options['potentialusers'] = $this->potentialusers;
$options['optionid'] = $this->optionid;
$options['cm'] = $this->cm;
$options['course'] = $this->course;
// Add our custom options to the $options array.
return $options;
}
/**
* Sets the existing subscribers
*
* @param array $users
*/
public function set_potential_users(array $users) {
$this->potentialusers = $users;
}
}
/**
* User selector for booking other users
*/
class booking_potential_user_selector extends booking_user_selector_base {
public $options;
public function __construct($name, $options) {
$this->options = $options;
parent::__construct($name, $options);
}
public function find_users($search) {
global $DB, $USER;
$onlygroupmembers = false;
if (groups_get_activity_groupmode($this->cm) == SEPARATEGROUPS and
!has_capability('moodle/site:accessallgroups',
\context_course::instance($this->course->id))) {
$onlygroupmembers = true;
}
$fields = "SELECT " . $this->required_fields_sql("u");
$countfields = 'SELECT COUNT(1)';
list($searchcondition, $searchparams) = $this->search_sql($search, 'u');
$groupsql = '';
if ($onlygroupmembers) {
list($groupsql, $groupparams) = \mod_booking\booking::booking_get_groupmembers_sql($this->course->id);
list($esql, $eparams) = get_enrolled_sql($this->options['accesscontext'], null, null, true);
$groupsql = " AND u.id IN (" . $groupsql.")";
$params = array_merge($eparams, $groupparams);
} else {
list($esql, $params) = get_enrolled_sql($this->options['accesscontext'], null, null, true);
}
$option = new stdClass();
$option->id = $this->options['optionid'];
$option->bookingid = $this->options['bookingid'];
if (booking_check_if_teacher($option) && !has_capability(
'mod/booking:readallinstitutionusers', $this->options['accesscontext'])) {
$institution = $DB->get_record('booking_options',
array('id' => $this->options['optionid']));
$searchparams['onlyinstitution'] = $institution->institution;
$searchcondition .= ' AND u.institution LIKE :onlyinstitution';
}
$sql = " FROM {user} u
WHERE $searchcondition
AND u.suspended = 0
AND u.id IN (SELECT nnn.id FROM ($esql) AS nnn WHERE nnn.id > 1)
$groupsql
AND u.id NOT IN (SELECT ba.userid FROM {booking_answers} ba WHERE ba.optionid = {$this->options['optionid']})";
list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
$order = ' ORDER BY ' . $sort;
if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql,
array_merge($searchparams, $params));
if ($potentialmemberscount > $this->maxusersperpage) {
return $this->too_many_results($search, $potentialmemberscount);
}
}
$availableusers = $DB->get_records_sql($fields . $sql . $order,
array_merge($searchparams, $params, $sortparams));
if (empty($availableusers)) {
return array();
}
if ($search) {
$groupname = get_string('enrolledusersmatching', 'enrol', $search);
} else {
$groupname = get_string('enrolledusers', 'enrol');
}
return array($groupname => $availableusers);
}
}
/**
* User selector control for removing booked users
*
* @package mod_booking
* @copyright 2013 David Bogner
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class booking_existing_user_selector extends booking_user_selector_base {
public $potentialusers;
public $options;
public function __construct($name, $options) {
$this->potentialusers = $options['potentialusers'];
$this->options = $options;
parent::__construct($name, $options);
}
/**
* Finds all booked users
*
* @param string $search
* @return array
*/
public function find_users($search) {
global $DB, $USER;
// Only active enrolled or everybody on the frontpage.
$fields = "SELECT " . $this->required_fields_sql("u");
$countfields = 'SELECT COUNT(1)';
list($searchcondition, $searchparams) = $this->search_sql($search, 'u');
list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
$order = ' ORDER BY ' . $sort;
if (!empty($this->potentialusers)) {
$potentialuserids = array_keys ($this->potentialusers);
list($subscriberssql, $subscribeparams) = $DB->get_in_or_equal($potentialuserids, SQL_PARAMS_NAMED, "in_");
} else {
return array();
}
$option = new stdClass();
$option->id = $this->options['optionid'];
$option->bookingid = $this->options['bookingid'];
if (booking_check_if_teacher($option) && !has_capability(
'mod/booking:readallinstitutionusers', $this->options['accesscontext'])) {
$institution = $DB->get_record('booking_options',
array('id' => $this->options['optionid']));
$searchparams['onlyinstitution'] = $institution->institution;
$searchcondition .= ' AND u.institution LIKE :onlyinstitution';
}
$sql = " FROM {user} u
WHERE u.id $subscriberssql
AND $searchcondition
";
if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, array_merge($subscribeparams, $searchparams));
if ($potentialmemberscount > $this->maxusersperpage) {
return $this->too_many_results($search, $potentialmemberscount);
}
}
$availableusers = $DB->get_records_sql($fields . $sql . $order,
array_merge($searchparams, $sortparams, $subscribeparams));
if (empty($availableusers)) {
return array();
}
return array(get_string("booked", 'booking') => $availableusers);
}
}
/**
* Outputs a confirm button on a separate page to confirm a booking.
*
* @param $optionid
* @param $user
* @param $cm
* @param $url
* @throws coding_exception
* @throws moodle_exception
*/
function booking_confirm_booking($optionid, $user, $cm, $url) {
global $OUTPUT;
echo $OUTPUT->header();
$option = new booking_option($cm->id, $optionid, array(), 0, 0, false);
$optionidarray['answer'] = $optionid;
$optionidarray['confirm'] = 1;
$optionidarray['sesskey'] = $user->sesskey;
$optionidarray['id'] = $cm->id;
$requestedcourse = "<br />" . $option->option->text;
if ($option->option->coursestarttime != 0) {
$requestedcourse .= "<br />" .
userdate($option->option->coursestarttime, get_string('strftimedatetime', 'langconfig')) . " - " .
userdate($option->option->courseendtime, get_string('strftimedatetime', 'langconfig'));
}
$message = "<h2>" . get_string('confirmbookingoffollowing', 'booking') . "</h2>" .
$requestedcourse;
$message .= "<p><b>" . get_string('agreetobookingpolicy', 'booking') . ":</b></p>";
$message .= "<p>" . format_text($option->booking->settings->bookingpolicy) . "<p>";
echo $OUTPUT->confirm($message, new moodle_url('/mod/booking/view.php', $optionidarray), $url);
echo $OUTPUT->footer();
}
/**
* Update start and enddate in booking_option when dates are set or deleted
* @param int $optionid
*/
function booking_updatestartenddate($optionid) {
global $DB;
// Bugfix: Only update start end date depending on session IF there actually are sessions.
if (booking_utils::booking_option_has_optiondates($optionid)) {
// Update start and end date of the option depending on the sessions.
$result = $DB->get_record_sql(
'SELECT MIN(coursestarttime) AS coursestarttime, MAX(courseendtime) AS courseendtime
FROM {booking_optiondates}
WHERE optionid = ?',
array($optionid));
$optionobj = new stdClass();
$optionobj->id = $optionid;
if (is_null($result->coursestarttime)) {
$optionobj->coursestarttime = 0;
$optionobj->courseendtime = 0;
} else {
$optionobj->coursestarttime = $result->coursestarttime;
$optionobj->courseendtime = $result->courseendtime;
}
$DB->update_record("booking_options", $optionobj);
}
}
/**
* Helper function to render custom fields data of an option date session.
* @param numeric $optiondateid the id of the option date for which the custom fields should be rendered
* @return string the rendered HTML of the session's custom fields
*/
function get_rendered_customfields($optiondateid) {
global $DB;
$customfieldshtml = ''; // The rendered HTML.
if ($customfields = $DB->get_records("booking_customfields", ["optiondateid" => $optiondateid])) {
foreach ($customfields as $customfield) {
$customfieldshtml .= '<p><i>' . $customfield->cfgname . ': </i>';
$customfieldshtml .= $customfield->value . '</p>';
}
}
return $customfieldshtml;
}
/**
* Helper function to render the full description (including custom fields) of option events or optiondate events.
* @param int $optionid
* @param int $cmid the course module id
* @param int $descriptionparam
* @param bool $forbookeduser
* @return string The rendered HTML of the full description.
*/
function get_rendered_eventdescription(int $optionid, int $cmid,
int $descriptionparam = DESCRIPTION_WEBSITE, bool $forbookeduser = false): string {
global $PAGE;
// We have the following differences:
// - Rendered live on the website (eg wihin a modal) -> use button.
// - Rendered in calendar event -> use link.php? link.
// - Rendered in ical file for mail -> use link.php? link.
$booking = singleton_service::get_instance_of_booking_by_cmid($cmid);
$data = new \mod_booking\output\bookingoption_description($booking, $optionid, null, $descriptionparam, true, $forbookeduser);
$output = $PAGE->get_renderer('mod_booking');
if ($descriptionparam == DESCRIPTION_ICAL) {
// If this is for ical.
return $output->render_bookingoption_description_ical($data);
} else if ($descriptionparam == DESCRIPTION_MAIL) {
// If this is used for a mail - placeholder {bookingdetails}.
return $output->render_bookingoption_description_mail($data);
} else if ($descriptionparam == DESCRIPTION_CALENDAR) {
// If this is used for an event.
return $output->render_bookingoption_description_event($data);
}
return $output->render_bookingoption_description($data);
}
/**
* Helper function to delete custom fields belonging to an option date.
* @param int $optiondateid id of the option date for which all custom fields will be deleted.
*/
function optiondate_deletecustomfields($optiondateid) {
global $DB;
// Delete all custom fields which belong to this optiondate.
$DB->delete_records("booking_customfields", array('optiondateid' => $optiondateid));
}
/**
* Helper function to duplicate custom fields belonging to an option date.
* @param int $optiondateid id of the option date for which all custom fields will be duplicated.
*/
function optiondate_duplicatecustomfields($oldoptiondateid, $newoptiondateid) {
global $DB;
// Duplicate all custom fields which belong to this optiondate.
$customfields = $DB->get_records("booking_customfields", array('optiondateid' => $oldoptiondateid));
foreach ($customfields as $customfield) {
$customfield->optiondateid = $newoptiondateid;
$DB->insert_record("booking_customfields", $customfield);
}
}
/**
* Helper function to update user calendar events after an option or optiondate (a session of a booking option) has been changed.
* @param stdClass $option
* @param stdClass $optiondate
* @param int $cmid
*/
function option_optiondate_update_event(stdClass $option, stdClass $optiondate = null, int $cmid) {
global $DB, $USER;
// We either do this for option or optiondate
// different way to retrieve the right events.
if ($optiondate) {
// Check if we have already associated userevents.
if (!isset($optiondate->eventid) || (!$event = $DB->get_record('event', ['id' => $optiondate->eventid]))) {
// If we don't find the event here, we might still be just switching to multisession.
// Let's create the event anew.
$bocreatedevent = bookingoptiondate_created::create(array('context' => context_module::instance($cmid),
'objectid' => $optiondate->id, 'userid' => $USER->id, 'other' => ['optionid' => $option->id]));
$bocreatedevent->trigger();
// We have to return false if we have switched from multisession to create the right events.
return false;
} else {
// Get all the userevents.
$sql = "SELECT e.* FROM {booking_userevents} ue
JOIN {event} e
ON ue.eventid = e.id
WHERE ue.optiondateid = :optiondateid";
$allevents = $DB->get_records_sql($sql, ['optiondateid' => $optiondate->id]);
// Use the optiondate as data object.
$data = $optiondate;
if ($event = $DB->get_record('event', ['id' => $optiondate->eventid])) {
if ($allevents && count($allevents) > 0) {
if ($event && isset($event->description)) {
$allevents[] = $event;
}
} else {
$allevents = [$event];
}
}
}
} else {
// Get all the userevents.
$sql = "SELECT e.* FROM {booking_userevents} ue
JOIN {event} e
ON ue.eventid = e.id
WHERE ue.optionid = :optionid";
$allevents = $DB->get_records_sql($sql, ['optionid' => $option->id]);
// Use the option as data object.
$data = $option;
if ($event = $DB->get_record('event', ['id' => $option->calendarid])) {
if ($allevents && count($allevents) > 0) {
if ($event && isset($event->description)) {
$allevents[] = $event;
}
} else {
$allevents = [$event];
}
}
}
// We use $data here for $option and $optiondate, the necessary keys are the same.
foreach ($allevents as $eventrecord) {
if ($eventrecord->eventtype == 'user') {
$eventrecord->description = get_rendered_eventdescription($option->id, $cmid, DESCRIPTION_CALENDAR, true);
} else {
$eventrecord->description = get_rendered_eventdescription($option->id, $cmid, DESCRIPTION_CALENDAR, false);
}
$eventrecord->name = $option->text;
$eventrecord->timestart = $data->coursestarttime;
$eventrecord->timeduration = $data->courseendtime - $data->coursestarttime;
$eventrecord->timesort = $data->coursestarttime;
if (!$DB->update_record('event', $eventrecord)) {
return false;
}
}
}
/**
* Get booking option status
*
* @param int $starttime
* @param int $endtime
* @return string
* @throws coding_exception
*/
function booking_getoptionstatus($starttime = 0, $endtime = 0) {
if ($starttime == 0 && $endtime == 0) {
return '';
} else if ($starttime < time() && $endtime > time()) {
return get_string('active', 'booking');
} else if ($endtime < time()) {
return get_string('terminated', 'booking');
} else if ($starttime > time()) {
return get_string('notstarted', 'booking');
}
return "";
}