Merge branch 'MDL-72672-310' of https://github.com/junpataleta/moodle into MOODLE_310...
[moodle.git] / enrol / self / locallib.php
blob850555e2990a7b324c5abcd8fb05b175c1653ed5
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.
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/>.
17 /**
18 * Self enrol plugin implementation.
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
25 defined('MOODLE_INTERNAL') || die();
27 require_once("$CFG->libdir/formslib.php");
28 require_once($CFG->dirroot . '/enrol/locallib.php');
30 /**
31 * Check if the given password match a group enrolment key in the specified course.
33 * @param int $courseid course id
34 * @param string $enrolpassword enrolment password
35 * @return bool True if match
36 * @since Moodle 3.0
38 function enrol_self_check_group_enrolment_key($courseid, $enrolpassword) {
39 global $DB;
41 $found = false;
42 $groups = $DB->get_records('groups', array('courseid' => $courseid), 'id ASC', 'id, enrolmentkey');
44 foreach ($groups as $group) {
45 if (empty($group->enrolmentkey)) {
46 continue;
48 if ($group->enrolmentkey === $enrolpassword) {
49 $found = true;
50 break;
53 return $found;
56 class enrol_self_enrol_form extends moodleform {
57 protected $instance;
58 protected $toomany = false;
60 /**
61 * Overriding this function to get unique form id for multiple self enrolments.
63 * @return string form identifier
65 protected function get_form_identifier() {
66 $formid = $this->_customdata->id.'_'.get_class($this);
67 return $formid;
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');
77 $heading = $plugin->get_instance_name($instance);
78 $mform->addElement('header', 'selfheader', $heading);
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'));
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);
100 $profilepic = $OUTPUT->user_picture($keyholder, array('size' => 35, 'courseid' => $this->instance->courseid));
101 $mform->addElement('static', 'keyholder'.$keyholdercount, '', $profilepic . $profilelink);
104 } else {
105 $mform->addElement('static', 'nokey', '', get_string('nopassword', 'enrol_self'));
108 $this->add_action_buttons(false, get_string('enrolme', 'enrol_self'));
110 $mform->addElement('hidden', 'id');
111 $mform->setType('id', PARAM_INT);
112 $mform->setDefault('id', $instance->courseid);
114 $mform->addElement('hidden', 'instance');
115 $mform->setType('instance', PARAM_INT);
116 $mform->setDefault('instance', $instance->id);
119 public function validation($data, $files) {
120 global $DB, $CFG;
122 $errors = parent::validation($data, $files);
123 $instance = $this->instance;
125 if ($this->toomany) {
126 $errors['notice'] = get_string('error');
127 return $errors;
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');
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');
151 return $errors;