tracking.lib.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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-2008 Dokeos SPRL
  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 address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  18. Mail: info@dokeos.com
  19. ==============================================================================
  20. */
  21. /**
  22. ==============================================================================
  23. * This is the tracking library for Dokeos.
  24. * Include/require it in your code to use its functionality.
  25. *
  26. * @package dokeos.library
  27. * @author Julio Montoya <gugli100@gmail.com> (Score average fixes)
  28. ==============================================================================
  29. */
  30. class Tracking {
  31. /**
  32. * Calculates the time spent on the platform by a user
  33. * @param integer $user_id the user id
  34. * @return timestamp $nb_seconds
  35. */
  36. public static function get_time_spent_on_the_platform($user_id) {
  37. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  38. $sql = 'SELECT login_date, logout_date FROM ' . $tbl_track_login . '
  39. WHERE login_user_id = ' . intval($user_id);
  40. $rs = Database::query($sql,__FILE__,__LINE__);
  41. $nb_seconds = 0;
  42. $wrong_logout_dates = false;
  43. while ($a_connections = Database::fetch_array($rs)) {
  44. $s_login_date = $a_connections["login_date"];
  45. $s_logout_date = $a_connections["logout_date"];
  46. $i_timestamp_login_date = strtotime($s_login_date);
  47. $i_timestamp_logout_date = strtotime($s_logout_date);
  48. if($i_timestamp_logout_date>0)
  49. {
  50. $nb_seconds += ($i_timestamp_logout_date - $i_timestamp_login_date);
  51. }
  52. else
  53. { // there are wrong datas in db, then we can't give a wrong time
  54. $wrong_logout_dates = true;
  55. }
  56. }
  57. if($nb_seconds>0 || !$wrong_logout_dates)
  58. {
  59. return $nb_seconds;
  60. }
  61. else
  62. {
  63. return -1; //-1 means we have wrong datas in the db
  64. }
  65. }
  66. /**
  67. * Calculates the time spent on the course
  68. * @param integer $user_id the user id
  69. * @param string $course_code the course code
  70. * @return timestamp $nb_seconds
  71. */
  72. public static function get_time_spent_on_the_course($user_id, $course_code) {
  73. // protect datas
  74. $course_code = Database::escape_string($course_code);
  75. $tbl_track_course = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  76. $condition_user = "";
  77. if (is_array($user_id)) {
  78. $condition_user = " AND user_id IN (".implode(',',$user_id).") ";
  79. } else {
  80. $user_id = intval($user_id);
  81. $condition_user = " AND user_id = '$user_id' ";
  82. }
  83. $sql = " SELECT SUM(UNIX_TIMESTAMP(logout_course_date)-UNIX_TIMESTAMP(login_course_date)) as nb_seconds
  84. FROM $tbl_track_course
  85. WHERE course_code='$course_code' $condition_user";
  86. $rs = Database::query($sql,__FILE__,__LINE__);
  87. $row = Database::fetch_array($rs);
  88. return $row['nb_seconds'];
  89. }
  90. public static function get_first_connection_date($student_id) {
  91. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  92. $sql = 'SELECT login_date FROM ' . $tbl_track_login . '
  93. WHERE login_user_id = ' . intval($student_id) . '
  94. ORDER BY login_date ASC LIMIT 0,1';
  95. $rs = Database::query($sql,__FILE__,__LINE__);
  96. if(Database::num_rows($rs)>0)
  97. {
  98. if ($first_login_date = Database::result($rs, 0, 0)) {
  99. return format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($first_login_date));
  100. }
  101. }
  102. return false;
  103. }
  104. public static function get_last_connection_date($student_id, $warning_message = false, $return_timestamp = false) {
  105. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  106. $sql = 'SELECT login_date FROM ' . $tbl_track_login . '
  107. WHERE login_user_id = ' . intval($student_id) . '
  108. ORDER BY login_date DESC LIMIT 0,1';
  109. $rs = Database::query($sql,__FILE__,__LINE__);
  110. if(Database::num_rows($rs)>0)
  111. {
  112. if ($last_login_date = Database::result($rs, 0, 0))
  113. {
  114. if ($return_timestamp)
  115. {
  116. return strtotime($last_login_date);
  117. }
  118. else
  119. {
  120. if (!$warning_message)
  121. {
  122. return format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_login_date));
  123. }
  124. else
  125. {
  126. $timestamp = strtotime($last_login_date);
  127. $currentTimestamp = mktime();
  128. //If the last connection is > than 7 days, the text is red
  129. //345600 = 7 days in seconds
  130. if ($currentTimestamp - $timestamp > 604800)
  131. {
  132. return '<span style="color: #F00;">' . format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_login_date)) . '</span>';
  133. }
  134. else
  135. {
  136. return format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_login_date));
  137. }
  138. }
  139. }
  140. }
  141. }
  142. return false;
  143. }
  144. public static function get_first_connection_date_on_the_course($student_id, $course_code) {
  145. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  146. $sql = 'SELECT login_course_date FROM ' . $tbl_track_login . '
  147. WHERE user_id = ' . intval($student_id) . '
  148. AND course_code = "' . Database::escape_string($course_code) . '"
  149. ORDER BY login_course_date ASC LIMIT 0,1';
  150. $rs = Database::query($sql,__FILE__,__LINE__);
  151. if(Database::num_rows($rs)>0)
  152. {
  153. if ($first_login_date = Database::result($rs, 0, 0)) {
  154. return format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($first_login_date));
  155. }
  156. }
  157. return false;
  158. }
  159. public static function get_last_connection_date_on_the_course($student_id, $course_code) {
  160. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  161. $sql = 'SELECT login_course_date FROM ' . $tbl_track_login . '
  162. WHERE user_id = ' . intval($student_id) . '
  163. AND course_code = "' . Database::escape_string($course_code) . '"
  164. ORDER BY login_course_date DESC LIMIT 0,1';
  165. $rs = Database::query($sql,__FILE__,__LINE__);
  166. if(Database::num_rows($rs)>0)
  167. {
  168. if ($last_login_date = Database::result($rs, 0, 0)) {
  169. $timestamp = strtotime($last_login_date);
  170. $currentTimestamp = mktime();
  171. //If the last connection is > than 7 days, the text is red
  172. //345600 = 7 days in seconds
  173. if ($currentTimestamp - $timestamp > 604800) {
  174. return '<span style="color: #F00;">' . format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_login_date)) . (api_is_allowed_to_edit()?' <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>';
  175. } else {
  176. return format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_login_date));
  177. }
  178. }
  179. }
  180. return false;
  181. }
  182. public static function count_course_per_student($user_id) {
  183. $user_id = intval($user_id);
  184. $tbl_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  185. $tbl_session_course_rel_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  186. $sql = 'SELECT DISTINCT course_code
  187. FROM ' . $tbl_course_rel_user . '
  188. WHERE user_id = ' . $user_id;
  189. $rs = Database::query($sql, __FILE__, __LINE__);
  190. $nb_courses = Database::num_rows($rs);
  191. $sql = 'SELECT DISTINCT course_code
  192. FROM ' . $tbl_session_course_rel_user . '
  193. WHERE id_user = ' . $user_id;
  194. $rs = Database::query($sql, __FILE__, __LINE__);
  195. $nb_courses += Database::num_rows($rs);
  196. return $nb_courses;
  197. }
  198. /**
  199. * This function gets the score average from all tests in a course by student
  200. * @param int $student_id - or array for multiples User id (array(0=>1,1=>2))
  201. * @param string $course_code - Course id
  202. * @return string value (number %) Which represents a round integer about the score average.
  203. */
  204. public static function get_avg_student_exercise_score($student_id, $course_code) {
  205. // protect datas
  206. $course_code = Database::escape_string($course_code);
  207. // get the informations of the course
  208. $a_course = CourseManager :: get_course_information($course_code);
  209. if(!empty($a_course['db_name'])) {
  210. // table definition
  211. $tbl_course_quiz = Database::get_course_table(TABLE_QUIZ_TEST,$a_course['db_name']);
  212. $tbl_stats_exercise = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  213. $count_quiz = Database::fetch_row(Database::query("SELECT count(id) FROM $tbl_course_quiz WHERE active <> -1",__FILE__,__LINE__));
  214. $quiz_avg_total_score = 0;
  215. if (!empty($count_quiz[0]) && !empty($student_id)) {
  216. $condition_user = "";
  217. if (is_array($student_id)) {
  218. $condition_user = " AND exe_user_id IN (".implode(',',$student_id).") ";
  219. } else {
  220. $condition_user = " AND exe_user_id = '$student_id' ";
  221. }
  222. $sql = "SELECT SUM(exe_result/exe_weighting*100) as avg_score
  223. FROM $tbl_stats_exercise
  224. WHERE exe_exo_id IN (SELECT id FROM $tbl_course_quiz WHERE active <> -1)
  225. $condition_user
  226. AND orig_lp_id = 0
  227. AND exe_cours_id = '$course_code'
  228. AND orig_lp_item_id = 0
  229. ORDER BY exe_date DESC";
  230. $res = Database::query($sql, __FILE__, __LINE__);
  231. $row = Database::fetch_array($res);
  232. $quiz_avg_score = 0;
  233. if (!empty($row['avg_score'])) {
  234. $quiz_avg_score = round($row['avg_score'],2);
  235. }
  236. $count_attempt = Database::fetch_row(Database::query("SELECT count(*) FROM $tbl_stats_exercise WHERE exe_exo_id IN (SELECT id FROM $tbl_course_quiz WHERE active <> -1) $condition_user AND orig_lp_id = 0 AND exe_cours_id = '$course_code' AND orig_lp_item_id = 0 ORDER BY exe_date DESC",__FILE__,__LINE__));
  237. if(!empty($count_attempt[0])) {
  238. $quiz_avg_score = $quiz_avg_score / $count_attempt[0];
  239. }
  240. $quiz_avg_total_score = $quiz_avg_score;
  241. return $quiz_avg_total_score/$count_quiz[0];
  242. }
  243. }
  244. return null;
  245. }
  246. public static function get_avg_student_progress($student_id, $course_code) {
  247. // protect datas
  248. $course_code = addslashes($course_code);
  249. // get the informations of the course
  250. $a_course = CourseManager :: get_course_information($course_code);
  251. if (!empty($a_course['db_name'])) {
  252. // table definition
  253. $tbl_course_lp_view = Database :: get_course_table(TABLE_LP_VIEW, $a_course['db_name']);
  254. $tbl_course_lp = Database :: get_course_table(TABLE_LP_MAIN, $a_course['db_name']);
  255. $count_lp = Database::fetch_row(Database::query("SELECT count(id) FROM $tbl_course_lp",__FILE__,__LINE__));
  256. $avg_progress = 0;
  257. if (!empty($count_lp[0]) && !empty($student_id)) {
  258. $condition_user = "";
  259. if (is_array($student_id)) {
  260. $condition_user = " lp_view.user_id IN (".implode(',',$student_id).") AND ";
  261. } else {
  262. $condition_user = " lp_view.user_id = '$student_id' AND ";
  263. }
  264. $sqlProgress = "SELECT SUM(progress) FROM $tbl_course_lp_view AS lp_view WHERE $condition_user lp_view.lp_id IN (SELECT id FROM $tbl_course_lp)";
  265. $resultItem = Database::query($sqlProgress, __FILE__, __LINE__);
  266. $rowItem = Database::fetch_row($resultItem);
  267. $avg_progress = round($rowItem[0] / $count_lp[0], 1);
  268. return $avg_progress;
  269. }
  270. }
  271. return null;
  272. }
  273. /**
  274. * This function gets:
  275. * 1. The score average from all SCORM Test items in all LP in a course-> All the answers / All the max score.
  276. * 2. The score average from all Tests (quiz) in all LP in a course-> All the answers / All the max score.
  277. * 3. And finally it will return the average between 1. and 2.
  278. * This function does not take the results of a Test out of a LP
  279. *
  280. * @param User id
  281. * @param Course id
  282. * @param Array limit average to listed lp ids
  283. * @return string value (number %) Which represents a round integer explain in got in 3.
  284. */
  285. public static function get_avg_student_score($student_id, $course_code, $lp_ids=array()) {
  286. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  287. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  288. $table_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  289. $tbl_stats_exercices = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  290. $tbl_stats_attempts= Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  291. $course = CourseManager :: get_course_information($course_code);
  292. if (!empty($course['db_name'])) {
  293. $tbl_quiz_questions= Database :: get_course_table(TABLE_QUIZ_QUESTION,$course['db_name']);
  294. $lp_table = Database :: get_course_table(TABLE_LP_MAIN,$course['db_name']);
  295. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM,$course['db_name']);
  296. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW,$course['db_name']);
  297. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW,$course['db_name']);
  298. $condition_lp = "";
  299. if(count($lp_ids) > 0) {
  300. $condition_lp =" WHERE id IN(".implode(',',$lp_ids).") ";
  301. }
  302. $count_row_lp = Database::fetch_row(Database::query("SELECT count(*) FROM $lp_table $condition_lp",__FILE__,__LINE__));
  303. $lp_scorm_score_total = 0;
  304. $lp_scorm_weighting_total = 0;
  305. $lp_scorm_result_score_total = 0;
  306. $lp_scorm_loop=0;
  307. $lp_count = 0;
  308. $progress = 0;
  309. if (!empty($count_row_lp[0]) && !empty($student_id)) {
  310. $condition_user1 = "";
  311. if (is_array($student_id)) {
  312. $condition_user1 =" AND user_id IN (".implode(',',$student_id).") ";
  313. } else {
  314. $condition_user1 =" AND user_id = '$student_id' ";
  315. }
  316. $sql = "SELECT max(id) as id, lp_id, user_id FROM $lp_view_table WHERE lp_id IN (SELECT id FROM $lp_table $condition_lp) $condition_user1 GROUP BY lp_id,user_id";
  317. $rs_last_lp_view_id = Database::query($sql, __FILE__, __LINE__);
  318. $score_of_scorm_calculate = 0;
  319. if (Database::num_rows($rs_last_lp_view_id) > 0) {
  320. while ($row_lp_view = Database::fetch_array($rs_last_lp_view_id)) {
  321. $lp_view_id = $row_lp_view['id'];
  322. // we get the progress
  323. $sql='SELECT progress FROM '.$lp_view_table.' WHERE id="'.$lp_view_id.'"';
  324. $rs = Database::query($sql, __FILE__, __LINE__);
  325. $progress = Database::result($rs,0,'progress');
  326. // item's list of an scorm
  327. $sql_max_score='SELECT lp_iv.score as score,lp_i.max_score
  328. FROM '.$lp_item_view_table.' as lp_iv
  329. INNER JOIN '.$lp_item_table.' as lp_i
  330. ON lp_i.id = lp_iv.lp_item_id
  331. AND lp_i.item_type="sco"
  332. WHERE lp_view_id="'.$lp_view_id.'"';
  333. $res_max_score=Database::query($sql_max_score,__FILE__,__LINE__);
  334. $count_total_loop=0;
  335. $num_rows_max_score=Database::num_rows($res_max_score);
  336. if ($num_rows_max_score==1) {
  337. while ($row_max_score=Database::fetch_array($res_max_score)) {
  338. if ($row_max_score['max_score']==0) {
  339. //when there's no max score, we assume 100 as the max score, as the SCORM 1.2 says that the value should always be between 0 and 100.
  340. $lp_scorm_result_score_total+=($row_max_score['score']/100);
  341. $current_value = $row_max_score['score']/100;
  342. } else {
  343. $lp_scorm_result_score_total+=($row_max_score['score']/$row_max_score['max_score']);
  344. $current_value = $row_max_score['score']/$row_max_score['max_score'];
  345. }
  346. $count_total_loop++;
  347. }
  348. } elseif ($num_rows_max_score > 1) {
  349. while ($row_max_score=Database::fetch_array($res_max_score)) {
  350. if ($row_max_score['max_score']==0) {
  351. $lp_scorm_result_score_total+=($row_max_score['score']/100);
  352. $current_value = $row_max_score['score']/100;
  353. } else {
  354. //when there's no max score, we assume 100 as the max score, as the SCORM 1.2 says that the value should always be between 0 and 100.
  355. $lp_scorm_result_score_total+=($row_max_score['score']/$row_max_score['max_score']);
  356. $current_value = $row_max_score['score']/$row_max_score['max_score'];
  357. }
  358. $count_total_loop++;
  359. }
  360. }
  361. if ($num_rows_max_score > 0 && ($progress > 0 || $current_value > 0 )) {
  362. $lp_count++;
  363. }
  364. if ($count_total_loop==0) {
  365. $count_total_loop=1;
  366. }
  367. $score_of_scorm_calculate=round((($lp_scorm_result_score_total/$count_total_loop)*100),2);
  368. }
  369. }
  370. if(count($lp_ids)==0 ) {
  371. $score_of_scorm_calculate=round((($score_of_scorm_calculate/$count_row_lp[0])),2);
  372. }
  373. $lp_scorm_score_total = $score_of_scorm_calculate;
  374. $condition_user2 = "";
  375. if (is_array($student_id)) {
  376. $condition_user2 =" lp_view.user_id IN (".implode(',',$student_id).") AND ";
  377. } else {
  378. $condition_user2 =" lp_view.user_id = '$student_id' AND ";
  379. }
  380. //Path is the exercise id
  381. $sql = "SELECT lp_view.id as lp_view_id, lp_view.lp_id as lp_id, lp_item.path, lp_item.id as item_id, lp_item.max_score, lp_view.user_id as user_id
  382. FROM $lp_view_table lp_view
  383. INNER JOIN $lp_item_table lp_item ON lp_item.lp_id = lp_view.lp_id AND item_type='".TOOL_QUIZ."'
  384. WHERE $condition_user2 lp_view.lp_id IN (SELECT id FROM $lp_table $condition_lp)";;
  385. $rsItems = Database::query($sql, __FILE__, __LINE__);
  386. $total_score = $total_weighting = 0;
  387. if (Database::num_rows($rsItems) > 0) {
  388. while ($item = Database::fetch_array($rsItems)) {
  389. $lp_view_id = $item['lp_view_id'];
  390. $lp_item_id = $item['item_id'];
  391. $lp_id = $item['lp_id'];
  392. $user_id = $item['user_id'];
  393. // we take the score from a LP because we have lp_view_id
  394. $sql = "SELECT score FROM $lp_item_view_table WHERE lp_item_id = '$lp_item_id' and lp_view_id = '$lp_view_id'
  395. ORDER BY view_count DESC limit 1";
  396. $rsScores = Database::query($sql, __FILE__, __LINE__);
  397. // Real max score - this was implemented because of the random exercises
  398. $sql_last_attempt = 'SELECT exe_id FROM '. $tbl_stats_exercices. ' ' .
  399. 'WHERE exe_exo_id="' .$item['path']. '" AND exe_user_id="' . $user_id . '" AND orig_lp_id = "'.$lp_id.'" AND orig_lp_item_id = "'.$lp_item_id.'" AND exe_cours_id="' . $course_code . '" ORDER BY exe_date DESC limit 1';
  400. $resultLastAttempt = Database::query($sql_last_attempt, __FILE__, __LINE__);
  401. $num = Database :: num_rows($resultLastAttempt);
  402. if ($num > 0){
  403. if ($num > 1){
  404. while ($rowLA = Database :: fetch_row($resultLastAttempt)) {
  405. $id_last_attempt = $rowLA[0];
  406. }
  407. } else {
  408. $id_last_attempt = Database :: result($resultLastAttempt, 0, 0);
  409. }
  410. }
  411. $sql = "SELECT SUM(t.ponderation) as maxscore from ( SELECT distinct question_id, marks,ponderation FROM $tbl_stats_attempts as at " .
  412. "INNER JOIN $tbl_quiz_questions as q on(q.id = at.question_id) where exe_id ='$id_last_attempt' ) as t";
  413. $result = Database::query($sql, __FILE__, __LINE__);
  414. $row_max_score = Database :: fetch_array($result);
  415. $maxscore = $row_max_score['maxscore'];
  416. if ($maxscore=='') {
  417. $maxscore = $item['max_score'];
  418. }
  419. if(Database::num_rows($rsScores)>0) {
  420. $total_score = Database::result($rsScores, 0, 0);
  421. $total_weighting += $maxscore;
  422. if($total_weighting>0 && $maxscore>0) {
  423. $lp_scorm_score_total += ($total_score/$maxscore)*100;
  424. $lp_scorm_weighting_total+=100;
  425. }
  426. }
  427. }
  428. }
  429. }
  430. $totalScore = $lp_scorm_score_total;
  431. $pourcentageScore = 0;
  432. if($lp_scorm_weighting_total>0) {
  433. //i.e 10.52
  434. $pourcentageScore = round( (($totalScore * 100) / $lp_scorm_weighting_total),2);
  435. return $pourcentageScore;
  436. } elseif ($score_of_scorm_calculate>0) {
  437. //echo '<br>'.$score_of_scorm_calculate;
  438. return $score_of_scorm_calculate;
  439. } else {
  440. return null;
  441. }
  442. }
  443. }
  444. /**
  445. * gets the list of students followed by coach
  446. * @param integer $coach_id the id of the coach
  447. * @return Array the list of students
  448. */
  449. public static function get_student_followed_by_coach($coach_id) {
  450. $coach_id = intval($coach_id);
  451. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  452. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  453. $tbl_session_user = Database :: get_main_table(TABLE_MAIN_SESSION_USER);
  454. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  455. $a_students = array ();
  456. //////////////////////////////////////////////////////////////
  457. // At first, courses where $coach_id is coach of the course //
  458. //////////////////////////////////////////////////////////////
  459. $sql = 'SELECT id_session, course_code FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id.' AND status=2';
  460. global $_configuration;
  461. if ($_configuration['multiple_access_urls']==true) {
  462. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  463. $access_url_id = api_get_current_access_url_id();
  464. if ($access_url_id != -1) {
  465. $sql = 'SELECT scu.id_session, scu.course_code
  466. FROM ' . $tbl_session_course_user . ' scu INNER JOIN '.$tbl_session_rel_access_url.' sru
  467. ON (scu.id_session=sru.session_id)
  468. WHERE scu.id_user=' . $coach_id.' AND scu.status=2 AND sru.access_url_id = '.$access_url_id;
  469. }
  470. }
  471. $result = Database::query($sql,__FILE__,__LINE__);
  472. while ($a_courses = Database::fetch_array($result)) {
  473. $course_code = $a_courses["course_code"];
  474. $id_session = $a_courses["id_session"];
  475. $sql = "SELECT distinct srcru.id_user
  476. FROM $tbl_session_course_user AS srcru, $tbl_session_user sru
  477. WHERE srcru.id_user = sru.id_user AND srcru.id_session = sru.id_session AND srcru.course_code='$course_code' AND srcru.id_session='$id_session'";
  478. $rs = Database::query($sql,__FILE__,__LINE__);
  479. while ($row = Database::fetch_array($rs)) {
  480. $a_students[$row['id_user']] = $row['id_user'];
  481. }
  482. }
  483. //////////////////////////////////////////////////////////////
  484. // Then, courses where $coach_id is coach of the session //
  485. //////////////////////////////////////////////////////////////
  486. $sql = 'SELECT session_course_user.id_user
  487. FROM ' . $tbl_session_course_user . ' as session_course_user
  488. INNER JOIN '.$tbl_session_user.' sru ON session_course_user.id_user = sru.id_user AND session_course_user.id_session = sru.id_session
  489. INNER JOIN ' . $tbl_session_course . ' as session_course
  490. ON session_course.course_code = session_course_user.course_code
  491. AND session_course_user.id_session = session_course.id_session
  492. INNER JOIN ' . $tbl_session . ' as session
  493. ON session.id = session_course.id_session
  494. AND session.id_coach = ' . $coach_id;
  495. if ($_configuration['multiple_access_urls']==true) {
  496. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  497. $access_url_id = api_get_current_access_url_id();
  498. if ($access_url_id != -1){
  499. $sql = 'SELECT session_course_user.id_user
  500. FROM ' . $tbl_session_course_user . ' as session_course_user
  501. INNER JOIN '.$tbl_session_user.' sru ON session_course_user.id_user = sru.id_user AND session_course_user.id_session = sru.id_session
  502. INNER JOIN ' . $tbl_session_course . ' as session_course
  503. ON session_course.course_code = session_course_user.course_code
  504. AND session_course_user.id_session = session_course.id_session
  505. INNER JOIN ' . $tbl_session . ' as session
  506. ON session.id = session_course.id_session
  507. AND session.id_coach = ' . $coach_id.'
  508. INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  509. ON session.id = session_rel_url.session_id WHERE access_url_id = '.$access_url_id;
  510. }
  511. }
  512. $result = Database::query($sql,__FILE__,__LINE__);
  513. while ($row = Database::fetch_array($result)) {
  514. $a_students[$row['id_user']] = $row['id_user'];
  515. }
  516. return $a_students;
  517. }
  518. public static function get_student_followed_by_coach_in_a_session($id_session, $coach_id) {
  519. $coach_id = intval($coach_id);
  520. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  521. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  522. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  523. $a_students = array ();
  524. //////////////////////////////////////////////////////////////
  525. // At first, courses where $coach_id is coach of the course //
  526. //////////////////////////////////////////////////////////////
  527. $sql = 'SELECT course_code FROM ' . $tbl_session_course_user . ' WHERE id_session="' . $id_session . '" AND id_user=' . $coach_id.' AND status=2';
  528. $result = Database::query($sql,__FILE__,__LINE__);
  529. while ($a_courses = Database::fetch_array($result)) {
  530. $course_code = $a_courses["course_code"];
  531. $sql = "SELECT distinct srcru.id_user
  532. FROM $tbl_session_course_user AS srcru
  533. WHERE course_code='$course_code' and id_session = '" . $id_session . "'";
  534. $rs = Database::query($sql, __FILE__, __LINE__);
  535. while ($row = Database::fetch_array($rs)) {
  536. $a_students[$row['id_user']] = $row['id_user'];
  537. }
  538. }
  539. //////////////////////////////////////////////////////////////
  540. // Then, courses where $coach_id is coach of the session //
  541. //////////////////////////////////////////////////////////////
  542. $dsl_session_coach = 'SELECT id_coach FROM ' . $tbl_session . ' WHERE id="' . $id_session . '" AND id_coach="' . $coach_id . '"';
  543. $result = Database::query($dsl_session_coach, __FILE__, __LINE__);
  544. //He is the session_coach so we select all the users in the session
  545. if (Database::num_rows($result) > 0) {
  546. $sql = 'SELECT DISTINCT srcru.id_user FROM ' . $tbl_session_course_user . ' AS srcru WHERE id_session="' . $id_session . '"';
  547. $result = Database::query($sql,__FILE__,__LINE__);
  548. while ($row = Database::fetch_array($result)) {
  549. $a_students[$row['id_user']] = $row['id_user'];
  550. }
  551. }
  552. return $a_students;
  553. }
  554. public static function is_allowed_to_coach_student($coach_id, $student_id) {
  555. $coach_id = intval($coach_id);
  556. $student_id = intval($student_id);
  557. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  558. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  559. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  560. //////////////////////////////////////////////////////////////
  561. // At first, courses where $coach_id is coach of the course //
  562. //////////////////////////////////////////////////////////////
  563. /*$sql = 'SELECT 1
  564. FROM ' . $tbl_session_course_user . ' AS session_course_user
  565. INNER JOIN ' . $tbl_session_course . ' AS session_course
  566. ON session_course.course_code = session_course_user.course_code
  567. AND id_coach=' . $coach_id . '
  568. WHERE id_user=' . $student_id;*/
  569. $sql = 'SELECT 1 FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id .' AND status=2';
  570. $result = Database::query($sql, __FILE__, __LINE__);
  571. if (Database::num_rows($result) > 0) {
  572. return true;
  573. }
  574. //////////////////////////////////////////////////////////////
  575. // Then, courses where $coach_id is coach of the session //
  576. //////////////////////////////////////////////////////////////
  577. $sql = 'SELECT session_course_user.id_user
  578. FROM ' . $tbl_session_course_user . ' as session_course_user
  579. INNER JOIN ' . $tbl_session_course . ' as session_course
  580. ON session_course.course_code = session_course_user.course_code
  581. INNER JOIN ' . $tbl_session . ' as session
  582. ON session.id = session_course.id_session
  583. AND session.id_coach = ' . $coach_id . '
  584. WHERE id_user = ' . $student_id;
  585. $result = Database::query($sql, __FILE__, __LINE__);
  586. if (Database::num_rows($result) > 0) {
  587. return true;
  588. }
  589. return false;
  590. }
  591. public static function get_courses_followed_by_coach($coach_id, $id_session = '')
  592. {
  593. $coach_id = intval($coach_id);
  594. if (!empty ($id_session))
  595. $id_session = intval($id_session);
  596. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  597. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  598. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  599. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  600. //////////////////////////////////////////////////////////////
  601. // At first, courses where $coach_id is coach of the course //
  602. //////////////////////////////////////////////////////////////
  603. $sql = 'SELECT DISTINCT course_code FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id.' AND status=2';
  604. global $_configuration;
  605. if ($_configuration['multiple_access_urls']==true) {
  606. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  607. $access_url_id = api_get_current_access_url_id();
  608. if ($access_url_id != -1){
  609. $sql = 'SELECT DISTINCT scu.course_code FROM ' . $tbl_session_course_user . ' scu INNER JOIN '.$tbl_course_rel_access_url.' cru
  610. ON (scu.course_code = cru.course_code)
  611. WHERE scu.id_user=' . $coach_id.' AND scu.status=2 AND cru.access_url_id = '.$access_url_id;
  612. }
  613. }
  614. if (!empty ($id_session))
  615. $sql .= ' AND id_session=' . $id_session;
  616. $result = Database::query($sql, __FILE__, __LINE__);
  617. while ($row = Database::fetch_array($result)) {
  618. $a_courses[$row['course_code']] = $row['course_code'];
  619. }
  620. //////////////////////////////////////////////////////////////
  621. // Then, courses where $coach_id is coach of the session //
  622. //////////////////////////////////////////////////////////////
  623. $sql = 'SELECT DISTINCT session_course.course_code
  624. FROM ' . $tbl_session_course . ' as session_course
  625. INNER JOIN ' . $tbl_session . ' as session
  626. ON session.id = session_course.id_session
  627. AND session.id_coach = ' . $coach_id . '
  628. INNER JOIN ' . $tbl_course . ' as course
  629. ON course.code = session_course.course_code';
  630. if ($_configuration['multiple_access_urls']==true) {
  631. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  632. $access_url_id = api_get_current_access_url_id();
  633. if ($access_url_id != -1){
  634. $sql = 'SELECT DISTINCT session_course.course_code
  635. FROM ' . $tbl_session_course . ' as session_course
  636. INNER JOIN ' . $tbl_session . ' as session
  637. ON session.id = session_course.id_session
  638. AND session.id_coach = ' . $coach_id . '
  639. INNER JOIN ' . $tbl_course . ' as course
  640. ON course.code = session_course.course_code
  641. INNER JOIN '.$tbl_course_rel_access_url.' course_rel_url
  642. ON (session_course.course_code = course_rel_url.course_code)';
  643. }
  644. }
  645. if (!empty ($id_session)) {
  646. $sql .= ' WHERE session_course.id_session=' . $id_session;
  647. if ($_configuration['multiple_access_urls']==true)
  648. $sql .= ' AND access_url_id = '.$access_url_id;
  649. } else {
  650. if ($_configuration['multiple_access_urls']==true)
  651. $sql .= ' WHERE access_url_id = '.$access_url_id;
  652. }
  653. $result = Database::query($sql, __FILE__, __LINE__);
  654. while ($row = Database::fetch_array($result)) {
  655. $a_courses[$row['course_code']] = $row['course_code'];
  656. }
  657. return $a_courses;
  658. }
  659. public static function get_sessions_coached_by_user($coach_id) {
  660. // table definition
  661. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  662. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  663. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  664. // protect datas
  665. $coach_id = intval($coach_id);
  666. // session where we are general coach
  667. $sql = 'SELECT DISTINCT id, name, date_start, date_end
  668. FROM ' . $tbl_session . '
  669. WHERE id_coach=' . $coach_id;
  670. global $_configuration;
  671. if ($_configuration['multiple_access_urls']==true) {
  672. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  673. $access_url_id = api_get_current_access_url_id();
  674. if ($access_url_id != -1){
  675. $sql = 'SELECT DISTINCT id, name, date_start, date_end
  676. FROM ' . $tbl_session . ' session INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  677. ON (session.id = session_rel_url.session_id)
  678. WHERE id_coach=' . $coach_id.' AND access_url_id = '.$access_url_id;
  679. }
  680. }
  681. $rs = Database::query($sql,__FILE__,__LINE__);
  682. while ($row = Database::fetch_array($rs))
  683. {
  684. $a_sessions[$row["id"]] = $row;
  685. }
  686. // session where we are coach of a course
  687. $sql = 'SELECT DISTINCT session.id, session.name, session.date_start, session.date_end
  688. FROM ' . $tbl_session . ' as session
  689. INNER JOIN ' . $tbl_session_course_user . ' as session_course_user
  690. ON session.id = session_course_user.id_session
  691. AND session_course_user.id_user=' . $coach_id.' AND session_course_user.status=2';
  692. global $_configuration;
  693. if ($_configuration['multiple_access_urls']==true) {
  694. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  695. $access_url_id = api_get_current_access_url_id();
  696. if ($access_url_id != -1){
  697. $sql = 'SELECT DISTINCT session.id, session.name, session.date_start, session.date_end
  698. FROM ' . $tbl_session . ' as session
  699. INNER JOIN ' . $tbl_session_course_user . ' as session_course_user
  700. ON session.id = session_course_user.id_session AND session_course_user.id_user=' . $coach_id.' AND session_course_user.status=2
  701. INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  702. ON (session.id = session_rel_url.session_id)
  703. WHERE access_url_id = '.$access_url_id;
  704. }
  705. }
  706. $rs = Database::query($sql,__FILE__,__LINE__);
  707. while ($row = Database::fetch_array($rs))
  708. {
  709. $a_sessions[$row["id"]] = $row;
  710. }
  711. if (is_array($a_sessions)) {
  712. foreach ($a_sessions as & $session) {
  713. if ($session['date_start'] == '0000-00-00') {
  714. $session['status'] = get_lang('SessionActive');
  715. }
  716. else {
  717. $date_start = explode('-', $session['date_start']);
  718. $time_start = mktime(0, 0, 0, $date_start[1], $date_start[2], $date_start[0]);
  719. $date_end = explode('-', $session['date_end']);
  720. $time_end = mktime(0, 0, 0, $date_end[1], $date_end[2], $date_end[0]);
  721. if ($time_start < time() && time() < $time_end) {
  722. $session['status'] = get_lang('SessionActive');
  723. }
  724. else{
  725. if (time() < $time_start) {
  726. $session['status'] = get_lang('SessionFuture');
  727. }
  728. else{
  729. if (time() > $time_end) {
  730. $session['status'] = get_lang('SessionPast');
  731. }
  732. }
  733. }
  734. }
  735. }
  736. }
  737. return $a_sessions;
  738. }
  739. public static function get_courses_list_from_session($session_id) {
  740. //protect datas
  741. $session_id = intval($session_id);
  742. // table definition
  743. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  744. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  745. $sql = 'SELECT DISTINCT course_code
  746. FROM ' . $tbl_session_course . '
  747. WHERE id_session=' . $session_id;
  748. $rs = Database::query($sql, __FILE__, __LINE__);
  749. $a_courses = array ();
  750. while ($row = Database::fetch_array($rs)) {
  751. $a_courses[$row['course_code']] = $row;
  752. }
  753. return $a_courses;
  754. }
  755. public static function count_student_assignments($student_id, $course_code) {
  756. require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php');
  757. // protect datas
  758. $course_code = Database::escape_string($course_code);
  759. // get the informations of the course
  760. $a_course = CourseManager :: get_course_information($course_code);
  761. if (!empty($a_course['db_name'])) {
  762. // table definition
  763. $tbl_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY, $a_course['db_name']);
  764. $condition_user = "";
  765. if (is_array($student_id)) {
  766. $condition_user = " AND insert_user_id IN (".implode(',',$student_id).") ";
  767. } else {
  768. $condition_user = " AND insert_user_id = '$student_id' ";
  769. }
  770. $sql = "SELECT count(tool) FROM $tbl_item_property WHERE tool='work' $condition_user ";
  771. $rs = Database::query($sql, __LINE__, __FILE__);
  772. $row = Database::fetch_row($rs);
  773. return $row[0];
  774. }
  775. return null;
  776. }
  777. public static function count_student_messages($student_id, $course_code) {
  778. require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php');
  779. // protect datas
  780. $course_code = addslashes($course_code);
  781. // get the informations of the course
  782. $a_course = CourseManager :: get_course_information($course_code);
  783. if (!empty($a_course['db_name'])) {
  784. // table definition
  785. $tbl_messages = Database :: get_course_table(TABLE_FORUM_POST, $a_course['db_name']);
  786. $condition_user = "";
  787. if (is_array($student_id)) {
  788. $condition_user = " WHERE poster_id IN (".implode(',',$student_id).") ";
  789. } else {
  790. $condition_user = " WHERE poster_id = '$student_id' ";
  791. }
  792. $sql = "SELECT count(post_id) FROM $tbl_messages $condition_user ";
  793. $rs = Database::query($sql, __LINE__, __FILE__);
  794. $row = Database::fetch_row($rs);
  795. return $row[0];
  796. }
  797. return null;
  798. }
  799. /**
  800. * This function counts the number of post by course
  801. * @param string $course_code - Course ID
  802. * @return int the number of post by course
  803. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  804. * @version enero 2009, dokeos 1.8.6
  805. */
  806. public static function count_number_of_posts_by_course($course_code) {
  807. //protect data
  808. $course_code = addslashes($course_code);
  809. // get the informations of the course
  810. $a_course = CourseManager :: get_course_information($course_code);
  811. $count = 0;
  812. if (!empty($a_course['db_name'])) {
  813. $tbl_posts = Database :: get_course_table(TABLE_FORUM_POST, $a_course['db_name']);
  814. $sql = "SELECT count(*) FROM $tbl_posts";
  815. $result = Database::query($sql, __FILE__, __LINE__);
  816. $row = Database::fetch_row($result);
  817. $count = $row[0];
  818. return $count;
  819. } else {
  820. return null;
  821. }
  822. }
  823. /**
  824. * This function counts the number of threads by course
  825. * @param string $course_code - Course ID
  826. * @return int the number of threads by course
  827. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  828. * @version enero 2009, dokeos 1.8.6
  829. */
  830. public static function count_number_of_threads_by_course($course_code) {
  831. //protect data
  832. $course_code = addslashes($course_code);
  833. // get the informations of the course
  834. $a_course = CourseManager :: get_course_information($course_code);
  835. $count = 0;
  836. if (!empty($a_course['db_name'])) {
  837. $tbl_threads = Database :: get_course_table(TABLE_FORUM_THREAD, $a_course['db_name']);
  838. $sql = "SELECT count(*) FROM $tbl_threads";
  839. $result = Database::query($sql, __FILE__, __LINE__);
  840. $row = Database::fetch_row($result);
  841. $count = $row[0];
  842. return $count;
  843. } else {
  844. return null;
  845. }
  846. }
  847. /**
  848. * This function counts the number of forums by course
  849. * @param string $course_code - Course ID
  850. * @return int the number of forums by course
  851. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  852. * @version enero 2009, dokeos 1.8.6
  853. */
  854. public static function count_number_of_forums_by_course($course_code) {
  855. //protect data
  856. $course_code = addslashes($course_code);
  857. // get the informations of the course
  858. $a_course = CourseManager :: get_course_information($course_code);
  859. $count = 0;
  860. if (!empty($a_course['db_name'])) {
  861. $tbl_forums = Database :: get_course_table(TABLE_FORUM, $a_course['db_name']);
  862. $sql = "SELECT count(*) FROM $tbl_forums";
  863. $result = Database::query($sql, __FILE__, __LINE__);
  864. $row = Database::fetch_row($result);
  865. $count = $row[0];
  866. return $count;
  867. } else {
  868. return null;
  869. }
  870. }
  871. /**
  872. * This function counts the chat last connections by course in x days
  873. * @param string $course_code - Course ID
  874. * @param int $last_days - last x days
  875. * @return int the chat last connections by course in x days
  876. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  877. * @version enero 2009, dokeos 1.8.6
  878. */
  879. public static function chat_connections_during_last_x_days_by_course($course_code,$last_days) {
  880. //protect data
  881. $last_days = intval($last_days);
  882. $course_code = addslashes($course_code);
  883. // get the informations of the course
  884. $a_course = CourseManager :: get_course_information($course_code);
  885. $count = 0;
  886. if (!empty($a_course['db_name'])) {
  887. $tbl_stats_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS, $a_course['db_name']);
  888. $sql = "SELECT count(*) FROM $tbl_stats_access WHERE DATE_SUB(NOW(),INTERVAL $last_days DAY) <= access_date
  889. AND access_cours_code = '$course_code' AND access_tool='".TOOL_CHAT."'";
  890. $result = Database::query($sql, __FILE__, __LINE__);
  891. $row = Database::fetch_row($result);
  892. $count = $row[0];
  893. return $count;
  894. } else {
  895. return null;
  896. }
  897. }
  898. /**
  899. * This function gets the last student's connection in chat
  900. * @param int $student_id - Student ID
  901. * @param string $course_code - Course ID
  902. * @return string the last connection
  903. * @author Christian Fasanando <christian.fasanando@dokeos.com>,
  904. * @version enero 2009, dokeos 1.8.6
  905. */
  906. public static function chat_last_connection($student_id,$course_code) {
  907. require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php');
  908. //protect datas
  909. $student_id = intval($student_id);
  910. $course_code = addslashes($course_code);
  911. // get the informations of the course
  912. $a_course = CourseManager :: get_course_information($course_code);
  913. $date_time = '';
  914. if (!empty($a_course['db_name'])) {
  915. // table definition
  916. $tbl_stats_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS, $a_course['db_name']);
  917. $sql = "SELECT access_date FROM $tbl_stats_access
  918. WHERE access_tool='".TOOL_CHAT."' AND access_user_id='$student_id' AND access_cours_code = '$course_code' ORDER BY access_date DESC limit 1";
  919. $rs = Database::query($sql, __LINE__, __FILE__);
  920. $row = Database::fetch_array($rs);
  921. $last_connection = $row['access_date'];
  922. if (!empty($last_connection)) {
  923. $date_format_long = format_locale_date(get_lang('DateFormatLongWithoutDay'), strtotime($last_connection));
  924. $time = explode(' ',$last_connection);
  925. $date_time = $date_format_long.' '.$time[1];
  926. }
  927. return $date_time;
  928. } else {
  929. return null;
  930. }
  931. }
  932. public static function count_student_visited_links($student_id, $course_code) {
  933. // protect datas
  934. $student_id = intval($student_id);
  935. $course_code = addslashes($course_code);
  936. // table definition
  937. $tbl_stats_links = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  938. $sql = 'SELECT 1
  939. FROM ' . $tbl_stats_links . '
  940. WHERE links_user_id=' . $student_id . '
  941. AND links_cours_id="' . $course_code . '"';
  942. $rs = Database::query($sql, __LINE__, __FILE__);
  943. return Database::num_rows($rs);
  944. }
  945. public static function count_student_downloaded_documents($student_id, $course_code) {
  946. // protect datas
  947. $student_id = intval($student_id);
  948. $course_code = addslashes($course_code);
  949. // table definition
  950. $tbl_stats_documents = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  951. $sql = 'SELECT 1
  952. FROM ' . $tbl_stats_documents . '
  953. WHERE down_user_id=' . $student_id . '
  954. AND down_cours_id="' . $course_code . '"';
  955. $rs = Database::query($sql, __LINE__, __FILE__);
  956. return Database::num_rows($rs);
  957. }
  958. public static function get_course_list_in_session_from_student($user_id, $id_session) {
  959. $user_id = intval($user_id);
  960. $id_session = intval($id_session);
  961. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  962. $sql = 'SELECT course_code FROM ' . $tbl_session_course_user . ' WHERE id_user="' . $user_id . '" AND id_session="' . $id_session . '"';
  963. $result = Database::query($sql, __LINE__, __FILE__);
  964. $a_courses = array ();
  965. while ($row = Database::fetch_array($result)) {
  966. $a_courses[$row['course_code']] = $row['course_code'];
  967. }
  968. return $a_courses;
  969. }
  970. public static function get_inactives_students_in_course($course_code, $since, $session_id=0)
  971. {
  972. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  973. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  974. $table_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  975. $inner = '';
  976. if($session_id!=0)
  977. {
  978. $inner = ' INNER JOIN '.$tbl_session_course_user.' session_course_user
  979. ON stats_login.course_code = session_course_user.course_code
  980. AND session_course_user.id_session = '.intval($session_id).'
  981. AND session_course_user.id_user = stats_login.user_id ';
  982. }
  983. $sql = 'SELECT user_id, MAX(login_course_date) max_date FROM'.$tbl_track_login.' stats_login'.$inner.'
  984. GROUP BY user_id
  985. HAVING DATE_SUB( NOW(), INTERVAL '.$since.' DAY) > max_date ';
  986. //HAVING DATE_ADD(max_date, INTERVAL '.$since.' DAY) < NOW() ';
  987. if ($since == 'never') {
  988. $sql = 'SELECT course_user.user_id FROM '.$table_course_rel_user.' course_user
  989. LEFT JOIN '. $tbl_track_login.' stats_login
  990. ON course_user.user_id = stats_login.user_id'.
  991. $inner.'
  992. WHERE course_user.course_code = \''.Database::escape_string($course_code).'\'
  993. AND stats_login.login_course_date IS NULL
  994. GROUP BY course_user.user_id';
  995. }
  996. $rs = api_sql_query($sql,__FILE__,__LINE__);
  997. $inactive_users = array();
  998. while($user = Database::fetch_array($rs))
  999. {
  1000. $inactive_users[] = $user['user_id'];
  1001. }
  1002. return $inactive_users;
  1003. }
  1004. public static function count_login_per_student($student_id, $course_code) {
  1005. $student_id = intval($student_id);
  1006. $course_code = addslashes($course_code);
  1007. $tbl_course_rel_user = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  1008. $sql = 'SELECT '.$student_id.'
  1009. FROM ' . $tbl_course_rel_user . '
  1010. WHERE access_user_id=' . $student_id . '
  1011. AND access_cours_code="' . $course_code . '"';
  1012. $rs = Database::query($sql, __FILE__, __LINE__);
  1013. $nb_login = Database::num_rows($rs);
  1014. return $nb_login;
  1015. }
  1016. public static function get_student_followed_by_drh($hr_dept_id) {
  1017. $hr_dept_id = intval($hr_dept_id);
  1018. $a_students = array ();
  1019. $tbl_organism = Database :: get_main_table(TABLE_MAIN_ORGANISM);
  1020. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  1021. $sql = 'SELECT DISTINCT user_id FROM '.$tbl_user.' as user
  1022. WHERE hr_dept_id='.$hr_dept_id;
  1023. $rs = Database::query($sql, __FILE__, __LINE__);
  1024. while($user = Database :: fetch_array($rs))
  1025. {
  1026. $a_students[$user['user_id']] = $user['user_id'];
  1027. }
  1028. return $a_students;
  1029. }
  1030. /**
  1031. * allow get average of test of scorm and lp
  1032. * @author isaac flores paz <florespaz@bidsoftperu.com>
  1033. * @param int the user id
  1034. * @param string the course id
  1035. */
  1036. public static function get_average_test_scorm_and_lp ($user_id,$course_id) {
  1037. //the score inside the Reporting table
  1038. $course_info=api_get_course_info($course_id);
  1039. $lp_table = Database :: get_course_table(TABLE_LP_MAIN,$course_info['dbName']);
  1040. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW,$course_info['dbName']);
  1041. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW,$course_info['dbName']);
  1042. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM,$course_info['dbName']);
  1043. $sql_type='SELECT id,lp_type FROM '.$lp_table;
  1044. $rs_type=Database::query($sql_type);
  1045. $average_data=0;
  1046. $count_loop=0;
  1047. while ($row_type=Database::fetch_array($rs_type)) {
  1048. if ($row_type['lp_type']==1) {//lp dokeos
  1049. $sql = "SELECT id FROM $lp_view_table WHERE user_id = '".intval($user_id)."' and lp_id='".$row_type['id']."'";
  1050. $rs_last_lp_view_id = Database::query($sql, __FILE__, __LINE__);
  1051. $lp_view_id = intval(Database::result($rs_last_lp_view_id,0,'id'));
  1052. $sql_list_view='SELECT li.max_score,lv.user_id,liw.score,(liw.score/li.max_score) as sum_data FROM '.$lp_item_table.' li INNER JOIN '.$lp_view_table.' lv
  1053. ON li.lp_id=lv.lp_id INNER JOIN '.$lp_item_view_table.' liw ON liw.lp_item_id=li.id WHERE lv.user_id="'.$user_id.'" AND li.item_type="quiz" AND liw.lp_view_id="'.$lp_view_id.'"';
  1054. $sum=0;
  1055. $tot=0;
  1056. $rs_list_view1=Database::query($sql_list_view,__FILE__,__LINE__);
  1057. while ($row_list_view=Database::fetch_array($rs_list_view1)) {
  1058. $sum=$sum+$row_list_view['sum_data'];
  1059. $tot++;
  1060. }
  1061. if ($tot==0) {
  1062. $tot=1;
  1063. }
  1064. $average_data1=$sum/$tot;
  1065. $sql_list_view='';
  1066. $rs_last_lp_view_id='';
  1067. } elseif ($row_type['lp_type']==2) {//lp scorm
  1068. $sql = "SELECT id FROM $lp_view_table WHERE user_id = '".intval($user_id)."' and lp_id='".$row_type['id']."'";
  1069. $rs_last_lp_view_id = Database::query($sql, __FILE__, __LINE__);
  1070. $lp_view_id = intval(Database::result($rs_last_lp_view_id,0,'id'));
  1071. $sql_list_view='SELECT li.max_score,lv.user_id,liw.score,((liw.score/li.max_score)*100) as sum_data FROM '.$lp_item_table.' li INNER JOIN '.$lp_view_table.' lv
  1072. ON li.lp_id=lv.lp_id INNER JOIN '.$lp_item_view_table.' liw ON liw.lp_item_id=li.id WHERE lv.user_id="'.$user_id.'" AND li.item_type="sco" AND liw.lp_view_id="'.$lp_view_id.'"';
  1073. $tot=0;
  1074. $sum=0;
  1075. $rs_list_view2=Database::query($sql_list_view,__FILE__,__LINE__);
  1076. while ($row_list_view=Database::fetch_array($rs_list_view2)) {
  1077. $sum=$sum+$row_list_view['sum_data'];
  1078. $tot++;
  1079. }
  1080. if ($tot==0) {
  1081. $tot=1;
  1082. }
  1083. $average_data2=$sum/$tot;
  1084. }
  1085. $average_data_sum=$average_data_sum+$average_data1+$average_data2;
  1086. $average_data2=0;
  1087. $average_data1=0;
  1088. $count_loop++;
  1089. }
  1090. if ((int)$count_loop > 0) {
  1091. $avg_student_score = round(($average_data_sum / $count_loop * 100), 2);
  1092. }
  1093. return $avg_student_score;
  1094. }
  1095. }
  1096. ?>