dokeos-exercise
[ class tree: dokeos-exercise ] [ index: dokeos-exercise ] [ all elements ]

Source for file exercise_result.class.php

Documentation is available at exercise_result.class.php

  1. <?php
  2. /*
  3.     DOKEOS - elearning and course management software
  4.  
  5.     For a full list of contributors, see documentation/credits.html
  6.  
  7.     This program is free software; you can redistribute it and/or
  8.     modify it under the terms of the GNU General Public License
  9.     as published by the Free Software Foundation; either version 2
  10.     of the License, or (at your option) any later version.
  11.     See "documentation/licence.html" more details.
  12.  
  13.     Contact:
  14.         Dokeos
  15.         Rue du Corbeau, 108
  16.         B-1030 Brussels - Belgium
  17.         info@dokeos.com
  18. */
  19. /**
  20. *    ExerciseResult class: This class allows to instantiate an object of type ExerciseResult
  21. *    which allows you to export exercises results in multiple presentation forms
  22. *    @package dokeos.exercise
  23. *     @author Yannick Warnier
  24. *     @version $Id: $
  25. */
  26.  
  27.  
  28. if(!class_exists('ExerciseResult')):
  29.  
  30. {
  31.     private $exercises_list = array()//stores the list of exercises
  32.     private $results = array()//stores the results
  33.     
  34.     /**
  35.      * constructor of the class
  36.      */
  37.     public function ExerciseResult($get_questions=false,$get_answers=false)
  38.     {
  39.         //nothing to do
  40.         /*
  41.         $this->exercise_list = array();
  42.         $this->readExercisesList();
  43.         if($get_questions)
  44.         {
  45.             foreach($this->exercises_list as $exe)
  46.             {
  47.                 $this->exercises_list['questions'] = $this->getExerciseQuestionList($exe['id']);
  48.             }
  49.         }
  50.         */
  51.     }
  52.  
  53.     /**
  54.      * Reads exercises information (minimal) from the data base
  55.      * @param    boolean        Whether to get only visible exercises (true) or all of them (false). Defaults to false.
  56.      * @return    array        A list of exercises available
  57.      */
  58.     private function _readExercisesList($only_visible false)
  59.     {
  60.         $return array();
  61.         $TBL_EXERCISES          Database::get_course_table(TABLE_QUIZ_TEST);
  62.  
  63.         $sql="SELECT id,title,type,random,active FROM $TBL_EXERCISES";
  64.         if($only_visible)
  65.         {
  66.             $sql.= ' WHERE active=1';
  67.         }
  68.         $sql .= ' ORDER BY title';
  69.         $result=api_sql_query($sql,__FILE__,__LINE__);
  70.  
  71.         // if the exercise has been found
  72.         while($row=Database::fetch_array($result,'ASSOC'))
  73.         {
  74.             $return[$row;
  75.         }
  76.         // exercise not found
  77.         return $return;
  78.     }
  79.  
  80.     /**
  81.      * Gets the questions related to one exercise
  82.      * @param    integer        Exercise ID
  83.      */
  84.     private function _readExerciseQuestionsList($e_id)
  85.     {
  86.         $return array();
  87.         $TBL_EXERCISE_QUESTION  Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  88.         $TBL_QUESTIONS Database::get_course_table(TABLE_QUIZ_QUESTION);
  89.         $sql="SELECT q.id, q.question, q.ponderation, q.position, q.type, q.picture " .
  90.             " FROM $TBL_EXERCISE_QUESTION eq, $TBL_QUESTIONS q .
  91.             " WHERE eq.question_id=q.id AND eq.exercice_id='$e_id.
  92.             " ORDER BY q.position";
  93.         $result=api_sql_query($sql,__FILE__,__LINE__);
  94.  
  95.         // fills the array with the question ID for this exercise
  96.         // the key of the array is the question position
  97.         while($row=Database::fetch_array($result,'ASSOC'))
  98.         {
  99.             $return[$row;
  100.         }
  101.         return true;        
  102.     }
  103.     /**
  104.      * Gets the results of all students (or just one student if access is limited)
  105.      * @param    string        The document path (for HotPotatoes retrieval)
  106.      * @param    integer        User ID. Optional. If no user ID is provided, we take all the results. Defauts to null
  107.      */
  108.     function _getExercisesReporting($document_path,$user_id=null)
  109.     {
  110.         $return array();
  111.         $TBL_EXERCISES          Database::get_course_table(TABLE_QUIZ_TEST);
  112.         $TBL_EXERCISE_QUESTION  Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  113.         $TBL_QUESTIONS             Database::get_course_table(TABLE_QUIZ_QUESTION);
  114.         $TBL_USER                  Database::get_main_table(TABLE_MAIN_USER);
  115.         $TBL_DOCUMENT              Database::get_course_table(TABLE_DOCUMENT);
  116.         $TBL_ITEM_PROPERTY      Database::get_course_table(TABLE_ITEM_PROPERTY);
  117.         $TBL_TRACK_EXERCISES    Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  118.         $TBL_TRACK_HOTPOTATOES    Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  119.         $TBL_TRACK_ATTEMPT        Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  120.  
  121.         $cid api_get_course_id();
  122.         if(empty($user_id))
  123.         {
  124.             //get all results (ourself and the others) as an admin should see them
  125.             //AND exe_user_id <> $_user['user_id']  clause has been removed
  126.             $sql="SELECT CONCAT(lastname,' ',firstname),ce.title, te.exe_result ,
  127.                         te.exe_weighting, UNIX_TIMESTAMP(te.exe_date),te.exe_id, user.email, user.user_id
  128.                   FROM $TBL_EXERCISES ce , $TBL_TRACK_EXERCISES te, $TBL_USER user
  129.                   WHERE te.exe_exo_id = ce.id AND user_id=te.exe_user_id AND te.exe_cours_id='$cid'
  130.                   ORDER BY te.exe_cours_id ASC, ce.title ASC, te.exe_date ASC";
  131.  
  132.             $hpsql="SELECT CONCAT(tu.lastname,' ',tu.firstname), tth.exe_name,
  133.                         tth.exe_result , tth.exe_weighting, UNIX_TIMESTAMP(tth.exe_date), tu.email, tu.user_id
  134.                     FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu
  135.                     WHERE  tu.user_id=tth.exe_user_id AND tth.exe_cours_id = '".$cid."'
  136.                     ORDER BY tth.exe_cours_id ASC, tth.exe_date ASC";
  137.  
  138.         }
  139.         else
  140.         // get only this user's results
  141.               $sql="SELECT '',ce.title, te.exe_result , te.exe_weighting, " .
  142.                       "UNIX_TIMESTAMP(te.exe_date),te.exe_id
  143.                   FROM $TBL_EXERCISES ce , $TBL_TRACK_EXERCISES te
  144.                   WHERE te.exe_exo_id = ce.id AND te.exe_user_id='".$user_id."' AND te.exe_cours_id='$cid'
  145.                   ORDER BY te.exe_cours_id ASC, ce.title ASC, te.exe_date ASC";
  146.  
  147.             $hpsql="SELECT '',exe_name, exe_result , exe_weighting, UNIX_TIMESTAMP(exe_date)
  148.                     FROM $TBL_TRACK_HOTPOTATOES
  149.                     WHERE exe_user_id = '".$user_id."' AND exe_cours_id = '".$cid."'
  150.                     ORDER BY exe_cours_id ASC, exe_date ASC";
  151.  
  152.         }
  153.  
  154.         $results=getManyResultsXCol($sql,8);
  155.         $hpresults=getManyResultsXCol($hpsql,7);
  156.  
  157.         $NoTestRes 0;
  158.         $NoHPTestRes 0;
  159.         $j=0;
  160.         //Print the results of tests
  161.         if(is_array($results))
  162.         {
  163.             for($i 0$i sizeof($results)$i++)
  164.             {
  165.                 $return[$iarray();
  166.                 $id $results[$i][5];
  167.                 $mailid $results[$i][6];
  168.                 $user $results[$i][0];
  169.                 $test $results[$i][1];
  170.                 $dt strftime(get_lang('dateTimeFormatLong'),$results[$i][4]);
  171.                 $res $results[$i][2];
  172.                 if(empty($user_id))
  173.                 {
  174.                     $user $results[$i][0];
  175.                     $return[$i]['user'$user;
  176.                     $return[$i]['user_id'$results[$i][7];
  177.                 }
  178.                 $return[$i]['title'$test;
  179.                 $return[$i]['time'format_locale_date(get_lang('dateTimeFormatLong'),$results[$i][4]);
  180.                 $return[$i]['result'$res;
  181.                 $return[$i]['max'$results[$i][3];
  182.                 $j=$i;
  183.             }
  184.         }
  185.         $j++;
  186.         // Print the Result of Hotpotatoes Tests
  187.         if(is_array($hpresults))
  188.         {
  189.             for($i 0$i sizeof($hpresults)$i++)
  190.             {
  191.                 $return[$j+$iarray();
  192.                 $title GetQuizName($hpresults[$i][1],$document_path);
  193.                 if ($title =='')
  194.                 {
  195.                     $title GetFileName($hpresults[$i][1]);
  196.                 }
  197.                 if(empty($user_id))
  198.                 {
  199.                     $return[$j+$i]['user'$hpresults[$i][0];
  200.                     $return[$j+$i]['user_id'$results[$i][6];
  201.                     
  202.                 }
  203.                 $return[$j+$i]['title'$title;
  204.                 $return[$j+$i]['time'strftime(get_lang('dateTimeFormatLong'),$hpresults[$i][4]);
  205.                 $return[$j+$i]['result'$hpresults[$i][2];
  206.                 $return[$j+$i]['max'$hpresults[$i][3];
  207.             }
  208.         }
  209.         $this->results = $return;
  210.         return true;
  211.     }
  212.     /**
  213.      * Exports the complete report as a CSV file
  214.      * @param    string        Document path inside the document tool
  215.      * @param    integer        Optional user ID
  216.      * @param    boolean        Whether to include user fields or not
  217.      * @return    boolean        False on error
  218.      */
  219.     public function exportCompleteReportCSV($document_path='',$user_id=null$export_user_fields)
  220.     {
  221.         $this->_getExercisesReporting($document_path,$user_id);
  222.         $filename 'exercise_results_'.date('YmdGis').'.csv';
  223.         if(!empty($user_id))
  224.         {
  225.             $filename 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.csv';
  226.         }
  227.         $data '';
  228.         //build the results
  229.         //titles
  230.         if(!empty($this->results[0]['user']))
  231.         {
  232.             $data .= get_lang('User').';';
  233.         }
  234.         if($export_user_fields)
  235.         {
  236.             //show user fields section with a big th colspan that spans over all fields
  237.             $extra_user_fields UserManager::get_extra_fields(0,0,5,'ASC',false);
  238.             $num count($extra_user_fields);
  239.             foreach($extra_user_fields as $field)
  240.             {
  241.                 $data .= '"'.str_replace("\r\n",'  ',html_entity_decode(strip_tags($field[3]))).'";';
  242.             }
  243.             $display_extra_user_fields true;
  244.         }
  245.         $data .= get_lang('Title').';';
  246.         $data .= get_lang('Date').';';
  247.         $data .= get_lang('Results').';';
  248.         $data .= get_lang('Weighting').';';
  249.         $data .= "\n";
  250.         //results
  251.         foreach($this->results as $row)
  252.         {
  253.             if(!empty($row['user']))
  254.             {
  255.                 $data .= str_replace("\r\n",'  ',html_entity_decode(strip_tags($row['user']))).';';
  256.             }
  257.             if($export_user_fields)
  258.             {
  259.                 //show user fields data, if any, for this user
  260.                 $user_fields_values UserManager::get_extra_user_data(intval($row['user_id']),false,false);
  261.                 foreach($user_fields_values as $value)
  262.                 {
  263.                     $data .= '"'.str_replace('"','""',html_entity_decode(strip_tags($value))).'";';
  264.                 }
  265.             }
  266.             $data .= str_replace("\r\n",'  ',html_entity_decode(strip_tags($row['title']))).';';
  267.             $data .= str_replace("\r\n",'  ',$row['time']).';';
  268.             $data .= str_replace("\r\n",'  ',$row['result']).';';
  269.             $data .= str_replace("\r\n",'  ',$row['max']).';';
  270.             $data .= "\n";
  271.         }
  272.         //output the results
  273.         $len strlen($data);
  274.         header('Content-type: application/octet-stream');
  275.         header('Content-Type: application/force-download');
  276.         header('Content-length: '.$len);
  277.         if (preg_match("/MSIE 5.5/"$_SERVER['HTTP_USER_AGENT']))
  278.         {
  279.             header('Content-Disposition: filename= '.$filename);
  280.         }
  281.         else
  282.         {
  283.             header('Content-Disposition: attachment; filename= '.$filename);
  284.         }
  285.         if (strpos($_SERVER['HTTP_USER_AGENT']'MSIE'))
  286.         {
  287.             header('Pragma: ');
  288.             header('Cache-Control: ');
  289.             header('Cache-Control: public')// IE cannot download from sessions without a cache
  290.         }
  291.         header('Content-Description: '.$filename);
  292.         header('Content-transfer-encoding: binary');
  293.         echo $data;
  294.         return true;
  295.     }
  296.     /**
  297.      * Exports the complete report as an XLS file
  298.      * @return    boolean        False on error
  299.      */
  300.     public function exportCompleteReportXLS($document_path='',$user_id=null$export_user_fields)
  301.     {
  302.         $this->_getExercisesReporting($document_path,$user_id);
  303.         $filename 'exercise_results_'.date('YmdGis').'.xls';
  304.         if(!empty($user_id))
  305.         {
  306.             $filename 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.xls';
  307.         }        //build the results
  308.         require_once(api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php');
  309.         $workbook new Spreadsheet_Excel_Writer();
  310.         $workbook->send($filename);
  311.         $worksheet =$workbook->addWorksheet('Report '.date('YmdGis'));
  312.         $line 0;
  313.         $column 0//skip the first column (row titles)
  314.         if(!empty($this->results[0]['user']))
  315.         {
  316.             $worksheet->write($line,$column,get_lang('User'));
  317.             $column++;
  318.         }
  319.         if($export_user_fields)
  320.         {
  321.             //show user fields section with a big th colspan that spans over all fields
  322.             $extra_user_fields UserManager::get_extra_fields(0,0,5,'ASC',false);
  323.             //show the fields names for user fields
  324.             foreach($extra_user_fields as $field)
  325.             {
  326.                 $worksheet->write($line,$column,html_entity_decode(strip_tags($field[3])));
  327.                 $column++;
  328.             }
  329.         }
  330.         $worksheet->write($line,$column,get_lang('Title'));
  331.         $column++;
  332.         $worksheet->write($line,$column,get_lang('Date'));
  333.         $column++;
  334.         $worksheet->write($line,$column,get_lang('Results'));
  335.         $column++;
  336.         $worksheet->write($line,$column,get_lang('Weighting'));
  337.         $line++;
  338.  
  339.         foreach($this->results as $row)
  340.         {
  341.             $column 0;
  342.             if(!empty($row['user']))
  343.             {
  344.                 $worksheet->write($line,$column,html_entity_decode(strip_tags($row['user'])));
  345.                 $column++;
  346.             }
  347.             //show user fields data, if any, for this user
  348.             $user_fields_values UserManager::get_extra_user_data(intval($row['user_id']),false,false);
  349.             foreach($user_fields_values as $value)
  350.             {
  351.                 $worksheet->write($line,$column,html_entity_decode(strip_tags($value)));
  352.                 $column++;
  353.             }
  354.             $worksheet->write($line,$column,html_entity_decode(strip_tags($row['title'])));
  355.             $column++;
  356.             $worksheet->write($line,$column,$row['time']);
  357.             $column++;
  358.             $worksheet->write($line,$column,$row['result']);
  359.             $column++;
  360.             $worksheet->write($line,$column,$row['max']);
  361.             $line++;
  362.         }
  363.         //output the results
  364.         $workbook->close();
  365.         return true;
  366.     }
  367. }
  368.  
  369. endif;
  370. ?>

Documentation generated on Thu, 12 Jun 2008 13:26:08 -0500 by phpDocumentor 1.4.1