Proyectos de Subversion Moodle

Rev

| 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
 * Featured courses renderable
19
 *
20
 * @package    block_featured_courses
21
 * @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_featured_courses\output;
26
 
27
use block_featured_courses\mini_course_summary_exporter;
28
use coding_exception;
29
use context_course;
30
use context_helper;
31
use dml_exception;
32
use renderable;
33
use renderer_base;
34
use templatable;
35
 
36
/**
37
 * Class containing data for featured_courses block.
38
 *
39
 * @package    block_featured_courses
40
 * @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
41
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
43
class featured_courses implements renderable, templatable {
44
 
45
    /**
46
     * The list of the courses. Initialized empty
47
     *
48
     * @var array $courses
49
     */
50
    public $courses = [];
51
 
52
    /**
53
     * featured_courses constructor.
54
     * Retrieve matchin courses
55
     *
56
     * @param array $coursesid
57
     * @throws coding_exception
58
     * @throws dml_exception
59
     */
60
    public function __construct(array $coursesid) {
61
        global $DB;
62
        // First make sure that we have id in the table and not empty strings.
63
        $realcourseids = [];
64
        foreach ($coursesid as $cid) {
65
            if ($cid && is_numeric($cid)) {
66
                $realcourseids[] = $cid;
67
            }
68
        }
69
        if (empty($realcourseids)) {
70
            $this->courses = [];
71
        } else {
72
            list($sql, $params) = $DB->get_in_or_equal($realcourseids, SQL_PARAMS_NAMED);
73
            $this->courses = $DB->get_records_select('course', 'id ' . $sql, $params);
74
        }
75
    }
76
 
77
    /**
78
     * Export the renderable for template
79
     *
80
     * @param renderer_base $renderer
81
     * @return array
82
     * @throws coding_exception
83
     */
84
    public function export_for_template(renderer_base $renderer): array {
85
        $formattedcourses = array_map(function($course) use ($renderer) {
86
            context_helper::preload_from_record($course);
87
            $context = context_course::instance($course->id);
88
            $exporter = new mini_course_summary_exporter($course, ['context' => $context]);
89
            return (array) $exporter->export($renderer);
90
        }, $this->courses);
91
        return [
92
            'courses' => array_values($formattedcourses),
93
        ];
94
    }
95
}