courseLogCSV.php 16 KB

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