Commit | Line | Data |
---|---|---|
df997f84 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 | * Guest access plugin. | |
20 | * | |
21 | * This plugin does not add any entries into the user_enrolments table, | |
22 | * the access control is granted on the fly via the tricks in require_login(). | |
23 | * | |
f6990871 PS |
24 | * @package enrol |
25 | * @subpackage guest | |
26 | * @copyright 2010 Petr Skoda {@link http://skodak.org} | |
27 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
df997f84 PS |
28 | */ |
29 | ||
97795859 | 30 | defined('MOODLE_INTERNAL') || die(); |
df997f84 PS |
31 | |
32 | class enrol_guest_plugin extends enrol_plugin { | |
33 | ||
3ead116a PS |
34 | public function get_info_icons(array $instances) { |
35 | //TODO: we need two different guest icons - with and without password | |
36 | return array(new pix_icon('i/guest', get_string('allowguests', 'enrol_guest'))); | |
37 | } | |
38 | ||
df997f84 PS |
39 | public function enrol_user(stdClass $instance, $userid, $roleid = null, $timestart = 0, $timeend = 0) { |
40 | // no real enrolments here! | |
41 | return; | |
42 | } | |
43 | ||
44 | public function unenrol_user(stdClass $instance, $userid) { | |
45 | // nothing to do, we never enrol here! | |
46 | return; | |
47 | } | |
48 | ||
49 | /** | |
50 | * Attempt to automatically gain temporary guest access to course, | |
51 | * calling code has to make sure the plugin and instance are active. | |
52 | * | |
53 | * @param stdClass $instance course enrol instance | |
54 | * @param stdClass $user record | |
55 | * @return bool|int false means no guest access, integer means end of cached time | |
56 | */ | |
57 | public function try_guestaccess(stdClass $instance) { | |
58 | global $USER, $CFG; | |
59 | ||
60 | if (empty($instance->password)) { | |
61 | // Temporarily assign them some guest role for this context | |
62 | $context = get_context_instance(CONTEXT_COURSE, $instance->courseid); | |
63 | $USER->access = load_temp_role($context, $CFG->guestroleid, $USER->access); | |
64 | return ENROL_REQUIRE_LOGIN_CACHE_PERIOD; | |
65 | } | |
66 | ||
67 | return false; | |
68 | } | |
69 | ||
70 | /** | |
71 | * Returns link to page which may be used to add new instance of enrolment plugin in course. | |
72 | * @param int $courseid | |
73 | * @return moodle_url page url | |
74 | */ | |
e25f2466 | 75 | public function get_newinstance_link($courseid) { |
df997f84 PS |
76 | global $DB; |
77 | ||
f6990871 PS |
78 | $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST); |
79 | ||
80 | if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/guest:config', $context)) { | |
df997f84 PS |
81 | return NULL; |
82 | } | |
83 | ||
84 | if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'guest'))) { | |
85 | return NULL; | |
86 | } | |
87 | ||
88 | return new moodle_url('/enrol/guest/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid)); | |
89 | } | |
90 | ||
91 | /** | |
92 | * Creates course enrol form, checks if form submitted | |
93 | * and enrols user if necessary. It can also redirect. | |
94 | * | |
95 | * @param stdClass $instance | |
96 | * @return string html text, usually a form in a text box | |
97 | */ | |
98 | public function enrol_page_hook(stdClass $instance) { | |
99 | global $CFG, $OUTPUT, $SESSION, $USER; | |
100 | ||
101 | if (empty($instance->password)) { | |
102 | return null; | |
103 | } | |
104 | ||
105 | require_once("$CFG->dirroot/enrol/guest/locallib.php"); | |
106 | $form = new enrol_guest_enrol_form(NULL, $instance); | |
107 | $instanceid = optional_param('instance', 0, PARAM_INT); | |
108 | ||
109 | if ($instance->id == $instanceid) { | |
110 | if ($data = $form->get_data()) { | |
111 | // set up primitive require_login() caching | |
112 | unset($USER->enrol['enrolled'][$instance->courseid]); | |
113 | $USER->enrol['tempguest'][$instance->courseid] = time() + 60*60*8; // 8 hours access before asking for pw again | |
114 | ||
115 | // add guest role | |
116 | $context = get_context_instance(CONTEXT_COURSE, $instance->courseid); | |
117 | $USER->access = load_temp_role($context, $CFG->guestroleid, $USER->access); | |
118 | ||
119 | // go to the originally requested page | |
120 | if (!empty($SESSION->wantsurl)) { | |
121 | $destination = $SESSION->wantsurl; | |
122 | unset($SESSION->wantsurl); | |
123 | } else { | |
124 | $destination = "$CFG->wwwroot/course/view.php?id=$instance->courseid"; | |
125 | } | |
126 | redirect($destination); | |
127 | } | |
128 | } | |
129 | ||
130 | ob_start(); | |
131 | $form->display(); | |
132 | $output = ob_get_clean(); | |
133 | ||
134 | return $OUTPUT->box($output, 'generalbox'); | |
135 | } | |
136 | ||
137 | /** | |
138 | * Adds enrol instance UI to course edit form | |
139 | * | |
140 | * @param object $instance enrol instance or null if does not exist yet | |
141 | * @param MoodleQuickForm $mform | |
142 | * @param object $data | |
143 | * @param object $context context of existing course or parent category if course does not exist | |
144 | * @return void | |
145 | */ | |
146 | public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) { | |
147 | ||
148 | $i = isset($instance->id) ? $instance->id : 0; | |
149 | $plugin = enrol_get_plugin('guest'); | |
150 | $header = $plugin->get_instance_name($instance); | |
151 | $config = has_capability('enrol/guest:config', $context); | |
152 | ||
153 | $mform->addElement('header', 'enrol_guest_header_'.$i, $header); | |
154 | ||
155 | ||
156 | $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), | |
157 | ENROL_INSTANCE_DISABLED => get_string('no')); | |
158 | $mform->addElement('select', 'enrol_guest_status_'.$i, get_string('status', 'enrol_guest'), $options); | |
9f38cdce | 159 | $mform->addHelpButton('enrol_guest_status_'.$i, 'status', 'enrol_guest'); |
df997f84 PS |
160 | $mform->setDefault('enrol_guest_status_'.$i, $this->get_config('status')); |
161 | $mform->setAdvanced('enrol_guest_status_'.$i, $this->get_config('status_adv')); | |
162 | if (!$config) { | |
163 | $mform->hardFreeze('enrol_guest_status_'.$i); | |
164 | } | |
165 | ||
166 | $mform->addElement('passwordunmask', 'enrol_guest_password_'.$i, get_string('password', 'enrol_guest')); | |
9f38cdce | 167 | $mform->addHelpButton('enrol_guest_password_'.$i, 'password', 'enrol_guest'); |
df997f84 PS |
168 | if (!$config) { |
169 | $mform->hardFreeze('enrol_guest_password_'.$i); | |
170 | } else { | |
171 | $mform->disabledIf('enrol_guest_password_'.$i, 'enrol_guest_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED); | |
172 | } | |
173 | ||
174 | ||
175 | // now add all values from enrol table | |
176 | if ($instance) { | |
177 | foreach($instance as $key=>$val) { | |
178 | $data->{'enrol_guest_'.$key.'_'.$i} = $val; | |
179 | } | |
180 | } | |
181 | } | |
182 | ||
183 | /** | |
184 | * Validates course edit form data | |
185 | * | |
186 | * @param object $instance enrol instance or null if does not exist yet | |
187 | * @param array $data | |
188 | * @param object $context context of existing course or parent category if course does not exist | |
189 | * @return array errors array | |
190 | */ | |
191 | public function course_edit_validation($instance, array $data, $context) { | |
192 | $errors = array(); | |
193 | ||
194 | if (!has_capability('enrol/guest:config', $context)) { | |
195 | // we are going to ignore the data later anyway, they would nto be able to fix the form anyway | |
196 | return $errors; | |
197 | } | |
198 | ||
199 | $i = isset($instance->id) ? $instance->id : 0; | |
200 | ||
201 | $password = empty($data['enrol_guest_password_'.$i]) ? '' : $data['enrol_guest_password_'.$i]; | |
202 | $checkpassword = false; | |
203 | ||
204 | if ($instance) { | |
205 | if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) { | |
206 | if ($instance->password !== $password) { | |
207 | $checkpassword = true; | |
208 | } | |
209 | } | |
210 | } else { | |
211 | if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) { | |
212 | $checkpassword = true; | |
213 | } | |
214 | } | |
215 | ||
216 | if ($checkpassword) { | |
217 | $require = $this->get_config('requirepassword'); | |
218 | $policy = $this->get_config('usepasswordpolicy'); | |
219 | if ($require and empty($password)) { | |
220 | $errors['enrol_guest_password_'.$i] = get_string('required'); | |
221 | } else if ($policy) { | |
222 | $errmsg = '';//prevent eclipse warning | |
223 | if (!check_password_policy($password, $errmsg)) { | |
224 | $errors['enrol_guest_password_'.$i] = $errmsg; | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
229 | return $errors; | |
230 | } | |
231 | ||
232 | /** | |
233 | * Called after updating/inserting course. | |
234 | * | |
235 | * @param bool $inserted true if course just inserted | |
236 | * @param object $course | |
237 | * @param object $data form data | |
238 | * @return void | |
239 | */ | |
240 | public function course_updated($inserted, $course, $data) { | |
241 | global $DB; | |
242 | ||
243 | $context = get_context_instance(CONTEXT_COURSE, $course->id); | |
244 | ||
245 | if (has_capability('enrol/guest:config', $context)) { | |
246 | if ($inserted) { | |
247 | if (isset($data->enrol_guest_status_0)) { | |
248 | $fields = array('status'=>$data->enrol_guest_status_0); | |
249 | if ($fields['status'] == ENROL_INSTANCE_ENABLED) { | |
250 | $fields['password'] = $data->enrol_guest_password_0; | |
251 | } else { | |
252 | if ($this->get_config('requirepassword')) { | |
253 | $fields['password'] = generate_password(20); | |
254 | } | |
255 | } | |
256 | $this->add_instance($course, $fields); | |
257 | } | |
258 | } else { | |
259 | $instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'guest')); | |
260 | foreach ($instances as $instance) { | |
261 | $i = $instance->id; | |
262 | ||
263 | if (isset($data->{'enrol_guest_status_'.$i})) { | |
264 | $instance->status = $data->{'enrol_guest_status_'.$i}; | |
265 | $instance->timemodified = time(); | |
266 | if ($instance->status == ENROL_INSTANCE_ENABLED) { | |
267 | $instance->password = $data->{'enrol_guest_password_'.$i}; | |
268 | } | |
269 | $DB->update_record('enrol', $instance); | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
274 | } else { | |
275 | if ($inserted) { | |
276 | if ($this->get_config('defaultenrol')) { | |
277 | $this->add_default_instance($course); | |
278 | } | |
279 | } else { | |
280 | // bad luck, user can not change anything | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | /** | |
286 | * Add new instance of enrol plugin with default settings. | |
287 | * @param object $course | |
288 | * @return int id of new instance | |
289 | */ | |
290 | public function add_default_instance($course) { | |
291 | $fields = array('status'=>$this->get_config('status')); | |
292 | ||
293 | if ($this->get_config('requirepassword')) { | |
294 | $fields['password'] = generate_password(20); | |
295 | } | |
296 | ||
297 | return $this->add_instance($course, $fields); | |
298 | } | |
299 | ||
300 | } | |
301 |