Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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