courseLogCSV.php 16 KB

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