MDL-63062 block_recentlyaccessedcourses: add web service
[moodle.git] / course / classes / external / course_summary_exporter.php
CommitLineData
f5f02ac0
FM
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 */
24namespace core_course\external;
25defined('MOODLE_INTERNAL') || die();
26
27use renderer_base;
28use 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 */
36class course_summary_exporter extends \core\external\exporter {
37
3cfff885
BB
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
f5f02ac0
FM
51 protected static function define_related() {
52 // We cache the context so it does not need to be retrieved from the course.
3cfff885 53 return array('context' => '\\context', 'isfavourite' => 'bool?');
f5f02ac0
FM
54 }
55
56 protected function get_other_values(renderer_base $output) {
3cfff885 57
6481a21f
BB
58 $courseimage = self::get_course_image($this->data);
59 if (!$courseimage) {
60 $courseimage = self::get_course_pattern($this->data);
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);
f5f02ac0 68 return array(
15c7d75f 69 'fullnamedisplay' => get_course_display_name_for_list($this->data),
6481a21f
BB
70 'viewurl' => (new moodle_url('/course/view.php', array('id' => $this->data->id)))->out(false),
71 'courseimage' => $courseimage,
72 'progress' => $progress,
3cfff885 73 'hasprogress' => $hasprogress,
e6f03948
P
74 'isfavourite' => $this->related['isfavourite'],
75 'hidden' => boolval(get_user_preferences('block_myoverview_hidden_course_' . $this->data->id, 0))
f5f02ac0
FM
76 );
77 }
78
79 public static function define_properties() {
80 return array(
81 'id' => array(
82 'type' => PARAM_INT,
83 ),
84 'fullname' => array(
85 'type' => PARAM_TEXT,
86 ),
87 'shortname' => array(
88 'type' => PARAM_TEXT,
89 ),
90 'idnumber' => array(
91 'type' => PARAM_RAW,
240e5236
MN
92 ),
93 'summary' => array(
94 'type' => PARAM_RAW,
0f5e3fd9 95 'null' => NULL_ALLOWED
240e5236 96 ),
47820478
AA
97 'summaryformat' => array(
98 'type' => PARAM_INT,
99 ),
240e5236
MN
100 'startdate' => array(
101 'type' => PARAM_INT,
102 ),
103 'enddate' => array(
104 'type' => PARAM_INT,
f5f02ac0
FM
105 )
106 );
107 }
108
47820478
AA
109 /**
110 * Get the formatting parameters for the summary.
111 *
112 * @return array
113 */
114 protected function get_format_parameters_for_summary() {
115 return [
116 'component' => 'course',
117 'filearea' => 'summary',
118 ];
119 }
120
f5f02ac0
FM
121 public static function define_other_properties() {
122 return array(
15c7d75f
MN
123 'fullnamedisplay' => array(
124 'type' => PARAM_TEXT,
125 ),
f5f02ac0
FM
126 'viewurl' => array(
127 'type' => PARAM_URL,
6481a21f
BB
128 ),
129 'courseimage' => array(
130 'type' => PARAM_RAW,
131 ),
132 'progress' => array(
133 'type' => PARAM_INT,
134 'optional' => true
135 ),
136 'hasprogress' => array(
137 'type' => PARAM_BOOL
3cfff885
BB
138 ),
139 'isfavourite' => array(
140 'type' => PARAM_BOOL
e6f03948
P
141 ),
142 'hidden' => array(
143 'type' => PARAM_BOOL
98a52c80
VD
144 ),
145 'timeaccess' => array(
146 'type' => PARAM_INT,
147 'optional' => true
240e5236 148 )
f5f02ac0
FM
149 );
150 }
6481a21f
BB
151
152 /**
153 * Get the course image if added to course.
154 *
155 * @param object $course
156 * @return string url of course image
157 */
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()
168 ];
169 $path = implode('/', $pathcomponents);
170 return (new moodle_url($path))->out();
171 }
172 }
173 return false;
174 }
175
176 /**
177 * Get the course pattern datauri.
178 *
179 * The datauri is an encoded svg that can be passed as a url.
180 * @param object $course
181 * @return string datauri
182 */
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();
189 }
190
191 /**
192 * Get the course progress percentage.
193 *
194 * @param object $course
195 * @return int progress
196 */
197 public static function get_course_progress($course) {
198 return \core_completion\progress::get_course_progress_percentage($course);
199 }
200
201 /**
202 * Get the course color.
203 *
204 * @param int $courseid
205 * @return string hex color code.
206 */
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'];
211
212 $color = $basecolors[$courseid % 10];
213 return $color;
214 }
f5f02ac0 215}