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_course\output;
18
 
19
use moodle_page;
20
use moodle_url;
21
 
22
/**
23
 * Class responsible for generating the action bar (tertiary nav) elements in the category management page
24
 *
25
 * @package    core
26
 * @copyright  2021 Peter Dias
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class manage_categories_action_bar implements \renderable {
30
    /** @var object $course The course we are dealing with. */
31
    protected $course;
32
    /** @var moodle_page $page The current page. */
33
    protected $page;
34
    /** @var string|null $viewmode The viewmode of the underlying page - Course and categories, categories or courses */
35
    protected $viewmode;
36
    /** @var string|null $heading The heading to display */
37
    protected $heading;
38
    /** @var string|null $searchvalue The search value if any */
39
    protected $searchvalue;
40
 
41
    /**
42
     * Constructor for the manage_categories_action_bar
43
     *
44
     * @param moodle_page $page The page object
45
     * @param string $viewmode The type of page we are viewing.
46
     * @param object|null $course The course that we are generating the nav for
47
     * @param string|null $searchvalue The search value if applicable
48
     */
49
    public function __construct(moodle_page $page, string $viewmode, ?object $course, ?string $searchvalue) {
50
        $this->course = $course;
51
        $this->page = $page;
52
        $this->viewmode = $viewmode;
53
        $this->searchvalue = $searchvalue;
54
        if ($searchvalue) {
55
            $this->heading = get_string('searchresults');
56
        }
57
    }
58
 
59
    /**
60
     * Gets the url_select to be displayed in the participants page if available.
61
     *
62
     * @param \renderer_base $output
63
     * @return object|null The content required to render the url_select
64
     */
65
    protected function get_dropdown(\renderer_base $output): ?object {
66
        // If a search is being performed then no need to display the dropdown.
67
        if ($this->searchvalue) {
68
            return null;
69
        }
70
 
71
        $modes = \core_course\management\helper::get_management_viewmodes();
72
        $activeurl = null;
73
        $content = [];
74
        foreach ($modes as $mode => $description) {
75
            $url = new moodle_url($this->page->url, ['view' => $mode]);
76
            $content[$url->out()] = $description;
77
            if ($this->viewmode == $mode) {
78
                $activeurl = $url->out();
79
                $this->heading = get_string("manage$mode");
80
            }
81
        }
82
 
83
        // Default to the first option if asking for default. This is combined.
84
        if (!$activeurl && $this->viewmode === 'default') {
85
            $activeurl = array_key_first($content);
86
            $this->heading = get_string("managecombined");
87
        }
88
 
89
        if ($content) {
90
            $urlselect = new \url_select($content, $activeurl, null);
91
            $urlselect->set_label(get_string('viewing'), ['class' => 'sr-only']);
92
            return $urlselect->export_for_template($output);
93
        }
94
 
95
        return null;
96
    }
97
 
98
    /**
99
     * Gets the url_select to be displayed in the participants page if available.
100
     *
101
     * @param \renderer_base $output
102
     * @return object|null The content required to render the url_select
103
     */
104
    protected function get_category_select(\renderer_base $output): ?object {
105
        if (!$this->searchvalue && $this->viewmode === 'courses') {
106
            $categories = \core_course_category::make_categories_list(array('moodle/category:manage', 'moodle/course:create'));
107
            if (!$categories) {
108
                return null;
109
            }
110
            $currentcat = $this->page->url->param('categoryid');
111
            foreach ($categories as $id => $cat) {
112
                $url = new moodle_url($this->page->url, ['categoryid' => $id]);
113
                if ($id == $currentcat) {
114
                    $currenturl = $url->out();
115
                }
116
                $options[$url->out()] = $cat;
117
            }
118
 
119
            $select = new \url_select($options, $currenturl);
120
            $select->set_label(get_string('category'), ['class' => 'sr-only']);
121
            $select->class .= ' text-truncate w-100';
122
            return $select->export_for_template($output);
123
        }
124
 
125
        return null;
126
    }
127
 
128
    /**
129
     * Get the search box
130
     *
131
     * @return array
132
     */
133
    protected function get_search_form(): array {
134
        $searchform = [
135
            'btnclass' => 'btn-primary',
136
            'inputname' => 'search',
137
            'searchstring' => get_string('searchcourses'),
138
            'query' => $this->searchvalue
139
        ];
140
        if (\core_course_category::has_capability_on_any(['moodle/category:manage', 'moodle/course:create'])) {
141
            $searchform['action'] = new moodle_url('/course/management.php');
142
        } else {
143
            $searchform['action'] = new moodle_url('/course/search.php');
144
        }
145
        return $searchform;
146
    }
147
 
148
    /**
149
     * Export the content to be displayed on the participants page.
150
     *
151
     * @param \renderer_base $output
152
     * @return array Consists of the following:
153
     *              - urlselect A stdclass representing the standard navigation options to be fed into a urlselect
154
     *              - renderedcontent Rendered content to be displayed in line with the tertiary nav
155
     */
156
    public function export_for_template(\renderer_base $output): array {
157
        return [
158
            'urlselect' => $this->get_dropdown($output),
159
            'categoryselect' => $this->get_category_select($output),
160
            'search' => $this->get_search_form(),
161
            'heading' => $this->heading,
162
        ];
163
    }
164
}