2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Self enrol plugin implementation.
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');
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
38 function enrol_self_check_group_enrolment_key($courseid, $enrolpassword) {
42 $groups = $DB->get_records('groups', array('courseid' => $courseid), 'id ASC', 'id, enrolmentkey');
44 foreach ($groups as $group) {
45 if (empty($group->enrolmentkey
)) {
48 if ($group->enrolmentkey
=== $enrolpassword) {
56 class enrol_self_enrol_form
extends moodleform
{
58 protected $toomany = false;
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);
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'));
87 foreach ($keyholders as $keyholder) {
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
. '&course=' .
96 $this->instance
->courseid
. '">' . fullname($keyholder) . '</a>';
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);
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) {
122 $errors = parent
::validation($data, $files);
123 $instance = $this->instance
;
125 if ($this->toomany
) {
126 $errors['notice'] = get_string('error');
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');
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);
145 $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');