PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/overrides.php

http://github.com/moodle/moodle
PHP | 337 lines | 240 code | 49 blank | 48 comment | 45 complexity | a45ead882e47010354bbf714473dc976 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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 lesson overrides
  18. *
  19. * @package mod_lesson
  20. * @copyright 2015 Jean-Michel Vedrine
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once(__DIR__ . '/../../config.php');
  24. require_once($CFG->dirroot.'/mod/lesson/lib.php');
  25. require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  26. require_once($CFG->dirroot.'/mod/lesson/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. list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'lesson');
  30. $lesson = $DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST);
  31. require_login($course, false, $cm);
  32. $context = context_module::instance($cm->id);
  33. // Check the user has the required capabilities to list overrides.
  34. require_capability('mod/lesson:manageoverrides', $context);
  35. $lessongroupmode = groups_get_activity_groupmode($cm);
  36. $accessallgroups = ($lessongroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $context);
  37. // Get the course groups that the current user can access.
  38. $groups = $accessallgroups ? groups_get_all_groups($cm->course) : groups_get_activity_allowed_groups($cm);
  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/lesson/overrides.php', array('cmid' => $cm->id, 'mode' => $mode));
  49. $PAGE->set_url($url);
  50. // Display a list of overrides.
  51. $PAGE->set_pagelayout('admin');
  52. $PAGE->set_title(get_string('overrides', 'lesson'));
  53. $PAGE->set_heading($course->fullname);
  54. echo $OUTPUT->header();
  55. echo $OUTPUT->heading(format_string($lesson->name, true, array('context' => $context)));
  56. // Delete orphaned group overrides.
  57. $sql = 'SELECT o.id
  58. FROM {lesson_overrides} o
  59. LEFT JOIN {groups} g ON o.groupid = g.id
  60. WHERE o.groupid IS NOT NULL
  61. AND g.id IS NULL
  62. AND o.lessonid = ?';
  63. $params = array($lesson->id);
  64. $orphaned = $DB->get_records_sql($sql, $params);
  65. if (!empty($orphaned)) {
  66. $DB->delete_records_list('lesson_overrides', 'id', array_keys($orphaned));
  67. }
  68. $overrides = [];
  69. // Fetch all overrides.
  70. if ($groupmode) {
  71. $colname = get_string('group');
  72. // To filter the result by the list of groups that the current user has access to.
  73. if ($groups) {
  74. $params = ['lessonid' => $lesson->id];
  75. list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
  76. $params += $inparams;
  77. $sql = "SELECT o.*, g.name
  78. FROM {lesson_overrides} o
  79. JOIN {groups} g ON o.groupid = g.id
  80. WHERE o.lessonid = :lessonid AND g.id $insql
  81. ORDER BY g.name";
  82. $overrides = $DB->get_records_sql($sql, $params);
  83. }
  84. } else {
  85. $colname = get_string('user');
  86. list($sort, $params) = users_order_by_sql('u');
  87. $params['lessonid'] = $lesson->id;
  88. if ($accessallgroups) {
  89. $sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
  90. FROM {lesson_overrides} o
  91. JOIN {user} u ON o.userid = u.id
  92. WHERE o.lessonid = :lessonid
  93. ORDER BY ' . $sort;
  94. $overrides = $DB->get_records_sql($sql, $params);
  95. } else if ($groups) {
  96. list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
  97. $params += $inparams;
  98. $sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
  99. FROM {lesson_overrides} o
  100. JOIN {user} u ON o.userid = u.id
  101. JOIN {groups_members} gm ON u.id = gm.userid
  102. WHERE o.lessonid = :lessonid AND gm.groupid ' . $insql . '
  103. ORDER BY ' . $sort;
  104. $overrides = $DB->get_records_sql($sql, $params);
  105. }
  106. }
  107. $overrides = $DB->get_records_sql($sql, $params);
  108. // Initialise table.
  109. $table = new html_table();
  110. $table->headspan = array(1, 2, 1);
  111. $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
  112. $table->head = array(
  113. $colname,
  114. get_string('overrides', 'lesson'),
  115. get_string('action'),
  116. );
  117. $userurl = new moodle_url('/user/view.php', array());
  118. $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
  119. $overridedeleteurl = new moodle_url('/mod/lesson/overridedelete.php');
  120. $overrideediturl = new moodle_url('/mod/lesson/overrideedit.php');
  121. $hasinactive = false; // Whether there are any inactive overrides.
  122. foreach ($overrides as $override) {
  123. $fields = array();
  124. $values = array();
  125. $active = true;
  126. // Check for inactive overrides.
  127. if (!$groupmode) {
  128. if (!is_enrolled($context, $override->userid)) {
  129. // User not enrolled.
  130. $active = false;
  131. } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
  132. // User cannot access the module.
  133. $active = false;
  134. }
  135. }
  136. // Format available.
  137. if (isset($override->available)) {
  138. $fields[] = get_string('lessonopens', 'lesson');
  139. $values[] = $override->available > 0 ?
  140. userdate($override->available) : get_string('noopen', 'lesson');
  141. }
  142. // Format deadline.
  143. if (isset($override->deadline)) {
  144. $fields[] = get_string('lessoncloses', 'lesson');
  145. $values[] = $override->deadline > 0 ?
  146. userdate($override->deadline) : get_string('noclose', 'lesson');
  147. }
  148. // Format timelimit.
  149. if (isset($override->timelimit)) {
  150. $fields[] = get_string('timelimit', 'lesson');
  151. $values[] = $override->timelimit > 0 ?
  152. format_time($override->timelimit) : get_string('none', 'lesson');
  153. }
  154. // Format option to try a question again.
  155. if (isset($override->review)) {
  156. $fields[] = get_string('displayreview', 'lesson');
  157. $values[] = $override->review ?
  158. get_string('yes') : get_string('no');
  159. }
  160. // Format number of attempts.
  161. if (isset($override->maxattempts)) {
  162. $fields[] = get_string('maximumnumberofattempts', 'lesson');
  163. $values[] = $override->maxattempts > 0 ?
  164. $override->maxattempts : get_string('unlimited');
  165. }
  166. // Format retake allowed.
  167. if (isset($override->retake)) {
  168. $fields[] = get_string('retakesallowed', 'lesson');
  169. $values[] = $override->retake ?
  170. get_string('yes') : get_string('no');
  171. }
  172. // Format password.
  173. if (isset($override->password)) {
  174. $fields[] = get_string('usepassword', 'lesson');
  175. $values[] = $override->password !== '' ?
  176. get_string('enabled', 'lesson') : get_string('none', 'lesson');
  177. }
  178. // Icons.
  179. $iconstr = '';
  180. if ($active) {
  181. // Edit.
  182. $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
  183. $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
  184. $OUTPUT->pix_icon('t/edit', get_string('edit')) . '</a> ';
  185. // Duplicate.
  186. $copyurlstr = $overrideediturl->out(true,
  187. array('id' => $override->id, 'action' => 'duplicate'));
  188. $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
  189. $OUTPUT->pix_icon('t/copy', get_string('copy')) . '</a> ';
  190. }
  191. // Delete.
  192. $deleteurlstr = $overridedeleteurl->out(true,
  193. array('id' => $override->id, 'sesskey' => sesskey()));
  194. $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
  195. $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>';
  199. } else {
  200. $usergroupstr = '<a href="' . $userurl->out(true,
  201. array('id' => $override->userid)) . '" >' . fullname($override) . '</a>';
  202. }
  203. $class = '';
  204. if (!$active) {
  205. $class = "dimmed_text";
  206. $usergroupstr .= '*';
  207. $hasinactive = true;
  208. }
  209. $usergroupcell = new html_table_cell();
  210. $usergroupcell->rowspan = count($fields);
  211. $usergroupcell->text = $usergroupstr;
  212. $actioncell = new html_table_cell();
  213. $actioncell->rowspan = count($fields);
  214. $actioncell->text = $iconstr;
  215. for ($i = 0; $i < count($fields); ++$i) {
  216. $row = new html_table_row();
  217. $row->attributes['class'] = $class;
  218. if ($i == 0) {
  219. $row->cells[] = $usergroupcell;
  220. }
  221. $cell1 = new html_table_cell();
  222. $cell1->text = $fields[$i];
  223. $row->cells[] = $cell1;
  224. $cell2 = new html_table_cell();
  225. $cell2->text = $values[$i];
  226. $row->cells[] = $cell2;
  227. if ($i == 0) {
  228. $row->cells[] = $actioncell;
  229. }
  230. $table->data[] = $row;
  231. }
  232. }
  233. // Output the table and button.
  234. echo html_writer::start_tag('div', array('id' => 'lessonoverrides'));
  235. if (count($table->data)) {
  236. echo html_writer::table($table);
  237. }
  238. if ($hasinactive) {
  239. echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'lesson'), 'dimmed_text');
  240. }
  241. echo html_writer::start_tag('div', array('class' => 'buttons'));
  242. $options = array();
  243. if ($groupmode) {
  244. if (empty($groups)) {
  245. // There are no groups.
  246. echo $OUTPUT->notification(get_string('groupsnone', 'lesson'), 'error');
  247. $options['disabled'] = true;
  248. }
  249. echo $OUTPUT->single_button($overrideediturl->out(true,
  250. array('action' => 'addgroup', 'cmid' => $cm->id)),
  251. get_string('addnewgroupoverride', 'lesson'), 'post', $options);
  252. } else {
  253. $users = array();
  254. // See if there are any users in the lesson.
  255. if ($accessallgroups) {
  256. $users = get_enrolled_users($context, '', 0, 'u.id');
  257. $nousermessage = get_string('usersnone', 'lesson');
  258. } else if ($groups) {
  259. $enrolledjoin = get_enrolled_join($context, 'u.id');
  260. list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
  261. $params = $enrolledjoin->params + $ingroupparams;
  262. $sql = "SELECT u.id
  263. FROM {user} u
  264. JOIN {groups_members} gm ON gm.userid = u.id
  265. {$enrolledjoin->joins}
  266. WHERE gm.groupid $ingroupsql
  267. AND {$enrolledjoin->wheres}
  268. ORDER BY $sort";
  269. $users = $DB->get_records_sql($sql, $params);
  270. $nousermessage = get_string('usersnone', 'lesson');
  271. } else {
  272. $nousermessage = get_string('groupsnone', 'lesson');
  273. }
  274. $info = new \core_availability\info_module($cm);
  275. $users = $info->filter_user_list($users);
  276. if (empty($users)) {
  277. // There are no users.
  278. echo $OUTPUT->notification($nousermessage, 'error');
  279. $options['disabled'] = true;
  280. }
  281. echo $OUTPUT->single_button($overrideediturl->out(true,
  282. array('action' => 'adduser', 'cmid' => $cm->id)),
  283. get_string('addnewuseroverride', 'lesson'), 'get', $options);
  284. }
  285. echo html_writer::end_tag('div');
  286. echo html_writer::end_tag('div');
  287. // Finish the page.
  288. echo $OUTPUT->footer();