Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 39 and 402]

   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   * Enrol users form.
  19   *
  20   * Simple form to search for users and add them using a manual enrolment to this course.
  21   *
  22   * @package enrol_manual
  23   * @copyright 2016 Damyon Wiese
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir.'/formslib.php');
  30  
  31  class enrol_manual_enrol_users_form extends moodleform {
  32  
  33      /**
  34       * Form definition.
  35       * @return void
  36       */
  37      public function definition() {
  38          global $PAGE, $DB, $CFG;
  39  
  40  
  41          require_once($CFG->dirroot . '/enrol/locallib.php');
  42  
  43          $context = $this->_customdata->context;
  44  
  45          // Get the course and enrolment instance.
  46          $coursecontext = $context->get_course_context();
  47          $course = $DB->get_record('course', ['id' => $coursecontext->instanceid]);
  48          $manager = new course_enrolment_manager($PAGE, $course);
  49  
  50          $instance = null;
  51          foreach ($manager->get_enrolment_instances() as $tempinstance) {
  52              if ($tempinstance->enrol == 'manual') {
  53                  if ($instance === null) {
  54                      $instance = $tempinstance;
  55                      break;
  56                  }
  57              }
  58          }
  59  
  60          $mform = $this->_form;
  61          $mform->setDisableShortforms();
  62          $mform->disable_form_change_checker();
  63          $periodmenu = enrol_get_period_list();
  64          // Work out the apropriate default settings.
  65          $defaultperiod = $instance->enrolperiod;
  66          if ($instance->enrolperiod > 0 && !isset($periodmenu[$instance->enrolperiod])) {
  67              $periodmenu[$instance->enrolperiod] = format_time($instance->enrolperiod);
  68          }
  69          if (empty($extendbase)) {
  70              if (!$extendbase = get_config('enrol_manual', 'enrolstart')) {
  71                  // Default to now if there is no system setting.
  72                  $extendbase = 4;
  73              }
  74          }
  75  
  76          // Build the list of options for the starting from dropdown.
  77          $now = time();
  78          $today = make_timestamp(date('Y', $now), date('m', $now), date('d', $now), 0, 0, 0);
  79          $dateformat = get_string('strftimedatefullshort');
  80  
  81          // Enrolment start.
  82          $basemenu = array();
  83          if ($course->startdate > 0) {
  84              $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $dateformat) . ')';
  85          }
  86          $basemenu[3] = get_string('today') . ' (' . userdate($today, $dateformat) . ')';
  87          $basemenu[4] = get_string('now', 'enrol_manual') . ' (' . userdate($now, get_string('strftimedatetimeshort')) . ')';
  88  
  89          $mform->addElement('header', 'main', get_string('enrolmentoptions', 'enrol'));
  90          $options = array(
  91              'ajax' => 'enrol_manual/form-potential-user-selector',
  92              'multiple' => true,
  93              'courseid' => $course->id,
  94              'enrolid' => $instance->id,
  95              'perpage' => $CFG->maxusersperpage,
  96              'userfields' => implode(',', \core_user\fields::get_identity_fields($context, true))
  97          );
  98          $mform->addElement('autocomplete', 'userlist', get_string('selectusers', 'enrol_manual'), array(), $options);
  99  
 100          // Confirm the user can search for cohorts before displaying select.
 101          if (has_capability('moodle/cohort:manage', $context) || has_capability('moodle/cohort:view', $context)) {
 102              // Check to ensure there is at least one visible cohort before displaying the select box.
 103              // Ideally it would be better to call external_api::call_external_function('core_cohort_search_cohorts')
 104              // (which is used to populate the select box) instead of duplicating logic but there is an issue with globals
 105              // being borked (in this case $PAGE) when combining the usage of fragments and call_external_function().
 106              require_once($CFG->dirroot . '/cohort/lib.php');
 107              $availablecohorts = cohort_get_cohorts($context->id, 0, 1, '');
 108              $availablecohorts = $availablecohorts['cohorts'];
 109              if (!($context instanceof context_system)) {
 110                  $availablecohorts = array_merge($availablecohorts,
 111                      cohort_get_available_cohorts($context, COHORT_ALL, 0, 1, ''));
 112              }
 113              if (!empty($availablecohorts)) {
 114                  $options = ['contextid' => $context->id, 'multiple' => true];
 115                  $mform->addElement('cohort', 'cohortlist', get_string('selectcohorts', 'enrol_manual'), $options);
 116              }
 117          }
 118  
 119          $roles = get_assignable_roles($context, ROLENAME_BOTH);
 120          $mform->addElement('select', 'roletoassign', get_string('assignrole', 'enrol_manual'), $roles);
 121          $mform->setDefault('roletoassign', $instance->roleid);
 122  
 123          $mform->addAdvancedStatusElement('main');
 124  
 125          $mform->addElement('checkbox', 'recovergrades', get_string('recovergrades', 'enrol'));
 126          $mform->setAdvanced('recovergrades');
 127          $mform->setDefault('recovergrades', $CFG->recovergradesdefault);
 128          $mform->addElement('select', 'startdate', get_string('startingfrom'), $basemenu);
 129          $mform->setDefault('startdate', $extendbase);
 130          $mform->setAdvanced('startdate');
 131          $mform->addElement('select', 'duration', get_string('enrolperiod', 'enrol'), $periodmenu);
 132          $mform->setDefault('duration', $defaultperiod);
 133          $mform->setAdvanced('duration');
 134          $mform->disabledIf('duration', 'timeend[enabled]', 'checked', 1);
 135          $mform->addElement('date_time_selector', 'timeend', get_string('enroltimeend', 'enrol'), ['optional' => true]);
 136          $mform->setAdvanced('timeend');
 137          $mform->addElement('hidden', 'id', $course->id);
 138          $mform->setType('id', PARAM_INT);
 139          $mform->addElement('hidden', 'action', 'enrol');
 140          $mform->setType('action', PARAM_ALPHA);
 141          $mform->addElement('hidden', 'enrolid', $instance->id);
 142          $mform->setType('enrolid', PARAM_INT);
 143      }
 144  
 145      /**
 146       * Validate the submitted form data.
 147       *
 148       * @param array $data array of ("fieldname"=>value) of submitted data
 149       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 150       * @return array of "element_name"=>"error_description" if there are errors,
 151       *         or an empty array if everything is OK (true allowed for backwards compatibility too).
 152       */
 153      public function validation($data, $files) {
 154          $errors = parent::validation($data, $files);
 155          if (!empty($data['startdate']) && !empty($data['timeend'])) {
 156              if ($data['startdate'] >= $data['timeend']) {
 157                  $errors['timeend'] = get_string('enroltimeendinvalid', 'enrol');
 158              }
 159          }
 160          return $errors;
 161      }
 162  }