PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/assign/overrides.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 298 lines | 208 code | 45 blank | 45 comment | 39 complexity | 886a2aad4e7eb1459a8c8b0ac9c5ad00 MD5 | raw file
  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.
  13. //
  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/>.
  16. /**
  17. * This page handles listing of assign overrides
  18. *
  19. * @package mod_assign
  20. * @copyright 2016 Ilya Tregubov
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once(dirname(__FILE__) . '/../../config.php');
  24. require_once($CFG->dirroot.'/mod/assign/lib.php');
  25. require_once($CFG->dirroot.'/mod/assign/locallib.php');
  26. require_once($CFG->dirroot.'/mod/assign/override_form.php');
  27. $cmid = required_param('cmid', PARAM_INT);
  28. $mode = optional_param('mode', '', PARAM_ALPHA); // One of 'user' or 'group', default is 'group'.
  29. $action = optional_param('action', '', PARAM_ALPHA);
  30. $redirect = $CFG->wwwroot.'/mod/assign/overrides.php?cmid=' . $cmid . '&amp;mode=group';
  31. list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'assign');
  32. $assign = $DB->get_record('assign', array('id' => $cm->instance), '*', MUST_EXIST);
  33. $overridecountgroup = $DB->count_records('assign_overrides', array('userid' => null, 'assignid' => $assign->id));
  34. // Get the course groups.
  35. $groups = groups_get_all_groups($cm->course);
  36. if ($groups === false) {
  37. $groups = array();
  38. }
  39. // Default mode is "group", unless there are no groups.
  40. if ($mode != "user" and $mode != "group") {
  41. if (!empty($groups)) {
  42. $mode = "group";
  43. } else {
  44. $mode = "user";
  45. }
  46. }
  47. $groupmode = ($mode == "group");
  48. $url = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id, 'mode' => $mode));
  49. $PAGE->set_url($url);
  50. require_login($course, false, $cm);
  51. $context = context_module::instance($cm->id);
  52. // Check the user has the required capabilities to list overrides.
  53. require_capability('mod/assign:manageoverrides', $context);
  54. if ($action == 'movegroupoverride') {
  55. $id = required_param('id', PARAM_INT);
  56. $dir = required_param('dir', PARAM_ALPHA);
  57. if (confirm_sesskey()) {
  58. move_group_override($id, $dir, $assign->id);
  59. }
  60. redirect($redirect);
  61. }
  62. // Display a list of overrides.
  63. $PAGE->set_pagelayout('admin');
  64. $PAGE->set_title(get_string('overrides', 'assign'));
  65. $PAGE->set_heading($course->fullname);
  66. echo $OUTPUT->header();
  67. echo $OUTPUT->heading(format_string($assign->name, true, array('context' => $context)));
  68. // Delete orphaned group overrides.
  69. $sql = 'SELECT o.id
  70. FROM {assign_overrides} o LEFT JOIN {groups} g
  71. ON o.groupid = g.id
  72. WHERE o.groupid IS NOT NULL
  73. AND g.id IS NULL
  74. AND o.assignid = ?';
  75. $params = array($assign->id);
  76. $orphaned = $DB->get_records_sql($sql, $params);
  77. if (!empty($orphaned)) {
  78. $DB->delete_records_list('assign_overrides', 'id', array_keys($orphaned));
  79. }
  80. // Fetch all overrides.
  81. if ($groupmode) {
  82. $colname = get_string('group');
  83. $sql = 'SELECT o.*, g.name
  84. FROM {assign_overrides} o
  85. JOIN {groups} g ON o.groupid = g.id
  86. WHERE o.assignid = :assignid
  87. ORDER BY o.sortorder';
  88. $params = array('assignid' => $assign->id);
  89. } else {
  90. $colname = get_string('user');
  91. list($sort, $params) = users_order_by_sql('u');
  92. $sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
  93. FROM {assign_overrides} o
  94. JOIN {user} u ON o.userid = u.id
  95. WHERE o.assignid = :assignid
  96. ORDER BY ' . $sort;
  97. $params['assignid'] = $assign->id;
  98. }
  99. $overrides = $DB->get_records_sql($sql, $params);
  100. // Initialise table.
  101. $table = new html_table();
  102. $table->headspan = array(1, 2, 1);
  103. $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
  104. $table->head = array(
  105. $colname,
  106. get_string('overrides', 'assign'),
  107. get_string('action'),
  108. );
  109. $userurl = new moodle_url('/user/view.php', array());
  110. $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
  111. $overridedeleteurl = new moodle_url('/mod/assign/overridedelete.php');
  112. $overrideediturl = new moodle_url('/mod/assign/overrideedit.php');
  113. $hasinactive = false; // Whether there are any inactive overrides.
  114. foreach ($overrides as $override) {
  115. $fields = array();
  116. $values = array();
  117. $active = true;
  118. // Check for inactive overrides.
  119. if (!$groupmode) {
  120. if (!is_enrolled($context, $override->userid)) {
  121. // User not enrolled.
  122. $active = false;
  123. } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
  124. // User cannot access the module.
  125. $active = false;
  126. }
  127. }
  128. // Format allowsubmissionsfromdate.
  129. if (isset($override->allowsubmissionsfromdate)) {
  130. $fields[] = get_string('open', 'assign');
  131. $values[] = $override->allowsubmissionsfromdate > 0 ? userdate($override->allowsubmissionsfromdate) : get_string('noopen',
  132. 'assign');
  133. }
  134. // Format duedate.
  135. if (isset($override->duedate)) {
  136. $fields[] = get_string('duedate', 'assign');
  137. $values[] = $override->duedate > 0 ? userdate($override->duedate) : get_string('noclose', 'assign');
  138. }
  139. // Format cutoffdate.
  140. if (isset($override->cutoffdate)) {
  141. $fields[] = get_string('cutoffdate', 'assign');
  142. $values[] = $override->cutoffdate > 0 ? userdate($override->cutoffdate) : get_string('noclose', 'assign');
  143. }
  144. // Icons.
  145. $iconstr = '';
  146. if ($active) {
  147. // Edit.
  148. $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
  149. $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
  150. $OUTPUT->pix_icon('t/edit', get_string('edit')) . '</a> ';
  151. // Duplicate.
  152. $copyurlstr = $overrideediturl->out(true,
  153. array('id' => $override->id, 'action' => 'duplicate'));
  154. $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
  155. $OUTPUT->pix_icon('t/copy', get_string('copy')) . '</a> ';
  156. }
  157. // Delete.
  158. $deleteurlstr = $overridedeleteurl->out(true,
  159. array('id' => $override->id, 'sesskey' => sesskey()));
  160. $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
  161. $OUTPUT->pix_icon('t/delete', get_string('delete')) . '</a> ';
  162. if ($groupmode) {
  163. $usergroupstr = '<a href="' . $groupurl->out(true,
  164. array('group' => $override->groupid)) . '" >' . $override->name . '</a>';
  165. // Move up.
  166. if ($override->sortorder > 1) {
  167. $iconstr .= '<a title="'.get_string('moveup').'" href="overrides.php?cmid=' . $cmid .
  168. '&amp;id=' . $override->id .'&amp;action=movegroupoverride&amp;dir=up&amp;sesskey='.sesskey().'">' .
  169. $OUTPUT->pix_icon('t/up', get_string('moveup')) . '</a> ';
  170. } else {
  171. $iconstr .= $OUTPUT->spacer() . ' ';
  172. }
  173. // Move down.
  174. if ($override->sortorder < $overridecountgroup) {
  175. $iconstr .= '<a title="'.get_string('movedown').'" href="overrides.php?cmid='.$cmid.
  176. '&amp;id=' . $override->id . '&amp;action=movegroupoverride&amp;dir=down&amp;sesskey='.sesskey().'">' .
  177. $OUTPUT->pix_icon('t/down', get_string('movedown')) . '</a> ';
  178. } else {
  179. $iconstr .= $OUTPUT->spacer() . ' ';
  180. }
  181. } else {
  182. $usergroupstr = html_writer::link($userurl->out(false,
  183. array('id' => $override->userid, 'course' => $course->id)),
  184. fullname($override));
  185. }
  186. $class = '';
  187. if (!$active) {
  188. $class = "dimmed_text";
  189. $usergroupstr .= '*';
  190. $hasinactive = true;
  191. }
  192. $usergroupcell = new html_table_cell();
  193. $usergroupcell->rowspan = count($fields);
  194. $usergroupcell->text = $usergroupstr;
  195. $actioncell = new html_table_cell();
  196. $actioncell->rowspan = count($fields);
  197. $actioncell->text = $iconstr;
  198. for ($i = 0; $i < count($fields); ++$i) {
  199. $row = new html_table_row();
  200. $row->attributes['class'] = $class;
  201. if ($i == 0) {
  202. $row->cells[] = $usergroupcell;
  203. }
  204. $cell1 = new html_table_cell();
  205. $cell1->text = $fields[$i];
  206. $row->cells[] = $cell1;
  207. $cell2 = new html_table_cell();
  208. $cell2->text = $values[$i];
  209. $row->cells[] = $cell2;
  210. if ($i == 0) {
  211. $row->cells[] = $actioncell;
  212. }
  213. $table->data[] = $row;
  214. }
  215. }
  216. // Output the table and button.
  217. echo html_writer::start_tag('div', array('id' => 'assignoverrides'));
  218. if (count($table->data)) {
  219. echo html_writer::table($table);
  220. }
  221. if ($hasinactive) {
  222. echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'assign'), 'dimmed_text');
  223. }
  224. echo html_writer::start_tag('div', array('class' => 'buttons'));
  225. $options = array();
  226. if ($groupmode) {
  227. if (empty($groups)) {
  228. // There are no groups.
  229. echo $OUTPUT->notification(get_string('groupsnone', 'assign'), 'error');
  230. $options['disabled'] = true;
  231. }
  232. echo $OUTPUT->single_button($overrideediturl->out(true,
  233. array('action' => 'addgroup', 'cmid' => $cm->id)),
  234. get_string('addnewgroupoverride', 'assign'), 'post', $options);
  235. } else {
  236. $users = array();
  237. // See if there are any users in the assign.
  238. $users = get_enrolled_users($context);
  239. $info = new \core_availability\info_module($cm);
  240. $users = $info->filter_user_list($users);
  241. if (empty($users)) {
  242. // There are no users.
  243. echo $OUTPUT->notification(get_string('usersnone', 'assign'), 'error');
  244. $options['disabled'] = true;
  245. }
  246. echo $OUTPUT->single_button($overrideediturl->out(true,
  247. array('action' => 'adduser', 'cmid' => $cm->id)),
  248. get_string('addnewuseroverride', 'assign'), 'get', $options);
  249. }
  250. echo html_writer::end_tag('div');
  251. echo html_writer::end_tag('div');
  252. // Finish the page.
  253. echo $OUTPUT->footer();