courseLogCSV.php 17 KB

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