exercise_result.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ExerciseResult
  5. * which allows you to export exercises results in multiple presentation forms
  6. * @package chamilo.exercise
  7. * @author Yannick Warnier
  8. */
  9. class ExerciseResult
  10. {
  11. private $results = array();
  12. public $includeAllUsers = false;
  13. public $onlyBestAttempts = false;
  14. /**
  15. * @param bool $includeAllUsers
  16. */
  17. public function setIncludeAllUsers($includeAllUsers)
  18. {
  19. $this->includeAllUsers = $includeAllUsers;
  20. }
  21. /**
  22. * @param bool $value
  23. */
  24. public function setOnlyBestAttempts($value)
  25. {
  26. $this->onlyBestAttempts = $value;
  27. }
  28. /**
  29. * Gets the results of all students (or just one student if access is limited)
  30. *
  31. * @param string $document_path The document path (for HotPotatoes retrieval)
  32. * @param int $user_id User ID. Optional. If no user ID is provided, we take all the results. Defauts to null
  33. * @param int $filter
  34. * @param int $exercise_id
  35. * @param null $hotpotato_name
  36. * @return bool
  37. */
  38. public function getExercisesReporting(
  39. $document_path,
  40. $user_id = null,
  41. $filter = 0,
  42. $exercise_id = 0,
  43. $hotpotato_name = null
  44. ) {
  45. $return = array();
  46. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  47. $TBL_TABLE_LP_MAIN = Database::get_course_table(TABLE_LP_MAIN);
  48. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  49. $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  50. $TBL_TRACK_ATTEMPT_RECORDING = Database:: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  51. $cid = api_get_course_id();
  52. $course_id = api_get_course_int_id();
  53. $user_id = intval($user_id);
  54. $sessionId = api_get_session_id();
  55. $session_id_and = ' AND te.session_id = ' . $sessionId . ' ';
  56. $exercise_id = intval($exercise_id);
  57. if (!empty($exercise_id)) {
  58. $session_id_and .= " AND exe_exo_id = $exercise_id ";
  59. }
  60. if (empty($user_id)) {
  61. $user_id_and = null;
  62. $sql = "SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").",
  63. official_code,
  64. ce.title as extitle,
  65. te.exe_result as exresult ,
  66. te.exe_weighting as exweight,
  67. te.exe_date as exdate,
  68. te.exe_id as exid,
  69. email as exemail,
  70. te.start_date as exstart,
  71. steps_counter as exstep,
  72. exe_user_id as excruid,
  73. te.exe_duration as duration,
  74. te.orig_lp_id as orig_lp_id,
  75. tlm.name as lp_name
  76. FROM $TBL_EXERCISES AS ce
  77. INNER JOIN $TBL_TRACK_EXERCISES AS te ON (te.exe_exo_id = ce.id)
  78. INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  79. LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm ON tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id
  80. WHERE
  81. ce.c_id = $course_id AND
  82. te.status != 'incomplete' AND
  83. te.exe_cours_id='" . Database :: escape_string($cid) . "' $user_id_and $session_id_and AND
  84. ce.active <>-1";
  85. } else {
  86. $user_id_and = ' AND te.exe_user_id = ' . api_get_user_id() . ' ';
  87. // get only this user's results
  88. $sql="SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").",
  89. official_code,
  90. ce.title as extitle,
  91. te.exe_result as exresult,
  92. te.exe_weighting as exweight,
  93. te.exe_date as exdate,
  94. te.exe_id as exid,
  95. email as exemail,
  96. te.start_date as exstart,
  97. steps_counter as exstep,
  98. exe_user_id as excruid,
  99. te.exe_duration as duration,
  100. ce.results_disabled as exdisabled,
  101. te.orig_lp_id as orig_lp_id,
  102. tlm.name as lp_name
  103. FROM $TBL_EXERCISES AS ce
  104. INNER JOIN $TBL_TRACK_EXERCISES AS te ON (te.exe_exo_id = ce.id)
  105. INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  106. LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm ON tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id
  107. WHERE
  108. ce.c_id = $course_id AND
  109. te.status != 'incomplete' AND
  110. te.exe_cours_id='" . Database :: escape_string($cid) . "' $user_id_and $session_id_and AND
  111. ce.active <>-1 AND
  112. ORDER BY userpart2, te.exe_cours_id ASC, ce.title ASC, te.exe_date DESC";
  113. }
  114. $results = array();
  115. $resx = Database::query($sql);
  116. $bestAttemptPerUser = array();
  117. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  118. if ($this->onlyBestAttempts) {
  119. if (!isset($bestAttemptPerUser[$rowx['excruid']])) {
  120. $bestAttemptPerUser[$rowx['excruid']] = $rowx;
  121. } else {
  122. if ($rowx['exresult'] > $bestAttemptPerUser[$rowx['excruid']]['exresult']) {
  123. $bestAttemptPerUser[$rowx['excruid']] = $rowx;
  124. }
  125. }
  126. } else {
  127. $results[] = $rowx;
  128. }
  129. }
  130. if ($this->onlyBestAttempts) {
  131. $results = $bestAttemptPerUser;
  132. }
  133. $filter_by_not_revised = false;
  134. $filter_by_revised = false;
  135. if ($filter) {
  136. switch ($filter) {
  137. case 1 :
  138. $filter_by_not_revised = true;
  139. break;
  140. case 2 :
  141. $filter_by_revised = true;
  142. break;
  143. default :
  144. null;
  145. }
  146. }
  147. if (empty($sessionId)) {
  148. $students = CourseManager::get_user_list_from_course_code($cid);
  149. } else {
  150. $students = CourseManager::get_user_list_from_course_code($cid, $sessionId);
  151. }
  152. $studentsUserIdList = array_keys($students);
  153. // Print the results of tests
  154. $userWithResults = array();
  155. if (is_array($results)) {
  156. $i = 0;
  157. foreach ($results as $result) {
  158. $revised = false;
  159. //revised or not
  160. $sql_exe = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT_RECORDING
  161. WHERE author != '' AND exe_id = ".Database :: escape_string($result['exid'])."
  162. LIMIT 1";
  163. $query = Database::query($sql_exe);
  164. if (Database:: num_rows($query) > 0) {
  165. $revised = true;
  166. }
  167. if ($filter_by_not_revised && $revised) {
  168. continue;
  169. }
  170. if ($filter_by_revised && !$revised) {
  171. continue;
  172. }
  173. $return[$i] = array();
  174. if (empty($user_id)) {
  175. $return[$i]['official_code'] = $result['official_code'];
  176. if (api_is_western_name_order()) {
  177. $return[$i]['first_name'] = $results[$i]['userpart1'];
  178. $return[$i]['last_name'] = $results[$i]['userpart2'];
  179. } else {
  180. $return[$i]['first_name'] = $results[$i]['userpart2'];
  181. $return[$i]['last_name'] = $results[$i]['userpart1'];
  182. }
  183. $return[$i]['user_id'] = $results[$i]['excruid'];
  184. $return[$i]['email'] = $results[$i]['exemail'];
  185. }
  186. $return[$i]['title'] = $result['extitle'];
  187. $return[$i]['start_date'] = api_get_local_time($result['exstart']);
  188. $return[$i]['end_date'] = api_get_local_time($result['exdate']);
  189. $return[$i]['duration'] = $result['duration'];
  190. $return[$i]['result'] = $result['exresult'];
  191. $return[$i]['max'] = $result['exweight'];
  192. $return[$i]['status'] = $revised ? get_lang('Validated') : get_lang('NotValidated');
  193. $return[$i]['lp_id'] = $result['orig_lp_id'];
  194. $return[$i]['lp_name'] = $result['lp_name'];
  195. if (in_array($result['excruid'], $studentsUserIdList)) {
  196. $return[$i]['is_user_subscribed'] = get_lang('Yes');
  197. } else {
  198. $return[$i]['is_user_subscribed'] = get_lang('No');
  199. }
  200. $userWithResults[$result['excruid']] = 1;
  201. $i++;
  202. }
  203. }
  204. if ($this->includeAllUsers) {
  205. $latestId = count($return);
  206. $userWithResults = array_keys($userWithResults);
  207. if (!empty($students)) {
  208. foreach ($students as $student) {
  209. if (!in_array($student['user_id'], $userWithResults)) {
  210. $i = $latestId;
  211. $isWestern = api_is_western_name_order();
  212. if (empty($user_id)) {
  213. $return[$i]['official_code'] = $student['official_code'];
  214. if ($isWestern) {
  215. $return[$i]['first_name'] = $student['firstname'];
  216. $return[$i]['last_name'] = $student['lastname'];
  217. } else {
  218. $return[$i]['first_name'] = $student['lastname'];
  219. $return[$i]['last_name'] = $student['firstname'];
  220. }
  221. $return[$i]['user_id'] = $student['user_id'];
  222. $return[$i]['email'] = $student['email'];
  223. }
  224. $return[$i]['title'] = null;
  225. $return[$i]['start_date'] = null;
  226. $return[$i]['end_date'] = null;
  227. $return[$i]['duration'] = null;
  228. $return[$i]['result'] = null;
  229. $return[$i]['max'] = null;
  230. $return[$i]['status'] = get_lang('NotAttempted');
  231. $return[$i]['lp_id'] = null;
  232. $return[$i]['lp_name'] = null;
  233. $return[$i]['is_user_subscribed'] = get_lang('Yes');
  234. $latestId++;
  235. }
  236. }
  237. }
  238. }
  239. $this->results = $return;
  240. return true;
  241. }
  242. /**
  243. * Exports the complete report as a CSV file
  244. * @param string Document path inside the document tool
  245. * @param integer Optional user ID
  246. * @param boolean Whether to include user fields or not
  247. * @return boolean False on error
  248. */
  249. public function exportCompleteReportCSV(
  250. $document_path = '',
  251. $user_id = null,
  252. $export_user_fields = false,
  253. $export_filter = 0,
  254. $exercise_id = 0,
  255. $hotpotato_name = null
  256. ) {
  257. global $charset;
  258. $this->getExercisesReporting($document_path, $user_id, $export_filter, $exercise_id, $hotpotato_name);
  259. $filename = 'exercise_results_'.date('YmdGis').'.csv';
  260. if(!empty($user_id)) {
  261. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.csv';
  262. }
  263. $data = '';
  264. if (api_is_western_name_order()) {
  265. if(!empty($this->results[0]['first_name'])) {
  266. $data .= get_lang('FirstName').';';
  267. }
  268. if(!empty($this->results[0]['last_name'])) {
  269. $data .= get_lang('LastName').';';
  270. }
  271. } else {
  272. if(!empty($this->results[0]['last_name'])) {
  273. $data .= get_lang('LastName').';';
  274. }
  275. if(!empty($this->results[0]['first_name'])) {
  276. $data .= get_lang('FirstName').';';
  277. }
  278. }
  279. $officialCodeInList = api_get_configuration_value('show_official_code_exercise_result_list');
  280. if ($officialCodeInList) {
  281. $data .= get_lang('OfficialCode').';';
  282. }
  283. $data .= get_lang('Email').';';
  284. $data .= get_lang('Groups').';';
  285. if ($export_user_fields) {
  286. //show user fields section with a big th colspan that spans over all fields
  287. $extra_user_fields = UserManager::get_extra_fields(0,1000,5,'ASC',false, 1);
  288. $num = count($extra_user_fields);
  289. foreach($extra_user_fields as $field) {
  290. $data .= '"'.str_replace("\r\n",' ',api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset)).'";';
  291. }
  292. }
  293. $data .= get_lang('Title').';';
  294. $data .= get_lang('StartDate').';';
  295. $data .= get_lang('EndDate').';';
  296. $data .= get_lang('Duration'). ' ('.get_lang('MinMinutes').') ;';
  297. $data .= get_lang('Score').';';
  298. $data .= get_lang('Total').';';
  299. $data .= get_lang('Status').';';
  300. $data .= get_lang('ToolLearnpath').';';
  301. $data .= get_lang('UserIsCurrentlySubscribed').';';
  302. $data .= "\n";
  303. //results
  304. foreach ($this->results as $row) {
  305. if (api_is_western_name_order()) {
  306. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
  307. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
  308. } else {
  309. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset)).';';
  310. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset)).';';
  311. }
  312. if ($officialCodeInList) {
  313. $data .= $row['official_code'].';';
  314. }
  315. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset)).';';
  316. $data .= str_replace("\r\n",' ',implode(", ", GroupManager :: get_user_group_name($row['user_id']))).';';
  317. if ($export_user_fields) {
  318. //show user fields data, if any, for this user
  319. $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true);
  320. foreach($user_fields_values as $value) {
  321. $data .= '"'.str_replace('"','""',api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)).'";';
  322. }
  323. }
  324. $data .= str_replace("\r\n",' ',api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset)).';';
  325. $data .= str_replace("\r\n",' ',$row['start_date']).';';
  326. $data .= str_replace("\r\n",' ',$row['end_date']).';';
  327. $data .= str_replace("\r\n",' ',$row['duration']).';';
  328. $data .= str_replace("\r\n",' ',$row['result']).';';
  329. $data .= str_replace("\r\n",' ',$row['max']).';';
  330. $data .= str_replace("\r\n",' ',$row['status']).';';
  331. $data .= str_replace("\r\n",' ',$row['lp_name']).';';
  332. $data .= str_replace("\r\n",' ',$row['is_user_subscribed']).';';
  333. $data .= "\n";
  334. }
  335. //output the results
  336. $len = strlen($data);
  337. header('Content-type: application/octet-stream');
  338. header('Content-Type: application/force-download');
  339. header('Content-length: '.$len);
  340. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  341. header('Content-Disposition: filename= '.$filename);
  342. } else {
  343. header('Content-Disposition: attachment; filename= '.$filename);
  344. }
  345. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  346. header('Pragma: ');
  347. header('Cache-Control: ');
  348. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  349. }
  350. header('Content-Description: '.$filename);
  351. header('Content-transfer-encoding: binary');
  352. echo $data;
  353. return true;
  354. }
  355. /**
  356. * Exports the complete report as an XLS file
  357. * @return boolean False on error
  358. */
  359. public function exportCompleteReportXLS(
  360. $document_path = '',
  361. $user_id = null,
  362. $export_user_fields = false,
  363. $export_filter = 0,
  364. $exercise_id = 0,
  365. $hotpotato_name = null
  366. ) {
  367. global $charset;
  368. $this->getExercisesReporting($document_path, $user_id, $export_filter, $exercise_id, $hotpotato_name);
  369. $filename = 'exercise_results_'.date('YmdGis').'.xls';
  370. if (!empty($user_id)) {
  371. $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.xls';
  372. }
  373. $workbook = new Spreadsheet_Excel_Writer();
  374. $workbook->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  375. $workbook->setVersion(8); // BIFF8
  376. $workbook->send($filename);
  377. $worksheet =& $workbook->addWorksheet('Report '.date('YmdGis'));
  378. $worksheet->setInputEncoding(api_get_system_encoding());
  379. $line = 0;
  380. $column = 0; //skip the first column (row titles)
  381. // check if exists column 'user'
  382. $with_column_user = false;
  383. foreach ($this->results as $result) {
  384. if (!empty($result['last_name']) && !empty($result['first_name'])) {
  385. $with_column_user = true;
  386. break;
  387. }
  388. }
  389. $officialCodeInList = api_get_configuration_value('show_official_code_exercise_result_list');
  390. if ($with_column_user) {
  391. if (api_is_western_name_order()) {
  392. $worksheet->write($line, $column,get_lang('FirstName'));
  393. $column++;
  394. $worksheet->write($line, $column,get_lang('LastName'));
  395. $column++;
  396. } else {
  397. $worksheet->write($line, $column,get_lang('LastName'));
  398. $column++;
  399. $worksheet->write($line, $column,get_lang('FirstName'));
  400. $column++;
  401. }
  402. if ($officialCodeInList) {
  403. $worksheet->write($line, $column, get_lang('OfficialCode'));
  404. $column++;
  405. }
  406. $worksheet->write($line, $column, get_lang('Email'));
  407. $column++;
  408. }
  409. $worksheet->write($line, $column, get_lang('Groups'));
  410. $column++;
  411. if ($export_user_fields) {
  412. //show user fields section with a big th colspan that spans over all fields
  413. $extra_user_fields = UserManager::get_extra_fields(0,1000,5,'ASC',false, 1);
  414. //show the fields names for user fields
  415. foreach ($extra_user_fields as $field) {
  416. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset));
  417. $column++;
  418. }
  419. }
  420. $worksheet->write($line, $column, get_lang('Title'));
  421. $column++;
  422. $worksheet->write($line, $column, get_lang('StartDate'));
  423. $column++;
  424. $worksheet->write($line, $column, get_lang('EndDate'));
  425. $column++;
  426. $worksheet->write($line, $column, get_lang('Duration').' ('.get_lang('MinMinutes').')');
  427. $column++;
  428. $worksheet->write($line, $column, get_lang('Score'));
  429. $column++;
  430. $worksheet->write($line, $column, get_lang('Total'));
  431. $column++;
  432. $worksheet->write($line, $column, get_lang('Status'));
  433. $column++;
  434. $worksheet->write($line, $column, get_lang('ToolLearnpath'));
  435. $column++;
  436. $worksheet->write($line, $column, get_lang('UserIsCurrentlySubscribed'));
  437. $line++;
  438. foreach ($this->results as $row) {
  439. $column = 0;
  440. if ($with_column_user) {
  441. if (api_is_western_name_order()) {
  442. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset));
  443. $column++;
  444. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset));
  445. $column++;
  446. } else {
  447. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['last_name']), ENT_QUOTES, $charset));
  448. $column++;
  449. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['first_name']), ENT_QUOTES, $charset));
  450. $column++;
  451. }
  452. if ($officialCodeInList) {
  453. $worksheet->write($line, $column,api_html_entity_decode(strip_tags($row['official_code']), ENT_QUOTES, $charset));
  454. $column++;
  455. }
  456. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['email']), ENT_QUOTES, $charset));
  457. $column++;
  458. }
  459. $worksheet->write($line,$column,api_html_entity_decode(strip_tags(implode(", ", GroupManager :: get_user_group_name($row['user_id']))), ENT_QUOTES, $charset));
  460. $column++;
  461. if ($export_user_fields) {
  462. //show user fields data, if any, for this user
  463. $user_fields_values = UserManager::get_extra_user_data($row['user_id'],false,false, false, true);
  464. foreach($user_fields_values as $value) {
  465. $worksheet->write($line,$column, api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset));
  466. $column++;
  467. }
  468. }
  469. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['title']), ENT_QUOTES, $charset));
  470. $column++;
  471. $worksheet->write($line, $column, $row['start_date']);
  472. $column++;
  473. $worksheet->write($line, $column, $row['end_date']);
  474. $column++;
  475. $worksheet->write($line, $column, $row['duration']);
  476. $column++;
  477. $worksheet->write($line, $column, $row['result']);
  478. $column++;
  479. $worksheet->write($line, $column, $row['max']);
  480. $column++;
  481. $worksheet->write($line, $column, $row['status']);
  482. $column++;
  483. $worksheet->write($line, $column, $row['lp_name']);
  484. $column++;
  485. $worksheet->write($line, $column, $row['is_user_subscribed']);
  486. $line++;
  487. }
  488. //output the results
  489. $workbook->close();
  490. return true;
  491. }
  492. }