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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This file contains a custom renderer class used by the forum module.
  20   *
  21   * @package   mod_forum
  22   * @copyright 2009 Sam Hemelryk
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  /**
  27   * A custom renderer class that extends the plugin_renderer_base and
  28   * is used by the forum module.
  29   *
  30   * @package   mod_forum
  31   * @copyright 2009 Sam Hemelryk
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   **/
  34  class mod_forum_renderer extends plugin_renderer_base {
  35  
  36      /**
  37       * Returns the navigation to the previous and next discussion.
  38       *
  39       * @param mixed $prev Previous discussion record, or false.
  40       * @param mixed $next Next discussion record, or false.
  41       * @return string The output.
  42       */
  43      public function neighbouring_discussion_navigation($prev, $next) {
  44          $html = '';
  45          if ($prev || $next) {
  46              $html .= html_writer::start_tag('div', array('class' => 'discussion-nav clearfix'));
  47              $html .= html_writer::start_tag('ul');
  48              if ($prev) {
  49                  $url = new moodle_url('/mod/forum/discuss.php', array('d' => $prev->id));
  50                  $html .= html_writer::start_tag('li', array('class' => 'prev-discussion'));
  51                  $html .= html_writer::link($url, $this->output->larrow() . ' ' . format_string($prev->name),
  52                      array('aria-label' => get_string('prevdiscussiona', 'mod_forum', format_string($prev->name)),
  53                      'class' => 'btn btn-link'));
  54                  $html .= html_writer::end_tag('li');
  55              }
  56              if ($next) {
  57                  $url = new moodle_url('/mod/forum/discuss.php', array('d' => $next->id));
  58                  $html .= html_writer::start_tag('li', array('class' => 'next-discussion'));
  59                  $html .= html_writer::link($url, format_string($next->name) . ' ' . $this->output->rarrow(),
  60                      array('aria-label' => get_string('nextdiscussiona', 'mod_forum', format_string($next->name)),
  61                      'class' => 'btn btn-link'));
  62                  $html .= html_writer::end_tag('li');
  63              }
  64              $html .= html_writer::end_tag('ul');
  65              $html .= html_writer::end_tag('div');
  66          }
  67          return $html;
  68      }
  69  
  70      /**
  71       * This method is used to generate HTML for a subscriber selection form that
  72       * uses two user_selector controls
  73       *
  74       * @param user_selector_base $existinguc
  75       * @param user_selector_base $potentialuc
  76       * @return string
  77       */
  78      public function subscriber_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
  79          $output = '';
  80          $formattributes = array();
  81          $formattributes['id'] = 'subscriberform';
  82          $formattributes['action'] = '';
  83          $formattributes['method'] = 'post';
  84          $output .= html_writer::start_tag('form', $formattributes);
  85          $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
  86  
  87          $existingcell = new html_table_cell();
  88          $existingcell->text = $existinguc->display(true);
  89          $existingcell->attributes['class'] = 'existing';
  90          $actioncell = new html_table_cell();
  91          $actioncell->text  = html_writer::start_tag('div', array());
  92          $actioncell->text .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'subscribe', 'value'=>$this->page->theme->larrow.' '.get_string('add'), 'class'=>'actionbutton'));
  93          $actioncell->text .= html_writer::empty_tag('br', array());
  94          $actioncell->text .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'unsubscribe', 'value'=>$this->page->theme->rarrow.' '.get_string('remove'), 'class'=>'actionbutton'));
  95          $actioncell->text .= html_writer::end_tag('div', array());
  96          $actioncell->attributes['class'] = 'actions';
  97          $potentialcell = new html_table_cell();
  98          $potentialcell->text = $potentialuc->display(true);
  99          $potentialcell->attributes['class'] = 'potential';
 100  
 101          $table = new html_table();
 102          $table->attributes['class'] = 'subscribertable boxaligncenter';
 103          $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
 104          $output .= html_writer::table($table);
 105  
 106          $output .= html_writer::end_tag('form');
 107          return $output;
 108      }
 109  
 110      /**
 111       * This function generates HTML to display a subscriber overview, primarily used on
 112       * the subscribers page if editing was turned off
 113       *
 114       * @param array $users
 115       * @param object $forum
 116       * @param object $course
 117       * @return string
 118       */
 119      public function subscriber_overview($users, $forum , $course) {
 120          $output = '';
 121          $modinfo = get_fast_modinfo($course);
 122          if (!$users || !is_array($users) || count($users)===0) {
 123              $output .= $this->output->notification(
 124                  get_string("nosubscribers", "forum"),
 125                  \core\output\notification::NOTIFY_INFO, false);
 126          } else if (!isset($modinfo->instances['forum'][$forum->id])) {
 127              $output .= $this->output->heading(get_string("invalidmodule", "error"));
 128          } else {
 129              $cm = $modinfo->instances['forum'][$forum->id];
 130              // TODO Does not support custom user profile fields (MDL-70456).
 131              $canviewemail = in_array('email', \core_user\fields::get_identity_fields(context_module::instance($cm->id), false));
 132              $strparams = new stdclass();
 133              $strparams->name = format_string($forum->name);
 134              $strparams->count = count($users);
 135              $output .= $this->output->heading(get_string("subscriberstowithcount", "forum", $strparams));
 136              $table = new html_table();
 137              $table->id = 'subscribers-table';
 138              $table->head = [];
 139              $table->head[] = get_string('pictureofuser');
 140              $table->head[] = get_string('fullname');
 141              if ($canviewemail) {
 142                  $table->head[] = get_string('email');
 143              }
 144              $table->cellpadding = 5;
 145              $table->cellspacing = 5;
 146              $table->tablealign = 'center';
 147              $table->data = array();
 148              foreach ($users as $user) {
 149                  $info = array($this->output->user_picture($user, array('courseid'=>$course->id)), fullname($user));
 150                  if ($canviewemail) {
 151                      array_push($info, $user->email);
 152                  }
 153                  $table->data[] = $info;
 154              }
 155              $output .= html_writer::table($table);
 156          }
 157          return $output;
 158      }
 159  
 160      /**
 161       * This is used to display a control containing all of the subscribed users so that
 162       * it can be searched
 163       *
 164       * @param user_selector_base $existingusers
 165       * @return string
 166       */
 167      public function subscribed_users(user_selector_base $existingusers) {
 168          $output  = $this->output->box_start('subscriberdiv boxaligncenter');
 169          $output .= html_writer::tag('p', get_string('forcesubscribed', 'forum'));
 170          $output .= $existingusers->display(true);
 171          $output .= $this->output->box_end();
 172          return $output;
 173      }
 174  
 175      /**
 176       * Generate the HTML for an icon to be displayed beside the subject of a timed discussion.
 177       *
 178       * @param object $discussion
 179       * @param bool $visiblenow Indicicates that the discussion is currently
 180       * visible to all users.
 181       * @return string
 182       */
 183      public function timed_discussion_tooltip($discussion, $visiblenow) {
 184          $dates = array();
 185          if ($discussion->timestart) {
 186              $dates[] = get_string('displaystart', 'mod_forum').': '.userdate($discussion->timestart);
 187          }
 188          if ($discussion->timeend) {
 189              $dates[] = get_string('displayend', 'mod_forum').': '.userdate($discussion->timeend);
 190          }
 191  
 192          $str = $visiblenow ? 'timedvisible' : 'timedhidden';
 193          $dates[] = get_string($str, 'mod_forum');
 194  
 195          $tooltip = implode("\n", $dates);
 196          return $this->pix_icon('i/calendar', $tooltip, 'moodle', array('class' => 'smallicon timedpost'));
 197      }
 198  
 199      /**
 200       * Display a forum post in the relevant context.
 201       *
 202       * @param \mod_forum\output\forum_post $post The post to display.
 203       * @return string
 204       */
 205      public function render_forum_post_email(\mod_forum\output\forum_post_email $post) {
 206          $data = $post->export_for_template($this, $this->target === RENDERER_TARGET_TEXTEMAIL);
 207          return $this->render_from_template('mod_forum/' . $this->forum_post_template(), $data);
 208      }
 209  
 210      /**
 211       * The template name for this renderer.
 212       *
 213       * @return string
 214       */
 215      public function forum_post_template() {
 216          return 'forum_post';
 217      }
 218  
 219      /**
 220       * Create the inplace_editable used to select forum digest options.
 221       *
 222       * @param   stdClass    $forum  The forum to create the editable for.
 223       * @param   int         $value  The current value for this user
 224       * @return  inplace_editable
 225       */
 226      public function render_digest_options($forum, $value) {
 227          $options = forum_get_user_digest_options();
 228          $editable = new \core\output\inplace_editable(
 229              'mod_forum',
 230              'digestoptions',
 231              $forum->id,
 232              true,
 233              $options[$value],
 234              $value
 235          );
 236  
 237          $editable->set_type_select($options);
 238  
 239          return $editable;
 240      }
 241  
 242      /**
 243       * Render quick search form.
 244       *
 245       * @param \mod_forum\output\quick_search_form $form The renderable.
 246       * @return string rendered HTML string from the template.
 247       */
 248      public function render_quick_search_form(\mod_forum\output\quick_search_form $form) {
 249          if (strpos($this->page->url->get_path(), "index.php")) {
 250              return $this->render_from_template('mod_forum/quick_search_form', $form->export_for_template($this));
 251          }
 252  
 253          return $this->render_from_template('mod_forum/forum_new_discussion_actionbar', $form->export_for_template($this));
 254      }
 255  
 256      /**
 257       * Render the view action area.
 258       *
 259       * @param \mod_forum\output\forum_actionbar $actionbar forum_actionbar object.
 260       * @return string rendered HTML string
 261       */
 262      public function render_activity_actionbar(\mod_forum\output\forum_actionbar $actionbar): string {
 263          return $this->render_from_template('mod_forum/forum_actionbar', $actionbar->export_for_template($this));
 264      }
 265  
 266      /**
 267       * Render the subscription action area.
 268       *
 269       * @param \mod_forum\output\subscription_actionbar $subscriptionactionbar subscription_actionbar object.
 270       * @return bool|string rendered HTML string.
 271       */
 272      public function subscription_actionbar(\mod_forum\output\subscription_actionbar $subscriptionactionbar): string {
 273          return $this->render_from_template('mod_forum/forum_subscription_action',
 274              $subscriptionactionbar->export_for_template($this));
 275      }
 276  
 277      /**
 278       * Render big search form.
 279       *
 280       * @param \mod_forum\output\big_search_form $form The renderable.
 281       * @return string
 282       */
 283      public function render_big_search_form(\mod_forum\output\big_search_form $form) {
 284          return $this->render_from_template('mod_forum/big_search_form', $form->export_for_template($this));
 285      }
 286  }