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.

Differences Between: [Versions 401 and 402] [Versions 401 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   * Class containing data for course competencies page
  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  defined('MOODLE_INTERNAL') || die();
  26  
  27  use renderable;
  28  use templatable;
  29  use renderer_base;
  30  use stdClass;
  31  use moodle_url;
  32  use context_system;
  33  use context_course;
  34  use core_competency\api;
  35  use tool_lp\course_competency_statistics;
  36  use core_competency\competency;
  37  use core_competency\course_competency;
  38  use core_competency\external\performance_helper;
  39  use core_competency\external\competency_exporter;
  40  use core_competency\external\course_competency_exporter;
  41  use core_competency\external\course_competency_settings_exporter;
  42  use core_competency\external\user_competency_course_exporter;
  43  use core_competency\external\user_competency_exporter;
  44  use core_competency\external\plan_exporter;
  45  use tool_lp\external\competency_path_exporter;
  46  use tool_lp\external\course_competency_statistics_exporter;
  47  use core_course\external\course_module_summary_exporter;
  48  
  49  /**
  50   * Class containing data for course competencies page
  51   *
  52   * @copyright  2015 Damyon Wiese
  53   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  54   */
  55  class course_competencies_page implements renderable, templatable {
  56  
  57      /** @var int $courseid Course id for this page. */
  58      protected $courseid = null;
  59  
  60      /** @var int $moduleid Module id for this page. */
  61      protected $moduleid = null;
  62  
  63      /** @var context $context The context for this page. */
  64      protected $context = null;
  65  
  66      /** @var \core_competency\course_competency[] $competencies List of competencies. */
  67      protected $coursecompetencylist = array();
  68  
  69      /** @var bool $canmanagecompetencyframeworks Can the current user manage competency frameworks. */
  70      protected $canmanagecompetencyframeworks = false;
  71  
  72      /** @var bool $canmanagecoursecompetencies Can the current user manage course competency frameworks.. */
  73      protected $canmanagecoursecompetencies = false;
  74  
  75      /** @var string $manageurl manage url. */
  76      protected $manageurl = null;
  77  
  78      /**
  79       * Construct this renderable.
  80       * @param int $courseid The course record for this page.
  81       */
  82      public function __construct($courseid, $moduleid) {
  83          $this->context = context_course::instance($courseid);
  84          $this->courseid = $courseid;
  85          $this->moduleid = $moduleid;
  86          $this->coursecompetencylist = api::list_course_competencies($courseid);
  87  
  88          if ($this->moduleid > 0) {
  89              $modulecompetencies = api::list_course_module_competencies_in_course_module($this->moduleid);
  90              foreach ($this->coursecompetencylist as $ccid => $coursecompetency) {
  91                  $coursecompetency = $coursecompetency['coursecompetency'];
  92                  $found = false;
  93                  foreach ($modulecompetencies as $mcid => $modulecompetency) {
  94                      if ($modulecompetency->get('competencyid') == $coursecompetency->get('competencyid')) {
  95                          $found = true;
  96                          break;
  97                      }
  98                  }
  99  
 100                  if (!$found) {
 101                      // We need to filter out this competency.
 102                      unset($this->coursecompetencylist[$ccid]);
 103                  }
 104              }
 105          }
 106  
 107          $this->canmanagecoursecompetencies = has_capability('moodle/competency:coursecompetencymanage', $this->context);
 108          $this->canconfigurecoursecompetencies = has_capability('moodle/competency:coursecompetencyconfigure', $this->context);
 109          $this->cangradecompetencies = has_capability('moodle/competency:competencygrade', $this->context);
 110          $this->coursecompetencysettings = api::read_course_competency_settings($courseid);
 111          $this->coursecompetencystatistics = new course_competency_statistics($courseid);
 112  
 113          // Check the lowest level in which the user can manage the competencies.
 114          $this->manageurl = null;
 115          $this->canmanagecompetencyframeworks = false;
 116          $contexts = array_reverse($this->context->get_parent_contexts(true));
 117          foreach ($contexts as $context) {
 118              $canmanage = has_capability('moodle/competency:competencymanage', $context);
 119              if ($canmanage) {
 120                  $this->manageurl = new moodle_url('/admin/tool/lp/competencyframeworks.php',
 121                      array('pagecontextid' => $context->id));
 122                  $this->canmanagecompetencyframeworks = true;
 123                  break;
 124              }
 125          }
 126      }
 127  
 128      /**
 129       * Export this data so it can be used as the context for a mustache template.
 130       *
 131       * @param renderer_base $output Renderer base.
 132       * @return stdClass
 133       */
 134      public function export_for_template(renderer_base $output) {
 135          global $USER;
 136  
 137          $data = new stdClass();
 138          $data->courseid = $this->courseid;
 139          $data->moduleid = $this->moduleid;
 140          $data->pagecontextid = $this->context->id;
 141          $data->competencies = array();
 142          $data->pluginbaseurl = (new moodle_url('/admin/tool/lp'))->out(true);
 143  
 144          $gradable = is_enrolled($this->context, $USER, 'moodle/competency:coursecompetencygradable');
 145          if ($gradable) {
 146              $usercompetencycourses = api::list_user_competencies_in_course($this->courseid, $USER->id);
 147              $data->gradableuserid = $USER->id;
 148  
 149              if ($this->moduleid > 0) {
 150                  $modulecompetencies = api::list_course_module_competencies_in_course_module($this->moduleid);
 151                  foreach ($usercompetencycourses as $ucid => $usercoursecompetency) {
 152                      $found = false;
 153                      foreach ($modulecompetencies as $mcid => $modulecompetency) {
 154                          if ($modulecompetency->get('competencyid') == $usercoursecompetency->get('competencyid')) {
 155                              $found = true;
 156                              break;
 157                          }
 158                      }
 159  
 160                      if (!$found) {
 161                          // We need to filter out this competency.
 162                          unset($usercompetencycourses[$ucid]);
 163                      }
 164                  }
 165              }
 166          }
 167  
 168          $ruleoutcomelist = course_competency::get_ruleoutcome_list();
 169          $ruleoutcomeoptions = array();
 170          foreach ($ruleoutcomelist as $value => $text) {
 171              $ruleoutcomeoptions[$value] = array('value' => $value, 'text' => (string) $text, 'selected' => false);
 172          }
 173  
 174          $helper = new performance_helper();
 175          foreach ($this->coursecompetencylist as $coursecompetencyelement) {
 176              $coursecompetency = $coursecompetencyelement['coursecompetency'];
 177              $competency = $coursecompetencyelement['competency'];
 178              $context = $helper->get_context_from_competency($competency);
 179  
 180              $compexporter = new competency_exporter($competency, array('context' => $context));
 181              $ccexporter = new course_competency_exporter($coursecompetency, array('context' => $context));
 182  
 183              $ccoutcomeoptions = (array) (object) $ruleoutcomeoptions;
 184              $ccoutcomeoptions[$coursecompetency->get('ruleoutcome')]['selected'] = true;
 185  
 186              $coursemodules = api::list_course_modules_using_competency($competency->get('id'), $this->courseid);
 187  
 188              $fastmodinfo = get_fast_modinfo($this->courseid);
 189              $exportedmodules = array();
 190              foreach ($coursemodules as $cmid) {
 191                  $cminfo = $fastmodinfo->cms[$cmid];
 192                  $cmexporter = new course_module_summary_exporter(null, array('cm' => $cminfo));
 193                  $exportedmodules[] = $cmexporter->export($output);
 194              }
 195              // Competency path.
 196              $pathexporter = new competency_path_exporter([
 197                  'ancestors' => $competency->get_ancestors(),
 198                  'framework' => $helper->get_framework_from_competency($competency),
 199                  'context' => $context
 200              ]);
 201  
 202              // User learning plans.
 203              $plans = api::list_plans_with_competency($USER->id, $competency);
 204              $exportedplans = array();
 205              foreach ($plans as $plan) {
 206                  $planexporter = new plan_exporter($plan, array('template' => $plan->get_template()));
 207                  $exportedplans[] = $planexporter->export($output);
 208              }
 209  
 210              $onerow = array(
 211                  'competency' => $compexporter->export($output),
 212                  'coursecompetency' => $ccexporter->export($output),
 213                  'ruleoutcomeoptions' => $ccoutcomeoptions,
 214                  'coursemodules' => $exportedmodules,
 215                  'comppath' => $pathexporter->export($output),
 216                  'plans' => $exportedplans
 217              );
 218              if ($gradable) {
 219                  $foundusercompetencycourse = false;
 220                  foreach ($usercompetencycourses as $usercompetencycourse) {
 221                      if ($usercompetencycourse->get('competencyid') == $competency->get('id')) {
 222                          $foundusercompetencycourse = $usercompetencycourse;
 223                      }
 224                  }
 225                  if ($foundusercompetencycourse) {
 226                      $related = array(
 227                          'scale' => $helper->get_scale_from_competency($competency)
 228                      );
 229                      $exporter = new user_competency_course_exporter($foundusercompetencycourse, $related);
 230                      $onerow['usercompetencycourse'] = $exporter->export($output);
 231                  }
 232              }
 233              array_push($data->competencies, $onerow);
 234          }
 235  
 236          $data->canmanagecompetencyframeworks = $this->canmanagecompetencyframeworks;
 237          $data->canmanagecoursecompetencies = $this->canmanagecoursecompetencies;
 238          $data->canconfigurecoursecompetencies = $this->canconfigurecoursecompetencies;
 239          $data->cangradecompetencies = $this->cangradecompetencies;
 240          $exporter = new course_competency_settings_exporter($this->coursecompetencysettings);
 241          $data->settings = $exporter->export($output);
 242          $related = array('context' => $this->context);
 243          $exporter = new course_competency_statistics_exporter($this->coursecompetencystatistics, $related);
 244          $data->statistics = $exporter->export($output);
 245          $data->manageurl = null;
 246          if ($this->canmanagecompetencyframeworks) {
 247              $data->manageurl = $this->manageurl->out(true);
 248          }
 249  
 250          return $data;
 251      }
 252  
 253  }