Merge branch 'MDL-33509-master' of git://github.com/mihailges/moodle
[moodle.git] / mod / assign / overrides.php
blob4a7c6894a7d470b707838ea48c777f7b33265f2a
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This page handles listing of assign overrides
20 * @package mod_assign
21 * @copyright 2016 Ilya Tregubov
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once(dirname(__FILE__) . '/../../config.php');
27 require_once($CFG->dirroot.'/mod/assign/lib.php');
28 require_once($CFG->dirroot.'/mod/assign/locallib.php');
29 require_once($CFG->dirroot.'/mod/assign/override_form.php');
32 $cmid = required_param('cmid', PARAM_INT);
33 $mode = optional_param('mode', '', PARAM_ALPHA); // One of 'user' or 'group', default is 'group'.
35 $action = optional_param('action', '', PARAM_ALPHA);
36 $redirect = $CFG->wwwroot.'/mod/assign/overrides.php?cmid=' . $cmid . '&amp;mode=group';
38 list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'assign');
39 $assign = $DB->get_record('assign', array('id' => $cm->instance), '*', MUST_EXIST);
41 $overridecountgroup = $DB->count_records('assign_overrides', array('userid' => null, 'assignid' => $assign->id));
43 // Get the course groups.
44 $groups = groups_get_all_groups($cm->course);
45 if ($groups === false) {
46 $groups = array();
49 // Default mode is "group", unless there are no groups.
50 if ($mode != "user" and $mode != "group") {
51 if (!empty($groups)) {
52 $mode = "group";
53 } else {
54 $mode = "user";
57 $groupmode = ($mode == "group");
59 $url = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id, 'mode' => $mode));
61 $PAGE->set_url($url);
63 require_login($course, false, $cm);
65 $context = context_module::instance($cm->id);
67 // Check the user has the required capabilities to list overrides.
68 require_capability('mod/assign:manageoverrides', $context);
70 if ($action == 'movegroupoverride') {
71 $id = required_param('id', PARAM_INT);
72 $dir = required_param('dir', PARAM_ALPHA);
74 if (confirm_sesskey()) {
75 move_group_override($id, $dir, $assign->id);
77 redirect($redirect);
80 // Display a list of overrides.
81 $PAGE->set_pagelayout('admin');
82 $PAGE->set_title(get_string('overrides', 'assign'));
83 $PAGE->set_heading($course->fullname);
84 echo $OUTPUT->header();
85 echo $OUTPUT->heading(format_string($assign->name, true, array('context' => $context)));
87 // Delete orphaned group overrides.
88 $sql = 'SELECT o.id
89 FROM {assign_overrides} o LEFT JOIN {groups} g
90 ON o.groupid = g.id
91 WHERE o.groupid IS NOT NULL
92 AND g.id IS NULL
93 AND o.assignid = ?';
94 $params = array($assign->id);
95 $orphaned = $DB->get_records_sql($sql, $params);
96 if (!empty($orphaned)) {
97 $DB->delete_records_list('assign_overrides', 'id', array_keys($orphaned));
100 // Fetch all overrides.
101 if ($groupmode) {
102 $colname = get_string('group');
103 $sql = 'SELECT o.*, g.name
104 FROM {assign_overrides} o
105 JOIN {groups} g ON o.groupid = g.id
106 WHERE o.assignid = :assignid
107 ORDER BY o.sortorder';
108 $params = array('assignid' => $assign->id);
109 } else {
110 $colname = get_string('user');
111 list($sort, $params) = users_order_by_sql('u');
112 $sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
113 FROM {assign_overrides} o
114 JOIN {user} u ON o.userid = u.id
115 WHERE o.assignid = :assignid
116 ORDER BY ' . $sort;
117 $params['assignid'] = $assign->id;
120 $overrides = $DB->get_records_sql($sql, $params);
122 // Initialise table.
123 $table = new html_table();
124 $table->headspan = array(1, 2, 1);
125 $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
126 $table->head = array(
127 $colname,
128 get_string('overrides', 'assign'),
129 get_string('action'),
132 $userurl = new moodle_url('/user/view.php', array());
133 $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
135 $overridedeleteurl = new moodle_url('/mod/assign/overridedelete.php');
136 $overrideediturl = new moodle_url('/mod/assign/overrideedit.php');
138 $hasinactive = false; // Whether there are any inactive overrides.
140 foreach ($overrides as $override) {
142 $fields = array();
143 $values = array();
144 $active = true;
146 // Check for inactive overrides.
147 if (!$groupmode) {
148 if (!is_enrolled($context, $override->userid)) {
149 // User not enrolled.
150 $active = false;
151 } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
152 // User cannot access the module.
153 $active = false;
157 // Format allowsubmissionsfromdate.
158 if (isset($override->allowsubmissionsfromdate)) {
159 $fields[] = get_string('open', 'assign');
160 $values[] = $override->allowsubmissionsfromdate > 0 ? userdate($override->allowsubmissionsfromdate) : get_string('noopen',
161 'assign');
164 // Format duedate.
165 if (isset($override->duedate)) {
166 $fields[] = get_string('duedate', 'assign');
167 $values[] = $override->duedate > 0 ? userdate($override->duedate) : get_string('noclose', 'assign');
170 // Format cutoffdate.
171 if (isset($override->cutoffdate)) {
172 $fields[] = get_string('cutoffdate', 'assign');
173 $values[] = $override->cutoffdate > 0 ? userdate($override->cutoffdate) : get_string('noclose', 'assign');
176 // Icons.
177 $iconstr = '';
179 if ($active) {
180 // Edit.
181 $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
182 $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
183 $OUTPUT->pix_icon('t/edit', get_string('edit')) . '</a> ';
184 // Duplicate.
185 $copyurlstr = $overrideediturl->out(true,
186 array('id' => $override->id, 'action' => 'duplicate'));
187 $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
188 $OUTPUT->pix_icon('t/copy', get_string('copy')) . '</a> ';
190 // Delete.
191 $deleteurlstr = $overridedeleteurl->out(true,
192 array('id' => $override->id, 'sesskey' => sesskey()));
193 $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
194 $OUTPUT->pix_icon('t/delete', get_string('delete')) . '</a> ';
196 if ($groupmode) {
197 $usergroupstr = '<a href="' . $groupurl->out(true,
198 array('group' => $override->groupid)) . '" >' . $override->name . '</a>';
200 // Move up.
201 if ($override->sortorder > 1) {
202 $iconstr .= '<a title="'.get_string('moveup').'" href="overrides.php?cmid=' . $cmid .
203 '&amp;id=' . $override->id .'&amp;action=movegroupoverride&amp;dir=up&amp;sesskey='.sesskey().'">' .
204 $OUTPUT->pix_icon('t/up', get_string('moveup')) . '</a> ';
205 } else {
206 $iconstr .= $OUTPUT->spacer() . ' ';
209 // Move down.
210 if ($override->sortorder < $overridecountgroup) {
211 $iconstr .= '<a title="'.get_string('movedown').'" href="overrides.php?cmid='.$cmid.
212 '&amp;id=' . $override->id . '&amp;action=movegroupoverride&amp;dir=down&amp;sesskey='.sesskey().'">' .
213 $OUTPUT->pix_icon('t/down', get_string('movedown')) . '</a> ';
214 } else {
215 $iconstr .= $OUTPUT->spacer() . ' ';
219 } else {
220 $usergroupstr = html_writer::link($userurl->out(false,
221 array('id' => $override->userid, 'course' => $course->id)),
222 fullname($override));
225 $class = '';
226 if (!$active) {
227 $class = "dimmed_text";
228 $usergroupstr .= '*';
229 $hasinactive = true;
232 $usergroupcell = new html_table_cell();
233 $usergroupcell->rowspan = count($fields);
234 $usergroupcell->text = $usergroupstr;
235 $actioncell = new html_table_cell();
236 $actioncell->rowspan = count($fields);
237 $actioncell->text = $iconstr;
239 for ($i = 0; $i < count($fields); ++$i) {
240 $row = new html_table_row();
241 $row->attributes['class'] = $class;
242 if ($i == 0) {
243 $row->cells[] = $usergroupcell;
245 $cell1 = new html_table_cell();
246 $cell1->text = $fields[$i];
247 $row->cells[] = $cell1;
248 $cell2 = new html_table_cell();
249 $cell2->text = $values[$i];
250 $row->cells[] = $cell2;
251 if ($i == 0) {
252 $row->cells[] = $actioncell;
254 $table->data[] = $row;
258 // Output the table and button.
259 echo html_writer::start_tag('div', array('id' => 'assignoverrides'));
260 if (count($table->data)) {
261 echo html_writer::table($table);
263 if ($hasinactive) {
264 echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'assign'), 'dimmed_text');
267 echo html_writer::start_tag('div', array('class' => 'buttons'));
268 $options = array();
269 if ($groupmode) {
270 if (empty($groups)) {
271 // There are no groups.
272 echo $OUTPUT->notification(get_string('groupsnone', 'assign'), 'error');
273 $options['disabled'] = true;
275 echo $OUTPUT->single_button($overrideediturl->out(true,
276 array('action' => 'addgroup', 'cmid' => $cm->id)),
277 get_string('addnewgroupoverride', 'assign'), 'post', $options);
278 } else {
279 $users = array();
280 // See if there are any users in the assign.
281 $users = get_enrolled_users($context);
282 $info = new \core_availability\info_module($cm);
283 $users = $info->filter_user_list($users);
285 if (empty($users)) {
286 // There are no users.
287 echo $OUTPUT->notification(get_string('usersnone', 'assign'), 'error');
288 $options['disabled'] = true;
290 echo $OUTPUT->single_button($overrideediturl->out(true,
291 array('action' => 'adduser', 'cmid' => $cm->id)),
292 get_string('addnewuseroverride', 'assign'), 'get', $options);
294 echo html_writer::end_tag('div');
295 echo html_writer::end_tag('div');
297 // Finish the page.
298 echo $OUTPUT->footer();