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