MDL-7996 Add ods export support - more reports

This commit is contained in:
skodak 2006-12-21 19:46:29 +00:00
parent 84fc088239
commit d81b7ffbd7
5 changed files with 212 additions and 16 deletions

View File

@ -140,7 +140,60 @@
/// OK, we have all the data, now present it to the user
if ($download == "xls" and confirm_sesskey()) {
if ($download == "ods" and confirm_sesskey()) {
require_once("../lib/odslib.class.php");
/// Calculate file name
$downloadfilename = clean_filename("$course->shortname $strgrades.ods");
/// Creating a workbook
$workbook = new MoodleODSWorkbook("-");
/// Sending HTTP headers
$workbook->send($downloadfilename);
/// Adding the worksheet
$myxls =& $workbook->add_worksheet($strgrades);
/// Print names of all the fields
$myxls->write_string(0,0,get_string("firstname"));
$myxls->write_string(0,1,get_string("lastname"));
$myxls->write_string(0,2,get_string("idnumber"));
$myxls->write_string(0,3,get_string("institution"));
$myxls->write_string(0,4,get_string("department"));
$myxls->write_string(0,5,get_string("email"));
$pos=6;
foreach ($columns as $column) {
$myxls->write_string(0,$pos++,strip_tags($column));
}
$myxls->write_string(0,$pos,get_string("total"));
/// Print all the lines of data.
$i = 0;
foreach ($grades as $studentid => $studentgrades) {
$i++;
$student = $students[$studentid];
if (empty($totals[$student->id])) {
$totals[$student->id] = '';
}
$myxls->write_string($i,0,$student->firstname);
$myxls->write_string($i,1,$student->lastname);
$myxls->write_string($i,2,$student->idnumber);
$myxls->write_string($i,3,$student->institution);
$myxls->write_string($i,4,$student->department);
$myxls->write_string($i,5,$student->email);
$j=6;
foreach ($studentgrades as $grade) {
$myxls->write_string($i,$j++,strip_tags($grade));
}
$myxls->write_number($i,$j,$totals[$student->id]);
}
/// Close the workbook
$workbook->close();
exit;
} else if ($download == "xls" and confirm_sesskey()) {
require_once("../lib/excellib.class.php");
/// Calculate file name
@ -246,13 +299,17 @@
echo "<table border=\"0\" align=\"center\"><tr>";
echo "<td>";
$options = array();
$options["id"] = "$course->id";
$options["download"] = "xls";
$options["download"] = "ods";
$options["sesskey"] = $USER->sesskey;
print_single_button("grades.php", $options, get_string("downloadods"));
echo "<td>";
$options["download"] = "xls";
print_single_button("grades.php", $options, get_string("downloadexcel"));
echo "<td>";
$options = array();
$options["download"] = "txt";
$options["sesskey"] = $USER->sesskey;
print_single_button("grades.php", $options, get_string("downloadtext"));
echo "</table>";

View File

@ -738,14 +738,13 @@ function print_log_ods($course, $user, $date, $order='l.time DESC', $modname,
}
}
$myxls->write($row, 0, $courses[$log->course], '');
// Excel counts from 1/1/1900
$myxls->write_date($row, 1, $log->time, $formatDate);
$myxls->write($row, 2, $log->ip, '');
$myxls->write_string($row, 0, $courses[$log->course]);
$myxls->write_date($row, 1, $log->time);
$myxls->write_string($row, 2, $log->ip);
$fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
$myxls->write($row, 3, $fullname, '');
$myxls->write($row, 4, $log->module.' '.$log->action, '');
$myxls->write($row, 5, $log->info, '');
$myxls->write_string($row, 3, $fullname);
$myxls->write_string($row, 4, $log->module.' '.$log->action);
$myxls->write_string($row, 5, $log->info);
$row++;
}

View File

