PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/grade/lib.php

https://bitbucket.org/bmbrands/swets
PHP | 2509 lines | 1705 code | 257 blank | 547 comment | 314 complexity | 7ffc0bbce945897a71ac25bcbb57bef3 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, GPL-2.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception

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

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