tracking.lib.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. <?php
  2. // $Id: tracking.lib.php 2007-28-02 15:51:53
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004 Dokeos S.A.
  7. Copyright (c) 2003 Ghent University (UGent)
  8. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9. Copyright (c) various contributors
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * This is the tracking library for Dokeos.
  23. * Include/require it in your code to use its functionality.
  24. *
  25. * @package dokeos.library
  26. ==============================================================================
  27. */
  28. class Tracking {
  29. /**
  30. * Calculates the time spent on the platform by a user
  31. * @param integer $user_id the user id
  32. * @return timestamp $nb_seconds
  33. */
  34. function get_time_spent_on_the_platform($user_id) {
  35. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  36. $sql = 'SELECT login_date, logout_date FROM ' . $tbl_track_login . '
  37. WHERE login_user_id = ' . intval($user_id).' AND logout_date IS NOT NULL';
  38. $rs = api_sql_query($sql);
  39. $nb_seconds = 0;
  40. while ($a_connections = mysql_fetch_array($rs)) {
  41. $s_login_date = $a_connections["login_date"];
  42. $s_logout_date = $a_connections["logout_date"];
  43. $i_timestamp_login_date = strtotime($s_login_date);
  44. $i_timestamp_logout_date = strtotime($s_logout_date);
  45. $nb_seconds += ($i_timestamp_logout_date - $i_timestamp_login_date);
  46. }
  47. return $nb_seconds;
  48. }
  49. /**
  50. * Calculates the time spent on the course
  51. * @param integer $user_id the user id
  52. * @param string $course_code the course code
  53. * @return timestamp $nb_seconds
  54. */
  55. function get_time_spent_on_the_course($user_id, $course_code) {
  56. // protect datas
  57. $student_id = intval($student_id);
  58. $course_code = addslashes($course_code);
  59. $tbl_track_course = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  60. $sql = 'SELECT login_course_date, logout_course_date FROM ' . $tbl_track_course . '
  61. WHERE user_id = ' . intval($user_id) . '
  62. AND course_code="' . $course_code . '"';
  63. $rs = api_sql_query($sql);
  64. $nb_seconds = 0;
  65. while ($a_connections = mysql_fetch_array($rs)) {
  66. $s_login_date = $a_connections["login_course_date"];
  67. $s_logout_date = $a_connections["logout_course_date"];
  68. $i_timestamp_login_date = strtotime($s_login_date);
  69. $i_timestamp_logout_date = strtotime($s_logout_date);
  70. $nb_seconds += ($i_timestamp_logout_date - $i_timestamp_login_date);
  71. }
  72. return $nb_seconds;
  73. }
  74. function get_first_connection_date($student_id) {
  75. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  76. $sql = 'SELECT login_date FROM ' . $tbl_track_login . '
  77. WHERE login_user_id = ' . intval($student_id) . '
  78. ORDER BY login_date ASC LIMIT 0,1';
  79. $rs = api_sql_query($sql);
  80. if ($first_login_date = mysql_result($rs, 0, 0)) {
  81. return format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($first_login_date));
  82. } else {
  83. return false;
  84. }
  85. }
  86. function get_last_connection_date($student_id, $warning_message = false) {
  87. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  88. $sql = 'SELECT login_date FROM ' . $tbl_track_login . '
  89. WHERE login_user_id = ' . intval($student_id) . '
  90. ORDER BY login_date DESC LIMIT 0,1';
  91. $rs = api_sql_query($sql);
  92. if ($last_login_date = mysql_result($rs, 0, 0)) {
  93. if (!$warning_message) {
  94. return format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_login_date));
  95. } else {
  96. $timestamp = strtotime($last_login_date);
  97. $currentTimestamp = mktime();
  98. //If the last connection is > than 7 days, the text is red
  99. //345600 = 7 days in seconds
  100. if ($currentTimestamp - $timestamp > 604800) {
  101. return '<span style="color: #F00;">' . format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_login_date)) . '</span>';
  102. } else {
  103. return format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_login_date));
  104. }
  105. }
  106. } else {
  107. return false;
  108. }
  109. }
  110. function get_first_connection_date_on_the_course($student_id, $course_code) {
  111. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  112. $sql = 'SELECT login_course_date FROM ' . $tbl_track_login . '
  113. WHERE user_id = ' . intval($student_id) . '
  114. AND course_code = "' . mysql_real_escape_string($course_code) . '"
  115. ORDER BY login_course_date ASC LIMIT 0,1';
  116. $rs = api_sql_query($sql);
  117. if ($first_login_date = mysql_result($rs, 0, 0)) {
  118. return format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($first_login_date));
  119. } else {
  120. return false;
  121. }
  122. }
  123. function get_last_connection_date_on_the_course($student_id, $course_code) {
  124. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  125. $sql = 'SELECT login_course_date FROM ' . $tbl_track_login . '
  126. WHERE user_id = ' . intval($student_id) . '
  127. AND course_code = "' . mysql_real_escape_string($course_code) . '"
  128. ORDER BY login_course_date DESC LIMIT 0,1';
  129. $rs = api_sql_query($sql);
  130. if ($last_login_date = mysql_result($rs, 0, 0)) {
  131. $timestamp = strtotime($last_login_date);
  132. $currentTimestamp = mktime();
  133. //If the last connection is > than 7 days, the text is red
  134. //345600 = 7 days in seconds
  135. if ($currentTimestamp - $timestamp > 604800) {
  136. return '<span style="color: #F00;">' . format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_login_date)) . ' <a href="'.api_get_path(REL_CODE_PATH).'announcements/announcements.php?action=add&remind_inactive='.$student_id.'" title="'.get_lang('RemindInactiveUser').'"><img align="middle" src="'.api_get_path(WEB_IMG_PATH).'messagebox_warning.gif" /></a></span>';
  137. } else {
  138. return format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_login_date));
  139. }
  140. } else {
  141. return false;
  142. }
  143. }
  144. function count_course_per_student($user_id) {
  145. $user_id = intval($user_id);
  146. $tbl_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  147. $tbl_session_course_rel_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  148. $sql = 'SELECT DISTINCT course_code
  149. FROM ' . $tbl_course_rel_user . '
  150. WHERE user_id = ' . $user_id;
  151. $rs = api_sql_query($sql, __FILE__, __LINE__);
  152. $nb_courses = mysql_num_rows($rs);
  153. $sql = 'SELECT DISTINCT course_code
  154. FROM ' . $tbl_session_course_rel_user . '
  155. WHERE id_user = ' . $user_id;
  156. $rs = api_sql_query($sql, __FILE__, __LINE__);
  157. $nb_courses += mysql_num_rows($rs);
  158. return $nb_courses;
  159. }
  160. function get_avg_student_progress($student_id, $course_code) {
  161. require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php');
  162. // protect datas
  163. $student_id = intval($student_id);
  164. $course_code = addslashes($course_code);
  165. // get the informations of the course
  166. $a_course = CourseManager :: get_course_information($course_code);
  167. if(!empty($a_course['db_name']))
  168. {
  169. // table definition
  170. $tbl_course_lp_view = Database :: get_course_table(TABLE_LP_VIEW, $a_course['db_name']);
  171. $tbl_course_lp_view_item = Database :: get_course_table(TABLE_LP_ITEM_VIEW, $a_course['db_name']);
  172. $tbl_course_lp_item = Database :: get_course_table(TABLE_LP_ITEM, $a_course['db_name']);
  173. $tbl_course_lp = Database :: get_course_table(TABLE_LP_MAIN, $a_course['db_name']);
  174. //get the list of learning paths
  175. $sql = 'SELECT id FROM ' . $tbl_course_lp;
  176. $rs = api_sql_query($sql, __FILE__, __LINE__);
  177. $nb_lp = mysql_num_rows($rs);
  178. $avg_progress = 0;
  179. if ($nb_lp > 0) {
  180. while ($lp = Database :: fetch_array($rs)) {
  181. // get the progress in learning pathes
  182. $sqlProgress = "SELECT progress
  183. FROM " . $tbl_course_lp_view . " AS lp_view
  184. WHERE lp_view.user_id = " . $student_id . "
  185. AND lp_view.lp_id = " . $lp['id'] . "
  186. ";
  187. $resultItem = api_sql_query($sqlProgress, __FILE__, __LINE__);
  188. if(Database::num_rows($resultItem)>0)
  189. {
  190. $avg_progress += mysql_result($resultItem, 0, 0);
  191. }
  192. }
  193. $avg_progress = round($avg_progress / $nb_lp, 1);
  194. }
  195. return $avg_progress;
  196. }
  197. else
  198. {
  199. return null;
  200. }
  201. }
  202. function get_avg_student_score($student_id, $course_code) {
  203. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  204. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  205. $table_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  206. $course = CourseManager :: get_course_information($course_code);
  207. if(!empty($course['db_name']))
  208. {
  209. $lp_table = Database :: get_course_table(TABLE_LP_MAIN,$course['db_name']);
  210. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM,$course['db_name']);
  211. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW,$course['db_name']);
  212. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW,$course['db_name']);
  213. $sql_course_lp = 'SELECT id FROM '.$lp_table;
  214. $sql_result_lp = api_sql_query($sql_course_lp, __FILE__, __LINE__);
  215. $lp_scorm_score_total = 0;
  216. $lp_scorm_weighting_total = 0;
  217. if(Database::num_rows($sql_result_lp)>0){
  218. //Scorm
  219. while($a_learnpath = mysql_fetch_array($sql_result_lp)){
  220. $sql = 'SELECT id, max_score
  221. FROM '.$lp_item_table.' AS lp_item
  222. WHERE lp_id='.$a_learnpath['id'].'
  223. AND item_type="sco" LIMIT 1';
  224. $rs_lp_item_id_scorm = api_sql_query($sql, __FILE__, __LINE__);
  225. if(Database::num_rows($rs_lp_item_id_scorm)>0){
  226. $lp_item_id = mysql_result($rs_lp_item_id_scorm,0,'id');
  227. $lp_item__max_score = mysql_result($rs_lp_item_id_scorm,0,'max_score');
  228. //We get the last view id of this LP
  229. $sql='SELECT max(id) as id FROM '.$lp_view_table.' WHERE lp_id='.$a_learnpath['id'].' AND user_id="'.intval($student_id).'"';
  230. $rs_last_lp_view_id = api_sql_query($sql, __FILE__, __LINE__);
  231. $lp_view_id = mysql_result($rs_last_lp_view_id,0,'id');
  232. $sql='SELECT SUM(score)/count(lp_item_id) as score FROM '.$lp_item_view_table.' WHERE lp_view_id="'.$lp_view_id.'" GROUP BY lp_view_id';
  233. $rs_score = api_sql_query($sql, __FILE__, __LINE__);
  234. if(Database::num_rows($rs_score)>0)
  235. {
  236. $lp_scorm_score = mysql_result($rs_score,0,'score');
  237. $lp_scorm_score = ($lp_scorm_score / $lp_item__max_score) * 100;
  238. $lp_scorm_score_total+=$lp_scorm_score;
  239. $lp_scorm_weighting_total+=100;
  240. }
  241. }
  242. }
  243. mysql_data_seek($sql_result_lp,0);
  244. //Quizz in LP
  245. while($a_learnpath = Database::fetch_array($sql_result_lp)){
  246. $sql = 'SELECT id as item_id, max_score
  247. FROM '.$lp_item_table.' AS lp_item
  248. WHERE lp_id='.$a_learnpath['id'].'
  249. AND item_type="quiz"';
  250. $rsItems = api_sql_query($sql, __FILE__, __LINE__);
  251. //We get the last view id of this LP
  252. $sql='SELECT max(id) as id FROM '.$lp_view_table.' WHERE lp_id='.$a_learnpath['id'].' AND user_id="'.intval($student_id).'"';
  253. $rs_last_lp_view_id = api_sql_query($sql, __FILE__, __LINE__);
  254. $lp_view_id = mysql_result($rs_last_lp_view_id,0,'id');
  255. $total_score = $total_weighting = 0;
  256. while($item = Database :: fetch_array($rsItems, 'ASSOC'))
  257. {
  258. $sql = 'SELECT score as student_score
  259. FROM '.$lp_item_view_table.' as lp_view_item
  260. WHERE lp_view_item.lp_item_id = '.$item['item_id'].'
  261. AND lp_view_id = "'.$lp_view_id.'"
  262. ';
  263. $rsScores = api_sql_query($sql, __FILE__, __LINE__);
  264. if(Database::num_rows($rsScores)>0)
  265. {
  266. $total_score += mysql_result($rsScores, 0, 0);
  267. $total_weighting += $item['max_score'];
  268. $lp_scorm_score_total += ($total_score/$total_weighting)*100;
  269. $lp_scorm_weighting_total+=100;
  270. }
  271. }
  272. }
  273. }
  274. $totalScore = $lp_scorm_score_total;
  275. $pourcentageScore = 0;
  276. if($lp_scorm_weighting_total>0)
  277. {
  278. $pourcentageScore = round(($totalScore * 100) / $lp_scorm_weighting_total);
  279. }
  280. return $pourcentageScore;
  281. }
  282. else
  283. {
  284. return null;
  285. }
  286. }
  287. /**
  288. * gets the list of students followed by coach
  289. * @param integer $coach_id the id of the coach
  290. * @return Array the list of students
  291. */
  292. function get_student_followed_by_coach($coach_id) {
  293. $coach_id = intval($coach_id);
  294. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  295. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  296. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  297. $a_students = array ();
  298. //////////////////////////////////////////////////////////////
  299. // At first, courses where $coach_id is coach of the course //
  300. //////////////////////////////////////////////////////////////
  301. $sql = 'SELECT id_session, course_code FROM ' . $tbl_session_course . ' WHERE id_coach=' . $coach_id;
  302. $result = api_sql_query($sql);
  303. while ($a_courses = mysql_fetch_array($result)) {
  304. $course_code = $a_courses["course_code"];
  305. $id_session = $a_courses["id_session"];
  306. $sql = "SELECT distinct srcru.id_user
  307. FROM $tbl_session_course_user AS srcru
  308. WHERE course_code='$course_code' AND id_session='$id_session'";
  309. $rs = api_sql_query($sql);
  310. while ($row = mysql_fetch_array($rs)) {
  311. $a_students[$row['id_user']] = $row['id_user'];
  312. }
  313. }
  314. //////////////////////////////////////////////////////////////
  315. // Then, courses where $coach_id is coach of the session //
  316. //////////////////////////////////////////////////////////////
  317. $sql = 'SELECT session_course_user.id_user
  318. FROM ' . $tbl_session_course_user . ' as session_course_user
  319. INNER JOIN ' . $tbl_session_course . ' as session_course
  320. ON session_course.course_code = session_course_user.course_code
  321. AND session_course_user.id_session = session_course.id_session
  322. INNER JOIN ' . $tbl_session . ' as session
  323. ON session.id = session_course.id_session
  324. AND session.id_coach = ' . $coach_id;
  325. $result = api_sql_query($sql);
  326. while ($row = mysql_fetch_array($result)) {
  327. $a_students[$row['id_user']] = $row['id_user'];
  328. }
  329. return $a_students;
  330. }
  331. function get_student_followed_by_coach_in_a_session($id_session, $coach_id) {
  332. $coach_id = intval($coach_id);
  333. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  334. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  335. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  336. $a_students = array ();
  337. //////////////////////////////////////////////////////////////
  338. // At first, courses where $coach_id is coach of the course //
  339. //////////////////////////////////////////////////////////////
  340. $sql = 'SELECT course_code FROM ' . $tbl_session_course . ' WHERE id_session="' . $id_session . '" AND id_coach=' . $coach_id;
  341. $result = api_sql_query($sql);
  342. while ($a_courses = mysql_fetch_array($result)) {
  343. $course_code = $a_courses["course_code"];
  344. $sql = "SELECT distinct srcru.id_user
  345. FROM $tbl_session_course_user AS srcru
  346. WHERE course_code='$course_code' and id_session = '" . $id_session . "'";
  347. $rs = api_sql_query($sql, __FILE__, __LINE__);
  348. while ($row = mysql_fetch_array($rs)) {
  349. $a_students[$row['id_user']] = $row['id_user'];
  350. }
  351. }
  352. //////////////////////////////////////////////////////////////
  353. // Then, courses where $coach_id is coach of the session //
  354. //////////////////////////////////////////////////////////////
  355. $dsl_session_coach = 'SELECT id_coach FROM ' . $tbl_session . ' WHERE id="' . $id_session . '" AND id_coach="' . $coach_id . '"';
  356. $result = api_sql_query($dsl_session_coach, __FILE__, __LINE__);
  357. //He is the session_coach so we select all the users in the session
  358. if (mysql_num_rows($result) > 0) {
  359. $sql = 'SELECT DISTINCT srcru.id_user FROM ' . $tbl_session_course_user . ' AS srcru WHERE id_session="' . $id_session . '"';
  360. $result = api_sql_query($sql);
  361. while ($row = mysql_fetch_array($result)) {
  362. $a_students[$row['id_user']] = $row['id_user'];
  363. }
  364. }
  365. return $a_students;
  366. }
  367. function is_allowed_to_coach_student($coach_id, $student_id) {
  368. $coach_id = intval($coach_id);
  369. $student_id = intval($student_id);
  370. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  371. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  372. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  373. //////////////////////////////////////////////////////////////
  374. // At first, courses where $coach_id is coach of the course //
  375. //////////////////////////////////////////////////////////////
  376. $sql = 'SELECT 1
  377. FROM ' . $tbl_session_course_user . ' AS session_course_user
  378. INNER JOIN ' . $tbl_session_course . ' AS session_course
  379. ON session_course.course_code = session_course_user.course_code
  380. AND id_coach=' . $coach_id . '
  381. WHERE id_user=' . $student_id;
  382. $result = api_sql_query($sql, __FILE__, __LINE__);
  383. if (mysql_num_rows($result) > 0) {
  384. return true;
  385. }
  386. //////////////////////////////////////////////////////////////
  387. // Then, courses where $coach_id is coach of the session //
  388. //////////////////////////////////////////////////////////////
  389. $sql = 'SELECT session_course_user.id_user
  390. FROM ' . $tbl_session_course_user . ' as session_course_user
  391. INNER JOIN ' . $tbl_session_course . ' as session_course
  392. ON session_course.course_code = session_course_user.course_code
  393. INNER JOIN ' . $tbl_session . ' as session
  394. ON session.id = session_course.id_session
  395. AND session.id_coach = ' . $coach_id . '
  396. WHERE id_user = ' . $student_id;
  397. $result = api_sql_query($sql, __FILE__, __LINE__);
  398. if (mysql_num_rows($result) > 0) {
  399. return true;
  400. }
  401. return false;
  402. }
  403. function get_courses_followed_by_coach($coach_id, $id_session = '') {
  404. $coach_id = intval($coach_id);
  405. if (!empty ($id_session))
  406. $id_session = intval($id_session);
  407. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  408. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  409. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  410. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  411. //////////////////////////////////////////////////////////////
  412. // At first, courses where $coach_id is coach of the course //
  413. //////////////////////////////////////////////////////////////
  414. $sql = 'SELECT DISTINCT course_code FROM ' . $tbl_session_course . ' WHERE id_coach=' . $coach_id;
  415. if (!empty ($id_session))
  416. $sql .= ' AND id_session=' . $id_session;
  417. $result = api_sql_query($sql, __FILE__, __LINE__);
  418. while ($row = mysql_fetch_array($result)) {
  419. $a_courses[$row['course_code']] = $row['course_code'];
  420. }
  421. //////////////////////////////////////////////////////////////
  422. // Then, courses where $coach_id is coach of the session //
  423. //////////////////////////////////////////////////////////////
  424. $sql = 'SELECT DISTINCT session_course.course_code
  425. FROM ' . $tbl_session_course . ' as session_course
  426. INNER JOIN ' . $tbl_session . ' as session
  427. ON session.id = session_course.id_session
  428. AND session.id_coach = ' . $coach_id . '
  429. INNER JOIN ' . $tbl_course . ' as course
  430. ON course.code = session_course.course_code';
  431. if (!empty ($id_session))
  432. $sql .= ' WHERE session_course.id_session=' . $id_session;
  433. $result = api_sql_query($sql, __FILE__, __LINE__);
  434. while ($row = mysql_fetch_array($result)) {
  435. $a_courses[$row['course_code']] = $row['course_code'];
  436. }
  437. return $a_courses;
  438. }
  439. function get_sessions_coached_by_user($coach_id) {
  440. // table definition
  441. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  442. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  443. // protect datas
  444. $coach_id = intval($coach_id);
  445. // session where we are general coach
  446. $sql = 'SELECT DISTINCT id, name, date_start, date_end
  447. FROM ' . $tbl_session . '
  448. WHERE id_coach=' . $coach_id;
  449. $rs = api_sql_query($sql);
  450. while ($row = mysql_fetch_array($rs)) {
  451. $a_sessions[$row["id"]] = $row;
  452. }
  453. // session where we are coach of a course
  454. $sql = 'SELECT DISTINCT session.id, session.name, session.date_start, session.date_end
  455. FROM ' . $tbl_session . ' as session
  456. INNER JOIN ' . $tbl_session_course . ' as session_course
  457. ON session.id = session_course.id_session
  458. AND session_course.id_coach=' . $coach_id;
  459. $rs = api_sql_query($sql);
  460. while ($row = mysql_fetch_array($rs)) {
  461. $a_sessions[$row["id"]] = $row;
  462. }
  463. foreach ($a_sessions as & $session) {
  464. if ($session['date_start'] == '0000-00-00') {
  465. $session['status'] = get_lang('SessionActive');
  466. }
  467. else {
  468. $date_start = explode('-', $session['date_start']);
  469. $time_start = mktime(0, 0, 0, $date_start[1], $date_start[2], $date_start[0]);
  470. $date_end = explode('-', $session['date_end']);
  471. $time_end = mktime(0, 0, 0, $date_end[1], $date_end[2], $date_end[0]);
  472. if ($time_start < time() && time() < $time_end) {
  473. $session['status'] = get_lang('SessionActive');
  474. }
  475. else{
  476. if (time() < $time_start) {
  477. $session['status'] = get_lang('SessionFuture');
  478. }
  479. else{
  480. if (time() > $time_end) {
  481. $session['status'] = get_lang('SessionPast');
  482. }
  483. }
  484. }
  485. }
  486. }
  487. return $a_sessions;
  488. }
  489. function get_courses_list_from_session($session_id) {
  490. //protect datas
  491. $session_id = intval($session_id);
  492. // table definition
  493. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  494. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  495. $sql = 'SELECT DISTINCT course_code, id_coach
  496. FROM ' . $tbl_session_course . '
  497. WHERE id_session=' . $session_id;
  498. $rs = api_sql_query($sql, __FILE__, __LINE__);
  499. $a_courses = array ();
  500. while ($row = mysql_fetch_array($rs)) {
  501. $a_courses[$row['course_code']] = $row;
  502. }
  503. return $a_courses;
  504. }
  505. function count_student_assignments($student_id, $course_code) {
  506. require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php');
  507. // protect datas
  508. $student_id = intval($student_id);
  509. $course_code = addslashes($course_code);
  510. // get the informations of the course
  511. $a_course = CourseManager :: get_course_information($course_code);
  512. if(!empty($a_course['db_name']))
  513. {
  514. // table definition
  515. $tbl_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY, $a_course['db_name']);
  516. $sql = 'SELECT 1
  517. FROM ' . $tbl_item_property . '
  518. WHERE insert_user_id=' . $student_id . '
  519. AND tool="work"';
  520. $rs = api_sql_query($sql, __LINE__, __FILE__);
  521. return mysql_num_rows($rs);
  522. }
  523. else
  524. {
  525. return null;
  526. }
  527. }
  528. function count_student_messages($student_id, $course_code) {
  529. require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php');
  530. // protect datas
  531. $student_id = intval($student_id);
  532. $course_code = addslashes($course_code);
  533. // get the informations of the course
  534. $a_course = CourseManager :: get_course_information($course_code);
  535. if(!empty($a_course['db_name']))
  536. {
  537. // table definition
  538. $tbl_messages = Database :: get_course_table(TABLE_FORUM_POST, $a_course['db_name']);
  539. $sql = 'SELECT 1
  540. FROM ' . $tbl_messages . '
  541. WHERE poster_id=' . $student_id;
  542. $rs = api_sql_query($sql, __LINE__, __FILE__);
  543. return mysql_num_rows($rs);
  544. }
  545. else
  546. {
  547. return null;
  548. }
  549. }
  550. function count_student_visited_links($student_id, $course_code) {
  551. // protect datas
  552. $student_id = intval($student_id);
  553. $course_code = addslashes($course_code);
  554. // table definition
  555. $tbl_stats_links = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  556. $sql = 'SELECT 1
  557. FROM ' . $tbl_stats_links . '
  558. WHERE links_user_id=' . $student_id . '
  559. AND links_cours_id="' . $course_code . '"';
  560. $rs = api_sql_query($sql, __LINE__, __FILE__);
  561. return mysql_num_rows($rs);
  562. }
  563. function count_student_downloaded_documents($student_id, $course_code) {
  564. // protect datas
  565. $student_id = intval($student_id);
  566. $course_code = addslashes($course_code);
  567. // table definition
  568. $tbl_stats_documents = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  569. $sql = 'SELECT 1
  570. FROM ' . $tbl_stats_documents . '
  571. WHERE down_user_id=' . $student_id . '
  572. AND down_cours_id="' . $course_code . '"';
  573. $rs = api_sql_query($sql, __LINE__, __FILE__);
  574. return mysql_num_rows($rs);
  575. }
  576. function get_course_list_in_session_from_student($user_id, $id_session) {
  577. $user_id = intval($user_id);
  578. $id_session = intval($id_session);
  579. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  580. $sql = 'SELECT course_code FROM ' . $tbl_session_course_user . ' WHERE id_user="' . $user_id . '" AND id_session="' . $id_session . '"';
  581. $result = api_sql_query($sql, __LINE__, __FILE__);
  582. $a_courses = array ();
  583. while ($row = mysql_fetch_array($result)) {
  584. $a_courses[$row['course_code']] = $row['course_code'];
  585. }
  586. return $a_courses;
  587. }
  588. function get_inactives_students_in_course($course_code, $since, $session_id=0)
  589. {
  590. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  591. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  592. $inner = '';
  593. if($session_id!=0)
  594. {
  595. $inner = ' INNER JOIN '.$tbl_session_course_user.' session_course_user
  596. ON stats_login.course_code = session_course_user.course_code
  597. AND session_course_user.id_session = '.intval($session_id).'
  598. AND session_course_user.id_user = stats_login.user_id ';
  599. }
  600. $sql = 'SELECT user_id, MAX(login_course_date) max_date FROM'.$tbl_track_login.' stats_login'.$inner.'
  601. GROUP BY user_id
  602. HAVING DATE_ADD(max_date, INTERVAL '.$since.' DAY) < NOW() ';
  603. $rs = api_sql_query($sql,__FILE__,__LINE__);
  604. $inactive_users = array();
  605. while($user = Database::fetch_array($rs))
  606. {
  607. $inactive_users[] = $user['user_id'];
  608. }
  609. return $inactive_users;
  610. }
  611. function count_login_per_student($student_id, $course_code) {
  612. $student_id = intval($student_id);
  613. $course_code = addslashes($course_code);
  614. $tbl_course_rel_user = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  615. $sql = 'SELECT '.$student_id.'
  616. FROM ' . $tbl_course_rel_user . '
  617. WHERE access_user_id=' . $student_id . '
  618. AND access_cours_code="' . $course_code . '"';
  619. $rs = api_sql_query($sql, __FILE__, __LINE__);
  620. $nb_login = mysql_num_rows($rs);
  621. return $nb_login;
  622. }
  623. function get_student_followed_by_drh($hr_dept_id) {
  624. $hr_dept_id = intval($hr_dept_id);
  625. $a_students = array ();
  626. $tbl_organism = Database :: get_main_table(TABLE_MAIN_ORGANISM);
  627. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  628. $sql = 'SELECT DISTINCT user_id FROM '.$tbl_user.' as user
  629. WHERE hr_dept_id='.$hr_dept_id;
  630. $rs = api_sql_query($sql, __FILE__, __LINE__);
  631. while($user = Database :: fetch_array($rs))
  632. {
  633. $a_students[$user['user_id']] = $user['user_id'];
  634. }
  635. return $a_students;
  636. }
  637. }
  638. ?>