[svn r14840] Added CSV/XLS export to general exercise tracking

skala
Yannick Warnier 17 years ago
parent b5e1b5217e
commit d680b3d305
  1. 94
      main/exercice/exercice.php
  2. 329
      main/exercice/exercise_result.class.php
  3. 6
      main/exercice/exercise_result.php

@ -12,9 +12,9 @@
Contact:
Dokeos
Rue des Palais 44 Paleizenstraat
Rue du Corbeau, 108
B-1030 Brussels - Belgium
Tel. +32 (2) 211 34 56
info@dokeos.com
*/
@ -275,6 +275,9 @@ else
}
}
// need functions of statsutils lib to display previous exercices scores
include_once(api_get_path(LIBRARY_PATH).'statsUtils.lib.inc.php');
if($is_allowedToEdit && !empty($choice) && $choice == 'exportqti2')
{
require_once('export/qti2/qti2_export.php');
@ -300,6 +303,30 @@ if($is_allowedToEdit && !empty($choice) && $choice == 'exportqti2')
exit(); //otherwise following clicks may become buggy
}
if($_POST['export_report'] == 'export_report')
{
$user_id = null;
if(!$is_allowedToEdit and !$is_tutor)
{
$user_id = api_get_user_id();
}
require_once('exercise_result.class.php');
switch($_POST['export_format'])
{
case 'xls':
$export = new ExerciseResult();
$export->exportCompleteReportXLS($documentPath, $user_id);
exit;
break;
case 'csv':
default:
$export = new ExerciseResult();
$export->exportCompleteReportCSV($documentPath, $user_id);
exit;
break;
}
}
if ($origin != 'learnpath')
{
//so we are not in learnpath tool
@ -323,17 +350,11 @@ include_once(api_get_path(LIBRARY_PATH).'events.lib.inc.php');
event_access_tool(TOOL_QUIZ);
// need functions of statsutils lib to display previous exercices scores
include_once(api_get_path(LIBRARY_PATH).'statsUtils.lib.inc.php');
Display::display_introduction_section(TOOL_QUIZ);
// selects $limitExPage exercises at the same time
$from=$page*$limitExPage;
// $sql="SELECT id,title,type,active FROM $TBL_EXERCICES ORDER BY title LIMIT $from,".($limitExPage+1);
// $result=api_sql_query($sql,__FILE__,__LINE__);
$sql="SELECT count(id) FROM $TBL_EXERCICES";
$res = api_sql_query($sql,__FILE__,__LINE__);
list($nbrexerc) = Database::fetch_array($res);
@ -457,20 +478,20 @@ if($show == 'test'){
$nbrExercises=Database::num_rows($result);
echo "<table border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"2\" width=\"100%\">",
"<tr>";
echo '<table border="0" align="center" cellpadding="2" cellspacing="2" width="100%">'.
'<tr>';
if (($is_allowedToEdit) and ($origin != 'learnpath'))
{
echo "<td width=\"50%\" nowrap=\"nowrap\">",
"<img src=\"../img/new_test.gif\" alt=\"new test\" align=\"absbottom\">&nbsp;<a href=\"exercise_admin.php?".api_get_cidreq()."\">".get_lang("NewEx")."</a>",
" | <img src=\"../img/jqz.jpg\" alt=\"HotPotatoes\" valign=\"ABSMIDDLE\">&nbsp;<a href=\"hotpotatoes.php\">".get_lang("ImportHotPotatoesQuiz")."</a>",
"</td>",
"<td width=\"50%\" align=\"right\">";
echo '<td width="50%" nowrap="nowrap">'.
'<img src="../img/new_test.gif" alt="new test" align="absbottom">&nbsp;<a href="exercise_admin.php?'.api_get_cidreq().'">'.get_lang('NewEx').'</a>'.
' | <img src="../img/jqz.jpg" alt="HotPotatoes" valign="ABSMIDDLE">&nbsp;<a href="hotpotatoes.php">'.get_lang('ImportHotPotatoesQuiz').'</a>'.
'</td>'.
'<td width="50%" align="right">';
}
else
{
echo "<td align=\"right\">";
echo '<td align="right">';
}
//get HotPotatoes files (active and inactive)
@ -518,9 +539,9 @@ if($show == 'test'){
echo get_lang("NextPage") . " &gt;&gt;";
}
echo "</td>",
"</tr>",
"</table>";
echo '</td>',
'</tr>',
'</table>';
?>
<table class="data_table">
@ -571,7 +592,7 @@ if($show == 'test'){
{
if($i%2==0) $s_class="row_odd"; else $s_class="row_even";
echo "<tr class='$s_class'>\n";
echo '<tr class="'.$s_class.'">'."\n";
// prof only
if($is_allowedToEdit)
@ -639,14 +660,14 @@ if($show == 'test'){
</tr>
</table></td>
<td align='center'> <?php
<td align="center"> <?php
$exid = $row['id'];
$sqlquery = "SELECT count(*) FROM $TBL_EXERCICE_QUESTION WHERE exercice_id = '$exid'";
$sqlresult =api_sql_query($sqlquery);
$rowi = mysql_result($sqlresult,0);
echo ($rowi>1?get_lang('Questions'):get_lang('Question')); ?> </td>
<td align='center'><?php
<td align="center"><?php
$eid = $row['id'];
$uid= api_get_user_id();
$qry = "select * from $TBL_TRACK_EXERCICES where exe_exo_id = $eid and exe_user_id = $uid and exe_cours_id = '".api_get_course_id()."'";
@ -709,7 +730,7 @@ if($show == 'test'){
$sql = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility
FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
WHERE d.id = ip.ref AND ip.tool = '".TOOL_DOCUMENT."' AND
(d.path LIKE '%htm%' OR d.path LIKE '%html%')
(d.path LIKE '%htm%')
AND d.path LIKE '".$uploadPath."/%/%' LIMIT $from,$to"; // only .htm or .html files listed
}
else
@ -717,7 +738,7 @@ if($show == 'test'){
$sql = "SELECT d.path as path, d.comment as comment, ip.visibility as visibility
FROM $TBL_DOCUMENT d, $TBL_ITEM_PROPERTY ip
WHERE d.id = ip.ref AND ip.tool = '".TOOL_DOCUMENT."' AND
(d.path LIKE '%htm%' OR d.path LIKE '%html%')
(d.path LIKE '%htm%')
AND d.path LIKE '".$uploadPath."/%/%' AND ip.visibility='1' LIMIT $from,$to";
}
@ -817,7 +838,6 @@ if($show == 'test'){
$ind++;
}
}
//echo '<tr><td colspan="5"><hr /></td></tr>';
}
}
@ -840,8 +860,22 @@ if($show == 'test'){
if($_configuration['tracking_enabled'])
{
if($show == 'result'){
if($show == 'result')
{
// the form
echo '<form id="form1a" name="form1a" method="post" action="'.api_get_self().'?show='.Security::remove_XSS($_GET['show']).'">';
echo '<input type="hidden" name="export_report" value="export_report">';
echo '<input type="hidden" name="export_format" value="csv">';
echo '</form>';
echo '<form id="form1b" name="form1b" method="post" action="'.api_get_self().'?show='.Security::remove_XSS($_GET['show']).'">';
echo '<input type="hidden" name="export_report" value="export_report">';
echo '<input type="hidden" name="export_format" value="xls">';
echo '</form>';
echo '<a class="quiz_export_link" href="#" onclick="document.form1a.submit();"><img align="absbottom" src="'.api_get_path(WEB_IMG_PATH).'excel.gif">&nbsp;'.get_lang('ExportAsCSV').'</a>';
echo '<a class="quiz_export_link" href="#" onclick="document.form1b.submit();"><img align="absbottom" src="'.api_get_path(WEB_IMG_PATH).'excel.gif">&nbsp;'.get_lang('ExportAsXLS').'</a>';
echo '<br /><br />';
?>
<table class="data_table">
@ -978,9 +1012,11 @@ if($_configuration['tracking_enabled'])
if ($origin != 'learnpath') { //so we are not in learnpath tool
Display::display_footer();
} else {
}
else
{
?>
<link rel="stylesheet" type="text/css" href="<?php echo $clarolineRepositoryWeb ?>css/default.css" />
<?php
}
?>
?>

@ -0,0 +1,329 @@
<?php
/*
DOKEOS - elearning and course management software
For a full list of contributors, see documentation/credits.html
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
See "documentation/licence.html" more details.
Contact:
Dokeos
Rue du Corbeau, 108
B-1030 Brussels - Belgium
info@dokeos.com
*/
/**
* ExerciseResult class: This class allows to instantiate an object of type ExerciseResult
* which allows you to export exercises results in multiple presentation forms
* @package dokeos.exercise
* @author Yannick Warnier
* @version $Id: $
*/
if(!class_exists('ExerciseResult')):
class ExerciseResult
{
private $exercises_list = array(); //stores the list of exercises
private $results = array(); //stores the results
/**
* constructor of the class
*/
public function ExerciseResult($get_questions=false,$get_answers=false)
{
//nothing to do
/*
$this->exercise_list = array();
$this->readExercisesList();
if($get_questions)
{
foreach($this->exercises_list as $exe)
{
$this->exercises_list['questions'] = $this->getExerciseQuestionList($exe['id']);
}
}
*/
}
/**
* Reads exercises information (minimal) from the data base
* @param boolean Whether to get only visible exercises (true) or all of them (false). Defaults to false.
* @return array A list of exercises available
*/
private function _readExercisesList($only_visible = false)
{
$return = array();
$TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
$sql="SELECT id,title,type,random,active FROM $TBL_EXERCISES";
if($only_visible)
{
$sql.= ' WHERE active=1';
}
$sql .= ' ORDER BY title';
$result=api_sql_query($sql,__FILE__,__LINE__);
// if the exercise has been found
while($row=Database::fetch_array($result,'ASSOC'))
{
$return[] = $row;
}
// exercise not found
return $return;
}
/**
* Gets the questions related to one exercise
* @param integer Exercise ID
*/
private function _readExerciseQuestionsList($e_id)
{
$return = array();
$TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
$sql="SELECT q.id, q.question, q.ponderation, q.position, q.type, q.picture " .
" FROM $TBL_EXERCISE_QUESTION eq, $TBL_QUESTIONS q " .
" WHERE eq.question_id=q.id AND eq.exercice_id='$e_id' " .
" ORDER BY q.position";
$result=api_sql_query($sql,__FILE__,__LINE__);
// fills the array with the question ID for this exercise
// the key of the array is the question position
while($row=Database::fetch_array($result,'ASSOC'))
{
$return[] = $row;
}
return true;
}
/**
* Gets the results of all students (or just one student if access is limited)
* @param string The document path (for HotPotatoes retrieval)
* @param integer User ID. Optional. If no user ID is provided, we take all the results. Defauts to null
*/
function _getExercisesReporting($document_path,$user_id=null)
{
$return = array();
$TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
$TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
$TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
$TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
$TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
$TBL_TRACK_EXERCISES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
$TBL_TRACK_HOTPOTATOES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
$TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
$cid = api_get_course_id();
if(empty($user_id))
{
//get all results (ourself and the others) as an admin should see them
//AND exe_user_id <> $_user['user_id'] clause has been removed
$sql="SELECT CONCAT(lastname,' ',firstname),ce.title, te.exe_result ,
te.exe_weighting, UNIX_TIMESTAMP(te.exe_date),te.exe_id,email
FROM $TBL_EXERCISES ce , $TBL_TRACK_EXERCISES te, $TBL_USER user
WHERE te.exe_exo_id = ce.id AND user_id=te.exe_user_id AND te.exe_cours_id='$cid'
ORDER BY te.exe_cours_id ASC, ce.title ASC, te.exe_date ASC";
$hpsql="SELECT CONCAT(tu.lastname,' ',tu.firstname), tth.exe_name,
tth.exe_result , tth.exe_weighting, UNIX_TIMESTAMP(tth.exe_date)
FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu
WHERE tu.user_id=tth.exe_user_id AND tth.exe_cours_id = '".$cid."'
ORDER BY tth.exe_cours_id ASC, tth.exe_date ASC";
}
else
{ // get only this user's results
$sql="SELECT '',ce.title, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date),te.exe_id
FROM $TBL_EXERCISES ce , $TBL_TRACK_EXERCISES te
WHERE te.exe_exo_id = ce.id AND te.exe_user_id='".$user_id."' AND te.exe_cours_id='$cid'
ORDER BY te.exe_cours_id ASC, ce.title ASC, te.exe_date ASC";
$hpsql="SELECT '',exe_name, exe_result , exe_weighting, UNIX_TIMESTAMP(exe_date)
FROM $TBL_TRACK_HOTPOTATOES
WHERE exe_user_id = '".$user_id."' AND exe_cours_id = '".$cid."'
ORDER BY exe_cours_id ASC, exe_date ASC";
}
$results=getManyResultsXCol($sql,7);
$hpresults=getManyResultsXCol($hpsql,5);
$NoTestRes = 0;
$NoHPTestRes = 0;
$j=0;
//Print the results of tests
if(is_array($results))
{
for($i = 0; $i < sizeof($results); $i++)
{
$return[$i] = array();
$id = $results[$i][5];
$mailid = $results[$i][6];
$user = $results[$i][0];
$test = $results[$i][1];
$dt = strftime(get_lang('dateTimeFormatLong'),$results[$i][4]);
$res = $results[$i][2];
if(empty($user_id))
{
$user = $results[$i][0];
$return[$i]['user'] = $user;
}
$return[$i]['title'] = $test;
$return[$i]['time'] = format_locale_date(get_lang('dateTimeFormatLong'),$results[$i][4]);
$return[$i]['result'] = $res;
$return[$i]['max'] = $results[$i][3];
$j=$i;
}
}
$j++;
// Print the Result of Hotpotatoes Tests
if(is_array($hpresults))
{
for($i = 0; $i < sizeof($hpresults); $i++)
{
$return[$j+$i] = array();
$title = GetQuizName($hpresults[$i][1],$document_path);
if ($title =='')
{
$title = GetFileName($hpresults[$i][1]);
}
if(empty($user_id))
{
$return[$j+$i]['user'] = $hpresults[$i][0];
}
$return[$j+$i]['title'] = $title;
$return[$j+$i]['time'] = strftime(get_lang('dateTimeFormatLong'),$hpresults[$i][4]);
$return[$j+$i]['result'] = $hpresults[$i][2];
$return[$j+$i]['max'] = $hpresults[$i][3];
}
}
$this->results = $return;
return true;
}
/**
* Exports the complete report as a CSV file
* @param string Document path inside the document tool
* @param integer Optional user ID
* @return boolean False on error
*/
public function exportCompleteReportCSV($document_path='',$user_id=null)
{
$this->_getExercisesReporting($document_path,$user_id);
$filename = 'exercise_results_'.date('YmdGis').'.csv';
if(!empty($user_id))
{
$filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.csv';
}
$data = '';
//build the results
//titles
if(!empty($this->results[0]['user']))
{
$data .= get_lang('User').';';
}
$data .= get_lang('Title').';';
$data .= get_lang('Date').';';
$data .= get_lang('Results').';';
$data .= get_lang('Weighting').';';
$data .= "\n";
//results
foreach($this->results as $row)
{
if(!empty($row['user']))
{
$data .= str_replace("\r\n",' ',html_entity_decode(strip_tags($row['user']))).';';
}
$data .= str_replace("\r\n",' ',html_entity_decode(strip_tags($row['title']))).';';
$data .= str_replace("\r\n",' ',$row['time']).';';
$data .= str_replace("\r\n",' ',$row['result']).';';
$data .= str_replace("\r\n",' ',$row['max']).';';
$data .= "\n";
}
//output the results
$len = strlen($data);
header('Content-type: application/octet-stream');
header('Content-Type: application/force-download');
header('Content-length: '.$len);
if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT']))
{
header('Content-Disposition: filename= '.$filename);
}
else
{
header('Content-Disposition: attachment; filename= '.$filename);
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
{
header('Pragma: ');
header('Cache-Control: ');
header('Cache-Control: public'); // IE cannot download from sessions without a cache
}
header('Content-Description: '.$filename);
header('Content-transfer-encoding: binary');
echo $data;
return true;
}
/**
* Exports the complete report as an XLS file
* @return boolean False on error
*/
public function exportCompleteReportXLS($document_path='',$user_id=null)
{
$this->_getExercisesReporting($document_path,$user_id);
$filename = 'exercise_results_'.date('YmdGis').'.xls';
if(!empty($user_id))
{
$filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.xls';
} //build the results
require_once(api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php');
$workbook = new Spreadsheet_Excel_Writer();
$workbook->send($filename);
$worksheet =& $workbook->addWorksheet('Report '.date('YmdGis'));
$line = 0;
$column = 0; //skip the first column (row titles)
if(!empty($this->results[0]['user']))
{
$worksheet->write($line,$column,get_lang('User'));
$column++;
}
$worksheet->write($line,$column,get_lang('Title'));
$column++;
$worksheet->write($line,$column,get_lang('Date'));
$column++;
$worksheet->write($line,$column,get_lang('Results'));
$column++;
$worksheet->write($line,$column,get_lang('Weighting'));
$line++;
foreach($this->results as $row)
{
$column = 0;
if(!empty($row['user']))
{
$worksheet->write($line,$column,html_entity_decode(strip_tags($row['user'])));
$column++;
}
$worksheet->write($line,$column,html_entity_decode(strip_tags($row['title'])));
$column++;
$worksheet->write($line,$column,$row['time']);
$column++;
$worksheet->write($line,$column,$row['result']);
$column++;
$worksheet->write($line,$column,$row['max']);
$line++;
}
//output the results
$workbook->close();
return true;
}
}
endif;
?>

@ -12,9 +12,9 @@
Contact:
Dokeos
Rue des Palais 44 Paleizenstraat
Rue du Corbeau, 108
B-1030 Brussels - Belgium
Tel. +32 (2) 211 34 56
info@dokeos.com
*/
/**
* Exercise result
@ -25,7 +25,7 @@
* @package dokeos.exercise
* @author Olivier Brouckaert, main author
* @author Roan Embrechts, some refactoring
* @version $Id: exercise_result.php 14786 2008-04-08 14:11:46Z elixir_inter $
* @version $Id: exercise_result.php 14840 2008-04-11 06:21:45Z yannoo $
*
* @todo split more code up in functions, move functions to library?
*/

Loading…
Cancel
Save