@ -38,7 +38,7 @@
redirect("report.php?id=$cm->id");
}
if ($download <> "xls" and $download <> "txt" ) {
if ($download <> "xls" and $download <> "txt" and $download <> "ods") {
print_header_simple(format_string($choice->name).": $strresponses", "",
"<a href=\"index.php?id=$course->id\">$strchoices</a> ->
<a href=\"view.php?id=$cm->id\">".format_string($choice->name,true)."</a> -> $strresponses", "", '', true,
@ -78,7 +78,69 @@
}
}
ksort($useranswer);
if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
require_once("$CFG->libdir/odslib.class.php");
/// Calculate file name
$filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
/// Creating a workbook
$workbook = new MoodleODSWorkbook("-");
/// Send HTTP headers
$workbook->send($filename);
/// Creating the first worksheet
$myxls =& $workbook->add_worksheet($strresponses);
/// Print names of all the fields
$myxls->write_string(0,0,get_string("lastname"));
$myxls->write_string(0,1,get_string("firstname"));
$myxls->write_string(0,2,get_string("idnumber"));
$myxls->write_string(0,3,get_string("group"));
$myxls->write_string(0,4,get_string("choice","choice"));
/// generate the data for the body of the spreadsheet
$i=0;
$row=1;
if ($users) {
foreach ($users as $user) {
// this needs fixing
if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) {
if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==0)) { // make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
$myxls->write_string($row,0,$user->lastname);
$myxls->write_string($row,1,$user->firstname);
$studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
$myxls->write_string($row,2,$studentid);
$ug2 = '';
if ($usergrps = user_group($course->id, $user->id)) {
foreach ($usergrps as $ug) {
$ug2 = $ug2. $ug->name;
}
}
$myxls->write_string($row,3,$ug2);
$useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
if (isset($useroption)) {
$myxls->write_string($row,4,format_string($useroption,true));
}
$row++;
}
$pos=4;
}
}
/// Close the workbook
$workbook->close();
exit;
}
}
//print spreadsheet if one is asked for:
if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
require_once("$CFG->libdir/excellib.class.php");
@ -188,8 +250,11 @@
echo "<br />\n";
echo "<table border=\"0\" align=\"center\"><tr>\n";
echo "<td>";
unset($options);
$options = array();
$options["id"] = "$cm->id";
$options["download"] = "ods";
print_single_button("report.php", $options, get_string("downloadods"));
echo "</td><td>";
$options["download"] = "xls";
print_single_button("report.php", $options, get_string("downloadexcel"));
echo "</td><td>";

View File

@ -116,6 +116,76 @@
}
}
// Output the file as a valid ODS spreadsheet if required
if ($type == "ods") {
require_once("$CFG->libdir/odslib.class.php");
/// Calculate file name
$downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.ods';
/// Creating a workbook
$workbook = new MoodleODSWorkbook("-");
/// Sending HTTP headers
$workbook->send($downloadfilename);
/// Creating the first worksheet
$myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
$header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
$col=0;
foreach ($header as $item) {
$myxls->write_string(0,$col++,$item);
}
foreach ($order as $key => $qid) {
$question = $questions["$qid"];
if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
$myxls->write_string(0,$col++,"$question->text");
}
if ($question->type == "2" || $question->type == "3") {
$myxls->write_string(0,$col++,"$question->text (preferred)");
}
}
// $date = $workbook->addformat();
// $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
$row = 0;
foreach ($results as $user => $rest) {
$col = 0;
$row++;
if (! $u = get_record("user", "id", $user)) {
error("Error finding student # $user");
}
if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
$notes = $n->notes;
} else {
$notes = "No notes made";
}
$myxls->write_string($row,$col++,$survey->id);
$myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
$myxls->write_string($row,$col++,$user);
$myxls->write_string($row,$col++,$u->firstname);
$myxls->write_string($row,$col++,$u->lastname);
$myxls->write_string($row,$col++,$u->email);
$myxls->write_string($row,$col++,$u->idnumber);
$myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
// $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
$myxls->write_string($row,$col++,$notes);
foreach ($order as $key => $qid) {
$question = $questions["$qid"];
if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
$myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
}
if ($question->type == "2" || $question->type == "3") {
$myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
}
}
}
$workbook->close();
exit;
}
// Output the file as a valid Excel spreadsheet if required
if ($type == "xls") {

View File

@ -420,13 +420,18 @@
echo '<p align="center">'.get_string("downloadinfo", "survey").'</p>';
echo '<center>';
$optons = array();
$options["id"] = "$cm->id";
$options["type"] = "xls";
$options["group"] = $currentgroup;
print_single_button("download.php", $options, get_string("downloadexcel", "survey"));
$options["type"] = "ods";
print_single_button("download.php", $options, get_string("downloadods"));
$options["type"] = "xls";
print_single_button("download.php", $options, get_string("downloadexcel"));
$options["type"] = "txt";
print_single_button("download.php", $options, get_string("downloadtext", "survey"));
print_single_button("download.php", $options, get_string("downloadtext"));
echo '</center>';
break;