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
namespace core_courseformat\output\local\state;
18
 
19
use core_availability\info_section;
20
use core_courseformat\base as course_format;
21
use section_info;
22
use renderable;
23
use stdClass;
24
use context_course;
25
 
26
/**
27
 * Contains the ajax update section structure.
28
 *
29
 * @package   core_course
30
 * @copyright 2021 Ferran Recio <ferran@moodle.com>
31
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class section implements renderable {
34
 
35
    /** @var course_format the course format class */
36
    protected $format;
37
 
38
    /** @var section_info the course section class */
39
    protected $section;
40
 
41
    /**
42
     * Constructor.
43
     *
44
     * @param course_format $format the course format
45
     * @param section_info $section the section info
46
     */
47
    public function __construct(course_format $format, section_info $section) {
48
        $this->format = $format;
49
        $this->section = $section;
50
    }
51
 
52
    /**
53
     * Export this data so it can be used as state object in the course editor.
54
     *
55
     * @param \renderer_base $output typically, the renderer that's calling this function
56
     * @return array data context for a mustache template
57
     */
58
    public function export_for_template(\renderer_base $output): stdClass {
59
        $format = $this->format;
60
        $course = $format->get_course();
61
        $section = $this->section;
62
        $modinfo = $format->get_modinfo();
63
 
64
        $indexcollapsed = false;
65
        $contentcollapsed = false;
66
        $preferences = $format->get_sections_preferences();
67
        if (isset($preferences[$section->id])) {
68
            $sectionpreferences = $preferences[$section->id];
69
            if (!empty($sectionpreferences->contentcollapsed)) {
70
                $contentcollapsed = true;
71
            }
72
            if (!empty($sectionpreferences->indexcollapsed)) {
73
                $indexcollapsed = true;
74
            }
75
        }
76
 
77
        $data = (object)[
78
            'id' => $section->id,
79
            'section' => $section->section,
80
            'number' => $section->section,
81
            'title' => $format->get_section_name($section),
82
            'hassummary' => !empty($section->summary),
83
            'rawtitle' => $section->name,
84
            'cmlist' => [],
85
            'visible' => !empty($section->visible),
1441 ariadna 86
            'sectionurl' => course_get_url($course, $section->section, ['navigation' => true])->out(false),
1 efrain 87
            'current' => $format->is_section_current($section),
88
            'indexcollapsed' => $indexcollapsed,
89
            'contentcollapsed' => $contentcollapsed,
90
            'hasrestrictions' => $this->get_has_restrictions(),
91
            'bulkeditable' => $this->is_bulk_editable(),
92
            'component' => $section->component,
93
            'itemid' => $section->itemid,
1441 ariadna 94
            'parentsectionid' => $section->get_component_instance()?->get_parent_section()?->id,
1 efrain 95
        ];
96
 
97
        if (empty($modinfo->sections[$section->section])) {
98
            return $data;
99
        }
100
 
101
        foreach ($modinfo->sections[$section->section] as $modnumber) {
102
            $mod = $modinfo->cms[$modnumber];
1441 ariadna 103
            if ($section->uservisible && $mod->is_visible_on_course_page() && $mod->is_of_type_that_can_display()) {
1 efrain 104
                $data->cmlist[] = $mod->id;
105
            }
106
        }
107
 
108
        return $data;
109
    }
110
 
111
    /**
112
     * Return if the section can be selected for bulk editing.
113
     * @return bool if the section can be edited in bulk
114
     */
115
    protected function is_bulk_editable(): bool {
116
        $section = $this->section;
117
        return ($section->section != 0);
118
    }
119
 
120
    /**
121
     * Return if the section has a restrictions icon displayed or not.
122
     *
123
     * @return bool if the section has visible restrictions for the user.
124
     */
125
    protected function get_has_restrictions(): bool {
126
        global $CFG;
127
 
128
        $section = $this->section;
129
        $course = $this->format->get_course();
130
        $context = context_course::instance($course->id);
131
 
132
        // Hidden sections have no restriction indicator displayed.
133
        if (empty($section->visible) || empty($CFG->enableavailability)) {
134
            return false;
135
        }
136
        // The activity is not visible to the user but it may have some availability information.
137
        if (!$section->uservisible) {
138
            return !empty($section->availableinfo);
139
        }
140
        // Course editors can see all restrictions if the section is visible.
141
        if (has_capability('moodle/course:viewhiddensections', $context)) {
142
            $ci = new info_section($section);
143
            return !empty($ci->get_full_information());
144
        }
145
        // Regular users can only see restrictions if apply to them.
146
        return false;
147
    }
148
}