Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
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\output; 18 19 use context_course; 20 use moodle_page; 21 use navigation_node; 22 use moodle_url; 23 24 /** 25 * Class responsible for generating the action bar (tertiary nav) elements in the participants page and related pages. 26 * 27 * @package core 28 * @copyright 2021 Peter Dias 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 class participants_action_bar implements \renderable { 32 /** @var object $course The course we are dealing with. */ 33 private $course; 34 /** @var moodle_page $page The current page. */ 35 private $page; 36 /** @var navigation_node $node The settings node for the participants page. */ 37 private $node; 38 /** @var string|null $renderedcontent Rendered buttons to be displayed in-line with the select box */ 39 private $renderedcontent; 40 41 /** 42 * Constructor participants_action_bar 43 * @param object $course The course that we are generating the nav for 44 * @param moodle_page $page The page object 45 * @param string|null $renderedcontent Any additional rendered content/actions to be displayed in line with the nav 46 */ 47 public function __construct(object $course, moodle_page $page, ?string $renderedcontent) { 48 $this->course = $course; 49 $this->page = $page; 50 $node = 'users'; 51 if ($this->page->context->contextlevel == CONTEXT_MODULE) { 52 $node = 'modulesettings'; 53 } else if ($this->page->context->contextlevel == CONTEXT_COURSECAT) { 54 $node = 'categorysettings'; 55 } 56 57 $this->node = $this->page->settingsnav->find($node, null); 58 $this->renderedcontent = $renderedcontent; 59 } 60 61 /** 62 * Return the nodes required to be displayed in the url_select box. 63 * The nodes are divided into 3 sections each with the heading as the key. 64 * 65 * @return array 66 */ 67 protected function get_ordered_nodes(): array { 68 return [ 69 'enrolments:enrol' => [ 70 'review', 71 'manageinstances', 72 'renameroles', 73 ], 74 'groups:group' => [ 75 'groups' 76 ], 77 'permissions:role' => [ 78 'override', 79 'roles', 80 'otherusers', 81 'permissions', 82 'roleoverride', 83 'rolecheck', 84 'roleassign', 85 ], 86 ]; 87 } 88 89 /** 90 * Get the content for the url_select select box. 91 * 92 * @return array 93 */ 94 protected function get_content_for_select(): array { 95 if (!$this->node) { 96 return []; 97 } 98 99 $formattedcontent = []; 100 $enrolmentsheading = get_string('enrolments', 'enrol'); 101 if ($this->page->context->contextlevel != CONTEXT_MODULE && 102 $this->page->context->contextlevel != CONTEXT_COURSECAT) { 103 // Pre-populate the formatted tertiary nav items with the "Enrolled users" node if user can view the participants page. 104 $coursecontext = context_course::instance($this->course->id); 105 $canviewparticipants = course_can_view_participants($coursecontext); 106 if ($canviewparticipants) { 107 $participantsurl = (new moodle_url('/user/index.php', ['id' => $this->course->id]))->out(); 108 $formattedcontent[] = [ 109 $enrolmentsheading => [ 110 $participantsurl => get_string('enrolledusers', 'enrol'), 111 ] 112 ]; 113 } 114 } 115 116 $nodes = $this->get_ordered_nodes(); 117 foreach ($nodes as $description => $content) { 118 list($stringid, $location) = explode(':', $description); 119 $heading = get_string($stringid, $location); 120 $items = []; 121 foreach ($content as $key) { 122 if ($node = $this->node->find($key, null)) { 123 if ($node->has_action()) { 124 $items[$node->action()->out()] = $node->text; 125 } 126 127 // Additional items to be added. 128 if ($key === 'groups') { 129 $params = ['id' => $this->course->id]; 130 $items += [ 131 (new moodle_url('/group/groupings.php', $params))->out() => get_string('groupings', 'group'), 132 (new moodle_url('/group/overview.php', $params))->out() => get_string('overview', 'group') 133 ]; 134 } 135 } 136 } 137 if ($items) { 138 if ($heading === $enrolmentsheading) { 139 // Merge the contents of the "Enrolments" group with the ones from the course settings nav. 140 $formattedcontent[0][$heading] = array_merge($formattedcontent[0][$heading], $items); 141 } else { 142 $formattedcontent[][$heading] = $items; 143 } 144 } 145 } 146 147 // If we are accessing a page from a module/category context additional nodes will not be visible. 148 if ($this->page->context->contextlevel != CONTEXT_MODULE && 149 $this->page->context->contextlevel != CONTEXT_COURSECAT) { 150 // Need to do some funky code here to find out if we have added third party navigation nodes. 151 $thirdpartynodearray = $this->get_thirdparty_node_array() ?: []; 152 $formattedcontent = array_merge($formattedcontent, $thirdpartynodearray); 153 } 154 return $formattedcontent; 155 } 156 157 /** 158 * Gets an array of third party navigation nodes in an array formatted for a url_select element. 159 * 160 * @return array|null The thirdparty node array. 161 */ 162 protected function get_thirdparty_node_array(): ?array { 163 $results = []; 164 165 $flatnodes = array_merge(...(array_values($this->get_ordered_nodes()))); 166 167 foreach ($this->node->children as $child) { 168 if (array_search($child->key, $flatnodes) === false) { 169 $results[] = $child; 170 } 171 } 172 173 return \core\navigation\views\secondary::create_menu_element($results, true); 174 } 175 176 /** 177 * Recursively tries to find a matching url 178 * @param array $urlcontent The content for the url_select 179 * @param int $strictness Strictness for the compare criteria 180 * @return string The matching active url 181 */ 182 protected function find_active_page(array $urlcontent, int $strictness = URL_MATCH_EXACT): string { 183 foreach ($urlcontent as $key => $value) { 184 if (is_array($value) && $activeitem = $this->find_active_page($value, $strictness)) { 185 return $activeitem; 186 } else if ($this->page->url->compare(new moodle_url($key), $strictness)) { 187 return $key; 188 } 189 } 190 191 return ""; 192 } 193 194 /** 195 * Gets the url_select to be displayed in the participants page if available. 196 * 197 * @param \renderer_base $output 198 * @return object|null The content required to render the tertiary navigation 199 */ 200 public function get_dropdown(\renderer_base $output): ?object { 201 if ($urlselectcontent = $this->get_content_for_select()) { 202 $activeurl = $this->find_active_page($urlselectcontent); 203 $activeurl = $activeurl ?: $this->find_active_page($urlselectcontent, URL_MATCH_BASE); 204 205 $selectmenu = new select_menu('participantsnavigation', $urlselectcontent, $activeurl); 206 $selectmenu->set_label(get_string('participantsnavigation', 'course'), ['class' => 'sr-only']); 207 208 return $selectmenu->export_for_template($output); 209 } 210 211 return null; 212 } 213 214 /** 215 * Export the content to be displayed on the participants page. 216 * 217 * @param \renderer_base $output 218 * @return array Consists of the following: 219 * - navigation A stdclass representing the standard navigation options to be fed into a urlselect 220 * - renderedcontent Rendered content to be displayed in line with the tertiary nav 221 */ 222 public function export_for_template(\renderer_base $output) { 223 return [ 224 'navigation' => $this->get_dropdown($output), 225 'renderedcontent' => $this->renderedcontent, 226 ]; 227 } 228 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body