Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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, \single_button::BUTTON_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;
  76          require_once($CFG->dirroot . '/user/lib.php');
  77          $context = $this->page->context;
  78          $heading = null;
  79          $imagedata = null;
  80          $userbuttons = null;
  81  
  82          // Make sure to use the heading if it has been set.
  83          if (isset($headerinfo['heading'])) {
  84              $heading = $headerinfo['heading'];
  85          } else {
  86              $heading = $this->page->heading;
  87          }
  88  
  89          // The user context currently has images and buttons. Other contexts may follow.
  90          if ((isset($headerinfo['user']) || $context->contextlevel == CONTEXT_USER) && $this->page->pagetype !== 'my-index') {
  91              if (isset($headerinfo['user'])) {
  92                  $user = $headerinfo['user'];
  93              } else {
  94                  // Look up the user information if it is not supplied.
  95                  $user = $DB->get_record('user', array('id' => $context->instanceid));
  96              }
  97  
  98              // If the user context is set, then use that for capability checks.
  99              if (isset($headerinfo['usercontext'])) {
 100                  $context = $headerinfo['usercontext'];
 101              }
 102  
 103              // Only provide user information if the user is the current user, or a user which the current user can view.
 104              // When checking user_can_view_profile(), either:
 105              // If the page context is course, check the course context (from the page object) or;
 106              // If page context is NOT course, then check across all courses.
 107              $course = ($this->page->context->contextlevel == CONTEXT_COURSE) ? $this->page->course : null;
 108  
 109              if (user_can_view_profile($user, $course)) {
 110                  // Use the user's full name if the heading isn't set.
 111                  if (empty($heading)) {
 112                      $heading = fullname($user);
 113                  }
 114  
 115                  $imagedata = $this->user_picture($user, array('size' => 100));
 116  
 117                  // Check to see if we should be displaying a message button.
 118                  if (!empty($CFG->messaging) && has_capability('moodle/site:sendmessage', $context)) {
 119                      $userbuttons = array(
 120                          'messages' => array(
 121                              'buttontype' => 'message',
 122                              'title' => get_string('message', 'message'),
 123                              'url' => new moodle_url('/message/index.php', array('id' => $user->id)),
 124                              'image' => 'message',
 125                              'linkattributes' => \core_message\helper::messageuser_link_params($user->id),
 126                              'page' => $this->page
 127                          )
 128                      );
 129  
 130                      if ($USER->id != $user->id) {
 131                          $iscontact = \core_message\api::is_contact($USER->id, $user->id);
 132                          $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
 133                          $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
 134                          $contactimage = $iscontact ? 'removecontact' : 'addcontact';
 135                          $userbuttons['togglecontact'] = array(
 136                                  'buttontype' => 'togglecontact',
 137                                  'title' => get_string($contacttitle, 'message'),
 138                                  'url' => new moodle_url('/message/index.php', array(
 139                                          'user1' => $USER->id,
 140                                          'user2' => $user->id,
 141                                          $contacturlaction => $user->id,
 142                                          'sesskey' => sesskey())
 143                                  ),
 144                                  'image' => $contactimage,
 145                                  'linkattributes' => \core_message\helper::togglecontact_link_params($user, $iscontact),
 146                                  'page' => $this->page
 147                              );
 148                      }
 149  
 150                      $this->page->requires->string_for_js('changesmadereallygoaway', 'moodle');
 151                  }
 152              } else {
 153                  $heading = null;
 154              }
 155          }
 156  
 157          $prefix = null;
 158          if ($context->contextlevel == CONTEXT_MODULE) {
 159              if ($this->page->course->format === 'singleactivity') {
 160                  $heading = format_string($this->page->course->fullname, true, ['context' => $context]);
 161              } else {
 162                  $heading = $this->page->cm->get_formatted_name();
 163                  $iconurl = $this->page->cm->get_icon_url();
 164                  $iconclass = $iconurl->get_param('filtericon') ? '' : 'nofilter';
 165                  $iconattrs = [
 166                      'class' => "icon activityicon $iconclass",
 167                      'aria-hidden' => 'true'
 168                  ];
 169                  $imagedata = html_writer::img($iconurl->out(false), '', $iconattrs);
 170                  $purposeclass = plugin_supports('mod', $this->page->activityname, FEATURE_MOD_PURPOSE);
 171                  $purposeclass .= ' activityiconcontainer icon-size-6';
 172                  $purposeclass .= ' modicon_' . $this->page->activityname;
 173                  $imagedata = html_writer::tag('div', $imagedata, ['class' => $purposeclass]);
 174                  if (!empty($USER->editing)) {
 175                      $prefix = get_string('modulename', $this->page->activityname);
 176                  }
 177              }
 178          }
 179  
 180          $contextheader = new \context_header($heading, $headinglevel, $imagedata, $userbuttons, $prefix);
 181          return $this->render_context_header($contextheader);
 182      }
 183  
 184       /**
 185        * Renders the header bar.
 186        *
 187        * @param context_header $contextheader Header bar object.
 188        * @return string HTML for the header bar.
 189        */
 190      protected function render_context_header(\context_header $contextheader) {
 191  
 192          // Generate the heading first and before everything else as we might have to do an early return.
 193          if (!isset($contextheader->heading)) {
 194              $heading = $this->heading($this->page->heading, $contextheader->headinglevel, 'h2');
 195          } else {
 196              $heading = $this->heading($contextheader->heading, $contextheader->headinglevel, 'h2');
 197          }
 198  
 199          // All the html stuff goes here.
 200          $html = html_writer::start_div('page-context-header');
 201  
 202          // Image data.
 203          if (isset($contextheader->imagedata)) {
 204              // Header specific image.
 205              $html .= html_writer::div($contextheader->imagedata, 'page-header-image mr-2');
 206          }
 207  
 208          // Headings.
 209          if (isset($contextheader->prefix)) {
 210              $prefix = html_writer::div($contextheader->prefix, 'text-muted text-uppercase small line-height-3');
 211              $heading = $prefix . $heading;
 212          }
 213          $html .= html_writer::tag('div', $heading, array('class' => 'page-header-headings'));
 214  
 215          // Buttons.
 216          if (isset($contextheader->additionalbuttons)) {
 217              $html .= html_writer::start_div('btn-group header-button-group');
 218              foreach ($contextheader->additionalbuttons as $button) {
 219                  if (!isset($button->page)) {
 220                      // Include js for messaging.
 221                      if ($button['buttontype'] === 'togglecontact') {
 222                          \core_message\helper::togglecontact_requirejs();
 223                      }
 224                      if ($button['buttontype'] === 'message') {
 225                          \core_message\helper::messageuser_requirejs();
 226                      }
 227                      $image = $this->pix_icon($button['formattedimage'], $button['title'], 'moodle', array(
 228                          'class' => 'iconsmall',
 229                          'role' => 'presentation'
 230                      ));
 231                      $image .= html_writer::span($button['title'], 'header-button-title');
 232                  } else {
 233                      $image = html_writer::empty_tag('img', array(
 234                          'src' => $button['formattedimage'],
 235                          'role' => 'presentation'
 236                      ));
 237                  }
 238                  $html .= html_writer::link($button['url'], html_writer::tag('span', $image), $button['linkattributes']);
 239              }
 240              $html .= html_writer::end_div();
 241          }
 242          $html .= html_writer::end_div();
 243  
 244          return $html;
 245      }
 246  
 247      /**
 248       * See if this is the first view of the current cm in the session if it has fake blocks.
 249       *
 250       * (We track up to 100 cms so as not to overflow the session.)
 251       * This is done for drawer regions containing fake blocks so we can show blocks automatically.
 252       *
 253       * @return boolean true if the page has fakeblocks and this is the first visit.
 254       */
 255      public function firstview_fakeblocks(): bool {
 256          global $SESSION;
 257  
 258          $firstview = false;
 259          if ($this->page->cm) {
 260              if (!$this->page->blocks->region_has_fakeblocks('side-pre')) {
 261                  return false;
 262              }
 263              if (!property_exists($SESSION, 'firstview_fakeblocks')) {
 264                  $SESSION->firstview_fakeblocks = [];
 265              }
 266              if (array_key_exists($this->page->cm->id, $SESSION->firstview_fakeblocks)) {
 267                  $firstview = false;
 268              } else {
 269                  $SESSION->firstview_fakeblocks[$this->page->cm->id] = true;
 270                  $firstview = true;
 271                  if (count($SESSION->firstview_fakeblocks) > 100) {
 272                      array_shift($SESSION->firstview_fakeblocks);
 273                  }
 274              }
 275          }
 276          return $firstview;
 277      }
 278  }