Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/grade/ -> renderer.php (source)

Differences Between: [Versions 400 and 402] [Versions 401 and 402] [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  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              'name' => 'group',
  63              'label' => $label,
  64              'courseid' => $course->id,
  65              'groupactionbaseurl' => $groupactionbaseurl
  66          ];
  67  
  68          $context = context_course::instance($course->id);
  69  
  70          if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
  71              $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
  72          } else {
  73              $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
  74          }
  75  
  76          $activegroup = groups_get_course_group($course, true, $allowedgroups);
  77          $data['group'] = $activegroup;
  78  
  79          if ($activegroup) {
  80              $group = groups_get_group($activegroup);
  81              $data['selectedgroup'] = format_string($group->name, true, ['context' => $context]);
  82          } else if ($activegroup === 0) {
  83              $data['selectedgroup'] = get_string('allparticipants');
  84          }
  85  
  86          $this->page->requires->js_call_amd('core_grades/searchwidget/group', 'init');
  87          return $this->render_from_template('core_grades/group_selector', $data);
  88      }
  89  
  90      /**
  91       * Build the data to render the initials bar filter within the gradebook.
  92       * Using this initials selector means you'll have to retain the use of the templates & JS to handle form submission.
  93       * If a simple redirect on each selection is desired the standard user_search() within the user renderer is what you are after.
  94       *
  95       * @param object $course The course object.
  96       * @param context $context Our current context.
  97       * @param string $slug The slug for the report that called this function.
  98       * @return stdClass The data to output.
  99       */
 100      public function initials_selector(
 101          object $course,
 102          context $context,
 103          string $slug
 104      ): stdClass {
 105          global $SESSION, $COURSE;
 106          // User search.
 107          $url = new moodle_url($slug, ['id' => $course->id]);
 108          $firstinitial = $SESSION->gradereport["filterfirstname-{$context->id}"] ?? '';
 109          $lastinitial  = $SESSION->gradereport["filtersurname-{$context->id}"] ?? '';
 110  
 111          $renderer = $this->page->get_renderer('core_user');
 112          $initialsbar = $renderer->partial_user_search($url, $firstinitial, $lastinitial, true);
 113  
 114          $currentfilter = '';
 115          if ($firstinitial !== '' && $lastinitial !== '') {
 116              $currentfilter = get_string('filterbothactive', 'grades', ['first' => $firstinitial, 'last' => $lastinitial]);
 117          } else if ($firstinitial !== '') {
 118              $currentfilter = get_string('filterfirstactive', 'grades', ['first' => $firstinitial]);
 119          } else if ($lastinitial !== '') {
 120              $currentfilter = get_string('filterlastactive', 'grades', ['last' => $lastinitial]);
 121          }
 122  
 123          $this->page->requires->js_call_amd('core_grades/searchwidget/initials', 'init', [$slug]);
 124  
 125          $formdata = (object) [
 126              'courseid' => $COURSE->id,
 127              'initialsbars' => $initialsbar,
 128          ];
 129          $dropdowncontent = $this->render_from_template('core_grades/initials_dropdown_form', $formdata);
 130  
 131          return (object) [
 132               'buttoncontent' => $currentfilter !== '' ? $currentfilter : get_string('filterbyname', 'core_grades'),
 133               'buttonheader' => $currentfilter !== '' ? get_string('name') : null,
 134               'dropdowncontent' => $dropdowncontent,
 135          ];
 136      }
 137  
 138      /**
 139       * Creates and renders a custom user heading.
 140       *
 141       * @param stdClass $user The user object.
 142       * @param int $courseid The course ID.
 143       * @param bool $showbuttons Whether to display buttons (message, add to contacts) within the heading.
 144       * @return string The raw HTML to render.
 145       */
 146      public function user_heading(stdClass $user, int $courseid, bool $showbuttons = true) : string {
 147          global $USER;
 148  
 149          $headingdata = [
 150              'userprofileurl' => (new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $courseid]))->out(false),
 151              'name' => fullname($user),
 152              'image' => $this->user_picture($user, ['size' => 50, 'link' => false])
 153          ];
 154  
 155          if ($showbuttons) {
 156              // Generate the data for the 'message' button.
 157              $messagelinkattributes = array_map(function($name, $value) {
 158                  return ['name' => $name, 'value' => $value];
 159              }, array_keys(helper::messageuser_link_params($user->id)), helper::messageuser_link_params($user->id));
 160              $messagelinkattributes[] = ['name' => 'class', 'value' => 'btn px-0'];
 161  
 162              $headingdata['buttons'][] = [
 163                  'title' => get_string('message', 'message'),
 164                  'url' => (new moodle_url('/message/index.php', ['id' => $user->id]))->out(false),
 165                  'icon' => ['name' => 't/message', 'component' => 'core'],
 166                  'linkattributes' => $messagelinkattributes
 167              ];
 168              // Include js for messaging.
 169              helper::messageuser_requirejs();
 170  
 171              if ($USER->id != $user->id) {
 172                  // Generate the data for the 'contact' button.
 173                  $iscontact = api::is_contact($USER->id, $user->id);
 174                  $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
 175                  $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
 176                  $contacticon = $iscontact ? 't/removecontact' : 't/addcontact';
 177  
 178                  $togglelinkparams = helper::togglecontact_link_params($user, $iscontact, false);
 179                  $togglecontactlinkattributes = array_map(function($name, $value) {
 180                      if ($name === 'class') {
 181                          $value .= ' btn px-0';
 182                      }
 183                      return ['name' => $name, 'value' => $value];
 184                  }, array_keys($togglelinkparams), $togglelinkparams);
 185  
 186                  $headingdata['buttons'][] = [
 187                      'title' => get_string($contacttitle, 'message'),
 188                      'url' => (new moodle_url('/message/index.php', ['user1' => $USER->id, 'user2' => $user->id,
 189                          $contacturlaction => $user->id, 'sesskey' => sesskey()]))->out(false),
 190                      'icon' => ['name' => $contacticon, 'component' => 'core'],
 191                      'linkattributes' => $togglecontactlinkattributes
 192                  ];
 193                  // Include js for contact toggle.
 194                  helper::togglecontact_requirejs();
 195              }
 196          }
 197  
 198          return $this->render_from_template('core_grades/user_heading', $headingdata);
 199      }
 200  }