Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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              $currentcat = $this->page->url->param('categoryid');
 108              foreach ($categories as $id => $cat) {
 109                  $url = new moodle_url($this->page->url, ['categoryid' => $id]);
 110                  if ($id == $currentcat) {
 111                      $currenturl = $url->out();
 112                  }
 113                  $options[$url->out()] = $cat;
 114              }
 115  
 116              $select = new \url_select($options, $currenturl, null);
 117              $select->set_label(get_string('category'), ['class' => 'sr-only']);
 118              $select->class .= ' text-truncate w-100';
 119              return $select->export_for_template($output);
 120          }
 121  
 122          return null;
 123      }
 124  
 125      /**
 126       * Get the search box
 127       *
 128       * @return array
 129       */
 130      protected function get_search_form(): array {
 131          $searchform = [
 132              'btnclass' => 'btn-primary',
 133              'inputname' => 'search',
 134              'searchstring' => get_string('searchcourses'),
 135              'query' => $this->searchvalue
 136          ];
 137          if (\core_course_category::has_capability_on_any(['moodle/category:manage', 'moodle/course:create'])) {
 138              $searchform['action'] = new moodle_url('/course/management.php');
 139          } else {
 140              $searchform['action'] = new moodle_url('/course/search.php');
 141          }
 142          return $searchform;
 143      }
 144  
 145      /**
 146       * Export the content to be displayed on the participants page.
 147       *
 148       * @param \renderer_base $output
 149       * @return array Consists of the following:
 150       *              - urlselect A stdclass representing the standard navigation options to be fed into a urlselect
 151       *              - renderedcontent Rendered content to be displayed in line with the tertiary nav
 152       */
 153      public function export_for_template(\renderer_base $output): array {
 154          return [
 155              'urlselect' => $this->get_dropdown($output),
 156              'categoryselect' => $this->get_category_select($output),
 157              'search' => $this->get_search_form(),
 158              'heading' => $this->heading,
 159          ];
 160      }
 161  }