forked from academic-moodle-cooperation/moodle-mod_organizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slotlib.php
executable file
·473 lines (406 loc) · 15.7 KB
/
slotlib.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
<?php
// This file is part of mod_organizer for Moodle - http://moodle.org/
//
// It 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.
//
// It 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/>.
/**
* slotlib.php
*
* @package mod_organizer
* @author Andreas Hruska ([email protected])
* @author Katarzyna Potocka ([email protected])
* @author Thomas Niedermaier ([email protected])
* @author Andreas Windbichler
* @author Ivan Šakić
* @copyright 2014 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
function organizer_get_last_user_appointment($organizer, $userid = null, $mergegroupapps = true, $getevaluated = false) {
global $DB, $USER;
if ($userid == null) {
$userid = $USER->id;
}
if (is_number($organizer) && $organizer == intval($organizer)) {
$organizer = $DB->get_record('organizer', array('id' => $organizer));
}
$paramssql = array('userid' => $userid, 'organizerid' => $organizer->id);
$query = "SELECT a.* FROM {organizer_slot_appointments} a
INNER JOIN {organizer_slots} s ON a.slotid = s.id
WHERE s.organizerid = :organizerid AND a.userid = :userid" .
($getevaluated ? " AND a.attended IS NOT NULL " : " ") .
"ORDER BY a.id DESC";
$apps = $DB->get_records_sql($query, $paramssql);
$app = reset($apps);
if ($organizer->isgrouporganizer == ORGANIZER_GROUPMODE_EXISTINGGROUPS && $mergegroupapps && $app !== false) {
$paramssql = array('slotid' => $app->slotid, 'organizerid' => $organizer->id);
$query = "SELECT a.* FROM {organizer_slot_appointments} a
INNER JOIN {organizer_slots} s ON a.slotid = s.id
WHERE s.organizerid = :organizerid AND s.id = :slotid
ORDER BY a.id DESC";
$groupapps = $DB->get_records_sql($query, $paramssql);
$appcount = 0;
$someoneattended = false;
foreach ($groupapps as $groupapp) {
if ($groupapp->userid == $userid) {
$app = $groupapp;
}
if (isset($groupapp->attended)) {
$appcount++;
if ($groupapp->attended == 1) {
$someoneattended = true;
}
}
}
if ($app) {
$app->attended = ($appcount == count($groupapps)) ? $someoneattended : null;
}
}
return $app;
}
function organizer_get_all_user_appointments($organizer, $userid = null, $mergegroupapps = true) {
global $DB, $USER;
if ($userid == null) {
$userid = $USER->id;
}
if (is_number($organizer) && $organizer == intval($organizer)) {
$organizer = $DB->get_record('organizer', array('id' => $organizer));
}
$paramssql = array('userid' => $userid, 'organizerid' => $organizer->id);
$query = "SELECT a.* FROM {organizer_slot_appointments} a
INNER JOIN {organizer_slots} s ON a.slotid = s.id
WHERE s.organizerid = :organizerid AND a.userid = :userid
ORDER BY a.id DESC";
$apps = $DB->get_records_sql($query, $paramssql);
$app = reset($apps);
if ($organizer->isgrouporganizer == ORGANIZER_GROUPMODE_EXISTINGGROUPS && $mergegroupapps && $app !== false) {
$paramssql = array('slotid' => $app->slotid, 'organizerid' => $organizer->id);
$query = "SELECT a.* FROM {organizer_slot_appointments} a
INNER JOIN {organizer_slots} s ON a.slotid = s.id
WHERE s.organizerid = :organizerid AND s.id = :slotid
ORDER BY a.id DESC";
$groupapps = $DB->get_records_sql($query, $paramssql);
$appcount = 0;
$someoneattended = false;
foreach ($groupapps as $groupapp) {
if ($groupapp->userid == $userid) {
$app = $groupapp;
}
if (isset($groupapp->attended)) {
$appcount++;
if ($groupapp->attended == 1) {
$someoneattended = true;
}
}
}
if ($app) {
$app->attended = ($appcount == count($groupapps)) ? $someoneattended : null;
}
$apps = $groupapps;
}
return $apps;
}
function organizer_get_next_user_appointment($organizer, $userid = null) {
global $DB, $USER, $CFG;
if ($userid == null) {
$userid = $USER->id;
}
if (is_number($organizer) && $organizer == intval($organizer)) {
$organizer = $DB->get_record('organizer', array('id' => $organizer));
}
$todaymidnight = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
if ($organizer->isgrouporganizer == ORGANIZER_GROUPMODE_EXISTINGGROUPS) {
require_once($CFG->dirroot . '/mod/organizer/locallib.php');
if ($group = organizer_fetch_user_group($userid, $organizer->id)) {
$paramssql = array('organizerid' => $organizer->id, 'groupid' => $group->id, 'todaymidnight' => $todaymidnight);
$query = "SELECT a.*, s.starttime FROM {organizer_slot_appointments} a
INNER JOIN {organizer_slots} s ON a.slotid = s.id
WHERE s.organizerid = :organizerid AND a.groupid = :groupid AND s.starttime > :todaymidnight
ORDER BY s.starttime ASC";
$apps = $DB->get_records_sql($query, $paramssql);
$app = reset($apps);
} else {
$app = null;
}
} else {
$paramssql = array('organizerid' => $organizer->id, 'userid' => $userid, 'todaymidnight' => $todaymidnight);
$query = "SELECT a.*, s.starttime FROM {organizer_slot_appointments} a
INNER JOIN {organizer_slots} s ON a.slotid = s.id
WHERE s.organizerid = :organizerid AND a.userid = :userid AND s.starttime > :todaymidnight
ORDER BY s.starttime ASC";
$apps = $DB->get_records_sql($query, $paramssql);
$app = reset($apps);
}
return $app;
}
class organizer_slot
{
private $slot;
private $organizer;
private $apps;
private $queue;
private $queuegroup;
public function __construct($slot, $lazy = true) {
global $DB;
if (is_number($slot) && $slot == intval($slot)) {
$this->slot = $DB->get_record('organizer_slots', array('id' => $slot));
} else {
$this->slot = $slot;
if (!isset($this->slot->organizerid)) {
$this->slot->organizerid = $DB->get_field('organizer_slots', 'organizerid', array('id' => $slot->slotid));
}
if (!isset($this->slot->maxparticipants)) {
$this->slot->maxparticipants = $DB->get_field('organizer_slots', 'maxparticipants', array('id' => $slot->slotid));
}
}
foreach ((array) $this->slot as $key => $value) {
$this->$key = $value;
}
if (!$lazy) {
$this->load_organizer();
$this->load_appointments();
if ($this->organizer->queue) {
$this->load_queue();
$this->load_queue_group();
}
}
}
public function get_organizer() {
$this->load_organizer();
return $this->organizer;
}
public function get_slot() {
return $this->slot;
}
public function has_participants() {
$this->load_appointments();
return count($this->apps) != 0;
}
public function get_rel_deadline() {
$this->load_organizer();
return $this->organizer->relativedeadline;
}
public function get_abs_deadline() {
$this->load_organizer();
return $this->organizer->duedate;
}
public function is_upcoming() {
return $this->slot->starttime > time();
}
public function is_past_deadline() {
$deadline = $this->get_rel_deadline($this->slot);
return $this->slot->starttime <= $deadline + time();
}
public function is_past_due() {
return $this->slot->starttime <= time();
}
public function is_full() {
$this->load_organizer();
$this->load_appointments();
if ($this->organizer->isgrouporganizer == ORGANIZER_GROUPMODE_EXISTINGGROUPS) {
return count($this->apps) > 0;
} else {
return count($this->apps) >= $this->slot->maxparticipants;
}
}
public function is_available() {
return ($this->slot->availablefrom == 0) || ($this->slot->starttime - $this->slot->availablefrom <= time());
}
public function organizer_expired() {
$this->load_organizer();
return isset($this->organizer->duedate) && $this->organizer->duedate - time() < 0;
}
public function organizer_unavailable() {
$this->load_organizer();
return isset($this->organizer->allowregistrationsfromdate) && $this->organizer->allowregistrationsfromdate - time() > 0;
}
public function is_evaluated() {
$this->load_appointments();
foreach ($this->apps as $app) {
if (!isset($app->attended)) {
return false;
}
}
return count($this->apps) > 0;
}
public function organizer_user_has_access() {
$this->load_organizer();
global $DB;
if ($this->organizer->isgrouporganizer == ORGANIZER_GROUPMODE_EXISTINGGROUPS) {
$moduleid = $DB->get_field('modules', 'id', array('name' => 'organizer'));
$courseid = $DB->get_field(
'course_modules', 'course',
array('module' => $moduleid, 'instance' => $this->organizer->id)
);
$groups = groups_get_user_groups($courseid);
$groupingid = $DB->get_field(
'course_modules', 'groupingid',
array('module' => $moduleid, 'instance' => $this->organizer->id)
);
if (!isset($groups[$groupingid]) || !count($groups[$groupingid])) {
return false;
}
}
return true;
}
/**
* Waiting list
* Returns the position of a given user in this slot's queue starting at 1.
* Returns 0 if the user is not in the queue.
*
* @param int $userid The ID of the user.
*/
public function is_user_in_queue($userid) {
$result = 0;
$this->queue = null;
$this->load_organizer();
// The organizer should exists. Otherwise we are in a pathological state.
if ($this->organizer->queue) {
$this->load_queue();
// The queue might be empty though.
if ($this->queue) {
$position = 0;
foreach ($this->queue as $entry) {
$position++;
if ($entry->userid == $userid) {
$result = $position;
break;
}
}
}
}
return $result;
}
/**
* Waiting list
* Returns the position of a given group in this slot's queue starting at 1.
* Returns 0 if the group is not in the queue.
*
* @param Int $groupid The ID of the group.
* @return Int the group's position in queue
*/
public function is_group_in_queue($groupid = 0) {
$result = 0;
$position = 0;
if ($groupid == 0) {
$group = organizer_fetch_my_group();
$groupid = $group ? $group->id : 0;
}
$this->load_organizer();
// The organizer should exists. Otherwise we are in a pathological state.
if ($this->organizer->queue) {
$this->load_queue_group();
// The queue might be empty though.
if ($this->queuegroup) {
foreach ($this->queuegroup as $entry) {
$position++;
if ($entry->groupid == $groupid) {
$result = $position;
break;
}
}
}
}
return $result;
}
/**
* Waiting list
* Returns the next entry of the waiting queue for this slot if the queue is not empty,
* null otherwise.
*
* @return Ambigous <NULL, mixed>
*/
public function get_next_in_queue() {
$result = null;
$this->load_organizer();
if ($this->organizer->queue) {
$this->load_queue();
if ($this->queue) {
$result = array_shift($this->queue);
}
}
return $result;
}
public function get_next_in_queue_group() {
$result = null;
$this->load_organizer();
if ($this->organizer->queue) {
$this->load_queue_group();
if ($this->queuegroup) {
$result = array_shift($this->queuegroup);
}
}
return $result;
}
private function load_organizer() {
global $DB;
if (!$this->organizer) {
$this->organizer = $DB->get_record('organizer', array('id' => $this->slot->organizerid));
}
}
private function load_appointments() {
global $DB;
if (!$this->apps) {
$this->apps = $DB->get_records('organizer_slot_appointments', array('slotid' => $this->slot->id));
}
}
private function load_queue() {
global $DB;
if (!$this->queue) {
$this->queue = $DB->get_records('organizer_slot_queues', array('slotid' => $this->slot->id), 'id ASC');
}
}
private function load_queue_group() {
global $DB;
if (!$this->queuegroup) {
$sql = "SELECT q.groupid FROM (SELECT groupid, slotid FROM {organizer_slot_queues} ORDER BY id asc) q
WHERE q.slotid = :slotid
GROUP BY q.groupid";
$paramssql = array('slotid' => $this->slot->id);
$this->queuegroup = $DB->get_records_sql($sql, $paramssql);
}
}
}
function organizer_user_has_access($slotid) {
global $DB;
$slot = $DB->get_record('organizer_slots', array('id' => $slotid));
$moduleid = $DB->get_field('modules', 'id', array('name' => 'organizer'));
$organizer = $DB->get_record('organizer', array('id' => $slot->organizerid));
$courseid = $DB->get_field('course_modules', 'course', array('module' => $moduleid, 'instance' => $organizer->id));
$groups = groups_get_user_groups($courseid);
$groupingid = $DB->get_field(
'course_modules', 'groupingid',
array('module' => $moduleid, 'instance' => $organizer->id)
);
if (!isset($groups[$groupingid]) || !count($groups[$groupingid])) {
return false;
}
return true;
}
function organizer_get_slot_trainers($slotid, $withname = false) {
global $DB;
if ($withname) {
$paramssql = array('slotid' => $slotid);
$slotquery = 'SELECT u.id, u.firstname, u.lastname, u.email
FROM {organizer_slot_trainer} t
INNER JOIN {user} u ON t.trainerid = u.id
WHERE t.slotid = :slotid';
$trainers = $DB->get_records_sql($slotquery, $paramssql);
} else {
if ($trainers = $DB->get_fieldset_select(
'organizer_slot_trainer', 'trainerid', 'slotid = :slotid', array('slotid' => $slotid))) {
sort($trainers);
}
}
return $trainers;
}