MDL-23899 PHP Intl extension is recommended in 2.0 now because we use it for asort...
[moodle.git] / enrol / self / edit_form.php
CommitLineData
c9f9c900
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Adds new instance of enrol_self to specified course
20 * or edits current instance.
21 *
22 * @package enrol
23 * @subpackage self
24 * @copyright 2010 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
97795859 28defined('MOODLE_INTERNAL') || die();
c9f9c900
PS
29
30require_once($CFG->libdir.'/formslib.php');
31
32class enrol_self_edit_form extends moodleform {
33
34 function definition() {
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'));
42
43 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
44 ENROL_INSTANCE_DISABLED => get_string('no'));
45 $mform->addElement('select', 'status', get_string('status', 'enrol_self'), $options);
46 $mform->addHelpButton('status', 'status', 'enrol_self');
47 $mform->setDefault('status', $plugin->get_config('status'));
48
49 $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self'));
50 $mform->addHelpButton('password', 'password', 'enrol_self');
51 if (empty($instance->id) and $plugin->get_config('requirepassword')) {
52 $mform->addRule('password', get_string('required'), 'required', null, 'client');
53 }
54
55 $options = array(1 => get_string('yes'),
56 0 => get_string('no'));
57 $mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options);
58 $mform->addHelpButton('customint1', 'groupkey', 'enrol_self');
59 $mform->setDefault('customint1', $plugin->get_config('groupkey'));
60
61 if ($instance->id) {
62 $roles = get_default_enrol_roles($context, $instance->roleid);
63 } else {
64 $roles = get_default_enrol_roles($context, $plugin->get_config('roleid'));
65 }
66 $mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles);
67 $mform->setDefault('roleid', $plugin->get_config('roleid'));
68
69 $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), array('optional' => true, 'defaultunit' => 86400));
70 $mform->setDefault('enrolperiod', $plugin->get_config('enrolperiod'));
71
72 $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), array('optional' => true));
73 $mform->setDefault('enrolstartdate', 0);
74
75 $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_self'), array('optional' => true));
76 $mform->setDefault('enrolenddate', 0);
77
770ab27f
PS
78 $options = array(0 => get_string('never'),
79 1800 * 3600 * 24 => get_string('numdays', '', 1800),
80 1000 * 3600 * 24 => get_string('numdays', '', 1000),
81 365 * 3600 * 24 => get_string('numdays', '', 365),
82 180 * 3600 * 24 => get_string('numdays', '', 180),
83 150 * 3600 * 24 => get_string('numdays', '', 150),
84 120 * 3600 * 24 => get_string('numdays', '', 120),
85 90 * 3600 * 24 => get_string('numdays', '', 90),
86 60 * 3600 * 24 => get_string('numdays', '', 60),
87 30 * 3600 * 24 => get_string('numdays', '', 30),
88 21 * 3600 * 24 => get_string('numdays', '', 21),
89 14 * 3600 * 24 => get_string('numdays', '', 14),
90 7 * 3600 * 24 => get_string('numdays', '', 7));
91 $mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_self'), $options);
92 $mform->setDefault('customint2', $plugin->get_config('longtimenosee'));
93 $mform->addHelpButton('customint2', 'longtimenosee', 'enrol_self');
94
af41d03d
PS
95 $mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_self'));
96 $mform->setDefault('customint3', $plugin->get_config('maxenrolled'));
97 $mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self');
98 $mform->setType('customint3', PARAM_INT);
99
c9f9c900 100 $mform->addElement('hidden', 'id');
af41d03d 101 $mform->setType('id', PARAM_INT);
c9f9c900 102 $mform->addElement('hidden', 'courseid');
af41d03d 103 $mform->setType('courseid', PARAM_INT);
c9f9c900 104
684784c8 105 $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));
c9f9c900
PS
106
107 $this->set_data($instance);
108 }
109
110 function validation($data, $files) {
111 global $DB, $CFG;
112 $errors = parent::validation($data, $files);
113
114 list($instance, $plugin, $context) = $this->_customdata;
115 $checkpassword = false;
116
117 if ($instance->id) {
118 if ($data['status'] == ENROL_INSTANCE_ENABLED) {
119 if ($instance->password !== $data['password']) {
120 $checkpassword = true;
121 }
122 }
123 } else {
124 if ($data['status'] == ENROL_INSTANCE_ENABLED) {
125 $checkpassword = true;
126 }
127 }
128
129 if ($checkpassword) {
130 $require = $plugin->get_config('requirepassword');
131 $policy = $plugin->get_config('usepasswordpolicy');
132 if ($require and trim($data['password'])) {
133 $errors['password'] = get_string('required');
134 } else if ($policy) {
135 $errmsg = '';//prevent eclipse warning
136 if (!check_password_policy($data['password'], $errmsg)) {
137 $errors['password'] = $errmsg;
138 }
139 }
140 }
141
142 if ($data['status'] == ENROL_INSTANCE_ENABLED) {
143 if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) {
144 $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_self');
145 }
146 }
147
148 return $errors;
149 }
150}