MDL-43175 events: Base event unit tests for JSON encoding / decoding comparison.
[moodle.git] / enrol / guest / lib.php
blob65444bdd6b65e16fae340d27f0f5d5005f990801
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.
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/>.
17 /**
18 * Guest access plugin.
20 * This plugin does not add any entries into the user_enrolments table,
21 * the access control is granted on the fly via the tricks in require_login().
23 * @package enrol_guest
24 * @copyright 2010 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 class enrol_guest_plugin extends enrol_plugin {
32 /**
33 * Returns optional enrolment information icons.
35 * This is used in course list for quick overview of enrolment options.
37 * We are not using single instance parameter because sometimes
38 * we might want to prevent icon repetition when multiple instances
39 * of one type exist. One instance may also produce several icons.
41 * @param array $instances all enrol instances of this type in one course
42 * @return array of pix_icon
44 public function get_info_icons(array $instances) {
45 foreach ($instances as $instance) {
46 if ($instance->password !== '') {
47 return array(new pix_icon('withpassword', get_string('pluginname', 'enrol_guest'), 'enrol_guest'));
48 } else {
49 return array(new pix_icon('withoutpassword', get_string('pluginname', 'enrol_guest'), 'enrol_guest'));
54 public function enrol_user(stdClass $instance, $userid, $roleid = null, $timestart = 0, $timeend = 0, $status = null, $recovergrades = null) {
55 // no real enrolments here!
56 return;
59 public function unenrol_user(stdClass $instance, $userid) {
60 // nothing to do, we never enrol here!
61 return;
64 /**
65 * Attempt to automatically gain temporary guest access to course,
66 * calling code has to make sure the plugin and instance are active.
68 * @param stdClass $instance course enrol instance
69 * @return bool|int false means no guest access, integer means end of cached time
71 public function try_guestaccess(stdClass $instance) {
72 global $USER, $CFG;
74 $allow = false;
76 if ($instance->password === '') {
77 $allow = true;
79 } else if (isset($USER->enrol_guest_passwords[$instance->id])) { // this is a hack, ideally we should not add stuff to $USER...
80 if ($USER->enrol_guest_passwords[$instance->id] === $instance->password) {
81 $allow = true;
85 if ($allow) {
86 // Temporarily assign them some guest role for this context
87 $context = context_course::instance($instance->courseid);
88 load_temp_course_role($context, $CFG->guestroleid);
89 return ENROL_MAX_TIMESTAMP;
92 return false;
95 /**
96 * Returns link to page which may be used to add new instance of enrolment plugin in course.
97 * @param int $courseid
98 * @return moodle_url page url
100 public function get_newinstance_link($courseid) {
101 global $DB;
103 $context = context_course::instance($courseid, MUST_EXIST);
105 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/guest:config', $context)) {
106 return NULL;
109 if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'guest'))) {
110 return NULL;
113 return new moodle_url('/enrol/guest/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid));
117 * Creates course enrol form, checks if form submitted
118 * and enrols user if necessary. It can also redirect.
120 * @param stdClass $instance
121 * @return string html text, usually a form in a text box
123 public function enrol_page_hook(stdClass $instance) {
124 global $CFG, $OUTPUT, $SESSION, $USER;
126 if ($instance->password === '') {
127 return null;
130 if (isset($USER->enrol['tempguest'][$instance->courseid]) and $USER->enrol['tempguest'][$instance->courseid] > time()) {
131 // no need to show the guest access when user can already enter course as guest
132 return null;
135 require_once("$CFG->dirroot/enrol/guest/locallib.php");
136 $form = new enrol_guest_enrol_form(NULL, $instance);
137 $instanceid = optional_param('instance', 0, PARAM_INT);
139 if ($instance->id == $instanceid) {
140 if ($data = $form->get_data()) {
141 // add guest role
142 $context = context_course::instance($instance->courseid);
143 $USER->enrol_guest_passwords[$instance->id] = $data->guestpassword; // this is a hack, ideally we should not add stuff to $USER...
144 if (isset($USER->enrol['tempguest'][$instance->courseid])) {
145 remove_temp_course_roles($context);
147 load_temp_course_role($context, $CFG->guestroleid);
148 $USER->enrol['tempguest'][$instance->courseid] = ENROL_MAX_TIMESTAMP;
150 // go to the originally requested page
151 if (!empty($SESSION->wantsurl)) {
152 $destination = $SESSION->wantsurl;
153 unset($SESSION->wantsurl);
154 } else {
155 $destination = "$CFG->wwwroot/course/view.php?id=$instance->courseid";
157 redirect($destination);
161 ob_start();
162 $form->display();
163 $output = ob_get_clean();
165 return $OUTPUT->box($output, 'generalbox');
169 * Adds enrol instance UI to course edit form
171 * @param object $instance enrol instance or null if does not exist yet
172 * @param MoodleQuickForm $mform
173 * @param object $data
174 * @param object $context context of existing course or parent category if course does not exist
175 * @return void
177 public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) {
179 $i = isset($instance->id) ? $instance->id : 0;
181 if (!$i and !$this->get_config('defaultenrol')) {
182 return;
185 $header = $this->get_instance_name($instance);
186 if (!$i) {
187 $config = guess_if_creator_will_have_course_capability('enrol/guest:config', $context);
188 } else {
189 $config = has_capability('enrol/guest:config', $context);
192 $mform->addElement('header', 'enrol_guest_header_'.$i, $header);
195 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
196 ENROL_INSTANCE_DISABLED => get_string('no'));
197 $mform->addElement('select', 'enrol_guest_status_'.$i, get_string('status', 'enrol_guest'), $options);
198 $mform->addHelpButton('enrol_guest_status_'.$i, 'status', 'enrol_guest');
199 $mform->setDefault('enrol_guest_status_'.$i, $this->get_config('status'));
200 $mform->setAdvanced('enrol_guest_status_'.$i, $this->get_config('status_adv'));
201 if (!$config) {
202 $mform->hardFreeze('enrol_guest_status_'.$i);
203 if (!$i) {
204 $mform->setConstant('enrol_guest_status_'.$i, $this->get_config('status'));
205 } else {
206 $mform->setConstant('enrol_guest_status_'.$i, $instance->status);
210 $mform->addElement('passwordunmask', 'enrol_guest_password_'.$i, get_string('password', 'enrol_guest'));
211 $mform->addHelpButton('enrol_guest_password_'.$i, 'password', 'enrol_guest');
212 if (!$config) {
213 $mform->hardFreeze('enrol_guest_password_'.$i);
214 if (!$i) {
215 if ($this->get_config('requirepassword')) {
216 $password = generate_password(20);
217 } else {
218 $password = '';
220 $mform->setConstant('enrol_guest_password_'.$i, $password);
221 } else {
222 $mform->setConstant('enrol_guest_password_'.$i, $instance->password);
224 } else {
225 $mform->disabledIf('enrol_guest_password_'.$i, 'enrol_guest_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
229 // now add all values from enrol table
230 if ($instance) {
231 foreach($instance as $key=>$val) {
232 $data->{'enrol_guest_'.$key.'_'.$i} = $val;
238 * Validates course edit form data
240 * @param object $instance enrol instance or null if does not exist yet
241 * @param array $data
242 * @param object $context context of existing course or parent category if course does not exist
243 * @return array errors array
245 public function course_edit_validation($instance, array $data, $context) {
246 $errors = array();
248 if (!has_capability('enrol/guest:config', $context)) {
249 // we are going to ignore the data later anyway, they would nto be able to fix the form anyway
250 return $errors;
253 $i = isset($instance->id) ? $instance->id : 0;
255 if (!isset($data['enrol_guest_status_'.$i])) {
256 return $errors;
259 $password = empty($data['enrol_guest_password_'.$i]) ? '' : $data['enrol_guest_password_'.$i];
260 $checkpassword = false;
262 if ($instance) {
263 if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) {
264 if ($instance->password !== $password) {
265 $checkpassword = true;
268 } else {
269 if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) {
270 $checkpassword = true;
274 if ($checkpassword) {
275 $require = $this->get_config('requirepassword');
276 $policy = $this->get_config('usepasswordpolicy');
277 if ($require and empty($password)) {
278 $errors['enrol_guest_password_'.$i] = get_string('required');
279 } else if ($policy) {
280 $errmsg = '';//prevent eclipse warning
281 if (!check_password_policy($password, $errmsg)) {
282 $errors['enrol_guest_password_'.$i] = $errmsg;
287 return $errors;
291 * Called after updating/inserting course.
293 * @param bool $inserted true if course just inserted
294 * @param object $course
295 * @param object $data form data
296 * @return void
298 public function course_updated($inserted, $course, $data) {
299 global $DB;
301 if ($inserted) {
302 if (isset($data->enrol_guest_status_0)) {
303 $fields = array('status'=>$data->enrol_guest_status_0);
304 if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
305 $fields['password'] = $data->enrol_guest_password_0;
306 } else {
307 if ($this->get_config('requirepassword')) {
308 $fields['password'] = generate_password(20);
311 $this->add_instance($course, $fields);
312 } else {
313 if ($this->get_config('defaultenrol')) {
314 $this->add_default_instance($course);
318 } else {
319 $instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'guest'));
320 foreach ($instances as $instance) {
321 $i = $instance->id;
323 if (isset($data->{'enrol_guest_status_'.$i})) {
324 $reset = ($instance->status != $data->{'enrol_guest_status_'.$i});
326 $instance->status = $data->{'enrol_guest_status_'.$i};
327 $instance->timemodified = time();
328 if ($instance->status == ENROL_INSTANCE_ENABLED) {
329 if ($instance->password !== $data->{'enrol_guest_password_'.$i}) {
330 $reset = true;
332 $instance->password = $data->{'enrol_guest_password_'.$i};
334 $DB->update_record('enrol', $instance);
336 if ($reset) {
337 $context = context_course::instance($course->id);
338 $context->mark_dirty();
346 * Add new instance of enrol plugin.
347 * @param object $course
348 * @param array instance fields
349 * @return int id of new instance, null if can not be created
351 public function add_instance($course, array $fields = NULL) {
352 $fields = (array)$fields;
354 if (!isset($fields['password'])) {
355 $fields['password'] = '';
358 return parent::add_instance($course, $fields);
362 * Add new instance of enrol plugin with default settings.
363 * @param object $course
364 * @return int id of new instance
366 public function add_default_instance($course) {
367 $fields = array('status'=>$this->get_config('status'));
369 if ($this->get_config('requirepassword')) {
370 $fields['password'] = generate_password(20);
373 return $this->add_instance($course, $fields);
377 * Restore instance and map settings.
379 * @param restore_enrolments_structure_step $step
380 * @param stdClass $data
381 * @param stdClass $course
382 * @param int $oldid
384 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
385 global $DB;
387 if (!$DB->record_exists('enrol', array('courseid' => $data->courseid, 'enrol' => $this->get_name()))) {
388 $this->add_instance($course, (array)$data);
391 // No need to set mapping, we do not restore users or roles here.
392 $step->set_mapping('enrol', $oldid, 0);