Commit | Line | Data |
---|---|---|
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 | 28 | defined('MOODLE_INTERNAL') || die(); |
c9f9c900 PS |
29 | |
30 | require_once($CFG->libdir.'/formslib.php'); | |
31 | ||
32 | class 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 | ||
6e0b0a39 PS |
100 | $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self')); |
101 | $mform->setDefault('customint4', $plugin->get_config('sendcoursewelcomemessage')); | |
102 | $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); | |
103 | ||
104 | $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_self'), array('cols'=>'60', 'rows'=>'8')); | |
105 | ||
c9f9c900 | 106 | $mform->addElement('hidden', 'id'); |
af41d03d | 107 | $mform->setType('id', PARAM_INT); |
c9f9c900 | 108 | $mform->addElement('hidden', 'courseid'); |
af41d03d | 109 | $mform->setType('courseid', PARAM_INT); |
c9f9c900 | 110 | |
684784c8 | 111 | $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol'))); |
c9f9c900 PS |
112 | |
113 | $this->set_data($instance); | |
114 | } | |
115 | ||
116 | function validation($data, $files) { | |
117 | global $DB, $CFG; | |
118 | $errors = parent::validation($data, $files); | |
119 | ||
120 | list($instance, $plugin, $context) = $this->_customdata; | |
121 | $checkpassword = false; | |
122 | ||
123 | if ($instance->id) { | |
124 | if ($data['status'] == ENROL_INSTANCE_ENABLED) { | |
125 | if ($instance->password !== $data['password']) { | |
126 | $checkpassword = true; | |
127 | } | |
128 | } | |
129 | } else { | |
130 | if ($data['status'] == ENROL_INSTANCE_ENABLED) { | |
131 | $checkpassword = true; | |
132 | } | |
133 | } | |
134 | ||
135 | if ($checkpassword) { | |
136 | $require = $plugin->get_config('requirepassword'); | |
137 | $policy = $plugin->get_config('usepasswordpolicy'); | |
dc157e09 | 138 | if ($require and trim($data['password']) === '') { |
c9f9c900 PS |
139 | $errors['password'] = get_string('required'); |
140 | } else if ($policy) { | |
141 | $errmsg = '';//prevent eclipse warning | |
142 | if (!check_password_policy($data['password'], $errmsg)) { | |
143 | $errors['password'] = $errmsg; | |
144 | } | |
145 | } | |
146 | } | |
147 | ||
148 | if ($data['status'] == ENROL_INSTANCE_ENABLED) { | |
149 | if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) { | |
150 | $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_self'); | |
151 | } | |
152 | } | |
153 | ||
154 | return $errors; | |
155 | } | |
156 | } |