source: moodle/trunk/fuentes/enrol/guest/classes/enrol_guest_edit_form.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 4.4 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 * Guest access plugin.
19 *
20 * Adds new instance of enrol_guest to specified course
21 * or edits current instance.
22 *
23 * @package    enrol_guest
24 * @copyright  2015 Andrew Hancox <andrewdchancox@googlemail.com>
25 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27namespace enrol_guest;
28use moodleform;
29
30defined('MOODLE_INTERNAL') || die();
31
32require_once($CFG->libdir.'/formslib.php');
33
34/**
35 * Class enrol_guest_edit_form
36 * @copyright  2015 Andrew Hancox <andrewdchancox@googlemail.com>
37 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class enrol_guest_edit_form extends moodleform {
40    /**
41     * Form definition
42     */
43    public function definition() {
44
45        $mform = $this->_form;
46
47        list($instance, $plugin) = $this->_customdata;
48
49        $mform->addElement('header', 'header', get_string('pluginname', 'enrol_guest'));
50
51        $options = array(ENROL_INSTANCE_ENABLED  => get_string('yes'),
52                         ENROL_INSTANCE_DISABLED => get_string('no'));
53        $mform->addElement('select', 'status', get_string('status', 'enrol_guest'), $options);
54        $mform->addHelpButton('status', 'status', 'enrol_guest');
55        $mform->setDefault('status', $plugin->get_config('status'));
56        $mform->setAdvanced('status', $plugin->get_config('status_adv'));
57
58        $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_guest'));
59        $mform->addHelpButton('password', 'password', 'enrol_guest');
60
61        // If we have a new instance and the password is required - make sure it is set. For existing
62        // instances we do not force the password to be required as it may have been set to empty before
63        // the password was required. We check in the validation function whether this check is required
64        // for existing instances.
65        if (empty($instance->id) && $plugin->get_config('requirepassword')) {
66            $mform->addRule('password', get_string('required'), 'required', null);
67        }
68
69        $mform->addElement('hidden', 'id');
70        $mform->setType('id', PARAM_INT);
71        $mform->addElement('hidden', 'courseid');
72        $mform->setType('courseid', PARAM_INT);
73
74        $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));
75    }
76
77    /**
78     * Form validation
79     *
80     * @param array $data
81     * @param array $files
82     * @return array
83     */
84    public function validation($data, $files) {
85        $errors = parent::validation($data, $files);
86
87        list($instance, $plugin) = $this->_customdata;
88        $checkpassword = false;
89
90        if ($data['id']) {
91            // Check the password if we are enabling the plugin again.
92            if (($instance->status == ENROL_INSTANCE_DISABLED) && ($data['status'] == ENROL_INSTANCE_ENABLED)) {
93                $checkpassword = true;
94            }
95
96            // Check the password if the instance is enabled and the password has changed.
97            if (($data['status'] == ENROL_INSTANCE_ENABLED) && ($instance->password !== $data['password'])) {
98                $checkpassword = true;
99            }
100        } else {
101            $checkpassword = true;
102        }
103
104        if ($checkpassword) {
105            $require = $plugin->get_config('requirepassword');
106            $policy  = $plugin->get_config('usepasswordpolicy');
107            if ($require && trim($data['password']) === '') {
108                $errors['password'] = get_string('required');
109            } else if (!empty($data['password']) && $policy) {
110                $errmsg = '';
111                if (!check_password_policy($data['password'], $errmsg)) {
112                    $errors['password'] = $errmsg;
113                }
114            }
115        }
116
117        return $errors;
118    }
119}
Note: See TracBrowser for help on using the repository browser.