MDL-13454 user iterator fixed; merged from MOODLE_19_STABLE

This commit is contained in:
skodak 2008-02-17 14:49:40 +00:00
parent e13f9c1008
commit 345674caa0
2 changed files with 63 additions and 47 deletions

View File

@ -95,29 +95,41 @@ class graded_users_iterator {
$groupwheresql = ""; $groupwheresql = "";
} }
$users_sql = "SELECT u.* 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->sortfield1 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";
}
}
$users_sql = "SELECT u.* $ofields
FROM {$CFG->prefix}user u FROM {$CFG->prefix}user u
INNER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid INNER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
$groupsql $groupsql
WHERE ra.roleid $gradebookroles WHERE ra.roleid $gradebookroles
AND ra.contextid $relatedcontexts AND ra.contextid $relatedcontexts
$groupwheresql"; $groupwheresql
ORDER BY $order";
// If only sortfield2 is given, it will be ignored
if (!empty($this->sortfield1)) {
$users_sql .= "ORDER BY u.$this->sortfield1 $this->sortorder1";
if (!empty($this->sortfield2)) {
$users_sql .= ", $this->sortfield2 $this->sortorder2";
}
}
$this->users_rs = get_recordset_sql($users_sql); $this->users_rs = get_recordset_sql($users_sql);
if (!empty($this->grade_items)) { if (!empty($this->grade_items)) {
$itemids = array_keys($this->grade_items); $itemids = array_keys($this->grade_items);
$itemids = implode(',', $itemids); $itemids = implode(',', $itemids);
$grades_sql = "SELECT g.* $grades_sql = "SELECT g.* $ofields
FROM {$CFG->prefix}grade_grades g FROM {$CFG->prefix}grade_grades g
INNER JOIN {$CFG->prefix}user u ON g.userid = u.id INNER JOIN {$CFG->prefix}user u ON g.userid = u.id
INNER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid INNER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
@ -126,9 +138,12 @@ class graded_users_iterator {
AND ra.contextid $relatedcontexts AND ra.contextid $relatedcontexts
AND g.itemid IN ($itemids) AND g.itemid IN ($itemids)
$groupwheresql $groupwheresql
ORDER BY g.userid ASC, g.itemid ASC"; ORDER BY $order, g.itemid ASC";
$this->grades_rs = get_recordset_sql($grades_sql); $this->grades_rs = get_recordset_sql($grades_sql);
} else {
$this->grades_rs = false;
} }
return true; return true;
} }
@ -142,22 +157,22 @@ class graded_users_iterator {
} }
if (!$user = rs_fetch_next_record($this->users_rs)) { if (!$user = rs_fetch_next_record($this->users_rs)) {
if ($current = $this->_pop()) {
// this is not good - user or grades updated between the two reads above :-(
}
return false; // no more users return false; // no more users
} }
//find the first grade of this user // find grades of this user
$grade_records = array(); $grade_records = array();
while (true) { while (true) {
if (!$current = $this->_pop()) { if (!$current = $this->_pop()) {
break; // no more grades break; // no more grades
} }
if ($current->userid < $user->id) { if ($current->userid != $user->id) {
// this should not happen, could be caused by concurrent updates - skip this record // grade of the next user, we have all for this user
continue;
} else if ($current->userid > $user->id) {
// this user does not have any more grades
$this->_push($current); $this->_push($current);
break; break;
} }
@ -169,20 +184,20 @@ class graded_users_iterator {
$feedbacks = array(); $feedbacks = array();
if (!empty($this->grade_items)) { if (!empty($this->grade_items)) {
foreach ($this->grade_items as $grade_item) { foreach ($this->grade_items as $grade_item) {
if (array_key_exists($grade_item->id, $grade_records)) { if (array_key_exists($grade_item->id, $grade_records)) {
$feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback; $feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback;
$feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat; $feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat;
unset($grade_records[$grade_item->id]->feedback); unset($grade_records[$grade_item->id]->feedback);
unset($grade_records[$grade_item->id]->feedbackformat); unset($grade_records[$grade_item->id]->feedbackformat);
$grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false); $grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false);
} else { } else {
$feedbacks[$grade_item->id]->feedback = ''; $feedbacks[$grade_item->id]->feedback = '';
$feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE; $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] = new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false);
}
} }
} }
}
$result = new object(); $result = new object();
$result->user = $user; $result->user = $user;
@ -246,7 +261,7 @@ class graded_users_iterator {
*/ */
function print_graded_users_selector($course, $actionpage, $userid=null, $return=false) { function print_graded_users_selector($course, $actionpage, $userid=null, $return=false) {
global $CFG, $USER; global $CFG, $USER;
if (is_null($userid)) { if (is_null($userid)) {
$userid = $USER->id; $userid = $USER->id;
} }
@ -257,11 +272,11 @@ function print_graded_users_selector($course, $actionpage, $userid=null, $return
$gui = new graded_users_iterator($course); $gui = new graded_users_iterator($course);
$gui->init(); $gui->init();
if ($userid !== 0) { if ($userid !== 0) {
$menu[0] = get_string('allusers', 'grades'); $menu[0] = get_string('allusers', 'grades');
} }
while ($userdata = $gui->next_user()) { while ($userdata = $gui->next_user()) {
$user = $userdata->user; $user = $userdata->user;
$menu[$user->id] = fullname($user); $menu[$user->id] = fullname($user);
@ -273,8 +288,8 @@ function print_graded_users_selector($course, $actionpage, $userid=null, $return
$menu[0] .= " (" . (count($menu) - 1) . ")"; $menu[0] .= " (" . (count($menu) - 1) . ")";
} }
return popup_form($CFG->wwwroot.'/grade/' . $actionpage . '&amp;userid=', $menu, 'choosegradeduser', $userid, 'choose', '', '', return popup_form($CFG->wwwroot.'/grade/' . $actionpage . '&amp;userid=', $menu, 'choosegradeduser', $userid, 'choose', '', '',
$return, 'self', get_string('selectalloroneuser', 'grades')); $return, 'self', get_string('selectalloroneuser', 'grades'));
} }
/** /**
@ -883,7 +898,7 @@ class grade_structure {
if (!method_exists($element['object'], 'get_name')) { if (!method_exists($element['object'], 'get_name')) {
return $strparams; return $strparams;
} }
$strparams->itemname = $element['object']->get_name(); $strparams->itemname = $element['object']->get_name();
// If element name is categorytotal, get the name of the parent category // If element name is categorytotal, get the name of the parent category
@ -897,7 +912,7 @@ class grade_structure {
$strparams->itemmodule = null; $strparams->itemmodule = null;
if (isset($element['object']->itemmodule)) { if (isset($element['object']->itemmodule)) {
$strparams->itemmodule = $element['object']->itemmodule; $strparams->itemmodule = $element['object']->itemmodule;
} }
return $strparams; return $strparams;
} }
@ -925,9 +940,9 @@ class grade_structure {
} }
$strparams = $this->get_params_for_iconstr($element); $strparams = $this->get_params_for_iconstr($element);
if ($element['type'] == 'item' or $element['type'] == 'category') { if ($element['type'] == 'item' or $element['type'] == 'category') {
} }
$object = $element['object']; $object = $element['object'];
$overlib = ''; $overlib = '';
@ -989,9 +1004,9 @@ class grade_structure {
return ''; return '';
} }
$strparams = $this->get_params_for_iconstr($element); $strparams = $this->get_params_for_iconstr($element);
$strshow = get_string('showverbose', 'grades', $strparams); $strshow = get_string('showverbose', 'grades', $strparams);
$strhide = get_string('hideverbose', 'grades', $strparams); $strhide = get_string('hideverbose', 'grades', $strparams);
if ($element['object']->is_hidden()) { if ($element['object']->is_hidden()) {
$icon = 'show'; $icon = 'show';
@ -1024,7 +1039,7 @@ class grade_structure {
function get_locking_icon($element, $gpr) { function get_locking_icon($element, $gpr) {
global $CFG; global $CFG;
$strparams = $this->get_params_for_iconstr($element); $strparams = $this->get_params_for_iconstr($element);
$strunlock = get_string('unlockverbose', 'grades', $strparams); $strunlock = get_string('unlockverbose', 'grades', $strparams);
$strlock = get_string('lockverbose', 'grades', $strparams); $strlock = get_string('lockverbose', 'grades', $strparams);
@ -1076,7 +1091,7 @@ class grade_structure {
if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') { if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
$strparams = $this->get_params_for_iconstr($element); $strparams = $this->get_params_for_iconstr($element);
$streditcalculation = get_string('editcalculationverbose', 'grades', $strparams); $streditcalculation = get_string('editcalculationverbose', 'grades', $strparams);
// show calculation icon only when calculation possible // show calculation icon only when calculation possible

View File

@ -317,10 +317,11 @@ class grade_report_grader extends grade_report {
// If lastname or firstname is given as sortitemid, add the other name (firstname or lastname respectively) as second sort param // If lastname or firstname is given as sortitemid, add the other name (firstname or lastname respectively) as second sort param
$sort2 = ''; $sort2 = '';
if ($this->sortitemid == 'lastname') { if ($this->sortitemid == 'lastname') {
$sort2 = ', u.firstname ' . $this->sortorder; $sort2 = ', u.firstname '.$this->sortorder;
} elseif ($this->sortitemid == 'firstname') { } elseif ($this->sortitemid == 'firstname') {
$sort2 = ', u.lastname ' . $this->sortorder; $sort2 = ', u.lastname '.$this->sortorder;
} }
$sort2 .= ', u.id ASC'; // make sure the order is the same in case the sort item values are the same
$roles = explode(',', $this->gradebookroles); $roles = explode(',', $this->gradebookroles);
$this->users = get_role_users($roles, $this->context, false, $this->users = get_role_users($roles, $this->context, false,
'u.id, u.firstname, u.lastname, u.idnumber, u.imagealt, u.picture', 'u.'.$this->sortitemid .' '. $this->sortorder . $sort2, 'u.id, u.firstname, u.lastname, u.idnumber, u.imagealt, u.picture', 'u.'.$this->sortitemid .' '. $this->sortorder . $sort2,