forked from ucla/moodle-enrol_invitation
-
Notifications
You must be signed in to change notification settings - Fork 8
/
enrol.php
executable file
·203 lines (169 loc) · 7.46 KB
/
enrol.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
// This file is part of the UCLA Site Invitation Plugin for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This page will try to enrol the user.
*
* @package enrol_invitation
* @copyright 2013 UC Regents
* @copyright 2011 Jerome Mouneyrac {@link http://www.moodleitandme.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
require($CFG->dirroot . '/enrol/invitation/locallib.php');
require_login(null, false);
// Check if param token exist. Support checking for both old
// "enrolinvitationtoken" token name and new "token" parameters.
$enrolinvitationtoken = optional_param('enrolinvitationtoken', null, PARAM_ALPHANUM);
if (empty($enrolinvitationtoken)) {
$enrolinvitationtoken = required_param('token', PARAM_ALPHANUM);
}
// Retrieve the token info.
$invitation = $DB->get_record('enrol_invitation',
array('token' => $enrolinvitationtoken, 'tokenused' => false));
// If token is valid, enrol the user into the course.
if (empty($invitation) or empty($invitation->courseid) or $invitation->timeexpiration < time()) {
$courseid = empty($invitation->courseid) ? $SITE->id : $invitation->courseid;
\enrol_invitation\event\invitation_attempted::create([
'objectid' => $courseid,
'context' => $PAGE->context,
'other' => [
'courseid' => $courseid,
'errormsg' => 'expired'
]
])->trigger();
throw new moodle_exception('expiredtoken', 'enrol_invitation');
}
// Make sure that course exists.
$course = $DB->get_record('course', array('id' => $invitation->courseid), '*', MUST_EXIST);
$context = context_course::instance($course->id);
// Set up page.
$PAGE->set_context($context);
$PAGE->set_url(new moodle_url('/enrol/invitation/enrol.php',
array('token' => $enrolinvitationtoken)));
$PAGE->set_pagelayout('course');
$PAGE->set_course($course);
$pagetitle = get_string('invitation_acceptance_title', 'enrol_invitation');
$PAGE->set_heading($pagetitle);
$PAGE->set_title($pagetitle);
$PAGE->navbar->add($pagetitle);
// Get.
$invitationmanager = new invitation_manager($invitation->courseid);
$instance = $invitationmanager->get_invitation_instance($invitation->courseid);
// First multiple check related to the invitation plugin config.
// @Todo better handle exceptions here.
if (isguestuser()) {
// Can not enrol guest!!
echo $OUTPUT->header();
// Print out a heading.
echo $OUTPUT->heading($pagetitle, 2, 'headingblock');
echo $OUTPUT->box_start('generalbox', 'notice');
$notice_object = prepare_notice_object($invitation);
echo get_string('loggedinnot', 'enrol_invitation', $notice_object);
$loginbutton = new single_button(new moodle_url($CFG->wwwroot
. '/login/index.php'), get_string('login'));
echo $OUTPUT->render($loginbutton);
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
exit;
}
// Have invitee confirm their acceptance of the site invitation.
$confirm = optional_param('confirm', 0, PARAM_BOOL);
if (empty($confirm)) {
echo $OUTPUT->header();
// Print out a heading.
echo $OUTPUT->heading($pagetitle, 2, 'headingblock');
\enrol_invitation\event\invitation_viewed::create([
'objectid' => $course->id,
'context' => context_course::instance($course->id),
'other' => [
'courseid' => $course->id
]
])->trigger();
$accepturl = new moodle_url('/enrol/invitation/enrol.php',
array('token' => $invitation->token, 'confirm' => true));
$accept = new single_button($accepturl,
get_string('invitationacceptancebutton', 'enrol_invitation'), 'get');
$cancel = new moodle_url('/');
$notice_object = prepare_notice_object($invitation);
$invitationacceptance = get_string('invitationacceptance',
'enrol_invitation', $notice_object);
// If invitation has "daysexpire" set, then give notice.
if (!empty($invitation->daysexpire)) {
$invitationacceptance .= html_writer::tag('p',
get_string('daysexpire_notice', 'enrol_invitation',
$invitation->daysexpire));
}
echo $OUTPUT->confirm($invitationacceptance, $accept, $cancel);
echo $OUTPUT->footer();
exit;
} else {
if ($invitation->email != $USER->email) {
\enrol_invitation\event\invitation_attempted::create([
'objectid' => $invitation->courseid,
'context' => $context,
'other' => [
'courseid' => $invitation->courseid,
'errormsg' => 'email does not match'
]
])->trigger();
}
// User confirmed, so add them.
require_once($CFG->dirroot . '/enrol/invitation/locallib.php');
$invitationmanager = new invitation_manager($invitation->courseid);
$invitationmanager->enroluser($invitation);
\enrol_invitation\event\invitation_accepted::create([
'objectid' => $invitation->courseid,
'context' => $context,
'other' => [
'courseid' => $invitation->courseid,
'errormsg' => 'email does not match'
]
])->trigger();
// Set token as used and mark which user was assigned the token.
$invitation->tokenused = true;
$invitation->timeused = time();
$invitation->userid = $USER->id;
$DB->update_record('enrol_invitation', $invitation);
if (!empty($invitation->notify_inviter)) {
// Send an email to the user who sent the invitation.
$inviter = $DB->get_record('user', array('id' => $invitation->inviterid));
$contactuser = new stdClass();
$contactuser->email = $inviter->email;
$contactuser->id = $invitation->inviterid; // required by new version of email_to_user since moodle 2.6
$contactuser->firstname = $inviter->firstname;
$contactuser->lastname = $inviter->lastname;
$contactuser->maildisplay = true;
$emailinfo = prepare_notice_object($invitation);
$emailinfo->userfullname = trim($USER->firstname . ' ' . $USER->lastname);
$emailinfo->useremail = $USER->email;
$courseenrolledusersurl = new moodle_url('/user/index.php',
array('id' => $invitation->courseid));
$emailinfo->courseenrolledusersurl = $courseenrolledusersurl->out(false);
$invitehistoryurl = new moodle_url('/enrol/invitation/history.php',
array('courseid' => $invitation->courseid));
$emailinfo->invitehistoryurl = $invitehistoryurl->out(false);
$course = $DB->get_record('course', array('id' => $invitation->courseid));
$emailinfo->coursefullname = sprintf('%s: %s', $course->shortname, $course->fullname);
$emailinfo->sitename = $SITE->fullname;
$siteurl = new moodle_url('/');
$emailinfo->siteurl = $siteurl->out(false);
email_to_user($contactuser, get_admin(),
get_string('emailtitleuserenrolled', 'enrol_invitation', $emailinfo),
get_string('emailmessageuserenrolled', 'enrol_invitation', $emailinfo));
}
$courseurl = new moodle_url('/course/view.php', array('id' => $invitation->courseid));
redirect($courseurl);
}