Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [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      $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);
 170  }
 171  
 172  /**
 173   * Hook the add/edit of the course module.
 174   *
 175   * @param stdClass $data Data from the form submission.
 176   * @param stdClass $course The course.
 177   */
 178  function tool_lp_coursemodule_edit_post_actions($data, $course) {
 179      if (!get_config('core_competency', 'enabled')) {
 180          return $data;
 181      }
 182  
 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;
 186      }
 187  
 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);
 191  
 192      $existingids = array();
 193      foreach ($existing as $cmc) {
 194          array_push($existingids, $cmc->get('competencyid'));
 195      }
 196  
 197      $newids = isset($data->competencies) ? $data->competencies : array();
 198  
 199      $removed = array_diff($existingids, $newids);
 200      $added = array_diff($newids, $existingids);
 201  
 202      foreach ($removed as $removedid) {
 203          \core_competency\api::remove_competency_from_course_module($data->coursemodule, $removedid);
 204      }
 205      foreach ($added as $addedid) {
 206          \core_competency\api::add_competency_to_course_module($data->coursemodule, $addedid);
 207      }
 208  
 209      if (isset($data->competency_rule)) {
 210          $overridegrade = isset($data->override_grade) ? $data->override_grade : false;
 211  
 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);
 217          }
 218      }
 219  
 220      return $data;
 221  }
 222  
 223  /**
 224   * Map icons for font-awesome themes.
 225   */
 226  function tool_lp_get_fontawesome_icon_map() {
 227      return [
 228          'tool_lp:url' => 'fa-external-link'
 229      ];
 230  }
 231  
 232  /**
 233   * Render a short bit of information about a competency.
 234   *
 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.
 241   */
 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;
 248  
 249      if (!$skipenabled && !get_config('core_competency', 'enabled')) {
 250          return;
 251      }
 252  
 253      $summary = new \tool_lp\output\competency_summary($competency, $framework, $includerelated, $includecourses);
 254  
 255      $output = $PAGE->get_renderer('tool_lp');
 256  
 257      return $output->render($summary);
 258  }
 259  
 260  /**
 261   * Inject a course competency picker into the form.
 262   *
 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
 267   */
 268  function tool_lp_competency_picker($mform, $courseid, $context, $elementname) {
 269      global $CFG, $COURSE;
 270  
 271      if (!get_config('core_competency', 'enabled')) {
 272          return;
 273      }
 274  
 275      if ($courseid == SITEID) {
 276          if (!has_capability('moodle/competency:competencymanage', $context)) {
 277              return;
 278          }
 279  
 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;
 287          }
 288  
 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
 294          );
 295          $mform->addElement('course_competencies', $elementname, get_string('modcompetencies', 'tool_lp'), $options);
 296      }
 297      $mform->setType($elementname, PARAM_SEQUENCE);
 298  }