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.
   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 evidence data.
  19   *
  20   * @package    core_competency
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_competency\external;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use context_system;
  28  use renderer_base;
  29  use core_competency\evidence;
  30  use core_competency\user_competency;
  31  use core_user\external\user_summary_exporter;
  32  
  33  /**
  34   * Class for exporting evidence data.
  35   *
  36   * @copyright  2015 Damyon Wiese
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class evidence_exporter extends \core\external\persistent_exporter {
  40  
  41      /**
  42       * Constructor.
  43       *
  44       * @param mixed $data The data.
  45       * @param array $related Array of relateds.
  46       */
  47      public function __construct($data, $related = array()) {
  48          if (!isset($related['context'])) {
  49              // Previous code was automatically using the system context which was not correct.
  50              // We let developers know that they must fix their code without breaking anything, and
  51              // fallback on the previous behaviour. This should be removed at a later stage: Moodle 3.5.
  52              debugging('Missing related context in evidence_exporter.', DEBUG_DEVELOPER);
  53              $related['context'] = context_system::instance();
  54          }
  55          parent::__construct($data, $related);
  56      }
  57  
  58      protected static function define_related() {
  59          return array(
  60              'actionuser' => 'stdClass?',
  61              'context' => 'context',
  62              'scale' => 'grade_scale',
  63              'usercompetency' => 'core_competency\\user_competency?',
  64              'usercompetencyplan' => 'core_competency\\user_competency_plan?',
  65          );
  66      }
  67  
  68      protected static function define_class() {
  69          return evidence::class;
  70      }
  71  
  72      protected function get_other_values(renderer_base $output) {
  73          $other = array();
  74  
  75          if (!empty($this->related['actionuser'])) {
  76              $exporter = new user_summary_exporter($this->related['actionuser']);
  77              $actionuser = $exporter->export($output);
  78              $other['actionuser'] = $actionuser;
  79          }
  80  
  81          $other['description'] = $this->persistent->get_description();
  82  
  83          $other['userdate'] = userdate($this->persistent->get('timecreated'));
  84  
  85          if ($this->persistent->get('grade') === null) {
  86              $gradename = '-';
  87          } else {
  88              $gradename = $this->related['scale']->scale_items[$this->persistent->get('grade') - 1];
  89          }
  90          $other['gradename'] = $gradename;
  91  
  92          // Try to guess the user from the user competency.
  93          $userid = null;
  94          if ($this->related['usercompetency']) {
  95              $userid = $this->related['usercompetency']->get('userid');
  96          } else if ($this->related['usercompetencyplan']) {
  97              $userid = $this->related['usercompetencyplan']->get('userid');
  98          } else {
  99              $uc = user_competency::get_record(['id' => $this->persistent->get('usercompetencyid')]);
 100              $userid = $uc->get('userid');
 101          }
 102          $other['candelete'] = evidence::can_delete_user($userid);
 103  
 104          return $other;
 105      }
 106  
 107      /**
 108       * Get the format parameters for gradename.
 109       *
 110       * @return array
 111       */
 112      protected function get_format_parameters_for_gradename() {
 113          return [
 114              'context' => context_system::instance(), // The system context is cached, so we can get it right away.
 115          ];
 116      }
 117  
 118      public static function define_other_properties() {
 119          return array(
 120              'actionuser' => array(
 121                  'type' => user_summary_exporter::read_properties_definition(),
 122                  'optional' => true
 123              ),
 124              'description' => array(
 125                  'type' => PARAM_TEXT,   // The description may contain course names, etc.. which may need filtering.
 126              ),
 127              'gradename' => array(
 128                  'type' => PARAM_TEXT,
 129              ),
 130              'userdate' => array(
 131                  'type' => PARAM_NOTAGS
 132              ),
 133              'candelete' => array(
 134                  'type' => PARAM_BOOL
 135              )
 136          );
 137      }
 138  }