courseLogCSV.php 16 KB

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