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   * Class for exporting user competency data with all the evidence
  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\external;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use context_user;
  28  use renderer_base;
  29  use stdClass;
  30  use core_comment\external\comment_area_exporter;
  31  use core_competency\external\evidence_exporter;
  32  use core_competency\external\user_competency_exporter;
  33  use core_competency\external\user_competency_plan_exporter;
  34  use core_competency\external\user_competency_course_exporter;
  35  use core_user\external\user_summary_exporter;
  36  use core_competency\user_competency;
  37  
  38  /**
  39   * Class for exporting user competency data with additional related data.
  40   *
  41   * @copyright  2015 Damyon Wiese
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class user_competency_summary_exporter extends \core\external\exporter {
  45  
  46      protected static function define_related() {
  47          // We cache the context so it does not need to be retrieved from the framework every time.
  48          return array('competency' => '\\core_competency\\competency',
  49                       'relatedcompetencies' => '\\core_competency\\competency[]',
  50                       'user' => '\\stdClass',
  51                       'usercompetency' => '\\core_competency\\user_competency?',
  52                       'usercompetencyplan' => '\\core_competency\\user_competency_plan?',
  53                       'usercompetencycourse' => '\\core_competency\\user_competency_course?',
  54                       'evidence' => '\\core_competency\\evidence[]');
  55      }
  56  
  57      protected static function define_other_properties() {
  58          return array(
  59              'showrelatedcompetencies' => array(
  60                  'type' => PARAM_BOOL
  61              ),
  62              'cangrade' => array(
  63                  'type' => PARAM_BOOL
  64              ),
  65              'competency' => array(
  66                  'type' => competency_summary_exporter::read_properties_definition()
  67              ),
  68              'user' => array(
  69                  'type' => user_summary_exporter::read_properties_definition(),
  70              ),
  71              'usercompetency' => array(
  72                  'type' => user_competency_exporter::read_properties_definition(),
  73                  'optional' => true
  74              ),
  75              'usercompetencyplan' => array(
  76                  'type' => user_competency_plan_exporter::read_properties_definition(),
  77                  'optional' => true
  78              ),
  79              'usercompetencycourse' => array(
  80                  'type' => user_competency_course_exporter::read_properties_definition(),
  81                  'optional' => true
  82              ),
  83              'evidence' => array(
  84                  'type' => evidence_exporter::read_properties_definition(),
  85                  'multiple' => true
  86              ),
  87              'commentarea' => array(
  88                  'type' => comment_area_exporter::read_properties_definition(),
  89                  'optional' => true
  90              ),
  91          );
  92      }
  93  
  94      protected function get_other_values(renderer_base $output) {
  95          global $DB;
  96          $result = new stdClass();
  97  
  98          $result->showrelatedcompetencies = true;
  99  
 100          $competency = $this->related['competency'];
 101          $exporter = new competency_summary_exporter(null, array(
 102              'competency' => $competency,
 103              'context' => $competency->get_context(),
 104              'framework' => $competency->get_framework(),
 105              'linkedcourses' => array(),
 106              'relatedcompetencies' => $this->related['relatedcompetencies']
 107          ));
 108          $result->competency = $exporter->export($output);
 109  
 110          $result->cangrade = user_competency::can_grade_user($this->related['user']->id);
 111          if ($this->related['user']) {
 112              $exporter = new user_summary_exporter($this->related['user']);
 113              $result->user = $exporter->export($output);
 114          }
 115          $related = array('scale' => $competency->get_scale());
 116          if ($this->related['usercompetency']) {
 117              $exporter = new user_competency_exporter($this->related['usercompetency'], $related);
 118              $result->usercompetency = $exporter->export($output);
 119          }
 120          if ($this->related['usercompetencyplan']) {
 121              $exporter = new user_competency_plan_exporter($this->related['usercompetencyplan'], $related);
 122              $result->usercompetencyplan = $exporter->export($output);
 123          }
 124          if ($this->related['usercompetencycourse']) {
 125              $exporter = new user_competency_course_exporter($this->related['usercompetencycourse'], $related);
 126              $result->usercompetencycourse = $exporter->export($output);
 127          }
 128  
 129          $allevidence = array();
 130          $usercache = array();
 131          $scale = $competency->get_scale();
 132  
 133          $result->evidence = array();
 134          if (count($this->related['evidence'])) {
 135              foreach ($this->related['evidence'] as $evidence) {
 136                  $actionuserid = $evidence->get('actionuserid');
 137                  if (!empty($actionuserid)) {
 138                      $usercache[$evidence->get('actionuserid')] = true;
 139                  }
 140              }
 141              $users = array();
 142              if (!empty($usercache)) {
 143                  list($sql, $params) = $DB->get_in_or_equal(array_keys($usercache));
 144                  $users = $DB->get_records_select('user', 'id ' . $sql, $params);
 145              }
 146  
 147              foreach ($users as $user) {
 148                  $usercache[$user->id] = $user;
 149              }
 150  
 151              foreach ($this->related['evidence'] as $evidence) {
 152                  $actionuserid = $evidence->get('actionuserid');
 153                  $related = array(
 154                      'scale' => $scale,
 155                      'usercompetency' => ($this->related['usercompetency'] ? $this->related['usercompetency'] : null),
 156                      'usercompetencyplan' => ($this->related['usercompetencyplan'] ? $this->related['usercompetencyplan'] : null),
 157                      'context' => $evidence->get_context()
 158                  );
 159                  $related['actionuser'] = !empty($actionuserid) ? $usercache[$actionuserid] : null;
 160                  $exporter = new evidence_exporter($evidence, $related);
 161                  $allevidence[] = $exporter->export($output);
 162              }
 163              $result->evidence = $allevidence;
 164          }
 165  
 166          $usercompetency = !empty($this->related['usercompetency']) ? $this->related['usercompetency'] : null;
 167  
 168          if (!empty($usercompetency) && $usercompetency->can_read_comments()) {
 169              $commentareaexporter = new comment_area_exporter($usercompetency->get_comment_object());
 170              $result->commentarea = $commentareaexporter->export($output);
 171          }
 172  
 173          return (array) $result;
 174      }
 175  }