the order of the items in the gradebook did not match the order or the items in the course page.
After the meeting, I opened the DB and looked at the grade_items table and saw the sortorder column. I checked a couple courses and verified the gradebook was using the column to order the items in the gradebook. My customer also noted that he had attempted to change the order via the moodle interface, but found it far to laborious a task, considering the number of items.After a bit of analysis on the problem, I put together a php page that grabbed all the courses that had enrollments in them (it could have been all the courses) - looped over the list, passed the course id into another function call, along with a counter value, and updated each records sortorder - achieving a consistency between the course order and the gradebook order.
This is the content of the .php file
/*
this util page will get all the courses in a moodle db that have enrollments
loop over the course
extract the courseid
get the gradeable items for the course
loop over the gradeable items
update the sortorder or the gradeable item
get the next courseid
end loop
*/
require_once('../config.php');
global $DB;
//var_dump($DB);
$courses = getCourses();
//var_dump($courses);
foreach($courses as $course){
$recs = getGradeItems($course->courseid); //get course gradeable items
$counter = 1;
foreach($recs as $row){
echo $row->id . ' '.$counter. '
';
$recObj = new stdClass();
$recObj->id = $row->id;
$recObj->sortorder = $counter;
$DB->update_record('grade_items', $recObj, $bulk=true);
$counter++;
}//end loop over course gradeable items
echo "
";
}//end loop over courses
/*********************** functions ************************/
function getCourses(){
global $DB;
return
$DB->get_records_sql('
SELECT distinct c.id AS courseid
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
');
}
function getGradeItems($courseid){
global $DB;
return
$DB->get_records_sql('
SELECT sortorder, id, itemname, itemtype, itemmodule, iteminstance
FROM mdl_grade_items
WHERE courseid = '.$courseid.' and itemtype = "mod"
ORDER BY id
');
}
Note the use of the global $DB class and the use of the update_record function. I try to use the moodle API as much as possible. The API requires the use of the stdClass object. I added to the $recObj, which is an instance of the stdClass, the itemid and the counter.
No comments:
Post a Comment