course_summary_exporter.php 7.16 KB
Newer Older
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
<?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 {

38
39
40
41
42
43
44
45
46
47
48
49
50
    /**
     * 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);
    }

51
52
    protected static function define_related() {
        // We cache the context so it does not need to be retrieved from the course.
53
        return array('context' => '\\context', 'isfavourite' => 'bool?');
54
55
56
    }

    protected function get_other_values(renderer_base $output) {
57
        global $CFG;
58
59
        $courseimage = self::get_course_image($this->data);
        if (!$courseimage) {
60
            $courseimage = $output->get_generated_image_for_id($this->data->id);
61
62
63
64
65
66
67
        }
        $progress = self::get_course_progress($this->data);
        $hasprogress = false;
        if ($progress === 0 || $progress > 0) {
            $hasprogress = true;
        }
        $progress = floor($progress);
68
        $coursecategory = \core_course_category::get($this->data->category, MUST_EXIST, true);
69
        return array(
70
            'fullnamedisplay' => get_course_display_name_for_list($this->data),
71
72
73
            'viewurl' => (new moodle_url('/course/view.php', array('id' => $this->data->id)))->out(false),
            'courseimage' => $courseimage,
            '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
78
            'showshortname' => $CFG->courselistshortnames ? true : false,
            'coursecategory' => $coursecategory->name
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        );
    }

    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,
95
96
97
            ),
            'summary' => array(
                'type' => PARAM_RAW,
98
                'null' => NULL_ALLOWED
99
            ),
100
101
102
            'summaryformat' => array(
                'type' => PARAM_INT,
            ),
103
104
105
106
107
            'startdate' => array(
                'type' => PARAM_INT,
            ),
            'enddate' => array(
                'type' => PARAM_INT,
108
109
110
111
            )
        );
    }

112
113
114
115
116
117
118
119
120
121
122
123
    /**
     * Get the formatting parameters for the summary.
     *
     * @return array
     */
    protected function get_format_parameters_for_summary() {
        return [
            'component' => 'course',
            'filearea' => 'summary',
        ];
    }

124
125
    public static function define_other_properties() {
        return array(
126
127
128
            'fullnamedisplay' => array(
                'type' => PARAM_TEXT,
            ),
129
130
            'viewurl' => array(
                'type' => PARAM_URL,
131
132
133
134
135
136
137
138
139
140
            ),
            'courseimage' => array(
                'type' => PARAM_RAW,
            ),
            'progress' => array(
                'type' => PARAM_INT,
                'optional' => true
            ),
            'hasprogress' => array(
                'type' => PARAM_BOOL
141
142
143
            ),
            'isfavourite' => array(
                'type' => PARAM_BOOL
144
145
146
            ),
            'hidden' => array(
                'type' => PARAM_BOOL
147
148
149
150
            ),
            'timeaccess' => array(
                'type' => PARAM_INT,
                'optional' => true
151
152
153
            ),
            'showshortname' => array(
                'type' => PARAM_BOOL
154
155
156
            ),
            'coursecategory' => array(
                'type' => PARAM_TEXT
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

    /**
     * Get the course image if added to course.
     *
     * @param object $course
     * @return string url of course image
     */
    public static function get_course_image($course) {
        global $CFG;
        $courseinlist = new \core_course_list_element($course);
        foreach ($courseinlist->get_course_overviewfiles() as $file) {
            if ($file->is_valid_image()) {
                $pathcomponents = [
                    '/pluginfile.php',
                    $file->get_contextid(),
                    $file->get_component(),
                    $file->get_filearea() . $file->get_filepath() . $file->get_filename()
                ];
                $path = implode('/', $pathcomponents);
                return (new moodle_url($path))->out();
            }
        }
        return false;
    }

    /**
     * Get the course pattern datauri.
     *
     * The datauri is an encoded svg that can be passed as a url.
     * @param object $course
     * @return string datauri
     */
    public static function get_course_pattern($course) {
193
194
195
196
        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);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    }

    /**
     * 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.
     */
    public static function coursecolor($courseid) {
216
217
218
219
        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);
220
    }
221
}