tracking.lib.php 48 KB

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