This is my gradebook report
This is my admin report, a bit more data...
For each report, I created a dbfunctions.php page, where I am using the moodle DB API to make calls to the DB. In other words, the best of both worlds, control of my functions while using the DB API.
Each plugin had its own version of the dbfunctions.php file.
grade/report/progress/dbfunctions.php - for the grade report
report/studentprogress/dbfunctions.php - for the admin report
Today, I merged the two dbfunctions.php files, removing the reduncency and moving the streamlined new file into the local folder with a new longer name.
local/progressReportsDBfunctions.php.
In each of the plugins index.php page, I changed the require_once statement
From:
require_once('dbfunctions.php');
To:
require_once($CFG->dirroot.'/local/progressReportsDBfunctions.php');
Now, when I get future requests for changed functionality, I can update in one place and ensure consistent results.
This is an example of one of the functions that is doing a bit of work determining how much time a student has been on-line over the past week and the life of the enrollment that was living in two different files.
function weeklyTimeInSeat($userid,$courseid,$what=0){
global $DB;
if($what == 1){ // give me all the recs for the student in the course
$whereclause = 'WHERE userid = '.$userid.' AND course = '.$courseid.' ';
}else{ // just give me recs from past week
$whereclause = 'WHERE DATE(from_unixtime(time,"%Y-%m-%d")) > CURDATE() - INTERVAL 7 Day
AND userid = '.$userid.' AND course = '.$courseid.' ';
}
$recs=
$DB->get_records_sql('
SELECT id, FROM_UNIXTIME(time,"%m/%d/%y") as fdate, FROM_UNIXTIME(time,"%H:%i") as ftime
FROM mdl_log
'.$whereclause.'
');
$temp = array();
$struct = array();
$i = 1;
$total = 0;
foreach($recs as $row){
$temp[$row->fdate][]=$row->ftime;
}
foreach($temp as $date=>$value){
//for each entity, get the date, first and last time
$struct[$i]['fdate'] = $date;
$struct[$i]['ftime1'] = current($value);
$struct[$i]['ftime2'] = end($value);
//convert the times to time
$temp1 = strtotime($struct[$i]['ftime1']);
$temp2 = strtotime($struct[$i]['ftime2']);
//determine the difference between two times
$difference = ($temp2 - $temp1)/60;
//store the difference
$struct[$i]['difference'] = $difference;
//accumulate the differences
$total = $total + $difference;
$i++;
//if it is the last item date write the total
if($i >= count($temp)){//then its the last row of the array
$struct[$i]['totalweeklytime'] = $total;
}
}
//if the struct is not empty, send back just the total time for each date
if(! empty($struct) ){
return $struct[$i]['totalweeklytime'];
}else{
return null;
}
}
Here are a couple other function, a bit smaller, that were living in both dbfunction.php files.
function sumOfGradeableItems($courseid){
global $DB;
return
$DB->get_record_sql('
SELECT round(sum(grademax),0) as total_points_could_be_earned
FROM mdl_grade_items
WHERE courseid = '.$courseid.'
AND itemtype IN ("mod","manual") AND itemname <> "pretest" AND hidden = 0
');
}
function sumOfUserCompletedGradableItems($courseid, $userid){
global $DB;
return
$DB->get_record_sql('
SELECT round(sum(finalgrade),0) AS total_points_earned_so_far
FROM mdl_grade_grades g
JOIN mdl_grade_items i ON i.id = g.itemid
#account for itemtype = mod - which are forum, assignment and quiz
#account for manually added gradeable items "manual"
#account for the itemname = pretest - which we do not want to include
WHERE itemtype IN ("mod","manual") AND itemname <> "pretest"
AND courseid = '.$courseid.' AND g.userid = '.$userid.'
');
}
No comments:
Post a Comment