Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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 mod_forum\output;
  18  
  19  use renderable;
  20  use renderer_base;
  21  use templatable;
  22  use moodle_url;
  23  use help_icon;
  24  use mod_forum\local\entities\forum as forum_entity;
  25  
  26  /**
  27   * Render activity page for tertiary nav
  28   *
  29   * Render elements search forum, add new discussion button and subscribe all
  30   * to the page action.
  31   *
  32   * @package mod_forum
  33   * @copyright 2021 Sujith Haridasan <sujith@moodle.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class forum_actionbar implements renderable, templatable {
  37      /**
  38       * @var forum_entity $forum
  39       */
  40      private $forum;
  41  
  42      /**
  43       * @var \stdClass $course
  44       */
  45      private $course;
  46  
  47      /**
  48       * @var mixed $groupid
  49       */
  50      private $groupid;
  51  
  52      /**
  53       * @var string $search
  54       */
  55      private $search;
  56  
  57      /**
  58       * forum_actionbar constructor.
  59       *
  60       * @param forum_entity $forum The forum object.
  61       * @param \stdClass $course The course object.
  62       * @param int|null $groupid The group id.
  63       * @param string $search The search string.
  64       */
  65      public function __construct(forum_entity $forum, \stdClass $course, ?int $groupid, string $search) {
  66          $this->forum = $forum;
  67          $this->course = $course;
  68          $this->groupid = $groupid;
  69          $this->search = $search;
  70      }
  71  
  72      /**
  73       * Render the new discussion button.
  74       *
  75       * @return string HTML button
  76       */
  77      private function get_new_discussion_topic_button(): string {
  78          global $USER;
  79          $renderfactory = \mod_forum\local\container::get_renderer_factory();
  80          $discussionrenderer = $renderfactory->get_discussion_list_renderer($this->forum);
  81          return $discussionrenderer->render_new_discussion($USER, $this->groupid);
  82      }
  83  
  84      /**
  85       * Data for the template.
  86       *
  87       * @param renderer_base $output The render_base object.
  88       * @return array data for the template
  89       */
  90      public function export_for_template(renderer_base $output): array {
  91          global $USER;
  92          $actionurl = (new moodle_url('/mod/forum/search.php'))->out(false);
  93          $helpicon = new help_icon('search', 'core');
  94          $hiddenfields = [
  95              (object) ['name' => 'id', 'value' => $this->course->id],
  96          ];
  97          $shownewdiscussionbtn = '';
  98          if ($this->forum->get_type() !== 'single') {
  99              $shownewdiscussionbtn = $this->get_new_discussion_topic_button();
 100          }
 101          $data = [
 102              'action' => $actionurl,
 103              'hiddenfields' => $hiddenfields,
 104              'query' => $this->search,
 105              'helpicon' => $helpicon->export_for_template($output),
 106              'inputname' => 'search',
 107              'searchstring' => get_string('searchforums', 'mod_forum'),
 108              'newdiscussionbtn' => $shownewdiscussionbtn,
 109          ];
 110  
 111          $legacydatamapperfactory = \mod_forum\local\container::get_legacy_data_mapper_factory();
 112          $forumobject = $legacydatamapperfactory->get_forum_data_mapper()->to_legacy_object($this->forum);
 113          $context = $this->forum->get_context();
 114          $activeenrolled = is_enrolled($context, $USER, '', true);
 115          $canmanage = has_capability('mod/forum:managesubscriptions', $context);
 116          $cansubscribe = $activeenrolled && !($this->forum->get_subscription_mode() === FORUM_FORCESUBSCRIBE) &&
 117              (!($this->forum->get_subscription_mode() === FORUM_DISALLOWSUBSCRIBE) || $canmanage);
 118          if ($cansubscribe) {
 119              $returnurl =
 120                  (new moodle_url('/mod/forum/view.php', ['id' => $this->forum->get_course_module_record()->id]))->out(false);
 121              if (!\mod_forum\subscriptions::is_subscribed($USER->id, $forumobject, null, $this->forum->get_course_module_record())) {
 122                  $data['subscribetoforum'] = (new moodle_url(
 123                      '/mod/forum/subscribe.php',
 124                      ['id' => $forumobject->id, 'sesskey' => sesskey(), 'returnurl' => $returnurl]
 125                      ))->out(false);
 126              } else {
 127                  $data['unsubscribefromforum'] = (new moodle_url(
 128                      '/mod/forum/subscribe.php',
 129                      ['id' => $forumobject->id, 'sesskey' => sesskey(), 'returnurl' => $returnurl]
 130                  ))->out(false);
 131              }
 132          }
 133          return $data;
 134      }
 135  }