-
Notifications
You must be signed in to change notification settings - Fork 8
/
extend.php
185 lines (164 loc) · 8.11 KB
/
extend.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
<?php
// This file is part of mod_checkmark 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/>.
/**
* view.php Prints the main view of a particular instance of checkmark
*
* @package mod_checkmark
* @author Philipp Hager
* @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
*/
require(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/mod/checkmark/locallib.php');
// We check that in detail afterwards!
require_login();
$id = required_param('id', PARAM_INT);
$type = required_param('type', PARAM_ALPHA);
$mode = optional_param('mode', \mod_checkmark\overrideform::ADD, PARAM_TEXT);
$confirm = optional_param('confirm', 0, PARAM_INT);
$return = optional_param('return', false, PARAM_RAW);
$return = !empty($return) ? urldecode($return) : (new moodle_url('/mod/checkmark/overrides.php',
['id' => $id, 'mode' => $type === \mod_checkmark\overrideform::USER ? 'user' : 'group']))->out();
$users = optional_param('users', false, PARAM_RAW);
try {
if (!in_array($type, [\mod_checkmark\overrideform::USER, \mod_checkmark\overrideform::GROUP])) {
throw new coding_exception('invalidformdata');
}
// Sets url with params and performs require_login!
$url = new moodle_url('/mod/checkmark/extend.php');
$url->param('id', $id);
$url->param('type', $type);
list($cm, $checkmark, $course) = \checkmark::init_checks($id, 0, $url);
$context = context_module::instance($id);
require_capability('mod/checkmark:manageoverrides', $context);
$customdata = [
'cm' => $cm,
'context' => $context,
'checkmark' => $checkmark,
'return' => $return,
'mode' => $mode
];
$stroverride = get_string('override', 'checkmark');
if ($mode === \mod_checkmark\overrideform::DELETE) {
$title = get_string('deletecheck', null, $stroverride);
} else {
$title = get_string('editoverride', 'checkmark');
}
$PAGE->set_url($url);
$PAGE->set_pagelayout('admin');
$PAGE->navbar->add($title);
$PAGE->set_title($title);
$PAGE->set_heading($course->fullname);
$form = new \mod_checkmark\overrideform($type, $url, $customdata);
if ($form->is_cancelled()) {
redirect($return);
} else if ($mode === \mod_checkmark\overrideform::DELETE && $confirm) {
$instance = new checkmark($cm->id, $checkmark, $cm, $course);
$instance->delete_override($users, $type);
redirect($return, "Entry deleted", null, \core\output\notification::NOTIFY_SUCCESS);
} else if ($mode === \mod_checkmark\overrideform::UP || $mode === \mod_checkmark\overrideform::DOWN) {
$instance = new checkmark($cm->id, $checkmark, $cm, $course);
$groups = json_decode(urldecode(required_param('users', PARAM_RAW)));
$groupidfrom = is_int($groups) ? $groups : $groups[0];
$instance->reorder_group_overrides($groupidfrom, $mode === \mod_checkmark\overrideform::DOWN);
redirect($return, "Entry swapped", null, \core\output\notification::NOTIFY_SUCCESS);
} else if ($data = $form->get_data()) {
$instance = new checkmark($cm->id, $checkmark, $cm, $course);
if ($type === \mod_checkmark\overrideform::GROUP) {
$data->userids = [];
if (!empty($data->groups)) {
$instance->override_dates($data->groups, $data->timeavailable, $data->timedue,
$data->cutoffdate, \mod_checkmark\overrideform::GROUP);
}
} else if (!empty($data->userids)) {
$instance->override_dates($data->userids, $data->timeavailable, $data->timedue, $data->cutoffdate);
}
if (!empty($data->override)) {
redirect($return, get_string('dates_overwritten', 'checkmark'), null, \core\output\notification::NOTIFY_SUCCESS);
}
\core\notification::add(get_string('dates_overwritten', 'checkmark'), \core\output\notification::NOTIFY_SUCCESS);
} else {
if (!empty($users)) {
$entities = json_decode(urldecode(required_param('users', PARAM_RAW)));
$firstentity = is_int($entities) ? $entities : $entities[0];
$data = array();
if ($mode == \mod_checkmark\overrideform::EDIT || $mode == \mod_checkmark\overrideform::COPY) {
$dates = array();
if ($type === \mod_checkmark\overrideform::USER) {
$dates = checkmark_get_overridden_dates($checkmark->id,
$firstentity);
} else {
$dates = checkmark_get_override_dates_for_group($checkmark->id, $firstentity);
}
// Insert date of checkmark if no override is present (indicated by null in table).
if ($dates->timeavailable === null) {
$dates->timeavailable = $checkmark->timeavailable;
}
if ($dates->timedue === null) {
$dates->timedue = $checkmark->timedue;
}
if ($dates->cutoffdate === null) {
$dates->cutoffdate = $checkmark->cutoffdate;
}
if ($dates) {
$data = array('timeavailable' => $dates->timeavailable, 'timedue' => $dates->timedue,
'cutoffdate' => $dates->cutoffdate);
}
}
if ($mode != \mod_checkmark\overrideform::COPY) {
if ($type === \mod_checkmark\overrideform::USER) {
$data['userids'] = $entities;
} else {
$data['groups'] = $entities;
}
}
}
$form->set_data($data);
}
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($checkmark->name, true, array('context' => $context)));
if ($mode != \mod_checkmark\overrideform::DELETE) {
$form->display();
} else {
if (!empty($users)) {
$entities = json_decode(urldecode(required_param('users', PARAM_RAW)));
$entities = is_int($entities) ? $entities : $entities[0];
$confirmurl = new moodle_url($url, array('id' => $id, 'type' => $type, 'users' => $entities,
'mode' => \mod_checkmark\overrideform::DELETE, 'confirm' => 1));
$cancelurl = new moodle_url('/mod/checkmark/overrides.php', array('id' => $id));
$confirmstr = null;
if ($type === \mod_checkmark\overrideform::USER) {
$namefields = get_all_user_name_fields(true);
$user = $DB->get_record('user', array('id' => $users),
'id, ' . $namefields);
$confirmstr = get_string('overridedeleteusersure', 'checkmark', fullname($user));
} else {
$groupname = groups_get_group_name($entities);
$confirmstr = get_string('overridedeletegroupsure', 'checkmark', $groupname);
}
echo $OUTPUT->confirm($confirmstr, $confirmurl, $cancelurl);
}
}
echo $OUTPUT->footer();
} catch (dml_exception $d) {
throw $d;
} catch (Throwable $t) {
redirect($return, $t->getFile().'#'.$t->getLine().': '.$t->getMessage().html_writer::empty_tag('br').
nl2br($t->getTraceAsString()), null, \core\output\notification::NOTIFY_ERROR);
} catch (\Exception $e) {
redirect($return, $e->getFile().'#'.$e->getLine().': '.$e->getMessage().html_writer::empty_tag('br').
nl2br($e->getTraceAsString()), null, \core\output\notification::NOTIFY_ERROR);
}