Dokeos PHP Cross Reference Learning Management Systems

Source: /main/exercice/exercise_result.class.php - 393 lines - 13671 bytes - Summary - Text - Print

Description: ExerciseResult class: This class allows to instantiate an object of type ExerciseResult which allows you to export exercises results in multiple presentation forms

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

title

Description

title

Description

title

Description

title

title

Body