Differences Between: [Versions 400 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 ], 73 'groups:group' => [ 74 'groups' 75 ], 76 'permissions:role' => [ 77 'override', 78 'roles', 79 'otherusers', 80 'permissions', 81 'roleoverride', 82 'rolecheck', 83 'roleassign', 84 ] 85 ]; 86 } 87 88 /** 89 * Get the content for the url_select select box. 90 * 91 * @return array 92 */ 93 protected function get_content_for_select(): array { 94 if (!$this->node) { 95 return []; 96 } 97 98 $formattedcontent = []; 99 $enrolmentsheading = get_string('enrolments', 'enrol'); 100 if ($this->page->context->contextlevel != CONTEXT_MODULE && 101 $this->page->context->contextlevel != CONTEXT_COURSECAT) { 102 // Pre-populate the formatted tertiary nav items with the "Enrolled users" node if user can view the participants page. 103 $coursecontext = context_course::instance($this->course->id); 104 $canviewparticipants = course_can_view_participants($coursecontext); 105 if ($canviewparticipants) { 106 $participantsurl = (new moodle_url('/user/index.php', ['id' => $this->course->id]))->out(); 107 $formattedcontent[] = [ 108 $enrolmentsheading => [ 109 $participantsurl => get_string('enrolledusers', 'enrol'), 110 ] 111 ]; 112 } 113 } 114 115 $nodes = $this->get_ordered_nodes(); 116 foreach ($nodes as $description => $content) { 117 list($stringid, $location) = explode(':', $description); 118 $heading = get_string($stringid, $location); 119 $items = []; 120 foreach ($content as $key) { 121 if ($node = $this->node->find($key, null)) { 122 if ($node->has_action()) { 123 $items[$node->action()->out()] = $node->text; 124 } 125 126 // Additional items to be added. 127 if ($key === 'groups') { 128 $params = ['id' => $this->course->id]; 129 $items += [ 130 (new moodle_url('/group/groupings.php', $params))->out() => get_string('groupings', 'group'), 131 (new moodle_url('/group/overview.php', $params))->out() => get_string('overview', 'group') 132 ]; 133 } 134 } 135 } 136 if ($items) { 137 if ($heading === $enrolmentsheading) { 138 // Merge the contents of the "Enrolments" group with the ones from the course settings nav. 139 $formattedcontent[0][$heading] = array_merge($formattedcontent[0][$heading], $items); 140 } else { 141 $formattedcontent[][$heading] = $items; 142 } 143 } 144 } 145 146 // If we are accessing a page from a module/category context additional nodes will not be visible. 147 if ($this->page->context->contextlevel != CONTEXT_MODULE && 148 $this->page->context->contextlevel != CONTEXT_COURSECAT) { 149 // Need to do some funky code here to find out if we have added third party navigation nodes. 150 $thirdpartynodearray = $this->get_thirdparty_node_array() ?: []; 151 $formattedcontent = array_merge($formattedcontent, $thirdpartynodearray); 152 } 153 return $formattedcontent; 154 } 155 156 /** 157 * Gets an array of third party navigation nodes in an array formatted for a url_select element. 158 * 159 * @return array|null The thirdparty node array. 160 */ 161 protected function get_thirdparty_node_array(): ?array { 162 $results = []; 163 164 $flatnodes = array_merge(...(array_values($this->get_ordered_nodes()))); 165 166 foreach ($this->node->children as $child) { 167 if (array_search($child->key, $flatnodes) === false) { 168 $results[] = $child; 169 } 170 } 171 172 return \core\navigation\views\secondary::create_menu_element($results, true); 173 } 174 175 /** 176 * Recursively tries to find a matching url 177 * @param array $urlcontent The content for the url_select 178 * @param int $strictness Strictness for the compare criteria 179 * @return string The matching active url 180 */ 181 protected function find_active_page(array $urlcontent, int $strictness = URL_MATCH_EXACT): string { 182 foreach ($urlcontent as $key => $value) { 183 if (is_array($value) && $activeitem = $this->find_active_page($value, $strictness)) { 184 return $activeitem; 185 } else if ($this->page->url->compare(new moodle_url($key), $strictness)) { 186 return $key; 187 } 188 } 189 190 return ""; 191 } 192 193 /** 194 * Gets the url_select to be displayed in the participants page if available. 195 * 196 * @param \renderer_base $output 197 * @return object|null The content required to render the url_select 198 */ 199 public function get_dropdown(\renderer_base $output): ?object { 200 if ($urlselectcontent = $this->get_content_for_select()) { 201 $activeurl = $this->find_active_page($urlselectcontent); 202 $activeurl = $activeurl ?: $this->find_active_page($urlselectcontent, URL_MATCH_BASE); 203 $urlselect = new \url_select($urlselectcontent, $activeurl, null); 204 $urlselect->set_label(get_string('participantsnavigation', 'course'), ['class' => 'sr-only']); 205 return $urlselect->export_for_template($output); 206 } 207 208 return null; 209 } 210 211 /** 212 * Export the content to be displayed on the participants page. 213 * 214 * @param \renderer_base $output 215 * @return array Consists of the following: 216 * - urlselect A stdclass representing the standard navigation options to be fed into a urlselect 217 * - renderedcontent Rendered content to be displayed in line with the tertiary nav 218 */ 219 public function export_for_template(\renderer_base $output) { 220 return [ 221 'urlselect' => $this->get_dropdown($output), 222 'renderedcontent' => $this->renderedcontent, 223 ]; 224 } 225 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body