MDL-10386 More refactoring (grade_report_grader->get_user_pref($pref_name))

This commit is contained in:
nicolasconnault 2007-07-12 17:42:23 +00:00
parent 9d452f139f
commit e5161d0cdd
2 changed files with 58 additions and 97 deletions

View file

@ -58,55 +58,11 @@ class grade_report_grader {
*/ */
var $gradeserror = array(); var $gradeserror = array();
//// USER PREFERENCES
/** /**
* Number of users on a page. * User preferences related to this report.
* @var int $perpage * @var array $user_prefs
*/ */
var $studentsperpage; var $user_prefs = array();
/**
* Number of digits after the decimal point.
* @var int $decimalspoints
*/
var $decimalspoints;
/**
* Whether or not to display the grandtotals row.
* @var bool $showgrandtotals
*/
var $showgrandtotals;
/**
* Whether or not to display group selector, total row and other group-related elements.
* @var bool $showgroups
*/
var $showgroups;
/**
* The position of the Aggregation column in relation to the raw grade items.
* @var int $aggregation_position
*/
var $aggregation_position;
/**
* Whether or not to display a row of scales/ranges for each grade_item.
* @var bool $showscales
*/
var $showscales;
/**
* Whether or not to use quickgrading.
* @var bool $quickgrading
*/
var $quickgrading;
/**
* Whether or not to use quickfeedback.
* @var bool $quickfeedback
*/
var $quickfeedback;
//// SQL-RELATED //// SQL-RELATED
@ -195,39 +151,40 @@ class grade_report_grader {
// roles to be displayed in the gradebook // roles to be displayed in the gradebook
$this->gradebookroles = $CFG->gradebookroles; $this->gradebookroles = $CFG->gradebookroles;
// User preferences
$this->studentsperpage = get_user_preferences('grade_report_studentsperpage',
$CFG->grade_report_studentsperpage);
$this->decimalpoints = get_user_preferences('grade_report_decimalpoints',
$CFG->grade_report_decimalpoints);
$this->showgrandtotals = get_user_preferences('grade_report_showgrandtotals',
$CFG->grade_report_showgrandtotals);
$this->showgroups = get_user_preferences('grade_report_showgroups',
$CFG->grade_report_showgroups);
$this->aggregation_position = get_user_preferences('grade_report_aggregationposition',
$CFG->grade_report_aggregationposition);
$this->showscales = get_user_preferences('grade_report_showscales',
$CFG->grade_report_showscales);
$this->quickgrading = get_user_preferences('grade_report_quickgrading',
$CFG->grade_report_quickgrading);
$this->quickfeedback = get_user_preferences('grade_report_quickfeedback',
$CFG->grade_report_quickfeedback);
// Grab the grade_tree for this course // Grab the grade_tree for this course
$this->gtree = new grade_tree($this->courseid, true, false, $this->aggregation_position); $this->gtree = new grade_tree($this->courseid, true, false, $this->get_user_pref('aggregationposition'));
// base url for sorting by first/last name // base url for sorting by first/last name
$this->baseurl = 'report.php?id='.$this->courseid.'&perpage='.$this->studentsperpage.'&report=grader&page='.$this->page; $this->baseurl = 'report.php?id='.$this->courseid.'&perpage='.$this->get_user_pref('studentsperpage')
.'&report=grader&page='.$this->page;
// //
$this->pbarurl = 'report.php?id='.$this->courseid.'&perpage='.$this->studentsperpage.'&report=grader&'; $this->pbarurl = 'report.php?id='.$this->courseid.'&perpage='.$this->get_user_pref('studentsperpage')
.'&report=grader&';
if ($this->showgroups) { if ($this->get_user_pref('showgroups')) {
$this->setup_groups(); $this->setup_groups();
} }
$this->setup_sortitemid(); $this->setup_sortitemid();
} }
/**
* Given the name of a user preference (without grade_report_ prefix), locally saves then returns
* the value of that preference. If the preference has already been fetched before,
* the saved value is returned. If the preference is not set at the User level, the $CFG equivalent
* is given (site default).
* @param string $pref The name of the preference (do not include the grade_report_ prefix)
* @return mixed The value of the preference
*/
function get_user_pref($pref) {
global $CFG;
if (empty($this->user_prefs[$pref])) {
$fullprefname = 'grade_report_' . $pref;
$this->user_prefs[$pref] = get_user_preferences($fullprefname, $CFG->$fullprefname);
}
return $this->user_prefs[$pref];
}
/** /**
* Uses set_user_preferences() to update the value of a user preference. * Uses set_user_preferences() to update the value of a user preference.
* Also updates the object's corresponding variable. * Also updates the object's corresponding variable.
@ -236,9 +193,9 @@ class grade_report_grader {
* @return bool Success or failure. * @return bool Success or failure.
* TODO print visual feedback * TODO print visual feedback
*/ */
function set_user_pref($pref_name, $pref_value) { function set_user_pref($pref, $pref_value) {
if ($result = set_user_preferences(array($pref_name => $pref_value))) { if ($result = set_user_preferences(array($pref => $pref_value))) {
$this->$pref_name = $pref_value; $this->$pref = $pref_value;
} }
return $result; return $result;
} }
@ -487,13 +444,15 @@ class grade_report_grader {
$this->groupwheresql $this->groupwheresql
AND ra.contextid ".get_related_contexts_string($this->context)." AND ra.contextid ".get_related_contexts_string($this->context)."
ORDER BY g.finalgrade $this->sortorder"; ORDER BY g.finalgrade $this->sortorder";
$this->users = get_records_sql($sql, $this->studentsperpage * $this->page, $this->studentsperpage); $this->users = get_records_sql($sql, $this->get_user_pref('studentsperpage') * $this->page,
$this->get_user_pref('studentsperpage'));
} else { } else {
// default sort // default sort
// get users sorted by lastname // get users sorted by lastname
$this->users = get_role_users(@implode(',', $CFG->gradebookroles), $this->context, false, $this->users = get_role_users(@implode(',', $CFG->gradebookroles), $this->context, false,
'u.id, u.firstname, u.lastname', 'u.'.$this->sortitemid .' '. $this->sortorder, 'u.id, u.firstname, u.lastname', 'u.'.$this->sortitemid .' '. $this->sortorder,
false, $this->page * $this->studentsperpage, $this->studentsperpage, $this->currentgroup); false, $this->page * $this->get_user_pref('studentsperpage'), $this->get_user_pref('studentsperpage'),
$this->currentgroup);
// need to cut users down by groups // need to cut users down by groups
} }
@ -794,7 +753,7 @@ class grade_report_grader {
$scaleopt[$i] = $scaleoption; $scaleopt[$i] = $scaleoption;
} }
if ($this->quickgrading) { if ($this->get_user_pref('quickgrading')) {
$studentshtml .= choose_from_menu($scaleopt, 'grade_'.$userid.'_'.$item->id, $studentshtml .= choose_from_menu($scaleopt, 'grade_'.$userid.'_'.$item->id,
$gradeval, get_string('nograde'), '', -1, true); $gradeval, get_string('nograde'), '', -1, true);
} elseif ($scale = get_record('scale', 'id', $item->scaleid)) { } elseif ($scale = get_record('scale', 'id', $item->scaleid)) {
@ -811,7 +770,7 @@ class grade_report_grader {
} }
} }
} else { } else {
if ($this->quickgrading) { if ($this->get_user_pref('quickgrading')) {
$studentshtml .= '<input size="6" type="text" name="grade_'.$userid.'_' $studentshtml .= '<input size="6" type="text" name="grade_'.$userid.'_'
.$item->id.'" value="'.get_grade_clean($gradeval).'"/>'; .$item->id.'" value="'.get_grade_clean($gradeval).'"/>';
} else { } else {
@ -821,8 +780,8 @@ class grade_report_grader {
// If quickfeedback is on, print an input element // If quickfeedback is on, print an input element
if ($this->quickfeedback) { if ($this->get_user_pref('quickfeedback')) {
if ($this->quickgrading) { if ($this->get_user_pref('quickgrading')) {
$studentshtml .= '<br />'; $studentshtml .= '<br />';
} }
$studentshtml .= '<input size="6" type="text" name="feedback_'.$userid.'_'.$item->id.'" value="' $studentshtml .= '<input size="6" type="text" name="feedback_'.$userid.'_'.$item->id.'" value="'
@ -884,7 +843,7 @@ class grade_report_grader {
$groupsumhtml = ''; $groupsumhtml = '';
if ($this->currentgroup && $this->showgroups) { if ($this->currentgroup && $this->get_user_pref('showgroups')) {
/** SQL for finding group sum */ /** SQL for finding group sum */
$SQL = "SELECT g.itemid, SUM(g.finalgrade) as sum $SQL = "SELECT g.itemid, SUM(g.finalgrade) as sum
@ -919,11 +878,15 @@ class grade_report_grader {
return $groupsumhtml; return $groupsumhtml;
} }
/**
* Builds and return the HTML row of column totals.
* @return string HTML
*/
function get_gradesumhtml() { function get_gradesumhtml() {
global $CFG; global $CFG;
$gradesumhtml = ''; $gradesumhtml = '';
if ($this->showgrandtotals) { if ($this->get_user_pref('showgrandtotals')) {
/** SQL for finding the SUM grades of all visible users ($CFG->gradebookroles) */ /** SQL for finding the SUM grades of all visible users ($CFG->gradebookroles) */
@ -957,9 +920,13 @@ class grade_report_grader {
return $gradesumhtml; return $gradesumhtml;
} }
/**
* Builds and return the HTML row of scales for each column (i.e. range).
* @return string HTML
*/
function get_scalehtml() { function get_scalehtml() {
$scalehtml = ''; $scalehtml = '';
if ($this->showscales) { if ($this->get_user_pref('showscales')) {
$scalehtml = '<tr><td>'.get_string('range','grades').'</td>'; $scalehtml = '<tr><td>'.get_string('range','grades').'</td>';
foreach ($this->items as $item) { foreach ($this->items as $item) {
$scalehtml .= '<td>'. get_grade_clean($item->grademin).'-'. get_grade_clean($item->grademax).'</td>'; $scalehtml .= '<td>'. get_grade_clean($item->grademin).'-'. get_grade_clean($item->grademax).'</td>';
@ -1014,12 +981,6 @@ class grade_report_grader {
$object->feedback = ''; $object->feedback = '';
} }
// Load user preferences
$aggregationview = get_user_preferences('grade_report_aggregationview', $CFG->grade_report_aggregationview);
$showeyecons = get_user_preferences('grade_report_showeyecons', $CFG->grade_report_showeyecons);
$showlocks = get_user_preferences('grade_report_showlocks', $CFG->grade_report_showlocks);
$showcalculations = get_user_preferences('grade_report_showcalculations', $CFG->grade_report_showcalculations);
// Prepare image strings // Prepare image strings
$edit_category_icon = '<a href="report/grader/edit_category.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">' $edit_category_icon = '<a href="report/grader/edit_category.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">'
. '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="' . '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
@ -1111,20 +1072,20 @@ class grade_report_grader {
} }
// Calculation icon for items and categories // Calculation icon for items and categories
if ($showcalculations && $type != 'grade') { if ($this->get_user_pref('showcalculations') && $type != 'grade') {
$html .= $edit_calculation_icon; $html .= $edit_calculation_icon;
} }
if ($showeyecons) { if ($this->get_user_pref('showeyecons')) {
$html .= $show_hide_icon; $html .= $show_hide_icon;
} }
if ($showlocks) { if ($this->get_user_pref('showlocks')) {
$html .= $lock_unlock_icon; $html .= $lock_unlock_icon;
} }
// If object is a category, display expand/contract icon // If object is a category, display expand/contract icon
if (get_class($object) == 'grade_category' && $aggregationview == GRADER_REPORT_AGGREGATION_VIEW_COMPACT) { if (get_class($object) == 'grade_category' && $this->get_user_pref('aggregationview') == GRADER_REPORT_AGGREGATION_VIEW_COMPACT) {
$html .= $contract_expand_icon; $html .= $contract_expand_icon;
} }
} else { // Editing mode is off } else { // Editing mode is off

View file

@ -14,7 +14,7 @@ $type = optional_param('type', 0, PARAM_ALPHA);
$target = optional_param('target', 0, PARAM_ALPHANUM); $target = optional_param('target', 0, PARAM_ALPHANUM);
$toggle = optional_param('toggle', NULL, PARAM_INT); $toggle = optional_param('toggle', NULL, PARAM_INT);
$toggle_type = optional_param('toggle_type', 0, PARAM_ALPHANUM); $toggle_type = optional_param('toggle_type', 0, PARAM_ALPHANUM);
$db->debug=true;
// Handle toggle change request // Handle toggle change request
if (!is_null($toggle) && !empty($toggle_type)) { if (!is_null($toggle) && !empty($toggle_type)) {
set_user_preferences(array('grade_report_show' . $toggle_type => $toggle)); set_user_preferences(array('grade_report_show' . $toggle_type => $toggle));
@ -30,7 +30,7 @@ if ($data = data_submitted() and confirm_sesskey()) {
// Override perpage if set in URL // Override perpage if set in URL
if ($perpageurl = optional_param('perpage', 0, PARAM_INT)) { if ($perpageurl = optional_param('perpage', 0, PARAM_INT)) {
$report->studentsperpage = $perpageurl; $report->user_prefs['studentsperpage'] = $perpageurl;
} }
// Perform actions on categories, items and grades // Perform actions on categories, items and grades
@ -58,7 +58,7 @@ include('tabs.php');
echo $report->group_selector; echo $report->group_selector;
echo $report->get_toggles_html(); echo $report->get_toggles_html();
print_paging_bar($numusers, $report->page, $report->studentsperpage, $report->pbarurl); print_paging_bar($numusers, $report->page, $report->get_user_pref('studentsperpage'), $report->pbarurl);
echo '<br />'; echo '<br />';
$reporthtml = '<table class="boxaligncenter">'; $reporthtml = '<table class="boxaligncenter">';
@ -80,13 +80,13 @@ if ($USER->gradeediting) {
echo $reporthtml; echo $reporthtml;
// print submit button // print submit button
if ($USER->gradeediting && ($report->quickfeedback || $report->quickgrading)) { if ($USER->gradeediting && ($report->get_user_pref('quickfeedback') || $report->get_user_pref('quickgrading'))) {
echo '<div class="submit"><input type="submit" value="'.get_string('update').'" /></div>'; echo '<div class="submit"><input type="submit" value="'.get_string('update').'" /></div>';
echo '</div></form>'; echo '</div></form>';
} }
// prints paging bar at bottom for large pages // prints paging bar at bottom for large pages
if ($report->studentsperpage >= 20) { if ($report->get_user_pref('studentsperpage') >= 20) {
print_paging_bar($numusers, $report->page, $report->studentsperpage, $report->pbarurl); print_paging_bar($numusers, $report->page, $report->get_user_pref('studentsperpage'), $report->pbarurl);
} }
?> ?>