Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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