MDL-30674 enrol_guest: Respect
[moodle.git] / enrol / guest / classes / enrol_guest_edit_form.php
CommitLineData
0c93dda8
AH
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
5164b76e
AN
61 if ($plugin->get_config('requirepassword')) {
62 $mform->addRule('password', get_string('required'), 'required', null);
63 }
64
0c93dda8
AH
65 $mform->addElement('hidden', 'id');
66 $mform->setType('id', PARAM_INT);
67 $mform->addElement('hidden', 'courseid');
68 $mform->setType('courseid', PARAM_INT);
69
70 $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));
71 }
72
73 /**
74 * Form validation
75 *
76 * @param array $data
77 * @param array $files
78 * @return array
79 */
80 public function validation($data, $files) {
81 $errors = parent::validation($data, $files);
82
83 list($instance, $plugin) = $this->_customdata;
84 $checkpassword = false;
85
86 if ($data['id']) {
87 if ($data['status'] == ENROL_INSTANCE_ENABLED) {
88 if ($instance->password !== $data['password']) {
89 $checkpassword = true;
90 }
91 }
92 } else {
93 if ($data['status'] == ENROL_INSTANCE_ENABLED) {
94 $checkpassword = true;
95 }
96 }
97
98 if ($checkpassword) {
0c93dda8 99 $policy = $plugin->get_config('usepasswordpolicy');
5164b76e 100 if ($policy) {
0c93dda8
AH
101 $errmsg = '';
102 if (!check_password_policy($data['password'], $errmsg)) {
103 $errors['password'] = $errmsg;
104 }
105 }
106 }
107
108 return $errors;
109 }
110}