MDL-22148 backup - support for manual/self/guest enrolments. Credit goes to Petr.
[moodle.git] / enrol / self / 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 * Self enrolment plugin.
20 *
c1dfc4a8
PS
21 * @package enrol
22 * @subpackage self
23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
df997f84
PS
25 */
26
27/**
28 * Self enrolment plugin implementation.
29 * @author Petr Skoda
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
32class enrol_self_plugin extends enrol_plugin {
33
9a13c5af
PS
34 /**
35 * Returns optional enrolment information icons.
36 *
37 * This is used in course list for quick overview of enrolment options.
38 *
39 * We are not using single instance parameter because sometimes
40 * we might want to prevent icon repetition when multiple instances
41 * of one type exist. One instance may also produce several icons.
42 *
43 * @param array $instances all enrol instances of this type in one course
44 * @return array of pix_icon
45 */
3ead116a 46 public function get_info_icons(array $instances) {
9a13c5af
PS
47 $key = false;
48 $nokey = false;
49 foreach ($instances as $instance) {
50 if ($instance->password or $instance->customint1) {
51 $key = true;
52 } else {
53 $nokey = true;
54 }
55 }
56 $icons = array();
57 if ($nokey) {
58 $icons[] = new pix_icon('withoutkey', get_string('pluginname', 'enrol_self'), 'enrol_self');
59 }
60 if ($key) {
61 $icons[] = new pix_icon('withkey', get_string('pluginname', 'enrol_self'), 'enrol_self');
62 }
63 return $icons;
3ead116a
PS
64 }
65
df997f84
PS
66 /**
67 * Returns localised name of enrol instance
68 *
69 * @param object $instance (null is accepted too)
70 * @return string
71 */
72 public function get_instance_name($instance) {
73 global $DB;
74
75 if (empty($instance->name)) {
76 if (!empty($instance->roleid) and $role = $DB->get_record('role', array('id'=>$instance->roleid))) {
77 $role = ' (' . role_get_name($role, get_context_instance(CONTEXT_COURSE, $instance->courseid)) . ')';
78 } else {
79 $role = '';
80 }
81 $enrol = $this->get_name();
82 return get_string('pluginname', 'enrol_'.$enrol) . $role;
83 } else {
84 return format_string($instance->name);
85 }
86 }
87
88 public function roles_protected() {
89 // users may tweak the roles later
90 return false;
91 }
92
93 public function allow_unenrol(stdClass $instance) {
94 // users with unenrol cap may unenrol other users manually manually
95 return true;
96 }
97
98 public function allow_manage(stdClass $instance) {
99 // users with manage cap may tweak period and status
100 return true;
101 }
102
217d0397
PS
103 public function show_enrolme_link(stdClass $instance) {
104 return ($instance->status == ENROL_INSTANCE_ENABLED);
105 }
106
c9f9c900 107 /**
5eb222fb 108 * Sets up navigation entries.
c9f9c900
PS
109 *
110 * @param object $instance
5eb222fb 111 * @return void
c9f9c900
PS
112 */
113 public function add_course_navigation($instancesnode, stdClass $instance) {
114 if ($instance->enrol !== 'self') {
115 throw new coding_exception('Invalid enrol instance type!');
116 }
117
118 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
119 if (has_capability('enrol/self:config', $context)) {
120 $managelink = new moodle_url('/enrol/self/edit.php', array('courseid'=>$instance->courseid, 'id'=>$instance->id));
121 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
122 }
123 }
124
125 /**
126 * Returns edit icons for the page with list of instances
127 * @param stdClass $instance
128 * @return array
129 */
130 public function get_action_icons(stdClass $instance) {
131 global $OUTPUT;
132
133 if ($instance->enrol !== 'self') {
134 throw new coding_exception('invalid enrol instance!');
135 }
136 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
137
138 $icons = array();
139
140 if (has_capability('enrol/self:config', $context)) {
141 $editlink = new moodle_url("/enrol/self/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id));
142 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon')));
143 }
144
145 return $icons;
146 }
147
df997f84
PS
148 /**
149 * Returns link to page which may be used to add new instance of enrolment plugin in course.
150 * @param int $courseid
151 * @return moodle_url page url
152 */
e25f2466 153 public function get_newinstance_link($courseid) {
c9f9c900
PS
154 $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
155
156 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/manual:config', $context)) {
df997f84
PS
157 return NULL;
158 }
159 // multiple instances supported - different roles with different password
c9f9c900 160 return new moodle_url('/enrol/self/edit.php', array('courseid'=>$courseid));
df997f84
PS
161 }
162
163 /**
164 * Creates course enrol form, checks if form submitted
165 * and enrols user if necessary. It can also redirect.
166 *
167 * @param stdClass $instance
168 * @return string html text, usually a form in a text box
169 */
170 public function enrol_page_hook(stdClass $instance) {
171 global $CFG, $OUTPUT, $SESSION, $USER, $DB;
172
173 if (isguestuser()) {
174 // can not enrol guest!!
175 return null;
176 }
177 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) {
178 //TODO: maybe we should tell them they are already enrolled, but can not access the course
179 return null;
180 }
181
14937414 182 if ($instance->enrolstartdate != 0 and $instance->enrolstartdate < time()) {
df997f84
PS
183 //TODO: inform that we can not enrol yet
184 return null;
185 }
186
14937414 187 if ($instance->enrolenddate != 0 and $instance->enrolenddate > time()) {
df997f84
PS
188 //TODO: inform that enrolment is not possible any more
189 return null;
190 }
191
af41d03d
PS
192 if ($instance->customint3 > 0) {
193 // max enrol limit specified
194 $count = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
195 if ($count >= $instance->customint3) {
196 // bad luck, no more self enrolments here
197 return $OUTPUT->notification(get_string('maxenrolledreached', 'enrol_self'));
198 }
199 }
200
df997f84 201 require_once("$CFG->dirroot/enrol/self/locallib.php");
52d58821
PS
202 require_once("$CFG->dirroot/group/lib.php");
203
df997f84
PS
204 $form = new enrol_self_enrol_form(NULL, $instance);
205 $instanceid = optional_param('instance', 0, PARAM_INT);
206
207 if ($instance->id == $instanceid) {
208 if ($data = $form->get_data()) {
209 $enrol = enrol_get_plugin('self');
2a6dcb72 210 $timestart = time();
df997f84 211 if ($instance->enrolperiod) {
2a6dcb72 212 $tineend = $timestart + $instance->enrolperiod;
df997f84 213 } else {
2a6dcb72 214 $tineend = 0;
df997f84
PS
215 }
216
217 $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $tineend);
218 add_to_log($instance->courseid, 'course', 'enrol', '../enrol/users.php?id='.$instance->courseid, $instance->courseid); //there should be userid somewhere!
52d58821
PS
219
220 if ($instance->password and $instance->customint1 and $data->enrolpassword !== $instance->password) {
221 // it must be a group enrolment, let's assign group too
222 $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id', 'id, enrolmentkey');
223 foreach ($groups as $group) {
224 if (empty($group->enrolmentkey)) {
225 continue;
226 }
227 if ($group->enrolmentkey === $data->enrolpassword) {
228 groups_add_member($group->id, $USER->id);
229 break;
230 }
231 }
232 }
df997f84 233 // send welcome
6e0b0a39 234 if ($instance->customint4) {
df997f84
PS
235 $this->email_welcome_message($instance, $USER);
236 }
237 }
238 }
239
240 ob_start();
241 $form->display();
242 $output = ob_get_clean();
243
244 return $OUTPUT->box($output);
245 }
246
df997f84
PS
247 /**
248 * Add new instance of enrol plugin with default settings.
249 * @param object $course
250 * @return int id of new instance
251 */
252 public function add_default_instance($course) {
8372f767 253 $fields = array('customint1' => $this->get_config('groupkey'),
770ab27f 254 'customint2' => $this->get_config('longtimenosee'),
af41d03d 255 'customint3' => $this->get_config('maxenrolled'),
6e0b0a39 256 'customint4' => $this->get_config('sendcoursewelcomemessage'),
8372f767
PS
257 'enrolperiod' => $this->get_config('enrolperiod', 0),
258 'status' => $this->get_config('status'),
259 'roleid' => $this->get_config('roleid', 0));
df997f84
PS
260
261 if ($this->get_config('requirepassword')) {
262 $fields['password'] = generate_password(20);
263 }
264
265 return $this->add_instance($course, $fields);
266 }
267
268 /**
269 * Send welcome email to specified user
270 *
271 * @param object $instance
272 * @param object $user user record
273 * @return void
274 */
275 protected function email_welcome_message($instance, $user) {
276 global $CFG, $DB;
277
278 $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
279
94b9c2e8 280 $a = new stdClass();
6e0b0a39
PS
281 $a->coursename = format_string($course->fullname);
282 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id";
283
284 if (trim($instance->customtext1) !== '') {
285 $message = $instance->customtext1;
286 $message = str_replace('{$a->coursename}', $a->coursename, $message);
287 $message = str_replace('{$a->profileurl}', $a->profileurl, $message);
df997f84 288 } else {
6e0b0a39 289 $message = get_string('welcometocoursetext', 'enrol_self', $a);
df997f84
PS
290 }
291
292 $subject = get_string('welcometocourse', 'enrol_self', format_string($course->fullname));
293
294 $context = get_context_instance(CONTEXT_COURSE, $course->id);
6e0b0a39 295 $rusers = array();
df997f84
PS
296 if (!empty($CFG->coursecontact)) {
297 $croles = explode(',', $CFG->coursecontact);
298 $rusers = get_role_users($croles, $context, true, '', 'r.sortorder ASC, u.lastname ASC');
299 }
300 if ($rusers) {
301 $contact = reset($rusers);
302 } else {
303 $contact = get_admin();
304 }
305
306 email_to_user($user, $contact, $subject, $message);
307 }
770ab27f
PS
308
309 /**
310 * Enrol self cron support
311 * @return void
312 */
313 public function cron() {
314 global $DB;
315
316 if (!enrol_is_enabled('self')) {
317 return;
318 }
319
320 $plugin = enrol_get_plugin('self');
321
322 $now = time();
323
cc1d9893
PS
324 //note: the logic of self enrolment guarantees that user logged in at least once (=== u.lastaccess set)
325 // and that user accessed course at least once too (=== user_lastaccess record exists)
326
770ab27f
PS
327 // first deal with users that did not log in for a really long time
328 $sql = "SELECT e.*, ue.userid
329 FROM {user_enrolments} ue
330 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0)
331 JOIN {user} u ON u.id = ue.userid
332 WHERE :now - u.lastaccess > e.customint2";
333 $rs = $DB->get_recordset_sql($sql, array('now'=>$now));
334 foreach ($rs as $instance) {
335 $userid = $instance->userid;
336 unset($instance->userid);
337 $plugin->unenrol_user($instance, $userid);
338 mtrace("unenrolling user $userid from course $instance->courseid as they have did not log in for $instance->customint2 days");
339 }
340 $rs->close();
341
342 // now unenrol from course user did not visit for a long time
343 $sql = "SELECT e.*, ue.userid
344 FROM {user_enrolments} ue
345 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0)
346 JOIN {user_lastaccess} ul ON (ul.userid = ue.userid AND ul.courseid = e.courseid)
347 WHERE :now - ul.timeaccess > e.customint2";
348 $rs = $DB->get_recordset_sql($sql, array('now'=>$now));
349 foreach ($rs as $instance) {
350 $userid = $instance->userid;
351 unset($instance->userid);
352 $plugin->unenrol_user($instance, $userid);
353 mtrace("unenrolling user $userid from course $instance->courseid as they have did not access course for $instance->customint2 days");
354 }
355 $rs->close();
356
357 flush();
358 }
df997f84
PS
359}
360
f2a9be5f
EL
361/**
362 * Indicates API features that the enrol plugin supports.
363 *
364 * @param string $feature
365 * @return mixed True if yes (some features may use other values)
366 */
367function enrol_self_supports($feature) {
368 switch($feature) {
369 case ENROL_RESTORE_TYPE: return ENROL_RESTORE_EXACT;
df997f84 370
f2a9be5f
EL
371 default: return null;
372 }
373}