📄 lib.php
字号:
<?php // $Id: lib.php,v 1.98.2.49 2009/01/01 23:53:47 skodak Exp $///////////////////////////////////////////////////////////////////////////// //// NOTICE OF COPYRIGHT //// //// Moodle - Modular Object-Oriented Dynamic Learning Environment //// http://moodle.com //// //// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //// //// This program 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 2 of the License, or //// (at your option) any later version. //// //// This program 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: //// //// http://www.gnu.org/copyleft/gpl.html //// //////////////////////////////////////////////////////////////////////////////** * File in which the grader_report class is defined. * @package gradebook */require_once($CFG->dirroot . '/grade/report/lib.php');require_once($CFG->libdir.'/tablelib.php');/** * Class providing an API for the grader report building and displaying. * @uses grade_report * @package gradebook */class grade_report_grader extends grade_report { /** * The final grades. * @var array $grades */ var $grades; /** * Array of errors for bulk grades updating. * @var array $gradeserror */ var $gradeserror = array();//// SQL-RELATED /** * The id of the grade_item by which this report will be sorted. * @var int $sortitemid */ var $sortitemid; /** * Sortorder used in the SQL selections. * @var int $sortorder */ var $sortorder; /** * An SQL fragment affecting the search for users. * @var string $userselect */ var $userselect; /** * List of collapsed categories from user preference * @var array $collapsed */ var $collapsed; /** * A count of the rows, used for css classes. * @var int $rowcount */ var $rowcount = 0; /** * Capability check caching * */ var $canviewhidden; /** * Constructor. Sets local copies of user preferences and initialises grade_tree. * @param int $courseid * @param object $gpr grade plugin return tracking object * @param string $context * @param int $page The current page being viewed (when report is paged) * @param int $sortitemid The id of the grade_item by which to sort the table */ function grade_report_grader($courseid, $gpr, $context, $page=null, $sortitemid=null) { global $CFG; parent::grade_report($courseid, $gpr, $context, $page); $this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id)); // load collapsed settings for this report if ($collapsed = get_user_preferences('grade_report_grader_collapsed_categories')) { $this->collapsed = unserialize($collapsed); } else { $this->collapsed = array('aggregatesonly' => array(), 'gradesonly' => array()); } if (empty($CFG->enableoutcomes)) { $nooutcomes = false; } else { $nooutcomes = get_user_preferences('grade_report_shownooutcomes'); } // if user report preference set or site report setting set use it, otherwise use course or site setting $switch = $this->get_pref('aggregationposition'); if ($switch == '') { $switch = grade_get_setting($this->courseid, 'aggregationposition', $CFG->grade_aggregationposition); } // Grab the grade_tree for this course $this->gtree = new grade_tree($this->courseid, true, $switch, $this->collapsed, $nooutcomes); $this->sortitemid = $sortitemid; // base url for sorting by first/last name $studentsperpage = $this->get_pref('studentsperpage'); $perpage = ''; $curpage = ''; if (!empty($studentsperpage)) { $perpage = '&perpage='.$studentsperpage; $curpage = '&page='.$this->page; } $this->baseurl = 'index.php?id='.$this->courseid. $perpage.$curpage.'&'; $this->pbarurl = 'index.php?id='.$this->courseid.$perpage.'&'; // Setup groups if requested if ($this->get_pref('showgroups')) { $this->setup_groups(); } $this->setup_sortitemid(); } /** * Processes the data sent by the form (grades and feedbacks). * Caller is reposible for all access control checks * @param array $data form submission (with magic quotes) * @return array empty array if success, array of warnings if something fails. */ function process_data($data) { $warnings = array(); // always initialize all arrays $queue = array(); foreach ($data as $varname => $postedvalue) { $needsupdate = false; // skip, not a grade nor feedback if (strpos($varname, 'grade') === 0) { $data_type = 'grade'; } else if (strpos($varname, 'feedback') === 0) { $data_type = 'feedback'; } else { continue; } $gradeinfo = explode("_", $varname); $userid = clean_param($gradeinfo[1], PARAM_INT); $itemid = clean_param($gradeinfo[2], PARAM_INT); $oldvalue = $data->{'old'.$varname}; // was change requested? if ($oldvalue == $postedvalue) { // string comparison continue; } if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$this->courseid))) { // we must verify course id here! error('Incorrect grade item id'); } // Pre-process grade if ($data_type == 'grade') { $feedback = false; $feedbackformat = false; if ($grade_item->gradetype == GRADE_TYPE_SCALE) { if ($postedvalue == -1) { // -1 means no grade $finalgrade = null; } else { $finalgrade = $postedvalue; } } else { $finalgrade = unformat_float($postedvalue); } $errorstr = ''; // Warn if the grade is out of bounds. if (is_null($finalgrade)) { // ok } else if ($finalgrade < $grade_item->grademin) { $errorstr = 'lessthanmin'; } else if ($finalgrade > $grade_item->grademax) { $errorstr = 'morethanmax'; } if ($errorstr) { $user = get_record('user', 'id', $userid, '', '', '', '', 'id, firstname, lastname'); $gradestr = new object(); $gradestr->username = fullname($user); $gradestr->itemname = $grade_item->get_name(); $warnings[] = get_string($errorstr, 'grades', $gradestr); } } else if ($data_type == 'feedback') { $finalgrade = false; $trimmed = trim($postedvalue); if (empty($trimmed)) { $feedback = NULL; } else { $feedback = stripslashes($postedvalue); } } $grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE); } return $warnings; } /** * Setting the sort order, this depends on last state * all this should be in the new table class that we might need to use * for displaying grades. */ function setup_sortitemid() { global $SESSION; if ($this->sortitemid) { if (!isset($SESSION->gradeuserreport->sort)) { if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') { $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC'; } else { $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC'; } } else { // this is the first sort, i.e. by last name if (!isset($SESSION->gradeuserreport->sortitemid)) { if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') { $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC'; } else { $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC'; } } else if ($SESSION->gradeuserreport->sortitemid == $this->sortitemid) { // same as last sort if ($SESSION->gradeuserreport->sort == 'ASC') { $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC'; } else { $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC'; } } else { if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') { $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC'; } else { $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC'; } } } $SESSION->gradeuserreport->sortitemid = $this->sortitemid; } else { // not requesting sort, use last setting (for paging) if (isset($SESSION->gradeuserreport->sortitemid)) { $this->sortitemid = $SESSION->gradeuserreport->sortitemid; }else{ $this->sortitemid = 'lastname'; } if (isset($SESSION->gradeuserreport->sort)) { $this->sortorder = $SESSION->gradeuserreport->sort; } else { $this->sortorder = 'ASC'; } } } /** * pulls out the userids of the users to be display, and sorts them */ function load_users() { global $CFG; if (is_numeric($this->sortitemid)) { // the MAX() magic is required in order to please PG $sort = "MAX(g.finalgrade) $this->sortorder"; $sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber FROM {$CFG->prefix}user u JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id $this->groupsql LEFT JOIN {$CFG->prefix}grade_grades g ON (g.userid = u.id AND g.itemid = $this->sortitemid) WHERE ra.roleid in ($this->gradebookroles) AND u.deleted = 0 $this->groupwheresql AND ra.contextid ".get_related_contexts_string($this->context)." GROUP BY u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber ORDER BY $sort"; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -