Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   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  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * This function extends the user navigation.
  29   *
  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
  35   */
  36  function tool_lp_extend_navigation_user($navigation, $user, $usercontext, $course, $coursecontext) {
  37      if (!get_config('core_competency', 'enabled')) {
  38          return;
  39      }
  40  
  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)));
  44  
  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)));
  48          }
  49      }
  50  
  51  }
  52  
  53  /**
  54   * Add nodes to myprofile page.
  55   *
  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
  60   *
  61   * @return bool
  62   */
  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;
  68      }
  69  
  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);
  74  
  75      return true;
  76  }
  77  
  78  /**
  79   * This function extends the category navigation to add learning plan links.
  80   *
  81   * @param navigation_node $navigation The navigation node to extend
  82   * @param context $coursecategorycontext The context of the course category
  83   */
  84  function tool_lp_extend_navigation_category_settings($navigation, $coursecategorycontext) {
  85      if (!get_config('core_competency', 'enabled')) {
  86          return false;
  87      }
  88  
  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;
  94      }
  95  
  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);
 109          }
 110      }
 111  
 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);
 125          }
 126      }
 127  }
 128  
 129  /**
 130   * Inject the competencies elements into all moodle module settings forms.
 131   *
 132   * @param moodleform $formwrapper The moodle quickforms wrapper object.
 133   * @param MoodleQuickForm $mform The actual form object (required to modify the form).
 134   */
 135  function tool_lp_coursemodule_standard_elements($formwrapper, $mform) {
 136      global $CFG, $COURSE;
 137  
 138      if (!get_config('core_competency', 'enabled')) {
 139          return;
 140      } else if (!has_capability('moodle/competency:coursecompetencymanage', $formwrapper->get_context())) {
 141          return;
 142      }
 143  
 144      $mform->addElement('header', 'competenciessection', get_string('competencies', 'core_competency'));
 145  
 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;
 152      }
 153      $options = array(
 154          'courseid' => $COURSE->id,
 155          'cmid' => $cmid
 156      );
 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);
 164  }
 165  
 166  /**
 167   * Hook the add/edit of the course module.
 168   *
 169   * @param stdClass $data Data from the form submission.
 170   * @param stdClass $course The course.
 171   */
 172  function tool_lp_coursemodule_edit_post_actions($data, $course) {
 173      if (!get_config('core_competency', 'enabled')) {
 174          return $data;
 175      }
 176  
 177      // It seems like the form did not contain any of the form fields, we can return.
 178      if (!isset($data->competency_rule) && !isset($data->competencies)) {
 179          return $data;
 180      }
 181  
 182      // We bypass the API here and go direct to the persistent layer - because we don't want to do permission
 183      // checks here - we need to load the real list of existing course module competencies.
 184      $existing = \core_competency\course_module_competency::list_course_module_competencies($data->coursemodule);
 185  
 186      $existingids = array();
 187      foreach ($existing as $cmc) {
 188          array_push($existingids, $cmc->get('competencyid'));
 189      }
 190  
 191      $newids = isset($data->competencies) ? $data->competencies : array();
 192  
 193      $removed = array_diff($existingids, $newids);
 194      $added = array_diff($newids, $existingids);
 195  
 196      foreach ($removed as $removedid) {
 197          \core_competency\api::remove_competency_from_course_module($data->coursemodule, $removedid);
 198      }
 199      foreach ($added as $addedid) {
 200          \core_competency\api::add_competency_to_course_module($data->coursemodule, $addedid);
 201      }
 202  
 203      if (isset($data->competency_rule)) {
 204          // Now update the rules for each course_module_competency.
 205          $current = \core_competency\api::list_course_module_competencies_in_course_module($data->coursemodule);
 206          foreach ($current as $coursemodulecompetency) {
 207              \core_competency\api::set_course_module_competency_ruleoutcome($coursemodulecompetency, $data->competency_rule);
 208          }
 209      }
 210  
 211      return $data;
 212  }
 213  
 214  /**
 215   * Map icons for font-awesome themes.
 216   */
 217  function tool_lp_get_fontawesome_icon_map() {
 218      return [
 219          'tool_lp:url' => 'fa-external-link'
 220      ];
 221  }
 222  
 223  /**
 224   * Render a short bit of information about a competency.
 225   *
 226   * @param \core_competency\competency $competency The competency to show.
 227   * @param \core_competency\competency_framework $framework The competency framework.
 228   * @param boolean $includerelated If true, show related competencies.
 229   * @param boolean $includecourses If true, show courses using this competency.
 230   * @param boolean $skipenabled If true, show this competency even if competencies are disabled.
 231   * @return string The html summary for the competency.
 232   */
 233  function tool_lp_render_competency_summary(\core_competency\competency $competency,
 234                                             \core_competency\competency_framework $framework,
 235                                             $includerelated,
 236                                             $includecourses,
 237                                             $skipenabled = false) {
 238      global $PAGE;
 239  
 240      if (!$skipenabled && !get_config('core_competency', 'enabled')) {
 241          return;
 242      }
 243  
 244      $summary = new \tool_lp\output\competency_summary($competency, $framework, $includerelated, $includecourses);
 245  
 246      $output = $PAGE->get_renderer('tool_lp');
 247  
 248      return $output->render($summary);
 249  }
 250  
 251  /**
 252   * Inject a course competency picker into the form.
 253   *
 254   * @param MoodleQuickForm $mform The actual form object (required to modify the form).
 255   * @param integer $courseid - SITEID or a course id
 256   * @param context $context - The page context
 257   * @param string $elementname - The name of the form element to create
 258   */
 259  function tool_lp_competency_picker($mform, $courseid, $context, $elementname) {
 260      global $CFG, $COURSE;
 261  
 262      if (!get_config('core_competency', 'enabled')) {
 263          return;
 264      }
 265  
 266      if ($courseid == SITEID) {
 267          if (!has_capability('moodle/competency:competencymanage', $context)) {
 268              return;
 269          }
 270  
 271          MoodleQuickForm::registerElementType('site_competencies',
 272                                               "$CFG->dirroot/$CFG->admin/tool/lp/classes/site_competencies_form_element.php",
 273                                               'tool_lp_site_competencies_form_element');
 274          $mform->addElement('site_competencies', $elementname);
 275      } else {
 276          if (!has_capability('moodle/competency:coursecompetencymanage', $context)) {
 277              return;
 278          }
 279  
 280          MoodleQuickForm::registerElementType('course_competencies',
 281                                               "$CFG->dirroot/$CFG->admin/tool/lp/classes/course_competencies_form_element.php",
 282                                               'tool_lp_course_competencies_form_element');
 283          $options = array(
 284              'courseid' => $COURSE->id
 285          );
 286          $mform->addElement('course_competencies', $elementname, get_string('modcompetencies', 'tool_lp'), $options);
 287      }
 288      $mform->setType($elementname, PARAM_SEQUENCE);
 289  }