Moodle release 3.10.2
[moodle.git] / competency / lib.php
CommitLineData
7160fb19
FM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Competency lib.
19 *
20 * @package core_competency
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27use core_competency\api;
4aa6acb2 28use core_competency\plan;
07fc0ec3 29use core_competency\url;
4aa6acb2 30use core_competency\user_competency;
7160fb19
FM
31use core_competency\user_evidence;
32
4aa6acb2
FM
33/**
34 * Hook when a comment is added.
35 *
36 * @param stdClass $comment The comment.
37 * @param stdClass $params The parameters.
38 * @return array
39 */
40function core_competency_comment_add($comment, $params) {
36fa0ec9 41 global $USER, $PAGE;
4aa6acb2 42
5592edb6 43 if (!get_config('core_competency', 'enabled')) {
4aa6acb2
FM
44 return;
45 }
46
47 if ($params->commentarea == 'user_competency') {
48 $uc = new user_competency($params->itemid);
49
50 // Message both the user and the reviewer, except when they are the author of the message.
9c91a959
DW
51 $recipients = array($uc->get('userid'));
52 if ($uc->get('reviewerid')) {
53 $recipients[] = $uc->get('reviewerid');
4aa6acb2
FM
54 }
55 $recipients = array_diff($recipients, array($comment->userid));
56 if (empty($recipients)) {
57 return;
58 }
59
60 // Get the sender.
61 $user = $USER;
62 if ($USER->id != $comment->userid) {
63 $user = core_user::get_user($comment->userid);
64 }
65 $fullname = fullname($user);
66
67 // Get the competency.
68 $competency = $uc->get_competency();
9c91a959 69 $competencyname = format_string($competency->get('shortname'), true, array('context' => $competency->get_context()));
4aa6acb2
FM
70
71 // We want to send a message for one plan, trying to find an active one first, or the last modified one.
72 $plan = null;
73 $plans = $uc->get_plans();
74 foreach ($plans as $candidate) {
9c91a959 75 if ($candidate->get('status') == plan::STATUS_ACTIVE) {
4aa6acb2
FM
76 $plan = $candidate;
77 break;
78
9c91a959 79 } else if (!empty($plan) && $plan->get('timemodified') < $candidate->get('timemodified')) {
4aa6acb2
FM
80 $plan = $candidate;
81
82 } else if (empty($plan)) {
83 $plan = $candidate;
84 }
85 }
86
87 // Urls.
88 // TODO MDL-52749 Replace the link to the plan with the user competency page.
4aa6acb2 89 if (empty($plan)) {
e6dc03d7 90 $urlname = get_string('userplans', 'core_competency');
9c91a959 91 $url = url::plans($uc->get('userid'));
4aa6acb2
FM
92 } else {
93 $urlname = $competencyname;
9c91a959 94 $url = url::user_competency_in_plan($uc->get('userid'), $uc->get('competencyid'), $plan->get('id'));
4aa6acb2
FM
95 }
96
97 // Construct the message content.
e6dc03d7 98 $fullmessagehtml = get_string('usercommentedonacompetencyhtml', 'core_competency', array(
4aa6acb2
FM
99 'fullname' => $fullname,
100 'competency' => $competencyname,
101 'comment' => format_text($comment->content, $comment->format, array('context' => $params->context->id)),
102 'url' => $url->out(true),
103 'urlname' => $urlname,
104 ));
105 if ($comment->format == FORMAT_PLAIN || $comment->format == FORMAT_MOODLE) {
106 $format = FORMAT_MOODLE;
e6dc03d7 107 $fullmessage = get_string('usercommentedonacompetency', 'core_competency', array(
4aa6acb2
FM
108 'fullname' => $fullname,
109 'competency' => $competencyname,
110 'comment' => $comment->content,
111 'url' => $url->out(false),
112 ));
113 } else {
114 $format = FORMAT_HTML;
115 $fullmessage = $fullmessagehtml;
116 }
117
118 $message = new \core\message\message();
cc350fd9 119 $message->courseid = SITEID;
4aa6acb2
FM
120 $message->component = 'moodle';
121 $message->name = 'competencyusercompcomment';
122 $message->notification = 1;
123 $message->userfrom = core_user::get_noreply_user();
e6dc03d7 124 $message->subject = get_string('usercommentedonacompetencysubject', 'core_competency', $fullname);
4aa6acb2
FM
125 $message->fullmessage = $fullmessage;
126 $message->fullmessageformat = $format;
127 $message->fullmessagehtml = $fullmessagehtml;
e6dc03d7 128 $message->smallmessage = get_string('usercommentedonacompetencysmall', 'core_competency', array(
4aa6acb2
FM
129 'fullname' => $fullname,
130 'competency' => $competencyname,
131 ));
132 $message->contexturl = $url->out(false);
133 $message->contexturlname = $urlname;
134
36fa0ec9 135 $userpicture = new \user_picture($user);
dcadc8c5 136 $userpicture->size = 1; // Use f1 size.
4aa6acb2
FM
137 // Message each recipient.
138 foreach ($recipients as $recipient) {
139 $msgcopy = clone($message);
140 $msgcopy->userto = $recipient;
36fa0ec9
JL
141 // Generate an out-of-session token for the user receiving the message.
142 $userpicture->includetoken = $recipient;
143 $msgcopy->customdata = [
144 'notificationiconurl' => $userpicture->get_url($PAGE)->out(false),
145 ];
4aa6acb2
FM
146 message_send($msgcopy);
147 }
148
149 } else if ($params->commentarea == 'plan') {
150 $plan = new plan($params->itemid);
151
152 // Message both the user and the reviewer, except when they are the author of the message.
9c91a959
DW
153 $recipients = array($plan->get('userid'));
154 if ($plan->get('reviewerid')) {
155 $recipients[] = $plan->get('reviewerid');
4aa6acb2
FM
156 }
157 $recipients = array_diff($recipients, array($comment->userid));
158 if (empty($recipients)) {
159 return;
160 }
161
162 // Get the sender.
163 $user = $USER;
164 if ($USER->id != $comment->userid) {
165 $user = core_user::get_user($comment->userid);
166 }
167
168 $fullname = fullname($user);
9c91a959 169 $planname = format_string($plan->get('name'), true, array('context' => $plan->get_context()));
4aa6acb2 170 $urlname = $planname;
9c91a959 171 $url = url::plan($plan->get('id'));
4aa6acb2
FM
172
173 // Construct the message content.
e6dc03d7 174 $fullmessagehtml = get_string('usercommentedonaplanhtml', 'core_competency', array(
4aa6acb2
FM
175 'fullname' => $fullname,
176 'plan' => $planname,
177 'comment' => format_text($comment->content, $comment->format, array('context' => $params->context->id)),
178 'url' => $url->out(true),
179 'urlname' => $urlname,
180 ));
181 if ($comment->format == FORMAT_PLAIN || $comment->format == FORMAT_MOODLE) {
182 $format = FORMAT_MOODLE;
e6dc03d7 183 $fullmessage = get_string('usercommentedonaplan', 'core_competency', array(
4aa6acb2
FM
184 'fullname' => $fullname,
185 'plan' => $planname,
186 'comment' => $comment->content,
187 'url' => $url->out(false),
188 ));
189 } else {
190 $format = FORMAT_HTML;
191 $fullmessage = $fullmessagehtml;
192 }
193
194 $message = new \core\message\message();
cc350fd9 195 $message->courseid = SITEID;
4aa6acb2
FM
196 $message->component = 'moodle';
197 $message->name = 'competencyplancomment';
198 $message->notification = 1;
199 $message->userfrom = core_user::get_noreply_user();
e6dc03d7 200 $message->subject = get_string('usercommentedonaplansubject', 'core_competency', $fullname);
4aa6acb2
FM
201 $message->fullmessage = $fullmessage;
202 $message->fullmessageformat = $format;
203 $message->fullmessagehtml = $fullmessagehtml;
e6dc03d7 204 $message->smallmessage = get_string('usercommentedonaplansmall', 'core_competency', array(
4aa6acb2
FM
205 'fullname' => $fullname,
206 'plan' => $planname,
207 ));
208 $message->contexturl = $url->out(false);
209 $message->contexturlname = $urlname;
210
36fa0ec9 211 $userpicture = new \user_picture($user);
dcadc8c5 212 $userpicture->size = 1; // Use f1 size.
4aa6acb2
FM
213 // Message each recipient.
214 foreach ($recipients as $recipient) {
215 $msgcopy = clone($message);
216 $msgcopy->userto = $recipient;
36fa0ec9
JL
217 // Generate an out-of-session token for the user receiving the message.
218 $userpicture->includetoken = $recipient;
219 $msgcopy->customdata = [
220 'notificationiconurl' => $userpicture->get_url($PAGE)->out(false),
221 ];
4aa6acb2
FM
222 message_send($msgcopy);
223 }
224 }
225}
226
227/**
228 * Return the permissions of for the comments.
229 *
230 * @param stdClass $params The parameters.
231 * @return array
232 */
233function core_competency_comment_permissions($params) {
5592edb6 234 if (!get_config('core_competency', 'enabled')) {
4aa6acb2
FM
235 return array('post' => false, 'view' => false);
236 }
237
238 if ($params->commentarea == 'user_competency') {
239 $uc = new user_competency($params->itemid);
240 if ($uc->can_read()) {
241 return array('post' => $uc->can_comment(), 'view' => $uc->can_read_comments());
242 }
243 } else if ($params->commentarea == 'plan') {
244 $plan = new plan($params->itemid);
245 if ($plan->can_read()) {
246 return array('post' => $plan->can_comment(), 'view' => $plan->can_read_comments());
247 }
248 }
249
250 return array('post' => false, 'view' => false);
251}
252
253/**
254 * Validates comments.
255 *
256 * @param stdClass $params The parameters.
257 * @return bool
258 */
259function core_competency_comment_validate($params) {
5592edb6 260 if (!get_config('core_competency', 'enabled')) {
4aa6acb2
FM
261 return false;
262 }
263
264 if ($params->commentarea == 'user_competency') {
265 if (!user_competency::record_exists($params->itemid)) {
266 return false;
267 }
268 return true;
269 } else if ($params->commentarea == 'plan') {
270 if (!plan::record_exists($params->itemid)) {
271 return false;
272 }
273 return true;
274 }
275 return false;
276}
7160fb19
FM
277
278/**
279 * File serving.
280 *
281 * @param stdClass $course The course object.
282 * @param stdClass $cm The cm object.
283 * @param context $context The context object.
284 * @param string $filearea The file area.
285 * @param array $args List of arguments.
286 * @param bool $forcedownload Whether or not to force the download of the file.
287 * @param array $options Array of options.
288 * @return void|false
289 */
290function core_competency_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
291 global $CFG;
292
5592edb6 293 if (!get_config('core_competency', 'enabled')) {
7160fb19
FM
294 return false;
295 }
296
297 $fs = get_file_storage();
298 $file = null;
299
300 $itemid = array_shift($args);
301 $filename = array_shift($args);
302 $filepath = $args ? '/' .implode('/', $args) . '/' : '/';
303
304 if ($filearea == 'userevidence' && $context->contextlevel == CONTEXT_USER) {
305 if (user_evidence::can_read_user($context->instanceid)) {
306 $file = $fs->get_file($context->id, 'core_competency', $filearea, $itemid, $filepath, $filename);
6f88c268 307 $forcedownload = true;
7160fb19
FM
308 }
309 }
310
311 if (!$file) {
312 return false;
313 }
314
315 send_stored_file($file, null, 0, $forcedownload);
316}