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
 * Contains the default section availability output class.
19
 *
20
 * @package   core_courseformat
21
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_courseformat\output\local\content\section;
26
 
27
use context_course;
28
use core_availability_multiple_messages;
29
use core\output\named_templatable;
30
use core_availability\info;
31
use core_availability\info_section;
32
use core_courseformat\base as course_format;
33
use core_courseformat\output\local\courseformat_named_templatable;
34
use renderable;
35
use section_info;
36
use stdClass;
37
 
38
/**
39
 * Base class to render section availability.
40
 *
41
 * @package   core_courseformat
42
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
43
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44
 */
45
class availability implements named_templatable, renderable {
46
 
47
    use courseformat_named_templatable;
48
 
49
    /** @var course_format the course format class */
50
    protected $format;
51
 
52
    /** @var section_info the section object */
53
    protected $section;
54
 
55
    /** @var string the has availability attribute name */
56
    protected $hasavailabilityname;
57
 
58
    /** @var stdClass|null the instance export data */
59
    protected $data = null;
60
 
61
    /** @var int Availability excerpt text max size treshold */
62
    protected const AVAILABILITY_EXCERPT_MAXSIZE = 100;
63
 
64
    /**
65
     * Constructor.
66
     *
67
     * @param course_format $format the course format
68
     * @param section_info $section the section info
69
     */
70
    public function __construct(course_format $format, section_info $section) {
71
        $this->format = $format;
72
        $this->section = $section;
73
        $this->hasavailabilityname = 'hasavailability';
74
    }
75
 
76
    /**
77
     * Export this data so it can be used as the context for a mustache template.
78
     *
79
     * @param \renderer_base $output typically, the renderer that's calling this function
1441 ariadna 80
     * @return stdClass|null data context for a mustache template
1 efrain 81
     */
1441 ariadna 82
    public function export_for_template(\renderer_base $output): ?stdClass {
1 efrain 83
        $this->build_export_data($output);
84
        return $this->data;
85
    }
86
 
87
    /**
88
     * Returns if the output has availability info to display.
89
     *
90
     * @param \renderer_base $output typically, the renderer that's calling this function
91
     * @return bool if the element has availability data to display
92
     */
93
    public function has_availability(\renderer_base $output): bool {
94
        $this->build_export_data($output);
95
        $attributename = $this->hasavailabilityname;
96
        return $this->data->$attributename;
97
    }
98
 
99
    /**
100
     * Protected method to build the export data.
101
     *
102
     * @param \renderer_base $output typically, the renderer that's calling this function
103
     */
104
    protected function build_export_data(\renderer_base $output) {
105
        if (!empty($this->data)) {
106
            return;
107
        }
108
 
109
        $data = (object) $this->get_info($output);
110
 
111
        $attributename = $this->hasavailabilityname;
112
        $data->$attributename = !empty($data->info);
113
 
114
        $this->data = $data;
115
    }
116
 
117
    /**
118
     * Export this data so it can be used as the context for a mustache template.
119
     *
120
     * If section is not visible, display the message about that ('Not available
121
     * until...', that sort of thing). Otherwise, returns blank.
122
     *
123
     * For users with the ability to view hidden sections, it shows the
124
     * information even though you can view the section and also may include
125
     * slightly fuller information (so that teachers can tell when sections
126
     * are going to be unavailable etc). This logic is the same as for
127
     * activities.
128
     *
129
     * @param \renderer_base $output typically, the renderer that's calling this function
130
     * @return stdclass data context for a mustache template
131
     */
132
    protected function get_info(\renderer_base $output): array {
133
        global $CFG, $USER;
134
 
135
        $section = $this->section;
136
        $context = context_course::instance($section->course);
137
 
138
        $canviewhidden = has_capability('moodle/course:viewhiddensections', $context, $USER);
139
 
140
        $editurl = new \moodle_url(
141
            '/course/editsection.php',
142
            ['id' => $this->section->id, 'showonly' => 'availabilityconditions']
143
        );
144
        $info = ['editurl' => $editurl->out(false)];
1441 ariadna 145
 
146
        if ($section->is_orphan()) {
147
            $info['editing'] = false;
148
        }
149
 
1 efrain 150
        if (!$section->visible) {
151
            return [];
152
        } else if (!$section->uservisible) {
153
            if ($section->availableinfo) {
154
                // Note: We only get to this function if availableinfo is non-empty,
155
                // so there is definitely something to print.
156
                $info['info'] = $this->get_availability_data($output, $section->availableinfo, 'isrestricted');
157
            }
158
        } else if ($canviewhidden && !empty($CFG->enableavailability)) {
159
            // Check if there is an availability restriction.
160
            $ci = new info_section($section);
161
            $fullinfo = $ci->get_full_information();
162
            if ($fullinfo) {
163
                $info['info'] = $this->get_availability_data($output, $fullinfo, 'isrestricted isfullinfo');
164
            }
165
        }
166
 
167
        return $info;
168
    }
169
 
170
    /**
171
     * Get the basic availability information data.
172
     *
173
     * @param \renderer_base $output typically, the renderer that's calling this function
174
     * @param string|core_availability_multiple_messages $availabilityinfo the avalability info
175
     * @param string $additionalclasses additional css classes
176
     * @return stdClass the availability information data
177
     */
178
    protected function get_availability_data($output, $availabilityinfo, $additionalclasses = ''): stdClass {
179
        // At this point, availabilityinfo is either a string or a renderable. We need to handle both cases in a different way.
180
        if (is_string($availabilityinfo)) {
181
            $data = $this->availability_info_from_string($output, $availabilityinfo);
182
        } else {
183
            $data = $this->availability_info_from_output($output, $availabilityinfo);
184
        }
185
 
186
        $data->classes = $additionalclasses;
187
 
188
        $additionalclasses = array_filter(explode(' ', $additionalclasses));
189
        if (in_array('ishidden', $additionalclasses)) {
190
            $data->ishidden = 1;
191
        } else if (in_array('isstealth', $additionalclasses)) {
192
            $data->isstealth = 1;
193
        } else if (in_array('isrestricted', $additionalclasses)) {
194
            $data->isrestricted = 1;
195
            if (in_array('isfullinfo', $additionalclasses)) {
196
                $data->isfullinfo = 1;
197
            }
198
        }
199
 
200
        return $data;
201
    }
202
 
203
    /**
204
     * Generate the basic availability information data from a string.
205
     * Just shorten availability text to generate the excerpt text.
206
     *
207
     * @param \renderer_base $output typically, the renderer that's calling this function
208
     * @param string $availabilityinfo the avalability info
209
     * @return stdClass the availability information data
210
     */
211
    protected function availability_info_from_string(\renderer_base $output, string $availabilityinfo): stdClass {
212
        $course = $this->format->get_course();
213
 
214
        $text = info::format_info($availabilityinfo, $course);
215
        $data = ['text' => $text];
216
 
217
        if (strlen(html_to_text($text, 0, false)) > self::AVAILABILITY_EXCERPT_MAXSIZE) {
218
            $data['excerpt'] = shorten_text($text, self::AVAILABILITY_EXCERPT_MAXSIZE);
219
        }
220
 
221
        return (object) $data;
222
    }
223
 
224
    /**
225
     * Generate the basic availability information data from a renderable.
226
     * Use the header and the first item to generate the excerpt text.
227
     *
228
     * @param \renderer_base $output typically, the renderer that's calling this function
229
     * @param core_availability_multiple_messages $availabilityinfo the avalability info
230
     * @return stdClass the availability information data
231
     */
232
    protected function availability_info_from_output(
233
        \renderer_base $output,
234
        core_availability_multiple_messages $availabilityinfo
235
    ): stdClass {
236
        $course = $this->format->get_course();
237
 
238
        $renderable = new \core_availability\output\availability_info($availabilityinfo);
239
        // We need to export_for_template() instead of directly render, to reuse the info for both 'text' and 'excerpt'.
240
        $info = $renderable->export_for_template($output);
241
 
242
        $text = $output->render_from_template('core_availability/availability_info', $info);
243
        $data = ['text' => info::format_info($text, $course)];
244
 
245
        if (!empty($info->items)) {
246
            $excerpttext = $info->header . ' ' . $info->items[0]->header;
247
            $data['excerpt'] = info::format_info($excerpttext, $course);
248
        }
249
 
250
        return (object) $data;
251
    }
252
 
253
    /**
254
     * @deprecated since Moodle 4.3 MDL-78204. Please use {@see self::get_availability_data} instead.
255
     */
1441 ariadna 256
    #[\core\attribute\deprecated('get_availability_data()', since: '4.3', mdl: 'MDL-78489', final: true)]
257
    protected function availability_info() {
258
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
1 efrain 259
    }
260
}