courseLogCSV.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2006 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Author: Frederic Vauthier (frederic.vauthier@dokeos.com)
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  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. See the GNU General Public License for more details.
  16. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * @author Thomas Depraetere
  23. * @author Hugues Peeters
  24. * @author Christophe Gesche
  25. * @author Sebastien Piraux
  26. * @author Toon Keppens (Vi-Host.net)
  27. *
  28. * @package dokeos.tracking
  29. ==============================================================================
  30. */
  31. /*
  32. ==============================================================================
  33. INIT SECTION
  34. ==============================================================================
  35. */
  36. $pathopen = isset($_REQUEST['pathopen']) ? $_REQUEST['pathopen'] : null;
  37. // name of the language file that needs to be included
  38. $language_file = "tracking";
  39. include('../inc/global.inc.php');
  40. //includes for SCORM and LP
  41. require_once('../newscorm/learnpath.class.php');
  42. require_once('../newscorm/learnpathItem.class.php');
  43. require_once('../newscorm/scorm.class.php');
  44. require_once('../newscorm/scormItem.class.php');
  45. // charset determination
  46. if ($_GET['scormcontopen'])
  47. {
  48. $tbl_lp = Database::get_course_table('lp');
  49. $contopen = (int) $_GET['scormcontopen'];
  50. $sql = "SELECT default_encoding FROM $tbl_lp WHERE id = ".$contopen;
  51. $res = api_sql_query($sql,__FILE__,__LINE__);
  52. $row = Database::fetch_array($res);
  53. $lp_charset = $row['default_encoding'];
  54. //header('Content-Type: text/html; charset='. $row['default_encoding']);
  55. }
  56. /*
  57. -----------------------------------------------------------
  58. Constants and variables
  59. -----------------------------------------------------------
  60. */
  61. // regroup table names for maintenance purpose
  62. $TABLETRACK_ACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  63. $TABLETRACK_LINKS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  64. $TABLETRACK_DOWNLOADS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  65. $TABLETRACK_ACCESS_2 = Database::get_statistic_table("track_e_access");
  66. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  67. $TABLECOURSE = Database::get_main_table(TABLE_MAIN_COURSE);
  68. $TABLECOURSE_LINKS = Database::get_course_table(TABLE_LINK);
  69. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  70. //$table_scormdata = Database::get_scorm_table(TABLE_SCORM_SCO_DATA);
  71. //$table_scormmain = Database::get_scorm_table(TABLE_SCORM_MAIN);
  72. //$tbl_learnpath_main = Database::get_course_table(TABLE_LEARNPATH_MAIN);
  73. //$tbl_learnpath_item = Database::get_course_table(TABLE_LEARNPATH_ITEM);
  74. //$tbl_learnpath_chapter = Database::get_course_table(TABLE_LEARNPATH_CHAPTER);
  75. $tbl_learnpath_main = Database::get_course_table('lp');
  76. $tbl_learnpath_item = Database::get_course_table('lp_item');
  77. $tbl_learnpath_view = Database::get_course_table('lp_view');
  78. $tbl_learnpath_item_view = Database::get_course_table('lp_item_view');
  79. $view = $_REQUEST['view'];
  80. if($view=="0000001") $nameTools=get_lang('SynthesisView');
  81. if($view=="1000000") $nameTools=get_lang('CourseStats');
  82. if($view=="0100000") $nameTools=get_lang('CourseAccess');
  83. if($view=="0010000") $nameTools=get_lang('ToolsAccess');
  84. if($view=="0001000") $nameTools=get_lang('LinksAccess');
  85. if($view=="0000100") $nameTools=get_lang('DocumentsAccess');
  86. if($view=="00000010") $nameTools=get_lang('ScormAccess');
  87. $interbreadcrumb[] = array ("url" => api_get_self()."?view=0000000", "name" => get_lang('ToolName'));
  88. include(api_get_path(LIBRARY_PATH)."statsUtils.lib.inc.php");
  89. include("../resourcelinker/resourcelinker.inc.php");
  90. $is_allowedToTrack = $is_courseAdmin || $is_platformAdmin;
  91. /*
  92. ==============================================================================
  93. MAIN CODE
  94. ==============================================================================
  95. */
  96. $title[0]=get_lang('StatsOfCourse')." : ".$_course['official_code'];
  97. // check if uid is prof of this group
  98. if($is_allowedToTrack && $_configuration['tracking_enabled'])
  99. {
  100. // show all : view must be equal to the sum of all view values (1024+512+...+64)
  101. // show none : less than the tiniest value
  102. /*echo "<div>
  103. [<a href='".api_get_self()."?view=1111111'>".get_lang('ShowAll')."</a>]
  104. [<a href='".api_get_self()."?view=0000000'>".get_lang('ShowNone')."</a>]
  105. </div><br>
  106. ";*/
  107. if(!isset($view)) $view ="0000000";
  108. /***************************************************************************
  109. *
  110. * Reporting
  111. *
  112. ***************************************************************************/
  113. $tempView = $view;
  114. if($view[6] == '1'){
  115. $tempView[6] = '0';
  116. //--------------------------------BEGIN users in this course
  117. $sql = "SELECT $TABLECOURSUSER.`user_id`, $table_user.`lastname`, $table_user.`firstname`
  118. FROM $TABLECOURSUSER, $table_user
  119. WHERE $TABLECOURSUSER.course_code = '".$_cid."' AND $TABLECOURSUSER.`user_id` = $table_user.`user_id`
  120. ORDER BY $table_user.`lastname`";
  121. $results = getManyResults3Col($sql);
  122. //BUGFIX: get visual code instead of real course code. Scormpaths use the visual code... (should be fixed in future versions)
  123. $sql = "SELECT visual_code FROM $TABLECOURSE WHERE code = '".$_cid."'";
  124. $_course['visual_code'] = getOneResult($sql);
  125. if (is_array($results))
  126. {
  127. $line='';
  128. $title_line = get_lang('Name').";".get_lang('FirstAccess').";".get_lang('LastAccess').";".get_lang('Visited')."\n";
  129. for($j = 0 ; $j < count($results) ; $j++)
  130. {
  131. //--------------------------------BEGIN % visited
  132. // sum of all items (= multiple learningpaths + SCORM imported paths)
  133. $sql = "SELECT COUNT(DISTINCT(iv.lp_item_id)) " .
  134. "FROM $tbl_learnpath_item_view iv " .
  135. "INNER JOIN $tbl_learnpath_view v ON iv.lp_view_id = v.id " .
  136. "WHERE v.user_id = ".$results[$j][0];
  137. $total_lpath_items = getOneResult($sql);
  138. // sum of all completed items (= multiple learningpaths + SCORM imported paths)
  139. $sql = "SELECT COUNT(DISTINCT(iv.lp_item_id)) " .
  140. "FROM $tbl_learnpath_item_view iv " .
  141. "INNER JOIN $tbl_learnpath_view v ON iv.lp_view_id = v.id " .
  142. "WHERE v.user_id = ".$results[$j][0]." " .
  143. "AND (status = 'completed' OR status='passed')";
  144. $total_lpath_items_completed = getOneResult($sql);
  145. // calculation & bgcolor setting
  146. $lpath_pct_completed = empty($total_lpath_items) ? "-" : round(($total_lpath_items_completed / $total_lpath_items) * 100);
  147. //--------------------------------END % visited
  148. //--------------------------------BEGIN first/last access
  149. // first access
  150. $sql = "SELECT access_date FROM $TABLETRACK_ACCESS_2 WHERE `access_user_id` = '".$results[$j][0]."' AND `access_cours_code` = '".$_course['official_code']."' AND `access_tool` = 'learnpath' ORDER BY access_id ASC LIMIT 1";
  151. $first_access = getOneResult($sql);
  152. $first_access = empty($first_access) ? "-" : date('d.m.y',strtotime($first_access));
  153. // last access
  154. $sql = "SELECT access_date FROM $TABLETRACK_ACCESS WHERE `access_user_id` = '".$results[$j][0]."' AND `access_cours_code` = '".$_course['official_code']."' AND `access_tool` = 'learnpath'";
  155. $last_access = getOneResult($sql);
  156. $last_access = empty($last_access) ? "-" : date('d.m.y',strtotime($last_access));
  157. //--------------------------------END first/last access
  158. //--------------------------------BEGIN presentation of data
  159. $line .= $results[$j][1]." ".$results[$j][2].";".$first_access.";".$last_access.";".$lpath_pct_completed."\n";
  160. //--------------------------------END presentation of data
  161. }
  162. }
  163. else
  164. {
  165. $line = get_lang('NoResult')."\n";
  166. }
  167. }
  168. /***************************************************************************
  169. *
  170. * Main
  171. *
  172. ***************************************************************************/
  173. $tempView = $view;
  174. if($view[0] == '1')
  175. {
  176. $title[1]= $nameTools;
  177. $tempView[0] = '0';
  178. $sql = "SELECT count(*)
  179. FROM $TABLECOURSUSER
  180. WHERE course_code = '".$_cid."'";
  181. $count = getOneResult($sql);
  182. $title_line = get_lang('CountUsers')." ; ".$count."\n";
  183. }
  184. /***************************************************************************
  185. *
  186. * Access to this course
  187. *
  188. ***************************************************************************/
  189. $tempView = $view;
  190. if($view[1] == '1'){
  191. $tempView[1] = '0';
  192. $title[1]=get_lang('ConnectionsToThisCourse');
  193. $title_line = '';
  194. $line = '';
  195. //Total
  196. $sql = "SELECT count(*)
  197. FROM $TABLETRACK_ACCESS
  198. WHERE access_cours_code = '".$_cid."'
  199. AND access_tool IS NULL";
  200. $count = getOneResult($sql);
  201. $line .= get_lang('CountToolAccess')." ; ".$count."\n";
  202. // last 31 days
  203. $sql = "SELECT count(*)
  204. FROM $TABLETRACK_ACCESS
  205. WHERE `access_cours_code` = '$_cid'
  206. AND (access_date > DATE_ADD(CURDATE(), INTERVAL -31 DAY))
  207. AND access_tool IS NULL";
  208. $count = getOneResult($sql);
  209. $line .= get_lang('Last31days')." ; ".$count."\n";
  210. // last 7 days
  211. $sql = "SELECT count(*)
  212. FROM $TABLETRACK_ACCESS
  213. WHERE `access_cours_code` = '$_cid'
  214. AND (access_date > DATE_ADD(CURDATE(), INTERVAL -7 DAY))
  215. AND access_tool IS NULL";
  216. $count = getOneResult($sql);
  217. $line .= get_lang('Last7days')." ; ".$count."\n";
  218. // today
  219. $sql = "SELECT count(*)
  220. FROM $TABLETRACK_ACCESS
  221. WHERE `access_cours_code` = '$_cid'
  222. AND ( access_date > CURDATE() )
  223. AND access_tool IS NULL";
  224. $count = getOneResult($sql);
  225. $line .= get_lang('Thisday')." ; ".$count."\n";
  226. }
  227. /***************************************************************************
  228. *
  229. * Tools
  230. *
  231. ***************************************************************************/
  232. $tempView = $view;
  233. if($view[2] == '1'){
  234. $tempView[2] = '0';
  235. $title[1]= $nameTools;
  236. $line ='';
  237. $title_line = get_lang('ToolTitleToolnameColumn').";".get_lang('ToolTitleUsersColumn').";".get_lang('ToolTitleCountColumn')."\n";
  238. $sql = "SELECT `access_tool`, COUNT(DISTINCT `access_user_id`),count( `access_tool` )
  239. FROM $TABLETRACK_ACCESS
  240. WHERE `access_tool` IS NOT NULL
  241. AND `access_cours_code` = '$_cid'
  242. GROUP BY `access_tool`";
  243. $results = getManyResults3Col($sql);
  244. if (is_array($results))
  245. {
  246. for($j = 0 ; $j < count($results) ; $j++)
  247. {
  248. $line .= $results[$j][0]."/".get_lang($results[$j][0]).";".$results[$j][1].";".$results[$j][2]."\n";
  249. }
  250. }
  251. else
  252. {
  253. $line = get_lang('NoResult')."\n";
  254. }
  255. }
  256. /***************************************************************************
  257. *
  258. * Links
  259. *
  260. ***************************************************************************/
  261. $tempView = $view;
  262. if($view[3] == '1'){
  263. $tempView[3] = '0';
  264. $sql = "SELECT `cl`.`title`, `cl`.`url`,count(DISTINCT `sl`.`links_user_id`), count(`cl`.`title`)
  265. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  266. WHERE `sl`.`links_link_id` = `cl`.`id`
  267. AND `sl`.`links_cours_id` = '$_cid'
  268. GROUP BY `cl`.`title`, `cl`.`url`";
  269. $results = getManyResultsXCol($sql,4);
  270. $title[1]= $nameTools;
  271. $line='';
  272. $title_line = get_lang('LinksTitleLinkColumn').";".get_lang('LinksTitleUsersColumn').";".get_lang('LinksTitleCountColumn')."\n";
  273. if (is_array($results))
  274. {
  275. for($j = 0 ; $j < count($results) ; $j++)
  276. {
  277. $line .= $results[$j][1]."'>".$results[$j][0].";".$results[$j][2].";".$results[$j][3]."\n";
  278. }
  279. }
  280. else
  281. {
  282. $line = get_lang('NoResult')."\n";
  283. }
  284. }
  285. /***************************************************************************
  286. *
  287. * Documents
  288. *
  289. ***************************************************************************/
  290. $tempView = $view;
  291. if($view[4] == '1'){
  292. $tempView[4] = '0';
  293. $sql = "SELECT `down_doc_path`, COUNT(DISTINCT `down_user_id`), COUNT(`down_doc_path`)
  294. FROM $TABLETRACK_DOWNLOADS
  295. WHERE `down_cours_id` = '$_cid'
  296. GROUP BY `down_doc_path`";
  297. $results = getManyResults3Col($sql);
  298. $title[1]= $nameTools;
  299. $line='';
  300. $title_line = get_lang('DocumentsTitleDocumentColumn').";".get_lang('DocumentsTitleUsersColumn').";".get_lang('DocumentsTitleCountColumn')."\n";
  301. if (is_array($results))
  302. {
  303. for($j = 0 ; $j < count($results) ; $j++)
  304. {
  305. $line .= $results[$j][0].";".$results[$j][1].";".$results[$j][2]."\n";
  306. }
  307. }
  308. else
  309. {
  310. $line=get_lang('NoResult')."\n";
  311. }
  312. }
  313. /***************************************************************************
  314. *
  315. * Scorm contents and Learning Path
  316. *
  317. ***************************************************************************/
  318. $tempView = $view;
  319. if($view[5] == '1'){
  320. $tempView[5] = '0';
  321. $sql = "SELECT id, name
  322. FROM $tbl_learnpath_main";
  323. //WHERE dokeosCourse='$_cid'"; we are using a table inside the course now, so no need for course id
  324. $result=api_sql_query($sql,__FILE__,__LINE__);
  325. $ar=Database::fetch_array($result);
  326. $title[1]= $nameTools;
  327. $line='';
  328. $title_line = get_lang('ScormContentColumn');
  329. $scormcontopen=$_REQUEST["scormcontopen"];
  330. $scormstudentopen=$_REQUEST["scormstudentopen"];
  331. if (is_array($ar)){
  332. while ($ar['id'] != '') {
  333. $lp_title = stripslashes($ar['name']);
  334. //echo "<a href='".api_get_self()."?view=".$view."&scormcontopen=".$ar['id']."' class='specialLink'>$lp_title</a>";
  335. if ($ar['id']==$scormcontopen) { //have to list the students here
  336. $contentId=$ar['id'];
  337. $sql2 = "SELECT u.user_id, u.lastname, u.firstname " .
  338. "FROM $tbl_learnpath_view sd " .
  339. "INNER JOIN $table_user u " .
  340. "ON u.user_id = sd.user_id " .
  341. "WHERE sd.lp_id=$contentId group by u.user_id";
  342. //error_log($sql2,0);
  343. $result2=api_sql_query($sql2,__FILE__,__LINE__);
  344. if(mysql_num_rows($result2)>0){
  345. $ar2=Database::fetch_array($result2);
  346. while ($ar2 != '') {
  347. if (isset($_REQUEST["scormstudentopen"]) && $ar2['user_id']==$scormstudentopen) {
  348. $line .= $ar['id']." ".$ar2['user_id']." ".$ar2['lastname']." ".$ar2['firstname'];
  349. }
  350. else{
  351. $line .= $ar['id']." ".$ar2['user_id']." ".$ar2['lastname']." ".$ar2['firstname'];
  352. }
  353. if ($ar2['user_id']==$scormstudentopen) { //have to list the student's results
  354. $studentId=$ar2['user_id'];
  355. $sql3 = "SELECT iv.status, iv.score, i.title, iv.total_time " .
  356. "FROM $tbl_learnpath_item i " .
  357. "INNER JOIN $tbl_learnpath_item_view iv ON i.id=iv.lp_item_id " .
  358. "INNER JOIN $tbl_learnpath_view v ON iv.lp_view_id=v.id " .
  359. "WHERE (v.user_id=$studentId and v.lp_id=$contentId) ORDER BY v.id, i.id";
  360. $result3=api_sql_query($sql3,__FILE__,__LINE__);
  361. $ar3=Database::fetch_array($result3);
  362. $title_line .= get_lang('ScormTitleColumn').";".get_lang('ScormStatusColumn').";".get_lang('ScormScoreColumn').";".get_lang('ScormTimeColumn');
  363. while ($ar3['status'] != '') {
  364. require_once('../newscorm/learnpathItem.class.php');
  365. $time = learnpathItem::get_scorm_time('php',$ar3['total_time']);
  366. $title = htmlentities($ar3['title'],ENT_QUOTES,$lp_charset);
  367. $line .= $title.";".$ar3['status'].";".$ar3['score'].";".$time;
  368. $ar3=Database::fetch_array($result3);
  369. }
  370. }
  371. $line .= "\n";
  372. $ar2=Database::fetch_array($result2);
  373. }
  374. $title_line .= "\n";
  375. }
  376. }
  377. $ar=Database::fetch_array($result);
  378. }
  379. }
  380. }
  381. /***************************************************************************
  382. *
  383. * Export to a CSV file
  384. * force the browser to save the file instead of opening it
  385. ***************************************************************************/
  386. $len = strlen($title_line.$line);
  387. header('Content-type: application/octet-stream');
  388. //header('Content-Type: application/force-download');
  389. header('Content-length: '.$len);
  390. $filename = html_entity_decode(str_replace(":","",str_replace(" ","_", $title[0].'_'.$title[1].'.csv')));
  391. if(preg_match("/MSIE 5.5/",$_SERVER['HTTP_USER_AGENT']))
  392. {
  393. header('Content-Disposition: filename= '.$filename);
  394. }
  395. else
  396. {
  397. header('Content-Disposition: attachment; filename= '.$filename);
  398. }
  399. if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
  400. {
  401. header('Pragma: ');
  402. header('Cache-Control: ');
  403. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  404. }
  405. header('Content-Description: '.$filename);
  406. header('Content-transfer-encoding: binary');
  407. echo html_entity_decode($title_line);
  408. echo html_entity_decode($line);
  409. exit;
  410. }
  411. // not allowed
  412. else
  413. {
  414. if(!$_configuration['tracking_enabled'])
  415. {
  416. echo get_lang('TrackingDisabled');
  417. }
  418. else
  419. {
  420. api_not_allowed();
  421. }
  422. }
  423. ?>