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