MDL-37717 warn teachers before disabling or deleting their enrolment method
[moodle.git] / enrol / self / edit_form.php
CommitLineData
c9f9c900 1<?php
c9f9c900
PS
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 * Adds new instance of enrol_self to specified course
19 * or edits current instance.
20 *
d9669db9 21 * @package enrol_self
c9f9c900
PS
22 * @copyright 2010 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
97795859 26defined('MOODLE_INTERNAL') || die();
c9f9c900
PS
27
28require_once($CFG->libdir.'/formslib.php');
29
30class enrol_self_edit_form extends moodleform {
31
32 function definition() {
dd6b1f15
PS
33 global $DB;
34
c9f9c900
PS
35 $mform = $this->_form;
36
37 list($instance, $plugin, $context) = $this->_customdata;
38
39 $mform->addElement('header', 'header', get_string('pluginname', 'enrol_self'));
40
41 $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
1ad89028 42 $mform->setType('name', PARAM_TEXT);
c9f9c900
PS
43
44 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
45 ENROL_INSTANCE_DISABLED => get_string('no'));
46 $mform->addElement('select', 'status', get_string('status', 'enrol_self'), $options);
47 $mform->addHelpButton('status', 'status', 'enrol_self');
c9f9c900 48
e1fa177c
PS
49 $options = array(1 => get_string('yes'), 0 => get_string('no'));
50 $mform->addElement('select', 'customint6', get_string('newenrols', 'enrol_self'), $options);
51 $mform->addHelpButton('customint6', 'newenrols', 'enrol_self');
52 $mform->disabledIf('customint6', 'status', 'eq', ENROL_INSTANCE_DISABLED);
53
c9f9c900
PS
54 $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self'));
55 $mform->addHelpButton('password', 'password', 'enrol_self');
56 if (empty($instance->id) and $plugin->get_config('requirepassword')) {
57 $mform->addRule('password', get_string('required'), 'required', null, 'client');
58 }
59
60 $options = array(1 => get_string('yes'),
61 0 => get_string('no'));
62 $mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options);
63 $mform->addHelpButton('customint1', 'groupkey', 'enrol_self');
c9f9c900 64
eac37ff4 65 $roles = $this->extend_assignable_roles($context, $instance->roleid);
c9f9c900 66 $mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles);
c9f9c900
PS
67
68 $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), array('optional' => true, 'defaultunit' => 86400));
94f630e3 69 $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_self');
c9f9c900 70
8a486fdb
PS
71 $options = array(0 => get_string('no'), 1 => get_string('expirynotifyenroller', 'core_enrol'), 2 => get_string('expirynotifyall', 'core_enrol'));
72 $mform->addElement('select', 'expirynotify', get_string('expirynotify', 'core_enrol'), $options);
73 $mform->addHelpButton('expirynotify', 'expirynotify', 'core_enrol');
74
75 $mform->addElement('duration', 'expirythreshold', get_string('expirythreshold', 'core_enrol'), array('optional' => false, 'defaultunit' => 86400));
76 $mform->addHelpButton('expirythreshold', 'expirythreshold', 'core_enrol');
77 $mform->disabledIf('expirythreshold', 'expirynotify', 'eq', 0);
78
c9f9c900
PS
79 $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), array('optional' => true));
80 $mform->setDefault('enrolstartdate', 0);
94f630e3 81 $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_self');
c9f9c900
PS
82
83 $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_self'), array('optional' => true));
84 $mform->setDefault('enrolenddate', 0);
94f630e3 85 $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_self');
c9f9c900 86
770ab27f
PS
87 $options = array(0 => get_string('never'),
88 1800 * 3600 * 24 => get_string('numdays', '', 1800),
89 1000 * 3600 * 24 => get_string('numdays', '', 1000),
90 365 * 3600 * 24 => get_string('numdays', '', 365),
91 180 * 3600 * 24 => get_string('numdays', '', 180),
92 150 * 3600 * 24 => get_string('numdays', '', 150),
93 120 * 3600 * 24 => get_string('numdays', '', 120),
94 90 * 3600 * 24 => get_string('numdays', '', 90),
95 60 * 3600 * 24 => get_string('numdays', '', 60),
96 30 * 3600 * 24 => get_string('numdays', '', 30),
97 21 * 3600 * 24 => get_string('numdays', '', 21),
98 14 * 3600 * 24 => get_string('numdays', '', 14),
99 7 * 3600 * 24 => get_string('numdays', '', 7));
100 $mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_self'), $options);
770ab27f
PS
101 $mform->addHelpButton('customint2', 'longtimenosee', 'enrol_self');
102
af41d03d 103 $mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_self'));
af41d03d
PS
104 $mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self');
105 $mform->setType('customint3', PARAM_INT);
106
dd6b1f15
PS
107 $cohorts = array(0 => get_string('no'));
108 list($sqlparents, $params) = $DB->get_in_or_equal($context->get_parent_context_ids(), SQL_PARAMS_NAMED);
109 $params['current'] = $instance->customint5;
110 $sql = "SELECT id, name, idnumber, contextid
111 FROM {cohort}
112 WHERE contextid $sqlparents OR id = :current
113 ORDER BY name ASC, idnumber ASC";
114 $rs = $DB->get_recordset_sql($sql, $params);
115 foreach ($rs as $c) {
116 $ccontext = context::instance_by_id($c->contextid);
117 if ($c->id != $instance->customint5 and !has_capability('moodle/cohort:view', $ccontext)) {
118 continue;
119 }
120 $cohorts[$c->id] = format_string($c->name, true, array('context'=>$context));
121 if ($c->idnumber) {
122 $cohorts[$c->id] .= ' ['.s($c->idnumber).']';
123 }
124 }
125 if (!isset($cohorts[$instance->customint5])) {
126 // Somebody deleted a cohort, better keep the wrong value so that random ppl can not enrol.
127 $cohorts[$instance->customint5] = get_string('unknowncohort', 'cohort', $instance->customint5);
128 }
129 $rs->close();
130 if (count($cohorts) > 1) {
131 $mform->addElement('select', 'customint5', get_string('cohortonly', 'enrol_self'), $cohorts);
132 $mform->addHelpButton('customint5', 'cohortonly', 'enrol_self');
133 } else {
134 $mform->addElement('hidden', 'customint5');
135 $mform->setType('customint5', PARAM_INT);
136 $mform->setConstant('customint5', 0);
137 }
138
6e0b0a39 139 $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self'));
6e0b0a39
PS
140 $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self');
141
142 $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_self'), array('cols'=>'60', 'rows'=>'8'));
08962926 143 $mform->addHelpButton('customtext1', 'customwelcomemessage', 'enrol_self');
6e0b0a39 144
c9f9c900 145 $mform->addElement('hidden', 'id');
af41d03d 146 $mform->setType('id', PARAM_INT);
c9f9c900 147 $mform->addElement('hidden', 'courseid');
af41d03d 148 $mform->setType('courseid', PARAM_INT);
c9f9c900 149
12c92bca
PS
150 if (enrol_accessing_via_instance($instance)) {
151 $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), get_string('instanceeditselfwarningtext', 'core_enrol'));
152 }
153
684784c8 154 $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));
c9f9c900
PS
155
156 $this->set_data($instance);
157 }
158
159 function validation($data, $files) {
160 global $DB, $CFG;
161 $errors = parent::validation($data, $files);
162
163 list($instance, $plugin, $context) = $this->_customdata;
164 $checkpassword = false;
165
166 if ($instance->id) {
167 if ($data['status'] == ENROL_INSTANCE_ENABLED) {
168 if ($instance->password !== $data['password']) {
169 $checkpassword = true;
170 }
171 }
172 } else {
173 if ($data['status'] == ENROL_INSTANCE_ENABLED) {
174 $checkpassword = true;
175 }
176 }
177
178 if ($checkpassword) {
179 $require = $plugin->get_config('requirepassword');
180 $policy = $plugin->get_config('usepasswordpolicy');
dc157e09 181 if ($require and trim($data['password']) === '') {
c9f9c900
PS
182 $errors['password'] = get_string('required');
183 } else if ($policy) {
184 $errmsg = '';//prevent eclipse warning
185 if (!check_password_policy($data['password'], $errmsg)) {
186 $errors['password'] = $errmsg;
187 }
188 }
189 }
190
191 if ($data['status'] == ENROL_INSTANCE_ENABLED) {
192 if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) {
193 $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_self');
194 }
195 }
196
8a486fdb
PS
197 if ($data['expirynotify'] > 0 and $data['expirythreshold'] < 86400) {
198 $errors['expirythreshold'] = get_string('errorthresholdlow', 'core_enrol');
199 }
200
c9f9c900
PS
201 return $errors;
202 }
2c585718
JF
203
204 /**
d9669db9 205 * Gets a list of roles that this user can assign for the course as the default for self-enrolment.
2c585718
JF
206 *
207 * @param context $context the context.
c52551dc 208 * @param integer $defaultrole the id of the role that is set as the default for self-enrolment
2c585718
JF
209 * @return array index is the role id, value is the role name
210 */
211 function extend_assignable_roles($context, $defaultrole) {
b50ac857 212 global $DB;
c52551dc
PS
213
214 $roles = get_assignable_roles($context, ROLENAME_BOTH);
215 if (!isset($roles[$defaultrole])) {
216 if ($role = $DB->get_record('role', array('id'=>$defaultrole))) {
217 $roles[$defaultrole] = role_get_name($role, $context, ROLENAME_BOTH);
218 }
2c585718
JF
219 }
220 return $roles;
221 }
c52551dc 222}