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] [Versions 401 and 402] [Versions 401 and 403]

   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 theme_boost\output;
  18  
  19  use moodle_url;
  20  use html_writer;
  21  use get_string;
  22  
  23  defined('MOODLE_INTERNAL') || die;
  24  
  25  /**
  26   * Renderers to align Moodle's HTML with that expected by Bootstrap
  27   *
  28   * @package    theme_boost
  29   * @copyright  2012 Bas Brands, www.basbrands.nl
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class core_renderer extends \core_renderer {
  33  
  34      /**
  35       * Returns HTML to display a "Turn editing on/off" button in a form.
  36       *
  37       * @param moodle_url $url The URL + params to send through when clicking the button
  38       * @param string $method
  39       * @return string HTML the button
  40       */
  41      public function edit_button(moodle_url $url, string $method = 'post') {
  42          if ($this->page->theme->haseditswitch) {
  43              return;
  44          }
  45          $url->param('sesskey', sesskey());
  46          if ($this->page->user_is_editing()) {
  47              $url->param('edit', 'off');
  48              $editstring = get_string('turneditingoff');
  49          } else {
  50              $url->param('edit', 'on');
  51              $editstring = get_string('turneditingon');
  52          }
  53          $button = new \single_button($url, $editstring, $method, ['class' => 'btn btn-primary']);
  54          return $this->render_single_button($button);
  55      }
  56  
  57      /**
  58       * Renders the "breadcrumb" for all pages in boost.
  59       *
  60       * @return string the HTML for the navbar.
  61       */
  62      public function navbar(): string {
  63          $newnav = new \theme_boost\boostnavbar($this->page);
  64          return $this->render_from_template('core/navbar', $newnav);
  65      }
  66  
  67      /**
  68       * Renders the context header for the page.
  69       *
  70       * @param array $headerinfo Heading information.
  71       * @param int $headinglevel What 'h' level to make the heading.
  72       * @return string A rendered context header.
  73       */
  74      public function context_header($headerinfo = null, $headinglevel = 1): string {
  75          global $DB, $USER, $CFG, $SITE;
  76          require_once($CFG->dirroot . '/user/lib.php');
  77          $context = $this->page->context;
  78          $heading = null;
  79          $imagedata = null;
  80          $subheader = null;
  81          $userbuttons = null;
  82  
  83          // Make sure to use the heading if it has been set.
  84          if (isset($headerinfo['heading'])) {
  85              $heading = $headerinfo['heading'];
  86          } else {
  87              $heading = $this->page->heading;
  88          }
  89  
  90          // The user context currently has images and buttons. Other contexts may follow.
  91          if ((isset($headerinfo['user']) || $context->contextlevel == CONTEXT_USER) && $this->page->pagetype !== 'my-index') {
  92              if (isset($headerinfo['user'])) {
  93                  $user = $headerinfo['user'];
  94              } else {
  95                  // Look up the user information if it is not supplied.
  96                  $user = $DB->get_record('user', array('id' => $context->instanceid));
  97              }
  98  
  99              // If the user context is set, then use that for capability checks.
 100              if (isset($headerinfo['usercontext'])) {
 101                  $context = $headerinfo['usercontext'];
 102              }
 103  
 104              // Only provide user information if the user is the current user, or a user which the current user can view.
 105              // When checking user_can_view_profile(), either:
 106              // If the page context is course, check the course context (from the page object) or;
 107              // If page context is NOT course, then check across all courses.
 108              $course = ($this->page->context->contextlevel == CONTEXT_COURSE) ? $this->page->course : null;
 109  
 110              if (user_can_view_profile($user, $course)) {
 111                  // Use the user's full name if the heading isn't set.
 112                  if (empty($heading)) {
 113                      $heading = fullname($user);
 114                  }
 115  
 116                  $imagedata = $this->user_picture($user, array('size' => 100));
 117  
 118                  // Check to see if we should be displaying a message button.
 119                  if (!empty($CFG->messaging) && has_capability('moodle/site:sendmessage', $context)) {
 120                      $userbuttons = array(
 121                          'messages' => array(
 122                              'buttontype' => 'message',
 123                              'title' => get_string('message', 'message'),
 124                              'url' => new moodle_url('/message/index.php', array('id' => $user->id)),
 125                              'image' => 'message',
 126                              'linkattributes' => \core_message\helper::messageuser_link_params($user->id),
 127                              'page' => $this->page
 128                          )
 129                      );
 130  
 131                      if ($USER->id != $user->id) {
 132                          $iscontact = \core_message\api::is_contact($USER->id, $user->id);
 133                          $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
 134                          $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
 135                          $contactimage = $iscontact ? 'removecontact' : 'addcontact';
 136                          $userbuttons['togglecontact'] = array(
 137                                  'buttontype' => 'togglecontact',
 138                                  'title' => get_string($contacttitle, 'message'),
 139                                  'url' => new moodle_url('/message/index.php', array(
 140                                          'user1' => $USER->id,
 141                                          'user2' => $user->id,
 142                                          $contacturlaction => $user->id,
 143                                          'sesskey' => sesskey())
 144                                  ),
 145                                  'image' => $contactimage,
 146                                  'linkattributes' => \core_message\helper::togglecontact_link_params($user, $iscontact),
 147                                  'page' => $this->page
 148                              );
 149                      }
 150  
 151                      $this->page->requires->string_for_js('changesmadereallygoaway', 'moodle');
 152                  }
 153              } else {
 154                  $heading = null;
 155              }
 156          }
 157  
 158          $prefix = null;
 159          if ($context->contextlevel == CONTEXT_MODULE) {
 160              if ($this->page->course->format === 'singleactivity') {
 161                  $heading = format_string($this->page->course->fullname, true, ['context' => $context]);
 162              } else {
 163                  $heading = $this->page->cm->get_formatted_name();
 164                  $iconurl = $this->page->cm->get_icon_url();
 165                  $iconclass = $iconurl->get_param('filtericon') ? '' : 'nofilter';
 166                  $iconattrs = [
 167                      'class' => "icon activityicon $iconclass",
 168                      'aria-hidden' => 'true'
 169                  ];
 170                  $imagedata = html_writer::img($iconurl->out(false), '', $iconattrs);
 171                  $purposeclass = plugin_supports('mod', $this->page->activityname, FEATURE_MOD_PURPOSE);
 172                  $purposeclass .= ' activityiconcontainer';
 173                  $purposeclass .= ' modicon_' . $this->page->activityname;
 174                  $imagedata = html_writer::tag('div', $imagedata, ['class' => $purposeclass]);
 175                  if (!empty($USER->editing)) {
 176                      $prefix = get_string('modulename', $this->page->activityname);
 177                  }
 178              }
 179          }
 180  
 181  
 182          $contextheader = new \context_header($heading, $headinglevel, $imagedata, $userbuttons, $prefix);
 183          return $this->render_context_header($contextheader);
 184      }
 185  
 186       /**
 187        * Renders the header bar.
 188        *
 189        * @param context_header $contextheader Header bar object.
 190        * @return string HTML for the header bar.
 191        */
 192      protected function render_context_header(\context_header $contextheader) {
 193  
 194          // Generate the heading first and before everything else as we might have to do an early return.
 195          if (!isset($contextheader->heading)) {
 196              $heading = $this->heading($this->page->heading, $contextheader->headinglevel, 'h2');
 197          } else {
 198              $heading = $this->heading($contextheader->heading, $contextheader->headinglevel, 'h2');
 199          }
 200  
 201          // All the html stuff goes here.
 202          $html = html_writer::start_div('page-context-header');
 203  
 204          // Image data.
 205          if (isset($contextheader->imagedata)) {
 206              // Header specific image.
 207              $html .= html_writer::div($contextheader->imagedata, 'page-header-image mr-2');
 208          }
 209  
 210          // Headings.
 211          if (isset($contextheader->prefix)) {
 212              $prefix = html_writer::div($contextheader->prefix, 'text-muted text-uppercase small line-height-3');
 213              $heading = $prefix . $heading;
 214          }
 215          $html .= html_writer::tag('div', $heading, array('class' => 'page-header-headings'));
 216  
 217          // Buttons.
 218          if (isset($contextheader->additionalbuttons)) {
 219              $html .= html_writer::start_div('btn-group header-button-group');
 220              foreach ($contextheader->additionalbuttons as $button) {
 221                  if (!isset($button->page)) {
 222                      // Include js for messaging.
 223                      if ($button['buttontype'] === 'togglecontact') {
 224                          \core_message\helper::togglecontact_requirejs();
 225                      }
 226                      if ($button['buttontype'] === 'message') {
 227                          \core_message\helper::messageuser_requirejs();
 228                      }
 229                      $image = $this->pix_icon($button['formattedimage'], $button['title'], 'moodle', array(
 230                          'class' => 'iconsmall',
 231                          'role' => 'presentation'
 232                      ));
 233                      $image .= html_writer::span($button['title'], 'header-button-title');
 234                  } else {
 235                      $image = html_writer::empty_tag('img', array(
 236                          'src' => $button['formattedimage'],
 237                          'role' => 'presentation'
 238                      ));
 239                  }
 240                  $html .= html_writer::link($button['url'], html_writer::tag('span', $image), $button['linkattributes']);
 241              }
 242              $html .= html_writer::end_div();
 243          }
 244          $html .= html_writer::end_div();
 245  
 246          return $html;
 247      }
 248  
 249      /**
 250       * See if this is the first view of the current cm in the session if it has fake blocks.
 251       *
 252       * (We track up to 100 cms so as not to overflow the session.)
 253       * This is done for drawer regions containing fake blocks so we can show blocks automatically.
 254       *
 255       * @return boolean true if the page has fakeblocks and this is the first visit.
 256       */
 257      public function firstview_fakeblocks(): bool {
 258          global $SESSION;
 259  
 260          $firstview = false;
 261          if ($this->page->cm) {
 262              if (!$this->page->blocks->region_has_fakeblocks('side-pre')) {
 263                  return false;
 264              }
 265              if (!property_exists($SESSION, 'firstview_fakeblocks')) {
 266                  $SESSION->firstview_fakeblocks = [];
 267              }
 268              if (array_key_exists($this->page->cm->id, $SESSION->firstview_fakeblocks)) {
 269                  $firstview = false;
 270              } else {
 271                  $SESSION->firstview_fakeblocks[$this->page->cm->id] = true;
 272                  $firstview = true;
 273                  if (count($SESSION->firstview_fakeblocks) > 100) {
 274                      array_shift($SESSION->firstview_fakeblocks);
 275                  }
 276              }
 277          }
 278          return $firstview;
 279      }
 280  }