forked from bostelm/moodle-mod_scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
studentview.controller.php
138 lines (111 loc) · 4.65 KB
/
studentview.controller.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
<?php
/**
* Controller for student view
*
* @package mod_scheduler
* @copyright 2015 Henning Bostelmann and others (see README.txt)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/mod/scheduler/mailtemplatelib.php');
$returnurl = new moodle_url('/mod/scheduler/view.php', array('id' => $cm->id));
/************************************************ Book a slot ************************************************/
if ($action == 'bookslot') {
require_sesskey();
require_capability('mod/scheduler:appoint', $context);
// Get the request parameters.
$slotid = required_param('slotid', PARAM_INT);
$slot = $scheduler->get_slot($slotid);
if (!$slot) {
throw new moodle_exception('error');
}
if (!$slot->is_in_bookable_period()) {
throw new moodle_exception('nopermissions');
}
$requiredcapacity = 1;
$userstobook = array($USER->id);
if ($appointgroup) {
$groupmembers = $scheduler->get_available_students($appointgroup);
$requiredcapacity = count($groupmembers);
$userstobook = array_keys($groupmembers);
}
$errormessage = '';
$bookinglimit = $scheduler->count_bookable_appointments($USER->id, false);
if ($bookinglimit == 0) {
$errormessage = get_string('selectedtoomany', 'scheduler', $bookinglimit);
}
if (!$errormessage) {
// Validate our user ids.
$existingstudents = array();
foreach ($slot->get_appointments() as $app) {
$existingstudents[] = $app->studentid;
}
$userstobook = array_diff($userstobook, $existingstudents);
$remaining = $slot->count_remaining_appointments();
// If the slot is already overcrowded...
if ($remaining >= 0 && $remaining < $requiredcapacity) {
if ($requiredcapacity > 1) {
$errormessage = get_string('notenoughplaces', 'scheduler');
} else {
$errormessage = get_string('slot_is_just_in_use', 'scheduler');
}
}
}
if ($errormessage) {
echo $output->header();
echo $output->box($errormessage, 'error');
echo $output->continue_button($returnurl);
echo $output->footer();
exit();
}
// Create new appointment and add it for each member of the group.
foreach ($userstobook as $studentid) {
$appointment = $slot->create_appointment();
$appointment->studentid = $studentid;
$appointment->attended = 0;
$appointment->timecreated = time();
$appointment->timemodified = time();
\mod_scheduler\event\booking_added::create_from_slot($slot)->trigger();
// Notify the teacher.
if ($scheduler->allownotifications) {
$student = $DB->get_record('user', array('id' => $appointment->studentid));
$teacher = $DB->get_record('user', array('id' => $slot->teacherid));
scheduler_messenger::send_slot_notification($slot, 'bookingnotification', 'applied',
$student, $teacher, $teacher, $student, $course);
}
}
$slot->save();
redirect($returnurl);
}
/******************************** Cancel a booking (for the current student or a group) ******************************/
if ($action == 'cancelbooking') {
require_sesskey();
require_capability('mod/scheduler:appoint', $context);
// Get the request parameters.
$slotid = required_param('slotid', PARAM_INT);
$slot = $scheduler->get_slot($slotid);
if (!$slot) {
throw new moodle_exception('error');
}
if (!$slot->is_in_bookable_period()) {
throw new moodle_exception('nopermissions');
}
$userstocancel = array($USER->id);
if ($appointgroup) {
$userstocancel = array_keys($scheduler->get_available_students($appointgroup));
}
foreach ($userstocancel as $userid) {
if ($appointment = $slot->get_student_appointment($userid)) {
$scheduler->delete_appointment($appointment->id);
// Notify the teacher.
if ($scheduler->allownotifications) {
$student = $DB->get_record('user', array('id' => $USER->id));
$teacher = $DB->get_record('user', array('id' => $slot->teacherid));
scheduler_messenger::send_slot_notification($slot, 'bookingnotification', 'cancelled',
$student, $teacher, $teacher, $student, $COURSE);
}
\mod_scheduler\event\booking_removed::create_from_slot($slot)->trigger();
}
}
redirect($returnurl);
}