courseLogCSV.php 16 KB

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