Moodle release 4.4beta
[moodle.git] / admin / tool / lp / lib.php
blob1160a63093b4e06bdf05dc46a6593ecd16c155a0
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 * This page contains navigation hooks for learning plans.
20 * @package tool_lp
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * This function extends the user navigation.
30 * @param navigation_node $navigation The navigation node to extend
31 * @param stdClass $user The user object
32 * @param context_user $usercontext The user context
33 * @param stdClass $course The course object
34 * @param context_course $coursecontext The context of the course
36 function tool_lp_extend_navigation_user($navigation, $user, $usercontext, $course, $coursecontext) {
37 if (!get_config('core_competency', 'enabled')) {
38 return;
41 if (\core_competency\plan::can_read_user($user->id)) {
42 $node = $navigation->add(get_string('learningplans', 'tool_lp'),
43 new moodle_url('/admin/tool/lp/plans.php', array('userid' => $user->id)));
45 if (\core_competency\user_evidence::can_read_user($user->id)) {
46 $node->add(get_string('userevidence', 'tool_lp'),
47 new moodle_url('/admin/tool/lp/user_evidence_list.php', array('userid' => $user->id)));
53 /**
54 * Add nodes to myprofile page.
56 * @param \core_user\output\myprofile\tree $tree Tree object
57 * @param stdClass $user user object
58 * @param bool $iscurrentuser
59 * @param stdClass $course Course object
61 * @return bool
63 function tool_lp_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
64 if (!get_config('core_competency', 'enabled')) {
65 return false;
66 } else if (!\core_competency\plan::can_read_user($user->id)) {
67 return false;
70 $url = new moodle_url('/admin/tool/lp/plans.php', array('userid' => $user->id));
71 $node = new core_user\output\myprofile\node('miscellaneous', 'learningplans',
72 get_string('learningplans', 'tool_lp'), null, $url);
73 $tree->add_node($node);
75 return true;
78 /**
79 * This function extends the category navigation to add learning plan links.
81 * @param navigation_node $navigation The navigation node to extend
82 * @param context $coursecategorycontext The context of the course category
84 function tool_lp_extend_navigation_category_settings($navigation, $coursecategorycontext) {
85 if (!get_config('core_competency', 'enabled')) {
86 return false;
89 // We check permissions before renderring the links.
90 $templatereadcapability = \core_competency\template::can_read_context($coursecategorycontext);
91 $competencyreadcapability = \core_competency\competency_framework::can_read_context($coursecategorycontext);
92 if (!$templatereadcapability && !$competencyreadcapability) {
93 return false;
96 // The link to the learning plan page.
97 if ($templatereadcapability) {
98 $title = get_string('templates', 'tool_lp');
99 $path = new moodle_url("/admin/tool/lp/learningplans.php", array('pagecontextid' => $coursecategorycontext->id));
100 $settingsnode = navigation_node::create($title,
101 $path,
102 navigation_node::TYPE_SETTING,
103 null,
104 'learningplantemplates',
105 new pix_icon('i/competencies', ''));
106 if (isset($settingsnode)) {
107 $settingsnode->set_force_into_more_menu(true);
108 $navigation->add_node($settingsnode);
112 // The link to the competency frameworks page.
113 if ($competencyreadcapability) {
114 $title = get_string('competencyframeworks', 'tool_lp');
115 $path = new moodle_url("/admin/tool/lp/competencyframeworks.php", array('pagecontextid' => $coursecategorycontext->id));
116 $settingsnode = navigation_node::create($title,
117 $path,
118 navigation_node::TYPE_SETTING,
119 null,
120 'competencyframeworks',
121 new pix_icon('i/competencies', ''));
122 if (isset($settingsnode)) {
123 $settingsnode->set_force_into_more_menu(true);
124 $navigation->add_node($settingsnode);
130 * Inject the competencies elements into all moodle module settings forms.
132 * @param moodleform $formwrapper The moodle quickforms wrapper object.
133 * @param MoodleQuickForm $mform The actual form object (required to modify the form).
135 function tool_lp_coursemodule_standard_elements($formwrapper, $mform) {
136 global $CFG, $COURSE;
138 if (!get_config('core_competency', 'enabled')) {
139 return;
140 } else if (!has_capability('moodle/competency:coursecompetencymanage', $formwrapper->get_context())) {
141 return;
144 $mform->addElement('header', 'competenciessection', get_string('competencies', 'core_competency'));
146 MoodleQuickForm::registerElementType('course_competencies',
147 "$CFG->dirroot/$CFG->admin/tool/lp/classes/course_competencies_form_element.php",
148 'tool_lp_course_competencies_form_element');
149 $cmid = null;
150 if ($cm = $formwrapper->get_coursemodule()) {
151 $cmid = $cm->id;
153 $options = array(
154 'courseid' => $COURSE->id,
155 'cmid' => $cmid
157 $mform->addElement('course_competencies', 'competencies', get_string('modcompetencies', 'tool_lp'), $options);
158 $mform->addHelpButton('competencies', 'modcompetencies', 'tool_lp');
159 MoodleQuickForm::registerElementType('course_competency_rule',
160 "$CFG->dirroot/$CFG->admin/tool/lp/classes/course_competency_rule_form_element.php",
161 'tool_lp_course_competency_rule_form_element');
162 // Reuse the same options.
163 $mform->addElement('course_competency_rule', 'competency_rule', get_string('uponcoursemodulecompletion', 'tool_lp'), $options);
165 $overrideelementfile = "$CFG->dirroot/$CFG->admin/tool/lp/classes/course_competency_overridegrade_form_element.php";
166 MoodleQuickForm::registerElementType('course_competency_overridegrade', $overrideelementfile,
167 'tool_lp_course_competency_overridegrade_form_element');
168 $mform->addElement('course_competency_overridegrade', 'override_grade', get_string('overridegrade', 'tool_lp'), $options);
169 $mform->hideIf('override_grade', 'competency_rule', 'noteq', \core_competency\competency::OUTCOME_COMPLETE + 1);
173 * Hook the add/edit of the course module.
175 * @param stdClass $data Data from the form submission.
176 * @param stdClass $course The course.
178 function tool_lp_coursemodule_edit_post_actions($data, $course) {
179 if (!get_config('core_competency', 'enabled')) {
180 return $data;
183 // It seems like the form did not contain any of the form fields, we can return.
184 if (!isset($data->competency_rule) && !isset($data->competencies)) {
185 return $data;
188 // We bypass the API here and go direct to the persistent layer - because we don't want to do permission
189 // checks here - we need to load the real list of existing course module competencies.
190 $existing = \core_competency\course_module_competency::list_course_module_competencies($data->coursemodule);
192 $existingids = array();
193 foreach ($existing as $cmc) {
194 array_push($existingids, $cmc->get('competencyid'));
197 $newids = isset($data->competencies) ? $data->competencies : array();
199 $removed = array_diff($existingids, $newids);
200 $added = array_diff($newids, $existingids);
202 foreach ($removed as $removedid) {
203 \core_competency\api::remove_competency_from_course_module($data->coursemodule, $removedid);
205 foreach ($added as $addedid) {
206 \core_competency\api::add_competency_to_course_module($data->coursemodule, $addedid);
209 if (isset($data->competency_rule)) {
210 $overridegrade = isset($data->override_grade) ? $data->override_grade : false;
212 // Now update the rules for each course_module_competency.
213 $current = \core_competency\api::list_course_module_competencies_in_course_module($data->coursemodule);
214 foreach ($current as $coursemodulecompetency) {
215 \core_competency\api::set_course_module_competency_ruleoutcome($coursemodulecompetency, $data->competency_rule,
216 $overridegrade);
220 return $data;
224 * Map icons for font-awesome themes.
226 function tool_lp_get_fontawesome_icon_map() {
227 return [
228 'tool_lp:url' => 'fa-external-link'
233 * Render a short bit of information about a competency.
235 * @param \core_competency\competency $competency The competency to show.
236 * @param \core_competency\competency_framework $framework The competency framework.
237 * @param boolean $includerelated If true, show related competencies.
238 * @param boolean $includecourses If true, show courses using this competency.
239 * @param boolean $skipenabled If true, show this competency even if competencies are disabled.
240 * @return string The html summary for the competency.
242 function tool_lp_render_competency_summary(\core_competency\competency $competency,
243 \core_competency\competency_framework $framework,
244 $includerelated,
245 $includecourses,
246 $skipenabled = false) {
247 global $PAGE;
249 if (!$skipenabled && !get_config('core_competency', 'enabled')) {
250 return;
253 $summary = new \tool_lp\output\competency_summary($competency, $framework, $includerelated, $includecourses);
255 $output = $PAGE->get_renderer('tool_lp');
257 return $output->render($summary);
261 * Inject a course competency picker into the form.
263 * @param MoodleQuickForm $mform The actual form object (required to modify the form).
264 * @param integer $courseid - SITEID or a course id
265 * @param context $context - The page context
266 * @param string $elementname - The name of the form element to create
268 function tool_lp_competency_picker($mform, $courseid, $context, $elementname) {
269 global $CFG, $COURSE;
271 if (!get_config('core_competency', 'enabled')) {
272 return;
275 if ($courseid == SITEID) {
276 if (!has_capability('moodle/competency:competencymanage', $context)) {
277 return;
280 MoodleQuickForm::registerElementType('site_competencies',
281 "$CFG->dirroot/$CFG->admin/tool/lp/classes/site_competencies_form_element.php",
282 'tool_lp_site_competencies_form_element');
283 $mform->addElement('site_competencies', $elementname);
284 } else {
285 if (!has_capability('moodle/competency:coursecompetencymanage', $context)) {
286 return;
289 MoodleQuickForm::registerElementType('course_competencies',
290 "$CFG->dirroot/$CFG->admin/tool/lp/classes/course_competencies_form_element.php",
291 'tool_lp_course_competencies_form_element');
292 $options = array(
293 'courseid' => $COURSE->id
295 $mform->addElement('course_competencies', $elementname, get_string('modcompetencies', 'tool_lp'), $options);
297 $mform->setType($elementname, PARAM_SEQUENCE);