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 mod_forum\output;
  18  
  19  use moodle_url;
  20  use renderer_base;
  21  use url_select;
  22  use renderable;
  23  use templatable;
  24  
  25  /**
  26   * Renders the subscribers page for this activity.
  27   *
  28   * @package   mod_forum
  29   * @copyright 2021 Sujith Haridasan <sujith@moodle.com>
  30   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class subscription_actionbar implements renderable, templatable {
  33      /** @var int course id */
  34      private $id;
  35  
  36      /** @var moodle_url */
  37      private $currenturl;
  38  
  39      /** @var \stdClass  */
  40      private $forum;
  41  
  42      /** @var int */
  43      private $edit;
  44  
  45      /**
  46       * subscription_actionbar constructor.
  47       *
  48       * @param int $id The forum id.
  49       * @param moodle_url $currenturl Current URL.
  50       * @param \stdClass $forum The forum object.
  51       * @param int $edit This argument decides to show view/manage subscribers view.
  52       */
  53      public function __construct(int $id, moodle_url $currenturl, \stdClass $forum, int $edit) {
  54          $this->id = $id;
  55          $this->currenturl = $currenturl;
  56          $this->forum = $forum;
  57          $this->edit = $edit;
  58      }
  59  
  60      /**
  61       * Create url select menu for subscription option
  62       *
  63       * @return url_select|null the url_select object
  64       */
  65      private function create_subscription_menu(): ?url_select {
  66          // When user is on manage subscription, we don't have to show the subscription selector.
  67          if ($this->edit === 1 && !\mod_forum\subscriptions::is_forcesubscribed($this->forum)) {
  68              return  null;
  69          }
  70  
  71          $sesskey = sesskey();
  72          $modeset = \mod_forum\subscriptions::get_subscription_mode($this->forum);
  73          $optionallink = new moodle_url('/mod/forum/subscribe.php',
  74              ['id' => $this->id, 'mode' => FORUM_CHOOSESUBSCRIBE, 'sesskey' => $sesskey, 'edit' => $this->edit]);
  75          $forcedlink = new moodle_url('/mod/forum/subscribe.php',
  76              ['id' => $this->id, 'mode' => FORUM_FORCESUBSCRIBE, 'sesskey' => $sesskey, 'edit' => $this->edit]);
  77          $autolink = new moodle_url('/mod/forum/subscribe.php',
  78              ['id' => $this->id, 'mode' => FORUM_INITIALSUBSCRIBE, 'sesskey' => $sesskey, 'edit' => $this->edit]);
  79          $disabledlink = new moodle_url('/mod/forum/subscribe.php',
  80              ['id' => $this->id, 'mode' => FORUM_DISALLOWSUBSCRIBE, 'sesskey' => $sesskey, 'edit' => $this->edit]);
  81  
  82          $menu = [
  83              $optionallink->out(false) => get_string('subscriptionoptional', 'forum'),
  84              $forcedlink->out(false) => get_string('subscriptionforced', 'forum'),
  85              $autolink->out(false) => get_string('subscriptionauto', 'forum'),
  86              $disabledlink->out(false) => get_string('subscriptiondisabled', 'forum'),
  87          ];
  88  
  89          switch ($modeset) {
  90              case FORUM_CHOOSESUBSCRIBE:
  91                  $set = get_string('subscriptionoptional', 'forum');
  92                  break;
  93              case FORUM_FORCESUBSCRIBE:
  94                  $set = get_string('subscriptionforced', 'forum');
  95                  break;
  96              case FORUM_INITIALSUBSCRIBE:
  97                  $set = get_string('subscriptionauto', 'forum');
  98                  break;
  99              case FORUM_DISALLOWSUBSCRIBE:
 100                  $set = get_string('subscriptiondisabled', 'forum');
 101                  break;
 102              default:
 103                  throw new \moodle_exception(get_string('invalidforcesubscribe', 'forum'));
 104          }
 105  
 106          $menu = array_filter($menu, function($key) use ($set) {
 107              if ($key !== $set) {
 108                  return true;
 109              }
 110          });
 111          $urlselect = new url_select($menu, $this->currenturl, ['' => $set], 'selectsubscriptionoptions');
 112          $urlselect->set_label(get_string('subscriptionmode', 'mod_forum'), ['class' => 'mr-1']);
 113          $urlselect->set_help_icon('subscriptionmode', 'mod_forum');
 114          $urlselect->class .= ' float-right';
 115          return $urlselect;
 116      }
 117  
 118      /**
 119       * Create view and manage subscribers select menu.
 120       *
 121       * @return url_select|null get url_select object.
 122       */
 123      private function create_view_manage_menu(): ?url_select {
 124          // If forced subscription is used then no need to show the view.
 125          if (\mod_forum\subscriptions::is_forcesubscribed($this->forum)) {
 126              return null;
 127          }
 128  
 129          $viewlink = new moodle_url('/mod/forum/subscribers.php', ['id' => $this->id, 'edit' => 'off']);
 130          $managelink = new moodle_url('/mod/forum/subscribers.php', ['id' => $this->id, 'edit' => 'on']);
 131  
 132          $menu = [
 133              $viewlink->out(false) => get_string('forum:viewsubscribers', 'forum'),
 134              $managelink->out(false) => get_string('managesubscriptionson', 'forum'),
 135          ];
 136  
 137          if ($this->edit === 0) {
 138              $this->currenturl = $viewlink;
 139          } else {
 140              $this->currenturl = $managelink;
 141          }
 142          $urlselect = new url_select($menu, $this->currenturl->out(false), null, 'selectviewandmanagesubscribers');
 143          $urlselect->set_label(get_string('subscribers', 'forum'), ['class' => 'accesshide']);
 144          return $urlselect;
 145      }
 146  
 147      /**
 148       * Data for the template.
 149       *
 150       * @param renderer_base $output The render_base object.
 151       * @return array data for template
 152       */
 153      public function export_for_template(renderer_base $output): array {
 154          $data = [];
 155          $subscribeoptionselect = $this->create_subscription_menu();
 156          $viewmanageselect = $this->create_view_manage_menu();
 157  
 158          if ($subscribeoptionselect) {
 159              $data ['subscriptionoptions'] = $subscribeoptionselect->export_for_template($output);
 160          }
 161          if ($viewmanageselect) {
 162              $data['viewandmanageselect'] = $viewmanageselect->export_for_template($output);
 163          }
 164          return $data;
 165      }
 166  }