MDL-53471 tool_lp: Hooking in to report when scale is being used
[moodle.git] / admin / tool / lp / lib.php
CommitLineData
d9a39950
DW
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 * This page contains navigation hooks for learning plans.
19 *
20 * @package tool_lp
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
dd1ce763 25defined('MOODLE_INTERNAL') || die();
6cc8480a 26require_once($CFG->libdir . '/externallib.php');
d9a39950
DW
27
28/**
29 * This function extends the course navigation
30 *
31 * @param navigation_node $navigation The navigation node to extend
32 * @param stdClass $course The course to object for the tool
33 * @param context $coursecontext The context of the course
34 */
35function tool_lp_extend_navigation_course($navigation, $course, $coursecontext) {
338386aa
FM
36 if (!\tool_lp\api::is_enabled()) {
37 return;
38 }
39
d9a39950
DW
40 // Just a link to course report.
41 $title = get_string('coursecompetencies', 'tool_lp');
42 $path = new moodle_url("/admin/tool/lp/coursecompetencies.php", array('courseid' => $course->id));
43 $settingsnode = navigation_node::create($title,
44 $path,
45 navigation_node::TYPE_SETTING,
46 null,
47 null,
48 new pix_icon('competency', '', 'tool_lp'));
49 if (isset($settingsnode)) {
50 $navigation->add_node($settingsnode);
51 }
52}
4db373d5 53
5c7383fd
JPG
54
55/**
56 * This function extends the user navigation.
57 *
58 * @param navigation_node $navigation The navigation node to extend
59 * @param stdClass $user The user object
60 * @param context_user $usercontext The user context
61 * @param stdClass $course The course object
62 * @param context_course $coursecontext The context of the course
63 */
64function tool_lp_extend_navigation_user($navigation, $user, $usercontext, $course, $coursecontext) {
338386aa
FM
65 if (!\tool_lp\api::is_enabled()) {
66 return;
67 }
68
f5acab74
DW
69 if (\tool_lp\plan::can_read_user($user->id)) {
70 $node = $navigation->add(get_string('learningplans', 'tool_lp'),
71 new moodle_url('/admin/tool/lp/plans.php', array('userid' => $user->id)));
72 }
6c2b163c
FM
73
74 if (\tool_lp\user_evidence::can_read_user($user->id)) {
75 $node->add(get_string('userevidence', 'tool_lp'),
76 new moodle_url('/admin/tool/lp/user_evidence_list.php', array('userid' => $user->id)));
77 }
5c7383fd
JPG
78}
79
4db373d5
DM
80/**
81 * Add nodes to myprofile page.
82 *
83 * @param \core_user\output\myprofile\tree $tree Tree object
84 * @param stdClass $user user object
85 * @param bool $iscurrentuser
86 * @param stdClass $course Course object
87 *
88 * @return bool
89 */
90function tool_lp_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
338386aa
FM
91 if (!\tool_lp\api::is_enabled()) {
92 return false;
93 } else if (!\tool_lp\plan::can_read_user($user->id)) {
4db373d5
DM
94 return false;
95 }
96
5c7383fd 97 $url = new moodle_url('/admin/tool/lp/plans.php', array('userid' => $user->id));
4db373d5
DM
98 $node = new core_user\output\myprofile\node('miscellaneous', 'learningplans',
99 get_string('learningplans', 'tool_lp'), null, $url);
100 $tree->add_node($node);
5c7383fd
JPG
101
102 return true;
4db373d5 103}
904291f2
IT
104
105/**
106 * This function extends the category navigation to add learning plan links.
107 *
108 * @param navigation_node $navigation The navigation node to extend
109 * @param context $coursecategorycontext The context of the course category
110 */
111function tool_lp_extend_navigation_category_settings($navigation, $coursecategorycontext) {
338386aa
FM
112 if (!\tool_lp\api::is_enabled()) {
113 return false;
114 }
115
904291f2 116 // We check permissions before renderring the links.
935e9e90 117 $templatereadcapability = \tool_lp\template::can_read_context($coursecategorycontext);
904291f2 118 $competencymanagecapability = has_capability('tool/lp:competencymanage', $coursecategorycontext);
935e9e90 119 if (!$templatereadcapability && !$competencymanagecapability) {
904291f2
IT
120 return false;
121 }
122
123 // The link to the learning plan page.
935e9e90 124 if ($templatereadcapability) {
c11d30e2 125 $title = get_string('templates', 'tool_lp');
904291f2
IT
126 $path = new moodle_url("/admin/tool/lp/learningplans.php", array('pagecontextid' => $coursecategorycontext->id));
127 $settingsnode = navigation_node::create($title,
128 $path,
129 navigation_node::TYPE_SETTING,
130 null,
131 null,
132 new pix_icon('competency', '', 'tool_lp'));
133 if (isset($settingsnode)) {
134 $navigation->add_node($settingsnode);
135 }
136 }
137
138 // The link to the competency frameworks page.
139 if ($competencymanagecapability) {
140 $title = get_string('competencyframeworks', 'tool_lp');
141 $path = new moodle_url("/admin/tool/lp/competencyframeworks.php", array('pagecontextid' => $coursecategorycontext->id));
142 $settingsnode = navigation_node::create($title,
143 $path,
144 navigation_node::TYPE_SETTING,
145 null,
146 null,
147 new pix_icon('competency', '', 'tool_lp'));
148 if (isset($settingsnode)) {
149 $navigation->add_node($settingsnode);
150 }
151 }
6c2b163c
FM
152}
153
154
155/**
156 * File serving.
157 *
158 * @param stdClass $course The course object.
159 * @param stdClass $cm The cm object.
160 * @param context $context The context object.
161 * @param string $filearea The file area.
162 * @param array $args List of arguments.
163 * @param bool $forcedownload Whether or not to force the download of the file.
164 * @param array $options Array of options.
165 * @return void|false
166 */
167function tool_lp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
168 global $CFG;
169
338386aa
FM
170 if (!\tool_lp\api::is_enabled()) {
171 return false;
172 }
173
6c2b163c
FM
174 $fs = get_file_storage();
175 $file = null;
176
177 $itemid = array_shift($args);
178 $filename = array_shift($args);
179 $filepath = $args ? '/' .implode('/', $args) . '/' : '/';
180
181 if ($filearea == 'userevidence' && $context->contextlevel == CONTEXT_USER) {
182 if (\tool_lp\user_evidence::can_read_user($context->instanceid)) {
183 $file = $fs->get_file($context->id, 'tool_lp', $filearea, $itemid, $filepath, $filename);
184 }
185 }
186
187 if (!$file) {
188 return false;
189 }
190
191 send_stored_file($file, null, 0, $forcedownload);
192}
c008e92d
FM
193
194/**
195 * Hook when a comment is added.
196 *
197 * @param stdClass $comment The comment.
198 * @param stdClass $params The parameters.
199 * @return array
200 */
201function tool_lp_comment_add($comment, $params) {
6cc8480a
FM
202 global $USER;
203
338386aa
FM
204 if (!\tool_lp\api::is_enabled()) {
205 return;
206 }
207
c008e92d
FM
208 if ($params->commentarea == 'user_competency') {
209 $uc = new \tool_lp\user_competency($params->itemid);
210
6cc8480a
FM
211 // Message both the user and the reviewer, except when they are the author of the message.
212 $recipients = array($uc->get_userid());
213 if ($uc->get_reviewerid()) {
214 $recipients[] = $uc->get_reviewerid();
215 }
216 $recipients = array_diff($recipients, array($comment->userid));
217 if (empty($recipients)) {
c008e92d
FM
218 return;
219 }
220
6cc8480a
FM
221 // Get the sender.
222 $user = $USER;
223 if ($USER->id != $comment->userid) {
224 $user = core_user::get_user($comment->userid);
225 }
226 $fullname = fullname($user);
227
228 // Get the competency.
229 $competency = $uc->get_competency();
230 $competencyname = format_string($competency->get_shortname(), true, array('context' => $competency->get_context()));
231
232 // We want to send a message for one plan, trying to find an active one first, or the last modified one.
233 $plan = null;
234 $plans = $uc->get_plans();
235 foreach ($plans as $candidate) {
236 if ($candidate->get_status() == \tool_lp\plan::STATUS_ACTIVE) {
237 $plan = $candidate;
238 break;
239
240 } else if (!empty($plan) && $plan->get_timemodified() < $candidate->get_timemodified()) {
241 $plan = $candidate;
242
243 } else if (empty($plan)) {
244 $plan = $candidate;
245 }
246 }
247
248 // Urls.
182f6092 249 // TODO MDL-52749 Replace the link to the plan with the user competency page.
6cc8480a
FM
250 if (empty($plan)) {
251 $urlname = get_string('userplans', 'tool_lp');
252 $url = new moodle_url('/admin/tool/lp/plans.php', array('userid' => $uc->get_userid()));
253 } else {
254 $urlname = $competencyname;
255 $url = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
256 'userid' => $uc->get_userid(),
257 'competencyid' => $uc->get_competencyid(),
258 'planid' => $plan->get_id()
259 ));
260 }
261
262 // Construct the message content.
263 $fullmessagehtml = get_string('usercommentedonacompetencyhtml', 'tool_lp', array(
264 'fullname' => $fullname,
265 'competency' => $competencyname,
266 'comment' => format_text($comment->content, $comment->format, array('context' => $params->context->id)),
267 'url' => $url->out(true),
268 'urlname' => $urlname,
269 ));
270 if ($comment->format == FORMAT_PLAIN || $comment->format == FORMAT_MOODLE) {
271 $format = FORMAT_MOODLE;
272 $fullmessage = get_string('usercommentedonacompetency', 'tool_lp', array(
273 'fullname' => $fullname,
274 'competency' => $competencyname,
275 'comment' => $comment->content,
276 'url' => $url->out(false),
277 ));
278 } else {
279 $format = FORMAT_HTML;
280 $fullmessage = $fullmessagehtml;
281 }
282
283 $message = new \core\message\message();
c008e92d
FM
284 $message->component = 'tool_lp';
285 $message->name = 'user_competency_comment';
182f6092
FM
286 $message->notification = 1;
287 $message->userfrom = core_user::get_noreply_user();
6cc8480a
FM
288 $message->subject = get_string('usercommentedonacompetencysubject', 'tool_lp', $fullname);
289 $message->fullmessage = $fullmessage;
290 $message->fullmessageformat = $format;
291 $message->fullmessagehtml = $fullmessagehtml;
292 $message->smallmessage = get_string('usercommentedonacompetencysmall', 'tool_lp', array(
293 'fullname' => $fullname,
294 'competency' => $competencyname,
295 ));
296 $message->contexturl = $url->out(false);
297 $message->contexturlname = $urlname;
298
3c230247
FM
299 // Message each recipient.
300 foreach ($recipients as $recipient) {
301 $msgcopy = clone($message);
302 $msgcopy->userto = $recipient;
303 message_send($msgcopy);
304 }
305
306 } else if ($params->commentarea == 'plan') {
307 $plan = new \tool_lp\plan($params->itemid);
308
309 // Message both the user and the reviewer, except when they are the author of the message.
310 $recipients = array($plan->get_userid());
311 if ($plan->get_reviewerid()) {
312 $recipients[] = $plan->get_reviewerid();
313 }
314 $recipients = array_diff($recipients, array($comment->userid));
315 if (empty($recipients)) {
316 return;
317 }
318
319 // Get the sender.
320 $user = $USER;
321 if ($USER->id != $comment->userid) {
322 $user = core_user::get_user($comment->userid);
323 }
324
325 $fullname = fullname($user);
326 $planname = format_string($plan->get_name(), true, array('context' => $plan->get_context()));
327 $urlname = $planname;
328 $url = new moodle_url('/admin/tool/lp/plan.php', array(
329 'id' => $plan->get_id()
330 ));
331
332 // Construct the message content.
333 $fullmessagehtml = get_string('usercommentedonaplanhtml', 'tool_lp', array(
334 'fullname' => $fullname,
335 'plan' => $planname,
336 'comment' => format_text($comment->content, $comment->format, array('context' => $params->context->id)),
337 'url' => $url->out(true),
338 'urlname' => $urlname,
339 ));
340 if ($comment->format == FORMAT_PLAIN || $comment->format == FORMAT_MOODLE) {
341 $format = FORMAT_MOODLE;
342 $fullmessage = get_string('usercommentedonaplan', 'tool_lp', array(
343 'fullname' => $fullname,
344 'plan' => $planname,
345 'comment' => $comment->content,
346 'url' => $url->out(false),
347 ));
348 } else {
349 $format = FORMAT_HTML;
350 $fullmessage = $fullmessagehtml;
351 }
352
353 $message = new \core\message\message();
354 $message->component = 'tool_lp';
355 $message->name = 'plan_comment';
356 $message->notification = 1;
357 $message->userfrom = core_user::get_noreply_user();
358 $message->subject = get_string('usercommentedonaplansubject', 'tool_lp', $fullname);
359 $message->fullmessage = $fullmessage;
360 $message->fullmessageformat = $format;
361 $message->fullmessagehtml = $fullmessagehtml;
362 $message->smallmessage = get_string('usercommentedonaplansmall', 'tool_lp', array(
363 'fullname' => $fullname,
364 'plan' => $planname,
365 ));
366 $message->contexturl = $url->out(false);
367 $message->contexturlname = $urlname;
368
6cc8480a
FM
369 // Message each recipient.
370 foreach ($recipients as $recipient) {
371 $msgcopy = clone($message);
372 $msgcopy->userto = $recipient;
373 message_send($msgcopy);
374 }
c008e92d
FM
375 }
376}
377
378/**
379 * Return the permissions of for the comments.
380 *
381 * @param stdClass $params The parameters.
382 * @return array
383 */
384function tool_lp_comment_permissions($params) {
338386aa
FM
385 if (!\tool_lp\api::is_enabled()) {
386 return array('post' => false, 'view' => false);
387 }
388
c008e92d
FM
389 if ($params->commentarea == 'user_competency') {
390 $uc = new \tool_lp\user_competency($params->itemid);
943989c2
FM
391 if ($uc->can_read()) {
392 return array('post' => $uc->can_comment(), 'view' => $uc->can_read_comments());
393 }
3c230247
FM
394 } else if ($params->commentarea == 'plan') {
395 $plan = new \tool_lp\plan($params->itemid);
396 if ($plan->can_read()) {
397 return array('post' => $plan->can_comment(), 'view' => $plan->can_read_comments());
398 }
c008e92d 399 }
3c230247 400
c008e92d
FM
401 return array('post' => false, 'view' => false);
402}
403
404/**
405 * Validates comments.
406 *
407 * @param stdClass $params The parameters.
182f6092 408 * @return bool
c008e92d
FM
409 */
410function tool_lp_comment_validate($params) {
338386aa
FM
411 if (!\tool_lp\api::is_enabled()) {
412 return false;
413 }
414
182f6092
FM
415 if ($params->commentarea == 'user_competency') {
416 if (!\tool_lp\user_competency::record_exists($params->itemid)) {
417 return false;
418 }
419 return true;
3c230247
FM
420 } else if ($params->commentarea == 'plan') {
421 if (!\tool_lp\plan::record_exists($params->itemid)) {
422 return false;
423 }
424 return true;
182f6092
FM
425 }
426 return false;
c008e92d 427}
db650737
DW
428
429/**
430 * Inject the competencies elements into all moodle module settings forms.
431 *
432 * @param moodleform $formwrapper The moodle quickforms wrapper object.
433 * @param MoodleQuickForm $mform The actual form object (required to modify the form).
434 */
435function tool_lp_coursemodule_standard_elements($formwrapper, $mform) {
436 global $CFG, $COURSE;
437
338386aa
FM
438 if (!\tool_lp\api::is_enabled()) {
439 return;
fe0ea879
FM
440 } else if (!has_capability('tool/lp:coursecompetencymanage', $formwrapper->get_context())) {
441 return;
338386aa
FM
442 }
443
db650737
DW
444 $mform->addElement('header', 'competenciessection', get_string('competencies', 'tool_lp'));
445
446 MoodleQuickForm::registerElementType('course_competencies',
447 "$CFG->dirroot/admin/tool/lp/classes/course_competencies_form_element.php",
448 'tool_lp_course_competencies_form_element');
449 $cmid = null;
450 if ($cm = $formwrapper->get_coursemodule()) {
451 $cmid = $cm->id;
452 }
453 $options = array(
454 'courseid' => $COURSE->id,
455 'cmid' => $cmid
456 );
457 $mform->addElement('course_competencies', 'competencies', get_string('modcompetencies', 'tool_lp'), $options);
458 $mform->addHelpButton('competencies', 'modcompetencies', 'tool_lp');
459 MoodleQuickForm::registerElementType('course_competency_rule',
460 "$CFG->dirroot/admin/tool/lp/classes/course_competency_rule_form_element.php",
461 'tool_lp_course_competency_rule_form_element');
462 // Reuse the same options.
463 $mform->addElement('course_competency_rule', 'competency_rule', get_string('uponcoursemodulecompletion', 'tool_lp'), $options);
464}
465
466/**
467 * Hook the add/edit of the course module.
468 *
469 * @param stdClass $data Data from the form submission.
96c2b847 470 * @param stdClass $course The course.
db650737
DW
471 */
472function tool_lp_coursemodule_edit_post_actions($data, $course) {
338386aa
FM
473 if (!\tool_lp\api::is_enabled()) {
474 return $data;
475 }
476
fe0ea879
FM
477 // It seems like the form did not contain any of the form fields, we can return.
478 if (!isset($data->competency_rule) && !isset($data->competencies)) {
479 return $data;
480 }
481
65c6d461
DW
482 // We bypass the API here and go direct to the persistent layer - because we don't want to do permission
483 // checks here - we need to load the real list of existing course module competencies.
484 $existing = \tool_lp\course_module_competency::list_course_module_competencies($data->coursemodule);
db650737 485
f446b2e1
DW
486 $existingids = array();
487 foreach ($existing as $cmc) {
488 array_push($existingids, $cmc->get_competencyid());
489 }
490
491 $newids = isset($data->competencies) ? $data->competencies : array();
db650737 492
f446b2e1
DW
493 $removed = array_diff($existingids, $newids);
494 $added = array_diff($newids, $existingids);
db650737
DW
495
496 foreach ($removed as $removedid) {
497 \tool_lp\api::remove_competency_from_course_module($data->coursemodule, $removedid);
498 }
499 foreach ($added as $addedid) {
500 \tool_lp\api::add_competency_to_course_module($data->coursemodule, $addedid);
501 }
502
f446b2e1
DW
503 if (isset($data->competency_rule)) {
504 // Now update the rules for each course_module_competency.
505 $current = \tool_lp\api::list_course_module_competencies_in_course_module($data->coursemodule);
506 foreach ($current as $coursemodulecompetency) {
507 \tool_lp\api::set_course_module_competency_ruleoutcome($coursemodulecompetency, $data->competency_rule);
508 }
db650737 509 }
f446b2e1
DW
510
511 return $data;
db650737 512}
4e89144c
FM
513
514/**
515 * Reports whether a scale is being used in the plugin.
516 *
517 * @param int $scaleid The scale ID.
518 * @return bool
519 */
520function tool_lp_scale_used_anywhere($scaleid) {
521 return \tool_lp\api::is_scale_used_anywhere($scaleid);
522}