Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 */
1093 ariadna 24
 
1 efrain 25
namespace core_course\external;
1093 ariadna 26
 
1 efrain 27
defined('MOODLE_INTERNAL') || die();
28
 
29
use renderer_base;
30
use moodle_url;
31
 
32
/**
33
 * Class for exporting a course summary from an stdClass.
34
 *
35
 * @copyright  2015 Damyon Wiese
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
1093 ariadna 38
class course_summary_exporter extends \core\external\exporter
39
{
1 efrain 40
 
41
    /**
42
     * Constructor - saves the persistent object, and the related objects.
43
     *
44
     * @param mixed $data - Either an stdClass or an array of values.
45
     * @param array $related - An optional list of pre-loaded objects related to this object.
46
     */
1093 ariadna 47
    public function __construct($data, $related = array())
48
    {
1 efrain 49
        if (!array_key_exists('isfavourite', $related)) {
50
            $related['isfavourite'] = false;
51
        }
52
        parent::__construct($data, $related);
53
    }
54
 
1093 ariadna 55
    protected static function define_related()
56
    {
1 efrain 57
        // We cache the context so it does not need to be retrieved from the course.
58
        return array('context' => '\\context', 'isfavourite' => 'bool?');
59
    }
60
 
1093 ariadna 61
    protected function get_other_values(renderer_base $output)
62
    {
1 efrain 63
        global $CFG;
64
        $courseimage = self::get_course_image($this->data);
65
        if (!$courseimage) {
66
            $courseimage = $output->get_generated_image_for_id($this->data->id);
67
        }
68
        $progress = self::get_course_progress($this->data);
69
        $hasprogress = false;
70
        if ($progress === 0 || $progress > 0) {
71
            $hasprogress = true;
72
        }
73
        $progress = floor($progress ?? 0);
74
        $coursecategory = \core_course_category::get($this->data->category, MUST_EXIST, true);
75
        return array(
76
            'fullnamedisplay' => get_course_display_name_for_list($this->data),
77
            'viewurl' => (new moodle_url('/course/view.php', array('id' => $this->data->id)))->out(false),
78
            'courseimage' => $courseimage,
79
            'progress' => $progress,
80
            'hasprogress' => $hasprogress,
81
            'isfavourite' => $this->related['isfavourite'],
82
            'hidden' => boolval(get_user_preferences('block_myoverview_hidden_course_' . $this->data->id, 0)),
83
            'showshortname' => $CFG->courselistshortnames ? true : false,
84
            'coursecategory' => $coursecategory->name
85
        );
86
    }
87
 
1093 ariadna 88
    public static function define_properties()
89
    {
1 efrain 90
        return array(
91
            'id' => array(
92
                'type' => PARAM_INT,
93
            ),
94
            'fullname' => array(
95
                'type' => PARAM_TEXT,
96
            ),
97
            'shortname' => array(
98
                'type' => PARAM_TEXT,
99
            ),
100
            'idnumber' => array(
101
                'type' => PARAM_RAW,
102
            ),
103
            'summary' => array(
104
                'type' => PARAM_RAW,
105
                'null' => NULL_ALLOWED,
106
                'default' => null,
107
            ),
108
            'summaryformat' => array(
109
                'type' => PARAM_INT,
110
                'default' => FORMAT_MOODLE,
111
            ),
112
            'startdate' => array(
113
                'type' => PARAM_INT,
114
            ),
115
            'enddate' => array(
116
                'type' => PARAM_INT,
117
            ),
118
            'visible' => array(
119
                'type' => PARAM_BOOL,
120
            ),
121
            'showactivitydates' => [
122
                'type' => PARAM_BOOL,
123
                'null' => NULL_ALLOWED
124
            ],
125
            'showcompletionconditions' => [
126
                'type' => PARAM_BOOL,
127
                'null' => NULL_ALLOWED
128
            ],
129
            'pdfexportfont' => [
130
                'type' => PARAM_TEXT,
131
                'null' => NULL_ALLOWED,
132
                'default' => null,
133
            ],
134
        );
135
    }
136
 
137
    /**
138
     * Get the formatting parameters for the summary.
139
     *
140
     * @return array
141
     */
1093 ariadna 142
    protected function get_format_parameters_for_summary()
143
    {
1 efrain 144
        return [
145
            'component' => 'course',
146
            'filearea' => 'summary',
147
        ];
148
    }
149
 
1093 ariadna 150
    public static function define_other_properties()
151
    {
1 efrain 152
        return array(
153
            'fullnamedisplay' => array(
154
                'type' => PARAM_TEXT,
155
            ),
156
            'viewurl' => array(
157
                'type' => PARAM_URL,
158
            ),
159
            'courseimage' => array(
160
                'type' => PARAM_RAW,
161
            ),
162
            'progress' => array(
163
                'type' => PARAM_INT,
164
                'optional' => true
165
            ),
166
            'hasprogress' => array(
167
                'type' => PARAM_BOOL
168
            ),
169
            'isfavourite' => array(
170
                'type' => PARAM_BOOL
171
            ),
172
            'hidden' => array(
173
                'type' => PARAM_BOOL
174
            ),
175
            'timeaccess' => array(
176
                'type' => PARAM_INT,
177
                'optional' => true
178
            ),
179
            'showshortname' => array(
180
                'type' => PARAM_BOOL
181
            ),
182
            'coursecategory' => array(
183
                'type' => PARAM_TEXT
184
            )
185
        );
186
    }
187
 
188
    /**
189
     * Get the course image if added to course.
190
     *
191
     * @param object $course
192
     * @return string|false url of course image or false if it's not exist.
193
     */
1093 ariadna 194
    public static function get_course_image($course)
195
    {
1 efrain 196
        $image = \cache::make('core', 'course_image')->get($course->id);
197
 
198
        if (is_null($image)) {
199
            $image = false;
200
        }
201
 
202
        return $image;
203
    }
204
 
205
    /**
206
     * Get the course pattern datauri.
207
     *
208
     * The datauri is an encoded svg that can be passed as a url.
209
     * @param object $course
210
     * @return string datauri
211
     * @deprecated 3.7
212
     */
1093 ariadna 213
    public static function get_course_pattern($course)
214
    {
1 efrain 215
        global $OUTPUT;
216
        debugging('course_summary_exporter::get_course_pattern() is deprecated. ' .
217
            'Please use $OUTPUT->get_generated_image_for_id() instead.', DEBUG_DEVELOPER);
218
        return $OUTPUT->get_generated_image_for_id($course->id);
219
    }
220
 
221
    /**
222
     * Get the course progress percentage.
223
     *
224
     * @param object $course
225
     * @return int progress
226
     */
1093 ariadna 227
    public static function get_course_progress($course)
228
    {
1 efrain 229
        return \core_completion\progress::get_course_progress_percentage($course);
230
    }
231
 
232
    /**
233
     * Get the course color.
234
     *
235
     * @param int $courseid
236
     * @return string hex color code.
237
     * @deprecated 3.7
238
     */
1093 ariadna 239
    public static function coursecolor($courseid)
240
    {
1 efrain 241
        global $OUTPUT;
242
        debugging('course_summary_exporter::coursecolor() is deprecated. ' .
243
            'Please use $OUTPUT->get_generated_color_for_id() instead.', DEBUG_DEVELOPER);
244
        return $OUTPUT->get_generated_color_for_id($courseid);
245
    }
246
}