2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Class for exporting a course summary from an stdClass.
20 * @package core_course
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core_course\external
;
25 defined('MOODLE_INTERNAL') ||
die();
31 * Class for exporting a course summary from an stdClass.
33 * @copyright 2015 Damyon Wiese
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class course_summary_exporter
extends \core\external\exporter
{
39 * Constructor - saves the persistent object, and the related objects.
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.
44 public function __construct($data, $related = array()) {
45 if (!array_key_exists('isfavourite', $related)) {
46 $related['isfavourite'] = false;
48 parent
::__construct($data, $related);
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?');
56 protected function get_other_values(renderer_base
$output) {
58 $courseimage = self
::get_course_image($this->data
);
60 $courseimage = $output->get_generated_image_for_id($this->data
->id
);
62 $progress = self
::get_course_progress($this->data
);
64 if ($progress === 0 ||
$progress > 0) {
67 $progress = floor($progress ??
0);
68 $coursecategory = \core_course_category
::get($this->data
->category
, MUST_EXIST
, true);
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
82 public static function define_properties() {
98 'null' => NULL_ALLOWED
,
101 'summaryformat' => array(
103 'default' => FORMAT_MOODLE
,
105 'startdate' => array(
112 'type' => PARAM_BOOL
,
114 'showactivitydates' => [
115 'type' => PARAM_BOOL
,
116 'null' => NULL_ALLOWED
118 'showcompletionconditions' => [
119 'type' => PARAM_BOOL
,
120 'null' => NULL_ALLOWED
123 'type' => PARAM_TEXT
,
124 'null' => NULL_ALLOWED
,
131 * Get the formatting parameters for the summary.
135 protected function get_format_parameters_for_summary() {
137 'component' => 'course',
138 'filearea' => 'summary',
142 public static function define_other_properties() {
144 'fullnamedisplay' => array(
145 'type' => PARAM_TEXT
,
150 'courseimage' => array(
157 'hasprogress' => array(
160 'isfavourite' => array(
166 'timeaccess' => array(
170 'showshortname' => array(
173 'coursecategory' => array(
180 * Get the course image if added to course.
182 * @param object $course
183 * @return string|false url of course image or false if it's not exist.
185 public static function get_course_image($course) {
186 $image = \cache
::make('core', 'course_image')->get($course->id
);
188 if (is_null($image)) {
196 * Get the course pattern datauri.
198 * The datauri is an encoded svg that can be passed as a url.
199 * @param object $course
200 * @return string datauri
203 public static function get_course_pattern($course) {
205 debugging('course_summary_exporter::get_course_pattern() is deprecated. ' .
206 'Please use $OUTPUT->get_generated_image_for_id() instead.', DEBUG_DEVELOPER
);
207 return $OUTPUT->get_generated_image_for_id($course->id
);
211 * Get the course progress percentage.
213 * @param object $course
214 * @return int progress
216 public static function get_course_progress($course) {
217 return \core_completion\progress
::get_course_progress_percentage($course);
221 * Get the course color.
223 * @param int $courseid
224 * @return string hex color code.
227 public static function coursecolor($courseid) {
229 debugging('course_summary_exporter::coursecolor() is deprecated. ' .
230 'Please use $OUTPUT->get_generated_color_for_id() instead.', DEBUG_DEVELOPER
);
231 return $OUTPUT->get_generated_color_for_id($courseid);