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 |
namespace core_courseformat\output\local\content\section;
|
|
|
17 |
|
|
|
18 |
use context_course;
|
|
|
19 |
use core\output\choicelist;
|
|
|
20 |
use core\output\local\dropdown\status;
|
|
|
21 |
use core\output\named_templatable;
|
|
|
22 |
use core_courseformat\base as course_format;
|
|
|
23 |
use core_courseformat\output\local\courseformat_named_templatable;
|
|
|
24 |
use pix_icon;
|
|
|
25 |
use renderable;
|
|
|
26 |
use section_info;
|
|
|
27 |
use stdClass;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Base class to render a section visibility inside a course format.
|
|
|
31 |
*
|
|
|
32 |
* @package core_courseformat
|
|
|
33 |
* @copyright 2024 Laurent David <laurent.david@moodle.com>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class visibility implements named_templatable, renderable {
|
|
|
37 |
use courseformat_named_templatable;
|
|
|
38 |
|
|
|
39 |
/** @var course_format the course format */
|
|
|
40 |
protected $format;
|
|
|
41 |
|
|
|
42 |
/** @var section_info the section object */
|
|
|
43 |
protected $section;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Constructor.
|
|
|
47 |
* @param course_format $format the course format
|
|
|
48 |
* @param section_info $section the section info
|
|
|
49 |
*/
|
|
|
50 |
public function __construct(course_format $format, section_info $section) {
|
|
|
51 |
$this->format = $format;
|
|
|
52 |
$this->section = $section;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Export this data so it can be used as the context for a mustache template.
|
|
|
57 |
*
|
|
|
58 |
* @param \renderer_base $output typically, the renderer that's calling this function
|
|
|
59 |
* @return stdClass|null data context for a mustache template
|
|
|
60 |
*/
|
|
|
61 |
public function export_for_template(\renderer_base $output): ?stdClass {
|
|
|
62 |
global $USER;
|
|
|
63 |
$context = context_course::instance($this->section->course);
|
|
|
64 |
$data = new stdClass();
|
|
|
65 |
$data->editing = $this->format->show_editor();
|
|
|
66 |
if (!$this->section->visible) {
|
|
|
67 |
$data->notavailable = true;
|
|
|
68 |
if (has_capability('moodle/course:sectionvisibility', $context, $USER)) {
|
|
|
69 |
$data->hiddenfromstudents = true;
|
|
|
70 |
$data->notavailable = false;
|
|
|
71 |
$badgetext = $output->sr_text(get_string('availability'));
|
|
|
72 |
$badgetext .= get_string("hiddenfromstudents");
|
|
|
73 |
$icon = $this->get_icon('hide');
|
|
|
74 |
$choice = new choicelist();
|
|
|
75 |
$choice->add_option(
|
|
|
76 |
'show',
|
|
|
77 |
get_string("availability_show", 'core_courseformat'),
|
|
|
78 |
$this->get_option_data('show', 'sectionShow')
|
|
|
79 |
);
|
|
|
80 |
$choice->add_option(
|
|
|
81 |
'hide',
|
|
|
82 |
get_string('availability_hide', 'core_courseformat'),
|
|
|
83 |
$this->get_option_data('hide', 'sectionHide')
|
|
|
84 |
);
|
|
|
85 |
$choice->set_selected_value('hide');
|
|
|
86 |
$dropdown = new status(
|
|
|
87 |
$output->render($icon) . ' ' . $badgetext,
|
|
|
88 |
$choice,
|
|
|
89 |
['dialogwidth' => status::WIDTH['big']],
|
|
|
90 |
);
|
|
|
91 |
$data->dropwdown = $dropdown->export_for_template($output);
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
return $data;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Get the data for the option.
|
|
|
99 |
*
|
|
|
100 |
* @param string $name the name of the option
|
|
|
101 |
* @param string $action the state action of the option
|
|
|
102 |
* @return array
|
|
|
103 |
*/
|
|
|
104 |
private function get_option_data(string $name, string $action): array {
|
|
|
105 |
$baseurl = course_get_url($this->section->course, $this->section);
|
|
|
106 |
$baseurl->param('sesskey', sesskey());
|
|
|
107 |
$baseurl->param($name, $this->section->section);
|
|
|
108 |
|
|
|
109 |
// The section page is not yet fully reactive and it needs to use the old non-ajax links.
|
|
|
110 |
$pagesectionid = $this->format->get_sectionid();
|
|
|
111 |
if ($this->section->id == $pagesectionid) {
|
|
|
112 |
$baseurl->param('sectionid', $pagesectionid);
|
|
|
113 |
$action = '';
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return [
|
|
|
117 |
'description' => get_string("availability_{$name}_help", 'core_courseformat'),
|
|
|
118 |
'icon' => $this->get_icon($name),
|
|
|
119 |
// Non-ajax behat is not smart enough to discrimante hidden links
|
|
|
120 |
// so we need to keep providing the non-ajax links.
|
|
|
121 |
'url' => $baseurl,
|
|
|
122 |
'extras' => [
|
|
|
123 |
'data-id' => $this->section->id,
|
|
|
124 |
'data-action' => $action,
|
|
|
125 |
],
|
|
|
126 |
];
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Get the icon for the section visibility.
|
|
|
131 |
* @param string $selected the visibility selected value
|
|
|
132 |
* @return pix_icon
|
|
|
133 |
*/
|
|
|
134 |
protected function get_icon(string $selected): pix_icon {
|
|
|
135 |
if ($selected === 'hide') {
|
|
|
136 |
return new pix_icon('t/show', '');
|
|
|
137 |
} else {
|
|
|
138 |
return new pix_icon('t/hide', '');
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
}
|