Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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 in a course
  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 core_competency\api;
  28  use core_competency\user_competency;
  29  use core_competency\external\plan_exporter;
  30  use core_course\external\course_module_summary_exporter;
  31  use core_course\external\course_summary_exporter;
  32  use context_course;
  33  use renderer_base;
  34  use stdClass;
  35  use moodle_url;
  36  
  37  /**
  38   * Class for exporting user competency data with additional related data in a plan.
  39   *
  40   * @copyright  2015 Damyon Wiese
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class user_competency_summary_in_course_exporter extends \core\external\exporter {
  44  
  45      protected static function define_related() {
  46          // We cache the context so it does not need to be retrieved from the framework every time.
  47          return array('competency' => '\\core_competency\\competency',
  48                       'relatedcompetencies' => '\\core_competency\\competency[]',
  49                       'user' => '\\stdClass',
  50                       'course' => '\\stdClass',
  51                       'usercompetencycourse' => '\\core_competency\\user_competency_course?',
  52                       'evidence' => '\\core_competency\\evidence[]',
  53                       'scale' => '\\grade_scale');
  54      }
  55  
  56      protected static function define_other_properties() {
  57          return array(
  58              'usercompetencysummary' => array(
  59                  'type' => user_competency_summary_exporter::read_properties_definition()
  60              ),
  61              'course' => array(
  62                  'type' => course_summary_exporter::read_properties_definition(),
  63              ),
  64              'coursemodules' => array(
  65                  'type' => course_module_summary_exporter::read_properties_definition(),
  66                  'multiple' => true
  67              ),
  68              'plans' => array(
  69                  'type' => plan_exporter::read_properties_definition(),
  70                  'multiple' => true
  71              ),
  72              'pluginbaseurl' => [
  73                  'type' => PARAM_URL
  74              ],
  75          );
  76      }
  77  
  78      protected function get_other_values(renderer_base $output) {
  79          // Arrays are copy on assign.
  80          $related = $this->related;
  81          $result = new stdClass();
  82          // Remove course from related as it is not wanted by the user_competency_summary_exporter.
  83          unset($related['course']);
  84          $related['usercompetencyplan'] = null;
  85          $related['usercompetency'] = null;
  86          $exporter = new user_competency_summary_exporter(null, $related);
  87          $result->usercompetencysummary = $exporter->export($output);
  88          $result->usercompetencysummary->cangrade = user_competency::can_grade_user_in_course($this->related['user']->id,
  89              $this->related['course']->id);
  90  
  91          $context = context_course::instance($this->related['course']->id);
  92          $exporter = new course_summary_exporter($this->related['course'], array('context' => $context));
  93          $result->course = $exporter->export($output);
  94  
  95          $coursemodules = api::list_course_modules_using_competency($this->related['competency']->get('id'),
  96                                                                     $this->related['course']->id);
  97  
  98          $fastmodinfo = get_fast_modinfo($this->related['course']->id);
  99          $exportedmodules = array();
 100          foreach ($coursemodules as $cm) {
 101              $cminfo = $fastmodinfo->cms[$cm];
 102              $cmexporter = new course_module_summary_exporter(null, array('cm' => $cminfo));
 103              $exportedmodules[] = $cmexporter->export($output);
 104          }
 105          $result->coursemodules = $exportedmodules;
 106  
 107          // User learning plans.
 108          $plans = api::list_plans_with_competency($this->related['user']->id, $this->related['competency']);
 109          $exportedplans = array();
 110          foreach ($plans as $plan) {
 111              $planexporter = new plan_exporter($plan, array('template' => $plan->get_template()));
 112              $exportedplans[] = $planexporter->export($output);
 113          }
 114          $result->plans = $exportedplans;
 115          $result->pluginbaseurl = (new moodle_url('/admin/tool/lp'))->out(true);
 116  
 117          return (array) $result;
 118      }
 119  }