Proyectos de Subversion Moodle

Rev

| 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();
71
 
72
        $data = (object)[
73
            'id' => $cm->id,
74
            'anchor' => "module-{$cm->id}",
75
            'name' => \core_external\util::format_string($cm->name, $cm->context, true),
76
            'visible' => !empty($cm->visible),
77
            'stealth' => $cm->is_stealth(),
78
            'sectionid' => $section->id,
79
            'sectionnumber' => $section->section,
80
            'uservisible' => $cm->uservisible,
81
            'hascmrestrictions' => $this->get_has_restrictions(),
82
            'modname' => get_string('pluginname', 'mod_' . $cm->modname),
83
            'indent' => ($format->uses_indentation()) ? $cm->indent : 0,
84
            'groupmode' => $cm->groupmode,
85
            'module' => $cm->modname,
86
            'plugin' => 'mod_' . $cm->modname,
87
            // Activities with delegate section has some restriction to prevent structure loops.
88
            'delegatesection' => sectiondelegate::has_delegate_class('mod_'.$cm->modname),
89
        ];
90
 
91
        // Check the user access type to this cm.
92
        $info = new info_module($cm);
93
        $data->accessvisible = ($data->visible && $info->is_available_for_all());
94
 
95
        // Add url if the activity is compatible.
96
        $url = $cm->url;
97
        if ($url) {
98
            $data->url = $url->out();
99
        }
100
 
101
        if ($this->exportcontent) {
102
            $data->content = $output->course_section_updated_cm_item($format, $section, $cm);
103
        }
104
 
105
        // Completion status.
106
        $completioninfo = new completion_info($course);
107
        $data->istrackeduser = $this->istrackeduser ?? $completioninfo->is_tracked_user($USER->id);
108
        if ($data->istrackeduser && $completioninfo->is_enabled($cm)) {
109
            $completiondata = new \core_completion\cm_completion_details($completioninfo, $cm, $USER->id, false);
110
            $data->completionstate = $completiondata->get_overall_completion();
111
            $data->isoverallcomplete = $completiondata->is_overall_complete();
112
        }
113
 
114
        $data->allowstealth = !empty($CFG->allowstealth) && $format->allow_stealth_module_visibility($cm, $section);
115
 
116
        return $data;
117
    }
118
 
119
    /**
120
     * Return if the activity has a restrictions icon displayed or not.
121
     *
122
     * @return bool if the activity has visible restrictions for the user.
123
     */
124
    protected function get_has_restrictions(): bool {
125
        global $CFG;
126
        $cm = $this->cm;
127
 
128
        if (empty($cm->visible) || empty($CFG->enableavailability)) {
129
            return false;
130
        }
131
        // Nothing to be displayed to the user.
132
        if (!$cm->is_visible_on_course_page()) {
133
            return false;
134
        }
135
        // Not allowed to see the module but might be allowed to see some availability.
136
        if (!$cm->uservisible) {
137
            return !empty($cm->availableinfo);
138
        }
139
        // Content editors can see all restrictions if the activity is visible.
140
        if (has_capability('moodle/course:viewhiddenactivities', $cm->context)) {
141
            $ci = new info_module($cm);
142
            return !empty($ci->get_full_information());
143
        }
144
        // Regular users can only see restrictions if apply to them.
145
        return false;
146
    }
147
}