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 = self::get_course_pattern($this->data);
62 $progress = self::get_course_progress($this->data);
64 if ($progress === 0 || $progress > 0) {
67 $progress = floor($progress);
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() {
95 'null' => NULL_ALLOWED
97 'summaryformat' => array(
100 'startdate' => array(
110 * Get the formatting parameters for the summary.
114 protected function get_format_parameters_for_summary() {
116 'component' => 'course',
117 'filearea' => 'summary',
121 public static function define_other_properties() {
123 'fullnamedisplay' => array(
124 'type' => PARAM_TEXT,
129 'courseimage' => array(
136 'hasprogress' => array(
139 'isfavourite' => array(
145 'timeaccess' => array(
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) {
160 $courseinlist = new \core_course_list_element($course);
161 foreach ($courseinlist->get_course_overviewfiles() as $file) {
162 if ($file->is_valid_image()) {
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();
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];