Merge branch 'MDL-62945-master' of https://github.com/HuongNV13/moodle
[moodle.git] / competency / lib.php
blob1e5bdfd4f9df1fbcb586a2ee4a21541e80b333c7
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.
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/>.
17 /**
18 * Competency lib.
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
25 defined('MOODLE_INTERNAL') || die();
27 use core_competency\api;
28 use core_competency\plan;
29 use core_competency\url;
30 use core_competency\user_competency;
31 use core_competency\user_evidence;
33 /**
34 * Hook when a comment is added.
36 * @param stdClass $comment The comment.
37 * @param stdClass $params The parameters.
38 * @return array
40 function core_competency_comment_add($comment, $params) {
41 global $USER;
43 if (!get_config('core_competency', 'enabled')) {
44 return;
47 if ($params->commentarea == 'user_competency') {
48 $uc = new user_competency($params->itemid);
50 // Message both the user and the reviewer, except when they are the author of the message.
51 $recipients = array($uc->get('userid'));
52 if ($uc->get('reviewerid')) {
53 $recipients[] = $uc->get('reviewerid');
55 $recipients = array_diff($recipients, array($comment->userid));
56 if (empty($recipients)) {
57 return;
60 // Get the sender.
61 $user = $USER;
62 if ($USER->id != $comment->userid) {
63 $user = core_user::get_user($comment->userid);
65 $fullname = fullname($user);
67 // Get the competency.
68 $competency = $uc->get_competency();
69 $competencyname = format_string($competency->get('shortname'), true, array('context' => $competency->get_context()));
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) {
75 if ($candidate->get('status') == plan::STATUS_ACTIVE) {
76 $plan = $candidate;
77 break;
79 } else if (!empty($plan) && $plan->get('timemodified') < $candidate->get('timemodified')) {
80 $plan = $candidate;
82 } else if (empty($plan)) {
83 $plan = $candidate;
87 // Urls.
88 // TODO MDL-52749 Replace the link to the plan with the user competency page.
89 if (empty($plan)) {
90 $urlname = get_string('userplans', 'core_competency');
91 $url = url::plans($uc->get('userid'));
92 } else {
93 $urlname = $competencyname;
94 $url = url::user_competency_in_plan($uc->get('userid'), $uc->get('competencyid'), $plan->get('id'));
97 // Construct the message content.
98 $fullmessagehtml = get_string('usercommentedonacompetencyhtml', 'core_competency', array(
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,
105 if ($comment->format == FORMAT_PLAIN || $comment->format == FORMAT_MOODLE) {
106 $format = FORMAT_MOODLE;
107 $fullmessage = get_string('usercommentedonacompetency', 'core_competency', array(
108 'fullname' => $fullname,
109 'competency' => $competencyname,
110 'comment' => $comment->content,
111 'url' => $url->out(false),
113 } else {
114 $format = FORMAT_HTML;
115 $fullmessage = $fullmessagehtml;
118 $message = new \core\message\message();
119 $message->courseid = SITEID;
120 $message->component = 'moodle';
121 $message->name = 'competencyusercompcomment';
122 $message->notification = 1;
123 $message->userfrom = core_user::get_noreply_user();
124 $message->subject = get_string('usercommentedonacompetencysubject', 'core_competency', $fullname);
125 $message->fullmessage = $fullmessage;
126 $message->fullmessageformat = $format;
127 $message->fullmessagehtml = $fullmessagehtml;
128 $message->smallmessage = get_string('usercommentedonacompetencysmall', 'core_competency', array(
129 'fullname' => $fullname,
130 'competency' => $competencyname,
132 $message->contexturl = $url->out(false);
133 $message->contexturlname = $urlname;
135 // Message each recipient.
136 foreach ($recipients as $recipient) {
137 $msgcopy = clone($message);
138 $msgcopy->userto = $recipient;
139 message_send($msgcopy);
142 } else if ($params->commentarea == 'plan') {
143 $plan = new plan($params->itemid);
145 // Message both the user and the reviewer, except when they are the author of the message.
146 $recipients = array($plan->get('userid'));
147 if ($plan->get('reviewerid')) {
148 $recipients[] = $plan->get('reviewerid');
150 $recipients = array_diff($recipients, array($comment->userid));
151 if (empty($recipients)) {
152 return;
155 // Get the sender.
156 $user = $USER;
157 if ($USER->id != $comment->userid) {
158 $user = core_user::get_user($comment->userid);
161 $fullname = fullname($user);
162 $planname = format_string($plan->get('name'), true, array('context' => $plan->get_context()));
163 $urlname = $planname;
164 $url = url::plan($plan->get('id'));
166 // Construct the message content.
167 $fullmessagehtml = get_string('usercommentedonaplanhtml', 'core_competency', array(
168 'fullname' => $fullname,
169 'plan' => $planname,
170 'comment' => format_text($comment->content, $comment->format, array('context' => $params->context->id)),
171 'url' => $url->out(true),
172 'urlname' => $urlname,
174 if ($comment->format == FORMAT_PLAIN || $comment->format == FORMAT_MOODLE) {
175 $format = FORMAT_MOODLE;
176 $fullmessage = get_string('usercommentedonaplan', 'core_competency', array(
177 'fullname' => $fullname,
178 'plan' => $planname,
179 'comment' => $comment->content,
180 'url' => $url->out(false),
182 } else {
183 $format = FORMAT_HTML;
184 $fullmessage = $fullmessagehtml;
187 $message = new \core\message\message();
188 $message->courseid = SITEID;
189 $message->component = 'moodle';
190 $message->name = 'competencyplancomment';
191 $message->notification = 1;
192 $message->userfrom = core_user::get_noreply_user();
193 $message->subject = get_string('usercommentedonaplansubject', 'core_competency', $fullname);
194 $message->fullmessage = $fullmessage;
195 $message->fullmessageformat = $format;
196 $message->fullmessagehtml = $fullmessagehtml;
197 $message->smallmessage = get_string('usercommentedonaplansmall', 'core_competency', array(
198 'fullname' => $fullname,
199 'plan' => $planname,
201 $message->contexturl = $url->out(false);
202 $message->contexturlname = $urlname;
204 // Message each recipient.
205 foreach ($recipients as $recipient) {
206 $msgcopy = clone($message);
207 $msgcopy->userto = $recipient;
208 message_send($msgcopy);
214 * Return the permissions of for the comments.
216 * @param stdClass $params The parameters.
217 * @return array
219 function core_competency_comment_permissions($params) {
220 if (!get_config('core_competency', 'enabled')) {
221 return array('post' => false, 'view' => false);
224 if ($params->commentarea == 'user_competency') {
225 $uc = new user_competency($params->itemid);
226 if ($uc->can_read()) {
227 return array('post' => $uc->can_comment(), 'view' => $uc->can_read_comments());
229 } else if ($params->commentarea == 'plan') {
230 $plan = new plan($params->itemid);
231 if ($plan->can_read()) {
232 return array('post' => $plan->can_comment(), 'view' => $plan->can_read_comments());
236 return array('post' => false, 'view' => false);
240 * Validates comments.
242 * @param stdClass $params The parameters.
243 * @return bool
245 function core_competency_comment_validate($params) {
246 if (!get_config('core_competency', 'enabled')) {
247 return false;
250 if ($params->commentarea == 'user_competency') {
251 if (!user_competency::record_exists($params->itemid)) {
252 return false;
254 return true;
255 } else if ($params->commentarea == 'plan') {
256 if (!plan::record_exists($params->itemid)) {
257 return false;
259 return true;
261 return false;
265 * File serving.
267 * @param stdClass $course The course object.
268 * @param stdClass $cm The cm object.
269 * @param context $context The context object.
270 * @param string $filearea The file area.
271 * @param array $args List of arguments.
272 * @param bool $forcedownload Whether or not to force the download of the file.
273 * @param array $options Array of options.
274 * @return void|false
276 function core_competency_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
277 global $CFG;
279 if (!get_config('core_competency', 'enabled')) {
280 return false;
283 $fs = get_file_storage();
284 $file = null;
286 $itemid = array_shift($args);
287 $filename = array_shift($args);
288 $filepath = $args ? '/' .implode('/', $args) . '/' : '/';
290 if ($filearea == 'userevidence' && $context->contextlevel == CONTEXT_USER) {
291 if (user_evidence::can_read_user($context->instanceid)) {
292 $file = $fs->get_file($context->id, 'core_competency', $filearea, $itemid, $filepath, $filename);
293 $forcedownload = true;
297 if (!$file) {
298 return false;
301 send_stored_file($file, null, 0, $forcedownload);