Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 for exporting a course summary from an stdClass.
  19   *
  20   * @package    core_course
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_course\external;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use renderer_base;
  28  use moodle_url;
  29  
  30  /**
  31   * Class for exporting a course summary from an stdClass.
  32   *
  33   * @copyright  2015 Damyon Wiese
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class course_summary_exporter extends \core\external\exporter {
  37  
  38      /**
  39       * Constructor - saves the persistent object, and the related objects.
  40       *
  41       * @param mixed $data - Either an stdClass or an array of values.
  42       * @param array $related - An optional list of pre-loaded objects related to this object.
  43       */
  44      public function __construct($data, $related = array()) {
  45          if (!array_key_exists('isfavourite', $related)) {
  46              $related['isfavourite'] = false;
  47          }
  48          parent::__construct($data, $related);
  49      }
  50  
  51      protected static function define_related() {
  52          // We cache the context so it does not need to be retrieved from the course.
  53          return array('context' => '\\context', 'isfavourite' => 'bool?');
  54      }
  55  
  56      protected function get_other_values(renderer_base $output) {
  57          global $CFG;
  58          $courseimage = self::get_course_image($this->data);
  59          if (!$courseimage) {
  60              $courseimage = $output->get_generated_image_for_id($this->data->id);
  61          }
  62          $progress = self::get_course_progress($this->data);
  63          $hasprogress = false;
  64          if ($progress === 0 || $progress > 0) {
  65              $hasprogress = true;
  66          }
  67          $progress = floor($progress);
  68          $coursecategory = \core_course_category::get($this->data->category, MUST_EXIST, true);
  69          return array(
  70              'fullnamedisplay' => get_course_display_name_for_list($this->data),
  71              'viewurl' => (new moodle_url('/course/view.php', array('id' => $this->data->id)))->out(false),
  72              'courseimage' => $courseimage,
  73              'progress' => $progress,
  74              'hasprogress' => $hasprogress,
  75              'isfavourite' => $this->related['isfavourite'],
  76              'hidden' => boolval(get_user_preferences('block_myoverview_hidden_course_' . $this->data->id, 0)),
  77              'showshortname' => $CFG->courselistshortnames ? true : false,
  78              'coursecategory' => $coursecategory->name
  79          );
  80      }
  81  
  82      public static function define_properties() {
  83          return array(
  84              'id' => array(
  85                  'type' => PARAM_INT,
  86              ),
  87              'fullname' => array(
  88                  'type' => PARAM_TEXT,
  89              ),
  90              'shortname' => array(
  91                  'type' => PARAM_TEXT,
  92              ),
  93              'idnumber' => array(
  94                  'type' => PARAM_RAW,
  95              ),
  96              'summary' => array(
  97                  'type' => PARAM_RAW,
  98                  'null' => NULL_ALLOWED
  99              ),
 100              'summaryformat' => array(
 101                  'type' => PARAM_INT,
 102              ),
 103              'startdate' => array(
 104                  'type' => PARAM_INT,
 105              ),
 106              'enddate' => array(
 107                  'type' => PARAM_INT,
 108              ),
 109              'visible' => array(
 110                  'type' => PARAM_BOOL,
 111              )
 112          );
 113      }
 114  
 115      /**
 116       * Get the formatting parameters for the summary.
 117       *
 118       * @return array
 119       */
 120      protected function get_format_parameters_for_summary() {
 121          return [
 122              'component' => 'course',
 123              'filearea' => 'summary',
 124          ];
 125      }
 126  
 127      public static function define_other_properties() {
 128          return array(
 129              'fullnamedisplay' => array(
 130                  'type' => PARAM_TEXT,
 131              ),
 132              'viewurl' => array(
 133                  'type' => PARAM_URL,
 134              ),
 135              'courseimage' => array(
 136                  'type' => PARAM_RAW,
 137              ),
 138              'progress' => array(
 139                  'type' => PARAM_INT,
 140                  'optional' => true
 141              ),
 142              'hasprogress' => array(
 143                  'type' => PARAM_BOOL
 144              ),
 145              'isfavourite' => array(
 146                  'type' => PARAM_BOOL
 147              ),
 148              'hidden' => array(
 149                  'type' => PARAM_BOOL
 150              ),
 151              'timeaccess' => array(
 152                  'type' => PARAM_INT,
 153                  'optional' => true
 154              ),
 155              'showshortname' => array(
 156                  'type' => PARAM_BOOL
 157              ),
 158              'coursecategory' => array(
 159                  'type' => PARAM_TEXT
 160              )
 161          );
 162      }
 163  
 164      /**
 165       * Get the course image if added to course.
 166       *
 167       * @param object $course
 168       * @return string url of course image
 169       */
 170      public static function get_course_image($course) {
 171          global $CFG;
 172          $courseinlist = new \core_course_list_element($course);
 173          foreach ($courseinlist->get_course_overviewfiles() as $file) {
 174              if ($file->is_valid_image()) {
 175                  $pathcomponents = [
 176                      '/pluginfile.php',
 177                      $file->get_contextid(),
 178                      $file->get_component(),
 179                      $file->get_filearea() . $file->get_filepath() . $file->get_filename()
 180                  ];
 181                  $path = implode('/', $pathcomponents);
 182                  return (new moodle_url($path))->out();
 183              }
 184          }
 185          return false;
 186      }
 187  
 188      /**
 189       * Get the course pattern datauri.
 190       *
 191       * The datauri is an encoded svg that can be passed as a url.
 192       * @param object $course
 193       * @return string datauri
 194       * @deprecated 3.7
 195       */
 196      public static function get_course_pattern($course) {
 197          global $OUTPUT;
 198          debugging('course_summary_exporter::get_course_pattern() is deprecated. ' .
 199              'Please use $OUTPUT->get_generated_image_for_id() instead.', DEBUG_DEVELOPER);
 200          return $OUTPUT->get_generated_image_for_id($course->id);
 201      }
 202  
 203      /**
 204       * Get the course progress percentage.
 205       *
 206       * @param object $course
 207       * @return int progress
 208       */
 209      public static function get_course_progress($course) {
 210          return \core_completion\progress::get_course_progress_percentage($course);
 211      }
 212  
 213      /**
 214       * Get the course color.
 215       *
 216       * @param int $courseid
 217       * @return string hex color code.
 218       * @deprecated 3.7
 219       */
 220      public static function coursecolor($courseid) {
 221          global $OUTPUT;
 222          debugging('course_summary_exporter::coursecolor() is deprecated. ' .
 223              'Please use $OUTPUT->get_generated_color_for_id() instead.', DEBUG_DEVELOPER);
 224          return $OUTPUT->get_generated_color_for_id($courseid);
 225      }
 226  }