-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
197 lines (170 loc) · 6.5 KB
/
locallib.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
<?php
// This file is part of 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/>.
/**
*
* @package {{PLUGIN_NAME}} {@link https://docs.moodle.org/dev/Frankenstyle}
* @copyright 2017 LearningWorks Ltd {@link http://www.learningworks.co.nz}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function local_bishop_mail_user(stdClass $user, progress_trace $trace = null) {
global $CFG, $DB;
static $config;
if (is_null($trace)) {
$trace = new null_progress_trace();
}
if (!isset($user->auth)) {
$trace->output('User auth not passed in user object');
return false;
}
$authplugin = get_auth_plugin($user->auth);
if ($authplugin->prevent_local_passwords()) {
$trace->output('User auth method prevents internal passwords');
return false;
}
if (! isset($config)) {
$config = get_config('local_bishop');
}
if (empty($config->userfield) or empty($config->matchregex)) {
$trace->output('Required config fields: userfield or matchregex not set');
return false;
}
$userfield = $config->userfield;
$matchregex = $config->matchregex;
if (empty($user->{$userfield})) {
$trace->output('User field ' . $userfield . ' not in object');
return false;
}
$matches = array();
if (! preg_match($matchregex, $user->{$userfield}, $matches)) {
$trace->output('No match found: ' . $matchregex . ' <> ' . $userfield);
return false;
}
// Following mustache variables are required in html message.
$required = array('username', 'newpassword', 'siteurl');
// Flag to use custom html message or default back to text lang string.
$usecustomhtmlmessage = false;
// Get site.
$site = get_site();
// Generate a new password.
$newpassword = generate_password();
update_internal_user_password($user, $newpassword, true);
// Force password change.
if ($authplugin->can_change_password()) {
set_user_preference('auth_forcepasswordchange', 1, $user);
}
$a = new stdClass();
$a->username = $user->username;
$a->newpassword = $newpassword;
$a->siteurl = $CFG->wwwroot .'/login/';
$a->fullname = fullname($user, true);
$a->firstname = $user->firstname;
$a->lastname = $user->lastname;
$a->sitename = format_string($site->fullname);
$a->signoff = generate_email_signoff();
// Get subject and body.
$subject = isset($config->subject) ? $config->subject : '';
$body = isset($config->body_text) ? $config->body_text : '';
$message = $body;
if ($subject <> '' && $message <> '') {
foreach (get_object_vars($a) as $name => $value) {
$variable = '{{'. $name. '}}';
$message = str_replace($variable, $value, $message, $count);
if (in_array($name, $required)) {
if (! $count) {
$trace->output('Failed to find placeholder' . $name);
break;
}
}
}
// We have all fields we need can use custom message.
$usecustomhtmlmessage = true;
}
if (! $usecustomhtmlmessage) {
// Plain text only.
$subject = get_string('templatesubject');
$messagetext = get_string('templatemessagetext', 'local_bishop', $a);
$messagehtml = text_to_html($messagetext, null, false, true);
} else {
// HTML custom.
$messagehtml = format_text($message, FORMAT_HTML);
$messagetext = html_to_text($messagehtml);
$user->mailformat = FORMAT_HTML;
}
// Deal with attachment.
$fs = get_file_storage();
$files = $fs->get_area_files(
context_system::instance()->id,
'local_bishop',
'attachment',
1,
'itemid, filepath, filename',
false
);
if ($files) {
$file = reset($files);
$attachment = $file->copy_content_to_temp();
$attachmentname = $file->get_filename();
}
// Mail out from support user.
$contact = core_user::get_support_user();
// Directly emailing welcome message rather than using messaging.
if (isset($attachment)) {
$status = email_to_user($user, $contact, $subject, $messagetext, $messagehtml, $attachment, $attachmentname);
} else {
$status = email_to_user($user, $contact, $subject, $messagetext, $messagehtml);
}
// Log delivery.
$log = new stdClass();
$log->userid = $user->id;
$log->delivered = $status;
$log->time = time();
$DB->insert_record('local_bishop_email_log', $log);
if ($status) {
$trace->output('SUCESSFULLY sent '. $a->fullname . ' new user details.');
} else {
$trace->output('FAILED to sent '. $a->fullname . ' new user details.');
}
return $status;
}
function local_bishop_queue_user(stdClass $user, progress_trace $trace = null) {
global $DB;
if (is_null($trace)) {
$trace = new null_progress_trace();
}
$exists = $DB->record_exists('local_bishop_queue', array('id' => $user->id));
if (! $exists) {
$record = new stdClass();
$record->userid = $user->id;
$record->timecreated = time();
$record->id = $DB->insert_record('local_bishop_queue', $record);
}
return $record->id;
}
function local_bishop_process_queue(progress_trace $trace = null) {
global $DB;
if (is_null($trace)) {
$trace = new null_progress_trace();
}
// Process users in queue.
$rs = $DB->get_recordset('local_bishop_queue');
foreach ($rs as $record) {
$user = core_user::get_user($record->userid);
local_bishop_mail_user($user, $trace);
$DB->delete_records('local_bishop_queue', array('userid' => $record->userid));
}
$rs->close();
set_config('lastrunat', time(), 'local_bishop');
}