Skip to content

Commit

Permalink
MDL-10386 More refactoring (grade_report_grader->get_user_pref($pref_…
Browse files Browse the repository at this point in the history
…name))
  • Loading branch information
nicolasconnault committed Jul 12, 2007
1 parent 9d452f1 commit e5161d0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 97 deletions.
143 changes: 52 additions & 91 deletions grade/report/grader/grader_report.php
Expand Up @@ -58,55 +58,11 @@ class grade_report_grader {
*/
var $gradeserror = array();

//// USER PREFERENCES

/**
* Number of users on a page.
* @var int $perpage
*/
var $studentsperpage;

/**
* 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
* User preferences related to this report.
* @var array $user_prefs
*/
var $quickgrading;

/**
* Whether or not to use quickfeedback.
* @var bool $quickfeedback
*/
var $quickfeedback;
var $user_prefs = array();

//// SQL-RELATED

Expand Down Expand Up @@ -195,39 +151,40 @@ function grade_report_grader($courseid, $context, $page=null, $sortitemid=null)
// roles to be displayed in the gradebook
$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
$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
$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_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.
* Also updates the object's corresponding variable.
Expand All @@ -236,9 +193,9 @@ function grade_report_grader($courseid, $context, $page=null, $sortitemid=null)
* @return bool Success or failure.
* TODO print visual feedback
*/
function set_user_pref($pref_name, $pref_value) {
if ($result = set_user_preferences(array($pref_name => $pref_value))) {
$this->$pref_name = $pref_value;
function set_user_pref($pref, $pref_value) {
if ($result = set_user_preferences(array($pref => $pref_value))) {
$this->$pref = $pref_value;
}
return $result;
}
Expand Down Expand Up @@ -487,13 +444,15 @@ function load_users() {
$this->groupwheresql
AND ra.contextid ".get_related_contexts_string($this->context)."
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 {
// default sort
// get users sorted by lastname
$this->users = get_role_users(@implode(',', $CFG->gradebookroles), $this->context, false,
'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

}
Expand Down Expand Up @@ -794,7 +753,7 @@ function get_studentshtml() {
$scaleopt[$i] = $scaleoption;
}

if ($this->quickgrading) {
if ($this->get_user_pref('quickgrading')) {
$studentshtml .= choose_from_menu($scaleopt, 'grade_'.$userid.'_'.$item->id,
$gradeval, get_string('nograde'), '', -1, true);
} elseif ($scale = get_record('scale', 'id', $item->scaleid)) {
Expand All @@ -811,7 +770,7 @@ function get_studentshtml() {
}
}
} else {
if ($this->quickgrading) {
if ($this->get_user_pref('quickgrading')) {
$studentshtml .= '<input size="6" type="text" name="grade_'.$userid.'_'
.$item->id.'" value="'.get_grade_clean($gradeval).'"/>';
} else {
Expand All @@ -821,8 +780,8 @@ function get_studentshtml() {


// If quickfeedback is on, print an input element
if ($this->quickfeedback) {
if ($this->quickgrading) {
if ($this->get_user_pref('quickfeedback')) {
if ($this->get_user_pref('quickgrading')) {
$studentshtml .= '<br />';
}
$studentshtml .= '<input size="6" type="text" name="feedback_'.$userid.'_'.$item->id.'" value="'
Expand Down Expand Up @@ -884,7 +843,7 @@ function get_groupsumhtml() {

$groupsumhtml = '';

if ($this->currentgroup && $this->showgroups) {
if ($this->currentgroup && $this->get_user_pref('showgroups')) {

/** SQL for finding group sum */
$SQL = "SELECT g.itemid, SUM(g.finalgrade) as sum
Expand Down Expand Up @@ -919,11 +878,15 @@ function get_groupsumhtml() {
return $groupsumhtml;
}

/**
* Builds and return the HTML row of column totals.
* @return string HTML
*/
function get_gradesumhtml() {
global $CFG;

$gradesumhtml = '';
if ($this->showgrandtotals) {
if ($this->get_user_pref('showgrandtotals')) {

/** SQL for finding the SUM grades of all visible users ($CFG->gradebookroles) */

Expand Down Expand Up @@ -957,9 +920,13 @@ function get_gradesumhtml() {
return $gradesumhtml;
}

/**
* Builds and return the HTML row of scales for each column (i.e. range).
* @return string HTML
*/
function get_scalehtml() {
$scalehtml = '';
if ($this->showscales) {
if ($this->get_user_pref('showscales')) {
$scalehtml = '<tr><td>'.get_string('range','grades').'</td>';
foreach ($this->items as $item) {
$scalehtml .= '<td>'. get_grade_clean($item->grademin).'-'. get_grade_clean($item->grademax).'</td>';
Expand Down Expand Up @@ -1014,12 +981,6 @@ function get_icons($element, $icons=null, $limit=true) {
$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
$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="'
Expand Down Expand Up @@ -1111,20 +1072,20 @@ function get_icons($element, $icons=null, $limit=true) {
}

// Calculation icon for items and categories
if ($showcalculations && $type != 'grade') {
if ($this->get_user_pref('showcalculations') && $type != 'grade') {
$html .= $edit_calculation_icon;
}

if ($showeyecons) {
if ($this->get_user_pref('showeyecons')) {
$html .= $show_hide_icon;
}

if ($showlocks) {
if ($this->get_user_pref('showlocks')) {
$html .= $lock_unlock_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;
}
} else { // Editing mode is off
Expand Down
12 changes: 6 additions & 6 deletions grade/report/grader/index.php
Expand Up @@ -14,7 +14,7 @@
$target = optional_param('target', 0, PARAM_ALPHANUM);
$toggle = optional_param('toggle', NULL, PARAM_INT);
$toggle_type = optional_param('toggle_type', 0, PARAM_ALPHANUM);
$db->debug=true;

// Handle toggle change request
if (!is_null($toggle) && !empty($toggle_type)) {
set_user_preferences(array('grade_report_show' . $toggle_type => $toggle));
Expand All @@ -30,7 +30,7 @@

// Override perpage if set in URL
if ($perpageurl = optional_param('perpage', 0, PARAM_INT)) {
$report->studentsperpage = $perpageurl;
$report->user_prefs['studentsperpage'] = $perpageurl;
}

// Perform actions on categories, items and grades
Expand Down Expand Up @@ -58,7 +58,7 @@

echo $report->group_selector;
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 />';

$reporthtml = '<table class="boxaligncenter">';
Expand All @@ -80,13 +80,13 @@
echo $reporthtml;

// 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></form>';
}

// prints paging bar at bottom for large pages
if ($report->studentsperpage >= 20) {
print_paging_bar($numusers, $report->page, $report->studentsperpage, $report->pbarurl);
if ($report->get_user_pref('studentsperpage') >= 20) {
print_paging_bar($numusers, $report->page, $report->get_user_pref('studentsperpage'), $report->pbarurl);
}
?>

0 comments on commit e5161d0

Please sign in to comment.