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

Differences Between: [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  defined('MOODLE_INTERNAL') || die;
  18  
  19  use core\output\comboboxsearch;
  20  use \core_grades\output\action_bar;
  21  use core_message\helper;
  22  use core_message\api;
  23  
  24  /**
  25   * Renderer class for the grade pages.
  26   *
  27   * @package    core_grades
  28   * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class core_grades_renderer extends plugin_renderer_base {
  32  
  33      /**
  34       * Renders the action bar for a given page.
  35       *
  36       * @param action_bar $actionbar
  37       * @return string The HTML output
  38       */
  39      public function render_action_bar(action_bar $actionbar): string {
  40          $data = $actionbar->export_for_template($this);
  41          return $this->render_from_template($actionbar->get_template(), $data);
  42      }
  43  
  44      /**
  45       * Renders the group selector trigger element.
  46       *
  47       * @param object $course The course object.
  48       * @param string|null $groupactionbaseurl The base URL for the group action.
  49       * @return string|null The raw HTML to render.
  50       */
  51      public function group_selector(object $course, ?string $groupactionbaseurl = null): ?string {
  52          global $USER;
  53  
  54          // Make sure that group mode is enabled.
  55          if (!$groupmode = $course->groupmode) {
  56              return null;
  57          }
  58  
  59          $sbody = $this->render_from_template('core_group/comboboxsearch/searchbody', [
  60              'courseid' => $course->id,
  61              'currentvalue' => optional_param('groupsearchvalue', '', PARAM_NOTAGS),
  62          ]);
  63  
  64          $label = $groupmode == VISIBLEGROUPS ? get_string('selectgroupsvisible') :
  65              get_string('selectgroupsseparate');
  66  
  67          $data = [
  68              'name' => 'group',
  69              'label' => $label,
  70              'courseid' => $course->id,
  71              'groupactionbaseurl' => $groupactionbaseurl
  72          ];
  73  
  74          $context = context_course::instance($course->id);
  75  
  76          if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
  77              $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
  78          } else {
  79              $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
  80          }
  81  
  82          $activegroup = groups_get_course_group($course, true, $allowedgroups);
  83          $data['group'] = $activegroup;
  84  
  85          if ($activegroup) {
  86              $group = groups_get_group($activegroup);
  87              $data['selectedgroup'] = format_string($group->name, true, ['context' => $context]);
  88          } else if ($activegroup === 0) {
  89              $data['selectedgroup'] = get_string('allparticipants');
  90          }
  91  
  92          $groupdropdown = new comboboxsearch(
  93              false,
  94              $this->render_from_template('core_group/comboboxsearch/group_selector', $data),
  95              $sbody,
  96              'group-search',
  97              'groupsearchwidget',
  98              'groupsearchdropdown overflow-auto w-100',
  99          );
 100          return $this->render_from_template($groupdropdown->get_template(), $groupdropdown->export_for_template($this));
 101      }
 102  
 103      /**
 104       * Build the data to render the initials bar filter within the gradebook.
 105       * Using this initials selector means you'll have to retain the use of the templates & JS to handle form submission.
 106       * If a simple redirect on each selection is desired the standard user_search() within the user renderer is what you are after.
 107       *
 108       * @param object $course The course object.
 109       * @param context $context Our current context.
 110       * @param string $slug The slug for the report that called this function.
 111       * @return stdClass The data to output.
 112       */
 113      public function initials_selector(
 114          object $course,
 115          context $context,
 116          string $slug
 117      ): stdClass {
 118          global $SESSION, $COURSE;
 119          // User search.
 120          $searchvalue = optional_param('gpr_search', null, PARAM_NOTAGS);
 121          $userid = optional_param('grp_userid', null, PARAM_INT);
 122          $url = new moodle_url($slug, ['id' => $course->id]);
 123          $firstinitial = $SESSION->gradereport["filterfirstname-{$context->id}"] ?? '';
 124          $lastinitial  = $SESSION->gradereport["filtersurname-{$context->id}"] ?? '';
 125  
 126          $renderer = $this->page->get_renderer('core_user');
 127          $initialsbar = $renderer->partial_user_search($url, $firstinitial, $lastinitial, true);
 128  
 129          $currentfilter = '';
 130          if ($firstinitial !== '' && $lastinitial !== '') {
 131              $currentfilter = get_string('filterbothactive', 'grades', ['first' => $firstinitial, 'last' => $lastinitial]);
 132          } else if ($firstinitial !== '') {
 133              $currentfilter = get_string('filterfirstactive', 'grades', ['first' => $firstinitial]);
 134          } else if ($lastinitial !== '') {
 135              $currentfilter = get_string('filterlastactive', 'grades', ['last' => $lastinitial]);
 136          }
 137  
 138          $this->page->requires->js_call_amd('core_grades/searchwidget/initials', 'init', [$slug, $userid, $searchvalue]);
 139  
 140          $formdata = (object) [
 141              'courseid' => $COURSE->id,
 142              'initialsbars' => $initialsbar,
 143          ];
 144          $dropdowncontent = $this->render_from_template('core_grades/initials_dropdown_form', $formdata);
 145  
 146          return (object) [
 147               'buttoncontent' => $currentfilter !== '' ? $currentfilter : get_string('filterbyname', 'core_grades'),
 148               'buttonheader' => $currentfilter !== '' ? get_string('name') : null,
 149               'dropdowncontent' => $dropdowncontent,
 150          ];
 151      }
 152  
 153      /**
 154       * Creates and renders a custom user heading.
 155       *
 156       * @param stdClass $user The user object.
 157       * @param int $courseid The course ID.
 158       * @param bool $showbuttons Whether to display buttons (message, add to contacts) within the heading.
 159       * @return string The raw HTML to render.
 160       */
 161      public function user_heading(stdClass $user, int $courseid, bool $showbuttons = true) : string {
 162          global $USER;
 163  
 164          $headingdata = [
 165              'userprofileurl' => (new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $courseid]))->out(false),
 166              'name' => fullname($user),
 167              'image' => $this->user_picture($user, ['size' => 50, 'link' => false])
 168          ];
 169  
 170          if ($showbuttons) {
 171              // Generate the data for the 'message' button.
 172              $messagelinkattributes = array_map(function($name, $value) {
 173                  return ['name' => $name, 'value' => $value];
 174              }, array_keys(helper::messageuser_link_params($user->id)), helper::messageuser_link_params($user->id));
 175              $messagelinkattributes[] = ['name' => 'class', 'value' => 'btn px-0'];
 176  
 177              $headingdata['buttons'][] = [
 178                  'title' => get_string('message', 'message'),
 179                  'url' => (new moodle_url('/message/index.php', ['id' => $user->id]))->out(false),
 180                  'icon' => ['name' => 't/message', 'component' => 'core'],
 181                  'linkattributes' => $messagelinkattributes
 182              ];
 183              // Include js for messaging.
 184              helper::messageuser_requirejs();
 185  
 186              if ($USER->id != $user->id) {
 187                  // Generate the data for the 'contact' button.
 188                  $iscontact = api::is_contact($USER->id, $user->id);
 189                  $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
 190                  $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
 191                  $contacticon = $iscontact ? 't/removecontact' : 't/addcontact';
 192  
 193                  $togglelinkparams = helper::togglecontact_link_params($user, $iscontact, false);
 194                  $togglecontactlinkattributes = array_map(function($name, $value) {
 195                      if ($name === 'class') {
 196                          $value .= ' btn px-0';
 197                      }
 198                      return ['name' => $name, 'value' => $value];
 199                  }, array_keys($togglelinkparams), $togglelinkparams);
 200  
 201                  $headingdata['buttons'][] = [
 202                      'title' => get_string($contacttitle, 'message'),
 203                      'url' => (new moodle_url('/message/index.php', ['user1' => $USER->id, 'user2' => $user->id,
 204                          $contacturlaction => $user->id, 'sesskey' => sesskey()]))->out(false),
 205                      'icon' => ['name' => $contacticon, 'component' => 'core'],
 206                      'linkattributes' => $togglecontactlinkattributes
 207                  ];
 208                  // Include js for contact toggle.
 209                  helper::togglecontact_requirejs();
 210              }
 211          }
 212  
 213          return $this->render_from_template('core_grades/user_heading', $headingdata);
 214      }
 215  }