PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/grade/lib.php

http://github.com/moodle/moodle
PHP | 3379 lines | 2181 code | 386 blank | 812 comment | 460 complexity | 90cf71998955b91d79c209955e5f4af3 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  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. * Functions used by gradebook plugins and reports.
  18. *
  19. * @package core_grades
  20. * @copyright 2009 Petr Skoda and Nicolas Connault
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once($CFG->libdir . '/gradelib.php');
  24. require_once($CFG->dirroot . '/grade/export/lib.php');
  25. /**
  26. * This class iterates over all users that are graded in a course.
  27. * Returns detailed info about users and their grades.
  28. *
  29. * @author Petr Skoda <skodak@moodle.org>
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class graded_users_iterator {
  33. /**
  34. * The couse whose users we are interested in
  35. */
  36. protected $course;
  37. /**
  38. * An array of grade items or null if only user data was requested
  39. */
  40. protected $grade_items;
  41. /**
  42. * The group ID we are interested in. 0 means all groups.
  43. */
  44. protected $groupid;
  45. /**
  46. * A recordset of graded users
  47. */
  48. protected $users_rs;
  49. /**
  50. * A recordset of user grades (grade_grade instances)
  51. */
  52. protected $grades_rs;
  53. /**
  54. * Array used when moving to next user while iterating through the grades recordset
  55. */
  56. protected $gradestack;
  57. /**
  58. * The first field of the users table by which the array of users will be sorted
  59. */
  60. protected $sortfield1;
  61. /**
  62. * Should sortfield1 be ASC or DESC
  63. */
  64. protected $sortorder1;
  65. /**
  66. * The second field of the users table by which the array of users will be sorted
  67. */
  68. protected $sortfield2;
  69. /**
  70. * Should sortfield2 be ASC or DESC
  71. */
  72. protected $sortorder2;
  73. /**
  74. * Should users whose enrolment has been suspended be ignored?
  75. */
  76. protected $onlyactive = false;
  77. /**
  78. * Enable user custom fields
  79. */
  80. protected $allowusercustomfields = false;
  81. /**
  82. * List of suspended users in course. This includes users whose enrolment status is suspended
  83. * or enrolment has expired or not started.
  84. */
  85. protected $suspendedusers = array();
  86. /**
  87. * Constructor
  88. *
  89. * @param object $course A course object
  90. * @param array $grade_items array of grade items, if not specified only user info returned
  91. * @param int $groupid iterate only group users if present
  92. * @param string $sortfield1 The first field of the users table by which the array of users will be sorted
  93. * @param string $sortorder1 The order in which the first sorting field will be sorted (ASC or DESC)
  94. * @param string $sortfield2 The second field of the users table by which the array of users will be sorted
  95. * @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC)
  96. */
  97. public function __construct($course, $grade_items=null, $groupid=0,
  98. $sortfield1='lastname', $sortorder1='ASC',
  99. $sortfield2='firstname', $sortorder2='ASC') {
  100. $this->course = $course;
  101. $this->grade_items = $grade_items;
  102. $this->groupid = $groupid;
  103. $this->sortfield1 = $sortfield1;
  104. $this->sortorder1 = $sortorder1;
  105. $this->sortfield2 = $sortfield2;
  106. $this->sortorder2 = $sortorder2;
  107. $this->gradestack = array();
  108. }
  109. /**
  110. * Initialise the iterator
  111. *
  112. * @return boolean success
  113. */
  114. public function init() {
  115. global $CFG, $DB;
  116. $this->close();
  117. export_verify_grades($this->course->id);
  118. $course_item = grade_item::fetch_course_item($this->course->id);
  119. if ($course_item->needsupdate) {
  120. // Can not calculate all final grades - sorry.
  121. return false;
  122. }
  123. $coursecontext = context_course::instance($this->course->id);
  124. list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
  125. list($gradebookroles_sql, $params) = $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr');
  126. list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, '', 0, $this->onlyactive);
  127. $params = array_merge($params, $enrolledparams, $relatedctxparams);
  128. if ($this->groupid) {
  129. $groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id";
  130. $groupwheresql = "AND gm.groupid = :groupid";
  131. // $params contents: gradebookroles
  132. $params['groupid'] = $this->groupid;
  133. } else {
  134. $groupsql = "";
  135. $groupwheresql = "";
  136. }
  137. if (empty($this->sortfield1)) {
  138. // We must do some sorting even if not specified.
  139. $ofields = ", u.id AS usrt";
  140. $order = "usrt ASC";
  141. } else {
  142. $ofields = ", u.$this->sortfield1 AS usrt1";
  143. $order = "usrt1 $this->sortorder1";
  144. if (!empty($this->sortfield2)) {
  145. $ofields .= ", u.$this->sortfield2 AS usrt2";
  146. $order .= ", usrt2 $this->sortorder2";
  147. }
  148. if ($this->sortfield1 != 'id' and $this->sortfield2 != 'id') {
  149. // User order MUST be the same in both queries,
  150. // must include the only unique user->id if not already present.
  151. $ofields .= ", u.id AS usrt";
  152. $order .= ", usrt ASC";
  153. }
  154. }
  155. $userfields = 'u.*';
  156. $customfieldssql = '';
  157. if ($this->allowusercustomfields && !empty($CFG->grade_export_customprofilefields)) {
  158. $customfieldscount = 0;
  159. $customfieldsarray = grade_helper::get_user_profile_fields($this->course->id, $this->allowusercustomfields);
  160. foreach ($customfieldsarray as $field) {
  161. if (!empty($field->customid)) {
  162. $customfieldssql .= "
  163. LEFT JOIN (SELECT * FROM {user_info_data}
  164. WHERE fieldid = :cf$customfieldscount) cf$customfieldscount
  165. ON u.id = cf$customfieldscount.userid";
  166. $userfields .= ", cf$customfieldscount.data AS customfield_{$field->customid}";
  167. $params['cf'.$customfieldscount] = $field->customid;
  168. $customfieldscount++;
  169. }
  170. }
  171. }
  172. $users_sql = "SELECT $userfields $ofields
  173. FROM {user} u
  174. JOIN ($enrolledsql) je ON je.id = u.id
  175. $groupsql $customfieldssql
  176. JOIN (
  177. SELECT DISTINCT ra.userid
  178. FROM {role_assignments} ra
  179. WHERE ra.roleid $gradebookroles_sql
  180. AND ra.contextid $relatedctxsql
  181. ) rainner ON rainner.userid = u.id
  182. WHERE u.deleted = 0
  183. $groupwheresql
  184. ORDER BY $order";
  185. $this->users_rs = $DB->get_recordset_sql($users_sql, $params);
  186. if (!$this->onlyactive) {
  187. $context = context_course::instance($this->course->id);
  188. $this->suspendedusers = get_suspended_userids($context);
  189. } else {
  190. $this->suspendedusers = array();
  191. }
  192. if (!empty($this->grade_items)) {
  193. $itemids = array_keys($this->grade_items);
  194. list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items');
  195. $params = array_merge($params, $grades_params);
  196. $grades_sql = "SELECT g.* $ofields
  197. FROM {grade_grades} g
  198. JOIN {user} u ON g.userid = u.id
  199. JOIN ($enrolledsql) je ON je.id = u.id
  200. $groupsql
  201. JOIN (
  202. SELECT DISTINCT ra.userid
  203. FROM {role_assignments} ra
  204. WHERE ra.roleid $gradebookroles_sql
  205. AND ra.contextid $relatedctxsql
  206. ) rainner ON rainner.userid = u.id
  207. WHERE u.deleted = 0
  208. AND g.itemid $itemidsql
  209. $groupwheresql
  210. ORDER BY $order, g.itemid ASC";
  211. $this->grades_rs = $DB->get_recordset_sql($grades_sql, $params);
  212. } else {
  213. $this->grades_rs = false;
  214. }
  215. return true;
  216. }
  217. /**
  218. * Returns information about the next user
  219. * @return mixed array of user info, all grades and feedback or null when no more users found
  220. */
  221. public function next_user() {
  222. if (!$this->users_rs) {
  223. return false; // no users present
  224. }
  225. if (!$this->users_rs->valid()) {
  226. if ($current = $this->_pop()) {
  227. // this is not good - user or grades updated between the two reads above :-(
  228. }
  229. return false; // no more users
  230. } else {
  231. $user = $this->users_rs->current();
  232. $this->users_rs->next();
  233. }
  234. // find grades of this user
  235. $grade_records = array();
  236. while (true) {
  237. if (!$current = $this->_pop()) {
  238. break; // no more grades
  239. }
  240. if (empty($current->userid)) {
  241. break;
  242. }
  243. if ($current->userid != $user->id) {
  244. // grade of the next user, we have all for this user
  245. $this->_push($current);
  246. break;
  247. }
  248. $grade_records[$current->itemid] = $current;
  249. }
  250. $grades = array();
  251. $feedbacks = array();
  252. if (!empty($this->grade_items)) {
  253. foreach ($this->grade_items as $grade_item) {
  254. if (!isset($feedbacks[$grade_item->id])) {
  255. $feedbacks[$grade_item->id] = new stdClass();
  256. }
  257. if (array_key_exists($grade_item->id, $grade_records)) {
  258. $feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback;
  259. $feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat;
  260. unset($grade_records[$grade_item->id]->feedback);
  261. unset($grade_records[$grade_item->id]->feedbackformat);
  262. $grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false);
  263. } else {
  264. $feedbacks[$grade_item->id]->feedback = '';
  265. $feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE;
  266. $grades[$grade_item->id] =
  267. new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false);
  268. }
  269. $grades[$grade_item->id]->grade_item = $grade_item;
  270. }
  271. }
  272. // Set user suspended status.
  273. $user->suspendedenrolment = isset($this->suspendedusers[$user->id]);
  274. $result = new stdClass();
  275. $result->user = $user;
  276. $result->grades = $grades;
  277. $result->feedbacks = $feedbacks;
  278. return $result;
  279. }
  280. /**
  281. * Close the iterator, do not forget to call this function
  282. */
  283. public function close() {
  284. if ($this->users_rs) {
  285. $this->users_rs->close();
  286. $this->users_rs = null;
  287. }
  288. if ($this->grades_rs) {
  289. $this->grades_rs->close();
  290. $this->grades_rs = null;
  291. }
  292. $this->gradestack = array();
  293. }
  294. /**
  295. * Should all enrolled users be exported or just those with an active enrolment?
  296. *
  297. * @param bool $onlyactive True to limit the export to users with an active enrolment
  298. */
  299. public function require_active_enrolment($onlyactive = true) {
  300. if (!empty($this->users_rs)) {
  301. debugging('Calling require_active_enrolment() has no effect unless you call init() again', DEBUG_DEVELOPER);
  302. }
  303. $this->onlyactive = $onlyactive;
  304. }
  305. /**
  306. * Allow custom fields to be included
  307. *
  308. * @param bool $allow Whether to allow custom fields or not
  309. * @return void
  310. */
  311. public function allow_user_custom_fields($allow = true) {
  312. if ($allow) {
  313. $this->allowusercustomfields = true;
  314. } else {
  315. $this->allowusercustomfields = false;
  316. }
  317. }
  318. /**
  319. * Add a grade_grade instance to the grade stack
  320. *
  321. * @param grade_grade $grade Grade object
  322. *
  323. * @return void
  324. */
  325. private function _push($grade) {
  326. array_push($this->gradestack, $grade);
  327. }
  328. /**
  329. * Remove a grade_grade instance from the grade stack
  330. *
  331. * @return grade_grade current grade object
  332. */
  333. private function _pop() {
  334. global $DB;
  335. if (empty($this->gradestack)) {
  336. if (empty($this->grades_rs) || !$this->grades_rs->valid()) {
  337. return null; // no grades present
  338. }
  339. $current = $this->grades_rs->current();
  340. $this->grades_rs->next();
  341. return $current;
  342. } else {
  343. return array_pop($this->gradestack);
  344. }
  345. }
  346. }
  347. /**
  348. * Print a selection popup form of the graded users in a course.
  349. *
  350. * @deprecated since 2.0
  351. *
  352. * @param int $course id of the course
  353. * @param string $actionpage The page receiving the data from the popoup form
  354. * @param int $userid id of the currently selected user (or 'all' if they are all selected)
  355. * @param int $groupid id of requested group, 0 means all
  356. * @param int $includeall bool include all option
  357. * @param bool $return If true, will return the HTML, otherwise, will print directly
  358. * @return null
  359. */
  360. function print_graded_users_selector($course, $actionpage, $userid=0, $groupid=0, $includeall=true, $return=false) {
  361. global $CFG, $USER, $OUTPUT;
  362. return $OUTPUT->render(grade_get_graded_users_select(substr($actionpage, 0, strpos($actionpage, '/')), $course, $userid, $groupid, $includeall));
  363. }
  364. function grade_get_graded_users_select($report, $course, $userid, $groupid, $includeall) {
  365. global $USER, $CFG;
  366. if (is_null($userid)) {
  367. $userid = $USER->id;
  368. }
  369. $coursecontext = context_course::instance($course->id);
  370. $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
  371. $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
  372. $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
  373. $menu = array(); // Will be a list of userid => user name
  374. $menususpendedusers = array(); // Suspended users go to a separate optgroup.
  375. $gui = new graded_users_iterator($course, null, $groupid);
  376. $gui->require_active_enrolment($showonlyactiveenrol);
  377. $gui->init();
  378. $label = get_string('selectauser', 'grades');
  379. if ($includeall) {
  380. $menu[0] = get_string('allusers', 'grades');
  381. $label = get_string('selectalloroneuser', 'grades');
  382. }
  383. while ($userdata = $gui->next_user()) {
  384. $user = $userdata->user;
  385. $userfullname = fullname($user);
  386. if ($user->suspendedenrolment) {
  387. $menususpendedusers[$user->id] = $userfullname;
  388. } else {
  389. $menu[$user->id] = $userfullname;
  390. }
  391. }
  392. $gui->close();
  393. if ($includeall) {
  394. $menu[0] .= " (" . (count($menu) + count($menususpendedusers) - 1) . ")";
  395. }
  396. if (!empty($menususpendedusers)) {
  397. $menu[] = array(get_string('suspendedusers') => $menususpendedusers);
  398. }
  399. $gpr = new grade_plugin_return(array('type' => 'report', 'course' => $course, 'groupid' => $groupid));
  400. $select = new single_select(
  401. new moodle_url('/grade/report/'.$report.'/index.php', $gpr->get_options()),
  402. 'userid', $menu, $userid
  403. );
  404. $select->label = $label;
  405. $select->formid = 'choosegradeuser';
  406. return $select;
  407. }
  408. /**
  409. * Hide warning about changed grades during upgrade to 2.8.
  410. *
  411. * @param int $courseid The current course id.
  412. */
  413. function hide_natural_aggregation_upgrade_notice($courseid) {
  414. unset_config('show_sumofgrades_upgrade_' . $courseid);
  415. }
  416. /**
  417. * Hide warning about changed grades during upgrade from 2.8.0-2.8.6 and 2.9.0.
  418. *
  419. * @param int $courseid The current course id.
  420. */
  421. function grade_hide_min_max_grade_upgrade_notice($courseid) {
  422. unset_config('show_min_max_grades_changed_' . $courseid);
  423. }
  424. /**
  425. * Use the grade min and max from the grade_grade.
  426. *
  427. * This is reserved for core use after an upgrade.
  428. *
  429. * @param int $courseid The current course id.
  430. */
  431. function grade_upgrade_use_min_max_from_grade_grade($courseid) {
  432. grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_GRADE);
  433. grade_force_full_regrading($courseid);
  434. // Do this now, because it probably happened to late in the page load to be happen automatically.
  435. grade_regrade_final_grades($courseid);
  436. }
  437. /**
  438. * Use the grade min and max from the grade_item.
  439. *
  440. * This is reserved for core use after an upgrade.
  441. *
  442. * @param int $courseid The current course id.
  443. */
  444. function grade_upgrade_use_min_max_from_grade_item($courseid) {
  445. grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM);
  446. grade_force_full_regrading($courseid);
  447. // Do this now, because it probably happened to late in the page load to be happen automatically.
  448. grade_regrade_final_grades($courseid);
  449. }
  450. /**
  451. * Hide warning about changed grades during upgrade to 2.8.
  452. *
  453. * @param int $courseid The current course id.
  454. */
  455. function hide_aggregatesubcats_upgrade_notice($courseid) {
  456. unset_config('show_aggregatesubcats_upgrade_' . $courseid);
  457. }
  458. /**
  459. * Hide warning about changed grades due to bug fixes
  460. *
  461. * @param int $courseid The current course id.
  462. */
  463. function hide_gradebook_calculations_freeze_notice($courseid) {
  464. unset_config('gradebook_calculations_freeze_' . $courseid);
  465. }
  466. /**
  467. * Print warning about changed grades during upgrade to 2.8.
  468. *
  469. * @param int $courseid The current course id.
  470. * @param context $context The course context.
  471. * @param string $thispage The relative path for the current page. E.g. /grade/report/user/index.php
  472. * @param boolean $return return as string
  473. *
  474. * @return nothing or string if $return true
  475. */
  476. function print_natural_aggregation_upgrade_notice($courseid, $context, $thispage, $return=false) {
  477. global $CFG, $OUTPUT;
  478. $html = '';
  479. // Do not do anything if they cannot manage the grades of this course.
  480. if (!has_capability('moodle/grade:manage', $context)) {
  481. return $html;
  482. }
  483. $hidesubcatswarning = optional_param('seenaggregatesubcatsupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
  484. $showsubcatswarning = get_config('core', 'show_aggregatesubcats_upgrade_' . $courseid);
  485. $hidenaturalwarning = optional_param('seensumofgradesupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
  486. $shownaturalwarning = get_config('core', 'show_sumofgrades_upgrade_' . $courseid);
  487. $hideminmaxwarning = optional_param('seenminmaxupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
  488. $showminmaxwarning = get_config('core', 'show_min_max_grades_changed_' . $courseid);
  489. $useminmaxfromgradeitem = optional_param('useminmaxfromgradeitem', false, PARAM_BOOL) && confirm_sesskey();
  490. $useminmaxfromgradegrade = optional_param('useminmaxfromgradegrade', false, PARAM_BOOL) && confirm_sesskey();
  491. $minmaxtouse = grade_get_setting($courseid, 'minmaxtouse', $CFG->grade_minmaxtouse);
  492. $gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
  493. $acceptgradebookchanges = optional_param('acceptgradebookchanges', false, PARAM_BOOL) && confirm_sesskey();
  494. // Hide the warning if the user told it to go away.
  495. if ($hidenaturalwarning) {
  496. hide_natural_aggregation_upgrade_notice($courseid);
  497. }
  498. // Hide the warning if the user told it to go away.
  499. if ($hidesubcatswarning) {
  500. hide_aggregatesubcats_upgrade_notice($courseid);
  501. }
  502. // Hide the min/max warning if the user told it to go away.
  503. if ($hideminmaxwarning) {
  504. grade_hide_min_max_grade_upgrade_notice($courseid);
  505. $showminmaxwarning = false;
  506. }
  507. if ($useminmaxfromgradegrade) {
  508. // Revert to the new behaviour, we now use the grade_grade for min/max.
  509. grade_upgrade_use_min_max_from_grade_grade($courseid);
  510. grade_hide_min_max_grade_upgrade_notice($courseid);
  511. $showminmaxwarning = false;
  512. } else if ($useminmaxfromgradeitem) {
  513. // Apply the new logic, we now use the grade_item for min/max.
  514. grade_upgrade_use_min_max_from_grade_item($courseid);
  515. grade_hide_min_max_grade_upgrade_notice($courseid);
  516. $showminmaxwarning = false;
  517. }
  518. if (!$hidenaturalwarning && $shownaturalwarning) {
  519. $message = get_string('sumofgradesupgradedgrades', 'grades');
  520. $hidemessage = get_string('upgradedgradeshidemessage', 'grades');
  521. $urlparams = array( 'id' => $courseid,
  522. 'seensumofgradesupgradedgrades' => true,
  523. 'sesskey' => sesskey());
  524. $goawayurl = new moodle_url($thispage, $urlparams);
  525. $goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
  526. $html .= $OUTPUT->notification($message, 'notifysuccess');
  527. $html .= $goawaybutton;
  528. }
  529. if (!$hidesubcatswarning && $showsubcatswarning) {
  530. $message = get_string('aggregatesubcatsupgradedgrades', 'grades');
  531. $hidemessage = get_string('upgradedgradeshidemessage', 'grades');
  532. $urlparams = array( 'id' => $courseid,
  533. 'seenaggregatesubcatsupgradedgrades' => true,
  534. 'sesskey' => sesskey());
  535. $goawayurl = new moodle_url($thispage, $urlparams);
  536. $goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
  537. $html .= $OUTPUT->notification($message, 'notifysuccess');
  538. $html .= $goawaybutton;
  539. }
  540. if ($showminmaxwarning) {
  541. $hidemessage = get_string('upgradedgradeshidemessage', 'grades');
  542. $urlparams = array( 'id' => $courseid,
  543. 'seenminmaxupgradedgrades' => true,
  544. 'sesskey' => sesskey());
  545. $goawayurl = new moodle_url($thispage, $urlparams);
  546. $hideminmaxbutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
  547. $moreinfo = html_writer::link(get_docs_url(get_string('minmaxtouse_link', 'grades')), get_string('moreinfo'),
  548. array('target' => '_blank'));
  549. if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_ITEM) {
  550. // Show the message that there were min/max issues that have been resolved.
  551. $message = get_string('minmaxupgradedgrades', 'grades') . ' ' . $moreinfo;
  552. $revertmessage = get_string('upgradedminmaxrevertmessage', 'grades');
  553. $urlparams = array('id' => $courseid,
  554. 'useminmaxfromgradegrade' => true,
  555. 'sesskey' => sesskey());
  556. $reverturl = new moodle_url($thispage, $urlparams);
  557. $revertbutton = $OUTPUT->single_button($reverturl, $revertmessage, 'get');
  558. $html .= $OUTPUT->notification($message);
  559. $html .= $revertbutton . $hideminmaxbutton;
  560. } else if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_GRADE) {
  561. // Show the warning that there are min/max issues that have not be resolved.
  562. $message = get_string('minmaxupgradewarning', 'grades') . ' ' . $moreinfo;
  563. $fixmessage = get_string('minmaxupgradefixbutton', 'grades');
  564. $urlparams = array('id' => $courseid,
  565. 'useminmaxfromgradeitem' => true,
  566. 'sesskey' => sesskey());
  567. $fixurl = new moodle_url($thispage, $urlparams);
  568. $fixbutton = $OUTPUT->single_button($fixurl, $fixmessage, 'get');
  569. $html .= $OUTPUT->notification($message);
  570. $html .= $fixbutton . $hideminmaxbutton;
  571. }
  572. }
  573. if ($gradebookcalculationsfreeze) {
  574. if ($acceptgradebookchanges) {
  575. // Accept potential changes in grades caused by extra credit bug MDL-49257.
  576. hide_gradebook_calculations_freeze_notice($courseid);
  577. $courseitem = grade_item::fetch_course_item($courseid);
  578. $courseitem->force_regrading();
  579. grade_regrade_final_grades($courseid);
  580. $html .= $OUTPUT->notification(get_string('gradebookcalculationsuptodate', 'grades'), 'notifysuccess');
  581. } else {
  582. // Show the warning that there may be extra credit weights problems.
  583. $a = new stdClass();
  584. $a->gradebookversion = $gradebookcalculationsfreeze;
  585. if (preg_match('/(\d{8,})/', $CFG->release, $matches)) {
  586. $a->currentversion = $matches[1];
  587. } else {
  588. $a->currentversion = $CFG->release;
  589. }
  590. $a->url = get_docs_url('Gradebook_calculation_changes');
  591. $message = get_string('gradebookcalculationswarning', 'grades', $a);
  592. $fixmessage = get_string('gradebookcalculationsfixbutton', 'grades');
  593. $urlparams = array('id' => $courseid,
  594. 'acceptgradebookchanges' => true,
  595. 'sesskey' => sesskey());
  596. $fixurl = new moodle_url($thispage, $urlparams);
  597. $fixbutton = $OUTPUT->single_button($fixurl, $fixmessage, 'get');
  598. $html .= $OUTPUT->notification($message);
  599. $html .= $fixbutton;
  600. }
  601. }
  602. if (!empty($html)) {
  603. $html = html_writer::tag('div', $html, array('class' => 'core_grades_notices'));
  604. }
  605. if ($return) {
  606. return $html;
  607. } else {
  608. echo $html;
  609. }
  610. }
  611. /**
  612. * Print grading plugin selection popup form.
  613. *
  614. * @param array $plugin_info An array of plugins containing information for the selector
  615. * @param boolean $return return as string
  616. *
  617. * @return nothing or string if $return true
  618. */
  619. function print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return=false) {
  620. global $CFG, $OUTPUT, $PAGE;
  621. $menu = array();
  622. $count = 0;
  623. $active = '';
  624. foreach ($plugin_info as $plugin_type => $plugins) {
  625. if ($plugin_type == 'strings') {
  626. continue;
  627. }
  628. $first_plugin = reset($plugins);
  629. $sectionname = $plugin_info['strings'][$plugin_type];
  630. $section = array();
  631. foreach ($plugins as $plugin) {
  632. $link = $plugin->link->out(false);
  633. $section[$link] = $plugin->string;
  634. $count++;
  635. if ($plugin_type === $active_type and $plugin->id === $active_plugin) {
  636. $active = $link;
  637. }
  638. }
  639. if ($section) {
  640. $menu[] = array($sectionname=>$section);
  641. }
  642. }
  643. // finally print/return the popup form
  644. if ($count > 1) {
  645. $select = new url_select($menu, $active, null, 'choosepluginreport');
  646. $select->set_label(get_string('gradereport', 'grades'), array('class' => 'accesshide'));
  647. if ($return) {
  648. return $OUTPUT->render($select);
  649. } else {
  650. echo $OUTPUT->render($select);
  651. }
  652. } else {
  653. // only one option - no plugin selector needed
  654. return '';
  655. }
  656. }
  657. /**
  658. * Print grading plugin selection tab-based navigation.
  659. *
  660. * @param string $active_type type of plugin on current page - import, export, report or edit
  661. * @param string $active_plugin active plugin type - grader, user, cvs, ...
  662. * @param array $plugin_info Array of plugins
  663. * @param boolean $return return as string
  664. *
  665. * @return nothing or string if $return true
  666. */
  667. function grade_print_tabs($active_type, $active_plugin, $plugin_info, $return=false) {
  668. global $CFG, $COURSE;
  669. if (!isset($currenttab)) { //TODO: this is weird
  670. $currenttab = '';
  671. }
  672. $tabs = array();
  673. $top_row = array();
  674. $bottom_row = array();
  675. $inactive = array($active_plugin);
  676. $activated = array($active_type);
  677. $count = 0;
  678. $active = '';
  679. foreach ($plugin_info as $plugin_type => $plugins) {
  680. if ($plugin_type == 'strings') {
  681. continue;
  682. }
  683. // If $plugins is actually the definition of a child-less parent link:
  684. if (!empty($plugins->id)) {
  685. $string = $plugins->string;
  686. if (!empty($plugin_info[$active_type]->parent)) {
  687. $string = $plugin_info[$active_type]->parent->string;
  688. }
  689. $top_row[] = new tabobject($plugin_type, $plugins->link, $string);
  690. continue;
  691. }
  692. $first_plugin = reset($plugins);
  693. $url = $first_plugin->link;
  694. if ($plugin_type == 'report') {
  695. $url = $CFG->wwwroot.'/grade/report/index.php?id='.$COURSE->id;
  696. }
  697. $top_row[] = new tabobject($plugin_type, $url, $plugin_info['strings'][$plugin_type]);
  698. if ($active_type == $plugin_type) {
  699. foreach ($plugins as $plugin) {
  700. $bottom_row[] = new tabobject($plugin->id, $plugin->link, $plugin->string);
  701. if ($plugin->id == $active_plugin) {
  702. $inactive = array($plugin->id);
  703. }
  704. }
  705. }
  706. }
  707. // Do not display rows that contain only one item, they are not helpful.
  708. if (count($top_row) > 1) {
  709. $tabs[] = $top_row;
  710. }
  711. if (count($bottom_row) > 1) {
  712. $tabs[] = $bottom_row;
  713. }
  714. if (empty($tabs)) {
  715. return;
  716. }
  717. $rv = html_writer::div(print_tabs($tabs, $active_plugin, $inactive, $activated, true), 'grade-navigation');
  718. if ($return) {
  719. return $rv;
  720. } else {
  721. echo $rv;
  722. }
  723. }
  724. /**
  725. * grade_get_plugin_info
  726. *
  727. * @param int $courseid The course id
  728. * @param string $active_type type of plugin on current page - import, export, report or edit
  729. * @param string $active_plugin active plugin type - grader, user, cvs, ...
  730. *
  731. * @return array
  732. */
  733. function grade_get_plugin_info($courseid, $active_type, $active_plugin) {
  734. global $CFG, $SITE;
  735. $context = context_course::instance($courseid);
  736. $plugin_info = array();
  737. $count = 0;
  738. $active = '';
  739. $url_prefix = $CFG->wwwroot . '/grade/';
  740. // Language strings
  741. $plugin_info['strings'] = grade_helper::get_plugin_strings();
  742. if ($reports = grade_helper::get_plugins_reports($courseid)) {
  743. $plugin_info['report'] = $reports;
  744. }
  745. if ($settings = grade_helper::get_info_manage_settings($courseid)) {
  746. $plugin_info['settings'] = $settings;
  747. }
  748. if ($scale = grade_helper::get_info_scales($courseid)) {
  749. $plugin_info['scale'] = array('view'=>$scale);
  750. }
  751. if ($outcomes = grade_helper::get_info_outcomes($courseid)) {
  752. $plugin_info['outcome'] = $outcomes;
  753. }
  754. if ($letters = grade_helper::get_info_letters($courseid)) {
  755. $plugin_info['letter'] = $letters;
  756. }
  757. if ($imports = grade_helper::get_plugins_import($courseid)) {
  758. $plugin_info['import'] = $imports;
  759. }
  760. if ($exports = grade_helper::get_plugins_export($courseid)) {
  761. $plugin_info['export'] = $exports;
  762. }
  763. foreach ($plugin_info as $plugin_type => $plugins) {
  764. if (!empty($plugins->id) && $active_plugin == $plugins->id) {
  765. $plugin_info['strings']['active_plugin_str'] = $plugins->string;
  766. break;
  767. }
  768. foreach ($plugins as $plugin) {
  769. if (is_a($plugin, 'grade_plugin_info')) {
  770. if ($active_plugin == $plugin->id) {
  771. $plugin_info['strings']['active_plugin_str'] = $plugin->string;
  772. }
  773. }
  774. }
  775. }
  776. return $plugin_info;
  777. }
  778. /**
  779. * A simple class containing info about grade plugins.
  780. * Can be subclassed for special rules
  781. *
  782. * @package core_grades
  783. * @copyright 2009 Nicolas Connault
  784. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  785. */
  786. class grade_plugin_info {
  787. /**
  788. * A unique id for this plugin
  789. *
  790. * @var mixed
  791. */
  792. public $id;
  793. /**
  794. * A URL to access this plugin
  795. *
  796. * @var mixed
  797. */
  798. public $link;
  799. /**
  800. * The name of this plugin
  801. *
  802. * @var mixed
  803. */
  804. public $string;
  805. /**
  806. * Another grade_plugin_info object, parent of the current one
  807. *
  808. * @var mixed
  809. */
  810. public $parent;
  811. /**
  812. * Constructor
  813. *
  814. * @param int $id A unique id for this plugin
  815. * @param string $link A URL to access this plugin
  816. * @param string $string The name of this plugin
  817. * @param object $parent Another grade_plugin_info object, parent of the current one
  818. *
  819. * @return void
  820. */
  821. public function __construct($id, $link, $string, $parent=null) {
  822. $this->id = $id;
  823. $this->link = $link;
  824. $this->string = $string;
  825. $this->parent = $parent;
  826. }
  827. }
  828. /**
  829. * Prints the page headers, breadcrumb trail, page heading, (optional) dropdown navigation menu and
  830. * (optional) navigation tabs for any gradebook page. All gradebook pages MUST use these functions
  831. * in favour of the usual print_header(), print_header_simple(), print_heading() etc.
  832. * !IMPORTANT! Use of tabs.php file in gradebook pages is forbidden unless tabs are switched off at
  833. * the site level for the gradebook ($CFG->grade_navmethod = GRADE_NAVMETHOD_DROPDOWN).
  834. *
  835. * @param int $courseid Course id
  836. * @param string $active_type The type of the current page (report, settings,
  837. * import, export, scales, outcomes, letters)
  838. * @param string $active_plugin The plugin of the current page (grader, fullview etc...)
  839. * @param string $heading The heading of the page. Tries to guess if none is given
  840. * @param boolean $return Whether to return (true) or echo (false) the HTML generated by this function
  841. * @param string $bodytags Additional attributes that will be added to the <body> tag
  842. * @param string $buttons Additional buttons to display on the page
  843. * @param boolean $shownavigation should the gradebook navigation drop down (or tabs) be shown?
  844. * @param string $headerhelpidentifier The help string identifier if required.
  845. * @param string $headerhelpcomponent The component for the help string.
  846. * @param stdClass $user The user object for use with the user context header.
  847. *
  848. * @return string HTML code or nothing if $return == false
  849. */
  850. function print_grade_page_head($courseid, $active_type, $active_plugin=null,
  851. $heading = false, $return=false,
  852. $buttons=false, $shownavigation=true, $headerhelpidentifier = null, $headerhelpcomponent = null,
  853. $user = null) {
  854. global $CFG, $OUTPUT, $PAGE;
  855. // Put a warning on all gradebook pages if the course has modules currently scheduled for background deletion.
  856. require_once($CFG->dirroot . '/course/lib.php');
  857. if (course_modules_pending_deletion($courseid, true)) {
  858. \core\notification::add(get_string('gradesmoduledeletionpendingwarning', 'grades'),
  859. \core\output\notification::NOTIFY_WARNING);
  860. }
  861. if ($active_type === 'preferences') {
  862. // In Moodle 2.8 report preferences were moved under 'settings'. Allow backward compatibility for 3rd party grade reports.
  863. $active_type = 'settings';
  864. }
  865. $plugin_info = grade_get_plugin_info($courseid, $active_type, $active_plugin);
  866. // Determine the string of the active plugin
  867. $stractive_plugin = ($active_plugin) ? $plugin_info['strings']['active_plugin_str'] : $heading;
  868. $stractive_type = $plugin_info['strings'][$active_type];
  869. if (empty($plugin_info[$active_type]->id) || !empty($plugin_info[$active_type]->parent)) {
  870. $title = $PAGE->course->fullname.': ' . $stractive_type . ': ' . $stractive_plugin;
  871. } else {
  872. $title = $PAGE->course->fullname.': ' . $stractive_plugin;
  873. }
  874. if ($active_type == 'report') {
  875. $PAGE->set_pagelayout('report');
  876. } else {
  877. $PAGE->set_pagelayout('admin');
  878. }
  879. $PAGE->set_title(get_string('grades') . ': ' . $stractive_type);
  880. $PAGE->set_heading($title);
  881. if ($buttons instanceof single_button) {
  882. $buttons = $OUTPUT->render($buttons);
  883. }
  884. $PAGE->set_button($buttons);
  885. if ($courseid != SITEID) {
  886. grade_extend_settings($plugin_info, $courseid);
  887. }
  888. // Set the current report as active in the breadcrumbs.
  889. if ($active_plugin !== null && $reportnav = $PAGE->settingsnav->find($active_plugin, navigation_node::TYPE_SETTING)) {
  890. $reportnav->make_active();
  891. }
  892. $returnval = $OUTPUT->header();
  893. if (!$return) {
  894. echo $returnval;
  895. }
  896. // Guess heading if not given explicitly
  897. if (!$heading) {
  898. $heading = $stractive_plugin;
  899. }
  900. if ($shownavigation) {
  901. $navselector = null;
  902. if ($courseid != SITEID &&
  903. ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_DROPDOWN)) {
  904. // It's absolutely essential that this grade plugin selector is shown after the user header. Just ask Fred.
  905. $navselector = print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, true);
  906. if ($return) {
  907. $returnval .= $navselector;
  908. } else if (!isset($user)) {
  909. echo $navselector;
  910. }
  911. }
  912. $output = '';
  913. // Add a help dialogue box if provided.
  914. if (isset($headerhelpidentifier)) {
  915. $output = $OUTPUT->heading_with_help($heading, $headerhelpidentifier, $headerhelpcomponent);
  916. } else {
  917. if (isset($user)) {
  918. $output = $OUTPUT->context_header(
  919. array(
  920. 'heading' => html_writer::link(new moodle_url('/user/view.php', array('id' => $user->id,
  921. 'course' => $courseid)), fullname($user)),
  922. 'user' => $user,
  923. 'usercontext' => context_user::instance($user->id)
  924. ), 2
  925. ) . $navselector;
  926. } else {
  927. $output = $OUTPUT->heading($heading);
  928. }
  929. }
  930. if ($return) {
  931. $returnval .= $output;
  932. } else {
  933. echo $output;
  934. }
  935. if ($courseid != SITEID &&
  936. ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_TABS)) {
  937. $returnval .= grade_print_tabs($active_type, $active_plugin, $plugin_info, $return);
  938. }
  939. }
  940. $returnval .= print_natural_aggregation_upgrade_notice($courseid,
  941. context_course::instance($courseid),
  942. $PAGE->url,
  943. $return);
  944. if ($return) {
  945. return $returnval;
  946. }
  947. }
  948. /**
  949. * Utility class used for return tracking when using edit and other forms in grade plugins
  950. *
  951. * @package core_grades
  952. * @copyright 2009 Nicolas Connault
  953. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  954. */
  955. class grade_plugin_return {
  956. /**
  957. * Type of grade plugin (e.g. 'edit', 'report')
  958. *
  959. * @var string
  960. */
  961. public $type;
  962. /**
  963. * Name of grade plugin (e.g. 'grader', 'overview')
  964. *
  965. * @var string
  966. */
  967. public $plugin;
  968. /**
  969. * Course id being viewed
  970. *
  971. * @var int
  972. */
  973. public $courseid;
  974. /**
  975. * Id of user whose information is being viewed/edited
  976. *
  977. * @var int
  978. */
  979. public $userid;
  980. /**
  981. * Id of group for which information is being viewed/edited
  982. *
  983. * @var int
  984. */
  985. public $groupid;
  986. /**
  987. * Current page # within output
  988. *
  989. * @var int
  990. */
  991. public $page;
  992. /**
  993. * Constructor
  994. *
  995. * @param array $params - associative array with return parameters, if not supplied parameter are taken from _GET or _POST
  996. */
  997. public function __construct($params = []) {
  998. $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR);
  999. $this->plugin = optional_param('gpr_plugin', null, PARAM_PLUGIN);
  1000. $this->courseid = optional_param('gpr_courseid', null, PARAM_INT);
  1001. $this->userid = optional_param('gpr_userid', null, PARAM_INT);
  1002. $this->groupid = optional_param('gpr_groupid', null, PARAM_INT);
  1003. $this->page = optional_param('gpr_page', null, PARAM_INT);
  1004. foreach ($params as $key => $value) {
  1005. if (property_exists($this, $key)) {
  1006. $this->$key = $value;
  1007. }
  1008. }
  1009. // Allow course object rather than id to be used to specify course
  1010. // - avoid unnecessary use of get_course.
  1011. if (array_key_exists('course', $params)) {
  1012. $course = $params['course'];
  1013. $this->courseid = $course->id;
  1014. } else {
  1015. $course = null;
  1016. }
  1017. // If group has been explicitly set in constructor parameters,
  1018. // we should respect that.
  1019. if (!array_key_exists('groupid', $params)) {
  1020. // Otherwise, 'group' in request parameters is a request for a change.
  1021. // In that case, or if we have no group at all, we should get groupid from
  1022. // groups_get_course_group, which will do some housekeeping as well as
  1023. // give us the correct value.
  1024. $changegroup = optional_param('group', -1, PARAM_INT);
  1025. if ($changegroup !== -1 or (empty($this->groupid) and !empty($this->courseid))) {
  1026. if ($course === null) {
  1027. $course = get_course($this->courseid);
  1028. }
  1029. $this->groupid = groups_get_course_group($course, true);
  1030. }
  1031. }
  1032. }
  1033. /**
  1034. * Old syntax of class constructor. Deprecated in PHP7.
  1035. *
  1036. * @deprecated since Moodle 3.1
  1037. */
  1038. public function grade_plugin_return($params = null) {
  1039. debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  1040. self::__construct($params);
  1041. }
  1042. /**
  1043. * Returns return parameters as options array suitable for buttons.
  1044. * @return array options
  1045. */
  1046. public function get_options() {
  1047. if (empty($this->type)) {
  1048. return array();
  1049. }
  1050. $params = array();
  1051. if (!empty($this->plugin)) {
  1052. $params['plugin'] = $this->plugin;
  1053. }
  1054. if (!empty($this->courseid)) {
  1055. $params['id'] = $this->courseid;
  1056. }
  1057. if (!empty($this->userid)) {
  1058. $params['userid'] = $this->userid;
  1059. }
  1060. if (!empty($this->groupid)) {
  1061. $params['group'] = $this->groupid;
  1062. }
  1063. if (!empty($this->page)) {
  1064. $params['page'] = $this->page;
  1065. }
  1066. return $params;
  1067. }
  1068. /**
  1069. * Returns return url
  1070. *
  1071. * @param string $default default url when params not set
  1072. * @param array $extras Extra URL parameters
  1073. *
  1074. * @return string url
  1075. */
  1076. public function get_return_url($default, $extras=null) {
  1077. global $CFG;
  1078. if (empty($this->type) or empty($this->plugin)) {
  1079. return $default;
  1080. }
  1081. $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php';
  1082. $glue = '?';
  1083. if (!empty($this->courseid)) {
  1084. $url .= $glue.'id='.$this->courseid;
  1085. $glue = '&amp;';
  1086. }
  1087. if (!empty($this->userid)) {
  1088. $url .= $glue.'userid='.$this->userid;
  1089. $glue = '&amp;';
  1090. }
  1091. if (!empty($this->groupid)) {
  1092. $url .= $glue.'group='.$this->groupid;
  1093. $glue = '&amp;';
  1094. }
  1095. if (!empty($this->page)) {
  1096. $url .= $glue.'page='.$this->page;
  1097. $glue = '&amp;';
  1098. }
  1099. if (!empty($extras)) {
  1100. foreach ($extras as $key=>$value) {
  1101. $url .= $glue.$key.'='.$value;
  1102. $glue = '&amp;';
  1103. }
  1104. }
  1105. return $url;
  1106. }
  1107. /**
  1108. * Returns string with hidden return tracking form elements.
  1109. * @return string
  1110. */
  1111. public function get_form_fields() {
  1112. if (empty($this->type)) {
  1113. return '';
  1114. }
  1115. $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />';
  1116. if (!empty($this->plugin)) {
  1117. $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />';
  1118. }
  1119. if (!empty($this->courseid)) {
  1120. $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />';
  1121. }
  1122. if (!empty($this->userid)) {
  1123. $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />';
  1124. }
  1125. if (!empty($this->groupid)) {
  1126. $result .= '<input type="hidden" name="gpr_groupid" value="'.$this->groupid.'" />';
  1127. }
  1128. if (!empty($this->page)) {
  1129. $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />';
  1130. }
  1131. return $result;
  1132. }
  1133. /**
  1134. * Add hidden elements into mform
  1135. *
  1136. * @param object &$mform moodle form object
  1137. *
  1138. * @return void
  1139. */
  1140. public function add_mform_elements(&$mform) {
  1141. if (empty($this->type)) {
  1142. return;
  1143. }
  1144. $mform->addElement('hidden', 'gpr_type', $this->type);
  1145. $mform->setType('gpr_type', PARAM_SAFEDIR);
  1146. if (!empty($this->plugin)) {
  1147. $mform->addElement('hidden', 'gpr_plugin', $this->plugin);
  1148. $mform->setType('gpr_plugin', PARAM_PLUGIN);
  1149. }
  1150. if (!empty($this->courseid)) {
  1151. $mform->addElement('hidden', 'gpr_courseid', $this->courseid);
  1152. $mform->setType('gpr_courseid', PARAM_INT);
  1153. }
  1154. if (!empty($this->userid)) {
  1155. $mform->addElement('hidden', 'gpr_userid', $this->userid);
  1156. $mform->setType('gpr_userid', PARAM_INT);
  1157. }
  1158. if (!empty($this->groupid)) {
  1159. $mform->addElement('hidden', 'gpr_groupid', $this->groupid);
  1160. $mform->setType('gpr_groupid', PARAM_INT);
  1161. }
  1162. if (!empty($this->page)) {
  1163. $mform->addElement('hidden', 'gpr_page', $this->page);
  1164. $mform->setType('gpr_page', PARAM_INT);
  1165. }
  1166. }
  1167. /**
  1168. * Add return tracking params into url
  1169. *
  1170. * @param moodle_url $url A URL
  1171. *
  1172. * @return string $url with return tracking params
  1173. */
  1174. public function add_url_params(moodle_url $url) {
  1175. if (empty($this->type)) {
  1176. return $url;
  1177. }
  1178. $url->param('gpr_type', $this->type);
  1179. if (!empty($this->plugin)) {
  1180. $url->param('gpr_plugin', $this->plugin);
  1181. }
  1182. if (!empty($this->courseid)) {
  1183. $url->param('gpr_courseid' ,$this->courseid);
  1184. }
  1185. if (!empty($this->userid)) {
  1186. $url->param('gpr_userid', $this->userid);
  1187. }
  1188. if (!empty($this->groupid)) {
  1189. $url->param('gpr_groupid', $this->groupid);
  1190. }
  1191. if (!empty($this->page)) {
  1192. $url->param('gpr_page', $this->page);
  1193. }
  1194. return $url;
  1195. }
  1196. }
  1197. /**
  1198. * Function central to gradebook for building and printing the navigation (breadcrumb trail).
  1199. *
  1200. * @param string $path The path of the calling script (using __FILE__?)
  1201. * @param string $pagename The language string to use as the last part of the navigation (non-link)
  1202. * @param mixed $id Either a plain integer (assuming the key is 'id') or
  1203. * an array of keys and values (e.g courseid => $courseid, itemid...)
  1204. *
  1205. * @return string
  1206. */
  1207. function grade_build_nav($path, $pagename=null, $id=null) {
  1208. global $CFG, $COURSE, $PAGE;
  1209. $strgrades = get_string('grades', 'grades');
  1210. // Parse the path and build navlinks from its elements
  1211. $dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash
  1212. $path = substr($path, $dirroot_length);
  1213. $path = str_replace('\\', '/', $path);
  1214. $path_elements = explode('/', $path);
  1215. $path_elements_count = count($path_elements);
  1216. // First link is always 'grade'
  1217. $PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$COURSE->id)));
  1218. $link = null;
  1219. $numberofelements = 3;
  1220. // Prepare URL params string
  1221. $linkparams = array();
  1222. if (!is_null($id)) {
  1223. if (is_array($id)) {
  1224. foreach ($id as $idkey => $idvalue) {
  1225. $linkparams[$idkey] = $idvalue;
  1226. }
  1227. } else {
  1228. $linkparams['id'] = $id;
  1229. }
  1230. }
  1231. $navlink4 = null;
  1232. // Remove file extensions from filenames
  1233. foreach ($path_elements as $key => $filename) {
  1234. $path_elements[$key] = str_replace('.php', '', $filename);
  1235. }
  1236. // Second le…

Large files files are truncated, but you can click here to view the full file