courseLogCSV.php 16 KB

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