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_courseformat\base as course_format;
20
use completion_info;
21
use core_courseformat\sectiondelegate;
22
use renderer_base;
23
use section_info;
24
use cm_info;
25
use renderable;
26
use stdClass;
27
use core_availability\info_module;
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
require_once($CFG->libdir . '/completionlib.php');
32
 
33
/**
34
 * Contains the ajax update course module structure.
35
 *
36
 * @package   core_courseformat
37
 * @copyright 2021 Ferran Recio <ferran@moodle.com>
38
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class cm implements renderable {
41
    /**
42
     * Constructor.
43
     */
44
    public function __construct(
45
        /** @var course_format $format The course format. */
46
        protected course_format $format,
47
        /** @var section_info $section The section data. */
48
        protected section_info $section,
49
        /** @var cm_info $cm The course module data. */
50
        protected cm_info $cm,
51
        /** @var bool $exportcontent False if pre-rendered cmitem HTML content must be exported. */
52
        protected bool $exportcontent = false,
53
        /** @var ?bool $istrackeduser If is_tracked_user is pre-computed for this CM's course, it can be provided here. */
54
        protected ?bool $istrackeduser = null,
55
    ) {
56
    }
57
 
58
    /**
59
     * Export this data so it can be used as state object in the course editor.
60
     *
61
     * @param renderer_base $output typically, the renderer that's calling this function
62
     * @return stdClass data context for a mustache template
63
     */
64
    public function export_for_template(renderer_base $output): stdClass {
65
        global $CFG, $USER;
66
 
67
        $format = $this->format;
68
        $section = $this->section;
69
        $cm = $this->cm;
70
        $course = $format->get_course();
1441 ariadna 71
        $delegatedsectioninfo = $cm->get_delegated_section_info();
1 efrain 72
 
73
        $data = (object)[
74
            'id' => $cm->id,
75
            'anchor' => "module-{$cm->id}",
76
            'name' => \core_external\util::format_string($cm->name, $cm->context, true),
77
            'visible' => !empty($cm->visible),
78
            'stealth' => $cm->is_stealth(),
79
            'sectionid' => $section->id,
80
            'sectionnumber' => $section->section,
81
            'uservisible' => $cm->uservisible,
82
            'hascmrestrictions' => $this->get_has_restrictions(),
83
            'modname' => get_string('pluginname', 'mod_' . $cm->modname),
84
            'indent' => ($format->uses_indentation()) ? $cm->indent : 0,
85
            'groupmode' => $cm->groupmode,
86
            'module' => $cm->modname,
87
            'plugin' => 'mod_' . $cm->modname,
88
            // Activities with delegate section has some restriction to prevent structure loops.
1441 ariadna 89
            'hasdelegatedsection' => !empty($delegatedsectioninfo),
1 efrain 90
        ];
91
 
1441 ariadna 92
        if (!empty($delegatedsectioninfo)) {
93
            $data->delegatesectionid = $delegatedsectioninfo->id;
94
        }
95
 
1 efrain 96
        // Check the user access type to this cm.
97
        $info = new info_module($cm);
98
        $data->accessvisible = ($data->visible && $info->is_available_for_all());
99
 
100
        // Add url if the activity is compatible.
101
        $url = $cm->url;
102
        if ($url) {
103
            $data->url = $url->out();
104
        }
105
 
106
        if ($this->exportcontent) {
107
            $data->content = $output->course_section_updated_cm_item($format, $section, $cm);
108
        }
109
 
110
        // Completion status.
111
        $completioninfo = new completion_info($course);
112
        $data->istrackeduser = $this->istrackeduser ?? $completioninfo->is_tracked_user($USER->id);
113
        if ($data->istrackeduser && $completioninfo->is_enabled($cm)) {
114
            $completiondata = new \core_completion\cm_completion_details($completioninfo, $cm, $USER->id, false);
115
            $data->completionstate = $completiondata->get_overall_completion();
116
            $data->isoverallcomplete = $completiondata->is_overall_complete();
117
        }
118
 
119
        $data->allowstealth = !empty($CFG->allowstealth) && $format->allow_stealth_module_visibility($cm, $section);
120
 
121
        return $data;
122
    }
123
 
124
    /**
125
     * Return if the activity has a restrictions icon displayed or not.
126
     *
127
     * @return bool if the activity has visible restrictions for the user.
128
     */
129
    protected function get_has_restrictions(): bool {
130
        global $CFG;
131
        $cm = $this->cm;
132
 
133
        if (empty($cm->visible) || empty($CFG->enableavailability)) {
134
            return false;
135
        }
136
        // Nothing to be displayed to the user.
137
        if (!$cm->is_visible_on_course_page()) {
138
            return false;
139
        }
140
        // Not allowed to see the module but might be allowed to see some availability.
141
        if (!$cm->uservisible) {
142
            return !empty($cm->availableinfo);
143
        }
144
        // Content editors can see all restrictions if the activity is visible.
145
        if (has_capability('moodle/course:viewhiddenactivities', $cm->context)) {
146
            $ci = new info_module($cm);
147
            return !empty($ci->get_full_information());
148
        }
149
        // Regular users can only see restrictions if apply to them.
150
        return false;
151
    }
152
}