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 lesson overrides
  19   *
  20   * @package    mod_lesson
  21   * @copyright  2015 Jean-Michel Vedrine
  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/lesson/lib.php');
  28  require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  29  require_once($CFG->dirroot.'/mod/lesson/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, 'lesson');
  36  $lesson = $DB->get_record('lesson', 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/lesson:manageoverrides', $context);
  44  
  45  $lessongroupmode = groups_get_activity_groupmode($cm);
  46  $accessallgroups = ($lessongroupmode == 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/lesson/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', 'lesson'));
  68  $PAGE->set_heading($course->fullname);
  69  echo $OUTPUT->header();
  70  echo $OUTPUT->heading(format_string($lesson->name, true, array('context' => $context)));
  71  
  72  // Delete orphaned group overrides.
  73  $sql = 'SELECT o.id
  74            FROM {lesson_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.lessonid = ?';
  79  $params = array($lesson->id);
  80  $orphaned = $DB->get_records_sql($sql, $params);
  81  if (!empty($orphaned)) {
  82      $DB->delete_records_list('lesson_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 = ['lessonid' => $lesson->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 {lesson_overrides} o
  98                    JOIN {groups} g ON o.groupid = g.id
  99                   WHERE o.lessonid = :lessonid 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['lessonid'] = $lesson->id;
 108  
 109      if ($accessallgroups) {
 110          $sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
 111                    FROM {lesson_overrides} o
 112                    JOIN {user} u ON o.userid = u.id
 113                   WHERE o.lessonid = :lessonid
 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 {lesson_overrides} o
 123                    JOIN {user} u ON o.userid = u.id
 124                    JOIN {groups_members} gm ON u.id = gm.userid
 125                   WHERE o.lessonid = :lessonid AND gm.groupid ' . $insql . '
 126                ORDER BY ' . $sort;
 127  
 128          $overrides = $DB->get_records_sql($sql, $params);
 129      }
 130  }
 131  
 132  $overrides = $DB->get_records_sql($sql, $params);
 133  
 134  // Initialise table.
 135  $table = new html_table();
 136  $table->headspan = array(1, 2, 1);
 137  $table->colclasses = array('colname', 'colsetting', 'colvalue', 'colaction');
 138  $table->head = array(
 139          $colname,
 140          get_string('overrides', 'lesson'),
 141          get_string('action'),
 142  );
 143  
 144  $userurl = new moodle_url('/user/view.php', array());
 145  $groupurl = new moodle_url('/group/overview.php', array('id' => $cm->course));
 146  
 147  $overridedeleteurl = new moodle_url('/mod/lesson/overridedelete.php');
 148  $overrideediturl = new moodle_url('/mod/lesson/overrideedit.php');
 149  
 150  $hasinactive = false; // Whether there are any inactive overrides.
 151  
 152  foreach ($overrides as $override) {
 153  
 154      $fields = array();
 155      $values = array();
 156      $active = true;
 157  
 158      // Check for inactive overrides.
 159      if (!$groupmode) {
 160          if (!is_enrolled($context, $override->userid)) {
 161              // User not enrolled.
 162              $active = false;
 163          } else if (!\core_availability\info_module::is_user_visible($cm, $override->userid)) {
 164              // User cannot access the module.
 165              $active = false;
 166          }
 167      }
 168  
 169      // Format available.
 170      if (isset($override->available)) {
 171          $fields[] = get_string('lessonopens', 'lesson');
 172          $values[] = $override->available > 0 ?
 173                  userdate($override->available) : get_string('noopen', 'lesson');
 174      }
 175  
 176      // Format deadline.
 177      if (isset($override->deadline)) {
 178          $fields[] = get_string('lessoncloses', 'lesson');
 179          $values[] = $override->deadline > 0 ?
 180                  userdate($override->deadline) : get_string('noclose', 'lesson');
 181      }
 182  
 183      // Format timelimit.
 184      if (isset($override->timelimit)) {
 185          $fields[] = get_string('timelimit', 'lesson');
 186          $values[] = $override->timelimit > 0 ?
 187                  format_time($override->timelimit) : get_string('none', 'lesson');
 188      }
 189  
 190      // Format option to try a question again.
 191      if (isset($override->review)) {
 192          $fields[] = get_string('displayreview', 'lesson');
 193          $values[] = $override->review ?
 194                  get_string('yes') : get_string('no');
 195      }
 196  
 197      // Format number of attempts.
 198      if (isset($override->maxattempts)) {
 199          $fields[] = get_string('maximumnumberofattempts', 'lesson');
 200          $values[] = $override->maxattempts > 0 ?
 201                  $override->maxattempts : get_string('unlimited');
 202      }
 203  
 204      // Format retake allowed.
 205      if (isset($override->retake)) {
 206          $fields[] = get_string('retakesallowed', 'lesson');
 207          $values[] = $override->retake ?
 208                  get_string('yes') : get_string('no');
 209      }
 210  
 211      // Format password.
 212      if (isset($override->password)) {
 213          $fields[] = get_string('usepassword', 'lesson');
 214          $values[] = $override->password !== '' ?
 215                  get_string('enabled', 'lesson') : get_string('none', 'lesson');
 216      }
 217  
 218      // Icons.
 219      $iconstr = '';
 220  
 221      // Edit.
 222      $editurlstr = $overrideediturl->out(true, array('id' => $override->id));
 223      $iconstr = '<a title="' . get_string('edit') . '" href="'. $editurlstr . '">' .
 224              $OUTPUT->pix_icon('t/edit', get_string('edit')) . '</a> ';
 225      // Duplicate.
 226      $copyurlstr = $overrideediturl->out(true,
 227              array('id' => $override->id, 'action' => 'duplicate'));
 228      $iconstr .= '<a title="' . get_string('copy') . '" href="' . $copyurlstr . '">' .
 229              $OUTPUT->pix_icon('t/copy', get_string('copy')) . '</a> ';
 230      // Delete.
 231      $deleteurlstr = $overridedeleteurl->out(true,
 232              array('id' => $override->id, 'sesskey' => sesskey()));
 233      $iconstr .= '<a title="' . get_string('delete') . '" href="' . $deleteurlstr . '">' .
 234                  $OUTPUT->pix_icon('t/delete', get_string('delete')) . '</a> ';
 235  
 236      if ($groupmode) {
 237          $usergroupstr = '<a href="' . $groupurl->out(true,
 238                  array('group' => $override->groupid)) . '" >' . $override->name . '</a>';
 239      } else {
 240          $usergroupstr = '<a href="' . $userurl->out(true,
 241                  array('id' => $override->userid)) . '" >' . fullname($override) . '</a>';
 242      }
 243  
 244      $class = '';
 245      if (!$active) {
 246          $class = "dimmed_text";
 247          $usergroupstr .= '*';
 248          $hasinactive = true;
 249      }
 250  
 251      $usergroupcell = new html_table_cell();
 252      $usergroupcell->rowspan = count($fields);
 253      $usergroupcell->text = $usergroupstr;
 254      $actioncell = new html_table_cell();
 255      $actioncell->rowspan = count($fields);
 256      $actioncell->text = $iconstr;
 257  
 258      for ($i = 0; $i < count($fields); ++$i) {
 259          $row = new html_table_row();
 260          $row->attributes['class'] = $class;
 261          if ($i == 0) {
 262              $row->cells[] = $usergroupcell;
 263          }
 264          $cell1 = new html_table_cell();
 265          $cell1->text = $fields[$i];
 266          $row->cells[] = $cell1;
 267          $cell2 = new html_table_cell();
 268          $cell2->text = $values[$i];
 269          $row->cells[] = $cell2;
 270          if ($i == 0) {
 271              $row->cells[] = $actioncell;
 272          }
 273          $table->data[] = $row;
 274      }
 275  }
 276  
 277  // Output the table and button.
 278  echo html_writer::start_tag('div', array('id' => 'lessonoverrides'));
 279  if (count($table->data)) {
 280      echo html_writer::table($table);
 281  }
 282  if ($hasinactive) {
 283      echo $OUTPUT->notification(get_string('inactiveoverridehelp', 'lesson'), 'dimmed_text');
 284  }
 285  
 286  echo html_writer::start_tag('div', array('class' => 'buttons'));
 287  $options = array();
 288  if ($groupmode) {
 289      if (empty($groups)) {
 290          // There are no groups.
 291          echo $OUTPUT->notification(get_string('groupsnone', 'lesson'), 'error');
 292          $options['disabled'] = true;
 293      }
 294      echo $OUTPUT->single_button($overrideediturl->out(true,
 295              array('action' => 'addgroup', 'cmid' => $cm->id)),
 296              get_string('addnewgroupoverride', 'lesson'), 'post', $options);
 297  } else {
 298      $users = array();
 299      // See if there are any users in the lesson.
 300      if ($accessallgroups) {
 301          $users = get_enrolled_users($context, '', 0, 'u.id');
 302          $nousermessage = get_string('usersnone', 'lesson');
 303      } else if ($groups) {
 304          $enrolledjoin = get_enrolled_join($context, 'u.id');
 305          list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
 306          $params = $enrolledjoin->params + $ingroupparams;
 307          $sql = "SELECT u.id
 308                    FROM {user} u
 309                    JOIN {groups_members} gm ON gm.userid = u.id
 310                         {$enrolledjoin->joins}
 311                   WHERE gm.groupid $ingroupsql
 312                         AND {$enrolledjoin->wheres}
 313                ORDER BY $sort";
 314          $users = $DB->get_records_sql($sql, $params);
 315          $nousermessage = get_string('usersnone', 'lesson');
 316      } else {
 317          $nousermessage = get_string('groupsnone', 'lesson');
 318      }
 319      $info = new \core_availability\info_module($cm);
 320      $users = $info->filter_user_list($users);
 321  
 322      if (empty($users)) {
 323          // There are no users.
 324          echo $OUTPUT->notification($nousermessage, 'error');
 325          $options['disabled'] = true;
 326      }
 327      echo $OUTPUT->single_button($overrideediturl->out(true,
 328              array('action' => 'adduser', 'cmid' => $cm->id)),
 329              get_string('addnewuseroverride', 'lesson'), 'get', $options);
 330  }
 331  echo html_writer::end_tag('div');
 332  echo html_writer::end_tag('div');
 333  
 334  // Finish the page.
 335  echo $OUTPUT->footer();