Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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   * User competency page class.
  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  namespace tool_lp\output;
  25  
  26  use renderable;
  27  use renderer_base;
  28  use templatable;
  29  use context_course;
  30  use core_competency\external\competency_exporter;
  31  use core_user\external\user_summary_exporter;
  32  use core_competency\external\performance_helper;
  33  use stdClass;
  34  
  35  /**
  36   * User competency course navigation class.
  37   *
  38   * @package    tool_lp
  39   * @copyright  2015 Damyon Wiese
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class user_competency_course_navigation implements renderable, templatable {
  43  
  44      /** @var userid */
  45      protected $userid;
  46  
  47      /** @var competencyid */
  48      protected $competencyid;
  49  
  50      /** @var courseid */
  51      protected $courseid;
  52  
  53      /** @var baseurl */
  54      protected $baseurl;
  55  
  56      /**
  57       * Construct.
  58       *
  59       * @param int $userid
  60       * @param int $competencyid
  61       * @param int $courseid
  62       * @param string $baseurl
  63       */
  64      public function __construct($userid, $competencyid, $courseid, $baseurl) {
  65          $this->userid = $userid;
  66          $this->competencyid = $competencyid;
  67          $this->courseid = $courseid;
  68          $this->baseurl = $baseurl;
  69      }
  70  
  71      /**
  72       * Export the data.
  73       *
  74       * @param renderer_base $output
  75       * @return stdClass
  76       */
  77      public function export_for_template(renderer_base $output) {
  78          global $CFG, $DB, $PAGE;
  79  
  80          $context = context_course::instance($this->courseid);
  81  
  82          $data = new stdClass();
  83          $data->userid = $this->userid;
  84          $data->competencyid = $this->competencyid;
  85          $data->courseid = $this->courseid;
  86          $data->baseurl = $this->baseurl;
  87          $data->groupselector = '';
  88  
  89          if (has_any_capability(array('moodle/competency:usercompetencyview', 'moodle/competency:coursecompetencymanage'),
  90                  $context)) {
  91              $course = $DB->get_record('course', array('id' => $this->courseid));
  92              $currentgroup = groups_get_course_group($course, true);
  93              if ($currentgroup !== false) {
  94                  $select = groups_allgroups_course_menu($course, $PAGE->url, true, $currentgroup);
  95                  $data->groupselector = $select;
  96              }
  97              // Fetch showactive.
  98              $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
  99              $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
 100              $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
 101  
 102              $users = get_enrolled_users($context, 'moodle/competency:coursecompetencygradable', $currentgroup,
 103                                          'u.*', null, 0, 0, $showonlyactiveenrol);
 104  
 105              $data->users = array();
 106              foreach ($users as $user) {
 107                  $exporter = new user_summary_exporter($user);
 108                  $user = $exporter->export($output);
 109                  if ($user->id == $this->userid) {
 110                      $user->selected = true;
 111                  }
 112                  $data->users[] = $user;
 113              }
 114              $data->hasusers = true;
 115          } else {
 116              $data->users = array();
 117              $data->hasusers = false;
 118          }
 119  
 120          $coursecompetencies = \core_competency\api::list_course_competencies($this->courseid);
 121          $data->competencies = array();
 122          $helper = new performance_helper();
 123          foreach ($coursecompetencies as $coursecompetency) {
 124              $coursecompetencycontext = $helper->get_context_from_competency($coursecompetency['competency']);
 125              $exporter = new competency_exporter($coursecompetency['competency'], array('context' => $coursecompetencycontext));
 126              $competency = $exporter->export($output);
 127              if ($competency->id == $this->competencyid) {
 128                  $competency->selected = true;
 129              }
 130              $data->competencies[] = $competency;
 131          }
 132          $data->hascompetencies = count($data->competencies);
 133          return $data;
 134      }
 135  }