Thursday, December 6, 2012

Changing the gradebook item order using a script

I meet with my customer the other day and she said something like "oh my Gosh, the gradebook items are sometimes all out of order - what is that about?"  With our vendor courses, that have a lot of gradable items, the gradebook has been a real focus this fall.  An average course  has 40 gradable items in it.  Most of those items are quizzes, assignments and forums.  They are the big three.  A lot of time is spend by teachers and by my customers looking in the gradebook.  This is the first time I heard anything about the order of the items in the gradebook being wacky.  What that really meant was

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