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
+ * Thumblinks Action block renderable.
19
+ *
20
+ * @package block_thumblinks_action
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
+ global $ CFG ;
27
+
28
+ defined ('MOODLE_INTERNAL ' ) || die ();
29
+
30
+ use context_course ;
31
+ use context_helper ;
32
+ use core_course \external \course_summary_exporter ;
33
+ use moodle_url ;
34
+ use renderable ;
35
+ use renderer_base ;
36
+ use templatable ;
37
+
38
+ class mini_course_summary_exporter extends course_summary_exporter {
39
+
40
+ /**
41
+ * Only a subset of the usual.
42
+ *
43
+ * @return array|array[]
44
+ */
45
+ public static function define_other_properties () {
46
+ return array (
47
+ 'fullnamedisplay ' => array (
48
+ 'type ' => PARAM_TEXT ,
49
+ ),
50
+ 'viewurl ' => array (
51
+ 'type ' => PARAM_URL ,
52
+ ),
53
+ 'courseimage ' => array (
54
+ 'type ' => PARAM_RAW ,
55
+ ),
56
+ 'showshortname ' => array (
57
+ 'type ' => PARAM_BOOL
58
+ ),
59
+ 'coursecategory ' => array (
60
+ 'type ' => PARAM_TEXT
61
+ )
62
+ );
63
+ }
64
+
65
+ /**
66
+ * Constructor - saves the persistent object, and the related objects.
67
+ *
68
+ * @param mixed $data - Either an stdClass or an array of values.
69
+ * @param array $related - An optional list of pre-loaded objects related to this object.
70
+ */
71
+ public function __construct ($ data , $ related = array ()) {
72
+ \core \external \exporter::__construct ($ data , $ related );
73
+ }
74
+
75
+ protected static function define_related () {
76
+ // We cache the context so it does not need to be retrieved from the course.
77
+ return array ('context ' => '\\context ' );
78
+ }
79
+
80
+ protected function get_other_values (renderer_base $ output ) {
81
+ global $ CFG ;
82
+ $ courseimage = self ::get_course_image ($ this ->data );
83
+ if (!$ courseimage ) {
84
+ $ courseimage = $ output ->get_generated_image_for_id ($ this ->data ->id );
85
+ }
86
+ $ coursecategory = \core_course_category::get ($ this ->data ->category , MUST_EXIST , true );
87
+ return array (
88
+ 'fullnamedisplay ' => get_course_display_name_for_list ($ this ->data ),
89
+ 'viewurl ' => (new moodle_url ('/course/view.php ' , array ('id ' => $ this ->data ->id )))->out (false ),
90
+ 'courseimage ' => $ courseimage ,
91
+ 'showshortname ' => $ CFG ->courselistshortnames ? true : false ,
92
+ 'coursecategory ' => $ coursecategory ->name
93
+ );
94
+ }
95
+
96
+ }
97
+
98
+ /**
99
+ * Class containing data for featured_courses block.
100
+ *
101
+ * @package block_mcms
102
+ * @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
103
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
104
+ */
105
+ class featured_courses implements renderable, templatable {
106
+
107
+ /**
108
+ * @var array course
109
+ */
110
+ public $ courses = [];
111
+
112
+ /**
113
+ * featured_courses constructor.
114
+ * Retrieve matchin courses
115
+ *
116
+ * @param $coursesid
117
+ * @throws \coding_exception
118
+ * @throws \dml_exception
119
+ */
120
+ public function __construct ($ coursesid ) {
121
+ global $ DB ;
122
+ list ($ sql , $ params ) = $ DB ->get_in_or_equal ($ coursesid , SQL_PARAMS_NAMED );
123
+ $ this ->courses = $ DB ->get_records_select ('course ' , 'id ' . $ sql , $ params );
124
+ }
125
+
126
+ public function export_for_template (renderer_base $ renderer ) {
127
+ $ formattedcourses = array_map (function ($ course ) use ($ renderer ) {
128
+ context_helper::preload_from_record ($ course );
129
+ $ context = context_course::instance ($ course ->id );
130
+ $ exporter = new mini_course_summary_exporter ($ course , ['context ' => $ context ]);
131
+ $ exported = (array ) $ exporter ->export ($ renderer );
132
+ return $ exported ;
133
+ }, $ this ->courses );
134
+ $ exportedvalue = [
135
+ 'courses ' => array_values ((array ) $ formattedcourses ),
136
+ ];
137
+ return $ exportedvalue ;
138
+ }
139
+ }
0 commit comments