2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
31 * Class enrol_guest_plugin
33 * @copyright 2010 Petr Skoda {@link http://skodak.org}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class enrol_guest_plugin
extends enrol_plugin
{
39 * Returns optional enrolment information icons.
41 * This is used in course list for quick overview of enrolment options.
43 * We are not using single instance parameter because sometimes
44 * we might want to prevent icon repetition when multiple instances
45 * of one type exist. One instance may also produce several icons.
47 * @param array $instances all enrol instances of this type in one course
48 * @return array of pix_icon
50 public function get_info_icons(array $instances) {
51 foreach ($instances as $instance) {
52 if ($instance->password
!== '') {
53 return array(new pix_icon('withpassword', get_string('guestaccess_withpassword', 'enrol_guest'), 'enrol_guest'));
55 return array(new pix_icon('withoutpassword', get_string('guestaccess_withoutpassword', 'enrol_guest'), 'enrol_guest'));
61 * Enrol a user using a given enrolment instance.
63 * @param stdClass $instance
66 * @param int $timestart
69 * @param null $recovergrades
71 public function enrol_user(stdClass
$instance, $userid, $roleid = null, $timestart = 0, $timeend = 0, $status = null, $recovergrades = null) {
72 // no real enrolments here!
77 * Enrol a user from a given enrolment instance.
79 * @param stdClass $instance
82 public function unenrol_user(stdClass
$instance, $userid) {
83 // nothing to do, we never enrol here!
88 * Sets up navigation entries.
90 * @param stdClass $instancesnode
91 * @param stdClass $instance
93 * @throws coding_exception
95 public function add_course_navigation($instancesnode, stdClass
$instance) {
96 if ($instance->enrol
!== 'guest') {
97 throw new coding_exception('Invalid enrol instance type!');
100 $context = context_course
::instance($instance->courseid
);
101 if (has_capability('enrol/guest:config', $context)) {
102 $managelink = new moodle_url('/enrol/guest/edit.php', array('courseid' => $instance->courseid
, 'id' => $instance->id
));
103 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node
::TYPE_SETTING
);
108 * Returns edit icons for the page with list of instances
109 * @param stdClass $instance
111 * @throws coding_exception
113 public function get_action_icons(stdClass
$instance) {
116 if ($instance->enrol
!== 'guest') {
117 throw new coding_exception('invalid enrol instance!');
119 $context = context_course
::instance($instance->courseid
);
123 if (has_capability('enrol/guest:config', $context)) {
124 $editlink = new moodle_url("/enrol/guest/edit.php", array('courseid' => $instance->courseid
, 'id' => $instance->id
));
125 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core',
126 array('class' => 'iconsmall')));
133 * Attempt to automatically gain temporary guest access to course,
134 * calling code has to make sure the plugin and instance are active.
136 * @param stdClass $instance course enrol instance
137 * @return bool|int false means no guest access, integer means end of cached time
139 public function try_guestaccess(stdClass
$instance) {
144 if ($instance->password
=== '') {
147 } else if (isset($USER->enrol_guest_passwords
[$instance->id
])) { // this is a hack, ideally we should not add stuff to $USER...
148 if ($USER->enrol_guest_passwords
[$instance->id
] === $instance->password
) {
154 // Temporarily assign them some guest role for this context
155 $context = context_course
::instance($instance->courseid
);
156 load_temp_course_role($context, $CFG->guestroleid
);
157 return ENROL_MAX_TIMESTAMP
;
164 * Returns link to page which may be used to add new instance of enrolment plugin in course.
165 * @param int $courseid
166 * @return moodle_url page url
168 public function get_newinstance_link($courseid) {
171 $context = context_course
::instance($courseid, MUST_EXIST
);
173 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/guest:config', $context)) {
177 if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'guest'))) {
181 return new moodle_url('/enrol/guest/edit.php', array('courseid' => $courseid));
185 * Creates course enrol form, checks if form submitted
186 * and enrols user if necessary. It can also redirect.
188 * @param stdClass $instance
189 * @return string html text, usually a form in a text box
191 public function enrol_page_hook(stdClass
$instance) {
192 global $CFG, $OUTPUT, $SESSION, $USER;
194 if ($instance->password
=== '') {
198 if (isset($USER->enrol
['tempguest'][$instance->courseid
]) and $USER->enrol
['tempguest'][$instance->courseid
] > time()) {
199 // no need to show the guest access when user can already enter course as guest
203 require_once("$CFG->dirroot/enrol/guest/locallib.php");
204 $form = new enrol_guest_enrol_form(NULL, $instance);
205 $instanceid = optional_param('instance', 0, PARAM_INT
);
207 if ($instance->id
== $instanceid) {
208 if ($data = $form->get_data()) {
210 $context = context_course
::instance($instance->courseid
);
211 $USER->enrol_guest_passwords
[$instance->id
] = $data->guestpassword
; // this is a hack, ideally we should not add stuff to $USER...
212 if (isset($USER->enrol
['tempguest'][$instance->courseid
])) {
213 remove_temp_course_roles($context);
215 load_temp_course_role($context, $CFG->guestroleid
);
216 $USER->enrol
['tempguest'][$instance->courseid
] = ENROL_MAX_TIMESTAMP
;
218 // go to the originally requested page
219 if (!empty($SESSION->wantsurl
)) {
220 $destination = $SESSION->wantsurl
;
221 unset($SESSION->wantsurl
);
223 $destination = "$CFG->wwwroot/course/view.php?id=$instance->courseid";
225 redirect($destination);
231 $output = ob_get_clean();
233 return $OUTPUT->box($output, 'generalbox');
237 * Called after updating/inserting course.
239 * @param bool $inserted true if course just inserted
240 * @param object $course
241 * @param object $data form data
244 public function course_updated($inserted, $course, $data) {
248 if (isset($data->enrol_guest_status_0
)) {
249 $fields = array('status'=>$data->enrol_guest_status_0
);
250 if ($fields['status'] == ENROL_INSTANCE_ENABLED
) {
251 $fields['password'] = $data->enrol_guest_password_0
;
253 if ($this->get_config('requirepassword')) {
254 $fields['password'] = generate_password(20);
257 $this->add_instance($course, $fields);
259 if ($this->get_config('defaultenrol')) {
260 $this->add_default_instance($course);
265 $instances = $DB->get_records('enrol', array('courseid'=>$course->id
, 'enrol'=>'guest'));
266 foreach ($instances as $instance) {
269 if (isset($data->{'enrol_guest_status_'.$i})) {
270 $reset = ($instance->status
!= $data->{'enrol_guest_status_'.$i});
272 $instance->status
= $data->{'enrol_guest_status_'.$i};
273 $instance->timemodified
= time();
274 if ($instance->status
== ENROL_INSTANCE_ENABLED
) {
275 if ($instance->password
!== $data->{'enrol_guest_password_'.$i}) {
278 $instance->password
= $data->{'enrol_guest_password_'.$i};
280 $DB->update_record('enrol', $instance);
281 \core\event\enrol_instance_updated
::create_from_record($instance)->trigger();
284 $context = context_course
::instance($course->id
);
285 $context->mark_dirty();
293 * Add new instance of enrol plugin.
294 * @param object $course
295 * @param array instance fields
296 * @return int id of new instance, null if can not be created
298 public function add_instance($course, array $fields = NULL) {
299 $fields = (array)$fields;
301 if (!isset($fields['password'])) {
302 $fields['password'] = '';
305 return parent
::add_instance($course, $fields);
309 * Add new instance of enrol plugin with default settings.
310 * @param object $course
311 * @return int id of new instance
313 public function add_default_instance($course) {
314 $fields = array('status'=>$this->get_config('status'));
316 if ($this->get_config('requirepassword')) {
317 $fields['password'] = generate_password(20);
320 return $this->add_instance($course, $fields);
324 * Restore instance and map settings.
326 * @param restore_enrolments_structure_step $step
327 * @param stdClass $data
328 * @param stdClass $course
331 public function restore_instance(restore_enrolments_structure_step
$step, stdClass
$data, $course, $oldid) {
334 if (!$DB->record_exists('enrol', array('courseid' => $data->courseid
, 'enrol' => $this->get_name()))) {
335 $this->add_instance($course, (array)$data);
338 // No need to set mapping, we do not restore users or roles here.
339 $step->set_mapping('enrol', $oldid, 0);
343 * Is it possible to delete enrol instance via standard UI?
345 * @param object $instance
348 public function can_delete_instance($instance) {
349 $context = context_course
::instance($instance->courseid
);
350 return has_capability('enrol/guest:config', $context);
354 * Is it possible to hide/show enrol instance via standard UI?
356 * @param stdClass $instance
359 public function can_hide_show_instance($instance) {
360 $context = context_course
::instance($instance->courseid
);
361 if (!has_capability('enrol/guest:config', $context)) {
365 // If the instance is currently disabled, before it can be enabled, we must check whether the password meets the
366 // password policies.
367 if ($instance->status
== ENROL_INSTANCE_DISABLED
) {
368 if ($this->get_config('requirepassword')) {
369 if (empty($instance->password
)) {
374 if ($this->get_config('usepasswordpolicy')) {
375 if (!check_password_policy($instance->password
, $errmsg)) {
385 * Get default settings for enrol_guest.
389 public function get_instance_defaults() {
391 $fields['status'] = $this->get_config('status');