Hi,
I have the same issue. I need to export scorm reports as Excel files. So I tried to modify a little the scorm module code in order to obtain this functionality. So if someone can take a look at my code and work together maybe we can obtain a good result.
So I started adding an extra option in the drop down menu of the scorm report ( export excel) :
>>> Picture
For that I modified this line (+ / - 215 ) :
$options = array('delete' => get_string('delete'), 'exportexcel' => get_string('exportexcel','scorm'));
and the scorm language file.
Then under the piece of code:
if ($action == 'delete' && has_capability('mod/scorm:deleteresponses',$contextmodule)) {
if (scorm_delete_responses($attemptids, $scorm->id)) { //delete responses.
notify(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
}
}
I added:
if ($action == ' exportexcel ' && has_capability('mod/scorm:….',$contextmodule)) {
scorm_download_score($scorm,$scorm->name, $scorm->id,$scorm->course);
exit();
}
So we have this new function scorm_download_score() which I placed in the scorm/lib.php file . I tried to implement this function like the developers did in the facetoface module where there is also an excel exporting feature.
function scorm_download_score($scormul,$scormname,$scormid,$scormcourse) {
global $CFG;
$timenow = time();
$timeformat = str_replace(' ', '_', get_string('strftimedate'));
$downloadfilename = clean_filename($scormname.'_'.userdate($timenow, $timeformat));
require_once $CFG->dirroot.'/grade/lib.php';
$userfields = array();
if (function_exists('grade_export_user_fields')) {
$userfields = grade_export_user_fields();
// echo ("exista grade_export_user_fields");
}
else {
// echo ("NUUUUUUUUUUUUUU exista grade_export_user_fields");
// Set default fields if the grade export patch is not
// detected (see MDL-17346)
$fieldnames = array('firstname', 'lastname', 'email', 'city',
'idnumber', 'institution', 'department', 'address');
foreach ($fieldnames as $shortname) {
$field = new object();
$field->shortname = $shortname;
$field->fullname = get_string($shortname);
// echo $field->fullname;
$userfields[] = $field;
//print_r($field);
// print_r($userfields[0]);
}
}
require_once($CFG->dirroot.'/lib/excellib.class.php');
echo " s a efectuat cererea de xcel";
$downloadfilename .= '.xls';
$workbook = new MoodleExcelWorkbook('-');
echo " s a creat xcelul";
$workbook->send($downloadfilename);
echo " s a trimis xcelul";
$worksheet =& $workbook->add_worksheet('score');
$pos=0;
$worksheet->write_string(0,$pos++,get_string('scormid', 'scorm'));
$worksheet->write_string(0,$pos++,get_string('scormname', 'scorm'));
$worksheet->write_string(0,$pos++,get_string('scormcourse', 'scorm'));
$worksheet->write_string(0,$pos++,get_string('started', 'scorm'));
$worksheet->write_string(0,$pos++,get_string('last', 'scorm'));
foreach ($userfields as $field) {
$worksheet->write_string(0,$pos++,$field->fullname);
}
$worksheet->write_string(0,$pos++,get_string('score', 'scorm'));
if (!empty($CFG->enablegroupings) && !empty($cm->groupingid)) {
//echo "intra pe gruping" ;
$sql = "SELECT st.userid, st.scormid
FROM {$CFG->prefix}scorm_scoes_track st
INNER JOIN {$CFG->prefix}groups_members gm ON st.userid = gm.userid
INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid
WHERE st.scormid = {$scorm->id} AND gg.groupingid = {$cm->groupingid}
GROUP BY st.userid,st.scormid
";
} else {
// echo "NUUUUUUUUUUUUUUUUUUUUUUU intra pe gruping" ;
$sql = "SELECT st.userid, st.scormid
FROM {$CFG->prefix}scorm_scoes_track st
WHERE st.scormid = $scormid
GROUP BY st.userid,st.scormid
";
//print_r($sql);
}
$scousers=get_records_sql($sql);
//print_r($scousers);
$i = 0;
if (!empty($scousers)) {
foreach ($scousers as $scouser) {
$attempt = scorm_get_last_attempt($scormid,$scouser->userid);
// $selectsc = 'scormid = '.$scormid.' and userid = '.$scouser->userid.' and attempt = '.$a;
$selectsc = 'scormid = '.$scormid.' and userid = '.$scouser->userid.' and attempt = '.'1';
$timetracks = get_record_select('scorm_scoes_track', $selectsc,'min(timemodified) as started, max(timemodified) as last');
$userdata = scorm_get_user_data($scouser->userid);
$studentsc = get_complete_user_data('id',$scouser->userid);
//print_r($userdata);
// print_r($studentsc);
if (!empty($studentsc)) {
$i++; $j=0;
$worksheet->write_string($i,$j++,$scormid);
$worksheet->write_string($i,$j++,$scormname);
$worksheet->write_string($i,$j++,$scormcourse);
$worksheet->write_string($i,$j++,userdate($timetracks->started, get_string('strftimedaydatetime')));
$worksheet->write_string($i,$j++,userdate($timetracks->last, get_string('strftimedaydatetime')));
foreach ($userfields as $field) {
$worksheet->write_string($i,$j++,$studentsc->{$field->shortname});
}
$worksheet->write_string($i,$j++,scorm_grade_user_attempt($scormul, $scouser->userid, 1));
// $scoruldinsange= scorm_grade_user_attempt($scormul, $scouser->userid, 1);
// echo $scoruldinsange;
// echo "scorul din sange" ;
}
}
}
else {
// no one has accessed the scorm, so let's just print the basic info
$i++; $j=0;
$worksheet->write_string($i,$j++,$scormid);
$worksheet->write_string($i,$j++,$scormname);
$worksheet->write_string($i,$j++,$scormcourse);
$worksheet->write_string($i,$j++,userdate($timetracks->started, get_string('strftimedaydatetime')));
$worksheet->write_string($i,$j++,userdate($timetracks->last, get_string('strftimedaydatetime')));
$worksheet->write_string($i,$j++,$scormid);
foreach ($userfields as $field) {
$worksheet->write_string($i,$j++,'-');
}
$worksheet->write_string($i,$j++,'-');
}
$workbook->close();
exit;
}
In the end I should retrieve the excel but I receive some strange characters like in this post of the forum:
http://moodle.org/mod/forum/discuss.php?d=128565
http://moodle.org/mod/forum/discuss.php?d=114972
From debugging I see that I have an : “ headers already sent error “ probably from the : $workbook->send($downloadfilename); line .
If anyone can help me or give me an advice I am waiting for your advices.
Thank you!