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
/**
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
    /**
79
     * Get the availability data to 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 array the availability data.
83
     */
84
    protected function get_info(\renderer_base $output): array {
85
        if (!$this->mod->is_visible_on_course_page()) {
86
            // Nothing to be displayed to the user.
87
            return [];
88
        }
89
 
90
        if (!$this->mod->uservisible) {
91
            return ['info' => $this->user_availability_info($output)];
92
        }
93
 
94
        $editurl = new \moodle_url(
95
            '/course/modedit.php',
96
            ['update' => $this->mod->id, 'showonly' => 'availabilityconditionsheader']
97
        );
98
        return ['editurl' => $editurl->out(false), 'info' => $this->conditional_availability_info($output)];
99
    }
100
 
101
    /**
102
     * Get the current user availability data.
103
     *
104
     * This is a student who is not allowed to see the module but might be allowed
105
     * to see availability info (i.e. "Available from ...").
106
     *
107
     * @param \renderer_base $output typically, the renderer that's calling this function
108
     * @return array the availability data.
109
     */
110
    protected function user_availability_info(\renderer_base $output): array {
111
        if (empty($this->mod->availableinfo)) {
112
            return [];
113
        }
114
 
115
        $info = [];
116
        $info[] = $this->get_availability_data($output, $this->mod->availableinfo, 'isrestricted');
117
        return $info;
118
    }
119
 
120
    /**
121
     * Get the activity availability data to display.
122
     *
123
     * @param \renderer_base $output typically, the renderer that's calling this function
124
     * @return array the availability data.
125
     */
126
    protected function conditional_availability_info(\renderer_base $output): array {
127
        global $CFG;
128
 
129
        // This is a teacher who is allowed to see module but still should see the
130
        // information that module is not available to all/some students.
131
        $mod  = $this->mod;
132
        $modcontext = $mod->context;
133
        $canviewhidden = has_capability('moodle/course:viewhiddenactivities', $modcontext);
134
        if (!$canviewhidden || empty($CFG->enableavailability)) {
135
            return [];
136
        }
137
 
138
        // Display information about conditional availability.
139
        // Don't add availability information if user is not editing and activity is hidden.
140
        if (!$mod->visible && !$this->format->show_editor()) {
141
            return [];
142
        }
143
 
144
        $ci = new info_module($mod);
145
        $fullinfo = $ci->get_full_information();
146
        if (!$fullinfo) {
147
            return [];
148
        }
149
 
150
        $info = [];
151
        $hidinfoclass = 'isrestricted isfullinfo';
152
        if (!$mod->visible) {
153
            $hidinfoclass .= ' hide';
154
        }
155
        $info[] = $this->get_availability_data($output, $fullinfo, $hidinfoclass);
156
 
157
        return $info;
158
    }
159
}