MDL-23502 sorting lang strings again
[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 */
e25f2466 69 public function get_newinstance_link($courseid) {
df997f84
PS
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);
9f38cdce 151 $mform->addHelpButton('enrol_guest_status_'.$i, 'status', 'enrol_guest');
df997f84
PS
152 $mform->setDefault('enrol_guest_status_'.$i, $this->get_config('status'));
153 $mform->setAdvanced('enrol_guest_status_'.$i, $this->get_config('status_adv'));
154 if (!$config) {
155 $mform->hardFreeze('enrol_guest_status_'.$i);
156 }
157
158 $mform->addElement('passwordunmask', 'enrol_guest_password_'.$i, get_string('password', 'enrol_guest'));
9f38cdce 159 $mform->addHelpButton('enrol_guest_password_'.$i, 'password', 'enrol_guest');
df997f84
PS
160 if (!$config) {
161 $mform->hardFreeze('enrol_guest_password_'.$i);
162 } else {
163 $mform->disabledIf('enrol_guest_password_'.$i, 'enrol_guest_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED);
164 }
165
166
167 // now add all values from enrol table
168 if ($instance) {
169 foreach($instance as $key=>$val) {
170 $data->{'enrol_guest_'.$key.'_'.$i} = $val;
171 }
172 }
173 }
174
175 /**
176 * Validates course edit form data
177 *
178 * @param object $instance enrol instance or null if does not exist yet
179 * @param array $data
180 * @param object $context context of existing course or parent category if course does not exist
181 * @return array errors array
182 */
183 public function course_edit_validation($instance, array $data, $context) {
184 $errors = array();
185
186 if (!has_capability('enrol/guest:config', $context)) {
187 // we are going to ignore the data later anyway, they would nto be able to fix the form anyway
188 return $errors;
189 }
190
191 $i = isset($instance->id) ? $instance->id : 0;
192
193 $password = empty($data['enrol_guest_password_'.$i]) ? '' : $data['enrol_guest_password_'.$i];
194 $checkpassword = false;
195
196 if ($instance) {
197 if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) {
198 if ($instance->password !== $password) {
199 $checkpassword = true;
200 }
201 }
202 } else {
203 if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) {
204 $checkpassword = true;
205 }
206 }
207
208 if ($checkpassword) {
209 $require = $this->get_config('requirepassword');
210 $policy = $this->get_config('usepasswordpolicy');
211 if ($require and empty($password)) {
212 $errors['enrol_guest_password_'.$i] = get_string('required');
213 } else if ($policy) {
214 $errmsg = '';//prevent eclipse warning
215 if (!check_password_policy($password, $errmsg)) {
216 $errors['enrol_guest_password_'.$i] = $errmsg;
217 }
218 }
219 }
220
221 return $errors;
222 }
223
224 /**
225 * Called after updating/inserting course.
226 *
227 * @param bool $inserted true if course just inserted
228 * @param object $course
229 * @param object $data form data
230 * @return void
231 */
232 public function course_updated($inserted, $course, $data) {
233 global $DB;
234
235 $context = get_context_instance(CONTEXT_COURSE, $course->id);
236
237 if (has_capability('enrol/guest:config', $context)) {
238 if ($inserted) {
239 if (isset($data->enrol_guest_status_0)) {
240 $fields = array('status'=>$data->enrol_guest_status_0);
241 if ($fields['status'] == ENROL_INSTANCE_ENABLED) {
242 $fields['password'] = $data->enrol_guest_password_0;
243 } else {
244 if ($this->get_config('requirepassword')) {
245 $fields['password'] = generate_password(20);
246 }
247 }
248 $this->add_instance($course, $fields);
249 }
250 } else {
251 $instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'guest'));
252 foreach ($instances as $instance) {
253 $i = $instance->id;
254
255 if (isset($data->{'enrol_guest_status_'.$i})) {
256 $instance->status = $data->{'enrol_guest_status_'.$i};
257 $instance->timemodified = time();
258 if ($instance->status == ENROL_INSTANCE_ENABLED) {
259 $instance->password = $data->{'enrol_guest_password_'.$i};
260 }
261 $DB->update_record('enrol', $instance);
262 }
263 }
264 }
265
266 } else {
267 if ($inserted) {
268 if ($this->get_config('defaultenrol')) {
269 $this->add_default_instance($course);
270 }
271 } else {
272 // bad luck, user can not change anything
273 }
274 }
275 }
276
277 /**
278 * Add new instance of enrol plugin with default settings.
279 * @param object $course
280 * @return int id of new instance
281 */
282 public function add_default_instance($course) {
283 $fields = array('status'=>$this->get_config('status'));
284
285 if ($this->get_config('requirepassword')) {
286 $fields['password'] = generate_password(20);
287 }
288
289 return $this->add_instance($course, $fields);
290 }
291
292}
293