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 activity availability information.
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\cm;
26
 
27
use core_courseformat\output\local\content\section\availability as section_avalability;
28
use cm_info;
29
use core_courseformat\base as course_format;
30
use section_info;
31
use stdClass;
32
use core_availability\info_module;
33
use core_availability\info;
34
 
35
/**
36
 * Base class to render a course module availability inside a course format.
37
 *
38
 * @package   core_courseformat
39
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
40
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class availability extends section_avalability {
43
 
44
    /** @var course_format the course format */
45
    protected $format;
46
 
47
    /** @var section_info the section object */
48
    protected $section;
49
 
50
    /** @var cm_info the course module instance */
51
    protected $mod;
52
 
53
    /** @var array optional display options */
54
    protected $displayoptions;
55
 
56
    /** @var bool the has availability attribute name */
57
    protected $hasavailabilityname;
58
 
59
    /** @var stdClass|null the instance export data */
60
    protected $data = null;
61
 
62
    /**
63
     * Constructor.
64
     *
65
     * @param course_format $format the course format
66
     * @param section_info $section the section info
67
     * @param cm_info $mod the course module ionfo
68
     * @param array $displayoptions optional extra display options
69
     */
70
    public function __construct(course_format $format, section_info $section, cm_info $mod, array $displayoptions = []) {
71
        $this->format = $format;
72
        $this->section = $section;
73
        $this->mod = $mod;
74
        $this->displayoptions = $displayoptions;
75
        $this->hasavailabilityname = 'hasmodavailability';
76
    }
77
 
78
    /**
1441 ariadna 79
     * Export this data so it can be used as the context for a mustache template.
80
     *
81
     * @param \renderer_base $output typically, the renderer that's calling this function
82
     * @return stdClass|null data context for a mustache template
83
     */
84
    public function export_for_template(\renderer_base $output): ?stdClass {
85
        if (!$this->format->show_activity_editor_options($this->mod)) {
86
            return null;
87
        }
88
        return parent::export_for_template($output);
89
    }
90
 
91
    /**
1 efrain 92
     * Get the availability data to be used as the context for a mustache template.
93
     *
94
     * @param \renderer_base $output typically, the renderer that's calling this function
95
     * @return array the availability data.
96
     */
97
    protected function get_info(\renderer_base $output): array {
98
        if (!$this->mod->is_visible_on_course_page()) {
99
            // Nothing to be displayed to the user.
100
            return [];
101
        }
102
 
103
        if (!$this->mod->uservisible) {
104
            return ['info' => $this->user_availability_info($output)];
105
        }
106
 
107
        $editurl = new \moodle_url(
108
            '/course/modedit.php',
109
            ['update' => $this->mod->id, 'showonly' => 'availabilityconditionsheader']
110
        );
111
        return ['editurl' => $editurl->out(false), 'info' => $this->conditional_availability_info($output)];
112
    }
113
 
114
    /**
115
     * Get the current user availability data.
116
     *
117
     * This is a student who is not allowed to see the module but might be allowed
118
     * to see availability info (i.e. "Available from ...").
119
     *
120
     * @param \renderer_base $output typically, the renderer that's calling this function
121
     * @return array the availability data.
122
     */
123
    protected function user_availability_info(\renderer_base $output): array {
124
        if (empty($this->mod->availableinfo)) {
125
            return [];
126
        }
127
 
128
        $info = [];
129
        $info[] = $this->get_availability_data($output, $this->mod->availableinfo, 'isrestricted');
130
        return $info;
131
    }
132
 
133
    /**
134
     * Get the activity availability data to display.
135
     *
136
     * @param \renderer_base $output typically, the renderer that's calling this function
137
     * @return array the availability data.
138
     */
139
    protected function conditional_availability_info(\renderer_base $output): array {
140
        global $CFG;
141
 
142
        // This is a teacher who is allowed to see module but still should see the
143
        // information that module is not available to all/some students.
144
        $mod  = $this->mod;
145
        $modcontext = $mod->context;
146
        $canviewhidden = has_capability('moodle/course:viewhiddenactivities', $modcontext);
147
        if (!$canviewhidden || empty($CFG->enableavailability)) {
148
            return [];
149
        }
150
 
151
        // Display information about conditional availability.
152
        // Don't add availability information if user is not editing and activity is hidden.
153
        if (!$mod->visible && !$this->format->show_editor()) {
154
            return [];
155
        }
156
 
157
        $ci = new info_module($mod);
158
        $fullinfo = $ci->get_full_information();
159
        if (!$fullinfo) {
160
            return [];
161
        }
162
 
163
        $info = [];
164
        $hidinfoclass = 'isrestricted isfullinfo';
165
        if (!$mod->visible) {
166
            $hidinfoclass .= ' hide';
167
        }
168
        $info[] = $this->get_availability_data($output, $fullinfo, $hidinfoclass);
169
 
170
        return $info;
171
    }
172
}