courseLogCSV.php 18 KB

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