cm.php 8.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Contains the default activity list from a section.
 *
20
 * @package   core_courseformat
21
22
23
24
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

25
namespace core_courseformat\output\local\content;
26

27
28
29
use cm_info;
use core\activity_dates;
use core_completion\cm_completion_details;
30
31
use core_courseformat\base as course_format;
use core_course\output\activity_information;
32
use renderable;
33
use section_info;
34
use stdClass;
35
use templatable;
36
use \core_availability\info_module;
37
38
39
40

/**
 * Base class to render a course module inside a course format.
 *
41
 * @package   core_courseformat
42
43
44
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
45
class cm implements renderable, templatable {
46
47
48
49
50
51
52
53
54
55
56
57
58

    /** @var course_format the course format */
    protected $format;

    /** @var section_info the section object */
    private $section;

    /** @var cm_info the course module instance */
    protected $mod;

    /** @var array optional display options */
    protected $displayoptions;

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    /** @var string activity link css classes */
    protected $linkclasses = null;

    /** @var string text css classes */
    protected $textclasses = null;

    /** @var string the activity name output class name */
    protected $cmnameclass;

    /** @var string the activity control menu class name */
    protected $controlmenuclass;

    /** @var string the activity availability class name */
    protected $availabilityclass;

74
75
76
77
78
79
80
81
    /**
     * Constructor.
     *
     * @param course_format $format the course format
     * @param section_info $section the section info
     * @param cm_info $mod the course module ionfo
     * @param array $displayoptions optional extra display options
     */
82
    public function __construct(course_format $format, section_info $section, cm_info $mod, array $displayoptions = []) {
83
84
85
86
        $this->format = $format;
        $this->section = $section;
        $this->mod = $mod;
        $this->displayoptions = $displayoptions;
87
88
89
90
91
92
93

        $this->load_classes();

        // Get the necessary classes.
        $this->cmnameclass = $format->get_output_classname('content\\cm\\cmname');
        $this->controlmenuclass = $format->get_output_classname('content\\cm\\controlmenu');
        $this->availabilityclass = $format->get_output_classname('content\\cm\\availability');
94
95
96
97
98
    }

    /**
     * Export this data so it can be used as the context for a mustache template.
     *
99
     * @param \renderer_base $output typically, the renderer that's calling this function
100
101
102
     * @return stdClass data context for a mustache template
     */
    public function export_for_template(\renderer_base $output): stdClass {
103
104
        global $USER;

105
        $format = $this->format;
106
        $section = $this->section;
107
108
        $mod = $this->mod;
        $displayoptions = $this->displayoptions;
109
        $course = $mod->get_course();
110

111
112
113
114
115
        // Fetch completion details.
        $showcompletionconditions = $course->showcompletionconditions == COMPLETION_SHOW_CONDITIONS;
        $completiondetails = cm_completion_details::get_instance($mod, $USER->id, $showcompletionconditions);

        // Fetch activity dates.
116
117
118
119
        $activitydates = [];
        if ($course->showactivitydates) {
            $activitydates = activity_dates::get_dates_for_module($mod, $USER->id);
        }
120

121
122
123
124
125
126
        $displayoptions['linkclasses'] = $this->get_link_classes();
        $displayoptions['textclasses'] = $this->get_text_classes();

        // Grouping activity.
        $groupinglabel = $mod->get_grouping_label($displayoptions['textclasses']);

127
        $activityinfodata = null;
128
129
130
131
132
133
134
135
        // - There are activity dates to be shown; or
        // - Completion info needs to be displayed
        //   * The activity tracks completion; AND
        //   * The showcompletionconditions setting is enabled OR an activity that tracks manual completion needs the manual
        //     completion button to be displayed on the course homepage.
        $showcompletioninfo = $completiondetails->has_completion() && ($showcompletionconditions ||
                        (!$completiondetails->is_automatic() && $completiondetails->show_manual_completion()));
        if ($showcompletioninfo || !empty($activitydates)) {
136
137
138
139
            $activityinfo = new activity_information($mod, $completiondetails, $activitydates);
            $activityinfodata = $activityinfo->export_for_template($output);
        }

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
        // Mod inplace name editable.
        $cmname = new $this->cmnameclass(
            $format,
            $this->section,
            $mod,
            $format->show_editor(),
            $this->displayoptions
        );

        // Mod availability.
        $availability = new $this->availabilityclass(
            $format,
            $this->section,
            $mod,
            $this->displayoptions
        );

157
        $data = (object)[
158
159
            'cmname' => $cmname->export_for_template($output),
            'grouping' => $groupinglabel,
160
            'afterlink' => $mod->afterlink,
161
162
            'altcontent' => $mod->get_formatted_content(['overflowdiv' => true, 'noclean' => true]),
            'modavailability' => $availability->export_for_template($output),
163
            'url' => $mod->url,
164
            'activityinfo' => $activityinfodata,
165
            'textclasses' => $displayoptions['textclasses'],
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
        ];

        if (!empty($mod->indent)) {
            $data->indent = $mod->indent;
            if ($mod->indent > 15) {
                $data->hugeindent = true;
            }
        }

        if (!empty($data->cmname)) {
            $data->hasname = true;
        }
        if (!empty($data->url)) {
            $data->hasurl = true;
        }

        $returnsection = $format->get_section_number();
183

184
185
        if ($format->show_editor()) {
            // Edit actions.
186
187
188
189
190
191
192
193
            $controlmenu = new $this->controlmenuclass(
                $format,
                $this->section,
                $mod,
                $this->displayoptions
            );
            $data->controlmenu = $controlmenu->export_for_template($output);

194
195
196
197
            // Move and select options.
            $data->moveicon = course_get_cm_move($mod, $returnsection);
        }

198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        return $data;
    }

    /**
     * Returns the CSS classes for the activity name/content
     *
     * For items which are hidden, unavailable or stealth but should be displayed
     * to current user ($mod->is_visible_on_course_page()), we show those as dimmed.
     * Students will also see as dimmed activities names that are not yet available
     * but should still be displayed (without link) with availability info.
     */
    protected function load_classes() {

        $mod = $this->mod;

        $linkclasses = '';
        $textclasses = '';
        if ($mod->uservisible) {
            $info = new info_module($mod);
            $conditionalhidden = !$info->is_available_for_all();
            $accessiblebutdim = (!$mod->visible || $conditionalhidden) &&
                has_capability('moodle/course:viewhiddenactivities', $mod->context);
            if ($accessiblebutdim) {
                $linkclasses .= ' dimmed';
                $textclasses .= ' dimmed_text';
                if ($conditionalhidden) {
                    $linkclasses .= ' conditionalhidden';
                    $textclasses .= ' conditionalhidden';
                }
            }
            if ($mod->is_stealth()) {
                // Stealth activity is the one that is not visible on course page.
                // It still may be displayed to the users who can manage it.
                $linkclasses .= ' stealth';
                $textclasses .= ' stealth';
            }
        } else {
            $linkclasses .= ' dimmed';
            $textclasses .= ' dimmed dimmed_text';
237
        }
238
239
240
        $this->linkclasses = $linkclasses;
        $this->textclasses = $textclasses;
    }
241

242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    /**
     * Get the activity link classes.
     *
     * @return string the activity link classes.
     */
    public function get_link_classes(): string {
        return $this->linkclasses;
    }

    /**
     * Get the activity text/description classes.
     *
     * @return string the activity text classes.
     */
    public function get_text_classes(): string {
        return $this->textclasses;
258
259
    }
}