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.
/grade/ -> renderer.php (source)

Differences Between: [Versions 400 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  defined('MOODLE_INTERNAL') || die;
  18  
  19  use \core_grades\output\action_bar;
  20  use core_message\helper;
  21  use core_message\api;
  22  
  23  /**
  24   * Renderer class for the grade pages.
  25   *
  26   * @package    core_grades
  27   * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class core_grades_renderer extends plugin_renderer_base {
  31  
  32      /**
  33       * Renders the action bar for a given page.
  34       *
  35       * @param action_bar $actionbar
  36       * @return string The HTML output
  37       */
  38      public function render_action_bar(action_bar $actionbar): string {
  39          $data = $actionbar->export_for_template($this);
  40          return $this->render_from_template($actionbar->get_template(), $data);
  41      }
  42  
  43      /**
  44       * Renders the group selector trigger element.
  45       *
  46       * @param object $course The course object.
  47       * @param string|null $groupactionbaseurl The base URL for the group action.
  48       * @return string|null The raw HTML to render.
  49       */
  50      public function group_selector(object $course, ?string $groupactionbaseurl = null): ?string {
  51          global $USER;
  52  
  53          // Make sure that group mode is enabled.
  54          if (!$groupmode = $course->groupmode) {
  55              return null;
  56          }
  57  
  58          $label = $groupmode == VISIBLEGROUPS ? get_string('selectgroupsvisible') :
  59              get_string('selectgroupsseparate');
  60  
  61          $data = [
  62              'label' => $label,
  63              'courseid' => $course->id,
  64              'groupactionbaseurl' => $groupactionbaseurl
  65          ];
  66  
  67          $context = context_course::instance($course->id);
  68  
  69          if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
  70              $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
  71          } else {
  72              $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
  73          }
  74  
  75          $activegroup = groups_get_course_group($course, true, $allowedgroups);
  76  
  77          if ($activegroup) {
  78              $group = groups_get_group($activegroup);
  79              $data['selectedgroup'] = format_string($group->name, true, ['context' => $context]);
  80          } else if ($activegroup === 0) {
  81              $data['selectedgroup'] = get_string('allparticipants');
  82          }
  83  
  84          $this->page->requires->js_call_amd('core_grades/searchwidget/group', 'init');
  85          return $this->render_from_template('core_grades/group_selector', $data);
  86      }
  87  
  88      /**
  89       * Creates and renders a custom user heading.
  90       *
  91       * @param stdClass $user The user object.
  92       * @param int $courseid The course ID.
  93       * @param bool $showbuttons Whether to display buttons (message, add to contacts) within the heading.
  94       * @return string The raw HTML to render.
  95       */
  96      public function user_heading(stdClass $user, int $courseid, bool $showbuttons = true) : string {
  97          global $USER;
  98  
  99          $headingdata = [
 100              'userprofileurl' => (new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $courseid]))->out(false),
 101              'name' => fullname($user),
 102              'image' => $this->user_picture($user, ['size' => 50, 'link' => false])
 103          ];
 104  
 105          if ($showbuttons) {
 106              // Generate the data for the 'message' button.
 107              $messagelinkattributes = array_map(function($name, $value) {
 108                  return ['name' => $name, 'value' => $value];
 109              }, array_keys(helper::messageuser_link_params($user->id)), helper::messageuser_link_params($user->id));
 110              $messagelinkattributes[] = ['name' => 'class', 'value' => 'btn px-0'];
 111  
 112              $headingdata['buttons'][] = [
 113                  'title' => get_string('message', 'message'),
 114                  'url' => (new moodle_url('/message/index.php', ['id' => $user->id]))->out(false),
 115                  'icon' => ['name' => 't/message', 'component' => 'core'],
 116                  'linkattributes' => $messagelinkattributes
 117              ];
 118              // Include js for messaging.
 119              helper::messageuser_requirejs();
 120  
 121              if ($USER->id != $user->id) {
 122                  // Generate the data for the 'contact' button.
 123                  $iscontact = api::is_contact($USER->id, $user->id);
 124                  $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
 125                  $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
 126                  $contacticon = $iscontact ? 't/removecontact' : 't/addcontact';
 127  
 128                  $togglelinkparams = helper::togglecontact_link_params($user, $iscontact, false);
 129                  $togglecontactlinkattributes = array_map(function($name, $value) {
 130                      if ($name === 'class') {
 131                          $value .= ' btn px-0';
 132                      }
 133                      return ['name' => $name, 'value' => $value];
 134                  }, array_keys($togglelinkparams), $togglelinkparams);
 135  
 136                  $headingdata['buttons'][] = [
 137                      'title' => get_string($contacttitle, 'message'),
 138                      'url' => (new moodle_url('/message/index.php', ['user1' => $USER->id, 'user2' => $user->id,
 139                          $contacturlaction => $user->id, 'sesskey' => sesskey()]))->out(false),
 140                      'icon' => ['name' => $contacticon, 'component' => 'core'],
 141                      'linkattributes' => $togglecontactlinkattributes
 142                  ];
 143                  // Include js for contact toggle.
 144                  helper::togglecontact_requirejs();
 145              }
 146          }
 147  
 148          return $this->render_from_template('core_grades/user_heading', $headingdata);
 149      }
 150  }