mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
course_summary_exporter.php
230 lines (215 loc) · 7.26 KB
/
course_summary_exporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class for exporting a course summary from an stdClass.
*
* @package core_course
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\external;
defined('MOODLE_INTERNAL') || die();
use renderer_base;
use moodle_url;
/**
* Class for exporting a course summary from an stdClass.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_summary_exporter extends \core\external\exporter {
/**
* Constructor - saves the persistent object, and the related objects.
*
* @param mixed $data - Either an stdClass or an array of values.
* @param array $related - An optional list of pre-loaded objects related to this object.
*/
public function __construct($data, $related = array()) {
if (!array_key_exists('isfavourite', $related)) {
$related['isfavourite'] = false;
}
parent::__construct($data, $related);
}
protected static function define_related() {
// We cache the context so it does not need to be retrieved from the course.
return array('context' => '\\context', 'isfavourite' => 'bool?');
}
protected function get_other_values(renderer_base $output) {
global $CFG;
$courseimage = self::get_course_image($this->data);
if (!$courseimage) {
$courseimage = $output->get_generated_image_for_id($this->data->id);
}
$progress = self::get_course_progress($this->data);
$hasprogress = false;
if ($progress === 0 || $progress > 0) {
$hasprogress = true;
}
$progress = floor($progress ?? 0);
$coursecategory = \core_course_category::get($this->data->category, MUST_EXIST, true);
return array(
'fullnamedisplay' => get_course_display_name_for_list($this->data),
'viewurl' => (new moodle_url('/course/view.php', array('id' => $this->data->id)))->out(false),
'courseimage' => $courseimage,
'progress' => $progress,
'hasprogress' => $hasprogress,
'isfavourite' => $this->related['isfavourite'],
'hidden' => boolval(get_user_preferences('block_myoverview_hidden_course_' . $this->data->id, 0)),
'showshortname' => $CFG->courselistshortnames ? true : false,
'coursecategory' => $coursecategory->name
);
}
public static function define_properties() {
return array(
'id' => array(
'type' => PARAM_INT,
),
'fullname' => array(
'type' => PARAM_TEXT,
),
'shortname' => array(
'type' => PARAM_TEXT,
),
'idnumber' => array(
'type' => PARAM_RAW,
),
'summary' => array(
'type' => PARAM_RAW,
'null' => NULL_ALLOWED
),
'summaryformat' => array(
'type' => PARAM_INT,
),
'startdate' => array(
'type' => PARAM_INT,
),
'enddate' => array(
'type' => PARAM_INT,
),
'visible' => array(
'type' => PARAM_BOOL,
),
'showactivitydates' => [
'type' => PARAM_BOOL,
'null' => NULL_ALLOWED
],
'showcompletionconditions' => [
'type' => PARAM_BOOL,
'null' => NULL_ALLOWED
],
'pdfexportfont' => [
'type' => PARAM_TEXT,
'null' => NULL_ALLOWED
],
);
}
/**
* Get the formatting parameters for the summary.
*
* @return array
*/
protected function get_format_parameters_for_summary() {
return [
'component' => 'course',
'filearea' => 'summary',
];
}
public static function define_other_properties() {
return array(
'fullnamedisplay' => array(
'type' => PARAM_TEXT,
),
'viewurl' => array(
'type' => PARAM_URL,
),
'courseimage' => array(
'type' => PARAM_RAW,
),
'progress' => array(
'type' => PARAM_INT,
'optional' => true
),
'hasprogress' => array(
'type' => PARAM_BOOL
),
'isfavourite' => array(
'type' => PARAM_BOOL
),
'hidden' => array(
'type' => PARAM_BOOL
),
'timeaccess' => array(
'type' => PARAM_INT,
'optional' => true
),
'showshortname' => array(
'type' => PARAM_BOOL
),
'coursecategory' => array(
'type' => PARAM_TEXT
)
);
}
/**
* Get the course image if added to course.
*
* @param object $course
* @return string|false url of course image or false if it's not exist.
*/
public static function get_course_image($course) {
$image = \cache::make('core', 'course_image')->get($course->id);
if (is_null($image)) {
$image = false;
}
return $image;
}
/**
* Get the course pattern datauri.
*
* The datauri is an encoded svg that can be passed as a url.
* @param object $course
* @return string datauri
* @deprecated 3.7
*/
public static function get_course_pattern($course) {
global $OUTPUT;
debugging('course_summary_exporter::get_course_pattern() is deprecated. ' .
'Please use $OUTPUT->get_generated_image_for_id() instead.', DEBUG_DEVELOPER);
return $OUTPUT->get_generated_image_for_id($course->id);
}
/**
* Get the course progress percentage.
*
* @param object $course
* @return int progress
*/
public static function get_course_progress($course) {
return \core_completion\progress::get_course_progress_percentage($course);
}
/**
* Get the course color.
*
* @param int $courseid
* @return string hex color code.
* @deprecated 3.7
*/
public static function coursecolor($courseid) {
global $OUTPUT;
debugging('course_summary_exporter::coursecolor() is deprecated. ' .
'Please use $OUTPUT->get_generated_color_for_id() instead.', DEBUG_DEVELOPER);
return $OUTPUT->get_generated_color_for_id($courseid);
}
}