courseLogCSV.php 16 KB

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