Merge branch 'MDL-63303_master-deleteduserfix' of https://github.com/markn86/moodle
[moodle.git] / course / classes / external / course_summary_exporter.php
blob2a1416c36cf89eb7cc029db6bf51de45684860f1
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.
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/>.
17 /**
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();
27 use renderer_base;
28 use moodle_url;
30 /**
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 {
38 /**
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);
59 if (!$courseimage) {
60 $courseimage = self::get_course_pattern($this->data);
62 $progress = self::get_course_progress($this->data);
63 $hasprogress = false;
64 if ($progress === 0 || $progress > 0) {
65 $hasprogress = true;
67 $progress = floor($progress);
68 return array(
69 'fullnamedisplay' => get_course_display_name_for_list($this->data),
70 'viewurl' => (new moodle_url('/course/view.php', array('id' => $this->data->id)))->out(false),
71 'courseimage' => $courseimage,
72 'progress' => $progress,
73 'hasprogress' => $hasprogress,
74 'isfavourite' => $this->related['isfavourite'],
75 'hidden' => boolval(get_user_preferences('block_myoverview_hidden_course_' . $this->data->id, 0))
79 public static function define_properties() {
80 return array(
81 'id' => array(
82 'type' => PARAM_INT,
84 'fullname' => array(
85 'type' => PARAM_TEXT,
87 'shortname' => array(
88 'type' => PARAM_TEXT,
90 'idnumber' => array(
91 'type' => PARAM_RAW,
93 'summary' => array(
94 'type' => PARAM_RAW,
95 'null' => NULL_ALLOWED
97 'summaryformat' => array(
98 'type' => PARAM_INT,
100 'startdate' => array(
101 'type' => PARAM_INT,
103 'enddate' => array(
104 'type' => PARAM_INT,
110 * Get the formatting parameters for the summary.
112 * @return array
114 protected function get_format_parameters_for_summary() {
115 return [
116 'component' => 'course',
117 'filearea' => 'summary',
121 public static function define_other_properties() {
122 return array(
123 'fullnamedisplay' => array(
124 'type' => PARAM_TEXT,
126 'viewurl' => array(
127 'type' => PARAM_URL,
129 'courseimage' => array(
130 'type' => PARAM_RAW,
132 'progress' => array(
133 'type' => PARAM_INT,
134 'optional' => true
136 'hasprogress' => array(
137 'type' => PARAM_BOOL
139 'isfavourite' => array(
140 'type' => PARAM_BOOL
142 'hidden' => array(
143 'type' => PARAM_BOOL
145 'timeaccess' => array(
146 'type' => PARAM_INT,
147 'optional' => true
153 * Get the course image if added to course.
155 * @param object $course
156 * @return string url of course image
158 public static function get_course_image($course) {
159 global $CFG;
160 $courseinlist = new \core_course_list_element($course);
161 foreach ($courseinlist->get_course_overviewfiles() as $file) {
162 if ($file->is_valid_image()) {
163 $pathcomponents = [
164 '/pluginfile.php',
165 $file->get_contextid(),
166 $file->get_component(),
167 $file->get_filearea() . $file->get_filepath() . $file->get_filename()
169 $path = implode('/', $pathcomponents);
170 return (new moodle_url($path))->out();
173 return false;
177 * Get the course pattern datauri.
179 * The datauri is an encoded svg that can be passed as a url.
180 * @param object $course
181 * @return string datauri
183 public static function get_course_pattern($course) {
184 $color = self::coursecolor($course->id);
185 $pattern = new \core_geopattern();
186 $pattern->setColor($color);
187 $pattern->patternbyid($course->id);
188 return $pattern->datauri();
192 * Get the course progress percentage.
194 * @param object $course
195 * @return int progress
197 public static function get_course_progress($course) {
198 return \core_completion\progress::get_course_progress_percentage($course);
202 * Get the course color.
204 * @param int $courseid
205 * @return string hex color code.
207 public static function coursecolor($courseid) {
208 // The colour palette is hardcoded for now. It would make sense to combine it with theme settings.
209 $basecolors = ['#81ecec', '#74b9ff', '#a29bfe', '#dfe6e9', '#00b894',
210 '#0984e3', '#b2bec3', '#fdcb6e', '#fd79a8', '#6c5ce7'];
212 $color = $basecolors[$courseid % 10];
213 return $color;