Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/grade/ -> lib.php (source)

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * Functions used by gradebook plugins and reports.
  19   *
  20   * @package   core_grades
  21   * @copyright 2009 Petr Skoda and Nicolas Connault
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once($CFG->libdir . '/gradelib.php');
  26  require_once($CFG->dirroot . '/grade/export/lib.php');
  27  
  28  /**
  29   * This class iterates over all users that are graded in a course.
  30   * Returns detailed info about users and their grades.
  31   *
  32   * @author Petr Skoda <skodak@moodle.org>
  33   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class graded_users_iterator {
  36  
  37      /**
  38       * The couse whose users we are interested in
  39       */
  40      protected $course;
  41  
  42      /**
  43       * An array of grade items or null if only user data was requested
  44       */
  45      protected $grade_items;
  46  
  47      /**
  48       * The group ID we are interested in. 0 means all groups.
  49       */
  50      protected $groupid;
  51  
  52      /**
  53       * A recordset of graded users
  54       */
  55      protected $users_rs;
  56  
  57      /**
  58       * A recordset of user grades (grade_grade instances)
  59       */
  60      protected $grades_rs;
  61  
  62      /**
  63       * Array used when moving to next user while iterating through the grades recordset
  64       */
  65      protected $gradestack;
  66  
  67      /**
  68       * The first field of the users table by which the array of users will be sorted
  69       */
  70      protected $sortfield1;
  71  
  72      /**
  73       * Should sortfield1 be ASC or DESC
  74       */
  75      protected $sortorder1;
  76  
  77      /**
  78       * The second field of the users table by which the array of users will be sorted
  79       */
  80      protected $sortfield2;
  81  
  82      /**
  83       * Should sortfield2 be ASC or DESC
  84       */
  85      protected $sortorder2;
  86  
  87      /**
  88       * Should users whose enrolment has been suspended be ignored?
  89       */
  90      protected $onlyactive = false;
  91  
  92      /**
  93       * Enable user custom fields
  94       */
  95      protected $allowusercustomfields = false;
  96  
  97      /**
  98       * List of suspended users in course. This includes users whose enrolment status is suspended
  99       * or enrolment has expired or not started.
 100       */
 101      protected $suspendedusers = array();
 102  
 103      /**
 104       * Constructor
 105       *
 106       * @param object $course A course object
 107       * @param array  $grade_items array of grade items, if not specified only user info returned
 108       * @param int    $groupid iterate only group users if present
 109       * @param string $sortfield1 The first field of the users table by which the array of users will be sorted
 110       * @param string $sortorder1 The order in which the first sorting field will be sorted (ASC or DESC)
 111       * @param string $sortfield2 The second field of the users table by which the array of users will be sorted
 112       * @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC)
 113       */
 114      public function __construct($course, $grade_items=null, $groupid=0,
 115                                            $sortfield1='lastname', $sortorder1='ASC',
 116                                            $sortfield2='firstname', $sortorder2='ASC') {
 117          $this->course      = $course;
 118          $this->grade_items = $grade_items;
 119          $this->groupid     = $groupid;
 120          $this->sortfield1  = $sortfield1;
 121          $this->sortorder1  = $sortorder1;
 122          $this->sortfield2  = $sortfield2;
 123          $this->sortorder2  = $sortorder2;
 124  
 125          $this->gradestack  = array();
 126      }
 127  
 128      /**
 129       * Initialise the iterator
 130       *
 131       * @return boolean success
 132       */
 133      public function init() {
 134          global $CFG, $DB;
 135  
 136          $this->close();
 137  
 138          export_verify_grades($this->course->id);
 139          $course_item = grade_item::fetch_course_item($this->course->id);
 140          if ($course_item->needsupdate) {
 141              // Can not calculate all final grades - sorry.
 142              return false;
 143          }
 144  
 145          $coursecontext = context_course::instance($this->course->id);
 146  
 147          list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
 148          list($gradebookroles_sql, $params) = $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr');
 149          list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, '', 0, $this->onlyactive);
 150  
 151          $params = array_merge($params, $enrolledparams, $relatedctxparams);
 152  
 153          if ($this->groupid) {
 154              $groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id";
 155              $groupwheresql = "AND gm.groupid = :groupid";
 156              // $params contents: gradebookroles
 157              $params['groupid'] = $this->groupid;
 158          } else {
 159              $groupsql = "";
 160              $groupwheresql = "";
 161          }
 162  
 163          if (empty($this->sortfield1)) {
 164              // We must do some sorting even if not specified.
 165              $ofields = ", u.id AS usrt";
 166              $order   = "usrt ASC";
 167  
 168          } else {
 169              $ofields = ", u.$this->sortfield1 AS usrt1";
 170              $order   = "usrt1 $this->sortorder1";
 171              if (!empty($this->sortfield2)) {
 172                  $ofields .= ", u.$this->sortfield2 AS usrt2";
 173                  $order   .= ", usrt2 $this->sortorder2";
 174              }
 175              if ($this->sortfield1 != 'id' and $this->sortfield2 != 'id') {
 176                  // User order MUST be the same in both queries,
 177                  // must include the only unique user->id if not already present.
 178                  $ofields .= ", u.id AS usrt";
 179                  $order   .= ", usrt ASC";
 180              }
 181          }
 182  
 183          $userfields = 'u.*';
 184          $customfieldssql = '';
 185          if ($this->allowusercustomfields && !empty($CFG->grade_export_customprofilefields)) {
 186              $customfieldscount = 0;
 187              $customfieldsarray = grade_helper::get_user_profile_fields($this->course->id, $this->allowusercustomfields);
 188              foreach ($customfieldsarray as $field) {
 189                  if (!empty($field->customid)) {
 190                      $customfieldssql .= "
 191                              LEFT JOIN (SELECT * FROM {user_info_data}
 192                                  WHERE fieldid = :cf$customfieldscount) cf$customfieldscount
 193                              ON u.id = cf$customfieldscount.userid";
 194                      $userfields .= ", cf$customfieldscount.data AS customfield_{$field->customid}";
 195                      $params['cf'.$customfieldscount] = $field->customid;
 196                      $customfieldscount++;
 197                  }
 198              }
 199          }
 200  
 201          $users_sql = "SELECT $userfields $ofields
 202                          FROM {user} u
 203                          JOIN ($enrolledsql) je ON je.id = u.id
 204                               $groupsql $customfieldssql
 205                          JOIN (
 206                                    SELECT DISTINCT ra.userid
 207                                      FROM {role_assignments} ra
 208                                     WHERE ra.roleid $gradebookroles_sql
 209                                       AND ra.contextid $relatedctxsql
 210                               ) rainner ON rainner.userid = u.id
 211                           WHERE u.deleted = 0
 212                               $groupwheresql
 213                      ORDER BY $order";
 214          $this->users_rs = $DB->get_recordset_sql($users_sql, $params);
 215  
 216          if (!$this->onlyactive) {
 217              $context = context_course::instance($this->course->id);
 218              $this->suspendedusers = get_suspended_userids($context);
 219          } else {
 220              $this->suspendedusers = array();
 221          }
 222  
 223          if (!empty($this->grade_items)) {
 224              $itemids = array_keys($this->grade_items);
 225              list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items');
 226              $params = array_merge($params, $grades_params);
 227  
 228              $grades_sql = "SELECT g.* $ofields
 229                               FROM {grade_grades} g
 230                               JOIN {user} u ON g.userid = u.id
 231                               JOIN ($enrolledsql) je ON je.id = u.id
 232                                    $groupsql
 233                               JOIN (
 234                                        SELECT DISTINCT ra.userid
 235                                          FROM {role_assignments} ra
 236                                         WHERE ra.roleid $gradebookroles_sql
 237                                           AND ra.contextid $relatedctxsql
 238                                    ) rainner ON rainner.userid = u.id
 239                                WHERE u.deleted = 0
 240                                AND g.itemid $itemidsql
 241                                $groupwheresql
 242                           ORDER BY $order, g.itemid ASC";
 243              $this->grades_rs = $DB->get_recordset_sql($grades_sql, $params);
 244          } else {
 245              $this->grades_rs = false;
 246          }
 247  
 248          return true;
 249      }
 250  
 251      /**
 252       * Returns information about the next user
 253       * @return mixed array of user info, all grades and feedback or null when no more users found
 254       */
 255      public function next_user() {
 256          if (!$this->users_rs) {
 257              return false; // no users present
 258          }
 259  
 260          if (!$this->users_rs->valid()) {
 261              if ($current = $this->_pop()) {
 262                  // this is not good - user or grades updated between the two reads above :-(
 263              }
 264  
 265              return false; // no more users
 266          } else {
 267              $user = $this->users_rs->current();
 268              $this->users_rs->next();
 269          }
 270  
 271          // find grades of this user
 272          $grade_records = array();
 273          while (true) {
 274              if (!$current = $this->_pop()) {
 275                  break; // no more grades
 276              }
 277  
 278              if (empty($current->userid)) {
 279                  break;
 280              }
 281  
 282              if ($current->userid != $user->id) {
 283                  // grade of the next user, we have all for this user
 284                  $this->_push($current);
 285                  break;
 286              }
 287  
 288              $grade_records[$current->itemid] = $current;
 289          }
 290  
 291          $grades = array();
 292          $feedbacks = array();
 293  
 294          if (!empty($this->grade_items)) {
 295              foreach ($this->grade_items as $grade_item) {
 296                  if (!isset($feedbacks[$grade_item->id])) {
 297                      $feedbacks[$grade_item->id] = new stdClass();
 298                  }
 299                  if (array_key_exists($grade_item->id, $grade_records)) {
 300                      $feedbacks[$grade_item->id]->feedback       = $grade_records[$grade_item->id]->feedback;
 301                      $feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat;
 302                      unset($grade_records[$grade_item->id]->feedback);
 303                      unset($grade_records[$grade_item->id]->feedbackformat);
 304                      $grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false);
 305                  } else {
 306                      $feedbacks[$grade_item->id]->feedback       = '';
 307                      $feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE;
 308                      $grades[$grade_item->id] =
 309                          new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false);
 310                  }
 311                  $grades[$grade_item->id]->grade_item = $grade_item;
 312              }
 313          }
 314  
 315          // Set user suspended status.
 316          $user->suspendedenrolment = isset($this->suspendedusers[$user->id]);
 317          $result = new stdClass();
 318          $result->user      = $user;
 319          $result->grades    = $grades;
 320          $result->feedbacks = $feedbacks;
 321          return $result;
 322      }
 323  
 324      /**
 325       * Close the iterator, do not forget to call this function
 326       */
 327      public function close() {
 328          if ($this->users_rs) {
 329              $this->users_rs->close();
 330              $this->users_rs = null;
 331          }
 332          if ($this->grades_rs) {
 333              $this->grades_rs->close();
 334              $this->grades_rs = null;
 335          }
 336          $this->gradestack = array();
 337      }
 338  
 339      /**
 340       * Should all enrolled users be exported or just those with an active enrolment?
 341       *
 342       * @param bool $onlyactive True to limit the export to users with an active enrolment
 343       */
 344      public function require_active_enrolment($onlyactive = true) {
 345          if (!empty($this->users_rs)) {
 346              debugging('Calling require_active_enrolment() has no effect unless you call init() again', DEBUG_DEVELOPER);
 347          }
 348          $this->onlyactive  = $onlyactive;
 349      }
 350  
 351      /**
 352       * Allow custom fields to be included
 353       *
 354       * @param bool $allow Whether to allow custom fields or not
 355       * @return void
 356       */
 357      public function allow_user_custom_fields($allow = true) {
 358          if ($allow) {
 359              $this->allowusercustomfields = true;
 360          } else {
 361              $this->allowusercustomfields = false;
 362          }
 363      }
 364  
 365      /**
 366       * Add a grade_grade instance to the grade stack
 367       *
 368       * @param grade_grade $grade Grade object
 369       *
 370       * @return void
 371       */
 372      private function _push($grade) {
 373          array_push($this->gradestack, $grade);
 374      }
 375  
 376  
 377      /**
 378       * Remove a grade_grade instance from the grade stack
 379       *
 380       * @return grade_grade current grade object
 381       */
 382      private function _pop() {
 383          global $DB;
 384          if (empty($this->gradestack)) {
 385              if (empty($this->grades_rs) || !$this->grades_rs->valid()) {
 386                  return null; // no grades present
 387              }
 388  
 389              $current = $this->grades_rs->current();
 390  
 391              $this->grades_rs->next();
 392  
 393              return $current;
 394          } else {
 395              return array_pop($this->gradestack);
 396          }
 397      }
 398  }
 399  
 400  /**
 401   * Print a selection popup form of the graded users in a course.
 402   *
 403   * @deprecated since 2.0
 404   *
 405   * @param int    $course id of the course
 406   * @param string $actionpage The page receiving the data from the popoup form
 407   * @param int    $userid   id of the currently selected user (or 'all' if they are all selected)
 408   * @param int    $groupid id of requested group, 0 means all
 409   * @param int    $includeall bool include all option
 410   * @param bool   $return If true, will return the HTML, otherwise, will print directly
 411   * @return null
 412   */
 413  function print_graded_users_selector($course, $actionpage, $userid=0, $groupid=0, $includeall=true, $return=false) {
 414      global $CFG, $USER, $OUTPUT;
 415      return $OUTPUT->render(grade_get_graded_users_select(substr($actionpage, 0, strpos($actionpage, '/')), $course, $userid, $groupid, $includeall));
 416  }
 417  
 418  function grade_get_graded_users_select($report, $course, $userid, $groupid, $includeall) {
 419      global $USER, $CFG;
 420  
 421      if (is_null($userid)) {
 422          $userid = $USER->id;
 423      }
 424      $coursecontext = context_course::instance($course->id);
 425      $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
 426      $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
 427      $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
 428      $menu = array(); // Will be a list of userid => user name
 429      $menususpendedusers = array(); // Suspended users go to a separate optgroup.
 430      $gui = new graded_users_iterator($course, null, $groupid);
 431      $gui->require_active_enrolment($showonlyactiveenrol);
 432      $gui->init();
 433      $label = get_string('selectauser', 'grades');
 434      if ($includeall) {
 435          $menu[0] = get_string('allusers', 'grades');
 436          $label = get_string('selectalloroneuser', 'grades');
 437      }
 438      while ($userdata = $gui->next_user()) {
 439          $user = $userdata->user;
 440          $userfullname = fullname($user);
 441          if ($user->suspendedenrolment) {
 442              $menususpendedusers[$user->id] = $userfullname;
 443          } else {
 444              $menu[$user->id] = $userfullname;
 445          }
 446      }
 447      $gui->close();
 448  
 449      if ($includeall) {
 450          $menu[0] .= " (" . (count($menu) + count($menususpendedusers) - 1) . ")";
 451      }
 452  
 453      if (!empty($menususpendedusers)) {
 454          $menu[] = array(get_string('suspendedusers') => $menususpendedusers);
 455      }
 456      $gpr = new grade_plugin_return(array('type' => 'report', 'course' => $course, 'groupid' => $groupid));
 457      $select = new single_select(
 458          new moodle_url('/grade/report/'.$report.'/index.php', $gpr->get_options()),
 459          'userid', $menu, $userid
 460      );
 461      $select->label = $label;
 462      $select->formid = 'choosegradeuser';
 463      return $select;
 464  }
 465  
 466  /**
 467   * Hide warning about changed grades during upgrade to 2.8.
 468   *
 469   * @param int $courseid The current course id.
 470   */
 471  function hide_natural_aggregation_upgrade_notice($courseid) {
 472      unset_config('show_sumofgrades_upgrade_' . $courseid);
 473  }
 474  
 475  /**
 476   * Hide warning about changed grades during upgrade from 2.8.0-2.8.6 and 2.9.0.
 477   *
 478   * @param int $courseid The current course id.
 479   */
 480  function grade_hide_min_max_grade_upgrade_notice($courseid) {
 481      unset_config('show_min_max_grades_changed_' . $courseid);
 482  }
 483  
 484  /**
 485   * Use the grade min and max from the grade_grade.
 486   *
 487   * This is reserved for core use after an upgrade.
 488   *
 489   * @param int $courseid The current course id.
 490   */
 491  function grade_upgrade_use_min_max_from_grade_grade($courseid) {
 492      grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_GRADE);
 493  
 494      grade_force_full_regrading($courseid);
 495      // Do this now, because it probably happened to late in the page load to be happen automatically.
 496      grade_regrade_final_grades($courseid);
 497  }
 498  
 499  /**
 500   * Use the grade min and max from the grade_item.
 501   *
 502   * This is reserved for core use after an upgrade.
 503   *
 504   * @param int $courseid The current course id.
 505   */
 506  function grade_upgrade_use_min_max_from_grade_item($courseid) {
 507      grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM);
 508  
 509      grade_force_full_regrading($courseid);
 510      // Do this now, because it probably happened to late in the page load to be happen automatically.
 511      grade_regrade_final_grades($courseid);
 512  }
 513  
 514  /**
 515   * Hide warning about changed grades during upgrade to 2.8.
 516   *
 517   * @param int $courseid The current course id.
 518   */
 519  function hide_aggregatesubcats_upgrade_notice($courseid) {
 520      unset_config('show_aggregatesubcats_upgrade_' . $courseid);
 521  }
 522  
 523  /**
 524   * Hide warning about changed grades due to bug fixes
 525   *
 526   * @param int $courseid The current course id.
 527   */
 528  function hide_gradebook_calculations_freeze_notice($courseid) {
 529      unset_config('gradebook_calculations_freeze_' . $courseid);
 530  }
 531  
 532  /**
 533   * Print warning about changed grades during upgrade to 2.8.
 534   *
 535   * @param int $courseid The current course id.
 536   * @param context $context The course context.
 537   * @param string $thispage The relative path for the current page. E.g. /grade/report/user/index.php
 538   * @param boolean $return return as string
 539   *
 540   * @return nothing or string if $return true
 541   */
 542  function print_natural_aggregation_upgrade_notice($courseid, $context, $thispage, $return=false) {
 543      global $CFG, $OUTPUT;
 544      $html = '';
 545  
 546      // Do not do anything if they cannot manage the grades of this course.
 547      if (!has_capability('moodle/grade:manage', $context)) {
 548          return $html;
 549      }
 550  
 551      $hidesubcatswarning = optional_param('seenaggregatesubcatsupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
 552      $showsubcatswarning = get_config('core', 'show_aggregatesubcats_upgrade_' . $courseid);
 553      $hidenaturalwarning = optional_param('seensumofgradesupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
 554      $shownaturalwarning = get_config('core', 'show_sumofgrades_upgrade_' . $courseid);
 555  
 556      $hideminmaxwarning = optional_param('seenminmaxupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
 557      $showminmaxwarning = get_config('core', 'show_min_max_grades_changed_' . $courseid);
 558  
 559      $useminmaxfromgradeitem = optional_param('useminmaxfromgradeitem', false, PARAM_BOOL) && confirm_sesskey();
 560      $useminmaxfromgradegrade = optional_param('useminmaxfromgradegrade', false, PARAM_BOOL) && confirm_sesskey();
 561  
 562      $minmaxtouse = grade_get_setting($courseid, 'minmaxtouse', $CFG->grade_minmaxtouse);
 563  
 564      $gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
 565      $acceptgradebookchanges = optional_param('acceptgradebookchanges', false, PARAM_BOOL) && confirm_sesskey();
 566  
 567      // Hide the warning if the user told it to go away.
 568      if ($hidenaturalwarning) {
 569          hide_natural_aggregation_upgrade_notice($courseid);
 570      }
 571      // Hide the warning if the user told it to go away.
 572      if ($hidesubcatswarning) {
 573          hide_aggregatesubcats_upgrade_notice($courseid);
 574      }
 575  
 576      // Hide the min/max warning if the user told it to go away.
 577      if ($hideminmaxwarning) {
 578          grade_hide_min_max_grade_upgrade_notice($courseid);
 579          $showminmaxwarning = false;
 580      }
 581  
 582      if ($useminmaxfromgradegrade) {
 583          // Revert to the new behaviour, we now use the grade_grade for min/max.
 584          grade_upgrade_use_min_max_from_grade_grade($courseid);
 585          grade_hide_min_max_grade_upgrade_notice($courseid);
 586          $showminmaxwarning = false;
 587  
 588      } else if ($useminmaxfromgradeitem) {
 589          // Apply the new logic, we now use the grade_item for min/max.
 590          grade_upgrade_use_min_max_from_grade_item($courseid);
 591          grade_hide_min_max_grade_upgrade_notice($courseid);
 592          $showminmaxwarning = false;
 593      }
 594  
 595  
 596      if (!$hidenaturalwarning && $shownaturalwarning) {
 597          $message = get_string('sumofgradesupgradedgrades', 'grades');
 598          $hidemessage = get_string('upgradedgradeshidemessage', 'grades');
 599          $urlparams = array( 'id' => $courseid,
 600                              'seensumofgradesupgradedgrades' => true,
 601                              'sesskey' => sesskey());
 602          $goawayurl = new moodle_url($thispage, $urlparams);
 603          $goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
 604          $html .= $OUTPUT->notification($message, 'notifysuccess');
 605          $html .= $goawaybutton;
 606      }
 607  
 608      if (!$hidesubcatswarning && $showsubcatswarning) {
 609          $message = get_string('aggregatesubcatsupgradedgrades', 'grades');
 610          $hidemessage = get_string('upgradedgradeshidemessage', 'grades');
 611          $urlparams = array( 'id' => $courseid,
 612                              'seenaggregatesubcatsupgradedgrades' => true,
 613                              'sesskey' => sesskey());
 614          $goawayurl = new moodle_url($thispage, $urlparams);
 615          $goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
 616          $html .= $OUTPUT->notification($message, 'notifysuccess');
 617          $html .= $goawaybutton;
 618      }
 619  
 620      if ($showminmaxwarning) {
 621          $hidemessage = get_string('upgradedgradeshidemessage', 'grades');
 622          $urlparams = array( 'id' => $courseid,
 623                              'seenminmaxupgradedgrades' => true,
 624                              'sesskey' => sesskey());
 625  
 626          $goawayurl = new moodle_url($thispage, $urlparams);
 627          $hideminmaxbutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
 628          $moreinfo = html_writer::link(get_docs_url(get_string('minmaxtouse_link', 'grades')), get_string('moreinfo'),
 629              array('target' => '_blank'));
 630  
 631          if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_ITEM) {
 632              // Show the message that there were min/max issues that have been resolved.
 633              $message = get_string('minmaxupgradedgrades', 'grades') . ' ' . $moreinfo;
 634  
 635              $revertmessage = get_string('upgradedminmaxrevertmessage', 'grades');
 636              $urlparams = array('id' => $courseid,
 637                                 'useminmaxfromgradegrade' => true,
 638                                 'sesskey' => sesskey());
 639              $reverturl = new moodle_url($thispage, $urlparams);
 640              $revertbutton = $OUTPUT->single_button($reverturl, $revertmessage, 'get');
 641  
 642              $html .= $OUTPUT->notification($message);
 643              $html .= $revertbutton . $hideminmaxbutton;
 644  
 645          } else if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_GRADE) {
 646              // Show the warning that there are min/max issues that have not be resolved.
 647              $message = get_string('minmaxupgradewarning', 'grades') . ' ' . $moreinfo;
 648  
 649              $fixmessage = get_string('minmaxupgradefixbutton', 'grades');
 650              $urlparams = array('id' => $courseid,
 651                                 'useminmaxfromgradeitem' => true,
 652                                 'sesskey' => sesskey());
 653              $fixurl = new moodle_url($thispage, $urlparams);
 654              $fixbutton = $OUTPUT->single_button($fixurl, $fixmessage, 'get');
 655  
 656              $html .= $OUTPUT->notification($message);
 657              $html .= $fixbutton . $hideminmaxbutton;
 658          }
 659      }
 660  
 661      if ($gradebookcalculationsfreeze) {
 662          if ($acceptgradebookchanges) {
 663              // Accept potential changes in grades caused by extra credit bug MDL-49257.
 664              hide_gradebook_calculations_freeze_notice($courseid);
 665              $courseitem = grade_item::fetch_course_item($courseid);
 666              $courseitem->force_regrading();
 667              grade_regrade_final_grades($courseid);
 668  
 669              $html .= $OUTPUT->notification(get_string('gradebookcalculationsuptodate', 'grades'), 'notifysuccess');
 670          } else {
 671              // Show the warning that there may be extra credit weights problems.
 672              $a = new stdClass();
 673              $a->gradebookversion = $gradebookcalculationsfreeze;
 674              if (preg_match('/(\d{8,})/', $CFG->release, $matches)) {
 675                  $a->currentversion = $matches[1];
 676              } else {
 677                  $a->currentversion = $CFG->release;
 678              }
 679              $a->url = get_docs_url('Gradebook_calculation_changes');
 680              $message = get_string('gradebookcalculationswarning', 'grades', $a);
 681  
 682              $fixmessage = get_string('gradebookcalculationsfixbutton', 'grades');
 683              $urlparams = array('id' => $courseid,
 684                  'acceptgradebookchanges' => true,
 685                  'sesskey' => sesskey());
 686              $fixurl = new moodle_url($thispage, $urlparams);
 687              $fixbutton = $OUTPUT->single_button($fixurl, $fixmessage, 'get');
 688  
 689              $html .= $OUTPUT->notification($message);
 690              $html .= $fixbutton;
 691          }
 692      }
 693  
 694      if (!empty($html)) {
 695          $html = html_writer::tag('div', $html, array('class' => 'core_grades_notices'));
 696      }
 697  
 698      if ($return) {
 699          return $html;
 700      } else {
 701          echo $html;
 702      }
 703  }
 704  
 705  /**
 706   * Print grading plugin selection popup form.
 707   *
 708   * @param array   $plugin_info An array of plugins containing information for the selector
 709   * @param boolean $return return as string
 710   *
 711   * @return nothing or string if $return true
 712   */
 713  function print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return=false) {
 714      global $CFG, $OUTPUT, $PAGE;
 715  
 716      $menu = array();
 717      $count = 0;
 718      $active = '';
 719  
 720      foreach ($plugin_info as $plugin_type => $plugins) {
 721          if ($plugin_type == 'strings') {
 722              continue;
 723          }
 724  
 725          $first_plugin = reset($plugins);
 726  
 727          $sectionname = $plugin_info['strings'][$plugin_type];
 728          $section = array();
 729  
 730          foreach ($plugins as $plugin) {
 731              $link = $plugin->link->out(false);
 732              $section[$link] = $plugin->string;
 733              $count++;
 734              if ($plugin_type === $active_type and $plugin->id === $active_plugin) {
 735                  $active = $link;
 736              }
 737          }
 738  
 739          if ($section) {
 740              $menu[] = array($sectionname=>$section);
 741          }
 742      }
 743  
 744      // finally print/return the popup form
 745      if ($count > 1) {
 746          $select = new url_select($menu, $active, null, 'choosepluginreport');
 747          $select->set_label(get_string('gradereport', 'grades'), array('class' => 'accesshide'));
 748          if ($return) {
 749              return $OUTPUT->render($select);
 750          } else {
 751              echo $OUTPUT->render($select);
 752          }
 753      } else {
 754          // only one option - no plugin selector needed
 755          return '';
 756      }
 757  }
 758  
 759  /**
 760   * Print grading plugin selection tab-based navigation.
 761   *
 762   * @param string  $active_type type of plugin on current page - import, export, report or edit
 763   * @param string  $active_plugin active plugin type - grader, user, cvs, ...
 764   * @param array   $plugin_info Array of plugins
 765   * @param boolean $return return as string
 766   *
 767   * @return nothing or string if $return true
 768   */
 769  function grade_print_tabs($active_type, $active_plugin, $plugin_info, $return=false) {
 770      global $CFG, $COURSE;
 771  
 772      if (!isset($currenttab)) { //TODO: this is weird
 773          $currenttab = '';
 774      }
 775  
 776      $tabs = array();
 777      $top_row  = array();
 778      $bottom_row = array();
 779      $inactive = array($active_plugin);
 780      $activated = array($active_type);
 781  
 782      $count = 0;
 783      $active = '';
 784  
 785      foreach ($plugin_info as $plugin_type => $plugins) {
 786          if ($plugin_type == 'strings') {
 787              continue;
 788          }
 789  
 790          // If $plugins is actually the definition of a child-less parent link:
 791          if (!empty($plugins->id)) {
 792              $string = $plugins->string;
 793              if (!empty($plugin_info[$active_type]->parent)) {
 794                  $string = $plugin_info[$active_type]->parent->string;
 795              }
 796  
 797              $top_row[] = new tabobject($plugin_type, $plugins->link, $string);
 798              continue;
 799          }
 800  
 801          $first_plugin = reset($plugins);
 802          $url = $first_plugin->link;
 803  
 804          if ($plugin_type == 'report') {
 805              $url = $CFG->wwwroot.'/grade/report/index.php?id='.$COURSE->id;
 806          }
 807  
 808          $top_row[] = new tabobject($plugin_type, $url, $plugin_info['strings'][$plugin_type]);
 809  
 810          if ($active_type == $plugin_type) {
 811              foreach ($plugins as $plugin) {
 812                  $bottom_row[] = new tabobject($plugin->id, $plugin->link, $plugin->string);
 813                  if ($plugin->id == $active_plugin) {
 814                      $inactive = array($plugin->id);
 815                  }
 816              }
 817          }
 818      }
 819  
 820      // Do not display rows that contain only one item, they are not helpful.
 821      if (count($top_row) > 1) {
 822          $tabs[] = $top_row;
 823      }
 824      if (count($bottom_row) > 1) {
 825          $tabs[] = $bottom_row;
 826      }
 827      if (empty($tabs)) {
 828          return;
 829      }
 830  
 831      $rv = html_writer::div(print_tabs($tabs, $active_plugin, $inactive, $activated, true), 'grade-navigation');
 832  
 833      if ($return) {
 834          return $rv;
 835      } else {
 836          echo $rv;
 837      }
 838  }
 839  
 840  /**
 841   * grade_get_plugin_info
 842   *
 843   * @param int    $courseid The course id
 844   * @param string $active_type type of plugin on current page - import, export, report or edit
 845   * @param string $active_plugin active plugin type - grader, user, cvs, ...
 846   *
 847   * @return array
 848   */
 849  function grade_get_plugin_info($courseid, $active_type, $active_plugin) {
 850      global $CFG, $SITE;
 851  
 852      $context = context_course::instance($courseid);
 853  
 854      $plugin_info = array();
 855      $count = 0;
 856      $active = '';
 857      $url_prefix = $CFG->wwwroot . '/grade/';
 858  
 859      // Language strings
 860      $plugin_info['strings'] = grade_helper::get_plugin_strings();
 861  
 862      if ($reports = grade_helper::get_plugins_reports($courseid)) {
 863          $plugin_info['report'] = $reports;
 864      }
 865  
 866      if ($settings = grade_helper::get_info_manage_settings($courseid)) {
 867          $plugin_info['settings'] = $settings;
 868      }
 869  
 870      if ($scale = grade_helper::get_info_scales($courseid)) {
 871          $plugin_info['scale'] = array('view'=>$scale);
 872      }
 873  
 874      if ($outcomes = grade_helper::get_info_outcomes($courseid)) {
 875          $plugin_info['outcome'] = $outcomes;
 876      }
 877  
 878      if ($letters = grade_helper::get_info_letters($courseid)) {
 879          $plugin_info['letter'] = $letters;
 880      }
 881  
 882      if ($imports = grade_helper::get_plugins_import($courseid)) {
 883          $plugin_info['import'] = $imports;
 884      }
 885  
 886      if ($exports = grade_helper::get_plugins_export($courseid)) {
 887          $plugin_info['export'] = $exports;
 888      }
 889  
 890      foreach ($plugin_info as $plugin_type => $plugins) {
 891          if (!empty($plugins->id) && $active_plugin == $plugins->id) {
 892              $plugin_info['strings']['active_plugin_str'] = $plugins->string;
 893              break;
 894          }
 895          foreach ($plugins as $plugin) {
 896              if (is_a($plugin, 'grade_plugin_info')) {
 897                  if ($active_plugin == $plugin->id) {
 898                      $plugin_info['strings']['active_plugin_str'] = $plugin->string;
 899                  }
 900              }
 901          }
 902      }
 903  
 904      return $plugin_info;
 905  }
 906  
 907  /**
 908   * A simple class containing info about grade plugins.
 909   * Can be subclassed for special rules
 910   *
 911   * @package core_grades
 912   * @copyright 2009 Nicolas Connault
 913   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 914   */
 915  class grade_plugin_info {
 916      /**
 917       * A unique id for this plugin
 918       *
 919       * @var mixed
 920       */
 921      public $id;
 922      /**
 923       * A URL to access this plugin
 924       *
 925       * @var mixed
 926       */
 927      public $link;
 928      /**
 929       * The name of this plugin
 930       *
 931       * @var mixed
 932       */
 933      public $string;
 934      /**
 935       * Another grade_plugin_info object, parent of the current one
 936       *
 937       * @var mixed
 938       */
 939      public $parent;
 940  
 941      /**
 942       * Constructor
 943       *
 944       * @param int $id A unique id for this plugin
 945       * @param string $link A URL to access this plugin
 946       * @param string $string The name of this plugin
 947       * @param object $parent Another grade_plugin_info object, parent of the current one
 948       *
 949       * @return void
 950       */
 951      public function __construct($id, $link, $string, $parent=null) {
 952          $this->id = $id;
 953          $this->link = $link;
 954          $this->string = $string;
 955          $this->parent = $parent;
 956      }
 957  }
 958  
 959  /**
 960   * Prints the page headers, breadcrumb trail, page heading, (optional) dropdown navigation menu and
 961   * (optional) navigation tabs for any gradebook page. All gradebook pages MUST use these functions
 962   * in favour of the usual print_header(), print_header_simple(), print_heading() etc.
 963   * !IMPORTANT! Use of tabs.php file in gradebook pages is forbidden unless tabs are switched off at
 964   * the site level for the gradebook ($CFG->grade_navmethod = GRADE_NAVMETHOD_DROPDOWN).
 965   *
 966   * @param int     $courseid Course id
 967   * @param string  $active_type The type of the current page (report, settings,
 968   *                             import, export, scales, outcomes, letters)
 969   * @param string  $active_plugin The plugin of the current page (grader, fullview etc...)
 970   * @param string  $heading The heading of the page. Tries to guess if none is given
 971   * @param boolean $return Whether to return (true) or echo (false) the HTML generated by this function
 972   * @param string  $bodytags Additional attributes that will be added to the <body> tag
 973   * @param string  $buttons Additional buttons to display on the page
 974   * @param boolean $shownavigation should the gradebook navigation drop down (or tabs) be shown?
 975   * @param string  $headerhelpidentifier The help string identifier if required.
 976   * @param string  $headerhelpcomponent The component for the help string.
 977   * @param stdClass $user The user object for use with the user context header.
 978   *
 979   * @return string HTML code or nothing if $return == false
 980   */
 981  function print_grade_page_head($courseid, $active_type, $active_plugin=null,
 982                                 $heading = false, $return=false,
 983                                 $buttons=false, $shownavigation=true, $headerhelpidentifier = null, $headerhelpcomponent = null,
 984                                 $user = null) {
 985      global $CFG, $OUTPUT, $PAGE;
 986  
 987      // Put a warning on all gradebook pages if the course has modules currently scheduled for background deletion.
 988      require_once($CFG->dirroot . '/course/lib.php');
 989      if (course_modules_pending_deletion($courseid, true)) {
 990          \core\notification::add(get_string('gradesmoduledeletionpendingwarning', 'grades'),
 991              \core\output\notification::NOTIFY_WARNING);
 992      }
 993  
 994      if ($active_type === 'preferences') {
 995          // In Moodle 2.8 report preferences were moved under 'settings'. Allow backward compatibility for 3rd party grade reports.
 996          $active_type = 'settings';
 997      }
 998  
 999      $plugin_info = grade_get_plugin_info($courseid, $active_type, $active_plugin);
1000  
1001      // Determine the string of the active plugin
1002      $stractive_plugin = ($active_plugin) ? $plugin_info['strings']['active_plugin_str'] : $heading;
1003      $stractive_type = $plugin_info['strings'][$active_type];
1004  
1005      if (empty($plugin_info[$active_type]->id) || !empty($plugin_info[$active_type]->parent)) {
1006          $title = $PAGE->course->fullname.': ' . $stractive_type . ': ' . $stractive_plugin;
1007      } else {
1008          $title = $PAGE->course->fullname.': ' . $stractive_plugin;
1009      }
1010  
1011      if ($active_type == 'report') {
1012          $PAGE->set_pagelayout('report');
1013      } else {
1014          $PAGE->set_pagelayout('admin');
1015      }
1016      $PAGE->set_title(get_string('grades') . ': ' . $stractive_type);
1017      $PAGE->set_heading($title);
1018      if ($buttons instanceof single_button) {
1019          $buttons = $OUTPUT->render($buttons);
1020      }
1021      $PAGE->set_button($buttons);
1022      if ($courseid != SITEID) {
1023          grade_extend_settings($plugin_info, $courseid);
1024      }
1025  
1026      // Set the current report as active in the breadcrumbs.
1027      if ($active_plugin !== null && $reportnav = $PAGE->settingsnav->find($active_plugin, navigation_node::TYPE_SETTING)) {
1028          $reportnav->make_active();
1029      }
1030  
1031      $returnval = $OUTPUT->header();
1032  
1033      if (!$return) {
1034          echo $returnval;
1035      }
1036  
1037      // Guess heading if not given explicitly
1038      if (!$heading) {
1039          $heading = $stractive_plugin;
1040      }
1041  
1042      if ($shownavigation) {
1043          $navselector = null;
1044          if ($courseid != SITEID &&
1045                  ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_DROPDOWN)) {
1046              // It's absolutely essential that this grade plugin selector is shown after the user header. Just ask Fred.
1047              $navselector = print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, true);
1048              if ($return) {
1049                  $returnval .= $navselector;
1050              } else if (!isset($user)) {
1051                  echo $navselector;
1052              }
1053          }
1054  
1055          $output = '';
1056          // Add a help dialogue box if provided.
1057          if (isset($headerhelpidentifier)) {
1058              $output = $OUTPUT->heading_with_help($heading, $headerhelpidentifier, $headerhelpcomponent);
1059          } else {
1060              if (isset($user)) {
1061                  $output = $OUTPUT->context_header(
1062                          array(
1063                              'heading' => html_writer::link(new moodle_url('/user/view.php', array('id' => $user->id,
1064                                  'course' => $courseid)), fullname($user)),
1065                              'user' => $user,
1066                              'usercontext' => context_user::instance($user->id)
1067                          ), 2
1068                      ) . $navselector;
1069              } else {
1070                  $output = $OUTPUT->heading($heading);
1071              }
1072          }
1073  
1074          if ($return) {
1075              $returnval .= $output;
1076          } else {
1077              echo $output;
1078          }
1079  
1080          if ($courseid != SITEID &&
1081                  ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_TABS)) {
1082              $returnval .= grade_print_tabs($active_type, $active_plugin, $plugin_info, $return);
1083          }
1084      }
1085  
1086      $returnval .= print_natural_aggregation_upgrade_notice($courseid,
1087                                                             context_course::instance($courseid),
1088                                                             $PAGE->url,
1089                                                             $return);
1090  
1091      if ($return) {
1092          return $returnval;
1093      }
1094  }
1095  
1096  /**
1097   * Utility class used for return tracking when using edit and other forms in grade plugins
1098   *
1099   * @package core_grades
1100   * @copyright 2009 Nicolas Connault
1101   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1102   */
1103  class grade_plugin_return {
1104      /**
1105       * Type of grade plugin (e.g. 'edit', 'report')
1106       *
1107       * @var string
1108       */
1109      public $type;
1110      /**
1111       * Name of grade plugin (e.g. 'grader', 'overview')
1112       *
1113       * @var string
1114       */
1115      public $plugin;
1116      /**
1117       * Course id being viewed
1118       *
1119       * @var int
1120       */
1121      public $courseid;
1122      /**
1123       * Id of user whose information is being viewed/edited
1124       *
1125       * @var int
1126       */
1127      public $userid;
1128      /**
1129       * Id of group for which information is being viewed/edited
1130       *
1131       * @var int
1132       */
1133      public $groupid;
1134      /**
1135       * Current page # within output
1136       *
1137       * @var int
1138       */
1139      public $page;
1140  
1141      /**
1142       * Constructor
1143       *
1144       * @param array $params - associative array with return parameters, if not supplied parameter are taken from _GET or _POST
1145       */
1146      public function __construct($params = []) {
1147          $this->type     = optional_param('gpr_type', null, PARAM_SAFEDIR);
1148          $this->plugin   = optional_param('gpr_plugin', null, PARAM_PLUGIN);
1149          $this->courseid = optional_param('gpr_courseid', null, PARAM_INT);
1150          $this->userid   = optional_param('gpr_userid', null, PARAM_INT);
1151          $this->groupid  = optional_param('gpr_groupid', null, PARAM_INT);
1152          $this->page     = optional_param('gpr_page', null, PARAM_INT);
1153  
1154          foreach ($params as $key => $value) {
1155              if (property_exists($this, $key)) {
1156                  $this->$key = $value;
1157              }
1158          }
1159          // Allow course object rather than id to be used to specify course
1160          // - avoid unnecessary use of get_course.
1161          if (array_key_exists('course', $params)) {
1162              $course = $params['course'];
1163              $this->courseid = $course->id;
1164          } else {
1165              $course = null;
1166          }
1167          // If group has been explicitly set in constructor parameters,
1168          // we should respect that.
1169          if (!array_key_exists('groupid', $params)) {
1170              // Otherwise, 'group' in request parameters is a request for a change.
1171              // In that case, or if we have no group at all, we should get groupid from
1172              // groups_get_course_group, which will do some housekeeping as well as
1173              // give us the correct value.
1174              $changegroup = optional_param('group', -1, PARAM_INT);
1175              if ($changegroup !== -1 or (empty($this->groupid) and !empty($this->courseid))) {
1176                  if ($course === null) {
1177                      $course = get_course($this->courseid);
1178                  }
1179                  $this->groupid = groups_get_course_group($course, true);
1180              }
1181          }
1182      }
1183  
1184      /**
1185       * Old syntax of class constructor. Deprecated in PHP7.
1186       *
1187       * @deprecated since Moodle 3.1
1188       */
1189      public function grade_plugin_return($params = null) {
1190          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
1191          self::__construct($params);
1192      }
1193  
1194      /**
1195       * Returns return parameters as options array suitable for buttons.
1196       * @return array options
1197       */
1198      public function get_options() {
1199          if (empty($this->type)) {
1200              return array();
1201          }
1202  
1203          $params = array();
1204  
1205          if (!empty($this->plugin)) {
1206              $params['plugin'] = $this->plugin;
1207          }
1208  
1209          if (!empty($this->courseid)) {
1210              $params['id'] = $this->courseid;
1211          }
1212  
1213          if (!empty($this->userid)) {
1214              $params['userid'] = $this->userid;
1215          }
1216  
1217          if (!empty($this->groupid)) {
1218              $params['group'] = $this->groupid;
1219          }
1220  
1221          if (!empty($this->page)) {
1222              $params['page'] = $this->page;
1223          }
1224  
1225          return $params;
1226      }
1227  
1228      /**
1229       * Returns return url
1230       *
1231       * @param string $default default url when params not set
1232       * @param array  $extras Extra URL parameters
1233       *
1234       * @return string url
1235       */
1236      public function get_return_url($default, $extras=null) {
1237          global $CFG;
1238  
1239          if (empty($this->type) or empty($this->plugin)) {
1240              return $default;
1241          }
1242  
1243          $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php';
1244          $glue = '?';
1245  
1246          if (!empty($this->courseid)) {
1247              $url .= $glue.'id='.$this->courseid;
1248              $glue = '&amp;';
1249          }
1250  
1251          if (!empty($this->userid)) {
1252              $url .= $glue.'userid='.$this->userid;
1253              $glue = '&amp;';
1254          }
1255  
1256          if (!empty($this->groupid)) {
1257              $url .= $glue.'group='.$this->groupid;
1258              $glue = '&amp;';
1259          }
1260  
1261          if (!empty($this->page)) {
1262              $url .= $glue.'page='.$this->page;
1263              $glue = '&amp;';
1264          }
1265  
1266          if (!empty($extras)) {
1267              foreach ($extras as $key=>$value) {
1268                  $url .= $glue.$key.'='.$value;
1269                  $glue = '&amp;';
1270              }
1271          }
1272  
1273          return $url;
1274      }
1275  
1276      /**
1277       * Returns string with hidden return tracking form elements.
1278       * @return string
1279       */
1280      public function get_form_fields() {
1281          if (empty($this->type)) {
1282              return '';
1283          }
1284  
1285          $result  = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />';
1286  
1287          if (!empty($this->plugin)) {
1288              $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />';
1289          }
1290  
1291          if (!empty($this->courseid)) {
1292              $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />';
1293          }
1294  
1295          if (!empty($this->userid)) {
1296              $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />';
1297          }
1298  
1299          if (!empty($this->groupid)) {
1300              $result .= '<input type="hidden" name="gpr_groupid" value="'.$this->groupid.'" />';
1301          }
1302  
1303          if (!empty($this->page)) {
1304              $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />';
1305          }
1306          return $result;
1307      }
1308  
1309      /**
1310       * Add hidden elements into mform
1311       *
1312       * @param object &$mform moodle form object
1313       *
1314       * @return void
1315       */
1316      public function add_mform_elements(&$mform) {
1317          if (empty($this->type)) {
1318              return;
1319          }
1320  
1321          $mform->addElement('hidden', 'gpr_type', $this->type);
1322          $mform->setType('gpr_type', PARAM_SAFEDIR);
1323  
1324          if (!empty($this->plugin)) {
1325              $mform->addElement('hidden', 'gpr_plugin', $this->plugin);
1326              $mform->setType('gpr_plugin', PARAM_PLUGIN);
1327          }
1328  
1329          if (!empty($this->courseid)) {
1330              $mform->addElement('hidden', 'gpr_courseid', $this->courseid);
1331              $mform->setType('gpr_courseid', PARAM_INT);
1332          }
1333  
1334          if (!empty($this->userid)) {
1335              $mform->addElement('hidden', 'gpr_userid', $this->userid);
1336              $mform->setType('gpr_userid', PARAM_INT);
1337          }
1338  
1339          if (!empty($this->groupid)) {
1340              $mform->addElement('hidden', 'gpr_groupid', $this->groupid);
1341              $mform->setType('gpr_groupid', PARAM_INT);
1342          }
1343  
1344          if (!empty($this->page)) {
1345              $mform->addElement('hidden', 'gpr_page', $this->page);
1346              $mform->setType('gpr_page', PARAM_INT);
1347          }
1348      }
1349  
1350      /**
1351       * Add return tracking params into url
1352       *
1353       * @param moodle_url $url A URL
1354       *
1355       * @return string $url with return tracking params
1356       */
1357      public function add_url_params(moodle_url $url) {
1358          if (empty($this->type)) {
1359              return $url;
1360          }
1361  
1362          $url->param('gpr_type', $this->type);
1363  
1364          if (!empty($this->plugin)) {
1365              $url->param('gpr_plugin', $this->plugin);
1366          }
1367  
1368          if (!empty($this->courseid)) {
1369              $url->param('gpr_courseid' ,$this->courseid);
1370          }
1371  
1372          if (!empty($this->userid)) {
1373              $url->param('gpr_userid', $this->userid);
1374          }
1375  
1376          if (!empty($this->groupid)) {
1377              $url->param('gpr_groupid', $this->groupid);
1378          }
1379  
1380          if (!empty($this->page)) {
1381              $url->param('gpr_page', $this->page);
1382          }
1383  
1384          return $url;
1385      }
1386  }
1387  
1388  /**
1389   * Function central to gradebook for building and printing the navigation (breadcrumb trail).
1390   *
1391   * @param string $path The path of the calling script (using __FILE__?)
1392   * @param string $pagename The language string to use as the last part of the navigation (non-link)
1393   * @param mixed  $id Either a plain integer (assuming the key is 'id') or
1394   *                   an array of keys and values (e.g courseid => $courseid, itemid...)
1395   *
1396   * @return string
1397   */
1398  function grade_build_nav($path, $pagename=null, $id=null) {
1399      global $CFG, $COURSE, $PAGE;
1400  
1401      $strgrades = get_string('grades', 'grades');
1402  
1403      // Parse the path and build navlinks from its elements
1404      $dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash
1405      $path = substr($path, $dirroot_length);
1406      $path = str_replace('\\', '/', $path);
1407  
1408      $path_elements = explode('/', $path);
1409  
1410      $path_elements_count = count($path_elements);
1411  
1412      // First link is always 'grade'
1413      $PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$COURSE->id)));
1414  
1415      $link = null;
1416      $numberofelements = 3;
1417  
1418      // Prepare URL params string
1419      $linkparams = array();
1420      if (!is_null($id)) {
1421          if (is_array($id)) {
1422              foreach ($id as $idkey => $idvalue) {
1423                  $linkparams[$idkey] = $idvalue;
1424              }
1425          } else {
1426              $linkparams['id'] = $id;
1427          }
1428      }
1429  
1430      $navlink4 = null;
1431  
1432      // Remove file extensions from filenames
1433      foreach ($path_elements as $key => $filename) {
1434          $path_elements[$key] = str_replace('.php', '', $filename);
1435      }
1436  
1437      // Second level links
1438      switch ($path_elements[1]) {
1439          case 'edit': // No link
1440              if ($path_elements[3] != 'index.php') {
1441                  $numberofelements = 4;
1442              }
1443              break;
1444          case 'import': // No link
1445              break;
1446          case 'export': // No link
1447              break;
1448          case 'report':
1449              // $id is required for this link. Do not print it if $id isn't given
1450              if (!is_null($id)) {
1451                  $link = new moodle_url('/grade/report/index.php', $linkparams);
1452              }
1453  
1454              if ($path_elements[2] == 'grader') {
1455                  $numberofelements = 4;
1456              }
1457              break;
1458  
1459          default:
1460              // If this element isn't among the ones already listed above, it isn't supported, throw an error.
1461              debugging("grade_build_nav() doesn't support ". $path_elements[1] .
1462                      " as the second path element after 'grade'.");
1463              return false;
1464      }
1465      $PAGE->navbar->add(get_string($path_elements[1], 'grades'), $link);
1466  
1467      // Third level links
1468      if (empty($pagename)) {
1469          $pagename = get_string($path_elements[2], 'grades');
1470      }
1471  
1472      switch ($numberofelements) {
1473          case 3:
1474              $PAGE->navbar->add($pagename, $link);
1475              break;
1476          case 4:
1477              if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') {
1478                  $PAGE->navbar->add(get_string('pluginname', 'gradereport_grader'), new moodle_url('/grade/report/grader/index.php', $linkparams));
1479              }
1480              $PAGE->navbar->add($pagename);
1481              break;
1482      }
1483  
1484      return '';
1485  }
1486  
1487  /**
1488   * General structure representing grade items in course
1489   *
1490   * @package core_grades
1491   * @copyright 2009 Nicolas Connault
1492   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1493   */
1494  class grade_structure {
1495      public $context;
1496  
1497      public $courseid;
1498  
1499      /**
1500      * Reference to modinfo for current course (for performance, to save
1501      * retrieving it from courseid every time). Not actually set except for
1502      * the grade_tree type.
1503      * @var course_modinfo
1504      */
1505      public $modinfo;
1506  
1507      /**
1508       * 1D array of grade items only
1509       */
1510      public $items;
1511  
1512      /**
1513       * Returns icon of element
1514       *
1515       * @param array &$element An array representing an element in the grade_tree
1516       * @param bool  $spacerifnone return spacer if no icon found
1517       *
1518       * @return string icon or spacer
1519       */
1520      public function get_element_icon(&$element, $spacerifnone=false) {
1521          global $CFG, $OUTPUT;
1522          require_once $CFG->libdir.'/filelib.php';
1523  
1524          $outputstr = '';
1525  
1526          // Object holding pix_icon information before instantiation.
1527          $icon = new stdClass();
1528          $icon->attributes = array(
1529              'class' => 'icon itemicon'
1530          );
1531          $icon->component = 'moodle';
1532  
1533          $none = true;
1534          switch ($element['type']) {
1535              case 'item':
1536              case 'courseitem':
1537              case 'categoryitem':
1538                  $none = false;
1539  
1540                  $is_course   = $element['object']->is_course_item();
1541                  $is_category = $element['object']->is_category_item();
1542                  $is_scale    = $element['object']->gradetype == GRADE_TYPE_SCALE;
1543                  $is_value    = $element['object']->gradetype == GRADE_TYPE_VALUE;
1544                  $is_outcome  = !empty($element['object']->outcomeid);
1545  
1546                  if ($element['object']->is_calculated()) {
1547                      $icon->pix = 'i/calc';
1548                      $icon->title = s(get_string('calculatedgrade', 'grades'));
1549  
1550                  } else if (($is_course or $is_category) and ($is_scale or $is_value)) {
1551                      if ($category = $element['object']->get_item_category()) {
1552                          $aggrstrings = grade_helper::get_aggregation_strings();
1553                          $stragg = $aggrstrings[$category->aggregation];
1554  
1555                          $icon->pix = 'i/calc';
1556                          $icon->title = s($stragg);
1557  
1558                          switch ($category->aggregation) {
1559                              case GRADE_AGGREGATE_MEAN:
1560                              case GRADE_AGGREGATE_MEDIAN:
1561                              case GRADE_AGGREGATE_WEIGHTED_MEAN:
1562                              case GRADE_AGGREGATE_WEIGHTED_MEAN2:
1563                              case GRADE_AGGREGATE_EXTRACREDIT_MEAN:
1564                                  $icon->pix = 'i/agg_mean';
1565                                  break;
1566                              case GRADE_AGGREGATE_SUM:
1567                                  $icon->pix = 'i/agg_sum';
1568                                  break;
1569                          }
1570                      }
1571  
1572                  } else if ($element['object']->itemtype == 'mod') {
1573                      // Prevent outcomes displaying the same icon as the activity they are attached to.
1574                      if ($is_outcome) {
1575                          $icon->pix = 'i/outcomes';
1576                          $icon->title = s(get_string('outcome', 'grades'));
1577                      } else {
1578                          $modinfo = get_fast_modinfo($element['object']->courseid);
1579                          $module = $element['object']->itemmodule;
1580                          $instanceid = $element['object']->iteminstance;
1581                          if (isset($modinfo->instances[$module][$instanceid])) {
1582                              $icon->url = $modinfo->instances[$module][$instanceid]->get_icon_url();
1583                          } else {
1584                              $icon->pix = 'icon';
1585                              $icon->component = $element['object']->itemmodule;
1586                          }
1587                          $icon->title = s(get_string('modulename', $element['object']->itemmodule));
1588                      }
1589                  } else if ($element['object']->itemtype == 'manual') {
1590                      if ($element['object']->is_outcome_item()) {
1591                          $icon->pix = 'i/outcomes';
1592                          $icon->title = s(get_string('outcome', 'grades'));
1593                      } else {
1594                          $icon->pix = 'i/manual_item';
1595                          $icon->title = s(get_string('manualitem', 'grades'));
1596                      }
1597                  }
1598                  break;
1599  
1600              case 'category':
1601                  $none = false;
1602                  $icon->pix = 'i/folder';
1603                  $icon->title = s(get_string('category', 'grades'));
1604                  break;
1605          }
1606  
1607          if ($none) {
1608              if ($spacerifnone) {
1609                  $outputstr = $OUTPUT->spacer() . ' ';
1610              }
1611          } else if (isset($icon->url)) {
1612              $outputstr = html_writer::img($icon->url, $icon->title, $icon->attributes);
1613          } else {
1614              $outputstr = $OUTPUT->pix_icon($icon->pix, $icon->title, $icon->component, $icon->attributes);
1615          }
1616  
1617          return $outputstr;
1618      }
1619  
1620      /**
1621       * Returns name of element optionally with icon and link
1622       *
1623       * @param array &$element An array representing an element in the grade_tree
1624       * @param bool  $withlink Whether or not this header has a link
1625       * @param bool  $icon Whether or not to display an icon with this header
1626       * @param bool  $spacerifnone return spacer if no icon found
1627       * @param bool  $withdescription Show description if defined by this item.
1628       * @param bool  $fulltotal If the item is a category total, returns $categoryname."total"
1629       *                         instead of "Category total" or "Course total"
1630       *
1631       * @return string header
1632       */
1633      public function get_element_header(&$element, $withlink = false, $icon = true, $spacerifnone = false,
1634          $withdescription = false, $fulltotal = false) {
1635          $header = '';
1636  
1637          if ($icon) {
1638              $header .= $this->get_element_icon($element, $spacerifnone);
1639          }
1640  
1641          $title = $element['object']->get_name($fulltotal);
1642          $titleunescaped = $element['object']->get_name($fulltotal, false);
1643          $header .= $title;
1644  
1645          if ($element['type'] != 'item' and $element['type'] != 'categoryitem' and
1646              $element['type'] != 'courseitem') {
1647              return $header;
1648          }
1649  
1650          if ($withlink && $url = $this->get_activity_link($element)) {
1651              $a = new stdClass();
1652              $a->name = get_string('modulename', $element['object']->itemmodule);
1653              $a->title = $titleunescaped;
1654              $title = get_string('linktoactivity', 'grades', $a);
1655  
1656              $header = html_writer::link($url, $header, array('title' => $title, 'class' => 'gradeitemheader'));
1657          } else {
1658              $header = html_writer::span($header, 'gradeitemheader', array('title' => $titleunescaped, 'tabindex' => '0'));
1659          }
1660  
1661          if ($withdescription) {
1662              $desc = $element['object']->get_description();
1663              if (!empty($desc)) {
1664                  $header .= '<div class="gradeitemdescription">' . s($desc) . '</div><div class="gradeitemdescriptionfiller"></div>';
1665              }
1666          }
1667  
1668          return $header;
1669      }
1670  
1671      private function get_activity_link($element) {
1672          global $CFG;
1673          /** @var array static cache of the grade.php file existence flags */
1674          static $hasgradephp = array();
1675  
1676          $itemtype = $element['object']->itemtype;
1677          $itemmodule = $element['object']->itemmodule;
1678          $iteminstance = $element['object']->iteminstance;
1679          $itemnumber = $element['object']->itemnumber;
1680  
1681          // Links only for module items that have valid instance, module and are
1682          // called from grade_tree with valid modinfo
1683          if ($itemtype != 'mod' || !$iteminstance || !$itemmodule || !$this->modinfo) {
1684              return null;
1685          }
1686  
1687          // Get $cm efficiently and with visibility information using modinfo
1688          $instances = $this->modinfo->get_instances();
1689          if (empty($instances[$itemmodule][$iteminstance])) {
1690              return null;
1691          }
1692          $cm = $instances[$itemmodule][$iteminstance];
1693  
1694          // Do not add link if activity is not visible to the current user
1695          if (!$cm->uservisible) {
1696              return null;
1697          }
1698  
1699          if (!array_key_exists($itemmodule, $hasgradephp)) {
1700              if (file_exists($CFG->dirroot . '/mod/' . $itemmodule . '/grade.php')) {
1701                  $hasgradephp[$itemmodule] = true;
1702              } else {
1703                  $hasgradephp[$itemmodule] = false;
1704              }
1705          }
1706  
1707          // If module has grade.php, link to that, otherwise view.php
1708          if ($hasgradephp[$itemmodule]) {
1709              $args = array('id' => $cm->id, 'itemnumber' => $itemnumber);
1710              if (isset($element['userid'])) {
1711                  $args['userid'] = $element['userid'];
1712              }
1713              return new moodle_url('/mod/' . $itemmodule . '/grade.php', $args);
1714          } else {
1715              return new moodle_url('/mod/' . $itemmodule . '/view.php', array('id' => $cm->id));
1716          }
1717      }
1718  
1719      /**
1720       * Returns URL of a page that is supposed to contain detailed grade analysis
1721       *
1722       * At the moment, only activity modules are supported. The method generates link
1723       * to the module's file grade.php with the parameters id (cmid), itemid, itemnumber,
1724       * gradeid and userid. If the grade.php does not exist, null is returned.
1725       *
1726       * @return moodle_url|null URL or null if unable to construct it
1727       */
1728      public function get_grade_analysis_url(grade_grade $grade) {
1729          global $CFG;
1730          /** @var array static cache of the grade.php file existence flags */
1731          static $hasgradephp = array();
1732  
1733          if (empty($grade->grade_item) or !($grade->grade_item instanceof grade_item)) {
1734              throw new coding_exception('Passed grade without the associated grade item');
1735          }
1736          $item = $grade->grade_item;
1737  
1738          if (!$item->is_external_item()) {
1739              // at the moment, only activity modules are supported
1740              return null;
1741          }
1742          if ($item->itemtype !== 'mod') {
1743              throw new coding_exception('Unknown external itemtype: '.$item->itemtype);
1744          }
1745          if (empty($item->iteminstance) or empty($item->itemmodule) or empty($this->modinfo)) {
1746              return null;
1747          }
1748  
1749          if (!array_key_exists($item->itemmodule, $hasgradephp)) {
1750              if (file_exists($CFG->dirroot . '/mod/' . $item->itemmodule . '/grade.php')) {
1751                  $hasgradephp[$item->itemmodule] = true;
1752              } else {
1753                  $hasgradephp[$item->itemmodule] = false;
1754              }
1755          }
1756  
1757          if (!$hasgradephp[$item->itemmodule]) {
1758              return null;
1759          }
1760  
1761          $instances = $this->modinfo->get_instances();
1762          if (empty($instances[$item->itemmodule][$item->iteminstance])) {
1763              return null;
1764          }
1765          $cm = $instances[$item->itemmodule][$item->iteminstance];
1766          if (!$cm->uservisible) {
1767              return null;
1768          }
1769  
1770          $url = new moodle_url('/mod/'.$item->itemmodule.'/grade.php', array(
1771              'id'         => $cm->id,
1772              'itemid'     => $item->id,
1773              'itemnumber' => $item->itemnumber,
1774              'gradeid'    => $grade->id,
1775              'userid'     => $grade->userid,
1776          ));
1777  
1778          return $url;
1779      }
1780  
1781      /**
1782       * Returns an action icon leading to the grade analysis page
1783       *
1784       * @param grade_grade $grade
1785       * @return string
1786       */
1787      public function get_grade_analysis_icon(grade_grade $grade) {
1788          global $OUTPUT;
1789  
1790          $url = $this->get_grade_analysis_url($grade);
1791          if (is_null($url)) {
1792              return '';
1793          }
1794  
1795          $title = get_string('gradeanalysis', 'core_grades');
1796          return $OUTPUT->action_icon($url, new pix_icon('t/preview', ''), null,
1797                  ['title' => $title, 'aria-label' => $title]);
1798      }
1799  
1800      /**
1801       * Returns the grade eid - the grade may not exist yet.
1802       *
1803       * @param grade_grade $grade_grade A grade_grade object
1804       *
1805       * @return string eid
1806       */
1807      public function get_grade_eid($grade_grade) {
1808          if (empty($grade_grade->id)) {
1809              return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid;
1810          } else {
1811              return 'g'.$grade_grade->id;
1812          }
1813      }
1814  
1815      /**
1816       * Returns the grade_item eid
1817       * @param grade_item $grade_item A grade_item object
1818       * @return string eid
1819       */
1820      public function get_item_eid($grade_item) {
1821          return 'ig'.$grade_item->id;
1822      }
1823  
1824      /**
1825       * Given a grade_tree element, returns an array of parameters
1826       * used to build an icon for that element.
1827       *
1828       * @param array $element An array representing an element in the grade_tree
1829       *
1830       * @return array
1831       */
1832      public function get_params_for_iconstr($element) {
1833          $strparams = new stdClass();
1834          $strparams->category = '';
1835          $strparams->itemname = '';
1836          $strparams->itemmodule = '';
1837  
1838          if (!method_exists($element['object'], 'get_name')) {
1839              return $strparams;
1840          }
1841  
1842          $strparams->itemname = html_to_text($element['object']->get_name());
1843  
1844          // If element name is categorytotal, get the name of the parent category
1845          if ($strparams->itemname == get_string('categorytotal', 'grades')) {
1846              $parent = $element['object']->get_parent_category();
1847              $strparams->category = $parent->get_name() . ' ';
1848          } else {
1849              $strparams->category = '';
1850          }
1851  
1852          $strparams->itemmodule = null;
1853          if (isset($element['object']->itemmodule)) {
1854              $strparams->itemmodule = $element['object']->itemmodule;
1855          }
1856          return $strparams;
1857      }
1858  
1859      /**
1860       * Return a reset icon for the given element.
1861       *
1862       * @param array  $element An array representing an element in the grade_tree
1863       * @param object $gpr A grade_plugin_return object
1864       * @param bool $returnactionmenulink return the instance of action_menu_link instead of string
1865       * @return string|action_menu_link
1866       */
1867      public function get_reset_icon($element, $gpr, $returnactionmenulink = false) {
1868          global $CFG, $OUTPUT;
1869  
1870          // Limit to category items set to use the natural weights aggregation method, and users
1871          // with the capability to manage grades.
1872          if ($element['type'] != 'category' || $element['object']->aggregation != GRADE_AGGREGATE_SUM ||
1873                  !has_capability('moodle/grade:manage', $this->context)) {
1874              return $returnactionmenulink ? null : '';
1875          }
1876  
1877          $str = get_string('resetweights', 'grades', $this->get_params_for_iconstr($element));
1878          $url = new moodle_url('/grade/edit/tree/action.php', array(
1879              'id' => $this->courseid,
1880              'action' => 'resetweights',
1881              'eid' => $element['eid'],
1882              'sesskey' => sesskey(),
1883          ));
1884  
1885          if ($returnactionmenulink) {
1886              return new action_menu_link_secondary($gpr->add_url_params($url), new pix_icon('t/reset', $str),
1887                  get_string('resetweightsshort', 'grades'));
1888          } else {
1889              return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/reset', $str));
1890          }
1891      }
1892  
1893      /**
1894       * Return edit icon for give element
1895       *
1896       * @param array  $element An array representing an element in the grade_tree
1897       * @param object $gpr A grade_plugin_return object
1898       * @param bool $returnactionmenulink return the instance of action_menu_link instead of string
1899       * @return string|action_menu_link
1900       */
1901      public function get_edit_icon($element, $gpr, $returnactionmenulink = false) {
1902          global $CFG, $OUTPUT;
1903  
1904          if (!has_capability('moodle/grade:manage', $this->context)) {
1905              if ($element['type'] == 'grade' and has_capability('moodle/grade:edit', $this->context)) {
1906                  // oki - let them override grade
1907              } else {
1908                  return $returnactionmenulink ? null : '';
1909              }
1910          }
1911  
1912          static $strfeedback   = null;
1913          static $streditgrade = null;
1914          if (is_null($streditgrade)) {
1915              $streditgrade = get_string('editgrade', 'grades');
1916              $strfeedback  = get_string('feedback');
1917          }
1918  
1919          $strparams = $this->get_params_for_iconstr($element);
1920  
1921          $object = $element['object'];
1922  
1923          switch ($element['type']) {
1924              case 'item':
1925              case 'categoryitem':
1926              case 'courseitem':
1927                  $stredit = get_string('editverbose', 'grades', $strparams);
1928                  if (empty($object->outcomeid) || empty($CFG->enableoutcomes)) {
1929                      $url = new moodle_url('/grade/edit/tree/item.php',
1930                              array('courseid' => $this->courseid, 'id' => $object->id));
1931                  } else {
1932                      $url = new moodle_url('/grade/edit/tree/outcomeitem.php',
1933                              array('courseid' => $this->courseid, 'id' => $object->id));
1934                  }
1935                  break;
1936  
1937              case 'category':
1938                  $stredit = get_string('editverbose', 'grades', $strparams);
1939                  $url = new moodle_url('/grade/edit/tree/category.php',
1940                          array('courseid' => $this->courseid, 'id' => $object->id));
1941                  break;
1942  
1943              case 'grade':
1944                  $stredit = $streditgrade;
1945                  if (empty($object->id)) {
1946                      $url = new moodle_url('/grade/edit/tree/grade.php',
1947                              array('courseid' => $this->courseid, 'itemid' => $object->itemid, 'userid' => $object->userid));
1948                  } else {
1949                      $url = new moodle_url('/grade/edit/tree/grade.php',
1950                              array('courseid' => $this->courseid, 'id' => $object->id));
1951                  }
1952                  if (!empty($object->feedback)) {
1953                      $feedback = addslashes_js(trim(format_string($object->feedback, $object->feedbackformat)));
1954                  }
1955                  break;
1956  
1957              default:
1958                  $url = null;
1959          }
1960  
1961          if ($url) {
1962              if ($returnactionmenulink) {
1963                  return new action_menu_link_secondary($gpr->add_url_params($url),
1964                      new pix_icon('t/edit', $stredit),
1965                      get_string('editsettings'));
1966              } else {
1967                  return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/edit', $stredit));
1968              }
1969  
1970          } else {
1971              return $returnactionmenulink ? null : '';
1972          }
1973      }
1974  
1975      /**
1976       * Return hiding icon for give element
1977       *
1978       * @param array  $element An array representing an element in the grade_tree
1979       * @param object $gpr A grade_plugin_return object
1980       * @param bool $returnactionmenulink return the instance of action_menu_link instead of string
1981       * @return string|action_menu_link
1982       */
1983      public function get_hiding_icon($element, $gpr, $returnactionmenulink = false) {
1984          global $CFG, $OUTPUT;
1985  
1986          if (!$element['object']->can_control_visibility()) {
1987              return $returnactionmenulink ? null : '';
1988          }
1989  
1990          if (!has_capability('moodle/grade:manage', $this->context) and
1991              !has_capability('moodle/grade:hide', $this->context)) {
1992              return $returnactionmenulink ? null : '';
1993          }
1994  
1995          $strparams = $this->get_params_for_iconstr($element);
1996          $strshow = get_string('showverbose', 'grades', $strparams);
1997          $strhide = get_string('hideverbose', 'grades', $strparams);
1998  
1999          $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid']));
2000          $url = $gpr->add_url_params($url);
2001  
2002          if ($element['object']->is_hidden()) {
2003              $type = 'show';
2004              $tooltip = $strshow;
2005  
2006              // Change the icon and add a tooltip showing the date
2007              if ($element['type'] != 'category' and $element['object']->get_hidden() > 1) {
2008                  $type = 'hiddenuntil';
2009                  $tooltip = get_string('hiddenuntildate', 'grades',
2010                          userdate($element['object']->get_hidden()));
2011              }
2012  
2013              $url->param('action', 'show');
2014  
2015              if ($returnactionmenulink) {
2016                  $hideicon = new action_menu_link_secondary($url, new pix_icon('t/'.$type, $tooltip), get_string('show'));
2017              } else {
2018                  $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strshow, 'class'=>'smallicon')));
2019              }
2020  
2021          } else {
2022              $url->param('action', 'hide');
2023              if ($returnactionmenulink) {
2024                  $hideicon = new action_menu_link_secondary($url, new pix_icon('t/hide', $strhide), get_string('hide'));
2025              } else {
2026                  $hideicon = $OUTPUT->action_icon($url, new pix_icon('t/hide', $strhide));
2027              }
2028          }
2029  
2030          return $hideicon;
2031      }
2032  
2033      /**
2034       * Return locking icon for given element
2035       *
2036       * @param array  $element An array representing an element in the grade_tree
2037       * @param object $gpr A grade_plugin_return object
2038       *
2039       * @return string
2040       */
2041      public function get_locking_icon($element, $gpr) {
2042          global $CFG, $OUTPUT;
2043  
2044          $strparams = $this->get_params_for_iconstr($element);
2045          $strunlock = get_string('unlockverbose', 'grades', $strparams);
2046          $strlock = get_string('lockverbose', 'grades', $strparams);
2047  
2048          $url = new moodle_url('/grade/edit/tree/action.php', array('id' => $this->courseid, 'sesskey' => sesskey(), 'eid' => $element['eid']));
2049          $url = $gpr->add_url_params($url);
2050  
2051          // Don't allow an unlocking action for a grade whose grade item is locked: just print a state icon
2052          if ($element['type'] == 'grade' && $element['object']->grade_item->is_locked()) {
2053              $strparamobj = new stdClass();
2054              $strparamobj->itemname = $element['object']->grade_item->itemname;
2055              $strnonunlockable = get_string('nonunlockableverbose', 'grades', $strparamobj);
2056  
2057              $action = html_writer::tag('span', $OUTPUT->pix_icon('t/locked', $strnonunlockable),
2058                      array('class' => 'action-icon'));
2059  
2060          } else if ($element['object']->is_locked()) {
2061              $type = 'unlock';
2062              $tooltip = $strunlock;
2063  
2064              // Change the icon and add a tooltip showing the date
2065              if ($element['type'] != 'category' and $element['object']->get_locktime() > 1) {
2066                  $type = 'locktime';
2067                  $tooltip = get_string('locktimedate', 'grades',
2068                          userdate($element['object']->get_locktime()));
2069              }
2070  
2071              if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:unlock', $this->context)) {
2072                  $action = '';
2073              } else {
2074                  $url->param('action', 'unlock');
2075                  $action = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strunlock, 'class'=>'smallicon')));
2076              }
2077  
2078          } else {
2079              if (!has_capability('moodle/grade:manage', $this->context) and !has_capability('moodle/grade:lock', $this->context)) {
2080                  $action = '';
2081              } else {
2082                  $url->param('action', 'lock');
2083                  $action = $OUTPUT->action_icon($url, new pix_icon('t/lock', $strlock));
2084              }
2085          }
2086  
2087          return $action;
2088      }
2089  
2090      /**
2091       * Return calculation icon for given element
2092       *
2093       * @param array  $element An array representing an element in the grade_tree
2094       * @param object $gpr A grade_plugin_return object
2095       * @param bool $returnactionmenulink return the instance of action_menu_link instead of string
2096       * @return string|action_menu_link
2097       */
2098      public function get_calculation_icon($element, $gpr, $returnactionmenulink = false) {
2099          global $CFG, $OUTPUT;
2100          if (!has_capability('moodle/grade:manage', $this->context)) {
2101              return $returnactionmenulink ? null : '';
2102          }
2103  
2104          $type   = $element['type'];
2105          $object = $element['object'];
2106  
2107          if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
2108              $strparams = $this->get_params_for_iconstr($element);
2109              $streditcalculation = get_string('editcalculationverbose', 'grades', $strparams);
2110  
2111              $is_scale = $object->gradetype == GRADE_TYPE_SCALE;
2112              $is_value = $object->gradetype == GRADE_TYPE_VALUE;
2113  
2114              // show calculation icon only when calculation possible
2115              if (!$object->is_external_item() and ($is_scale or $is_value)) {
2116                  if ($object->is_calculated()) {
2117                      $icon = 't/calc';
2118                  } else {
2119                      $icon = 't/calc_off';
2120                  }
2121  
2122                  $url = new moodle_url('/grade/edit/tree/calculation.php', array('courseid' => $this->courseid, 'id' => $object->id));
2123                  $url = $gpr->add_url_params($url);
2124                  if ($returnactionmenulink) {
2125                      return new action_menu_link_secondary($url,
2126                          new pix_icon($icon, $streditcalculation),
2127                          get_string('editcalculation', 'grades'));
2128                  } else {
2129                      return $OUTPUT->action_icon($url, new pix_icon($icon, $streditcalculation));
2130                  }
2131              }
2132          }
2133  
2134          return $returnactionmenulink ? null : '';
2135      }
2136  }
2137  
2138  /**
2139   * Flat structure similar to grade tree.
2140   *
2141   * @uses grade_structure
2142   * @package core_grades
2143   * @copyright 2009 Nicolas Connault
2144   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2145   */
2146  class grade_seq extends grade_structure {
2147  
2148      /**
2149       * 1D array of elements
2150       */
2151      public $elements;
2152  
2153      /**
2154       * Constructor, retrieves and stores array of all grade_category and grade_item
2155       * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed.
2156       *
2157       * @param int  $courseid The course id
2158       * @param bool $category_grade_last category grade item is the last child
2159       * @param bool $nooutcomes Whether or not outcomes should be included
2160       */
2161      public function __construct($courseid, $category_grade_last=false, $nooutcomes=false) {
2162          global $USER, $CFG;
2163  
2164          $this->courseid   = $courseid;
2165          $this->context    = context_course::instance($courseid);
2166  
2167          // get course grade tree
2168          $top_element = grade_category::fetch_course_tree($courseid, true);
2169  
2170          $this->elements = grade_seq::flatten($top_element, $category_grade_last, $nooutcomes);
2171  
2172          foreach ($this->elements as $key=>$unused) {
2173              $this->items[$this->elements[$key]['object']->id] =& $this->elements[$key]['object'];
2174          }
2175      }
2176  
2177      /**
2178       * Old syntax of class constructor. Deprecated in PHP7.
2179       *
2180       * @deprecated since Moodle 3.1
2181       */
2182      public function grade_seq($courseid, $category_grade_last=false, $nooutcomes=false) {
2183          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
2184          self::__construct($courseid, $category_grade_last, $nooutcomes);
2185      }
2186  
2187      /**
2188       * Static recursive helper - makes the grade_item for category the last children
2189       *
2190       * @param array &$element The seed of the recursion
2191       * @param bool $category_grade_last category grade item is the last child
2192       * @param bool $nooutcomes Whether or not outcomes should be included
2193       *
2194       * @return array
2195       */
2196      public function flatten(&$element, $category_grade_last, $nooutcomes) {
2197          if (empty($element['children'])) {
2198              return array();
2199          }
2200          $children = array();
2201  
2202          foreach ($element['children'] as $sortorder=>$unused) {
2203              if ($nooutcomes and $element['type'] != 'category' and
2204                  $element['children'][$sortorder]['object']->is_outcome_item()) {
2205                  continue;
2206              }
2207              $children[] = $element['children'][$sortorder];
2208          }
2209          unset($element['children']);
2210  
2211          if ($category_grade_last and count($children) > 1 and
2212              (
2213                  $children[0]['type'] === 'courseitem' or
2214                  $children[0]['type'] === 'categoryitem'
2215              )
2216          ) {
2217              $cat_item = array_shift($children);
2218              array_push($children, $cat_item);
2219          }
2220  
2221          $result = array();
2222          foreach ($children as $child) {
2223              if ($child['type'] == 'category') {
2224                  $result = $result + grade_seq::flatten($child, $category_grade_last, $nooutcomes);
2225              } else {
2226                  $child['eid'] = 'i'.$child['object']->id;
2227                  $result[$child['object']->id] = $child;
2228              }
2229          }
2230  
2231          return $result;
2232      }
2233  
2234      /**
2235       * Parses the array in search of a given eid and returns a element object with
2236       * information about the element it has found.
2237       *
2238       * @param int $eid Gradetree Element ID
2239       *
2240       * @return object element
2241       */
2242      public function locate_element($eid) {
2243          // it is a grade - construct a new object
2244          if (strpos($eid, 'n') === 0) {
2245              if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
2246                  return null;
2247              }
2248  
2249              $itemid = $matches[1];
2250              $userid = $matches[2];
2251  
2252              //extra security check - the grade item must be in this tree
2253              if (!$item_el = $this->locate_element('ig'.$itemid)) {
2254                  return null;
2255              }
2256  
2257              // $gradea->id may be null - means does not exist yet
2258              $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
2259  
2260              $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
2261              return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
2262  
2263          } else if (strpos($eid, 'g') === 0) {
2264              $id = (int) substr($eid, 1);
2265              if (!$grade = grade_grade::fetch(array('id'=>$id))) {
2266                  return null;
2267              }
2268              //extra security check - the grade item must be in this tree
2269              if (!$item_el = $this->locate_element('ig'.$grade->itemid)) {
2270                  return null;
2271              }
2272              $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
2273              return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
2274          }
2275  
2276          // it is a category or item
2277          foreach ($this->elements as $element) {
2278              if ($element['eid'] == $eid) {
2279                  return $element;
2280              }
2281          }
2282  
2283          return null;
2284      }
2285  }
2286  
2287  /**
2288   * This class represents a complete tree of categories, grade_items and final grades,
2289   * organises as an array primarily, but which can also be converted to other formats.
2290   * It has simple method calls with complex implementations, allowing for easy insertion,
2291   * deletion and moving of items and categories within the tree.
2292   *
2293   * @uses grade_structure
2294   * @package core_grades
2295   * @copyright 2009 Nicolas Connault
2296   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2297   */
2298  class grade_tree extends grade_structure {
2299  
2300      /**
2301       * The basic representation of the tree as a hierarchical, 3-tiered array.
2302       * @var object $top_element
2303       */
2304      public $top_element;
2305  
2306      /**
2307       * 2D array of grade items and categories
2308       * @var array $levels
2309       */
2310      public $levels;
2311  
2312      /**
2313       * Grade items
2314       * @var array $items
2315       */
2316      public $items;
2317  
2318      /**
2319       * Constructor, retrieves and stores a hierarchical array of all grade_category and grade_item
2320       * objects for the given courseid. Full objects are instantiated. Ordering sequence is fixed if needed.
2321       *
2322       * @param int   $courseid The Course ID
2323       * @param bool  $fillers include fillers and colspans, make the levels var "rectangular"
2324       * @param bool  $category_grade_last category grade item is the last child
2325       * @param array $collapsed array of collapsed categories
2326       * @param bool  $nooutcomes Whether or not outcomes should be included
2327       */
2328      public function __construct($courseid, $fillers=true, $category_grade_last=false,
2329                                 $collapsed=null, $nooutcomes=false) {
2330          global $USER, $CFG, $COURSE, $DB;
2331  
2332          $this->courseid   = $courseid;
2333          $this->levels     = array();
2334          $this->context    = context_course::instance($courseid);
2335  
2336          if (!empty($COURSE->id) && $COURSE->id == $this->courseid) {
2337              $course = $COURSE;
2338          } else {
2339              $course = $DB->get_record('course', array('id' => $this->courseid));
2340          }
2341          $this->modinfo = get_fast_modinfo($course);
2342  
2343          // get course grade tree
2344          $this->top_element = grade_category::fetch_course_tree($courseid, true);
2345  
2346          // collapse the categories if requested
2347          if (!empty($collapsed)) {
2348              grade_tree::category_collapse($this->top_element, $collapsed);
2349          }
2350  
2351          // no otucomes if requested
2352          if (!empty($nooutcomes)) {
2353              grade_tree::no_outcomes($this->top_element);
2354          }
2355  
2356          // move category item to last position in category
2357          if ($category_grade_last) {
2358              grade_tree::category_grade_last($this->top_element);
2359          }
2360  
2361          if ($fillers) {
2362              // inject fake categories == fillers
2363              grade_tree::inject_fillers($this->top_element, 0);
2364              // add colspans to categories and fillers
2365              grade_tree::inject_colspans($this->top_element);
2366          }
2367  
2368          grade_tree::fill_levels($this->levels, $this->top_element, 0);
2369  
2370      }
2371  
2372      /**
2373       * Old syntax of class constructor. Deprecated in PHP7.
2374       *
2375       * @deprecated since Moodle 3.1
2376       */
2377      public function grade_tree($courseid, $fillers=true, $category_grade_last=false,
2378                                 $collapsed=null, $nooutcomes=false) {
2379          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
2380          self::__construct($courseid, $fillers, $category_grade_last, $collapsed, $nooutcomes);
2381      }
2382  
2383      /**
2384       * Static recursive helper - removes items from collapsed categories
2385       *
2386       * @param array &$element The seed of the recursion
2387       * @param array $collapsed array of collapsed categories
2388       *
2389       * @return void
2390       */
2391      public function category_collapse(&$element, $collapsed) {
2392          if ($element['type'] != 'category') {
2393              return;
2394          }
2395          if (empty($element['children']) or count($element['children']) < 2) {
2396              return;
2397          }
2398  
2399          if (in_array($element['object']->id, $collapsed['aggregatesonly'])) {
2400              $category_item = reset($element['children']); //keep only category item
2401              $element['children'] = array(key($element['children'])=>$category_item);
2402  
2403          } else {
2404              if (in_array($element['object']->id, $collapsed['gradesonly'])) { // Remove category item
2405                  reset($element['children']);
2406                  $first_key = key($element['children']);
2407                  unset($element['children'][$first_key]);
2408              }
2409              foreach ($element['children'] as $sortorder=>$child) { // Recurse through the element's children
2410                  grade_tree::category_collapse($element['children'][$sortorder], $collapsed);
2411              }
2412          }
2413      }
2414  
2415      /**
2416       * Static recursive helper - removes all outcomes
2417       *
2418       * @param array &$element The seed of the recursion
2419       *
2420       * @return void
2421       */
2422      public function no_outcomes(&$element) {
2423          if ($element['type'] != 'category') {
2424              return;
2425          }
2426          foreach ($element['children'] as $sortorder=>$child) {
2427              if ($element['children'][$sortorder]['type'] == 'item'
2428                and $element['children'][$sortorder]['object']->is_outcome_item()) {
2429                  unset($element['children'][$sortorder]);
2430  
2431              } else if ($element['children'][$sortorder]['type'] == 'category') {
2432                  grade_tree::no_outcomes($element['children'][$sortorder]);
2433              }
2434          }
2435      }
2436  
2437      /**
2438       * Static recursive helper - makes the grade_item for category the last children
2439       *
2440       * @param array &$element The seed of the recursion
2441       *
2442       * @return void
2443       */
2444      public function category_grade_last(&$element) {
2445          if (empty($element['children'])) {
2446              return;
2447          }
2448          if (count($element['children']) < 2) {
2449              return;
2450          }
2451          $first_item = reset($element['children']);
2452          if ($first_item['type'] == 'categoryitem' or $first_item['type'] == 'courseitem') {
2453              // the category item might have been already removed
2454              $order = key($element['children']);
2455              unset($element['children'][$order]);
2456              $element['children'][$order] =& $first_item;
2457          }
2458          foreach ($element['children'] as $sortorder => $child) {
2459              grade_tree::category_grade_last($element['children'][$sortorder]);
2460          }
2461      }
2462  
2463      /**
2464       * Static recursive helper - fills the levels array, useful when accessing tree elements of one level
2465       *
2466       * @param array &$levels The levels of the grade tree through which to recurse
2467       * @param array &$element The seed of the recursion
2468       * @param int   $depth How deep are we?
2469       * @return void
2470       */
2471      public function fill_levels(&$levels, &$element, $depth) {
2472          if (!array_key_exists($depth, $levels)) {
2473              $levels[$depth] = array();
2474          }
2475  
2476          // prepare unique identifier
2477          if ($element['type'] == 'category') {
2478              $element['eid'] = 'cg'.$element['object']->id;
2479          } else if (in_array($element['type'], array('item', 'courseitem', 'categoryitem'))) {
2480              $element['eid'] = 'ig'.$element['object']->id;
2481              $this->items[$element['object']->id] =& $element['object'];
2482          }
2483  
2484          $levels[$depth][] =& $element;
2485          $depth++;
2486          if (empty($element['children'])) {
2487              return;
2488          }
2489          $prev = 0;
2490          foreach ($element['children'] as $sortorder=>$child) {
2491              grade_tree::fill_levels($levels, $element['children'][$sortorder], $depth);
2492              $element['children'][$sortorder]['prev'] = $prev;
2493              $element['children'][$sortorder]['next'] = 0;
2494              if ($prev) {
2495                  $element['children'][$prev]['next'] = $sortorder;
2496              }
2497              $prev = $sortorder;
2498          }
2499      }
2500  
2501      /**
2502       * Determines whether the grade tree item can be displayed.
2503       * This is particularly targeted for grade categories that have no total (None) when rendering the grade tree.
2504       * It checks if the grade tree item is of type 'category', and makes sure that the category, or at least one of children,
2505       * can be output.
2506       *
2507       * @param array $element The grade category element.
2508       * @return bool True if the grade tree item can be displayed. False, otherwise.
2509       */
2510      public static function can_output_item($element) {
2511          $canoutput = true;
2512  
2513          if ($element['type'] === 'category') {
2514              $object = $element['object'];
2515              $category = grade_category::fetch(array('id' => $object->id));
2516              // Category has total, we can output this.
2517              if ($category->get_grade_item()->gradetype != GRADE_TYPE_NONE) {
2518                  return true;
2519              }
2520  
2521              // Category has no total and has no children, no need to output this.
2522              if (empty($element['children'])) {
2523                  return false;
2524              }
2525  
2526              $canoutput = false;
2527              // Loop over children and make sure at least one child can be output.
2528              foreach ($element['children'] as $child) {
2529                  $canoutput = self::can_output_item($child);
2530                  if ($canoutput) {
2531                      break;
2532                  }
2533              }
2534          }
2535  
2536          return $canoutput;
2537      }
2538  
2539      /**
2540       * Static recursive helper - makes full tree (all leafes are at the same level)
2541       *
2542       * @param array &$element The seed of the recursion
2543       * @param int   $depth How deep are we?
2544       *
2545       * @return int
2546       */
2547      public function inject_fillers(&$element, $depth) {
2548          $depth++;
2549  
2550          if (empty($element['children'])) {
2551              return $depth;
2552          }
2553          $chdepths = array();
2554          $chids = array_keys($element['children']);
2555          $last_child  = end($chids);
2556          $first_child = reset($chids);
2557  
2558          foreach ($chids as $chid) {
2559              $chdepths[$chid] = grade_tree::inject_fillers($element['children'][$chid], $depth);
2560          }
2561          arsort($chdepths);
2562  
2563          $maxdepth = reset($chdepths);
2564          foreach ($chdepths as $chid=>$chd) {
2565              if ($chd == $maxdepth) {
2566                  continue;
2567              }
2568              if (!self::can_output_item($element['children'][$chid])) {
2569                  continue;
2570              }
2571              for ($i=0; $i < $maxdepth-$chd; $i++) {
2572                  if ($chid == $first_child) {
2573                      $type = 'fillerfirst';
2574                  } else if ($chid == $last_child) {
2575                      $type = 'fillerlast';
2576                  } else {
2577                      $type = 'filler';
2578                  }
2579                  $oldchild =& $element['children'][$chid];
2580                  $element['children'][$chid] = array('object'=>'filler', 'type'=>$type,
2581                                                      'eid'=>'', 'depth'=>$element['object']->depth,
2582                                                      'children'=>array($oldchild));
2583              }
2584          }
2585  
2586          return $maxdepth;
2587      }
2588  
2589      /**
2590       * Static recursive helper - add colspan information into categories
2591       *
2592       * @param array &$element The seed of the recursion
2593       *
2594       * @return int
2595       */
2596      public function inject_colspans(&$element) {
2597          if (empty($element['children'])) {
2598              return 1;
2599          }
2600          $count = 0;
2601          foreach ($element['children'] as $key=>$child) {
2602              if (!self::can_output_item($child)) {
2603                  continue;
2604              }
2605              $count += grade_tree::inject_colspans($element['children'][$key]);
2606          }
2607          $element['colspan'] = $count;
2608          return $count;
2609      }
2610  
2611      /**
2612       * Parses the array in search of a given eid and returns a element object with
2613       * information about the element it has found.
2614       * @param int $eid Gradetree Element ID
2615       * @return object element
2616       */
2617      public function locate_element($eid) {
2618          // it is a grade - construct a new object
2619          if (strpos($eid, 'n') === 0) {
2620              if (!preg_match('/n(\d+)u(\d+)/', $eid, $matches)) {
2621                  return null;
2622              }
2623  
2624              $itemid = $matches[1];
2625              $userid = $matches[2];
2626  
2627              //extra security check - the grade item must be in this tree
2628              if (!$item_el = $this->locate_element('ig'.$itemid)) {
2629                  return null;
2630              }
2631  
2632              // $gradea->id may be null - means does not exist yet
2633              $grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$userid));
2634  
2635              $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
2636              return array('eid'=>'n'.$itemid.'u'.$userid,'object'=>$grade, 'type'=>'grade');
2637  
2638          } else if (strpos($eid, 'g') === 0) {
2639              $id = (int) substr($eid, 1);
2640              if (!$grade = grade_grade::fetch(array('id'=>$id))) {
2641                  return null;
2642              }
2643              //extra security check - the grade item must be in this tree
2644              if (!$item_el = $this->locate_element('ig'.$grade->itemid)) {
2645                  return null;
2646              }
2647              $grade->grade_item =& $item_el['object']; // this may speedup grade_grade methods!
2648              return array('eid'=>'g'.$id,'object'=>$grade, 'type'=>'grade');
2649          }
2650  
2651          // it is a category or item
2652          foreach ($this->levels as $row) {
2653              foreach ($row as $element) {
2654                  if ($element['type'] == 'filler') {
2655                      continue;
2656                  }
2657                  if ($element['eid'] == $eid) {
2658                      return $element;
2659                  }
2660              }
2661          }
2662  
2663          return null;
2664      }
2665  
2666      /**
2667       * Returns a well-formed XML representation of the grade-tree using recursion.
2668       *
2669       * @param array  $root The current element in the recursion. If null, starts at the top of the tree.
2670       * @param string $tabs The control character to use for tabs
2671       *
2672       * @return string $xml
2673       */
2674      public function exporttoxml($root=null, $tabs="\t") {
2675          $xml = null;
2676          $first = false;
2677          if (is_null($root)) {
2678              $root = $this->top_element;
2679              $xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
2680              $xml .= "<gradetree>\n";
2681              $first = true;
2682          }
2683  
2684          $type = 'undefined';
2685          if (strpos($root['object']->table, 'grade_categories') !== false) {
2686              $type = 'category';
2687          } else if (strpos($root['object']->table, 'grade_items') !== false) {
2688              $type = 'item';
2689          } else if (strpos($root['object']->table, 'grade_outcomes') !== false) {
2690              $type = 'outcome';
2691          }
2692  
2693          $xml .= "$tabs<element type=\"$type\">\n";
2694          foreach ($root['object'] as $var => $value) {
2695              if (!is_object($value) && !is_array($value) && !empty($value)) {
2696                  $xml .= "$tabs\t<$var>$value</$var>\n";
2697              }
2698          }
2699  
2700          if (!empty($root['children'])) {
2701              $xml .= "$tabs\t<children>\n";
2702              foreach ($root['children'] as $sortorder => $child) {
2703                  $xml .= $this->exportToXML($child, $tabs."\t\t");
2704              }
2705              $xml .= "$tabs\t</children>\n";
2706          }
2707  
2708          $xml .= "$tabs</element>\n";
2709  
2710          if ($first) {
2711              $xml .= "</gradetree>";
2712          }
2713  
2714          return $xml;
2715      }
2716  
2717      /**
2718       * Returns a JSON representation of the grade-tree using recursion.
2719       *
2720       * @param array $root The current element in the recursion. If null, starts at the top of the tree.
2721       * @param string $tabs Tab characters used to indent the string nicely for humans to enjoy
2722       *
2723       * @return string
2724       */
2725      public function exporttojson($root=null, $tabs="\t") {
2726          $json = null;
2727          $first = false;
2728          if (is_null($root)) {
2729              $root = $this->top_element;
2730              $first = true;
2731          }
2732  
2733          $name = '';
2734  
2735  
2736          if (strpos($root['object']->table, 'grade_categories') !== false) {
2737              $name = $root['object']->fullname;
2738              if ($name == '?') {
2739                  $name = $root['object']->get_name();
2740              }
2741          } else if (strpos($root['object']->table, 'grade_items') !== false) {
2742              $name = $root['object']->itemname;
2743          } else if (strpos($root['object']->table, 'grade_outcomes') !== false) {
2744              $name = $root['object']->itemname;
2745          }
2746  
2747          $json .= "$tabs {\n";
2748          $json .= "$tabs\t \"type\": \"{$root['type']}\",\n";
2749          $json .= "$tabs\t \"name\": \"$name\",\n";
2750  
2751          foreach ($root['object'] as $var => $value) {
2752              if (!is_object($value) && !is_array($value) && !empty($value)) {
2753                  $json .= "$tabs\t \"$var\": \"$value\",\n";
2754              }
2755          }
2756  
2757          $json = substr($json, 0, strrpos($json, ','));
2758  
2759          if (!empty($root['children'])) {
2760              $json .= ",\n$tabs\t\"children\": [\n";
2761              foreach ($root['children'] as $sortorder => $child) {
2762                  $json .= $this->exportToJSON($child, $tabs."\t\t");
2763              }
2764              $json = substr($json, 0, strrpos($json, ','));
2765              $json .= "\n$tabs\t]\n";
2766          }
2767  
2768          if ($first) {
2769              $json .= "\n}";
2770          } else {
2771              $json .= "\n$tabs},\n";
2772          }
2773  
2774          return $json;
2775      }
2776  
2777      /**
2778       * Returns the array of levels
2779       *
2780       * @return array
2781       */
2782      public function get_levels() {
2783          return $this->levels;
2784      }
2785  
2786      /**
2787       * Returns the array of grade items
2788       *
2789       * @return array
2790       */
2791      public function get_items() {
2792          return $this->items;
2793      }
2794  
2795      /**
2796       * Returns a specific Grade Item
2797       *
2798       * @param int $itemid The ID of the grade_item object
2799       *
2800       * @return grade_item
2801       */
2802      public function get_item($itemid) {
2803          if (array_key_exists($itemid, $this->items)) {
2804              return $this->items[$itemid];
2805          } else {
2806              return false;
2807          }
2808      }
2809  }
2810  
2811  /**
2812   * Local shortcut function for creating an edit/delete button for a grade_* object.
2813   * @param string $type 'edit' or 'delete'
2814   * @param int $courseid The Course ID
2815   * @param grade_* $object The grade_* object
2816   * @return string html
2817   */
2818  function grade_button($type, $courseid, $object) {
2819      global $CFG, $OUTPUT;
2820      if (preg_match('/grade_(.*)/', get_class($object), $matches)) {
2821          $objectidstring = $matches[1] . 'id';
2822      } else {
2823          throw new coding_exception('grade_button() only accepts grade_* objects as third parameter!');
2824      }
2825  
2826      $strdelete = get_string('delete');
2827      $stredit   = get_string('edit');
2828  
2829      if ($type == 'delete') {
2830          $url = new moodle_url('index.php', array('id' => $courseid, $objectidstring => $object->id, 'action' => 'delete', 'sesskey' => sesskey()));
2831      } else if ($type == 'edit') {
2832          $url = new moodle_url('edit.php', array('courseid' => $courseid, 'id' => $object->id));
2833      }
2834  
2835      return $OUTPUT->action_icon($url, new pix_icon('t/'.$type, ${'str'.$type}, '', array('class' => 'iconsmall')));
2836  
2837  }
2838  
2839  /**
2840   * This method adds settings to the settings block for the grade system and its
2841   * plugins
2842   *
2843   * @global moodle_page $PAGE
2844   */
2845  function grade_extend_settings($plugininfo, $courseid) {
2846      global $PAGE;
2847  
2848      $gradenode = $PAGE->settingsnav->prepend(get_string('gradeadministration', 'grades'), null, navigation_node::TYPE_CONTAINER);
2849  
2850      $strings = array_shift($plugininfo);
2851  
2852      if ($reports = grade_helper::get_plugins_reports($courseid)) {
2853          foreach ($reports as $report) {
2854              $gradenode->add($report->string, $report->link, navigation_node::TYPE_SETTING, null, $report->id, new pix_icon('i/report', ''));
2855          }
2856      }
2857  
2858      if ($settings = grade_helper::get_info_manage_settings($courseid)) {
2859          $settingsnode = $gradenode->add($strings['settings'], null, navigation_node::TYPE_CONTAINER);
2860          foreach ($settings as $setting) {
2861              $settingsnode->add($setting->string, $setting->link, navigation_node::TYPE_SETTING, null, $setting->id, new pix_icon('i/settings', ''));
2862          }
2863      }
2864  
2865      if ($imports = grade_helper::get_plugins_import($courseid)) {
2866          $importnode = $gradenode->add($strings['import'], null, navigation_node::TYPE_CONTAINER);
2867          foreach ($imports as $import) {
2868              $importnode->add($import->string, $import->link, navigation_node::TYPE_SETTING, null, $import->id, new pix_icon('i/import', ''));
2869          }
2870      }
2871  
2872      if ($exports = grade_helper::get_plugins_export($courseid)) {
2873          $exportnode = $gradenode->add($strings['export'], null, navigation_node::TYPE_CONTAINER);
2874          foreach ($exports as $export) {
2875              $exportnode->add($export->string, $export->link, navigation_node::TYPE_SETTING, null, $export->id, new pix_icon('i/export', ''));
2876          }
2877      }
2878  
2879      if ($letters = grade_helper::get_info_letters($courseid)) {
2880          $letters = array_shift($letters);
2881          $gradenode->add($strings['letter'], $letters->link, navigation_node::TYPE_SETTING, null, $letters->id, new pix_icon('i/settings', ''));
2882      }
2883  
2884      if ($outcomes = grade_helper::get_info_outcomes($courseid)) {
2885          $outcomes = array_shift($outcomes);
2886          $gradenode->add($strings['outcome'], $outcomes->link, navigation_node::TYPE_SETTING, null, $outcomes->id, new pix_icon('i/outcomes', ''));
2887      }
2888  
2889      if ($scales = grade_helper::get_info_scales($courseid)) {
2890          $gradenode->add($strings['scale'], $scales->link, navigation_node::TYPE_SETTING, null, $scales->id, new pix_icon('i/scales', ''));
2891      }
2892  
2893      if ($gradenode->contains_active_node()) {
2894          // If the gradenode is active include the settings base node (gradeadministration) in
2895          // the navbar, typcially this is ignored.
2896          $PAGE->navbar->includesettingsbase = true;
2897  
2898          // If we can get the course admin node make sure it is closed by default
2899          // as in this case the gradenode will be opened
2900          if ($coursenode = $PAGE->settingsnav->get('courseadmin', navigation_node::TYPE_COURSE)){
2901              $coursenode->make_inactive();
2902              $coursenode->forceopen = false;
2903          }
2904      }
2905  }
2906  
2907  /**
2908   * Grade helper class
2909   *
2910   * This class provides several helpful functions that work irrespective of any
2911   * current state.
2912   *
2913   * @copyright 2010 Sam Hemelryk
2914   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2915   */
2916  abstract class grade_helper {
2917      /**
2918       * Cached manage settings info {@see get_info_settings}
2919       * @var grade_plugin_info|false
2920       */
2921      protected static $managesetting = null;
2922      /**
2923       * Cached grade report plugins {@see get_plugins_reports}
2924       * @var array|false
2925       */
2926      protected static $gradereports = null;
2927      /**
2928       * Cached grade report plugins preferences {@see get_info_scales}
2929       * @var array|false
2930       */
2931      protected static $gradereportpreferences = null;
2932      /**
2933       * Cached scale info {@see get_info_scales}
2934       * @var grade_plugin_info|false
2935       */
2936      protected static $scaleinfo = null;
2937      /**
2938       * Cached outcome info {@see get_info_outcomes}
2939       * @var grade_plugin_info|false
2940       */
2941      protected static $outcomeinfo = null;
2942      /**
2943       * Cached leftter info {@see get_info_letters}
2944       * @var grade_plugin_info|false
2945       */
2946      protected static $letterinfo = null;
2947      /**
2948       * Cached grade import plugins {@see get_plugins_import}
2949       * @var array|false
2950       */
2951      protected static $importplugins = null;
2952      /**
2953       * Cached grade export plugins {@see get_plugins_export}
2954       * @var array|false
2955       */
2956      protected static $exportplugins = null;
2957      /**
2958       * Cached grade plugin strings
2959       * @var array
2960       */
2961      protected static $pluginstrings = null;
2962      /**
2963       * Cached grade aggregation strings
2964       * @var array
2965       */
2966      protected static $aggregationstrings = null;
2967  
2968      /**
2969       * Gets strings commonly used by the describe plugins
2970       *
2971       * report => get_string('view'),
2972       * scale => get_string('scales'),
2973       * outcome => get_string('outcomes', 'grades'),
2974       * letter => get_string('letters', 'grades'),
2975       * export => get_string('export', 'grades'),
2976       * import => get_string('import'),
2977       * settings => get_string('settings')
2978       *
2979       * @return array
2980       */
2981      public static function get_plugin_strings() {
2982          if (self::$pluginstrings === null) {
2983              self::$pluginstrings = array(
2984                  'report' => get_string('view'),
2985                  'scale' => get_string('scales'),
2986                  'outcome' => get_string('outcomes', 'grades'),
2987                  'letter' => get_string('letters', 'grades'),
2988                  'export' => get_string('export', 'grades'),
2989                  'import' => get_string('import'),
2990                  'settings' => get_string('edittree', 'grades')
2991              );
2992          }
2993          return self::$pluginstrings;
2994      }
2995  
2996      /**
2997       * Gets strings describing the available aggregation methods.
2998       *
2999       * @return array
3000       */
3001      public static function get_aggregation_strings() {
3002          if (self::$aggregationstrings === null) {
3003              self::$aggregationstrings = array(
3004                  GRADE_AGGREGATE_MEAN             => get_string('aggregatemean', 'grades'),
3005                  GRADE_AGGREGATE_WEIGHTED_MEAN    => get_string('aggregateweightedmean', 'grades'),
3006                  GRADE_AGGREGATE_WEIGHTED_MEAN2   => get_string('aggregateweightedmean2', 'grades'),
3007                  GRADE_AGGREGATE_EXTRACREDIT_MEAN => get_string('aggregateextracreditmean', 'grades'),
3008                  GRADE_AGGREGATE_MEDIAN           => get_string('aggregatemedian', 'grades'),
3009                  GRADE_AGGREGATE_MIN              => get_string('aggregatemin', 'grades'),
3010                  GRADE_AGGREGATE_MAX              => get_string('aggregatemax', 'grades'),
3011                  GRADE_AGGREGATE_MODE             => get_string('aggregatemode', 'grades'),
3012                  GRADE_AGGREGATE_SUM              => get_string('aggregatesum', 'grades')
3013              );
3014          }
3015          return self::$aggregationstrings;
3016      }
3017  
3018      /**
3019       * Get grade_plugin_info object for managing settings if the user can
3020       *
3021       * @param int $courseid
3022       * @return grade_plugin_info[]
3023       */
3024      public static function get_info_manage_settings($courseid) {
3025          if (self::$managesetting !== null) {
3026              return self::$managesetting;
3027          }
3028          $context = context_course::instance($courseid);
3029          self::$managesetting = array();
3030          if ($courseid != SITEID && has_capability('moodle/grade:manage', $context)) {
3031              self::$managesetting['gradebooksetup'] = new grade_plugin_info('setup',
3032                  new moodle_url('/grade/edit/tree/index.php', array('id' => $courseid)),
3033                  get_string('gradebooksetup', 'grades'));
3034              self::$managesetting['coursesettings'] = new grade_plugin_info('coursesettings',
3035                  new moodle_url('/grade/edit/settings/index.php', array('id'=>$courseid)),
3036                  get_string('coursegradesettings', 'grades'));
3037          }
3038          if (self::$gradereportpreferences === null) {
3039              self::get_plugins_reports($courseid);
3040          }
3041          if (self::$gradereportpreferences) {
3042              self::$managesetting = array_merge(self::$managesetting, self::$gradereportpreferences);
3043          }
3044          return self::$managesetting;
3045      }
3046      /**
3047       * Returns an array of plugin reports as grade_plugin_info objects
3048       *
3049       * @param int $courseid
3050       * @return array
3051       */
3052      public static function get_plugins_reports($courseid) {
3053          global $SITE, $CFG;
3054  
3055          if (self::$gradereports !== null) {
3056              return self::$gradereports;
3057          }
3058          $context = context_course::instance($courseid);
3059          $gradereports = array();
3060          $gradepreferences = array();
3061          foreach (core_component::get_plugin_list('gradereport') as $plugin => $plugindir) {
3062              //some reports make no sense if we're not within a course
3063              if ($courseid==$SITE->id && ($plugin=='grader' || $plugin=='user')) {
3064                  continue;
3065              }
3066  
3067              // Remove outcomes report if outcomes not enabled.
3068              if ($plugin === 'outcomes' && empty($CFG->enableoutcomes)) {
3069                  continue;
3070              }
3071  
3072              // Remove ones we can't see
3073              if (!has_capability('gradereport/'.$plugin.':view', $context)) {
3074                  continue;
3075              }
3076  
3077              // Singleview doesn't doesn't accomodate for all cap combos yet, so this is hardcoded..
3078              if ($plugin === 'singleview' && !has_all_capabilities(array('moodle/grade:viewall',
3079                      'moodle/grade:edit'), $context)) {
3080                  continue;
3081              }
3082  
3083              $pluginstr = get_string('pluginname', 'gradereport_'.$plugin);
3084              $url = new moodle_url('/grade/report/'.$plugin.'/index.php', array('id'=>$courseid));
3085              $gradereports[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr);
3086  
3087              // Add link to preferences tab if such a page exists
3088              if (file_exists($plugindir.'/preferences.php')) {
3089                  $url = new moodle_url('/grade/report/'.$plugin.'/preferences.php', array('id'=>$courseid));
3090                  $gradepreferences[$plugin] = new grade_plugin_info($plugin, $url,
3091                      get_string('preferences', 'grades') . ': ' . $pluginstr);
3092              }
3093          }
3094          if (count($gradereports) == 0) {
3095              $gradereports = false;
3096              $gradepreferences = false;
3097          } else if (count($gradepreferences) == 0) {
3098              $gradepreferences = false;
3099              asort($gradereports);
3100          } else {
3101              asort($gradereports);
3102              asort($gradepreferences);
3103          }
3104          self::$gradereports = $gradereports;
3105          self::$gradereportpreferences = $gradepreferences;
3106          return self::$gradereports;
3107      }
3108  
3109      /**
3110       * Get information on scales
3111       * @param int $courseid
3112       * @return grade_plugin_info
3113       */
3114      public static function get_info_scales($courseid) {
3115          if (self::$scaleinfo !== null) {
3116              return self::$scaleinfo;
3117          }
3118          if (has_capability('moodle/course:managescales', context_course::instance($courseid))) {
3119              $url = new moodle_url('/grade/edit/scale/index.php', array('id'=>$courseid));
3120              self::$scaleinfo = new grade_plugin_info('scale', $url, get_string('view'));
3121          } else {
3122              self::$scaleinfo = false;
3123          }
3124          return self::$scaleinfo;
3125      }
3126      /**
3127       * Get information on outcomes
3128       * @param int $courseid
3129       * @return grade_plugin_info
3130       */
3131      public static function get_info_outcomes($courseid) {
3132          global $CFG, $SITE;
3133  
3134          if (self::$outcomeinfo !== null) {
3135              return self::$outcomeinfo;
3136          }
3137          $context = context_course::instance($courseid);
3138          $canmanage = has_capability('moodle/grade:manage', $context);
3139          $canupdate = has_capability('moodle/course:update', $context);
3140          if (!empty($CFG->enableoutcomes) && ($canmanage || $canupdate)) {
3141              $outcomes = array();
3142              if ($canupdate) {
3143                  if ($courseid!=$SITE->id) {
3144                      $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid));
3145                      $outcomes['course'] = new grade_plugin_info('course', $url, get_string('outcomescourse', 'grades'));
3146                  }
3147                  $url = new moodle_url('/grade/edit/outcome/index.php', array('id'=>$courseid));
3148                  $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('editoutcomes', 'grades'));
3149                  $url = new moodle_url('/grade/edit/outcome/import.php', array('courseid'=>$courseid));
3150                  $outcomes['import'] = new grade_plugin_info('import', $url, get_string('importoutcomes', 'grades'));
3151              } else {
3152                  if ($courseid!=$SITE->id) {
3153                      $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid));
3154                      $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('outcomescourse', 'grades'));
3155                  }
3156              }
3157              self::$outcomeinfo = $outcomes;
3158          } else {
3159              self::$outcomeinfo = false;
3160          }
3161          return self::$outcomeinfo;
3162      }
3163      /**
3164       * Get information on letters
3165       * @param int $courseid
3166       * @return array
3167       */
3168      public static function get_info_letters($courseid) {
3169          global $SITE;
3170          if (self::$letterinfo !== null) {
3171              return self::$letterinfo;
3172          }
3173          $context = context_course::instance($courseid);
3174          $canmanage = has_capability('moodle/grade:manage', $context);
3175          $canmanageletters = has_capability('moodle/grade:manageletters', $context);
3176          if ($canmanage || $canmanageletters) {
3177              // Redirect to system context when report is accessed from admin settings MDL-31633
3178              if ($context->instanceid == $SITE->id) {
3179                  $param = array('edit' => 1);
3180              } else {
3181                  $param = array('edit' => 1,'id' => $context->id);
3182              }
3183              self::$letterinfo = array(
3184                  'view' => new grade_plugin_info('view', new moodle_url('/grade/edit/letter/index.php', array('id'=>$context->id)), get_string('view')),
3185                  'edit' => new grade_plugin_info('edit', new moodle_url('/grade/edit/letter/index.php', $param), get_string('edit'))
3186              );
3187          } else {
3188              self::$letterinfo = false;
3189          }
3190          return self::$letterinfo;
3191      }
3192      /**
3193       * Get information import plugins
3194       * @param int $courseid
3195       * @return array
3196       */
3197      public static function get_plugins_import($courseid) {
3198          global $CFG;
3199  
3200          if (self::$importplugins !== null) {
3201              return self::$importplugins;
3202          }
3203          $importplugins = array();
3204          $context = context_course::instance($courseid);
3205  
3206          if (has_capability('moodle/grade:import', $context)) {
3207              foreach (core_component::get_plugin_list('gradeimport') as $plugin => $plugindir) {
3208                  if (!has_capability('gradeimport/'.$plugin.':view', $context)) {
3209                      continue;
3210                  }
3211                  $pluginstr = get_string('pluginname', 'gradeimport_'.$plugin);
3212                  $url = new moodle_url('/grade/import/'.$plugin.'/index.php', array('id'=>$courseid));
3213                  $importplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr);
3214              }
3215  
3216              // Show key manager if grade publishing is enabled and the user has xml publishing capability.
3217              // XML is the only grade import plugin that has publishing feature.
3218              if ($CFG->gradepublishing && has_capability('gradeimport/xml:publish', $context)) {
3219                  $url = new moodle_url('/grade/import/keymanager.php', array('id'=>$courseid));
3220                  $importplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades'));
3221              }
3222          }
3223  
3224          if (count($importplugins) > 0) {
3225              asort($importplugins);
3226              self::$importplugins = $importplugins;
3227          } else {
3228              self::$importplugins = false;
3229          }
3230          return self::$importplugins;
3231      }
3232      /**
3233       * Get information export plugins
3234       * @param int $courseid
3235       * @return array
3236       */
3237      public static function get_plugins_export($courseid) {
3238          global $CFG;
3239  
3240          if (self::$exportplugins !== null) {
3241              return self::$exportplugins;
3242          }
3243          $context = context_course::instance($courseid);
3244          $exportplugins = array();
3245          $canpublishgrades = 0;
3246          if (has_capability('moodle/grade:export', $context)) {
3247              foreach (core_component::get_plugin_list('gradeexport') as $plugin => $plugindir) {
3248                  if (!has_capability('gradeexport/'.$plugin.':view', $context)) {
3249                      continue;
3250                  }
3251                  // All the grade export plugins has grade publishing capabilities.
3252                  if (has_capability('gradeexport/'.$plugin.':publish', $context)) {
3253                      $canpublishgrades++;
3254                  }
3255  
3256                  $pluginstr = get_string('pluginname', 'gradeexport_'.$plugin);
3257                  $url = new moodle_url('/grade/export/'.$plugin.'/index.php', array('id'=>$courseid));
3258                  $exportplugins[$plugin] = new grade_plugin_info($plugin, $url, $pluginstr);
3259              }
3260  
3261              // Show key manager if grade publishing is enabled and the user has at least one grade publishing capability.
3262              if ($CFG->gradepublishing && $canpublishgrades != 0) {
3263                  $url = new moodle_url('/grade/export/keymanager.php', array('id'=>$courseid));
3264                  $exportplugins['keymanager'] = new grade_plugin_info('keymanager', $url, get_string('keymanager', 'grades'));
3265              }
3266          }
3267          if (count($exportplugins) > 0) {
3268              asort($exportplugins);
3269              self::$exportplugins = $exportplugins;
3270          } else {
3271              self::$exportplugins = false;
3272          }
3273          return self::$exportplugins;
3274      }
3275  
3276      /**
3277       * Returns the value of a field from a user record
3278       *
3279       * @param stdClass $user object
3280       * @param stdClass $field object
3281       * @return string value of the field
3282       */
3283      public static function get_user_field_value($user, $field) {
3284          if (!empty($field->customid)) {
3285              $fieldname = 'customfield_' . $field->customid;
3286              if (!empty($user->{$fieldname}) || is_numeric($user->{$fieldname})) {
3287                  $fieldvalue = $user->{$fieldname};
3288              } else {
3289                  $fieldvalue = $field->default;
3290              }
3291          } else {
3292              $fieldvalue = $user->{$field->shortname};
3293          }
3294          return $fieldvalue;
3295      }
3296  
3297      /**
3298       * Returns an array of user profile fields to be included in export
3299       *
3300       * @param int $courseid
3301       * @param bool $includecustomfields
3302       * @return array An array of stdClass instances with customid, shortname, datatype, default and fullname fields
3303       */
3304      public static function get_user_profile_fields($courseid, $includecustomfields = false) {
3305          global $CFG, $DB;
3306  
3307          // Gets the fields that have to be hidden
3308          $hiddenfields = array_map('trim', explode(',', $CFG->hiddenuserfields));
3309          $context = context_course::instance($courseid);
3310          $canseehiddenfields = has_capability('moodle/course:viewhiddenuserfields', $context);
3311          if ($canseehiddenfields) {
3312              $hiddenfields = array();
3313          }
3314  
3315          $fields = array();
3316          require_once($CFG->dirroot.'/user/lib.php');                // Loads user_get_default_fields()
3317          require_once($CFG->dirroot.'/user/profile/lib.php');        // Loads constants, such as PROFILE_VISIBLE_ALL
3318          $userdefaultfields = user_get_default_fields();
3319  
3320          // Sets the list of profile fields
3321          $userprofilefields = array_map('trim', explode(',', $CFG->grade_export_userprofilefields));
3322          if (!empty($userprofilefields)) {
3323              foreach ($userprofilefields as $field) {
3324                  $field = trim($field);
3325                  if (in_array($field, $hiddenfields) || !in_array($field, $userdefaultfields)) {
3326                      continue;
3327                  }
3328                  $obj = new stdClass();
3329                  $obj->customid  = 0;
3330                  $obj->shortname = $field;
3331                  $obj->fullname  = get_string($field);
3332                  $fields[] = $obj;
3333              }
3334          }
3335  
3336          // Sets the list of custom profile fields
3337          $customprofilefields = array_map('trim', explode(',', $CFG->grade_export_customprofilefields));
3338          if ($includecustomfields && !empty($customprofilefields)) {
3339              $customfields = profile_get_user_fields_with_data(0);
3340  
3341              foreach ($customfields as $fieldobj) {
3342                  $field = (object)$fieldobj->get_field_config_for_external();
3343                  // Make sure we can display this custom field
3344                  if (!in_array($field->shortname, $customprofilefields)) {
3345                      continue;
3346                  } else if (in_array($field->shortname, $hiddenfields)) {
3347                      continue;
3348                  } else if ($field->visible != PROFILE_VISIBLE_ALL && !$canseehiddenfields) {
3349                      continue;
3350                  }
3351  
3352                  $obj = new stdClass();
3353                  $obj->customid  = $field->id;
3354                  $obj->shortname = $field->shortname;
3355                  $obj->fullname  = format_string($field->name);
3356                  $obj->datatype  = $field->datatype;
3357                  $obj->default   = $field->defaultdata;
3358                  $fields[] = $obj;
3359              }
3360          }
3361  
3362          return $fields;
3363      }
3364  
3365      /**
3366       * This helper method gets a snapshot of all the weights for a course.
3367       * It is used as a quick method to see if any wieghts have been automatically adjusted.
3368       * @param int $courseid
3369       * @return array of itemid -> aggregationcoef2
3370       */
3371      public static function fetch_all_natural_weights_for_course($courseid) {
3372          global $DB;
3373          $result = array();
3374  
3375          $records = $DB->get_records('grade_items', array('courseid'=>$courseid), 'id', 'id, aggregationcoef2');
3376          foreach ($records as $record) {
3377              $result[$record->id] = $record->aggregationcoef2;
3378          }
3379          return $result;
3380      }
3381  }
3382