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 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  /**
  18   * Definition of the grade_user_report class is defined
  19   *
  20   * @package gradereport_user
  21   * @copyright 2007 Nicolas Connault
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use core_user\output\myprofile\tree;
  26  
  27  require_once($CFG->dirroot . '/grade/report/lib.php');
  28  require_once($CFG->libdir.'/tablelib.php');
  29  
  30  define("GRADE_REPORT_USER_HIDE_HIDDEN", 0);
  31  define("GRADE_REPORT_USER_HIDE_UNTIL", 1);
  32  define("GRADE_REPORT_USER_SHOW_HIDDEN", 2);
  33  
  34  define("GRADE_REPORT_USER_VIEW_SELF", 1);
  35  define("GRADE_REPORT_USER_VIEW_USER", 2);
  36  
  37  function grade_report_user_settings_definition(&$mform) {
  38      global $CFG;
  39  
  40      $options = [
  41          -1 => get_string('default', 'grades'),
  42          0 => get_string('hide'),
  43          1 => get_string('show')
  44      ];
  45  
  46      if (empty($CFG->grade_report_user_showrank)) {
  47          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
  48      } else {
  49          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
  50      }
  51  
  52      $mform->addElement('select', 'report_user_showrank', get_string('showrank', 'grades'), $options);
  53      $mform->addHelpButton('report_user_showrank', 'showrank', 'grades');
  54  
  55      if (empty($CFG->grade_report_user_showpercentage)) {
  56          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
  57      } else {
  58          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
  59      }
  60  
  61      $mform->addElement('select', 'report_user_showpercentage', get_string('showpercentage', 'grades'), $options);
  62      $mform->addHelpButton('report_user_showpercentage', 'showpercentage', 'grades');
  63  
  64      if (empty($CFG->grade_report_user_showgrade)) {
  65          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
  66      } else {
  67          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
  68      }
  69  
  70      $mform->addElement('select', 'report_user_showgrade', get_string('showgrade', 'grades'), $options);
  71  
  72      if (empty($CFG->grade_report_user_showfeedback)) {
  73          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
  74      } else {
  75          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
  76      }
  77  
  78      $mform->addElement('select', 'report_user_showfeedback', get_string('showfeedback', 'grades'), $options);
  79  
  80      if (empty($CFG->grade_report_user_showweight)) {
  81          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
  82      } else {
  83          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
  84      }
  85  
  86      $mform->addElement('select', 'report_user_showweight', get_string('showweight', 'grades'), $options);
  87  
  88      if (empty($CFG->grade_report_user_showaverage)) {
  89          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
  90      } else {
  91          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
  92      }
  93  
  94      $mform->addElement('select', 'report_user_showaverage', get_string('showaverage', 'grades'), $options);
  95      $mform->addHelpButton('report_user_showaverage', 'showaverage', 'grades');
  96  
  97      if (empty($CFG->grade_report_user_showlettergrade)) {
  98          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
  99      } else {
 100          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
 101      }
 102  
 103      $mform->addElement('select', 'report_user_showlettergrade', get_string('showlettergrade', 'grades'), $options);
 104      if (empty($CFG->grade_report_user_showcontributiontocoursetotal)) {
 105          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
 106      } else {
 107          $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showcontributiontocoursetotal]);
 108      }
 109  
 110      $mform->addElement('select', 'report_user_showcontributiontocoursetotal', get_string('showcontributiontocoursetotal', 'grades'), $options);
 111      $mform->addHelpButton('report_user_showcontributiontocoursetotal', 'showcontributiontocoursetotal', 'grades');
 112  
 113      if (empty($CFG->grade_report_user_showrange)) {
 114          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
 115      } else {
 116          $options[-1] = get_string('defaultprev', 'grades', $options[1]);
 117      }
 118  
 119      $mform->addElement('select', 'report_user_showrange', get_string('showrange', 'grades'), $options);
 120  
 121      $options = [
 122          0 => 0,
 123          1 => 1,
 124          2 => 2,
 125          3 => 3,
 126          4 => 4,
 127          5 => 5
 128      ];
 129  
 130      if (!empty($CFG->grade_report_user_rangedecimals)) {
 131          $options[-1] = $options[$CFG->grade_report_user_rangedecimals];
 132      }
 133      $mform->addElement('select', 'report_user_rangedecimals', get_string('rangedecimals', 'grades'), $options);
 134  
 135      $options = [
 136          -1 => get_string('default', 'grades'),
 137          0 => get_string('shownohidden', 'grades'),
 138          1 => get_string('showhiddenuntilonly', 'grades'),
 139          2 => get_string('showallhidden', 'grades')
 140      ];
 141  
 142      if (empty($CFG->grade_report_user_showhiddenitems)) {
 143          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
 144      } else {
 145          $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showhiddenitems]);
 146      }
 147  
 148      $mform->addElement('select', 'report_user_showhiddenitems', get_string('showhiddenitems', 'grades'), $options);
 149      $mform->addHelpButton('report_user_showhiddenitems', 'showhiddenitems', 'grades');
 150  
 151      $options = [
 152          -1 => get_string('default', 'grades'),
 153          GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'),
 154          GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'),
 155          GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades')
 156      ];
 157  
 158      if (empty($CFG->grade_report_user_showtotalsifcontainhidden)) {
 159          $options[-1] = get_string('defaultprev', 'grades', $options[0]);
 160      } else {
 161          $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showtotalsifcontainhidden]);
 162      }
 163  
 164      $mform->addElement('select', 'report_user_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options);
 165      $mform->addHelpButton('report_user_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades');
 166  
 167  }
 168  
 169  /**
 170   * Profile report callback.
 171   *
 172   * @param object $course The course.
 173   * @param object $user The user.
 174   * @param boolean $viewasuser True when we are viewing this as the targetted user sees it.
 175   */
 176  function grade_report_user_profilereport(object $course, object $user, bool $viewasuser = false) {
 177      if (!empty($course->showgrades)) {
 178  
 179          $context = context_course::instance($course->id);
 180  
 181          // Fetch the return tracking object.
 182          $gpr = new grade_plugin_return(
 183              ['type' => 'report', 'plugin' => 'user', 'courseid' => $course->id, 'userid' => $user->id]
 184          );
 185          // Create a report instance.
 186          $report = new gradereport_user\report\user($course->id, $gpr, $context, $user->id, $viewasuser);
 187  
 188          // Print the page.
 189          // A css fix to share styles with real report page.
 190          echo '<div class="grade-report-user">';
 191          if ($report->fill_table()) {
 192              echo $report->print_table(true);
 193          }
 194          echo '</div>';
 195      }
 196  }
 197  
 198  /**
 199   * Add nodes to myprofile page.
 200   *
 201   * @param tree $tree Tree object
 202   * @param stdClass $user user object
 203   * @param bool $iscurrentuser
 204   * @param null|stdClass $course Course object
 205   */
 206  function gradereport_user_myprofile_navigation(tree $tree, stdClass $user, bool $iscurrentuser, ?stdClass $course) {
 207      if (empty($course)) {
 208          // We want to display these reports under the site context.
 209          $course = get_fast_modinfo(SITEID)->get_course();
 210      }
 211      $usercontext = context_user::instance($user->id);
 212      $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext);
 213  
 214      // Start capability checks.
 215      if ($anyreport || $iscurrentuser) {
 216          // Add grade hardcoded grade report if necessary.
 217          $gradeaccess = false;
 218          $coursecontext = context_course::instance($course->id);
 219          if (has_capability('moodle/grade:viewall', $coursecontext)) {
 220              // Can view all course grades.
 221              $gradeaccess = true;
 222          } else if ($course->showgrades) {
 223              if ($iscurrentuser && has_capability('moodle/grade:view', $coursecontext)) {
 224                  // Can view own grades.
 225                  $gradeaccess = true;
 226              } else if (has_capability('moodle/grade:viewall', $usercontext)) {
 227                  // Can view grades of this user - parent most probably.
 228                  $gradeaccess = true;
 229              } else if ($anyreport) {
 230                  // Can view grades of this user - parent most probably.
 231                  $gradeaccess = true;
 232              }
 233          }
 234          if ($gradeaccess) {
 235              $url = new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $user->id));
 236              $node = new core_user\output\myprofile\node('reports', 'grade', get_string('grades'), null, $url);
 237              $tree->add_node($node);
 238          }
 239      }
 240  }