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   * Settings form for overrides in the lesson module.
  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  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir . '/formslib.php');
  29  require_once($CFG->dirroot . '/mod/lesson/mod_form.php');
  30  
  31  
  32  /**
  33   * Form for editing settings overrides.
  34   *
  35   * @copyright  2015 Jean-Michel Vedrine
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class lesson_override_form extends moodleform {
  39  
  40      /** @var object course module object. */
  41      protected $cm;
  42  
  43      /** @var object the lesson settings object. */
  44      protected $lesson;
  45  
  46      /** @var context the lesson context. */
  47      protected $context;
  48  
  49      /** @var bool editing group override (true) or user override (false). */
  50      protected $groupmode;
  51  
  52      /** @var int groupid, if provided. */
  53      protected $groupid;
  54  
  55      /** @var int userid, if provided. */
  56      protected $userid;
  57  
  58      /**
  59       * Constructor.
  60       * @param moodle_url $submiturl the form action URL.
  61       * @param object $cm course module object.
  62       * @param object $lesson the lesson settings object.
  63       * @param object $context the lesson context.
  64       * @param bool $groupmode editing group override (true) or user override (false).
  65       * @param object $override the override being edited, if it already exists.
  66       */
  67      public function __construct($submiturl, $cm, $lesson, $context, $groupmode, $override) {
  68  
  69          $this->cm = $cm;
  70          $this->lesson = $lesson;
  71          $this->context = $context;
  72          $this->groupmode = $groupmode;
  73          $this->groupid = empty($override->groupid) ? 0 : $override->groupid;
  74          $this->userid = empty($override->userid) ? 0 : $override->userid;
  75  
  76          parent::__construct($submiturl, null, 'post');
  77  
  78      }
  79  
  80      /**
  81       * Define this form - called by the parent constructor
  82       */
  83      protected function definition() {
  84          global $DB;
  85  
  86          $cm = $this->cm;
  87          $mform = $this->_form;
  88  
  89          $mform->addElement('header', 'override', get_string('override', 'lesson'));
  90  
  91          $lessongroupmode = groups_get_activity_groupmode($cm);
  92          $accessallgroups = ($lessongroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $this->context);
  93  
  94          if ($this->groupmode) {
  95              // Group override.
  96              if ($this->groupid) {
  97                  // There is already a groupid, so freeze the selector.
  98                  $groupchoices = array();
  99                  $groupchoices[$this->groupid] = groups_get_group_name($this->groupid);
 100                  $mform->addElement('select', 'groupid',
 101                          get_string('overridegroup', 'lesson'), $groupchoices);
 102                  $mform->freeze('groupid');
 103              } else {
 104                  // Prepare the list of groups.
 105                  // Only include the groups the current can access.
 106                  $groups = $accessallgroups ? groups_get_all_groups($cm->course) :  groups_get_activity_allowed_groups($cm);
 107                  if (empty($groups)) {
 108                      // Generate an error.
 109                      $link = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id));
 110                      print_error('groupsnone', 'lesson', $link);
 111                  }
 112  
 113                  $groupchoices = array();
 114                  foreach ($groups as $group) {
 115                      $groupchoices[$group->id] = $group->name;
 116                  }
 117                  unset($groups);
 118  
 119                  if (count($groupchoices) == 0) {
 120                      $groupchoices[0] = get_string('none');
 121                  }
 122  
 123                  $mform->addElement('select', 'groupid',
 124                          get_string('overridegroup', 'lesson'), $groupchoices);
 125                  $mform->addRule('groupid', get_string('required'), 'required', null, 'client');
 126              }
 127          } else {
 128              // User override.
 129              if ($this->userid) {
 130                  // There is already a userid, so freeze the selector.
 131                  $user = $DB->get_record('user', array('id' => $this->userid));
 132                  $userchoices = array();
 133                  $userchoices[$this->userid] = fullname($user);
 134                  $mform->addElement('select', 'userid',
 135                          get_string('overrideuser', 'lesson'), $userchoices);
 136                  $mform->freeze('userid');
 137              } else {
 138                  // Prepare the list of users.
 139                  $users = [];
 140                  list($sort) = users_order_by_sql('u');
 141  
 142                  // Get the list of appropriate users, depending on whether and how groups are used.
 143                  $userfieldsapi = \core_user\fields::for_name();
 144                  if ($accessallgroups) {
 145                      $users = get_enrolled_users($this->context, '', 0,
 146                              'u.id, u.email, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects, $sort);
 147                  } else if ($groups = groups_get_activity_allowed_groups($cm)) {
 148                      $enrolledjoin = get_enrolled_join($this->context, 'u.id');
 149                      $userfields = 'u.id, u.email, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects;
 150                      list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
 151                      $params = $enrolledjoin->params + $ingroupparams;
 152                      $sql = "SELECT $userfields
 153                                FROM {user} u
 154                                JOIN {groups_members} gm ON gm.userid = u.id
 155                                     {$enrolledjoin->joins}
 156                               WHERE gm.groupid $ingroupsql
 157                                     AND {$enrolledjoin->wheres}
 158                            ORDER BY $sort";
 159                      $users = $DB->get_records_sql($sql, $params);
 160                  }
 161  
 162                  // Filter users based on any fixed restrictions (groups, profile).
 163                  $info = new \core_availability\info_module($cm);
 164                  $users = $info->filter_user_list($users);
 165  
 166                  if (empty($users)) {
 167                      // Generate an error.
 168                      $link = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id));
 169                      print_error('usersnone', 'lesson', $link);
 170                  }
 171  
 172                  $userchoices = array();
 173                  // TODO Does not support custom user profile fields (MDL-70456).
 174                  $canviewemail = in_array('email', \core_user\fields::get_identity_fields($this->context, false));
 175                  foreach ($users as $id => $user) {
 176                      if (empty($invalidusers[$id]) || (!empty($override) &&
 177                              $id == $override->userid)) {
 178                          if ($canviewemail) {
 179                              $userchoices[$id] = fullname($user) . ', ' . $user->email;
 180                          } else {
 181                              $userchoices[$id] = fullname($user);
 182                          }
 183                      }
 184                  }
 185                  unset($users);
 186  
 187                  if (count($userchoices) == 0) {
 188                      $userchoices[0] = get_string('none');
 189                  }
 190                  $mform->addElement('searchableselector', 'userid',
 191                          get_string('overrideuser', 'lesson'), $userchoices);
 192                  $mform->addRule('userid', get_string('required'), 'required', null, 'client');
 193              }
 194          }
 195  
 196          // Password.
 197          // This field has to be above the date and timelimit fields,
 198          // otherwise browsers will clear it when those fields are changed.
 199          $mform->addElement('passwordunmask', 'password', get_string('usepassword', 'lesson'));
 200          $mform->setType('password', PARAM_TEXT);
 201          $mform->addHelpButton('password', 'usepassword', 'lesson');
 202          $mform->setDefault('password', $this->lesson->password);;
 203  
 204          // Open and close dates.
 205          $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional' => true));
 206          $mform->setDefault('available', $this->lesson->available);
 207  
 208          $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional' => true));
 209          $mform->setDefault('deadline', $this->lesson->deadline);
 210  
 211          // Lesson time limit.
 212          $mform->addElement('duration', 'timelimit',
 213                  get_string('timelimit', 'lesson'), array('optional' => true));
 214          if ($this->lesson->timelimit != 0) {
 215              $mform->setDefault('timelimit', 0);
 216          } else {
 217              $mform->setDefault('timelimit', $this->lesson->timelimit);
 218          }
 219  
 220          // Try a question again.
 221          $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
 222          $mform->addHelpButton('review', 'displayreview', 'lesson');
 223          $mform->setDefault('review', $this->lesson->review);
 224  
 225          // Number of attempts.
 226          $numbers = ['0' => get_string('unlimited')];
 227          for ($i = 10; $i > 0; $i--) {
 228              $numbers[$i] = $i;
 229          }
 230          $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
 231          $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson');
 232          $mform->setDefault('maxattempts', $this->lesson->maxattempts);
 233  
 234          // Retake allowed.
 235          $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson'));
 236          $mform->addHelpButton('retake', 'retakesallowed', 'lesson');
 237          $mform->setDefault('retake', $this->lesson->retake);
 238  
 239          // Submit buttons.
 240          $mform->addElement('submit', 'resetbutton',
 241                  get_string('reverttodefaults', 'lesson'));
 242  
 243          $buttonarray = array();
 244          $buttonarray[] = $mform->createElement('submit', 'submitbutton',
 245                  get_string('save', 'lesson'));
 246          $buttonarray[] = $mform->createElement('submit', 'againbutton',
 247                  get_string('saveoverrideandstay', 'lesson'));
 248          $buttonarray[] = $mform->createElement('cancel');
 249  
 250          $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
 251          $mform->closeHeaderBefore('buttonbar');
 252  
 253      }
 254  
 255      /**
 256       * Validate the submitted form data.
 257       *
 258       * @param array $data array of ("fieldname"=>value) of submitted data
 259       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 260       * @return array of "element_name"=>"error_description" if there are errors
 261       */
 262      public function validation($data, $files) {
 263          $errors = parent::validation($data, $files);
 264  
 265          $mform =& $this->_form;
 266          $lesson = $this->lesson;
 267  
 268          if ($mform->elementExists('userid')) {
 269              if (empty($data['userid'])) {
 270                  $errors['userid'] = get_string('required');
 271              }
 272          }
 273  
 274          if ($mform->elementExists('groupid')) {
 275              if (empty($data['groupid'])) {
 276                  $errors['groupid'] = get_string('required');
 277              }
 278          }
 279  
 280          // Ensure that the dates make sense.
 281          if (!empty($data['available']) && !empty($data['deadline'])) {
 282              if ($data['deadline'] < $data['available'] ) {
 283                  $errors['deadline'] = get_string('closebeforeopen', 'lesson');
 284              }
 285          }
 286  
 287          // Ensure that at least one lesson setting was changed.
 288          $changed = false;
 289          $keys = array('available', 'deadline', 'review', 'timelimit', 'maxattempts', 'retake', 'password');
 290          foreach ($keys as $key) {
 291              if ($data[$key] != $lesson->{$key}) {
 292                  $changed = true;
 293                  break;
 294              }
 295          }
 296  
 297          if (!$changed) {
 298              $errors['available'] = get_string('nooverridedata', 'lesson');
 299          }
 300  
 301          return $errors;
 302      }
 303  }