[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/enrol/self/ -> locallib.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1