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");
29 class enrol_self_enrol_form
extends moodleform
{
31 protected $toomany = false;
34 * Overriding this function to get unique form id for multiple self enrolments.
36 * @return string form identifier
38 protected function get_form_identifier() {
39 $formid = $this->_customdata
->id
.'_'.get_class($this);
43 public function definition() {
44 $mform = $this->_form
;
45 $instance = $this->_customdata
;
46 $this->instance
= $instance;
47 $plugin = enrol_get_plugin('self');
49 $heading = $plugin->get_instance_name($instance);
50 $mform->addElement('header', 'selfheader', $heading);
52 if ($instance->password
) {
53 // Change the id of self enrolment key input as there can be multiple self enrolment methods.
54 $mform->addElement('passwordunmask', 'enrolpassword', get_string('password', 'enrol_self'),
55 array('id' => 'enrolpassword_'.$instance->id
));
57 $mform->addElement('static', 'nokey', '', get_string('nopassword', 'enrol_self'));
60 $this->add_action_buttons(false, get_string('enrolme', 'enrol_self'));
62 $mform->addElement('hidden', 'id');
63 $mform->setType('id', PARAM_INT
);
64 $mform->setDefault('id', $instance->courseid
);
66 $mform->addElement('hidden', 'instance');
67 $mform->setType('instance', PARAM_INT
);
68 $mform->setDefault('instance', $instance->id
);
71 public function validation($data, $files) {
74 $errors = parent
::validation($data, $files);
75 $instance = $this->instance
;
78 $errors['notice'] = get_string('error');
82 if ($instance->password
) {
83 if ($data['enrolpassword'] !== $instance->password
) {
84 if ($instance->customint1
) {
85 $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid
), 'id ASC', 'id, enrolmentkey');
87 foreach ($groups as $group) {
88 if (empty($group->enrolmentkey
)) {
91 if ($group->enrolmentkey
=== $data['enrolpassword']) {
97 // We can not hint because there are probably multiple passwords.
98 $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
102 $plugin = enrol_get_plugin('self');
103 if ($plugin->get_config('showhint')) {
104 $hint = core_text
::substr($instance->password
, 0, 1);
105 $errors['enrolpassword'] = get_string('passwordinvalidhint', 'enrol_self', $hint);
107 $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');