lib.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. * This page contains navigation hooks for learning plans.
  18. *
  19. * @package tool_lp
  20. * @copyright 2015 Damyon Wiese
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * This function extends the course navigation
  26. *
  27. * @param navigation_node $navigation The navigation node to extend
  28. * @param stdClass $course The course to object for the tool
  29. * @param context $coursecontext The context of the course
  30. */
  31. function tool_lp_extend_navigation_course($navigation, $course, $coursecontext) {
  32. if (!get_config('core_competency', 'enabled')) {
  33. return;
  34. }
  35. // Check access to the course and competencies page.
  36. $capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage');
  37. $context = context_course::instance($course->id);
  38. if (!has_any_capability($capabilities, $context) || !can_access_course($course)) {
  39. return;
  40. }
  41. // Just a link to course competency.
  42. $title = get_string('competencies', 'core_competency');
  43. $path = new moodle_url("/admin/tool/lp/coursecompetencies.php", array('courseid' => $course->id));
  44. $settingsnode = navigation_node::create($title,
  45. $path,
  46. navigation_node::TYPE_SETTING,
  47. null,
  48. null,
  49. new pix_icon('i/competencies', ''));
  50. if (isset($settingsnode)) {
  51. $navigation->add_node($settingsnode);
  52. }
  53. }
  54. /**
  55. * This function extends the user navigation.
  56. *
  57. * @param navigation_node $navigation The navigation node to extend
  58. * @param stdClass $user The user object
  59. * @param context_user $usercontext The user context
  60. * @param stdClass $course The course object
  61. * @param context_course $coursecontext The context of the course
  62. */
  63. function tool_lp_extend_navigation_user($navigation, $user, $usercontext, $course, $coursecontext) {
  64. if (!get_config('core_competency', 'enabled')) {
  65. return;
  66. }
  67. if (\core_competency\plan::can_read_user($user->id)) {
  68. $node = $navigation->add(get_string('learningplans', 'tool_lp'),
  69. new moodle_url('/admin/tool/lp/plans.php', array('userid' => $user->id)));
  70. if (\core_competency\user_evidence::can_read_user($user->id)) {
  71. $node->add(get_string('userevidence', 'tool_lp'),
  72. new moodle_url('/admin/tool/lp/user_evidence_list.php', array('userid' => $user->id)));
  73. }
  74. }
  75. }
  76. /**
  77. * Add nodes to myprofile page.
  78. *
  79. * @param \core_user\output\myprofile\tree $tree Tree object
  80. * @param stdClass $user user object
  81. * @param bool $iscurrentuser
  82. * @param stdClass $course Course object
  83. *
  84. * @return bool
  85. */
  86. function tool_lp_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
  87. if (!get_config('core_competency', 'enabled')) {
  88. return false;
  89. } else if (!\core_competency\plan::can_read_user($user->id)) {
  90. return false;
  91. }
  92. $url = new moodle_url('/admin/tool/lp/plans.php', array('userid' => $user->id));
  93. $node = new core_user\output\myprofile\node('miscellaneous', 'learningplans',
  94. get_string('learningplans', 'tool_lp'), null, $url);
  95. $tree->add_node($node);
  96. return true;
  97. }
  98. /**
  99. * This function extends the category navigation to add learning plan links.
  100. *
  101. * @param navigation_node $navigation The navigation node to extend
  102. * @param context $coursecategorycontext The context of the course category
  103. */
  104. function tool_lp_extend_navigation_category_settings($navigation, $coursecategorycontext) {
  105. if (!get_config('core_competency', 'enabled')) {
  106. return false;
  107. }
  108. // We check permissions before renderring the links.
  109. $templatereadcapability = \core_competency\template::can_read_context($coursecategorycontext);
  110. $competencyreadcapability = \core_competency\competency_framework::can_read_context($coursecategorycontext);
  111. if (!$templatereadcapability && !$competencyreadcapability) {
  112. return false;
  113. }
  114. // The link to the learning plan page.
  115. if ($templatereadcapability) {
  116. $title = get_string('templates', 'tool_lp');
  117. $path = new moodle_url("/admin/tool/lp/learningplans.php", array('pagecontextid' => $coursecategorycontext->id));
  118. $settingsnode = navigation_node::create($title,
  119. $path,
  120. navigation_node::TYPE_SETTING,
  121. null,
  122. null,
  123. new pix_icon('i/competencies', ''));
  124. if (isset($settingsnode)) {
  125. $navigation->add_node($settingsnode);
  126. }
  127. }
  128. // The link to the competency frameworks page.
  129. if ($competencyreadcapability) {
  130. $title = get_string('competencyframeworks', 'tool_lp');
  131. $path = new moodle_url("/admin/tool/lp/competencyframeworks.php", array('pagecontextid' => $coursecategorycontext->id));
  132. $settingsnode = navigation_node::create($title,
  133. $path,
  134. navigation_node::TYPE_SETTING,
  135. null,
  136. null,
  137. new pix_icon('i/competencies', ''));
  138. if (isset($settingsnode)) {
  139. $navigation->add_node($settingsnode);
  140. }
  141. }
  142. }
  143. /**
  144. * Inject the competencies elements into all moodle module settings forms.
  145. *
  146. * @param moodleform $formwrapper The moodle quickforms wrapper object.
  147. * @param MoodleQuickForm $mform The actual form object (required to modify the form).
  148. */
  149. function tool_lp_coursemodule_standard_elements($formwrapper, $mform) {
  150. global $CFG, $COURSE;
  151. if (!get_config('core_competency', 'enabled')) {
  152. return;
  153. } else if (!has_capability('moodle/competency:coursecompetencymanage', $formwrapper->get_context())) {
  154. return;
  155. }
  156. $mform->addElement('header', 'competenciessection', get_string('competencies', 'core_competency'));
  157. MoodleQuickForm::registerElementType('course_competencies',
  158. "$CFG->dirroot/admin/tool/lp/classes/course_competencies_form_element.php",
  159. 'tool_lp_course_competencies_form_element');
  160. $cmid = null;
  161. if ($cm = $formwrapper->get_coursemodule()) {
  162. $cmid = $cm->id;
  163. }
  164. $options = array(
  165. 'courseid' => $COURSE->id,
  166. 'cmid' => $cmid
  167. );
  168. $mform->addElement('course_competencies', 'competencies', get_string('modcompetencies', 'tool_lp'), $options);
  169. $mform->addHelpButton('competencies', 'modcompetencies', 'tool_lp');
  170. MoodleQuickForm::registerElementType('course_competency_rule',
  171. "$CFG->dirroot/admin/tool/lp/classes/course_competency_rule_form_element.php",
  172. 'tool_lp_course_competency_rule_form_element');
  173. // Reuse the same options.
  174. $mform->addElement('course_competency_rule', 'competency_rule', get_string('uponcoursemodulecompletion', 'tool_lp'), $options);
  175. }
  176. /**
  177. * Hook the add/edit of the course module.
  178. *
  179. * @param stdClass $data Data from the form submission.
  180. * @param stdClass $course The course.
  181. */
  182. function tool_lp_coursemodule_edit_post_actions($data, $course) {
  183. if (!get_config('core_competency', 'enabled')) {
  184. return $data;
  185. }
  186. // It seems like the form did not contain any of the form fields, we can return.
  187. if (!isset($data->competency_rule) && !isset($data->competencies)) {
  188. return $data;
  189. }
  190. // We bypass the API here and go direct to the persistent layer - because we don't want to do permission
  191. // checks here - we need to load the real list of existing course module competencies.
  192. $existing = \core_competency\course_module_competency::list_course_module_competencies($data->coursemodule);
  193. $existingids = array();
  194. foreach ($existing as $cmc) {
  195. array_push($existingids, $cmc->get_competencyid());
  196. }
  197. $newids = isset($data->competencies) ? $data->competencies : array();
  198. $removed = array_diff($existingids, $newids);
  199. $added = array_diff($newids, $existingids);
  200. foreach ($removed as $removedid) {
  201. \core_competency\api::remove_competency_from_course_module($data->coursemodule, $removedid);
  202. }
  203. foreach ($added as $addedid) {
  204. \core_competency\api::add_competency_to_course_module($data->coursemodule, $addedid);
  205. }
  206. if (isset($data->competency_rule)) {
  207. // Now update the rules for each course_module_competency.
  208. $current = \core_competency\api::list_course_module_competencies_in_course_module($data->coursemodule);
  209. foreach ($current as $coursemodulecompetency) {
  210. \core_competency\api::set_course_module_competency_ruleoutcome($coursemodulecompetency, $data->competency_rule);
  211. }
  212. }
  213. return $data;
  214. }