-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudentview.controller.php
More file actions
171 lines (153 loc) · 5.77 KB
/
studentview.controller.php
File metadata and controls
171 lines (153 loc) · 5.77 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
<?php
/**
* Controller for student view
*
* @package mod
* @subpackage simplescheduler
* @copyright 2013 Nathan White 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/simplescheduler/mailtemplatelib.php');
/************************************************ Saving choice ************************************************/
if ($action == 'savechoice') {
$slot_id_array = NULL;
if ($simplescheduler->simpleschedulermode == 'multi')
{
$slot_id_array_raw = optional_param_array('slotid', '', PARAM_INT);
}
else
{
$slot_id_array_raw[] = optional_param('slotid', '', PARAM_INT);
}
if (!empty($slot_id_array_raw))
{
foreach ($slot_id_array_raw as $k=>$v)
{
if (!empty($v))
{
$slot_id_array_request[] = $v;
}
}
}
else $slot_id_array_request = false;
$appointgroup = optional_param('appointgroup', 0, PARAM_INT);
// $notes = optional_param('notes', '', PARAM_TEXT);
//if (!$slot_id_array_request) {
// notice(get_string('notselected', 'simplescheduler'), "view.php?id={$cm->id}");
//}
// validate our slot ids
if (!empty($slot_id_array_request))
{
foreach ($slot_id_array_request as $index => $slotid)
{
if (!$slot = $DB->get_record('simplescheduler_slots', array('id' => $slotid))) {
print_error('errorinvalidslot', 'simplescheduler');
}
$available = simplescheduler_get_appointments($slotid);
$consumed = ($available) ? count($available) : 0 ;
$users_for_slot = simplescheduler_get_appointed($slotid);
$already_signed_up = (isset($users_for_slot[$USER->id]));
if (!$already_signed_up)
{
// if slot is already overcrowded
if ($slot->exclusivity > 0 && ($slot->exclusivity <= $consumed)) {
if ($updating = $DB->count_records('simplescheduler_appointment', array('slotid' => $slot->id, 'studentid' => $USER->id))) {
$message = get_string('alreadyappointed', 'simplescheduler');
} else {
$message = get_string('slot_is_just_in_use', 'simplescheduler');
}
echo $OUTPUT->box_start('error');
echo $message;
echo $OUTPUT->continue_button("{$CFG->wwwroot}/mod/simplescheduler/view.php?id={$cm->id}");
echo $OUTPUT->box_end();
echo $OUTPUT->footer($course);
exit();
}
$slot_id_array[$index] = $slotid;
}
$slot_id_array_validated[$index] = $slotid;
}
}
/// If we are scheduling a full group we must discard all pending appointments of other participants of the scheduled group
/// just add the list of other students for searching slots to delete
if ($appointgroup){
if (!function_exists('build_navigation')){
// we are still in 1.8
$oldslotownersarray = groups_get_members($appointgroup, 'student');
} else {
// we are still in 1.8
$oldslotownersarray = groups_get_members($appointgroup);
}
// special hack for 1.8 / 1.9 compatibility for groups_get_members()
foreach($oldslotownersarray as $oldslotownermember){
if (is_numeric($oldslotownermember)){
// we are in 1.8
if (has_capability("mod/simplescheduler:appoint", $context, $oldslotownermember)){
$oldslotowners[] = $oldslotownermember;
}
} else {
// we are in 1.9
if (has_capability("mod/simplescheduler:appoint", $context, $oldslotownermember->id)){
$oldslotowners[] = $oldslotownermember->id;
}
}
}
} else {
// single user appointment : get current user in
$oldslotowners[] = $USER->id;
}
$oldslotownerlist = implode("','", $oldslotowners);
// cleans up future slots that are no longer selected
$sql = "
SELECT
s.*,
a.id as appointmentid,
a.studentid as studentid
FROM
{simplescheduler_slots} AS s,
{simplescheduler_appointment} AS a
WHERE
s.id = a.slotid AND
s.simpleschedulerid = '{$simplescheduler->id}' AND
a.studentid IN ('$oldslotownerlist')
";
if (!empty($slot_id_array_validated))
{
$sql .= " AND a.slotid NOT IN (" . implode(",", $slot_id_array_validated) . ")";
}
$sql .= " AND s.starttime > ".time(); // only mark future ones for deletion
if ($oldslots = $DB->get_records_sql($sql))
{
foreach($oldslots as $id => $slot) {
simplescheduler_student_revoke_appointment($id, $slot->studentid);
}
}
if (!empty($slot_id_array))
{
foreach ($slot_id_array as $slotid)
{
/// create new appointment and add it for each member of the group
foreach($oldslotowners as $studentid) {
simplescheduler_student_appoint_student($slotid, $studentid);
}
}
}
}
// ************************************ Disengage alone from the slot ******************************* /
if ($action == 'disengage') {
$where = 'studentid = :studentid AND ' .
'EXISTS(SELECT 1 FROM {simplescheduler_slots} sl WHERE sl.id = slotid AND sl.simpleschedulerid = :simplescheduler )';
$params = array('simplescheduler'=>$simplescheduler->id, 'studentid'=>$USER->id);
$appointments = $DB->get_records_select('simplescheduler_appointment', $where, $params);
if ($appointments) {
foreach($appointments as $appointment) {
$slot = $DB->get_record('simplescheduler_slots', array('id' => $appointment->slotid));
if ($slot->starttime > time()) // only modify future slots
{
simplescheduler_student_revoke_appointment($slot->id, $USER->id);
}
}
}
}
?>