/grade/lib.php
PHP | 3379 lines | 2181 code | 386 blank | 812 comment | 460 complexity | 90cf71998955b91d79c209955e5f4af3 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- <?php
- // This file is part of Moodle - http://moodle.org/
- //
- // Moodle is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // Moodle is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
- /**
- * Functions used by gradebook plugins and reports.
- *
- * @package core_grades
- * @copyright 2009 Petr Skoda and Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- require_once($CFG->libdir . '/gradelib.php');
- require_once($CFG->dirroot . '/grade/export/lib.php');
- /**
- * This class iterates over all users that are graded in a course.
- * Returns detailed info about users and their grades.
- *
- * @author Petr Skoda <skodak@moodle.org>
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- class graded_users_iterator {
- /**
- * The couse whose users we are interested in
- */
- protected $course;
- /**
- * An array of grade items or null if only user data was requested
- */
- protected $grade_items;
- /**
- * The group ID we are interested in. 0 means all groups.
- */
- protected $groupid;
- /**
- * A recordset of graded users
- */
- protected $users_rs;
- /**
- * A recordset of user grades (grade_grade instances)
- */
- protected $grades_rs;
- /**
- * Array used when moving to next user while iterating through the grades recordset
- */
- protected $gradestack;
- /**
- * The first field of the users table by which the array of users will be sorted
- */
- protected $sortfield1;
- /**
- * Should sortfield1 be ASC or DESC
- */
- protected $sortorder1;
- /**
- * The second field of the users table by which the array of users will be sorted
- */
- protected $sortfield2;
- /**
- * Should sortfield2 be ASC or DESC
- */
- protected $sortorder2;
- /**
- * Should users whose enrolment has been suspended be ignored?
- */
- protected $onlyactive = false;
- /**
- * Enable user custom fields
- */
- protected $allowusercustomfields = false;
- /**
- * List of suspended users in course. This includes users whose enrolment status is suspended
- * or enrolment has expired or not started.
- */
- protected $suspendedusers = array();
- /**
- * Constructor
- *
- * @param object $course A course object
- * @param array $grade_items array of grade items, if not specified only user info returned
- * @param int $groupid iterate only group users if present
- * @param string $sortfield1 The first field of the users table by which the array of users will be sorted
- * @param string $sortorder1 The order in which the first sorting field will be sorted (ASC or DESC)
- * @param string $sortfield2 The second field of the users table by which the array of users will be sorted
- * @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC)
- */
- public function __construct($course, $grade_items=null, $groupid=0,
- $sortfield1='lastname', $sortorder1='ASC',
- $sortfield2='firstname', $sortorder2='ASC') {
- $this->course = $course;
- $this->grade_items = $grade_items;
- $this->groupid = $groupid;
- $this->sortfield1 = $sortfield1;
- $this->sortorder1 = $sortorder1;
- $this->sortfield2 = $sortfield2;
- $this->sortorder2 = $sortorder2;
- $this->gradestack = array();
- }
- /**
- * Initialise the iterator
- *
- * @return boolean success
- */
- public function init() {
- global $CFG, $DB;
- $this->close();
- export_verify_grades($this->course->id);
- $course_item = grade_item::fetch_course_item($this->course->id);
- if ($course_item->needsupdate) {
- // Can not calculate all final grades - sorry.
- return false;
- }
- $coursecontext = context_course::instance($this->course->id);
- list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
- list($gradebookroles_sql, $params) = $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr');
- list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, '', 0, $this->onlyactive);
- $params = array_merge($params, $enrolledparams, $relatedctxparams);
- if ($this->groupid) {
- $groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id";
- $groupwheresql = "AND gm.groupid = :groupid";
- // $params contents: gradebookroles
- $params['groupid'] = $this->groupid;
- } else {
- $groupsql = "";
- $groupwheresql = "";
- }
- if (empty($this->sortfield1)) {
- // We must do some sorting even if not specified.
- $ofields = ", u.id AS usrt";
- $order = "usrt ASC";
- } else {
- $ofields = ", u.$this->sortfield1 AS usrt1";
- $order = "usrt1 $this->sortorder1";
- if (!empty($this->sortfield2)) {
- $ofields .= ", u.$this->sortfield2 AS usrt2";
- $order .= ", usrt2 $this->sortorder2";
- }
- if ($this->sortfield1 != 'id' and $this->sortfield2 != 'id') {
- // User order MUST be the same in both queries,
- // must include the only unique user->id if not already present.
- $ofields .= ", u.id AS usrt";
- $order .= ", usrt ASC";
- }
- }
- $userfields = 'u.*';
- $customfieldssql = '';
- if ($this->allowusercustomfields && !empty($CFG->grade_export_customprofilefields)) {
- $customfieldscount = 0;
- $customfieldsarray = grade_helper::get_user_profile_fields($this->course->id, $this->allowusercustomfields);
- foreach ($customfieldsarray as $field) {
- if (!empty($field->customid)) {
- $customfieldssql .= "
- LEFT JOIN (SELECT * FROM {user_info_data}
- WHERE fieldid = :cf$customfieldscount) cf$customfieldscount
- ON u.id = cf$customfieldscount.userid";
- $userfields .= ", cf$customfieldscount.data AS customfield_{$field->customid}";
- $params['cf'.$customfieldscount] = $field->customid;
- $customfieldscount++;
- }
- }
- }
- $users_sql = "SELECT $userfields $ofields
- FROM {user} u
- JOIN ($enrolledsql) je ON je.id = u.id
- $groupsql $customfieldssql
- JOIN (
- SELECT DISTINCT ra.userid
- FROM {role_assignments} ra
- WHERE ra.roleid $gradebookroles_sql
- AND ra.contextid $relatedctxsql
- ) rainner ON rainner.userid = u.id
- WHERE u.deleted = 0
- $groupwheresql
- ORDER BY $order";
- $this->users_rs = $DB->get_recordset_sql($users_sql, $params);
- if (!$this->onlyactive) {
- $context = context_course::instance($this->course->id);
- $this->suspendedusers = get_suspended_userids($context);
- } else {
- $this->suspendedusers = array();
- }
- if (!empty($this->grade_items)) {
- $itemids = array_keys($this->grade_items);
- list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items');
- $params = array_merge($params, $grades_params);
- $grades_sql = "SELECT g.* $ofields
- FROM {grade_grades} g
- JOIN {user} u ON g.userid = u.id
- JOIN ($enrolledsql) je ON je.id = u.id
- $groupsql
- JOIN (
- SELECT DISTINCT ra.userid
- FROM {role_assignments} ra
- WHERE ra.roleid $gradebookroles_sql
- AND ra.contextid $relatedctxsql
- ) rainner ON rainner.userid = u.id
- WHERE u.deleted = 0
- AND g.itemid $itemidsql
- $groupwheresql
- ORDER BY $order, g.itemid ASC";
- $this->grades_rs = $DB->get_recordset_sql($grades_sql, $params);
- } else {
- $this->grades_rs = false;
- }
- return true;
- }
- /**
- * Returns information about the next user
- * @return mixed array of user info, all grades and feedback or null when no more users found
- */
- public function next_user() {
- if (!$this->users_rs) {
- return false; // no users present
- }
- if (!$this->users_rs->valid()) {
- if ($current = $this->_pop()) {
- // this is not good - user or grades updated between the two reads above :-(
- }
- return false; // no more users
- } else {
- $user = $this->users_rs->current();
- $this->users_rs->next();
- }
- // find grades of this user
- $grade_records = array();
- while (true) {
- if (!$current = $this->_pop()) {
- break; // no more grades
- }
- if (empty($current->userid)) {
- break;
- }
- if ($current->userid != $user->id) {
- // grade of the next user, we have all for this user
- $this->_push($current);
- break;
- }
- $grade_records[$current->itemid] = $current;
- }
- $grades = array();
- $feedbacks = array();
- if (!empty($this->grade_items)) {
- foreach ($this->grade_items as $grade_item) {
- if (!isset($feedbacks[$grade_item->id])) {
- $feedbacks[$grade_item->id] = new stdClass();
- }
- if (array_key_exists($grade_item->id, $grade_records)) {
- $feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback;
- $feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat;
- unset($grade_records[$grade_item->id]->feedback);
- unset($grade_records[$grade_item->id]->feedbackformat);
- $grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false);
- } else {
- $feedbacks[$grade_item->id]->feedback = '';
- $feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE;
- $grades[$grade_item->id] =
- new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false);
- }
- $grades[$grade_item->id]->grade_item = $grade_item;
- }
- }
- // Set user suspended status.
- $user->suspendedenrolment = isset($this->suspendedusers[$user->id]);
- $result = new stdClass();
- $result->user = $user;
- $result->grades = $grades;
- $result->feedbacks = $feedbacks;
- return $result;
- }
- /**
- * Close the iterator, do not forget to call this function
- */
- public function close() {
- if ($this->users_rs) {
- $this->users_rs->close();
- $this->users_rs = null;
- }
- if ($this->grades_rs) {
- $this->grades_rs->close();
- $this->grades_rs = null;
- }
- $this->gradestack = array();
- }
- /**
- * Should all enrolled users be exported or just those with an active enrolment?
- *
- * @param bool $onlyactive True to limit the export to users with an active enrolment
- */
- public function require_active_enrolment($onlyactive = true) {
- if (!empty($this->users_rs)) {
- debugging('Calling require_active_enrolment() has no effect unless you call init() again', DEBUG_DEVELOPER);
- }
- $this->onlyactive = $onlyactive;
- }
- /**
- * Allow custom fields to be included
- *
- * @param bool $allow Whether to allow custom fields or not
- * @return void
- */
- public function allow_user_custom_fields($allow = true) {
- if ($allow) {
- $this->allowusercustomfields = true;
- } else {
- $this->allowusercustomfields = false;
- }
- }
- /**
- * Add a grade_grade instance to the grade stack
- *
- * @param grade_grade $grade Grade object
- *
- * @return void
- */
- private function _push($grade) {
- array_push($this->gradestack, $grade);
- }
- /**
- * Remove a grade_grade instance from the grade stack
- *
- * @return grade_grade current grade object
- */
- private function _pop() {
- global $DB;
- if (empty($this->gradestack)) {
- if (empty($this->grades_rs) || !$this->grades_rs->valid()) {
- return null; // no grades present
- }
- $current = $this->grades_rs->current();
- $this->grades_rs->next();
- return $current;
- } else {
- return array_pop($this->gradestack);
- }
- }
- }
- /**
- * Print a selection popup form of the graded users in a course.
- *
- * @deprecated since 2.0
- *
- * @param int $course id of the course
- * @param string $actionpage The page receiving the data from the popoup form
- * @param int $userid id of the currently selected user (or 'all' if they are all selected)
- * @param int $groupid id of requested group, 0 means all
- * @param int $includeall bool include all option
- * @param bool $return If true, will return the HTML, otherwise, will print directly
- * @return null
- */
- function print_graded_users_selector($course, $actionpage, $userid=0, $groupid=0, $includeall=true, $return=false) {
- global $CFG, $USER, $OUTPUT;
- return $OUTPUT->render(grade_get_graded_users_select(substr($actionpage, 0, strpos($actionpage, '/')), $course, $userid, $groupid, $includeall));
- }
- function grade_get_graded_users_select($report, $course, $userid, $groupid, $includeall) {
- global $USER, $CFG;
- if (is_null($userid)) {
- $userid = $USER->id;
- }
- $coursecontext = context_course::instance($course->id);
- $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
- $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
- $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
- $menu = array(); // Will be a list of userid => user name
- $menususpendedusers = array(); // Suspended users go to a separate optgroup.
- $gui = new graded_users_iterator($course, null, $groupid);
- $gui->require_active_enrolment($showonlyactiveenrol);
- $gui->init();
- $label = get_string('selectauser', 'grades');
- if ($includeall) {
- $menu[0] = get_string('allusers', 'grades');
- $label = get_string('selectalloroneuser', 'grades');
- }
- while ($userdata = $gui->next_user()) {
- $user = $userdata->user;
- $userfullname = fullname($user);
- if ($user->suspendedenrolment) {
- $menususpendedusers[$user->id] = $userfullname;
- } else {
- $menu[$user->id] = $userfullname;
- }
- }
- $gui->close();
- if ($includeall) {
- $menu[0] .= " (" . (count($menu) + count($menususpendedusers) - 1) . ")";
- }
- if (!empty($menususpendedusers)) {
- $menu[] = array(get_string('suspendedusers') => $menususpendedusers);
- }
- $gpr = new grade_plugin_return(array('type' => 'report', 'course' => $course, 'groupid' => $groupid));
- $select = new single_select(
- new moodle_url('/grade/report/'.$report.'/index.php', $gpr->get_options()),
- 'userid', $menu, $userid
- );
- $select->label = $label;
- $select->formid = 'choosegradeuser';
- return $select;
- }
- /**
- * Hide warning about changed grades during upgrade to 2.8.
- *
- * @param int $courseid The current course id.
- */
- function hide_natural_aggregation_upgrade_notice($courseid) {
- unset_config('show_sumofgrades_upgrade_' . $courseid);
- }
- /**
- * Hide warning about changed grades during upgrade from 2.8.0-2.8.6 and 2.9.0.
- *
- * @param int $courseid The current course id.
- */
- function grade_hide_min_max_grade_upgrade_notice($courseid) {
- unset_config('show_min_max_grades_changed_' . $courseid);
- }
- /**
- * Use the grade min and max from the grade_grade.
- *
- * This is reserved for core use after an upgrade.
- *
- * @param int $courseid The current course id.
- */
- function grade_upgrade_use_min_max_from_grade_grade($courseid) {
- grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_GRADE);
- grade_force_full_regrading($courseid);
- // Do this now, because it probably happened to late in the page load to be happen automatically.
- grade_regrade_final_grades($courseid);
- }
- /**
- * Use the grade min and max from the grade_item.
- *
- * This is reserved for core use after an upgrade.
- *
- * @param int $courseid The current course id.
- */
- function grade_upgrade_use_min_max_from_grade_item($courseid) {
- grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM);
- grade_force_full_regrading($courseid);
- // Do this now, because it probably happened to late in the page load to be happen automatically.
- grade_regrade_final_grades($courseid);
- }
- /**
- * Hide warning about changed grades during upgrade to 2.8.
- *
- * @param int $courseid The current course id.
- */
- function hide_aggregatesubcats_upgrade_notice($courseid) {
- unset_config('show_aggregatesubcats_upgrade_' . $courseid);
- }
- /**
- * Hide warning about changed grades due to bug fixes
- *
- * @param int $courseid The current course id.
- */
- function hide_gradebook_calculations_freeze_notice($courseid) {
- unset_config('gradebook_calculations_freeze_' . $courseid);
- }
- /**
- * Print warning about changed grades during upgrade to 2.8.
- *
- * @param int $courseid The current course id.
- * @param context $context The course context.
- * @param string $thispage The relative path for the current page. E.g. /grade/report/user/index.php
- * @param boolean $return return as string
- *
- * @return nothing or string if $return true
- */
- function print_natural_aggregation_upgrade_notice($courseid, $context, $thispage, $return=false) {
- global $CFG, $OUTPUT;
- $html = '';
- // Do not do anything if they cannot manage the grades of this course.
- if (!has_capability('moodle/grade:manage', $context)) {
- return $html;
- }
- $hidesubcatswarning = optional_param('seenaggregatesubcatsupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
- $showsubcatswarning = get_config('core', 'show_aggregatesubcats_upgrade_' . $courseid);
- $hidenaturalwarning = optional_param('seensumofgradesupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
- $shownaturalwarning = get_config('core', 'show_sumofgrades_upgrade_' . $courseid);
- $hideminmaxwarning = optional_param('seenminmaxupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
- $showminmaxwarning = get_config('core', 'show_min_max_grades_changed_' . $courseid);
- $useminmaxfromgradeitem = optional_param('useminmaxfromgradeitem', false, PARAM_BOOL) && confirm_sesskey();
- $useminmaxfromgradegrade = optional_param('useminmaxfromgradegrade', false, PARAM_BOOL) && confirm_sesskey();
- $minmaxtouse = grade_get_setting($courseid, 'minmaxtouse', $CFG->grade_minmaxtouse);
- $gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
- $acceptgradebookchanges = optional_param('acceptgradebookchanges', false, PARAM_BOOL) && confirm_sesskey();
- // Hide the warning if the user told it to go away.
- if ($hidenaturalwarning) {
- hide_natural_aggregation_upgrade_notice($courseid);
- }
- // Hide the warning if the user told it to go away.
- if ($hidesubcatswarning) {
- hide_aggregatesubcats_upgrade_notice($courseid);
- }
- // Hide the min/max warning if the user told it to go away.
- if ($hideminmaxwarning) {
- grade_hide_min_max_grade_upgrade_notice($courseid);
- $showminmaxwarning = false;
- }
- if ($useminmaxfromgradegrade) {
- // Revert to the new behaviour, we now use the grade_grade for min/max.
- grade_upgrade_use_min_max_from_grade_grade($courseid);
- grade_hide_min_max_grade_upgrade_notice($courseid);
- $showminmaxwarning = false;
- } else if ($useminmaxfromgradeitem) {
- // Apply the new logic, we now use the grade_item for min/max.
- grade_upgrade_use_min_max_from_grade_item($courseid);
- grade_hide_min_max_grade_upgrade_notice($courseid);
- $showminmaxwarning = false;
- }
- if (!$hidenaturalwarning && $shownaturalwarning) {
- $message = get_string('sumofgradesupgradedgrades', 'grades');
- $hidemessage = get_string('upgradedgradeshidemessage', 'grades');
- $urlparams = array( 'id' => $courseid,
- 'seensumofgradesupgradedgrades' => true,
- 'sesskey' => sesskey());
- $goawayurl = new moodle_url($thispage, $urlparams);
- $goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
- $html .= $OUTPUT->notification($message, 'notifysuccess');
- $html .= $goawaybutton;
- }
- if (!$hidesubcatswarning && $showsubcatswarning) {
- $message = get_string('aggregatesubcatsupgradedgrades', 'grades');
- $hidemessage = get_string('upgradedgradeshidemessage', 'grades');
- $urlparams = array( 'id' => $courseid,
- 'seenaggregatesubcatsupgradedgrades' => true,
- 'sesskey' => sesskey());
- $goawayurl = new moodle_url($thispage, $urlparams);
- $goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
- $html .= $OUTPUT->notification($message, 'notifysuccess');
- $html .= $goawaybutton;
- }
- if ($showminmaxwarning) {
- $hidemessage = get_string('upgradedgradeshidemessage', 'grades');
- $urlparams = array( 'id' => $courseid,
- 'seenminmaxupgradedgrades' => true,
- 'sesskey' => sesskey());
- $goawayurl = new moodle_url($thispage, $urlparams);
- $hideminmaxbutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
- $moreinfo = html_writer::link(get_docs_url(get_string('minmaxtouse_link', 'grades')), get_string('moreinfo'),
- array('target' => '_blank'));
- if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_ITEM) {
- // Show the message that there were min/max issues that have been resolved.
- $message = get_string('minmaxupgradedgrades', 'grades') . ' ' . $moreinfo;
- $revertmessage = get_string('upgradedminmaxrevertmessage', 'grades');
- $urlparams = array('id' => $courseid,
- 'useminmaxfromgradegrade' => true,
- 'sesskey' => sesskey());
- $reverturl = new moodle_url($thispage, $urlparams);
- $revertbutton = $OUTPUT->single_button($reverturl, $revertmessage, 'get');
- $html .= $OUTPUT->notification($message);
- $html .= $revertbutton . $hideminmaxbutton;
- } else if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_GRADE) {
- // Show the warning that there are min/max issues that have not be resolved.
- $message = get_string('minmaxupgradewarning', 'grades') . ' ' . $moreinfo;
- $fixmessage = get_string('minmaxupgradefixbutton', 'grades');
- $urlparams = array('id' => $courseid,
- 'useminmaxfromgradeitem' => true,
- 'sesskey' => sesskey());
- $fixurl = new moodle_url($thispage, $urlparams);
- $fixbutton = $OUTPUT->single_button($fixurl, $fixmessage, 'get');
- $html .= $OUTPUT->notification($message);
- $html .= $fixbutton . $hideminmaxbutton;
- }
- }
- if ($gradebookcalculationsfreeze) {
- if ($acceptgradebookchanges) {
- // Accept potential changes in grades caused by extra credit bug MDL-49257.
- hide_gradebook_calculations_freeze_notice($courseid);
- $courseitem = grade_item::fetch_course_item($courseid);
- $courseitem->force_regrading();
- grade_regrade_final_grades($courseid);
- $html .= $OUTPUT->notification(get_string('gradebookcalculationsuptodate', 'grades'), 'notifysuccess');
- } else {
- // Show the warning that there may be extra credit weights problems.
- $a = new stdClass();
- $a->gradebookversion = $gradebookcalculationsfreeze;
- if (preg_match('/(\d{8,})/', $CFG->release, $matches)) {
- $a->currentversion = $matches[1];
- } else {
- $a->currentversion = $CFG->release;
- }
- $a->url = get_docs_url('Gradebook_calculation_changes');
- $message = get_string('gradebookcalculationswarning', 'grades', $a);
- $fixmessage = get_string('gradebookcalculationsfixbutton', 'grades');
- $urlparams = array('id' => $courseid,
- 'acceptgradebookchanges' => true,
- 'sesskey' => sesskey());
- $fixurl = new moodle_url($thispage, $urlparams);
- $fixbutton = $OUTPUT->single_button($fixurl, $fixmessage, 'get');
- $html .= $OUTPUT->notification($message);
- $html .= $fixbutton;
- }
- }
- if (!empty($html)) {
- $html = html_writer::tag('div', $html, array('class' => 'core_grades_notices'));
- }
- if ($return) {
- return $html;
- } else {
- echo $html;
- }
- }
- /**
- * Print grading plugin selection popup form.
- *
- * @param array $plugin_info An array of plugins containing information for the selector
- * @param boolean $return return as string
- *
- * @return nothing or string if $return true
- */
- function print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, $return=false) {
- global $CFG, $OUTPUT, $PAGE;
- $menu = array();
- $count = 0;
- $active = '';
- foreach ($plugin_info as $plugin_type => $plugins) {
- if ($plugin_type == 'strings') {
- continue;
- }
- $first_plugin = reset($plugins);
- $sectionname = $plugin_info['strings'][$plugin_type];
- $section = array();
- foreach ($plugins as $plugin) {
- $link = $plugin->link->out(false);
- $section[$link] = $plugin->string;
- $count++;
- if ($plugin_type === $active_type and $plugin->id === $active_plugin) {
- $active = $link;
- }
- }
- if ($section) {
- $menu[] = array($sectionname=>$section);
- }
- }
- // finally print/return the popup form
- if ($count > 1) {
- $select = new url_select($menu, $active, null, 'choosepluginreport');
- $select->set_label(get_string('gradereport', 'grades'), array('class' => 'accesshide'));
- if ($return) {
- return $OUTPUT->render($select);
- } else {
- echo $OUTPUT->render($select);
- }
- } else {
- // only one option - no plugin selector needed
- return '';
- }
- }
- /**
- * Print grading plugin selection tab-based navigation.
- *
- * @param string $active_type type of plugin on current page - import, export, report or edit
- * @param string $active_plugin active plugin type - grader, user, cvs, ...
- * @param array $plugin_info Array of plugins
- * @param boolean $return return as string
- *
- * @return nothing or string if $return true
- */
- function grade_print_tabs($active_type, $active_plugin, $plugin_info, $return=false) {
- global $CFG, $COURSE;
- if (!isset($currenttab)) { //TODO: this is weird
- $currenttab = '';
- }
- $tabs = array();
- $top_row = array();
- $bottom_row = array();
- $inactive = array($active_plugin);
- $activated = array($active_type);
- $count = 0;
- $active = '';
- foreach ($plugin_info as $plugin_type => $plugins) {
- if ($plugin_type == 'strings') {
- continue;
- }
- // If $plugins is actually the definition of a child-less parent link:
- if (!empty($plugins->id)) {
- $string = $plugins->string;
- if (!empty($plugin_info[$active_type]->parent)) {
- $string = $plugin_info[$active_type]->parent->string;
- }
- $top_row[] = new tabobject($plugin_type, $plugins->link, $string);
- continue;
- }
- $first_plugin = reset($plugins);
- $url = $first_plugin->link;
- if ($plugin_type == 'report') {
- $url = $CFG->wwwroot.'/grade/report/index.php?id='.$COURSE->id;
- }
- $top_row[] = new tabobject($plugin_type, $url, $plugin_info['strings'][$plugin_type]);
- if ($active_type == $plugin_type) {
- foreach ($plugins as $plugin) {
- $bottom_row[] = new tabobject($plugin->id, $plugin->link, $plugin->string);
- if ($plugin->id == $active_plugin) {
- $inactive = array($plugin->id);
- }
- }
- }
- }
- // Do not display rows that contain only one item, they are not helpful.
- if (count($top_row) > 1) {
- $tabs[] = $top_row;
- }
- if (count($bottom_row) > 1) {
- $tabs[] = $bottom_row;
- }
- if (empty($tabs)) {
- return;
- }
- $rv = html_writer::div(print_tabs($tabs, $active_plugin, $inactive, $activated, true), 'grade-navigation');
- if ($return) {
- return $rv;
- } else {
- echo $rv;
- }
- }
- /**
- * grade_get_plugin_info
- *
- * @param int $courseid The course id
- * @param string $active_type type of plugin on current page - import, export, report or edit
- * @param string $active_plugin active plugin type - grader, user, cvs, ...
- *
- * @return array
- */
- function grade_get_plugin_info($courseid, $active_type, $active_plugin) {
- global $CFG, $SITE;
- $context = context_course::instance($courseid);
- $plugin_info = array();
- $count = 0;
- $active = '';
- $url_prefix = $CFG->wwwroot . '/grade/';
- // Language strings
- $plugin_info['strings'] = grade_helper::get_plugin_strings();
- if ($reports = grade_helper::get_plugins_reports($courseid)) {
- $plugin_info['report'] = $reports;
- }
- if ($settings = grade_helper::get_info_manage_settings($courseid)) {
- $plugin_info['settings'] = $settings;
- }
- if ($scale = grade_helper::get_info_scales($courseid)) {
- $plugin_info['scale'] = array('view'=>$scale);
- }
- if ($outcomes = grade_helper::get_info_outcomes($courseid)) {
- $plugin_info['outcome'] = $outcomes;
- }
- if ($letters = grade_helper::get_info_letters($courseid)) {
- $plugin_info['letter'] = $letters;
- }
- if ($imports = grade_helper::get_plugins_import($courseid)) {
- $plugin_info['import'] = $imports;
- }
- if ($exports = grade_helper::get_plugins_export($courseid)) {
- $plugin_info['export'] = $exports;
- }
- foreach ($plugin_info as $plugin_type => $plugins) {
- if (!empty($plugins->id) && $active_plugin == $plugins->id) {
- $plugin_info['strings']['active_plugin_str'] = $plugins->string;
- break;
- }
- foreach ($plugins as $plugin) {
- if (is_a($plugin, 'grade_plugin_info')) {
- if ($active_plugin == $plugin->id) {
- $plugin_info['strings']['active_plugin_str'] = $plugin->string;
- }
- }
- }
- }
- return $plugin_info;
- }
- /**
- * A simple class containing info about grade plugins.
- * Can be subclassed for special rules
- *
- * @package core_grades
- * @copyright 2009 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- class grade_plugin_info {
- /**
- * A unique id for this plugin
- *
- * @var mixed
- */
- public $id;
- /**
- * A URL to access this plugin
- *
- * @var mixed
- */
- public $link;
- /**
- * The name of this plugin
- *
- * @var mixed
- */
- public $string;
- /**
- * Another grade_plugin_info object, parent of the current one
- *
- * @var mixed
- */
- public $parent;
- /**
- * Constructor
- *
- * @param int $id A unique id for this plugin
- * @param string $link A URL to access this plugin
- * @param string $string The name of this plugin
- * @param object $parent Another grade_plugin_info object, parent of the current one
- *
- * @return void
- */
- public function __construct($id, $link, $string, $parent=null) {
- $this->id = $id;
- $this->link = $link;
- $this->string = $string;
- $this->parent = $parent;
- }
- }
- /**
- * Prints the page headers, breadcrumb trail, page heading, (optional) dropdown navigation menu and
- * (optional) navigation tabs for any gradebook page. All gradebook pages MUST use these functions
- * in favour of the usual print_header(), print_header_simple(), print_heading() etc.
- * !IMPORTANT! Use of tabs.php file in gradebook pages is forbidden unless tabs are switched off at
- * the site level for the gradebook ($CFG->grade_navmethod = GRADE_NAVMETHOD_DROPDOWN).
- *
- * @param int $courseid Course id
- * @param string $active_type The type of the current page (report, settings,
- * import, export, scales, outcomes, letters)
- * @param string $active_plugin The plugin of the current page (grader, fullview etc...)
- * @param string $heading The heading of the page. Tries to guess if none is given
- * @param boolean $return Whether to return (true) or echo (false) the HTML generated by this function
- * @param string $bodytags Additional attributes that will be added to the <body> tag
- * @param string $buttons Additional buttons to display on the page
- * @param boolean $shownavigation should the gradebook navigation drop down (or tabs) be shown?
- * @param string $headerhelpidentifier The help string identifier if required.
- * @param string $headerhelpcomponent The component for the help string.
- * @param stdClass $user The user object for use with the user context header.
- *
- * @return string HTML code or nothing if $return == false
- */
- function print_grade_page_head($courseid, $active_type, $active_plugin=null,
- $heading = false, $return=false,
- $buttons=false, $shownavigation=true, $headerhelpidentifier = null, $headerhelpcomponent = null,
- $user = null) {
- global $CFG, $OUTPUT, $PAGE;
- // Put a warning on all gradebook pages if the course has modules currently scheduled for background deletion.
- require_once($CFG->dirroot . '/course/lib.php');
- if (course_modules_pending_deletion($courseid, true)) {
- \core\notification::add(get_string('gradesmoduledeletionpendingwarning', 'grades'),
- \core\output\notification::NOTIFY_WARNING);
- }
- if ($active_type === 'preferences') {
- // In Moodle 2.8 report preferences were moved under 'settings'. Allow backward compatibility for 3rd party grade reports.
- $active_type = 'settings';
- }
- $plugin_info = grade_get_plugin_info($courseid, $active_type, $active_plugin);
- // Determine the string of the active plugin
- $stractive_plugin = ($active_plugin) ? $plugin_info['strings']['active_plugin_str'] : $heading;
- $stractive_type = $plugin_info['strings'][$active_type];
- if (empty($plugin_info[$active_type]->id) || !empty($plugin_info[$active_type]->parent)) {
- $title = $PAGE->course->fullname.': ' . $stractive_type . ': ' . $stractive_plugin;
- } else {
- $title = $PAGE->course->fullname.': ' . $stractive_plugin;
- }
- if ($active_type == 'report') {
- $PAGE->set_pagelayout('report');
- } else {
- $PAGE->set_pagelayout('admin');
- }
- $PAGE->set_title(get_string('grades') . ': ' . $stractive_type);
- $PAGE->set_heading($title);
- if ($buttons instanceof single_button) {
- $buttons = $OUTPUT->render($buttons);
- }
- $PAGE->set_button($buttons);
- if ($courseid != SITEID) {
- grade_extend_settings($plugin_info, $courseid);
- }
- // Set the current report as active in the breadcrumbs.
- if ($active_plugin !== null && $reportnav = $PAGE->settingsnav->find($active_plugin, navigation_node::TYPE_SETTING)) {
- $reportnav->make_active();
- }
- $returnval = $OUTPUT->header();
- if (!$return) {
- echo $returnval;
- }
- // Guess heading if not given explicitly
- if (!$heading) {
- $heading = $stractive_plugin;
- }
- if ($shownavigation) {
- $navselector = null;
- if ($courseid != SITEID &&
- ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_DROPDOWN)) {
- // It's absolutely essential that this grade plugin selector is shown after the user header. Just ask Fred.
- $navselector = print_grade_plugin_selector($plugin_info, $active_type, $active_plugin, true);
- if ($return) {
- $returnval .= $navselector;
- } else if (!isset($user)) {
- echo $navselector;
- }
- }
- $output = '';
- // Add a help dialogue box if provided.
- if (isset($headerhelpidentifier)) {
- $output = $OUTPUT->heading_with_help($heading, $headerhelpidentifier, $headerhelpcomponent);
- } else {
- if (isset($user)) {
- $output = $OUTPUT->context_header(
- array(
- 'heading' => html_writer::link(new moodle_url('/user/view.php', array('id' => $user->id,
- 'course' => $courseid)), fullname($user)),
- 'user' => $user,
- 'usercontext' => context_user::instance($user->id)
- ), 2
- ) . $navselector;
- } else {
- $output = $OUTPUT->heading($heading);
- }
- }
- if ($return) {
- $returnval .= $output;
- } else {
- echo $output;
- }
- if ($courseid != SITEID &&
- ($CFG->grade_navmethod == GRADE_NAVMETHOD_COMBO || $CFG->grade_navmethod == GRADE_NAVMETHOD_TABS)) {
- $returnval .= grade_print_tabs($active_type, $active_plugin, $plugin_info, $return);
- }
- }
- $returnval .= print_natural_aggregation_upgrade_notice($courseid,
- context_course::instance($courseid),
- $PAGE->url,
- $return);
- if ($return) {
- return $returnval;
- }
- }
- /**
- * Utility class used for return tracking when using edit and other forms in grade plugins
- *
- * @package core_grades
- * @copyright 2009 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- class grade_plugin_return {
- /**
- * Type of grade plugin (e.g. 'edit', 'report')
- *
- * @var string
- */
- public $type;
- /**
- * Name of grade plugin (e.g. 'grader', 'overview')
- *
- * @var string
- */
- public $plugin;
- /**
- * Course id being viewed
- *
- * @var int
- */
- public $courseid;
- /**
- * Id of user whose information is being viewed/edited
- *
- * @var int
- */
- public $userid;
- /**
- * Id of group for which information is being viewed/edited
- *
- * @var int
- */
- public $groupid;
- /**
- * Current page # within output
- *
- * @var int
- */
- public $page;
- /**
- * Constructor
- *
- * @param array $params - associative array with return parameters, if not supplied parameter are taken from _GET or _POST
- */
- public function __construct($params = []) {
- $this->type = optional_param('gpr_type', null, PARAM_SAFEDIR);
- $this->plugin = optional_param('gpr_plugin', null, PARAM_PLUGIN);
- $this->courseid = optional_param('gpr_courseid', null, PARAM_INT);
- $this->userid = optional_param('gpr_userid', null, PARAM_INT);
- $this->groupid = optional_param('gpr_groupid', null, PARAM_INT);
- $this->page = optional_param('gpr_page', null, PARAM_INT);
- foreach ($params as $key => $value) {
- if (property_exists($this, $key)) {
- $this->$key = $value;
- }
- }
- // Allow course object rather than id to be used to specify course
- // - avoid unnecessary use of get_course.
- if (array_key_exists('course', $params)) {
- $course = $params['course'];
- $this->courseid = $course->id;
- } else {
- $course = null;
- }
- // If group has been explicitly set in constructor parameters,
- // we should respect that.
- if (!array_key_exists('groupid', $params)) {
- // Otherwise, 'group' in request parameters is a request for a change.
- // In that case, or if we have no group at all, we should get groupid from
- // groups_get_course_group, which will do some housekeeping as well as
- // give us the correct value.
- $changegroup = optional_param('group', -1, PARAM_INT);
- if ($changegroup !== -1 or (empty($this->groupid) and !empty($this->courseid))) {
- if ($course === null) {
- $course = get_course($this->courseid);
- }
- $this->groupid = groups_get_course_group($course, true);
- }
- }
- }
- /**
- * Old syntax of class constructor. Deprecated in PHP7.
- *
- * @deprecated since Moodle 3.1
- */
- public function grade_plugin_return($params = null) {
- debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
- self::__construct($params);
- }
- /**
- * Returns return parameters as options array suitable for buttons.
- * @return array options
- */
- public function get_options() {
- if (empty($this->type)) {
- return array();
- }
- $params = array();
- if (!empty($this->plugin)) {
- $params['plugin'] = $this->plugin;
- }
- if (!empty($this->courseid)) {
- $params['id'] = $this->courseid;
- }
- if (!empty($this->userid)) {
- $params['userid'] = $this->userid;
- }
- if (!empty($this->groupid)) {
- $params['group'] = $this->groupid;
- }
- if (!empty($this->page)) {
- $params['page'] = $this->page;
- }
- return $params;
- }
- /**
- * Returns return url
- *
- * @param string $default default url when params not set
- * @param array $extras Extra URL parameters
- *
- * @return string url
- */
- public function get_return_url($default, $extras=null) {
- global $CFG;
- if (empty($this->type) or empty($this->plugin)) {
- return $default;
- }
- $url = $CFG->wwwroot.'/grade/'.$this->type.'/'.$this->plugin.'/index.php';
- $glue = '?';
- if (!empty($this->courseid)) {
- $url .= $glue.'id='.$this->courseid;
- $glue = '&';
- }
- if (!empty($this->userid)) {
- $url .= $glue.'userid='.$this->userid;
- $glue = '&';
- }
- if (!empty($this->groupid)) {
- $url .= $glue.'group='.$this->groupid;
- $glue = '&';
- }
- if (!empty($this->page)) {
- $url .= $glue.'page='.$this->page;
- $glue = '&';
- }
- if (!empty($extras)) {
- foreach ($extras as $key=>$value) {
- $url .= $glue.$key.'='.$value;
- $glue = '&';
- }
- }
- return $url;
- }
- /**
- * Returns string with hidden return tracking form elements.
- * @return string
- */
- public function get_form_fields() {
- if (empty($this->type)) {
- return '';
- }
- $result = '<input type="hidden" name="gpr_type" value="'.$this->type.'" />';
- if (!empty($this->plugin)) {
- $result .= '<input type="hidden" name="gpr_plugin" value="'.$this->plugin.'" />';
- }
- if (!empty($this->courseid)) {
- $result .= '<input type="hidden" name="gpr_courseid" value="'.$this->courseid.'" />';
- }
- if (!empty($this->userid)) {
- $result .= '<input type="hidden" name="gpr_userid" value="'.$this->userid.'" />';
- }
- if (!empty($this->groupid)) {
- $result .= '<input type="hidden" name="gpr_groupid" value="'.$this->groupid.'" />';
- }
- if (!empty($this->page)) {
- $result .= '<input type="hidden" name="gpr_page" value="'.$this->page.'" />';
- }
- return $result;
- }
- /**
- * Add hidden elements into mform
- *
- * @param object &$mform moodle form object
- *
- * @return void
- */
- public function add_mform_elements(&$mform) {
- if (empty($this->type)) {
- return;
- }
- $mform->addElement('hidden', 'gpr_type', $this->type);
- $mform->setType('gpr_type', PARAM_SAFEDIR);
- if (!empty($this->plugin)) {
- $mform->addElement('hidden', 'gpr_plugin', $this->plugin);
- $mform->setType('gpr_plugin', PARAM_PLUGIN);
- }
- if (!empty($this->courseid)) {
- $mform->addElement('hidden', 'gpr_courseid', $this->courseid);
- $mform->setType('gpr_courseid', PARAM_INT);
- }
- if (!empty($this->userid)) {
- $mform->addElement('hidden', 'gpr_userid', $this->userid);
- $mform->setType('gpr_userid', PARAM_INT);
- }
- if (!empty($this->groupid)) {
- $mform->addElement('hidden', 'gpr_groupid', $this->groupid);
- $mform->setType('gpr_groupid', PARAM_INT);
- }
- if (!empty($this->page)) {
- $mform->addElement('hidden', 'gpr_page', $this->page);
- $mform->setType('gpr_page', PARAM_INT);
- }
- }
- /**
- * Add return tracking params into url
- *
- * @param moodle_url $url A URL
- *
- * @return string $url with return tracking params
- */
- public function add_url_params(moodle_url $url) {
- if (empty($this->type)) {
- return $url;
- }
- $url->param('gpr_type', $this->type);
- if (!empty($this->plugin)) {
- $url->param('gpr_plugin', $this->plugin);
- }
- if (!empty($this->courseid)) {
- $url->param('gpr_courseid' ,$this->courseid);
- }
- if (!empty($this->userid)) {
- $url->param('gpr_userid', $this->userid);
- }
- if (!empty($this->groupid)) {
- $url->param('gpr_groupid', $this->groupid);
- }
- if (!empty($this->page)) {
- $url->param('gpr_page', $this->page);
- }
- return $url;
- }
- }
- /**
- * Function central to gradebook for building and printing the navigation (breadcrumb trail).
- *
- * @param string $path The path of the calling script (using __FILE__?)
- * @param string $pagename The language string to use as the last part of the navigation (non-link)
- * @param mixed $id Either a plain integer (assuming the key is 'id') or
- * an array of keys and values (e.g courseid => $courseid, itemid...)
- *
- * @return string
- */
- function grade_build_nav($path, $pagename=null, $id=null) {
- global $CFG, $COURSE, $PAGE;
- $strgrades = get_string('grades', 'grades');
- // Parse the path and build navlinks from its elements
- $dirroot_length = strlen($CFG->dirroot) + 1; // Add 1 for the first slash
- $path = substr($path, $dirroot_length);
- $path = str_replace('\\', '/', $path);
- $path_elements = explode('/', $path);
- $path_elements_count = count($path_elements);
- // First link is always 'grade'
- $PAGE->navbar->add($strgrades, new moodle_url('/grade/index.php', array('id'=>$COURSE->id)));
- $link = null;
- $numberofelements = 3;
- // Prepare URL params string
- $linkparams = array();
- if (!is_null($id)) {
- if (is_array($id)) {
- foreach ($id as $idkey => $idvalue) {
- $linkparams[$idkey] = $idvalue;
- }
- } else {
- $linkparams['id'] = $id;
- }
- }
- $navlink4 = null;
- // Remove file extensions from filenames
- foreach ($path_elements as $key => $filename) {
- $path_elements[$key] = str_replace('.php', '', $filename);
- }
- // Second le…
Large files files are truncated, but you can click here to view the full file