MDL-25476 messages: switched some notification emails to come from the support contac...
[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
c03ae033 156 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/self: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
03531e27 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
03531e27 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
192 require_once("$CFG->dirroot/enrol/self/locallib.php");
52d58821
PS
193 require_once("$CFG->dirroot/group/lib.php");
194
df997f84
PS
195 $form = new enrol_self_enrol_form(NULL, $instance);
196 $instanceid = optional_param('instance', 0, PARAM_INT);
197
198 if ($instance->id == $instanceid) {
199 if ($data = $form->get_data()) {
200 $enrol = enrol_get_plugin('self');
2a6dcb72 201 $timestart = time();
df997f84 202 if ($instance->enrolperiod) {
d587f077 203 $timeend = $timestart + $instance->enrolperiod;
df997f84 204 } else {
d587f077 205 $timeend = 0;
df997f84
PS
206 }
207
d587f077 208 $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
df997f84 209 add_to_log($instance->courseid, 'course', 'enrol', '../enrol/users.php?id='.$instance->courseid, $instance->courseid); //there should be userid somewhere!
52d58821
PS
210
211 if ($instance->password and $instance->customint1 and $data->enrolpassword !== $instance->password) {
212 // it must be a group enrolment, let's assign group too
213 $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id', 'id, enrolmentkey');
214 foreach ($groups as $group) {
215 if (empty($group->enrolmentkey)) {
216 continue;
217 }
218 if ($group->enrolmentkey === $data->enrolpassword) {
219 groups_add_member($group->id, $USER->id);
220 break;
221 }
222 }
223 }
df997f84 224 // send welcome
6e0b0a39 225 if ($instance->customint4) {
df997f84
PS
226 $this->email_welcome_message($instance, $USER);
227 }
228 }
229 }
230
231 ob_start();
232 $form->display();
233 $output = ob_get_clean();
234
235 return $OUTPUT->box($output);
236 }
237
df997f84
PS
238 /**
239 * Add new instance of enrol plugin with default settings.
240 * @param object $course
241 * @return int id of new instance
242 */
243 public function add_default_instance($course) {
8372f767 244 $fields = array('customint1' => $this->get_config('groupkey'),
770ab27f 245 'customint2' => $this->get_config('longtimenosee'),
af41d03d 246 'customint3' => $this->get_config('maxenrolled'),
6e0b0a39 247 'customint4' => $this->get_config('sendcoursewelcomemessage'),
8372f767
PS
248 'enrolperiod' => $this->get_config('enrolperiod', 0),
249 'status' => $this->get_config('status'),
250 'roleid' => $this->get_config('roleid', 0));
df997f84
PS
251
252 if ($this->get_config('requirepassword')) {
253 $fields['password'] = generate_password(20);
254 }
255
256 return $this->add_instance($course, $fields);
257 }
258
259 /**
260 * Send welcome email to specified user
261 *
262 * @param object $instance
263 * @param object $user user record
264 * @return void
265 */
266 protected function email_welcome_message($instance, $user) {
267 global $CFG, $DB;
268
269 $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
270
94b9c2e8 271 $a = new stdClass();
6e0b0a39
PS
272 $a->coursename = format_string($course->fullname);
273 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id";
274
275 if (trim($instance->customtext1) !== '') {
276 $message = $instance->customtext1;
277 $message = str_replace('{$a->coursename}', $a->coursename, $message);
278 $message = str_replace('{$a->profileurl}', $a->profileurl, $message);
df997f84 279 } else {
6e0b0a39 280 $message = get_string('welcometocoursetext', 'enrol_self', $a);
df997f84
PS
281 }
282
283 $subject = get_string('welcometocourse', 'enrol_self', format_string($course->fullname));
284
285 $context = get_context_instance(CONTEXT_COURSE, $course->id);
6e0b0a39 286 $rusers = array();
df997f84
PS
287 if (!empty($CFG->coursecontact)) {
288 $croles = explode(',', $CFG->coursecontact);
289 $rusers = get_role_users($croles, $context, true, '', 'r.sortorder ASC, u.lastname ASC');
290 }
291 if ($rusers) {
292 $contact = reset($rusers);
293 } else {
6737eda4 294 $contact = generate_email_supportuser();
df997f84
PS
295 }
296
d8f14128 297 //directly emailing welcome message rather than using messaging
df997f84
PS
298 email_to_user($user, $contact, $subject, $message);
299 }
770ab27f
PS
300
301 /**
302 * Enrol self cron support
303 * @return void
304 */
305 public function cron() {
306 global $DB;
307
308 if (!enrol_is_enabled('self')) {
309 return;
310 }
311
312 $plugin = enrol_get_plugin('self');
313
314 $now = time();
315
cc1d9893
PS
316 //note: the logic of self enrolment guarantees that user logged in at least once (=== u.lastaccess set)
317 // and that user accessed course at least once too (=== user_lastaccess record exists)
318
770ab27f
PS
319 // first deal with users that did not log in for a really long time
320 $sql = "SELECT e.*, ue.userid
321 FROM {user_enrolments} ue
322 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0)
323 JOIN {user} u ON u.id = ue.userid
324 WHERE :now - u.lastaccess > e.customint2";
325 $rs = $DB->get_recordset_sql($sql, array('now'=>$now));
326 foreach ($rs as $instance) {
327 $userid = $instance->userid;
328 unset($instance->userid);
329 $plugin->unenrol_user($instance, $userid);
330 mtrace("unenrolling user $userid from course $instance->courseid as they have did not log in for $instance->customint2 days");
331 }
332 $rs->close();
333
334 // now unenrol from course user did not visit for a long time
335 $sql = "SELECT e.*, ue.userid
336 FROM {user_enrolments} ue
337 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0)
338 JOIN {user_lastaccess} ul ON (ul.userid = ue.userid AND ul.courseid = e.courseid)
339 WHERE :now - ul.timeaccess > e.customint2";
340 $rs = $DB->get_recordset_sql($sql, array('now'=>$now));
341 foreach ($rs as $instance) {
342 $userid = $instance->userid;
343 unset($instance->userid);
344 $plugin->unenrol_user($instance, $userid);
345 mtrace("unenrolling user $userid from course $instance->courseid as they have did not access course for $instance->customint2 days");
346 }
347 $rs->close();
348
349 flush();
350 }
8c6e0ebe
SH
351
352 /**
353 * Gets an array of the user enrolment actions
354 *
355 * @param course_enrolment_manager $manager
356 * @param stdClass $ue A user enrolment object
357 * @return array An array of user_enrolment_actions
358 */
359 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
360 $actions = array();
361 $context = $manager->get_context();
362 $instance = $ue->enrolmentinstance;
363 $params = $manager->get_moodlepage()->url->params();
364 $params['ue'] = $ue->id;
365 if ($this->allow_unenrol($instance) && has_capability("enrol/self:unenrol", $context)) {
282b5cc7 366 $url = new moodle_url('/enrol/unenroluser.php', $params);
8c6e0ebe
SH
367 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
368 }
369 if ($this->allow_manage($instance) && has_capability("enrol/self:manage", $context)) {
370 $url = new moodle_url('/enrol/self/editenrolment.php', $params);
371 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id));
372 }
373 return $actions;
374 }
df997f84
PS
375}
376
f2a9be5f
EL
377/**
378 * Indicates API features that the enrol plugin supports.
379 *
380 * @param string $feature
381 * @return mixed True if yes (some features may use other values)
382 */
383function enrol_self_supports($feature) {
384 switch($feature) {
385 case ENROL_RESTORE_TYPE: return ENROL_RESTORE_EXACT;
df997f84 386
f2a9be5f
EL
387 default: return null;
388 }
389}