source: moodle/trunk/fuentes/enrol/self/locallib.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 3 years ago

Ported code to xenial

File size: 4.2 KB
Line 
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
25defined('MOODLE_INTERNAL') || die();
26
27require_once("$CFG->libdir/formslib.php");
28
29class enrol_self_enrol_form extends moodleform {
30    protected $instance;
31    protected $toomany = false;
32
33    /**
34     * Overriding this function to get unique form id for multiple self enrolments.
35     *
36     * @return string form identifier
37     */
38    protected function get_form_identifier() {
39        $formid = $this->_customdata->id.'_'.get_class($this);
40        return $formid;
41    }
42
43    public function definition() {
44        $mform = $this->_form;
45        $instance = $this->_customdata;
46        $this->instance = $instance;
47        $plugin = enrol_get_plugin('self');
48
49        $heading = $plugin->get_instance_name($instance);
50        $mform->addElement('header', 'selfheader', $heading);
51
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));
56        } else {
57            $mform->addElement('static', 'nokey', '', get_string('nopassword', 'enrol_self'));
58        }
59
60        $this->add_action_buttons(false, get_string('enrolme', 'enrol_self'));
61
62        $mform->addElement('hidden', 'id');
63        $mform->setType('id', PARAM_INT);
64        $mform->setDefault('id', $instance->courseid);
65
66        $mform->addElement('hidden', 'instance');
67        $mform->setType('instance', PARAM_INT);
68        $mform->setDefault('instance', $instance->id);
69    }
70
71    public function validation($data, $files) {
72        global $DB, $CFG;
73
74        $errors = parent::validation($data, $files);
75        $instance = $this->instance;
76
77        if ($this->toomany) {
78            $errors['notice'] = get_string('error');
79            return $errors;
80        }
81
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');
86                    $found = false;
87                    foreach ($groups as $group) {
88                        if (empty($group->enrolmentkey)) {
89                            continue;
90                        }
91                        if ($group->enrolmentkey === $data['enrolpassword']) {
92                            $found = true;
93                            break;
94                        }
95                    }
96                    if (!$found) {
97                        // We can not hint because there are probably multiple passwords.
98                        $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
99                    }
100
101                } else {
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);
106                    } else {
107                        $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self');
108                    }
109                }
110            }
111        }
112
113        return $errors;
114    }
115}
Note: See TracBrowser for help on using the repository browser.