[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/lp/ -> lib.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1