courseLogCSV.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. $is_allowedToTrack = $is_courseAdmin || $is_platformAdmin || api_is_drh();
  48. /* MAIN CODE */
  49. $title[0] = get_lang('StatsOfCourse') . " : " . $_course['official_code'];
  50. $courseInfo = api_get_course_info($_course['official_code']);
  51. $courseId = $courseInfo['real_id'];
  52. // check if uid is prof of this group
  53. if ($is_allowedToTrack) {
  54. // show all : view must be equal to the sum of all view values (1024+512+...+64)
  55. // show none : less than the tiniest value
  56. /* echo "<div>
  57. [<a href='".api_get_self()."?view=1111111'>".get_lang('ShowAll')."</a>]
  58. [<a href='".api_get_self()."?view=0000000'>".get_lang('ShowNone')."</a>]
  59. </div><br>
  60. "; */
  61. if (!isset($view))
  62. $view = "0000000";
  63. /* Reporting */
  64. $tempView = $view;
  65. if ($view[6] == '1') {
  66. $tempView[6] = '0';
  67. // BEGIN users in this course
  68. $sql = "SELECT $TABLECOURSUSER.user_i, $table_user.lastname, $table_user.firstname
  69. FROM $TABLECOURSUSER, $table_user
  70. WHERE
  71. $TABLECOURSUSER.c_id = '" . api_get_course_int_id() . "' AND
  72. $TABLECOURSUSER.user_id = $table_user.user_id AND
  73. $TABLECOURSUSER.relation_type<>" . COURSE_RELATION_TYPE_RRHH . "
  74. ORDER BY $table_user.lastname";
  75. $results = StatsUtils::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 = '" . api_get_course_id() . "'";
  78. $_course['visual_code'] = StatsUtils::getOneResult($sql);
  79. if (is_array($results)) {
  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 = StatsUtils::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 = StatsUtils::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
  109. WHERE access_user_id = '" . $results[$j][0] . "' AND c_id = '" . $courseId . "' AND access_tool = 'learnpath' AND access_session_id = '" . api_get_session_id() . "'
  110. ORDER BY access_id ASC LIMIT 1";
  111. $first_access = StatsUtils::getOneResult($sql);
  112. $first_access = empty($first_access) ? "-" : date('d.m.y', strtotime($first_access));
  113. // last access
  114. $sql = "SELECT access_date FROM $TABLETRACK_ACCESS WHERE access_user_id = '" . $results[$j][0] . "' AND c_id = '" . $courseId . "' AND access_tool = 'learnpath'";
  115. $last_access = StatsUtils::getOneResult($sql);
  116. $last_access = empty($last_access) ? "-" : date('d.m.y', strtotime($last_access));
  117. // END first/last access
  118. // BEGIN presentation of data
  119. $line .= $results[$j][1] . " " . $results[$j][2] . ";" . $first_access . ";" . $last_access . ";" . $lpath_pct_completed . "\n";
  120. // END presentation of data
  121. }
  122. } else {
  123. $line = get_lang('NoResult') . "\n";
  124. }
  125. }
  126. /* Main */
  127. $tempView = $view;
  128. if ($view[0] == '1') {
  129. $title[1] = $nameTools;
  130. $tempView[0] = '0';
  131. $sql = "SELECT count(*)
  132. FROM $TABLECOURSUSER
  133. WHERE c_id = '" . api_get_course_int_id() . "' AND relation_type<>" . COURSE_RELATION_TYPE_RRHH . "";
  134. $count = StatsUtils::getOneResult($sql);
  135. $title_line = get_lang('CountUsers') . " ; " . $count . "\n";
  136. }
  137. /* Access to this course */
  138. $tempView = $view;
  139. if ($view[1] == '1') {
  140. $tempView[1] = '0';
  141. $title[1] = get_lang('ConnectionsToThisCourse');
  142. $title_line = '';
  143. $line = '';
  144. //Total
  145. $sql = "SELECT count(*)
  146. FROM $TABLETRACK_ACCESS
  147. WHERE c_id = $courseId
  148. AND access_tool IS NULL";
  149. $count = StatsUtils::getOneResult($sql);
  150. $line .= get_lang('CountToolAccess') . " ; " . $count . "\n";
  151. // last 31 days
  152. $sql = "SELECT count(*)
  153. FROM $TABLETRACK_ACCESS
  154. WHERE c_id = $courseId
  155. AND (access_date > DATE_ADD(CURDATE(), INTERVAL -31 DAY))
  156. AND access_tool IS NULL";
  157. $count = StatsUtils::getOneResult($sql);
  158. $line .= get_lang('Last31days') . " ; " . $count . "\n";
  159. // last 7 days
  160. $sql = "SELECT count(*)
  161. FROM $TABLETRACK_ACCESS
  162. WHERE c_id = $courseId
  163. AND (access_date > DATE_ADD(CURDATE(), INTERVAL -7 DAY))
  164. AND access_tool IS NULL";
  165. $count = StatsUtils::getOneResult($sql);
  166. $line .= get_lang('Last7days') . " ; " . $count . "\n";
  167. // today
  168. $sql = "SELECT count(*)
  169. FROM $TABLETRACK_ACCESS
  170. WHERE c_id = $courseId
  171. AND ( access_date > CURDATE() )
  172. AND access_tool IS NULL";
  173. $count = StatsUtils::getOneResult($sql);
  174. $line .= get_lang('ThisDay') . " ; " . $count . "\n";
  175. }
  176. /* Tools */
  177. $tempView = $view;
  178. if ($view[2] == '1') {
  179. $tempView[2] = '0';
  180. $title[1] = $nameTools;
  181. $line = '';
  182. $title_line = get_lang('ToolTitleToolnameColumn') . ";" . get_lang('ToolTitleUsersColumn') . ";" . get_lang('ToolTitleCountColumn') . "\n";
  183. $sql = "SELECT access_tool, COUNT(DISTINCT access_user_id),count( access_tool )
  184. FROM $TABLETRACK_ACCESS
  185. WHERE access_tool IS NOT NULL
  186. AND c_id = $courseId
  187. GROUP BY access_tool";
  188. $results = StatsUtils::getManyResults3Col($sql);
  189. if (is_array($results)) {
  190. for ($j = 0; $j < count($results); $j++) {
  191. $line .= $results[$j][0] . "/" . get_lang($results[$j][0]) . ";" . $results[$j][1] . ";" . $results[$j][2] . "\n";
  192. }
  193. } else {
  194. $line = get_lang('NoResult') . "\n";
  195. }
  196. }
  197. /* Links */
  198. $tempView = $view;
  199. if ($view[3] == '1') {
  200. $tempView[3] = '0';
  201. $sql = "SELECT cl.title, cl.url,count(DISTINCT sl.links_user_id), count(cl.title)
  202. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  203. WHERE
  204. cl.c_id = $courseId AND
  205. sl.links_link_id = cl.id AND
  206. sl.c_id = $courseId
  207. GROUP BY cl.title, cl.url";
  208. $results = StatsUtils::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. for ($j = 0; $j < count($results); $j++) {
  214. $line .= $results[$j][1] . "'>" . $results[$j][0] . ";" . $results[$j][2] . ";" . $results[$j][3] . "\n";
  215. }
  216. } else {
  217. $line = get_lang('NoResult') . "\n";
  218. }
  219. }
  220. /* Documents */
  221. $tempView = $view;
  222. if ($view[4] == '1') {
  223. $tempView[4] = '0';
  224. $sql = "SELECT down_doc_path, COUNT(DISTINCT down_user_id), COUNT(down_doc_path)
  225. FROM $TABLETRACK_DOWNLOADS
  226. WHERE c_id = $courseId
  227. GROUP BY down_doc_path";
  228. $results = StatsUtils::getManyResults3Col($sql);
  229. $title[1] = $nameTools;
  230. $line = '';
  231. $title_line = get_lang('DocumentsTitleDocumentColumn') . ";" . get_lang('DocumentsTitleUsersColumn') . ";" . get_lang('DocumentsTitleCountColumn') . "\n";
  232. if (is_array($results)) {
  233. for ($j = 0; $j < count($results); $j++) {
  234. $line .= $results[$j][0] . ";" . $results[$j][1] . ";" . $results[$j][2] . "\n";
  235. }
  236. } else {
  237. $line = get_lang('NoResult') . "\n";
  238. }
  239. }
  240. /* Scorm contents and Learning Path */
  241. $tempView = $view;
  242. if ($view[5] == '1') {
  243. $tempView[5] = '0';
  244. $sql = "SELECT id, name FROM $tbl_learnpath_main WHERE c_id = $course_id ";
  245. $result = Database::query($sql);
  246. $ar = Database::fetch_array($result);
  247. $title[1] = $nameTools;
  248. $line = '';
  249. $title_line = get_lang('ScormContentColumn');
  250. $scormcontopen = $_REQUEST["scormcontopen"];
  251. $scormstudentopen = $_REQUEST["scormstudentopen"];
  252. if (is_array($ar)) {
  253. while ($ar['id'] != '') {
  254. $lp_title = stripslashes($ar['name']);
  255. //echo "<a href='".api_get_self()."?view=".$view."&scormcontopen=".$ar['id']."' class='specialLink'>$lp_title</a>";
  256. if ($ar['id'] == $scormcontopen) { //have to list the students here
  257. $contentId = $ar['id'];
  258. $sql2 = "SELECT u.user_id, u.lastname, u.firstname " .
  259. "FROM $tbl_learnpath_view sd " .
  260. "INNER JOIN $table_user u " .
  261. "ON u.user_id = sd.user_id " .
  262. "WHERE sd.c_id = $course_id AND sd.lp_id=$contentId group by u.user_id";
  263. //error_log($sql2,0);
  264. $result2 = Database::query($sql2);
  265. if (Database::num_rows($result2) > 0) {
  266. $ar2 = Database::fetch_array($result2);
  267. while ($ar2 != '') {
  268. if (isset($_REQUEST["scormstudentopen"]) && $ar2['user_id'] == $scormstudentopen) {
  269. $line .= $ar['id'] . " " . $ar2['user_id'] . " " . api_get_person_name($ar2['firstname'], $ar2['lastname']);
  270. } else {
  271. $line .= $ar['id'] . " " . $ar2['user_id'] . " " . api_get_person_name($ar2['firstname'], $ar2['lastname']);
  272. }
  273. if ($ar2['user_id'] == $scormstudentopen) { //have to list the student's results
  274. $studentId = $ar2['user_id'];
  275. $sql3 = "SELECT iv.status, iv.score, i.title, iv.total_time " .
  276. "FROM $tbl_learnpath_item i " .
  277. "INNER JOIN $tbl_learnpath_item_view iv ON i.id=iv.lp_item_id " .
  278. "INNER JOIN $tbl_learnpath_view v ON iv.lp_view_id=v.id " .
  279. "WHERE i.c_id = $course_id AND
  280. iv.c_id = $course_id AND
  281. v.c_id = $course_id AND
  282. v.user_id=$studentId and v.lp_id=$contentId ORDER BY v.id, i.id";
  283. $result3 = Database::query($sql3);
  284. $ar3 = Database::fetch_array($result3);
  285. $title_line .= get_lang('ScormTitleColumn') . ";" . get_lang('ScormStatusColumn') . ";" . get_lang('ScormScoreColumn') . ";" . get_lang('ScormTimeColumn');
  286. while ($ar3['status'] != '') {
  287. require_once '../newscorm/learnpathItem.class.php';
  288. $time = learnpathItem::getScormTimeFromParameter('php', $ar3['total_time']);
  289. $line .= $title . ";" . $ar3['status'] . ";" . $ar3['score'] . ";" . $time;
  290. $ar3 = Database::fetch_array($result3);
  291. }
  292. }
  293. $line .= "\n";
  294. $ar2 = Database::fetch_array($result2);
  295. }
  296. $title_line .= "\n";
  297. }
  298. }
  299. $ar = Database::fetch_array($result);
  300. }
  301. }
  302. }
  303. /*
  304. * Export to a CSV file
  305. * Force the browser to save the file instead of opening it.
  306. */
  307. $len = strlen($title_line . $line);
  308. header('Content-type: application/octet-stream');
  309. //header('Content-Type: application/force-download');
  310. header('Content-length: ' . $len);
  311. $filename = api_html_entity_decode(str_replace(":", "", str_replace(" ", "_", $title[0] . '_' . $title[1] . '.csv')));
  312. $filename = api_replace_dangerous_char($filename);
  313. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  314. header('Content-Disposition: filename= ' . $filename);
  315. } else {
  316. header('Content-Disposition: attachment; filename= ' . $filename);
  317. }
  318. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  319. header('Pragma: ');
  320. header('Cache-Control: ');
  321. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  322. }
  323. header('Content-Description: ' . $filename);
  324. header('Content-transfer-encoding: binary');
  325. echo api_html_entity_decode($title_line, ENT_COMPAT);
  326. echo api_html_entity_decode($line, ENT_COMPAT);
  327. exit;
  328. } else {
  329. api_not_allowed();
  330. }