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
 * Course class utility class
19
 *
20
 * @package   theme_universe
1441 ariadna 21
 * @copyright 2024 Marcin Czaja (https://rosea.io)
1 efrain 22
 * @license   Commercial https://themeforest.net/licenses
23
 */
24
 
25
namespace theme_universe\util;
26
 
27
use moodle_url;
28
use core_course_list_element;
29
use coursecat_helper;
30
use core_course_category;
31
use html_writer;
32
use context_course;
1441 ariadna 33
use theme_config;
1 efrain 34
 
35
/**
36
 * Course class utility class
37
 *
38
 * @package   theme_universe
1441 ariadna 39
 * @copyright 2024 Marcin Czaja (https://rosea.io)
1 efrain 40
 * @license   Commercial https://themeforest.net/licenses
41
 */
42
class course {
43
    /**
44
     * @var \stdClass $course The course object.
45
     */
46
    protected $course;
47
 
48
    /**
49
     * Class constructor
50
     *
51
     * @param core_course_list_element $course
52
     *
53
     */
54
    public function __construct($course) {
55
        $this->course = $course;
56
    }
57
 
58
    /**
59
     * Returns the first course's summary image url
60
     *
61
     * @return string
62
     */
1441 ariadna 63
    public function get_summary_image($returngeneratedimageifnone = true) {
1 efrain 64
        global $CFG, $OUTPUT;
65
 
66
        foreach ($this->course->get_course_overviewfiles() as $file) {
67
            if ($file->is_valid_image()) {
1441 ariadna 68
                $url = moodle_url::make_file_url(
69
                    "$CFG->wwwroot/pluginfile.php",
1 efrain 70
                    '/' . $file->get_contextid() . '/' . $file->get_component() . '/' .
1441 ariadna 71
                        $file->get_filearea() . $file->get_filepath() . $file->get_filename(),
72
                    !$file->is_valid_image()
73
                );
1 efrain 74
                return $url->out();
75
            }
76
        }
77
 
1441 ariadna 78
        if (!$returngeneratedimageifnone) {
79
            return '';
80
        }
81
 
1 efrain 82
        return $OUTPUT->get_generated_image_for_id($this->course->id);
83
    }
84
 
85
    /**
86
     * Returns HTML to display course contacts.
87
     *
88
     * @return array
89
     */
90
    public function get_course_contacts() {
91
        $theme = \theme_config::load('universe');
92
 
93
        $contacts = [];
94
        if ($this->course->has_course_contacts() && $theme->settings->cccteachers == 1) {
95
            $instructors = $this->course->get_course_contacts();
96
 
97
            foreach ($instructors as $instructor) {
98
                $user = $instructor['user'];
99
                $userutil = new user($user->id);
100
 
101
                $contacts[] = [
102
                    'id' => $user->id,
103
                    'fullname' => fullname($user),
104
                    'userpicture' => $userutil->get_user_picture(),
105
                    'role' => $instructor['role']->displayname
106
                ];
107
            }
108
        }
109
 
110
        return $contacts;
111
    }
112
 
113
    /**
1441 ariadna 114
     * Returns HTML to display course contacts for filter.
1 efrain 115
     *
1441 ariadna 116
     * @return array
1 efrain 117
     */
1441 ariadna 118
    public function get_meta_course_contacts() {
119
        if ($this->course->has_course_contacts()) {
120
            $contacts = [];
1 efrain 121
 
1441 ariadna 122
            $instructors = $this->course->get_course_contacts();
123
 
124
            // Counter for teachers IDs separator.
125
            $k = 0;
126
            $last_key = count($instructors);
127
 
128
            foreach ($instructors as $instructor) {
129
                $k++;
130
                $user = $instructor['user'];
131
 
132
                $contacts = "{$user->firstname} {$user->lastname}";
133
 
134
                // Counter for teachers IDs separator.
135
                if ($k < $last_key) {
136
                    $contacts =  ', ';
137
                }
138
            }
139
 
140
            return $contacts;
1 efrain 141
        }
1441 ariadna 142
    }
1 efrain 143
 
1441 ariadna 144
    /**
145
     * Returns HTML to display course language for filter.
146
     *
147
     * @return array
148
     */
149
    public function get_meta_course_lang() {
150
        $lang = strval($this->course->lang);
151
        return $lang;
1 efrain 152
    }
153
 
154
    /**
1441 ariadna 155
     * Returns HTML to display course custom field for a given filter.
1 efrain 156
     *
1441 ariadna 157
     * @param int $filterNumber The filter number (e.g., 1, 2, 3, etc.).
158
     * @return string The HTML output for the filter.
1 efrain 159
     */
1441 ariadna 160
    public function get_meta_course_customfilter($filterNumber) {
161
        global $DB;
162
        $output = '';
163
 
164
        $theme = theme_config::load('universe');
165
 
166
        $customfilter = $theme->settings->{'customfilter' . $filterNumber};
167
        $record = $DB->get_record('customfield_field', ['shortname' => $customfilter]);
168
 
169
        if ($record != null) {
170
            $cfid = $record;
171
            $record = $DB->get_record('customfield_data', ['fieldid' => $cfid->id, 'instanceid' => $this->course->id]);
172
 
173
            if ($record != null) {
174
                if($record->intvalue != null) {
175
 
176
                    $fieldid = $DB->get_record('customfield_field', ['id' => $record->fieldid]);
177
                    $jsonString = $fieldid->configdata;
178
 
179
                    // Decode JSON string into a PHP associative array
180
                    $array = json_decode($jsonString, true);
181
                    // Check if decoding was successful
182
                    if (json_last_error() === JSON_ERROR_NONE) {
183
                        // Access the 'options' key
184
                        $options = $array['options'];
185
 
186
                        // // Print the options
187
                        // echo $options;
188
                    } else {
189
                        // Handle JSON decoding error
190
                        echo "Error decoding JSON: " . json_last_error_msg();
191
                    }
192
                    $optionsArray = explode("\r\n", $options);
193
                    $key = $record->intvalue - 1;
194
                    if($key >= 0) {
195
                        $output .= $optionsArray[$key];
196
                    }
197
                } else {
198
                    $output .= $record->value;
199
                }
200
            }
201
 
1 efrain 202
        }
203
 
1441 ariadna 204
        return $output;
1 efrain 205
    }
206
 
207
    /**
1441 ariadna 208
     * Returns HTML to display course custom field #1 for filter.
1 efrain 209
     */
1441 ariadna 210
    public function get_meta_course_customfilter1() {
211
        return $this->get_meta_course_customfilter(1);
212
    }
1 efrain 213
 
1441 ariadna 214
    /**
215
     * Returns HTML to display course custom field #2 for filter.
216
     */
217
    public function get_meta_course_customfilter2() {
218
        return $this->get_meta_course_customfilter(2);
219
    }
1 efrain 220
 
1441 ariadna 221
    /**
222
     * Returns HTML to display course custom field #3 for filter.
223
     */
224
    public function get_meta_course_customfilter3() {
225
        return $this->get_meta_course_customfilter(3);
1 efrain 226
    }
227
 
228
    /**
1441 ariadna 229
     * Returns HTML to display course custom field #4 for filter.
230
     */
231
    public function get_meta_course_customfilter4() {
232
        return $this->get_meta_course_customfilter(4);
233
    }
234
 
235
    /**
236
     * Returns HTML to display course custom field #5 for filter.
237
     */
238
    public function get_meta_course_customfilter5() {
239
        return $this->get_meta_course_customfilter(5);
240
    }
241
 
242
    /**
243
     * Returns HTML to display course custom field #6 for filter.
244
     */
245
    public function get_meta_course_customfilter6() {
246
        return $this->get_meta_course_customfilter(6);
247
    }
248
 
249
 
250
    /**
251
     * Returns HTML to display course category name.
1 efrain 252
     *
1441 ariadna 253
     * @return string
254
     *
255
     * @throws \moodle_exception
1 efrain 256
     */
1441 ariadna 257
    public function get_category(): string {
258
        $cat = core_course_category::get($this->course->category, IGNORE_MISSING);
259
 
260
        if (!$cat) {
261
            return '';
1 efrain 262
        }
263
 
1441 ariadna 264
        return $cat->get_formatted_name();
1 efrain 265
    }
266
 
267
    /**
1441 ariadna 268
     * Returns course summary.
1 efrain 269
     *
1441 ariadna 270
     * @param coursecat_helper $chelper
1 efrain 271
     */
1441 ariadna 272
    public function get_summary(coursecat_helper $chelper): string {
273
        if ($this->course->has_summary()) {
274
            return $chelper->get_course_formatted_summary(
275
                $this->course,
276
                ['overflowdiv' => true, 'noclean' => true, 'para' => false]
277
            );
278
        }
279
 
280
        return false;
1 efrain 281
    }
282
 
283
    public function course_get_taux() {
284
        global $CFG, $COURSE;
285
 
286
        $course = get_course($this->course->id);
1441 ariadna 287
        $customfieldvalue = null;
1 efrain 288
        $courseelement = new \core_course_list_element($course);
289
 
290
        if ($courseelement->has_custom_fields()) {
291
 
292
            $fields = $courseelement->get_custom_fields();
293
            $content = '';
294
 
1441 ariadna 295
            foreach ($fields as $field) {
1 efrain 296
                if (empty($field->get_value())) {
297
                    continue;
298
                }
299
 
300
                // Get field shortname
301
                $customfieldname = $field->get_field()->get('name');
1441 ariadna 302
 
303
                switch ($field->get_field()->get('type')) {
304
                    case 'select':
305
                        $value = $field->get_value();
306
 
307
                        if ($value != null) {
308
                            $customfieldvalue = null;
309
                        }
310
 
311
                        $options = $field->get_field()->get_options();
312
                        if (array_key_exists($value, $options)) {
313
                            $customfieldvalue = format_string(
314
                                $options[$value],
315
                                true,
316
                                ['context' => $field->get_field()->get_handler()->get_configuration_context()]
317
                            );
318
                        }
319
                        break;
320
                    default:
321
                        $customfieldvalue = $field->get_value();
322
                        break;
323
                }
324
 
1 efrain 325
                $customfieldshortname =  $field->get_field()->get('shortname');
326
 
327
                // Array with custom fields which need to be hidden.
1441 ariadna 328
                $hiddencustomfields = array(
329
                    "enrolldesc",
330
                    "enrollvideo",
331
                    "customcoursefavicon",
332
                    "topbarcolor",
333
                    "dmtopbarcolor",
334
                    "maincolor1",
335
                    "maincolor2",
336
                    "customcourselogo",
337
                    "customcoursedmlogo",
338
                    "footercolor",
339
                    "topbartextcolor",
340
                    "topbarbghovercolor",
341
                    "topbartexthovercolor",
342
                    "dmtopbartextcolor",
343
                    "dmtopbarbghovercolor",
344
                    "dmtopbartexthovercolor",
345
                    "topbarcolor2",
346
                    "dmtopbarcolor2"
347
                );
348
                $hiddencftitles = ["tool_courserating"];
1 efrain 349
 
1441 ariadna 350
                if (!in_array($customfieldshortname, $hiddencustomfields)) {
351
                    $content .= html_writer::start_tag('div', ['class' => 'rui-custom-field-box rui-cf-' . $customfieldshortname]);
352
                    if (!in_array($customfieldshortname, $hiddencftitles)) {
353
                        $content .= html_writer::tag(
354
                            'div',
355
                            format_string($customfieldname),
356
                            array('class' => 'rui-custom-field-name rui-custom-field-' . $customfieldshortname)
357
                        );
358
                    }
359
                    $content .= html_writer::tag(
360
                        'div',
361
                        format_string($customfieldvalue),
362
                        ['class' => 'rui-custom-field-value']
363
                    );
1 efrain 364
                    $content .= html_writer::end_tag('div');
365
                }
366
            }
367
            return $content;
368
        } else {
369
            return false;
370
        }
371
    }
1441 ariadna 372
 
373
 
374
    /**
375
     * Returns HTML to display course enrolment icons.
376
     *
377
     * @return array
378
     */
379
    public function get_enrolment_icons(): array {
380
        if ($icons = enrol_get_course_info_icons($this->course)) {
381
            return $icons;
382
        }
383
 
384
        return [];
385
    }
386
 
387
    /**
388
     * Get the user progress in the course.
389
     *
390
     * @param null $userid
391
     *
392
     * @return mixed
393
     */
394
    public function get_progress($userid = null) {
395
        return \core_completion\progress::get_course_progress_percentage($this->course, $userid);
396
    }
1 efrain 397
}