| 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 |   | 
        
           | 1441 | ariadna | 19 | use action_menu;
 | 
        
           |  |  | 20 | use action_menu_link_secondary;
 | 
        
           | 1 | efrain | 21 | use context_coursecat;
 | 
        
           |  |  | 22 | use core_course_category;
 | 
        
           |  |  | 23 | use course_request;
 | 
        
           |  |  | 24 | use moodle_page;
 | 
        
           |  |  | 25 | use moodle_url;
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | /**
 | 
        
           |  |  | 28 |  * Class responsible for generating the action bar (tertiary nav) elements in an individual category page
 | 
        
           |  |  | 29 |  *
 | 
        
           |  |  | 30 |  * @package    core
 | 
        
           |  |  | 31 |  * @copyright  2021 Peter Dias
 | 
        
           |  |  | 32 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 33 |  */
 | 
        
           |  |  | 34 | class category_action_bar extends manage_categories_action_bar {
 | 
        
           |  |  | 35 |     /**
 | 
        
           |  |  | 36 |      * @var object The current category we are referring to.
 | 
        
           |  |  | 37 |      */
 | 
        
           |  |  | 38 |     protected $category;
 | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      * Constructor category_action_bar
 | 
        
           |  |  | 41 |      *
 | 
        
           |  |  | 42 |      * @param moodle_page $page The page object
 | 
        
           |  |  | 43 |      * @param object $category
 | 
        
           |  |  | 44 |      * @param object|null $course The course that we are generating the nav for
 | 
        
           |  |  | 45 |      * @param string|null $searchvalue
 | 
        
           |  |  | 46 |      */
 | 
        
           |  |  | 47 |     public function __construct(moodle_page $page, object $category, ?object $course = null, ?string $searchvalue = null) {
 | 
        
           |  |  | 48 |         $this->category = $category;
 | 
        
           |  |  | 49 |         parent::__construct($page, 'courses', $course, $searchvalue);
 | 
        
           |  |  | 50 |     }
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |     /**
 | 
        
           |  |  | 53 |      * Gets the url_select to be displayed in the participants page if available.
 | 
        
           |  |  | 54 |      *
 | 
        
           |  |  | 55 |      * @param \renderer_base $output
 | 
        
           |  |  | 56 |      * @return object|null The content required to render the url_select
 | 
        
           |  |  | 57 |      */
 | 
        
           |  |  | 58 |     protected function get_category_select(\renderer_base $output): ?object {
 | 
        
           |  |  | 59 |         if (!$this->searchvalue && !core_course_category::is_simple_site()) {
 | 
        
           |  |  | 60 |             $categories = core_course_category::make_categories_list();
 | 
        
           |  |  | 61 |             if (count($categories) > 1) {
 | 
        
           |  |  | 62 |                 foreach ($categories as $id => $cat) {
 | 
        
           |  |  | 63 |                     $url = new moodle_url($this->page->url, ['categoryid' => $id]);
 | 
        
           |  |  | 64 |                     $options[$url->out()] = $cat;
 | 
        
           |  |  | 65 |                 }
 | 
        
           |  |  | 66 |                 $currenturl = new moodle_url($this->page->url, ['categoryid' => $this->category->id]);
 | 
        
           |  |  | 67 |                 $select = new \url_select($options, $currenturl, null);
 | 
        
           | 1441 | ariadna | 68 |                 $select->set_label(get_string('categories'), ['class' => 'visually-hidden']);
 | 
        
           | 1 | efrain | 69 |                 $select->class .= ' text-truncate w-100';
 | 
        
           |  |  | 70 |                 return $select->export_for_template($output);
 | 
        
           |  |  | 71 |             }
 | 
        
           |  |  | 72 |         }
 | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 |         return null;
 | 
        
           |  |  | 75 |     }
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |     /**
 | 
        
           |  |  | 78 |      * Gets the additional options to be displayed within a 'More' dropdown in the tertiary navigation.
 | 
        
           |  |  | 79 |      * The predefined order defined by UX is:
 | 
        
           |  |  | 80 |      *  - Add a course
 | 
        
           |  |  | 81 |      *  - Add a sub cat
 | 
        
           |  |  | 82 |      *  - Manage course
 | 
        
           |  |  | 83 |      *  - Request a course
 | 
        
           |  |  | 84 |      *  - Course pending approval
 | 
        
           |  |  | 85 |      *
 | 
        
           |  |  | 86 |      * @return array
 | 
        
           |  |  | 87 |      */
 | 
        
           |  |  | 88 |     protected function get_additional_category_options(): array {
 | 
        
           |  |  | 89 |         global $CFG, $DB;
 | 
        
           |  |  | 90 |         if ($this->category->is_uservisible()) {
 | 
        
           |  |  | 91 |             $context = get_category_or_system_context($this->category->id);
 | 
        
           |  |  | 92 |             if (has_capability('moodle/course:create', $context)) {
 | 
        
           |  |  | 93 |                 $params = [
 | 
        
           |  |  | 94 |                     'category' => $this->category->id ?: $CFG->defaultrequestcategory,
 | 
        
           |  |  | 95 |                     'returnto' => $this->category->id ? 'category' : 'topcat'
 | 
        
           |  |  | 96 |                 ];
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |                 $options[0] = [
 | 
        
           |  |  | 99 |                     'url' => new moodle_url('/course/edit.php', $params),
 | 
        
           |  |  | 100 |                     'string' => get_string('addnewcourse')
 | 
        
           |  |  | 101 |                 ];
 | 
        
           |  |  | 102 |             }
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 |             if (!empty($CFG->enablecourserequests)) {
 | 
        
           |  |  | 105 |                 // Display an option to request a new course.
 | 
        
           |  |  | 106 |                 if (course_request::can_request($context)) {
 | 
        
           |  |  | 107 |                     $params = [];
 | 
        
           |  |  | 108 |                     if ($context instanceof context_coursecat) {
 | 
        
           |  |  | 109 |                         $params['category'] = $context->instanceid;
 | 
        
           |  |  | 110 |                     }
 | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 |                     $options[3] = [
 | 
        
           |  |  | 113 |                         'url' => new moodle_url('/course/request.php', $params),
 | 
        
           |  |  | 114 |                         'string' => get_string('requestcourse')
 | 
        
           |  |  | 115 |                     ];
 | 
        
           |  |  | 116 |                 }
 | 
        
           |  |  | 117 |   | 
        
           |  |  | 118 |                 // Display the manage pending requests option.
 | 
        
           |  |  | 119 |                 if (has_capability('moodle/site:approvecourse', $context)) {
 | 
        
           |  |  | 120 |                     $disabled = !$DB->record_exists('course_request', array());
 | 
        
           |  |  | 121 |                     if (!$disabled) {
 | 
        
           |  |  | 122 |                         $options[4] = [
 | 
        
           |  |  | 123 |                             'url' => new moodle_url('/course/pending.php'),
 | 
        
           |  |  | 124 |                             'string' => get_string('coursespending')
 | 
        
           |  |  | 125 |                         ];
 | 
        
           |  |  | 126 |                     }
 | 
        
           |  |  | 127 |                 }
 | 
        
           |  |  | 128 |             }
 | 
        
           |  |  | 129 |         }
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 |         if ($this->category->can_create_course() || $this->category->has_manage_capability()) {
 | 
        
           |  |  | 132 |             // Add 'Manage' button if user has permissions to edit this category.
 | 
        
           |  |  | 133 |             $options[2] = [
 | 
        
           |  |  | 134 |                 'url' => new moodle_url('/course/management.php', ['categoryid' => $this->category->id]),
 | 
        
           |  |  | 135 |                 'string' => get_string('managecourses')
 | 
        
           |  |  | 136 |             ];
 | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 |             if ($this->category->has_manage_capability()) {
 | 
        
           |  |  | 139 |                 $addsubcaturl = new moodle_url('/course/editcategory.php', array('parent' => $this->category->id));
 | 
        
           |  |  | 140 |                 $options[1] = [
 | 
        
           |  |  | 141 |                     'url' => $addsubcaturl,
 | 
        
           |  |  | 142 |                     'string' => get_string('addsubcategory')
 | 
        
           |  |  | 143 |                 ];
 | 
        
           |  |  | 144 |             }
 | 
        
           |  |  | 145 |         }
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |         // We have stored the options in a predefined order. Sort it based on index and return.
 | 
        
           |  |  | 148 |         if (isset($options)) {
 | 
        
           |  |  | 149 |             sort($options);
 | 
        
           |  |  | 150 |             return ['options' => $options];
 | 
        
           |  |  | 151 |         }
 | 
        
           |  |  | 152 |   | 
        
           |  |  | 153 |         return [];
 | 
        
           |  |  | 154 |     }
 | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 |     /**
 | 
        
           |  |  | 157 |      * Export the content to be displayed on the category page.
 | 
        
           |  |  | 158 |      *
 | 
        
           |  |  | 159 |      * @param \renderer_base $output
 | 
        
           |  |  | 160 |      * @return array Consists of the following:
 | 
        
           |  |  | 161 |      *              - categoryselect A list of available categories to be fed into a urlselect
 | 
        
           |  |  | 162 |      *              - search The course search form
 | 
        
           |  |  | 163 |      *              - additionaloptions Additional actions that can be performed in a category
 | 
        
           |  |  | 164 |      */
 | 
        
           |  |  | 165 |     public function export_for_template(\renderer_base $output): array {
 | 
        
           | 1441 | ariadna | 166 |         $additionaloptions = $this->get_additional_category_options();
 | 
        
           |  |  | 167 |         // Generate the action menu if there are additional options.
 | 
        
           |  |  | 168 |         if (!empty($additionaloptions)) {
 | 
        
           |  |  | 169 |             $actionmenu = new action_menu();
 | 
        
           |  |  | 170 |             $actionmenu->set_kebab_trigger(get_string('moreactions'));
 | 
        
           |  |  | 171 |             $actionmenu->set_additional_classes('ms-auto');
 | 
        
           |  |  | 172 |             foreach ($additionaloptions['options'] as $option) {
 | 
        
           |  |  | 173 |                 $actionmenu->add(new action_menu_link_secondary(
 | 
        
           |  |  | 174 |                     $option['url'],
 | 
        
           |  |  | 175 |                     null,
 | 
        
           |  |  | 176 |                     $option['string']
 | 
        
           |  |  | 177 |                 ));
 | 
        
           |  |  | 178 |             }
 | 
        
           |  |  | 179 |             $actionmenucontent = $output->render($actionmenu);
 | 
        
           |  |  | 180 |         }
 | 
        
           |  |  | 181 |   | 
        
           | 1 | efrain | 182 |         return [
 | 
        
           |  |  | 183 |             'categoryselect' => $this->get_category_select($output),
 | 
        
           |  |  | 184 |             'search' => $this->get_search_form(),
 | 
        
           | 1441 | ariadna | 185 |             'additionaloptions' => $actionmenucontent ?? '',
 | 
        
           | 1 | efrain | 186 |         ];
 | 
        
           |  |  | 187 |     }
 | 
        
           |  |  | 188 |   | 
        
           |  |  | 189 | }
 |