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   * Self enrol plugin implementation.
  19   *
  20   * @package    enrol_self
  21   * @copyright  2010 Petr Skoda  {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once("$CFG->libdir/formslib.php");
  28  require_once($CFG->dirroot . '/enrol/locallib.php');
  29  
  30  /**
  31   * Check if the given password match a group enrolment key in the specified course.
  32   *
  33   * @param  int $courseid            course id
  34   * @param  string $enrolpassword    enrolment password
  35   * @return bool                     True if match
  36   * @since  Moodle 3.0
  37   */
  38  function enrol_self_check_group_enrolment_key($courseid, $enrolpassword) {
  39      global $DB;
  40  
  41      $found = false;
  42      $groups = $DB->get_records('groups', array('courseid' => $courseid), 'id ASC', 'id, enrolmentkey');
  43  
  44      foreach ($groups as $group) {
  45          if (empty($group->enrolmentkey)) {
  46              continue;
  47          }
  48          if ($group->enrolmentkey === $enrolpassword) {
  49              $found = true;
  50              break;
  51          }
  52      }
  53      return $found;
  54  }
  55  
  56  class enrol_self_enrol_form extends moodleform {
  57      protected $instance;
  58      protected $toomany = false;
  59  
  60      /**
  61       * Overriding this function to get unique form id for multiple self enrolments.
  62       *
  63       * @return string form identifier
  64       */
  65      protected function get_form_identifier() {
  66          $formid = $this->_customdata->id.'_'.get_class($this);
  67          return $formid;
  68      }
  69  
  70      public function definition() {
  71          global $USER, $OUTPUT, $CFG;
  72          $mform = $this->_form;
  73          $instance = $this->_customdata;
  74          $this->instance = $instance;
  75          $plugin = enrol_get_plugin('self');
  76  
  77          $heading = $plugin->get_instance_name($instance);
  78          $mform->addElement('header', 'selfheader', $heading);
  79  
  80          if ($instance->password) {
  81              // Change the id of self enrolment key input as there can be multiple self enrolment methods.
  82              $mform->addElement('password', 'enrolpassword', get_string('password', 'enrol_self'),
  83                      array('id' => 'enrolpassword_'.$instance->id));
  84              $context = context_course::instance($this->instance->courseid);
  85              $keyholders = get_users_by_capability($context, 'enrol/self:holdkey', user_picture::fields('u'));
  86              $keyholdercount = 0;
  87              foreach ($keyholders as $keyholder) {
  88                  $keyholdercount++;
  89                  if ($keyholdercount === 1) {
  90                      $mform->addElement('static', 'keyholder', '', get_string('keyholder', 'enrol_self'));
  91                  }
  92                  $keyholdercontext = context_user::instance($keyholder->id);
  93                  if ($USER->id == $keyholder->id || has_capability('moodle/user:viewdetails', context_system::instance()) ||
  94                          has_coursecontact_role($keyholder->id)) {
  95                      $profilelink = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $keyholder->id . '&amp;course=' .
  96                      $this->instance->courseid . '">' . fullname($keyholder) . '</a>';
  97                  } else {
  98                      $profilelink = fullname($keyholder);
  99                  }
 100                  $profilepic = $OUTPUT->user_picture($keyholder, array('size' => 35, 'courseid' => $this->instance->courseid));
 101                  $mform->addElement('static', 'keyholder'.$keyholdercount, '', $profilepic . $profilelink);
 102              }
 103  
 104          } else {
 105              $mform->addElement('static', 'nokey', '', get_string('nopassword', 'enrol_self'));
 106          }
 107  
 108          $this->add_action_buttons(false, get_string('enrolme', 'enrol_self'));
 109  
 110          $mform->addElement('hidden', 'id');
 111          $mform->setType('id', PARAM_INT);
 112          $mform->setDefault('id', $instance->courseid);
 113  
 114          $mform->addElement('hidden', 'instance');
 115          $mform->setType('instance', PARAM_INT);
 116          $mform->setDefault('instance', $instance->id);
 117      }
 118  
 119      public function validation($data, $files) {
 120          global $DB, $CFG;
 121  
 122          $errors = parent::validation($data, $files);
 123          $instance = $this->instance;
 124  
 125          if ($this->toomany) {
 126              $errors['notice'] = get_string('error');
 127              return $errors;
 128          }
 129  
 130          if ($instance->password) {
 131              if ($data['enrolpassword'] !== $instance->password) {
 132                  if ($instance->customint1) {
 133                      // Check group enrolment key.
 134                      if (!enrol_self_check_group_enrolment_key($instance->courseid, $data['enrolpassword'])) {
 135                          // We can not hint because there are probably multiple passwords.
 136                          $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
 137                      }
 138  
 139                  } else {
 140                      $plugin = enrol_get_plugin('self');
 141                      if ($plugin->get_config('showhint')) {
 142                          $hint = core_text::substr($instance->password, 0, 1);
 143                          $errors['enrolpassword'] = get_string('passwordinvalidhint', 'enrol_self', $hint);
 144                      } else {
 145                          $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
 146                      }
 147                  }
 148              }
 149          }
 150  
 151          return $errors;
 152      }
 153  }