PageRenderTime 113ms CodeModel.GetById 35ms RepoModel.GetById 11ms app.codeStats 0ms

/grade/lib.php

https://github.com/7HM/moodle
PHP | 2838 lines | 1840 code | 324 blank | 674 comment | 388 complexity | 71c7e9d3cd3f81a1c1b52af5fb886e64 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1, GPL-3.0

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. /**
  25. * This class iterates over all users that are graded in a course.
  26. * Returns detailed info about users and their grades.
  27. *
  28. * @author Petr Skoda <skodak@moodle.org>
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class graded_users_iterator {
  32. /**
  33. * The couse whose users we are interested in
  34. */
  35. protected $course;
  36. /**
  37. * An array of grade items or null if only user data was requested
  38. */
  39. protected $grade_items;
  40. /**
  41. * The group ID we are interested in. 0 means all groups.
  42. */
  43. protected $groupid;
  44. /**
  45. * A recordset of graded users
  46. */
  47. protected $users_rs;
  48. /**
  49. * A recordset of user grades (grade_grade instances)
  50. */
  51. protected $grades_rs;
  52. /**
  53. * Array used when moving to next user while iterating through the grades recordset
  54. */
  55. protected $gradestack;
  56. /**
  57. * The first field of the users table by which the array of users will be sorted
  58. */
  59. protected $sortfield1;
  60. /**
  61. * Should sortfield1 be ASC or DESC
  62. */
  63. protected $sortorder1;
  64. /**
  65. * The second field of the users table by which the array of users will be sorted
  66. */
  67. protected $sortfield2;
  68. /**
  69. * Should sortfield2 be ASC or DESC
  70. */
  71. protected $sortorder2;
  72. /**
  73. * Should users whose enrolment has been suspended be ignored?
  74. */
  75. protected $onlyactive = false;
  76. /**
  77. * Enable user custom fields
  78. */
  79. protected $allowusercustomfields = false;
  80. /**
  81. * List of suspended users in course. This includes users whose enrolment status is suspended
  82. * or enrolment has expired or not started.
  83. */
  84. protected $suspendedusers = array();
  85. /**
  86. * Constructor
  87. *
  88. * @param object $course A course object
  89. * @param array $grade_items array of grade items, if not specified only user info returned
  90. * @param int $groupid iterate only group users if present
  91. * @param string $sortfield1 The first field of the users table by which the array of users will be sorted
  92. * @param string $sortorder1 The order in which the first sorting field will be sorted (ASC or DESC)
  93. * @param string $sortfield2 The second field of the users table by which the array of users will be sorted
  94. * @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC)
  95. */
  96. public function __construct($course, $grade_items=null, $groupid=0,
  97. $sortfield1='lastname', $sortorder1='ASC',
  98. $sortfield2='firstname', $sortorder2='ASC') {
  99. $this->course = $course;
  100. $this->grade_items = $grade_items;
  101. $this->groupid = $groupid;
  102. $this->sortfield1 = $sortfield1;
  103. $this->sortorder1 = $sortorder1;
  104. $this->sortfield2 = $sortfield2;
  105. $this->sortorder2 = $sortorder2;
  106. $this->gradestack = array();
  107. }
  108. /**
  109. * Initialise the iterator
  110. *
  111. * @return boolean success
  112. */
  113. public function init() {
  114. global $CFG, $DB;
  115. $this->close();
  116. grade_regrade_final_grades($this->course->id);
  117. $course_item = grade_item::fetch_course_item($this->course->id);
  118. if ($course_item->needsupdate) {
  119. // can not calculate all final grades - sorry
  120. return false;
  121. }
  122. $coursecontext = context_course::instance($this->course->id);
  123. $relatedcontexts = get_related_contexts_string($coursecontext);
  124. list($gradebookroles_sql, $params) =
  125. $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);
  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->shortname}'";
  167. $params['cf'.$customfieldscount] = $field->customid;
  168. $customfieldscount++;
  169. }
  170. }
  171. }
  172. // $params contents: gradebookroles and groupid (for $groupwheresql)
  173. $users_sql = "SELECT $userfields $ofields
  174. FROM {user} u
  175. JOIN ($enrolledsql) je ON je.id = u.id
  176. $groupsql $customfieldssql
  177. JOIN (
  178. SELECT DISTINCT ra.userid
  179. FROM {role_assignments} ra
  180. WHERE ra.roleid $gradebookroles_sql
  181. AND ra.contextid $relatedcontexts
  182. ) rainner ON rainner.userid = u.id
  183. WHERE u.deleted = 0
  184. $groupwheresql
  185. ORDER BY $order";
  186. $this->users_rs = $DB->get_recordset_sql($users_sql, $params);
  187. if (!$this->onlyactive) {
  188. $context = context_course::instance($this->course->id);
  189. $this->suspendedusers = get_suspended_userids($context);
  190. } else {
  191. $this->suspendedusers = array();
  192. }
  193. if (!empty($this->grade_items)) {
  194. $itemids = array_keys($this->grade_items);
  195. list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items');
  196. $params = array_merge($params, $grades_params);
  197. // $params contents: gradebookroles, enrolledparams, groupid (for $groupwheresql) and itemids
  198. $grades_sql = "SELECT g.* $ofields
  199. FROM {grade_grades} g
  200. JOIN {user} u ON g.userid = u.id
  201. JOIN ($enrolledsql) je ON je.id = u.id
  202. $groupsql
  203. JOIN (
  204. SELECT DISTINCT ra.userid
  205. FROM {role_assignments} ra
  206. WHERE ra.roleid $gradebookroles_sql
  207. AND ra.contextid $relatedcontexts
  208. ) rainner ON rainner.userid = u.id
  209. WHERE u.deleted = 0
  210. AND g.itemid $itemidsql
  211. $groupwheresql
  212. ORDER BY $order, g.itemid ASC";
  213. $this->grades_rs = $DB->get_recordset_sql($grades_sql, $params);
  214. } else {
  215. $this->grades_rs = false;
  216. }
  217. return true;
  218. }
  219. /**
  220. * Returns information about the next user
  221. * @return mixed array of user info, all grades and feedback or null when no more users found
  222. */
  223. public function next_user() {
  224. if (!$this->users_rs) {
  225. return false; // no users present
  226. }
  227. if (!$this->users_rs->valid()) {
  228. if ($current = $this->_pop()) {
  229. // this is not good - user or grades updated between the two reads above :-(
  230. }
  231. return false; // no more users
  232. } else {
  233. $user = $this->users_rs->current();
  234. $this->users_rs->next();
  235. }
  236. // find grades of this user
  237. $grade_records = array();
  238. while (true) {
  239. if (!$current = $this->_pop()) {
  240. break; // no more grades
  241. }
  242. if (empty($current->userid)) {
  243. break;
  244. }
  245. if ($current->userid != $user->id) {
  246. // grade of the next user, we have all for this user
  247. $this->_push($current);
  248. break;
  249. }
  250. $grade_records[$current->itemid] = $current;
  251. }
  252. $grades = array();
  253. $feedbacks = array();
  254. if (!empty($this->grade_items)) {
  255. foreach ($this->grade_items as $grade_item) {
  256. if (!isset($feedbacks[$grade_item->id])) {
  257. $feedbacks[$grade_item->id] = new stdClass();
  258. }
  259. if (array_key_exists($grade_item->id, $grade_records)) {
  260. $feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback;
  261. $feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat;
  262. unset($grade_records[$grade_item->id]->feedback);
  263. unset($grade_records[$grade_item->id]->feedbackformat);
  264. $grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false);
  265. } else {
  266. $feedbacks[$grade_item->id]->feedback = '';
  267. $feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE;
  268. $grades[$grade_item->id] =
  269. new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false);
  270. }
  271. }
  272. }
  273. // Set user suspended status.
  274. $user->suspendedenrolment = isset($this->suspendedusers[$user->id]);
  275. $result = new stdClass();
  276. $result->user = $user;
  277. $result->grades = $grades;
  278. $result->feedbacks = $feedbacks;
  279. return $result;
  280. }
  281. /**
  282. * Close the iterator, do not forget to call this function
  283. */
  284. public function close() {
  285. if ($this->users_rs) {
  286. $this->users_rs->close();
  287. $this->users_rs = null;
  288. }
  289. if ($this->grades_rs) {
  290. $this->grades_rs->close();
  291. $this->grades_rs = null;
  292. }
  293. $this->gradestack = array();
  294. }
  295. /**
  296. * Should all enrolled users be exported or just those with an active enrolment?
  297. *
  298. * @param bool $onlyactive True to limit the export to users with an active enrolment
  299. */
  300. public function require_active_enrolment($onlyactive = true) {
  301. if (!empty($this->users_rs)) {
  302. debugging('Calling require_active_enrolment() has no effect unless you call init() again', DEBUG_DEVELOPER);
  303. }
  304. $this->onlyactive = $onlyactive;
  305. }
  306. /**
  307. * Allow custom fields to be included
  308. *
  309. * @param bool $allow Whether to allow custom fields or not
  310. * @return void
  311. */
  312. public function allow_user_custom_fields($allow = true) {
  313. if ($allow) {
  314. $this->allowusercustomfields = true;
  315. } else {
  316. $this->allowusercustomfields = false;
  317. }
  318. }
  319. /**
  320. * Add a grade_grade instance to the grade stack
  321. *
  322. * @param grade_grade $grade Grade object
  323. *
  324. * @return void
  325. */
  326. private function _push($grade) {
  327. array_push($this->gradestack, $grade);
  328. }
  329. /**
  330. * Remove a grade_grade instance from the grade stack
  331. *
  332. * @return grade_grade current grade object
  333. */
  334. private function _pop() {
  335. global $DB;
  336. if (empty($this->gradestack)) {
  337. if (empty($this->grades_rs) || !$this->grades_rs->valid()) {
  338. return null; // no grades present
  339. }
  340. $current = $this->grades_rs->current();
  341. $this->grades_rs->next();
  342. return $current;
  343. } else {
  344. return array_pop($this->gradestack);
  345. }
  346. }
  347. }
  348. /**
  349. * Print a selection popup form of the graded users in a course.
  350. *
  351. * @deprecated since 2.0
  352. *
  353. * @param int $course id of the course
  354. * @param string $actionpage The page receiving the data from the popoup form
  355. * @param int $userid id of the currently selected user (or 'all' if they are all selected)
  356. * @param int $groupid id of requested group, 0 means all
  357. * @param int $includeall bool include all option
  358. * @param bool $return If true, will return the HTML, otherwise, will print directly
  359. * @return null
  360. */
  361. function print_graded_users_selector($course, $actionpage, $userid=0, $groupid=0, $includeall=true, $return=false) {
  362. global $CFG, $USER, $OUTPUT;
  363. return $OUTPUT->render(grade_get_graded_users_select(substr($actionpage, 0, strpos($actionpage, '/')), $course, $userid, $groupid, $includeall));
  364. }
  365. function grade_get_graded_users_select($report, $course, $userid, $groupid, $includeall) {
  366. global $USER;
  367. if (is_null($userid)) {
  368. $userid = $USER->id;
  369. }
  370. $coursecontext = context_course::instance($course->id);
  371. $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
  372. $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
  373. $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
  374. $menu = array(); // Will be a list of userid => user name
  375. $menususpendedusers = array(); // Suspended users go to a separate optgroup.
  376. $gui = new graded_users_iterator($course, null, $groupid);
  377. $gui->require_active_enrolment($showonlyactiveenrol);
  378. $gui->init();
  379. $label = get_string('selectauser', 'grades');
  380. if ($includeall) {
  381. $menu[0] = get_string('allusers', 'grades');
  382. $label = get_string('selectalloroneuser', 'grades');
  383. }
  384. while ($userdata = $gui->next_user()) {
  385. $user = $userdata->user;
  386. $userfullname = fullname($user);
  387. if ($user->suspendedenrolment) {
  388. $menususpendedusers[$user->id] = $userfullname;
  389. } else {
  390. $menu[$user->id] = $userfullname;
  391. }
  392. }
  393. $gui->close();
  394. if ($includeall) {
  395. $menu[0] .= " (" . (count($menu) + count($menususpendedusers) - 1) . ")";
  396. }
  397. if (!empty($menususpendedusers)) {
  398. $menu[] = array(get_string('suspendedusers') => $menususpendedusers);
  399. }
  400. $select = new single_select(new moodle_url('/grade/report/'.$report.'/index.php', array('id'=>$course->id)), 'userid', $menu, $userid);
  401. $select->label = $label;
  402. $select->formid = 'choosegradeuser';
  403. return $select;
  404. }
  405. /**
  406. * Print grading plugin selection popup form.
  407. *
  408. * @param array $plugin_info An array of plugins containing information for the selector
  409. * @param boolean $return return as string
  410. *
  411. * @return nothing or string if $return true
  412. */
  413. function print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return=false) {
  414. global $CFG, $OUTPUT, $PAGE;
  415. $menu = array();
  416. $count = 0;
  417. $active = '';
  418. foreach ($plugin_info as $plugin_type => $plugins) {
  419. if ($plugin_type == 'strings') {
  420. continue;
  421. }
  422. $first_plugin = reset($plugins);
  423. $sectionname = $plugin_info['strings'][$plugin_type];
  424. $section = array();
  425. foreach ($plugins as $plugin) {
  426. $link = $plugin->link->out(false);
  427. $section[$link] = $plugin->string;
  428. $count++;
  429. if ($plugin_type === $active_type and $plugin->id === $active_plugin) {
  430. $active = $link;
  431. }
  432. }
  433. if ($section) {
  434. $menu[] = array($sectionname=>$section);
  435. }
  436. }
  437. // finally print/return the popup form
  438. if ($count > 1) {
  439. $select = new url_select($menu, $active, null, 'choosepluginreport');
  440. $select->set_label(get_string('gradereport', 'grades'), array('class' => 'accesshide'));
  441. if ($return) {
  442. return $OUTPUT->render($select);
  443. } else {
  444. echo $OUTPUT->render($select);
  445. }
  446. } else {
  447. // only one option - no plugin selector needed
  448. return '';
  449. }
  450. }
  451. /**
  452. * Print grading plugin selection tab-based navigation.
  453. *
  454. * @param string $active_type type of plugin on current page - import, export, report or edit
  455. * @param string $active_plugin active plugin type - grader, user, cvs, ...
  456. * @param array $plugin_info Array of plugins
  457. * @param boolean $return return as string
  458. *
  459. * @return nothing or string if $return true
  460. */
  461. function grade_print_tabs($active_type, $active_plugin, $plugin_info, $return=false) {
  462. global $CFG, $COURSE;
  463. if (!isset($currenttab)) { //TODO: this is weird
  464. $currenttab = '';
  465. }
  466. $tabs = array();
  467. $top_row = array();
  468. $bottom_row = array();
  469. $inactive = array($active_plugin);
  470. $activated = array();
  471. $count = 0;
  472. $active = '';
  473. foreach ($plugin_info as $plugin_type => $plugins) {
  474. if ($plugin_type == 'strings') {
  475. continue;
  476. }
  477. // If $plugins is actually the definition of a child-less parent link:
  478. if (!empty($plugins->id)) {
  479. $string = $plugins->string;
  480. if (!empty($plugin_info[$active_type]->parent)) {
  481. $string = $plugin_info[$active_type]->parent->string;
  482. }
  483. $top_row[] = new tabobject($plugin_type, $plugins->link, $string);
  484. continue;
  485. }
  486. $first_plugin = reset($plugins);
  487. $url = $first_plugin->link;
  488. if ($plugin_type == 'report') {
  489. $url = $CFG->wwwroot.'/grade/report/index.php?id='.$COURSE->id;
  490. }
  491. $top_row[] = new tabobject($plugin_type, $url, $plugin_info['strings'][$plugin_type]);
  492. if ($active_type == $plugin_type) {
  493. foreach ($plugins as $plugin) {
  494. $bottom_row[] = new tabobject($plugin->id, $plugin->link, $plugin->string);
  495. if ($plugin->id == $active_plugin) {
  496. $inactive = array($plugin->id);
  497. }
  498. }
  499. }
  500. }
  501. $tabs[] = $top_row;
  502. $tabs[] = $bottom_row;
  503. if ($return) {
  504. return print_tabs($tabs, $active_type, $inactive, $activated, true);
  505. } else {
  506. print_tabs($tabs, $active_type, $inactive, $activated);
  507. }
  508. }
  509. /**
  510. * grade_get_plugin_info
  511. *
  512. * @param int $courseid The course id
  513. * @param string $active_type type of plugin on current page - import, export, report or edit
  514. * @param string $active_plugin active plugin type - grader, user, cvs, ...
  515. *
  516. * @return array
  517. */
  518. function grade_get_plugin_info($courseid, $active_type, $active_plugin) {
  519. global $CFG, $SITE;
  520. $context = context_course::instance($courseid);
  521. $plugin_info = array();
  522. $count = 0;
  523. $active = '';
  524. $url_prefix = $CFG->wwwroot . '/grade/';
  525. // Language strings
  526. $plugin_info['strings'] = grade_helper::get_plugin_strings();
  527. if ($reports = grade_helper::get_plugins_reports($courseid)) {
  528. $plugin_info['report'] = $reports;
  529. }
  530. //showing grade categories and items make no sense if we're not within a course
  531. if ($courseid!=$SITE->id) {
  532. if ($edittree = grade_helper::get_info_edit_structure($courseid)) {
  533. $plugin_info['edittree'] = $edittree;
  534. }
  535. }
  536. if ($scale = grade_helper::get_info_scales($courseid)) {
  537. $plugin_info['scale'] = array('view'=>$scale);
  538. }
  539. if ($outcomes = grade_helper::get_info_outcomes($courseid)) {
  540. $plugin_info['outcome'] = $outcomes;
  541. }
  542. if ($letters = grade_helper::get_info_letters($courseid)) {
  543. $plugin_info['letter'] = $letters;
  544. }
  545. if ($imports = grade_helper::get_plugins_import($courseid)) {
  546. $plugin_info['import'] = $imports;
  547. }
  548. if ($exports = grade_helper::get_plugins_export($courseid)) {
  549. $plugin_info['export'] = $exports;
  550. }
  551. foreach ($plugin_info as $plugin_type => $plugins) {
  552. if (!empty($plugins->id) && $active_plugin == $plugins->id) {
  553. $plugin_info['strings']['active_plugin_str'] = $plugins->string;
  554. break;
  555. }
  556. foreach ($plugins as $plugin) {
  557. if (is_a($plugin, 'grade_plugin_info')) {
  558. if ($active_plugin == $plugin->id) {
  559. $plugin_info['strings']['active_plugin_str'] = $plugin->string;
  560. }
  561. }
  562. }
  563. }
  564. //hide course settings if we're not in a course
  565. if ($courseid!=$SITE->id) {
  566. if ($setting = grade_helper::get_info_manage_settings($courseid)) {
  567. $plugin_info['settings'] = array('course'=>$setting);
  568. }
  569. }
  570. // Put preferences last
  571. if ($preferences = grade_helper::get_plugins_report_preferences($courseid)) {
  572. $plugin_info['preferences'] = $preferences;
  573. }
  574. foreach ($plugin_info as $plugin_type => $plugins) {
  575. if (!empty($plugins->id) && $active_plugin == $plugins->id) {
  576. $plugin_info['strings']['active_plugin_str'] = $plugins->string;
  577. break;
  578. }
  579. foreach ($plugins as $plugin) {
  580. if (is_a($plugin, 'grade_plugin_info')) {
  581. if ($active_plugin == $plugin->id) {
  582. $plugin_info['strings']['active_plugin_str'] = $plugin->string;
  583. }
  584. }
  585. }
  586. }
  587. return $plugin_info;
  588. }
  589. /**
  590. * A simple class containing info about grade plugins.
  591. * Can be subclassed for special rules
  592. *
  593. * @package core_grades
  594. * @copyright 2009 Nicolas Connault
  595. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  596. */
  597. class grade_plugin_info {
  598. /**
  599. * A unique id for this plugin
  600. *
  601. * @var mixed
  602. */
  603. public $id;
  604. /**
  605. * A URL to access this plugin
  606. *
  607. * @var mixed
  608. */
  609. public $link;
  610. /**
  611. * The name of this plugin
  612. *
  613. * @var mixed
  614. */
  615. public $string;
  616. /**
  617. * Another grade_plugin_info object, parent of the current one
  618. *
  619. * @var mixed
  620. */
  621. public $parent;
  622. /**
  623. * Constructor
  624. *
  625. * @param int $id A unique id for this plugin
  626. * @param string $link A URL to access this plugin
  627. * @param string $string The name of this plugin
  628. * @param object $parent Another grade_plugin_info object, parent of the current one
  629. *
  630. * @return void
  631. */
  632. public function __construct($id, $link, $string, $parent=null) {
  633. $this->id = $id;
  634. $this->link = $link;
  635. $this->string = $string;
  636. $this->parent = $parent;
  637. }
  638. }
  639. /**
  640. * Prints the page headers, breadcrumb trail, page heading, (optional) dropdown navigation menu and
  641. * (optional) navigation tabs for any gradebook page. All gradebook pages MUST use these functions
  642. * in favour of the usual print_header(), print_header_simple(), print_heading() etc.
  643. * !IMPORTANT! Use of tabs.php file in gradebook pages is forbidden unless tabs are switched off at
  644. * the site level for the gradebook ($CFG->grade_navmethod = GRADE_NAVMETHOD_DROPDOWN).
  645. *
  646. * @param int $courseid Course id
  647. * @param string $active_type The type of the current page (report, settings,
  648. * import, export, scales, outcomes, letters)
  649. * @param string $active_plugin The plugin of the current page (grader, fullview etc...)
  650. * @param string $heading The heading of the page. Tries to guess if none is given
  651. * @param boolean $return Whether to return (true) or echo (false) the HTML generated by this function
  652. * @param string $bodytags Additional attributes that will be added to the <body> tag
  653. * @param string $buttons Additional buttons to display on the page
  654. * @param boolean $shownavigation should the gradebook navigation drop down (or tabs) be shown?
  655. *
  656. * @return string HTML code or nothing if $return == false
  657. */
  658. function print_grade_page_head($courseid, $active_type, $active_plugin=null,
  659. $heading = false, $return=false,
  660. $buttons=false, $shownavigation=true) {
  661. global $CFG, $OUTPUT, $PAGE;
  662. $plugin_info = grade_get_plugin_info($courseid, $active_type, $active_plugin);
  663. // Determine the string of the active plugin
  664. $stractive_plugin = ($active_plugin) ? $plugin_info['strings']['active_plugin_str'] : $heading;
  665. $stractive_type = $plugin_info['strings'][$active_type];
  666. if (empty($plugin_info[$active_type]->id) || !empty($plugin_info[$active_type]->parent)) {
  667. $title = $PAGE->course->fullname.': ' . $stractive_type . ': ' . $stractive_plugin;
  668. } else {
  669. $title = $PAGE->course->fullname.': ' . $stractive_plugin;
  670. }
  671. if ($active_type == 'report') {
  672. $PAGE->set_pagelayout('report');
  673. } else {
  674. $PAGE->set_pagelayout('admin');
  675. }
  676. $PAGE->set_title(get_string('grades') . ': ' . $stractive_type);
  677. $PAGE->set_heading($title);
  678. if ($buttons instanceof single_button) {
  679. $buttons = $OUTPUT->render($buttons);
  680. }
  681. $PAGE->set_button($buttons);
  682. grade_extend_settings($plugin_info, $courseid);
  683. $returnval = $OUTPUT->header();
  684. if (!$return) {
  685. echo $returnval;
  686. }
  687. // Guess heading if not given explicitly
  688. if (!$heading) {
  689. $heading = $stractive_plugin;
  690. }
  691. if ($shownavigation) {
  692. if ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_DROPDOWN) {
  693. $returnval .= print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return);
  694. }
  695. if ($return) {
  696. $returnval .= $OUTPUT->heading($heading);
  697. } else {
  698. echo $OUTPUT->heading($heading);
  699. }
  700. if ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_TABS) {
  701. $returnval .= grade_print_tabs($active_type, $active_plugin, $plugin_info, $return);
  702. }
  703. }
  704. if ($return) {
  705. return $returnval;
  706. }
  707. }
  708. /**
  709. * Utility class used for return tracking when using edit and other forms in grade plugins
  710. *
  711. * @package core_grades
  712. * @copyright 2009 Nicolas Connault
  713. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  714. */
  715. class grade_plugin_return {
  716. public $type;
  717. public $plugin;
  718. public $courseid;
  719. public $userid;
  720. public $page;
  721. /**
  722. * Constructor
  723. *
  724. * @param array $params - associative array with return parameters, if null parameter are taken from _GET or _POST
  725. */
  726. public function grade_plugin_return($params = null) {
  727. if (empty($params)) {
  728. $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR);
  729. $this->plugin = optional_param('gpr_plugin', null, PARAM_PLUGIN);
  730. $this->courseid = optional_param('gpr_courseid', null, PARAM_INT);
  731. $this->userid = optional_param('gpr_userid', null, PARAM_INT);
  732. $this->page = optional_param('gpr_page', null, PARAM_INT);
  733. } else {
  734. foreach ($params as $key=>$value) {
  735. if (property_exists($this, $key)) {
  736. $this->$key = $value;
  737. }
  738. }
  739. }
  740. }
  741. /**
  742. * Returns return parameters as options array suitable for buttons.
  743. * @return array options
  744. */
  745. public function get_options() {
  746. if (empty($this->type)) {
  747. return array();
  748. }
  749. $params = array();
  750. if (!empty($this->plugin)) {
  751. $params['plugin'] = $this->plugin;
  752. }
  753. if (!empty($this->courseid)) {
  754. $params['id'] = $this->courseid;
  755. }
  756. if (!empty($this->userid)) {
  757. $params['userid'] = $this->userid;
  758. }
  759. if (!empty($this->page)) {
  760. $params['page'] = $this->page;
  761. }
  762. return $params;
  763. }
  764. /**
  765. * Returns return url
  766. *
  767. * @param string $default default url when params not set
  768. * @param array $extras Extra URL parameters
  769. *
  770. * @return string url
  771. */
  772. public function get_return_url($default, $extras=null) {
  773. global $CFG;
  774. if (empty($this->type) or empty($this->plugin)) {
  775. return $default;
  776. }
  777. $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php';
  778. $glue = '?';
  779. if (!empty($this->courseid)) {
  780. $url .= $glue.'id='.$this->courseid;
  781. $glue = '&amp;';
  782. }
  783. if (!empty($this->userid)) {
  784. $url .= $glue.'userid='.$this->userid;
  785. $glue = '&amp;';
  786. }
  787. if (!empty($this->page)) {
  788. $url .= $glue.'page='.$this->page;
  789. $glue = '&amp;';
  790. }
  791. if (!empty($extras)) {
  792. foreach ($extras as $key=>$value) {
  793. $url .= $glue.$key.'='.$value;
  794. $glue = '&amp;';
  795. }
  796. }
  797. return $url;
  798. }
  799. /**
  800. * Returns string with hidden return tracking form elements.
  801. * @return string
  802. */
  803. public function get_form_fields() {
  804. if (empty($this->type)) {
  805. return '';
  806. }
  807. $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />';
  808. if (!empty($this->plugin)) {
  809. $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />';
  810. }
  811. if (!empty($this->courseid)) {
  812. $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />';
  813. }
  814. if (!empty($this->userid)) {
  815. $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />';
  816. }
  817. if (!empty($this->page)) {
  818. $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />';
  819. }
  820. }
  821. /**
  822. * Add hidden elements into mform
  823. *
  824. * @param object &$mform moodle form object
  825. *
  826. * @return void
  827. */
  828. public function add_mform_elements(&$mform) {
  829. if (empty($this->type)) {
  830. return;
  831. }
  832. $mform->addElement('hidden', 'gpr_type', $this->type);
  833. $mform->setType('gpr_type', PARAM_SAFEDIR);
  834. if (!empty($this->plugin)) {
  835. $mform->addElement('hidden', 'gpr_plugin', $this->plugin);
  836. $mform->setType('gpr_plugin', PARAM_PLUGIN);
  837. }
  838. if (!empty($this->courseid)) {
  839. $mform->addElement('hidden', 'gpr_courseid', $this->courseid);
  840. $mform->setType('gpr_courseid', PARAM_INT);
  841. }
  842. if (!empty($this->userid)) {
  843. $mform->addElement('hidden', 'gpr_userid', $this->userid);
  844. $mform->setType('gpr_userid', PARAM_INT);
  845. }
  846. if (!empty($this->page)) {
  847. $mform->addElement('hidden', 'gpr_page', $this->page);
  848. $mform->setType('gpr_page', PARAM_INT);
  849. }
  850. }
  851. /**
  852. * Add return tracking params into url
  853. *
  854. * @param moodle_url $url A URL
  855. *
  856. * @return string $url with return tracking params
  857. */
  858. public function add_url_params(moodle_url $url) {
  859. if (empty($this->type)) {
  860. return $url;
  861. }
  862. $url->param('gpr_type', $this->type);
  863. if (!empty($this->plugin)) {
  864. $url->param('gpr_plugin', $this->plugin);
  865. }
  866. if (!empty($this->courseid)) {
  867. $url->param('gpr_courseid' ,$this->courseid);
  868. }
  869. if (!empty($this->userid)) {
  870. $url->param('gpr_userid', $this->userid);
  871. }
  872. if (!empty($this->page)) {
  873. $url->param('gpr_page', $this->page);
  874. }
  875. return $url;
  876. }
  877. }
  878. /**
  879. * Function central to gradebook for building and printing the navigation (breadcrumb trail).
  880. *
  881. * @param string $path The path of the calling script (using __FILE__?)
  882. * @param string $pagename The language string to use as the last part of the navigation (non-link)
  883. * @param mixed $id Either a plain integer (assuming the key is 'id') or
  884. * an array of keys and values (e.g courseid => $courseid, itemid...)
  885. *
  886. * @return string
  887. */
  888. function grade_build_nav($path, $pagename=null, $id=null) {
  889. global $CFG, $COURSE, $PAGE;
  890. $strgrades = get_string('grades', 'grades');
  891. // Parse the path and build navlinks from its elements
  892. $dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash
  893. $path = substr($path, $dirroot_length);
  894. $path = str_replace('\\', '/', $path);
  895. $path_elements = explode('/', $path);
  896. $path_elements_count = count($path_elements);
  897. // First link is always 'grade'
  898. $PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$COURSE->id)));
  899. $link = null;
  900. $numberofelements = 3;
  901. // Prepare URL params string
  902. $linkparams = array();
  903. if (!is_null($id)) {
  904. if (is_array($id)) {
  905. foreach ($id as $idkey => $idvalue) {
  906. $linkparams[$idkey] = $idvalue;
  907. }
  908. } else {
  909. $linkparams['id'] = $id;
  910. }
  911. }
  912. $navlink4 = null;
  913. // Remove file extensions from filenames
  914. foreach ($path_elements as $key => $filename) {
  915. $path_elements[$key] = str_replace('.php', '', $filename);
  916. }
  917. // Second level links
  918. switch ($path_elements[1]) {
  919. case 'edit': // No link
  920. if ($path_elements[3] != 'index.php') {
  921. $numberofelements = 4;
  922. }
  923. break;
  924. case 'import': // No link
  925. break;
  926. case 'export': // No link
  927. break;
  928. case 'report':
  929. // $id is required for this link. Do not print it if $id isn't given
  930. if (!is_null($id)) {
  931. $link = new moodle_url('/grade/report/index.php', $linkparams);
  932. }
  933. if ($path_elements[2] == 'grader') {
  934. $numberofelements = 4;
  935. }
  936. break;
  937. default:
  938. // If this element isn't among the ones already listed above, it isn't supported, throw an error.
  939. debugging("grade_build_nav() doesn't support ". $path_elements[1] .
  940. " as the second path element after 'grade'.");
  941. return false;
  942. }
  943. $PAGE->navbar->add(get_string($path_elements[1], 'grades'), $link);
  944. // Third level links
  945. if (empty($pagename)) {
  946. $pagename = get_string($path_elements[2], 'grades');
  947. }
  948. switch ($numberofelements) {
  949. case 3:
  950. $PAGE->navbar->add($pagename, $link);
  951. break;
  952. case 4:
  953. if ($path_elements[2] == 'grader' AND $path_elements[3] != 'index.php') {
  954. $PAGE->navbar->add(get_string('pluginname', 'gradereport_grader'), new moodle_url('/grade/report/grader/index.php', $linkparams));
  955. }
  956. $PAGE->navbar->add($pagename);
  957. break;
  958. }
  959. return '';
  960. }
  961. /**
  962. * General structure representing grade items in course
  963. *
  964. * @package core_grades
  965. * @copyright 2009 Nicolas Connault
  966. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  967. */
  968. class grade_structure {
  969. public $context;
  970. public $courseid;
  971. /**
  972. * Reference to modinfo for current course (for performance, to save
  973. * retrieving it from courseid every time). Not actually set except for
  974. * the grade_tree type.
  975. * @var course_modinfo
  976. */
  977. public $modinfo;
  978. /**
  979. * 1D array of grade items only
  980. */
  981. public $items;
  982. /**
  983. * Returns icon of element
  984. *
  985. * @param array &$element An array representing an element in the grade_tree
  986. * @param bool $spacerifnone return spacer if no icon found
  987. *
  988. * @return string icon or spacer
  989. */
  990. public function get_element_icon(&$element, $spacerifnone=false) {
  991. global $CFG, $OUTPUT;
  992. require_once $CFG->libdir.'/filelib.php';
  993. switch ($element['type']) {
  994. case 'item':
  995. case 'courseitem':
  996. case 'categoryitem':
  997. $is_course = $element['object']->is_course_item();
  998. $is_category = $element['object']->is_category_item();
  999. $is_scale = $element['object']->gradetype == GRADE_TYPE_SCALE;
  1000. $is_value = $element['object']->gradetype == GRADE_TYPE_VALUE;
  1001. $is_outcome = !empty($element['object']->outcomeid);
  1002. if ($element['object']->is_calculated()) {
  1003. $strcalc = get_string('calculatedgrade', 'grades');
  1004. return '<img src="'.$OUTPUT->pix_url('i/calc') . '" class="icon itemicon" title="'.
  1005. s($strcalc).'" alt="'.s($strcalc).'"/>';
  1006. } else if (($is_course or $is_category) and ($is_scale or $is_value)) {
  1007. if ($category = $element['object']->get_item_category()) {
  1008. switch ($category->aggregation) {
  1009. case GRADE_AGGREGATE_MEAN:
  1010. case GRADE_AGGREGATE_MEDIAN:
  1011. case GRADE_AGGREGATE_WEIGHTED_MEAN:
  1012. case GRADE_AGGREGATE_WEIGHTED_MEAN2:
  1013. case GRADE_AGGREGATE_EXTRACREDIT_MEAN:
  1014. $stragg = get_string('aggregation', 'grades');
  1015. return '<img src="'.$OUTPUT->pix_url('i/agg_mean') . '" ' .
  1016. 'class="icon itemicon" title="'.s($stragg).'" alt="'.s($stragg).'"/>';
  1017. case GRADE_AGGREGATE_SUM:
  1018. $stragg = get_string('aggregation', 'grades');
  1019. return '<img src="'.$OUTPUT->pix_url('i/agg_sum') . '" ' .
  1020. 'class="icon itemicon" title="'.s($stragg).'" alt="'.s($stragg).'"/>';
  1021. }
  1022. }
  1023. } else if ($element['object']->itemtype == 'mod') {
  1024. //prevent outcomes being displaying the same icon as the activity they are attached to
  1025. if ($is_outcome) {
  1026. $stroutcome = s(get_string('outcome', 'grades'));
  1027. return '<img src="'.$OUTPUT->pix_url('i/outcomes') . '" ' .
  1028. 'class="icon itemicon" title="'.$stroutcome.
  1029. '" alt="'.$stroutcome.'"/>';
  1030. } else {
  1031. $strmodname = get_string('modulename', $element['object']->itemmodule);
  1032. return '<img src="'.$OUTPUT->pix_url('icon',
  1033. $element['object']->itemmodule) . '" ' .
  1034. 'class="icon itemicon" title="' .s($strmodname).
  1035. '" alt="' .s($strmodname).'"/>';
  1036. }
  1037. } else if ($element['object']->itemtype == 'manual') {
  1038. if ($element['object']->is_outcome_item()) {
  1039. $stroutcome = get_string('outcome', 'grades');
  1040. return '<img src="'.$OUTPUT->pix_url('i/outcomes') . '" ' .
  1041. 'class="icon itemicon" title="'.s($stroutcome).
  1042. '" alt="'.s($stroutcome).'"/>';
  1043. } else {
  1044. $strmanual = get_string('manualitem', 'grades');
  1045. return '<img src="'.$OUTPUT->pix_url('i/manual_item') . '" '.
  1046. 'class="icon itemicon" title="'.s($strmanual).
  1047. '" alt="'.s($strmanual).'"/>';
  1048. }
  1049. }
  1050. break;
  1051. case 'category':
  1052. $strcat = get_string('category', 'grades');
  1053. return '<img src="'.$OUTPUT->pix_url('i/folder') . '" class="icon itemicon" ' .
  1054. 'title="'.s($strcat).'" alt="'.s($strcat).'" />';
  1055. }
  1056. if ($spacerifnone) {
  1057. return $OUTPUT->spacer().' ';
  1058. } else {
  1059. return '';
  1060. }
  1061. }
  1062. /**
  1063. * Returns name of element optionally with icon and link
  1064. *
  1065. * @param array &$element An array representing an element in the grade_tree
  1066. * @param bool $withlink Whether or not this header has a link
  1067. * @param bool $icon Whether or not to display an icon with this header
  1068. * @param bool $spacerifnone return spacer if no icon found
  1069. *
  1070. * @return string header
  1071. */
  1072. public function get_element_header(&$element, $withlink=false, $icon=true, $spacerifnone=false) {
  1073. $header = '';
  1074. if ($icon) {
  1075. $header .= $this->get_element_icon($element, $spacerifnone);
  1076. }
  1077. $header .= $element['object']->get_name();
  1078. if ($element['type'] != 'item' and $element['type'] != 'categoryitem' and
  1079. $element['type'] != 'courseitem') {
  1080. return $header;
  1081. }
  1082. if ($withlink) {
  1083. $url = $this->get_activity_link($element);
  1084. if ($url) {
  1085. $a = new stdClass();
  1086. $a->name = get_string('modulename', $element['object']->itemmodule);
  1087. $title = get_string('linktoactivity', 'grades', $a);
  1088. $header = html_writer::link($url, $header, array('title' => $title));
  1089. }
  1090. }
  1091. return $header;
  1092. }
  1093. private function get_activity_link($element) {
  1094. global $CFG;
  1095. /** @var array static cache of the grade.php file existence flags */
  1096. static $hasgradephp = array();
  1097. $itemtype = $element['object']->itemtype;
  1098. $itemmodule = $element['object']->itemmodule;
  1099. $iteminstance = $element['object']->iteminstance;
  1100. $itemnumber = $element['object']->itemnumber;
  1101. // Links only for module items that have valid instance, module and are
  1102. // called from grade_tree with valid modinfo
  1103. if ($itemtype != 'mod' || !$iteminstance || !$itemmodule || !$this->modinfo) {
  1104. return null;
  1105. }
  1106. // Get $cm efficiently and with visibility information using modinfo
  1107. $instances = $this->modinfo->get_instances();
  1108. if (empty($instances[$itemmodule][$iteminstance])) {
  1109. return null;
  1110. }
  1111. $cm = $instances[$itemmodule][$iteminstance];
  1112. // Do not add link if activity is not visible to the current user
  1113. if (!$cm->uservisible) {
  1114. return null;
  1115. }
  1116. if (!array_key_exists($itemmodule, $hasgradephp)) {
  1117. if (file_exists($CFG->dirroot . '/mod/' . $itemmodule . '/grade.php')) {
  1118. $hasgradephp[$itemmodule] = true;
  1119. } else {
  1120. $hasgradephp[$itemmodule] = false;
  1121. }
  1122. }
  1123. // If module has grade.php, link to that, otherwise view.php
  1124. if ($hasgradephp[$itemmodule]) {
  1125. $args = array('id' => $cm->id, 'itemnumber' => $itemnumber);
  1126. if (isset($element['userid'])) {
  1127. $args['userid'] = $element['userid'];
  1128. }
  1129. return new moodle_url('/mod/' . $itemmodule . '/grade.php', $args);
  1130. } else {
  1131. return new moodle_url('/mod/' . $itemmodule . '/view.php', array('id' => $cm->id));
  1132. }
  1133. }
  1134. /**
  1135. * Returns URL of a page that is supposed to contain detailed grade analysis
  1136. *
  1137. * At the moment, only activity modules are supported. The method generates link
  1138. * to the module's file grade.php with the parameters id (cmid), itemid, itemnumber,
  1139. * gradeid and userid. If the grade.php does not exist, null is returned.
  1140. *
  1141. * @return moodle_url|null URL or null if unable to construct it
  1142. */
  1143. public function get_grade_analysis_url(grade_grade $grade) {
  1144. global $CFG;
  1145. /** @var array static cache of the grade.php file existence flags */
  1146. static $hasgradephp = array();
  1147. if (empty($grade->grade_item) or !($grade->grade_item instanceof grade_item)) {
  1148. throw new coding_exception('Passed grade without the associated grade item');
  1149. }
  1150. $item = $grade->grade_item;
  1151. if (!$item->is_external_item()) {
  1152. // at the moment, only activity modules are supported
  1153. return null;
  1154. }
  1155. if ($item->itemtype !== 'mod') {
  1156. throw new coding_exception('Unknown external itemtype: '.$item->itemtype);
  1157. }
  1158. if (empty($item->iteminstance) or empty($item->itemmodule) or empty($this->modinfo)) {
  1159. return null;
  1160. }
  1161. if (!array_key_exists($item->itemmodule, $hasgradephp)) {
  1162. if (file_exists($CFG->dirroot . '/mod/' . $item->itemmodule . '/grade.php')) {
  1163. $hasgradephp[$item->itemmodule] = true;
  1164. } else {
  1165. $hasgradephp[$item->itemmodule] = false;
  1166. }
  1167. }
  1168. if (!$hasgradephp[$item->itemmodule]) {
  1169. return null;
  1170. }
  1171. $instances = $this->modinfo->get_instances();
  1172. if (empty($instances[$item->itemmodule][$item->iteminstance])) {
  1173. return null;
  1174. }
  1175. $cm = $instances[$item->itemmodule][$item->iteminstance];
  1176. if (!$cm->uservisible) {
  1177. return null;
  1178. }
  1179. $url = new moodle_url('/mod/'.$item->itemmodule.'/grade.php', array(
  1180. 'id' => $cm->id,
  1181. 'itemid' => $item->id,
  1182. 'itemnumber' => $item->itemnumber,
  1183. 'gradeid' => $grade->id,
  1184. 'userid' => $grade->userid,
  1185. ));
  1186. return $url;
  1187. }
  1188. /**
  1189. * Returns an action icon leading to the grade analysis page
  1190. *
  1191. * @param grade_grade $grade
  1192. * @return string
  1193. */
  1194. public function get_grade_analysis_icon(grade_grade $grade) {
  1195. global $OUTPUT;
  1196. $url = $this->get_grade_analysis_url($grade);
  1197. if (is_null($url)) {
  1198. return '';
  1199. }
  1200. return $OUTPUT->action_icon($url, new pix_icon('t/preview',
  1201. get_string('gradeanalysis', 'core_grades')));
  1202. }
  1203. /**
  1204. * Returns the grade eid - the grade may not exist yet.
  1205. *
  1206. * @param grade_grade $grade_grade A grade_grade object
  1207. *
  1208. * @return string eid
  1209. */
  1210. public function get_grade_eid($grade_grade) {
  1211. if (empty($grade_grade->id)) {
  1212. return 'n'.$grade_grade->itemid.'u'.$grade_grade->userid;
  1213. } else {
  1214. return 'g'.$grade_grade->id;
  1215. }
  1216. }
  1217. /**
  1218. * Returns the grade_item eid
  1219. * @param grade_item $grade_item A grade_item object
  1220. * @return string eid
  1221. */
  1222. public function get_item_eid($grade_item) {
  1223. return 'i'.$grade_item->id;
  1224. }
  1225. /**
  1226. * Given a grade_tree element, returns an array of parameters
  1227. * used to build an icon for that element.
  1228. *
  1229. * @param array $element An array representing an element in the grade_tree
  1230. *
  1231. * @return array
  1232. */
  1233. public function get_params_for_iconstr($element) {
  1234. $strparams = new stdClass();
  1235. $strparams->category = '';
  1236. $strparams->itemname = '';
  1237. $strparams->itemmodule = '';
  1238. if (!method_exists($element['object'], 'get_name')) {
  1239. return $strparams;
  1240. }
  1241. $strparams->itemname = html_to_text($element['object']->get_name());
  1242. // If element name is categorytotal, get the name of the parent category
  1243. if ($strparams->itemname == get_string('categorytotal', 'grades')) {
  1244. $parent = $element['object']->get_parent_category();
  1245. $strparams->category = $parent->get_name() . ' ';
  1246. } else {
  1247. $strparams->category = '';
  1248. }
  1249. $strparams->itemmodule = null;
  1250. if (isset($element['object']->itemmodule)) {
  1251. $strparams->itemmodule = $element['object']->itemmodule;
  1252. }
  1253. return $strparams;
  1254. }
  1255. /**
  1256. * Return edit icon for give element
  1257. *

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