. /** * 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 */ public $grades; /** * Array of errors for bulk grades updating. * @var array $gradeserror */ public $gradeserror = array(); //// SQL-RELATED /** * The id of the grade_item by which this report will be sorted. * @var int $sortitemid */ public $sortitemid; /** * Sortorder used in the SQL selections. * @var int $sortorder */ public $sortorder; /** * An SQL fragment affecting the search for users. * @var string $userselect */ public $userselect; /** * The bound params for $userselect * @var array $userselectparams */ public $userselectparams = array(); /** * List of collapsed categories from user preference * @var array $collapsed */ public $collapsed; /** * A count of the rows, used for css classes. * @var int $rowcount */ public $rowcount = 0; /** * Capability check caching * */ public $canviewhidden; var $preferencespage=false; /** * 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 */ public function __construct($courseid, $gpr, $context, $page=null, $sortitemid=null) { global $CFG; parent::__construct($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 $this->baseurl = new moodle_url('index.php', array('id' => $this->courseid)); $studentsperpage = $this->get_pref('studentsperpage'); if (!empty($studentsperpage)) { $this->baseurl->params(array('perpage' => $studentsperpage, 'page' => $this->page)); } $this->pbarurl = new moodle_url('index.php', array('id' => $this->courseid, 'perpage' => $studentsperpage)); $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. */ public function process_data($data) { global $DB; $warnings = array(); $separategroups = false; $mygroups = array(); if ($this->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $this->context)) { $separategroups = true; $mygroups = groups_get_user_groups($this->course->id); $mygroups = $mygroups[0]; // ignore groupings // reorder the groups fro better perf bellow $current = array_search($this->currentgroup, $mygroups); if ($current !== false) { unset($mygroups[$current]); array_unshift($mygroups, $this->currentgroup); } } // always initialize all arrays $queue = array(); foreach ($data as $varname => $postedvalue) { $needsupdate = false; // skip, not a grade nor feedback if (strpos($varname, 'grade') === 0) { $datatype = 'grade'; } else if (strpos($varname, 'feedback') === 0) { $datatype = '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 (!$gradeitem = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$this->courseid))) { // we must verify course id here! print_error('invalidgradeitmeid'); } // Pre-process grade if ($datatype == 'grade') { $feedback = false; $feedbackformat = false; if ($gradeitem->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 { $bounded = $gradeitem->bounded_grade($finalgrade); if ($bounded > $finalgrade) { $errorstr = 'lessthanmin'; } else if ($bounded < $finalgrade) { $errorstr = 'morethanmax'; } } if ($errorstr) { $user = $DB->get_record('user', array('id' => $userid), 'id, firstname, lastname'); $gradestr = new object(); $gradestr->username = fullname($user); $gradestr->itemname = $gradeitem->get_name(); $warnings[] = get_string($errorstr, 'grades', $gradestr); } } else if ($datatype == 'feedback') { $finalgrade = false; $trimmed = trim($postedvalue); if (empty($trimmed)) { $feedback = NULL; } else { $feedback = stripslashes($postedvalue); } } // group access control if ($separategroups) { // note: we can not use $this->currentgroup because it would fail badly // when having two browser windows each with different group $sharinggroup = false; foreach($mygroups as $groupid) { if (groups_is_member($groupid, $userid)) { $sharinggroup = true; break; } } if (!$sharinggroup) { // either group membership changed or somebedy is hacking grades of other group $warnings[] = get_string('errorsavegrade', 'grades'); continue; } } $gradeitem->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. */ private 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 */ public function load_users() { global $CFG, $DB; list($usql, $gbrparams) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0'); if (is_numeric($this->sortitemid)) { $params = array_merge(array('gitemid'=>$this->sortitemid), $gbrparams, $this->groupwheresql_params); // 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 {user} u JOIN {role_assignments} ra ON ra.userid = u.id $this->groupsql LEFT JOIN {grade_grades} g ON (g.userid = u.id AND g.itemid = :gitemid) WHERE ra.roleid $usql 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 { switch($this->sortitemid) { case 'lastname': $sort = "u.lastname $this->sortorder, u.firstname $this->sortorder"; break; case 'firstname': $sort = "u.firstname $this->sortorder, u.lastname $this->sortorder"; break; case 'idnumber': default: $sort = "u.idnumber $this->sortorder"; break; } $params = array_merge($gbrparams, $this->groupwheresql_params); $sql = "SELECT DISTINCT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber FROM {user} u JOIN {role_assignments} ra ON u.id = ra.userid $this->groupsql WHERE ra.roleid $usql AND u.deleted = 0 $this->groupwheresql AND ra.contextid ".get_related_contexts_string($this->context)." ORDER BY $sort"; } $this->users = $DB->get_records_sql($sql, $params, $this->get_pref('studentsperpage') * $this->page, $this->get_pref('studentsperpage')); if (empty($this->users)) { $this->userselect = ''; $this->users = array(); $this->userselect_params = array(); } else { list($usql, $params) = $DB->get_in_or_equal(array_keys($this->users), SQL_PARAMS_NAMED, 'usid0'); $this->userselect = "AND g.userid $usql"; $this->userselect_params = $params; } return $this->users; } /** * we supply the userids in this query, and get all the grades * pulls out all the grades, this does not need to worry about paging */ public function load_final_grades() { global $CFG, $DB; // please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it! $params = array_merge(array('courseid'=>$this->courseid), $this->userselect_params); $sql = "SELECT g.* FROM {grade_items} gi, {grade_grades} g WHERE g.itemid = gi.id AND gi.courseid = :courseid {$this->userselect}"; $userids = array_keys($this->users); if ($grades = $DB->get_records_sql($sql, $params)) { foreach ($grades as $graderec) { if (in_array($graderec->userid, $userids) and array_key_exists($graderec->itemid, $this->gtree->get_items())) { // some items may not be present!! $this->grades[$graderec->userid][$graderec->itemid] = new grade_grade($graderec, false); $this->grades[$graderec->userid][$graderec->itemid]->grade_item =& $this->gtree->get_item($graderec->itemid); // db caching } } } // prefil grades that do not exist yet foreach ($userids as $userid) { foreach ($this->gtree->get_items() as $itemid=>$unused) { if (!isset($this->grades[$userid][$itemid])) { $this->grades[$userid][$itemid] = new grade_grade(); $this->grades[$userid][$itemid]->itemid = $itemid; $this->grades[$userid][$itemid]->userid = $userid; $this->grades[$userid][$itemid]->grade_item =& $this->gtree->get_item($itemid); // db caching } } } } /** * Builds and returns a div with on/off toggles. * @return string HTML code */ public function get_toggles_html() { global $CFG, $USER, $COURSE, $OUTPUT; $html = ''; if ($USER->gradeediting[$this->courseid]) { if (has_capability('moodle/grade:manage', $this->context) or has_capability('moodle/grade:hide', $this->context)) { $html .= $this->print_toggle('eyecons'); } if (has_capability('moodle/grade:manage', $this->context) or has_capability('moodle/grade:lock', $this->context) or has_capability('moodle/grade:unlock', $this->context)) { $html .= $this->print_toggle('locks'); } if (has_capability('moodle/grade:manage', $this->context)) { $html .= $this->print_toggle('quickfeedback'); } if (has_capability('moodle/grade:manage', $this->context)) { $html .= $this->print_toggle('calculations'); } } if ($this->canviewhidden) { $html .= $this->print_toggle('averages'); } $html .= $this->print_toggle('ranges'); if (!empty($CFG->enableoutcomes)) { $html .= $this->print_toggle('nooutcomes'); } return $OUTPUT->container($html, 'grade-report-toggles'); } /** * Shortcut function for printing the grader report toggles. * @param string $type The type of toggle * @param bool $return Whether to return the HTML string rather than printing it * @return void */ public function print_toggle($type) { global $CFG, $OUTPUT; $icons = array('eyecons' => 't/hide', 'calculations' => 't/calc', 'locks' => 't/lock', 'averages' => 't/mean', 'quickfeedback' => 't/feedback', 'nooutcomes' => 't/outcomes'); $prefname = 'grade_report_show' . $type; if (array_key_exists($prefname, $CFG)) { $showpref = get_user_preferences($prefname, $CFG->$prefname); } else { $showpref = get_user_preferences($prefname); } $strshow = $this->get_lang_string('show' . $type, 'grades'); $strhide = $this->get_lang_string('hide' . $type, 'grades'); $showhide = 'show'; $toggleaction = 1; if ($showpref) { $showhide = 'hide'; $toggleaction = 0; } if (array_key_exists($type, $icons)) { $imagename = $icons[$type]; } else { $imagename = "t/$type.gif"; } $string = ${'str' . $showhide}; $toggleicon = new moodle_action_icon(); $toggleicon->image->src = $OUTPUT->old_icon_url($imagename); $toggleicon->image->add_class('iconsmall'); $toggleicon->alt = $string; $toggleicon->title = $string; $toggleicon->link->url = $this->baseurl->out(false, array('toggle' => $toggleaction, 'toggle_type' => $type), false); $retval = $OUTPUT->container($OUTPUT->action_icon($toggleicon, 'gradertoggle')); return $retval; } /** * Builds and returns the HTML code for the headers. * @return string $headerhtml */ public function get_headerhtml() { global $CFG, $USER, $OUTPUT; $this->rowcount = 0; $fixedstudents = $this->is_fixed_students(); if (!$fixedstudents) { $strsortasc = $this->get_lang_string('sortasc', 'grades'); $strsortdesc = $this->get_lang_string('sortdesc', 'grades'); $strfirstname = $this->get_lang_string('firstname'); $strlastname = $this->get_lang_string('lastname'); $showuseridnumber = $this->get_pref('showuseridnumber'); if ($this->sortitemid === 'lastname') { if ($this->sortorder == 'ASC') { $lastarrow = print_arrow('up', $strsortasc, true); } else { $lastarrow = print_arrow('down', $strsortdesc, true); } } else { $lastarrow = ''; } if ($this->sortitemid === 'firstname') { if ($this->sortorder == 'ASC') { $firstarrow = print_arrow('up', $strsortasc, true); } else { $firstarrow = print_arrow('down', $strsortdesc, true); } } else { $firstarrow = ''; } } // Prepare Table Headers $headerhtml = ''; $numrows = count($this->gtree->get_levels()); $columnstounset = array(); foreach ($this->gtree->get_levels() as $key=>$row) { $columncount = 0; if ($key == 0) { // do not display course grade category // continue; } if ($fixedstudents) { $headerhtml .= ''; } else { $headerhtml .= ''; if ($key == $numrows - 1) { $sortfirstname = html_link::make(clone($this->baseurl), $strfirstname); $sortfirstname->url->param('sortitemid', 'firstname'); $sortlastname = html_link::make(clone($this->baseurl), $strlastname); $sortlastname->url->param('sortitemid', 'lastname'); $headerhtml .= '' . $OUTPUT->link($sortfirstname) . $firstarrow. '/ ' . $OUTPUT->link($sortlastname) . $lastarrow .''; if ($showuseridnumber) { if ('idnumber' == $this->sortitemid) { if ($this->sortorder == 'ASC') { $idnumberarrow = print_arrow('up', $strsortasc, true); } else { $idnumberarrow = print_arrow('down', $strsortdesc, true); } } else { $idnumberarrow = ''; } $sortidnumber = html_link::make(clone($this->baseurl), get_string('idnumber')); $sortidnumber->url->param('sortitemid', 'idnumber'); $headerhtml .= '' . $OUTPUT->link($sortidnumber) . $idnumberarrow . ''; } } else { $colspan=''; if ($showuseridnumber) { $colspan = 'colspan="2" '; } $headerhtml .= ' '; if ($showuseridnumber) { $columncount++; } } } foreach ($row as $columnkey => $element) { $sortlink = clone($this->baseurl); if (isset($element['object']->id)) { $sortlink->param('sortitemid', $element['object']->id); } $eid = $element['eid']; $object = $element['object']; $type = $element['type']; $categorystate = @$element['categorystate']; $itemmodule = null; $iteminstance = null; $columnclass = 'c' . $columncount++; if (!empty($element['colspan'])) { $colspan = 'colspan="'.$element['colspan'].'"'; $columnclass = ''; } else { $colspan = ''; } if (!empty($element['depth'])) { $catlevel = ' catlevel'.$element['depth']; } else { $catlevel = ''; } // Element is a filler if ($type == 'filler' or $type == 'fillerfirst' or $type == 'fillerlast') { $headerhtml .= ' '; } // Element is a category else if ($type == 'category') { $headerhtml .= '' . shorten_text($element['object']->get_name()); $headerhtml .= $this->get_collapsing_icon($element); // Print icons if ($USER->gradeediting[$this->courseid]) { $headerhtml .= $this->get_icons($element); } $headerhtml .= ''; } // Element is a grade_item else { $itemmodule = $element['object']->itemmodule; $iteminstance = $element['object']->iteminstance; if ($element['object']->id == $this->sortitemid) { if ($this->sortorder == 'ASC') { $arrow = $this->get_sort_arrow('up', $sortlink); } else { $arrow = $this->get_sort_arrow('down', $sortlink); } } else { $arrow = $this->get_sort_arrow('move', $sortlink); } $hidden = ''; if ($element['object']->is_hidden()) { $hidden = ' hidden '; } $headerlink = $this->gtree->get_element_header($element, true, $this->get_pref('showactivityicons'), false); $headerhtml .= '' . shorten_text($headerlink) . $arrow; $headerhtml .= ''; } } $headerhtml .= ''; } return $headerhtml; } /** * Builds and return the HTML rows of the table (grades headed by student). * @return string HTML */ public function get_studentshtml() { global $CFG, $USER, $DB, $OUTPUT; $studentshtml = ''; $strfeedback = $this->get_lang_string("feedback"); $strgrade = $this->get_lang_string('grade'); $gradetabindex = 1; $numusers = count($this->users); $showuserimage = $this->get_pref('showuserimage'); $showuseridnumber = $this->get_pref('showuseridnumber'); $fixedstudents = $this->is_fixed_students(); // Preload scale objects for items with a scaleid $scaleslist = array(); $tabindices = array(); foreach ($this->gtree->get_items() as $item) { if (!empty($item->scaleid)) { $scaleslist[] = $item->scaleid; } $tabindices[$item->id]['grade'] = $gradetabindex; $tabindices[$item->id]['feedback'] = $gradetabindex + $numusers; $gradetabindex += $numusers * 2; } $scalesarray = array(); if (!empty($scaleslist)) { $scalesarray = $DB->get_records_list('scale', 'id', $scaleslist); } $rowclasses = array(' even ', ' odd '); $rowclasses = array(' even ', ' odd '); foreach ($this->users as $userid => $user) { if ($this->canviewhidden) { $altered = array(); $unknown = array(); } else { $hidingaffected = grade_grade::get_hiding_affected($this->grades[$userid], $this->gtree->get_items()); $altered = $hidingaffected['altered']; $unknown = $hidingaffected['unknown']; unset($hidingaffected); } $columncount = 0; if ($fixedstudents) { $studentshtml .= ''; } else { // Student name and link $userpic = null; if ($showuserimage) { $userpic = $OUTPUT->container($OUTPUT->user_picture(moodle_user_picture::make($user, $this->courseid)), 'userpic'); } $userlink = html_link::make(new moodle_url($CFG->wwwroot.'/user/view.php', array('id' => $user->id, 'course' => $this->course->id)), fullname($user)); $studentshtml .= '' .''.$userpic .$OUTPUT->link($userlink).''; if ($showuseridnumber) { $studentshtml .= ''. $user->idnumber.''; } } foreach ($this->gtree->items as $itemid=>$unused) { $item =& $this->gtree->items[$itemid]; $grade = $this->grades[$userid][$item->id]; // Get the decimal points preference for this item $decimalpoints = $item->get_decimals(); if (in_array($itemid, $unknown)) { $gradeval = null; } else if (array_key_exists($itemid, $altered)) { $gradeval = $altered[$itemid]; } else { $gradeval = $grade->finalgrade; } // MDL-11274 // Hide grades in the grader report if the current grader doesn't have 'moodle/grade:viewhidden' if (!$this->canviewhidden and $grade->is_hidden()) { if (!empty($CFG->grade_hiddenasdate) and $grade->get_datesubmitted() and !$item->is_category_item() and !$item->is_course_item()) { // the problem here is that we do not have the time when grade value was modified, 'timemodified' is general modification date for grade_grades records $studentshtml .= '' . $OUTPUT->span(userdate($grade->get_datesubmitted(),get_string('strftimedatetimeshort')), 'datesubmitted') . ''; } else { $studentshtml .= '-'; } continue; } // emulate grade element $eid = $this->gtree->get_grade_eid($grade); $element = array('eid'=>$eid, 'object'=>$grade, 'type'=>'grade'); $cellclasses = 'grade cell c'.$columncount++; if ($item->is_category_item()) { $cellclasses .= ' cat'; } if ($item->is_course_item()) { $cellclasses .= ' course'; } if ($grade->is_overridden()) { $cellclasses .= ' overridden'; } if ($grade->is_excluded()) { // $cellclasses .= ' excluded'; } $gradetitle = $OUTPUT->container(fullname($user), 'fullname'); $gradetitle .= $OUTPUT->container($item->get_name(true), 'itemname'); if (!empty($grade->feedback) && !$USER->gradeediting[$this->courseid]) { $gradetitle .= $OUTPUT->container(wordwrap(trim(format_string($grade->feedback, $grade->feedbackformat)), 34, '
'), 'feedback'); } $studentshtml .= ''; if ($grade->is_excluded()) { $studentshtml .= $OUTPUT->span(get_string('excluded', 'grades'), 'excludedfloater'); } // Do not show any icons if no grade (no record in DB to match) if (!$item->needsupdate and $USER->gradeediting[$this->courseid]) { $studentshtml .= $this->get_icons($element); } $hidden = ''; if ($grade->is_hidden()) { $hidden = ' hidden '; } $gradepass = ' gradefail '; if ($grade->is_passed($item)) { $gradepass = ' gradepass '; } elseif (is_null($grade->is_passed($item))) { $gradepass = ''; } // if in editting mode, we need to print either a text box // or a drop down (for scales) // grades in item of type grade category or course are not directly editable if ($item->needsupdate) { $studentshtml .= $OUTPUT->span(get_string('error'), "gradingerror$hidden"); } else if ($USER->gradeediting[$this->courseid]) { if ($item->scaleid && !empty($scalesarray[$item->scaleid])) { $scale = $scalesarray[$item->scaleid]; $gradeval = (int)$gradeval; // scales use only integers $scales = explode(",", $scale->scale); // reindex because scale is off 1 // MDL-12104 some previous scales might have taken up part of the array // so this needs to be reset $scaleopt = array(); $i = 0; foreach ($scales as $scaleoption) { $i++; $scaleopt[$i] = $scaleoption; } if ($this->get_pref('quickgrading') and $grade->is_editable()) { $oldval = empty($gradeval) ? -1 : $gradeval; if (empty($item->outcomeid)) { $nogradestr = $this->get_lang_string('nograde'); } else { $nogradestr = $this->get_lang_string('nooutcome', 'grades'); } $studentshtml .= ''; $select = html_select::make($scaleopt, 'grade_'.$userid.'_'.$item->id,$gradeval, $nogradestr); $select->nothingvalue = '-1'; $select->tabindex = $tabindices[$item->id]['grade']; $studentshtml .= $OUTPUT->select($select); } elseif(!empty($scale)) { $scales = explode(",", $scale->scale); // invalid grade if gradeval < 1 if ($gradeval < 1) { $studentshtml .= $OUTPUT->span('-', "gradevalue$hidden$gradepass"); } else { $gradeval = $grade->grade_item->bounded_grade($gradeval); //just in case somebody changes scale $studentshtml .= $OUTPUT->span($scales[$gradeval-1], "gradevalue$hidden$gradepass"); } } else { // no such scale, throw error? } } else if ($item->gradetype != GRADE_TYPE_TEXT) { // Value type if ($this->get_pref('quickgrading') and $grade->is_editable()) { $value = format_float($gradeval, $decimalpoints); $studentshtml .= ''; $studentshtml .= ''; } else { $studentshtml .= $OUTPUT->span(format_float($gradeval, $decimalpoints), "gradevalue$hidden$gradepass"); } } // If quickfeedback is on, print an input element if ($this->get_pref('showquickfeedback') and $grade->is_editable()) { $studentshtml .= ''; $studentshtml .= ''; } } else { // Not editing $gradedisplaytype = $item->get_displaytype(); // If feedback present, surround grade with feedback tooltip: Open span here if ($item->needsupdate) { $studentshtml .= $OUTPUT->span(get_string('error'), "gradingerror$hidden$gradepass"); } else { $studentshtml .= $OUTPUT->span(grade_format_gradevalue($gradeval, $item, true, $gradedisplaytype, null), "gradevalue$hidden$gradepass"); } } if (!empty($this->gradeserror[$item->id][$userid])) { $studentshtml .= $this->gradeserror[$item->id][$userid]; } $studentshtml .= '' . "\n"; } $studentshtml .= ''; } return $studentshtml; } function get_studentnameshtml() { global $CFG, $USER, $OUTPUT; $studentshtml = ''; $showuserimage = $this->get_pref('showuserimage'); $showuseridnumber = $this->get_pref('showuseridnumber'); $fixedstudents = $this->is_fixed_students(); $strsortasc = $this->get_lang_string('sortasc', 'grades'); $strsortdesc = $this->get_lang_string('sortdesc', 'grades'); $strfirstname = $this->get_lang_string('firstname'); $strlastname = $this->get_lang_string('lastname'); if ($this->sortitemid === 'lastname') { if ($this->sortorder == 'ASC') { $lastarrow = print_arrow('up', $strsortasc, true); } else { $lastarrow = print_arrow('down', $strsortdesc, true); } } else { $lastarrow = ''; } if ($this->sortitemid === 'firstname') { if ($this->sortorder == 'ASC') { $firstarrow = print_arrow('up', $strsortasc, true); } else { $firstarrow = print_arrow('down', $strsortdesc, true); } } else { $firstarrow = ''; } if ($fixedstudents) { $fixedcolumntable = new html_table(); $fixedcolumntable->id = 'fixed_column'; $fixedcolumntable->add_class('fixed_grades_column'); $fixedcolumntable->bodyclasses = 'leftbody'; $colspan = null; if ($showuseridnumber) { $colspan = 2; } $levels = count($this->gtree->levels) - 1; for ($i = 0; $i < $levels; $i++) { $fillercell = new html_table_cell(); $fillercell->add_classes(array('fixedcolumn', 'cell', 'c0', 'topleft')); $fillercell->text = ' '; $fillercell->colspan = $colspan; $fixedcolumntable->data[] = html_table_row::make(array($fillercell)); } $sortfirstname = html_link::make(clone($this->baseurl), $strfirstname); $sortfirstname->url->param('sortitemid', 'firstname'); $sortlastname = html_link::make(clone($this->baseurl), $strlastname); $sortlastname->url->param('sortitemid', 'lastname'); $headerrow = new html_table_row(); $headerrow->add_class('heading'); $studentheader = new html_table_cell(); $studentheader->add_classes(array('header', 'c0')); $studentheader->scope = 'col'; $studentheader->header = true; $studentheader->id = 'studentheader'; $studentheader->text = $OUTPUT->link($sortfirstname) . $firstarrow. '/ ' . $OUTPUT->link($sortlastname) . $lastarrow; $headerrow->cells[] = $studentheader; if ($showuseridnumber) { $sortidnumberlink = html_link::make(clone($this->baseurl), get_string('idnumber')); $sortidnumberlink->url->param('sortitemid', 'idnumber'); $idnumberheader = new html_table_cell(); $idnumberheader->add_classes(array('header', 'c0', 'useridnumber')); $idnumberheader->scope = 'col'; $idnumberheader->header = true; $idnumberheader->text = $OUTPUT->link($sortidnumberlink); if ('idnumber' == $this->sortitemid) { if ($this->sortorder == 'ASC') { $idnumberheader->text .= print_arrow('up', $strsortasc, true); } else { $idnumberheader->text .= print_arrow('down', $strsortdesc, true); } } $headerrow->cells[] = $idnumberheader; } $fixedcolumntable->data[] = $headerrow; if ($USER->gradeediting[$this->courseid]) { $controlsrow = new html_table_row(); $controlsrow->add_class('controls'); $controlscell = new html_table_cell(); $controlscell->add_classes(array('header', 'c0', 'controls')); $controlscell->colspan = $colspan; $controlscell->text = $this->get_lang_string('controls','grades'); $controlsrow->cells[] = $controlscell; $fixedcolumntable->data[] = $controlsrow; } $rowclasses = array('even', 'odd'); foreach ($this->users as $userid => $user) { $userrow = new html_table_row(); $userrow->add_classes(array('r'.$this->rowcount++, $rowclasses[$this->rowcount % 2])); $usercell = new html_table_cell(); $usercell->add_classes(array('c0', 'user')); $usercell->header = true; $usercell->scope = 'row'; $usercell->add_action('click', 'yui_set_row'); if ($showuserimage) { $usercell->text = $OUTPUT->container($OUTPUT->user_picture(moodle_user_picture::make($user, $this->courseid)), 'userpic'); } $usercell->text .= $OUTPUT->link(html_link::make(new moodle_url($CFG->wwwroot.'/user/view.php', array('id' => $user->id, 'course' => $this->course->id)), fullname($user))); $userrow->cells[] = $usercell; if ($showuseridnumber) { $idnumbercell = new html_table_cell(); $idnumbercell->add_classes(array('header', 'c0', 'useridnumber')); $idnumbercell->header = true; $idnumbercell->scope = 'row'; $idnumbercell->add_action('click', 'yui_set_row'); $userrow->cells[] = $idnumbercell; } $fixedcolumntable->data[] = $userrow; } if ($this->get_pref('showranges')) { $rangerow = new html_table_row(); $rangerow->add_classes(array('range', 'r'.$this->rowcount++)); $rangecell = new html_table_cell(); $rangecell->add_classes(array('header', 'c0', 'range')); $rangecell->colspan = $colspan; $rangecell->header = true; $rangecell->scope = 'row'; $rangecell->text = $this->get_lang_string('range','grades'); $rangerow->cells[] = $rangecell; $fixedcolumntable->data[] = $rangerow; } // Averages heading $straveragegroup = get_string('groupavg', 'grades'); $straverage = get_string('overallaverage', 'grades'); $showaverages = $this->get_pref('showaverages'); $showaveragesgroup = $this->currentgroup && $showaverages; if ($showaveragesgroup) { $groupavgrow = new html_table_row(); $groupavgrow->add_classes(array('groupavg', 'r'.$this->rowcount++)); $groupavgcell = new html_table_cell(); $groupavgcell->add_classes(array('header', 'c0', 'range')); $groupavgcell->colspan = $colspan; $groupavgcell->header = true; $groupavgcell->scope = 'row'; $groupavgcell->text = $straveragegroup; $groupavgrow->cells[] = $groupavgcell; $fixedcolumntable->data[] = $groupavgrow; } if ($showaverages) { $avgrow = new html_table_row(); $avgrow->add_classes(array('avg', 'r'.$this->rowcount++)); $avgcell = new html_table_cell(); $avgcell->add_classes(array('header', 'c0', 'range')); $avgcell->colspan = $colspan; $avgcell->header = true; $avgcell->scope = 'row'; $avgcell->text = $straverage; $avgrow->cells[] = $avgcell; $fixedcolumntable->data[] = $avgrow; } $studentshtml .= $OUTPUT->container($OUTPUT->table($fixedcolumntable), 'left_scroller'); $studentshtml .= '
'; } else { $studentshtml .= '
'; } return $studentshtml; } /** * Closes all open elements */ public function get_endhtml() { global $CFG, $USER; $fixedstudents = $this->is_fixed_students(); if ($fixedstudents) { return "
"; } else { return ""; } } /** * Builds and return the HTML row of column totals. * @param bool $grouponly Whether to return only group averages or all averages. * @return string HTML */ public function get_avghtml($grouponly=false) { global $CFG, $USER, $DB; if (!$this->canviewhidden) { // totals might be affected by hiding, if user can not see hidden grades the aggregations might be altered // better not show them at all if user can not see all hideen grades return; } $averagesdisplaytype = $this->get_pref('averagesdisplaytype'); $averagesdecimalpoints = $this->get_pref('averagesdecimalpoints'); $meanselection = $this->get_pref('meanselection'); $shownumberofgrades = $this->get_pref('shownumberofgrades'); $avghtml = ''; $avgcssclass = 'avg'; if ($grouponly) { $straverage = get_string('groupavg', 'grades'); $showaverages = $this->currentgroup && $this->get_pref('showaverages'); $groupsql = $this->groupsql; $groupwheresql = $this->groupwheresql; $groupwheresqlparams = $this->groupwheresql_params; $avgcssclass = 'groupavg'; } else { $straverage = get_string('overallaverage', 'grades'); $showaverages = $this->get_pref('showaverages'); $groupsql = ""; $groupwheresql = ""; $groupwheresqlparams = array(); } if ($shownumberofgrades) { $straverage .= ' (' . get_string('submissions', 'grades') . ') '; } $totalcount = $this->get_numusers($grouponly); list($usql, $rolesparams) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0'); if ($showaverages) { $params = array_merge(array('courseid'=>$this->courseid), $rolesparams, $groupwheresqlparams); // find sums of all grade items in course $SQL = "SELECT g.itemid, SUM(g.finalgrade) AS sum FROM {grade_items} gi JOIN {grade_grades} g ON g.itemid = gi.id JOIN {user} u ON u.id = g.userid JOIN {role_assignments} ra ON ra.userid = u.id $groupsql WHERE gi.courseid = :courseid AND ra.roleid $usql AND ra.contextid ".get_related_contexts_string($this->context)." AND g.finalgrade IS NOT NULL $groupwheresql GROUP BY g.itemid"; $sumarray = array(); if ($sums = $DB->get_records_sql($SQL, $params)) { foreach ($sums as $itemid => $csum) { $sumarray[$itemid] = $csum->sum; } } $columncount=0; $avghtml = ''; // MDL-10875 Empty grades must be evaluated as grademin, NOT always 0 // This query returns a count of ungraded grades (NULL finalgrade OR no matching record in grade_grades table) $params = array_merge(array('courseid'=>$this->courseid), $rolesparams, $groupwheresqlparams); $SQL = "SELECT gi.id, COUNT(u.id) AS count FROM {grade_items} gi CROSS JOIN {user} u JOIN {role_assignments} ra ON ra.userid = u.id LEFT OUTER JOIN {grade_grades} g ON (g.itemid = gi.id AND g.userid = u.id AND g.finalgrade IS NOT NULL) $groupsql WHERE gi.courseid = :courseid AND ra.roleid $usql AND ra.contextid ".get_related_contexts_string($this->context)." AND g.id IS NULL $groupwheresql GROUP BY gi.id"; $ungradedcounts = $DB->get_records_sql($SQL, $params); $fixedstudents = $this->is_fixed_students(); if (!$fixedstudents) { $colspan=''; if ($this->get_pref('showuseridnumber')) { $colspan = 'colspan="2" '; } $avghtml .= ''.$straverage.''; } foreach ($this->gtree->items as $itemid=>$unused) { $item =& $this->gtree->items[$itemid]; if ($item->needsupdate) { $avghtml .= ''.get_string('error').''; continue; } if (!isset($sumarray[$item->id])) { $sumarray[$item->id] = 0; } if (empty($ungradedcounts[$itemid])) { $ungradedcount = 0; } else { $ungradedcount = $ungradedcounts[$itemid]->count; } if ($meanselection == GRADE_REPORT_MEAN_GRADED) { $meancount = $totalcount - $ungradedcount; } else { // Bump up the sum by the number of ungraded items * grademin $sumarray[$item->id] += $ungradedcount * $item->grademin; $meancount = $totalcount; } $decimalpoints = $item->get_decimals(); // Determine which display type to use for this average if ($USER->gradeediting[$this->courseid]) { $displaytype = GRADE_DISPLAY_TYPE_REAL; } else if ($averagesdisplaytype == GRADE_REPORT_PREFERENCE_INHERIT) { // no ==0 here, please resave the report and user preferences $displaytype = $item->get_displaytype(); } else { $displaytype = $averagesdisplaytype; } // Override grade_item setting if a display preference (not inherit) was set for the averages if ($averagesdecimalpoints == GRADE_REPORT_PREFERENCE_INHERIT) { $decimalpoints = $item->get_decimals(); } else { $decimalpoints = $averagesdecimalpoints; } if (!isset($sumarray[$item->id]) || $meancount == 0) { $avghtml .= '-'; } else { $sum = $sumarray[$item->id]; $avgradeval = $sum/$meancount; $gradehtml = grade_format_gradevalue($avgradeval, $item, true, $displaytype, $decimalpoints); $numberofgrades = ''; if ($shownumberofgrades) { $numberofgrades = " ($meancount)"; } $avghtml .= ''.$gradehtml.$numberofgrades.''; } } $avghtml .= ''; } return $avghtml; } /** * Builds and return the HTML row of ranges for each column (i.e. range). * @return string HTML */ public function get_rangehtml() { global $CFG, $USER; $rangehtml = ''; if ($this->get_pref('showranges')) { $rangesdisplaytype = $this->get_pref('rangesdisplaytype'); $rangesdecimalpoints = $this->get_pref('rangesdecimalpoints'); $columncount=0; $rangehtml = ''; $fixedstudents = $this->is_fixed_students(); if (!$fixedstudents) { $colspan=''; if ($this->get_pref('showuseridnumber')) { $colspan = 'colspan="2" '; } $rangehtml .= ''.$this->get_lang_string('range','grades').''; } foreach ($this->gtree->items as $itemid=>$unused) { $item =& $this->gtree->items[$itemid]; $hidden = ''; if ($item->is_hidden()) { $hidden = ' hidden '; } $formattedrange = $item->get_formatted_range($rangesdisplaytype, $rangesdecimalpoints); $rangehtml .= ''. $formattedrange .''; } $rangehtml .= ''; } return $rangehtml; } /** * Builds and return the HTML row of ranges for each column (i.e. range). * @return string HTML */ public function get_iconshtml() { global $USER, $CFG; $iconshtml = ''; if ($USER->gradeediting[$this->courseid]) { $iconshtml = ''; $fixedstudents = $this->is_fixed_students(); $showuseridnumber = $this->get_pref('showuseridnumber'); $colspan = ''; if ($showuseridnumber) { $colspan = 'colspan="2"'; } if (!$fixedstudents) { $iconshtml .= ''.$this->get_lang_string('controls','grades').''; } $columncount = 0; foreach ($this->gtree->items as $itemid=>$unused) { // emulate grade element $item =& $this->gtree->get_item($itemid); $eid = $this->gtree->get_item_eid($item); $element = $this->gtree->locate_element($eid); $iconshtml .= '' . $this->get_icons($element) . ''; } $iconshtml .= ''; } return $iconshtml; } /** * Given a grade_category, grade_item or grade_grade, this function * figures out the state of the object and builds then returns a div * with the icons needed for the grader report. * * @param object $object * @return string HTML */ protected function get_icons($element) { global $CFG, $USER, $OUTPUT; if (!$USER->gradeediting[$this->courseid]) { return '
'; } // Init all icons $editicon = ''; if ($element['type'] != 'categoryitem' && $element['type'] != 'courseitem') { $editicon = $this->gtree->get_edit_icon($element, $this->gpr); } $editcalculationicon = ''; $showhideicon = ''; $lockunlockicon = ''; if (has_capability('moodle/grade:manage', $this->context)) { if ($this->get_pref('showcalculations')) { $editcalculationicon = $this->gtree->get_calculation_icon($element, $this->gpr); } if ($this->get_pref('showeyecons')) { $showhideicon = $this->gtree->get_hiding_icon($element, $this->gpr); } if ($this->get_pref('showlocks')) { $lockunlockicon = $this->gtree->get_locking_icon($element, $this->gpr); } } return $OUTPUT->container($editicon.$editcalculationicon.$showhideicon.$lockunlockicon, 'grade_icons'); } /** * Given a category element returns collapsing +/- icon if available * @param object $object * @return string HTML */ protected function get_collapsing_icon($element) { global $OUTPUT; $icon = ''; // If object is a category, display expand/contract icon if ($element['type'] == 'category') { // Load language strings $strswitchminus = $this->get_lang_string('aggregatesonly', 'grades'); $strswitchplus = $this->get_lang_string('gradesonly', 'grades'); $strswitchwhole = $this->get_lang_string('fullmode', 'grades'); $contractexpandicon = new moodle_action_icon(); $contractexpandicon->link->url = new moodle_url($this->gpr->get_return_url(null, array('target'=>$element['eid'], 'action'=>'switch_minus', 'sesskey'=>sesskey()))); $contractexpandicon->image->add_class('iconsmall'); $contractexpandicon->image->src = $OUTPUT->old_icon_url('t/switch_minus'); $contractexpandicon->image->alt = $strswitchminus; if (in_array($element['object']->id, $this->collapsed['aggregatesonly'])) { $contractexpandicon->image->src = $OUTPUT->old_icon_url('t/switch_plus'); $contractexpandicon->image->alt = $strswitchplus; $contractexpandicon->link->url->param('action', 'switch_plus'); } elseif (in_array($element['object']->id, $this->collapsed['gradesonly'])) { $contractexpandicon->image->src = $OUTPUT->old_icon_url('t/switch_whole'); $contractexpandicon->image->alt = $strswitchwhole; $contractexpandicon->link->url->param('action', 'switch_whole'); } $icon = $OUTPUT->action_icon($contractexpandicon); } return $icon; } /** * Processes a single action against a category, grade_item or grade. * @param string $target eid ({type}{id}, e.g. c4 for category4) * @param string $action Which action to take (edit, delete etc...) * @return */ public function process_action($target, $action) { // TODO: this code should be in some grade_tree static method $targettype = substr($target, 0, 1); $targetid = substr($target, 1); // TODO: end if ($collapsed = get_user_preferences('grade_report_grader_collapsed_categories')) { $collapsed = unserialize($collapsed); } else { $collapsed = array('aggregatesonly' => array(), 'gradesonly' => array()); } switch ($action) { case 'switch_minus': // Add category to array of aggregatesonly if (!in_array($targetid, $collapsed['aggregatesonly'])) { $collapsed['aggregatesonly'][] = $targetid; set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed)); } break; case 'switch_plus': // Remove category from array of aggregatesonly, and add it to array of gradesonly $key = array_search($targetid, $collapsed['aggregatesonly']); if ($key !== false) { unset($collapsed['aggregatesonly'][$key]); } if (!in_array($targetid, $collapsed['gradesonly'])) { $collapsed['gradesonly'][] = $targetid; } set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed)); break; case 'switch_whole': // Remove the category from the array of collapsed cats $key = array_search($targetid, $collapsed['gradesonly']); if ($key !== false) { unset($collapsed['gradesonly'][$key]); set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed)); } break; default: break; } return true; } /** * Returns whether or not to display fixed students column. * Includes a browser check, because IE6 doesn't support the scrollbar. * * @return bool */ public function is_fixed_students() { global $USER, $CFG; return empty($USER->screenreader) && $CFG->grade_report_fixedstudents && (check_browser_version('MSIE', '7.0') || check_browser_version('Firefox', '2.0') || check_browser_version('Gecko', '2006010100') || check_browser_version('Camino', '1.0') || check_browser_version('Opera', '6.0') || check_browser_version('Safari', '2.0')); } } ?>