events.lib.inc.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * EVENTS LIBRARY
  5. *
  6. * This is the events library for Chamilo.
  7. * Include/require it in your code to use its functionality.
  8. * Functions of this library are used to record informations when some kind
  9. * of event occur. Each event has his own types of informations then each event
  10. * use its own function.
  11. *
  12. * @package chamilo.library
  13. */
  14. $TABLETRACK_LOGIN = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  15. $TABLETRACK_OPEN = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_OPEN);
  16. $TABLETRACK_ACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  17. $course_tracking_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  18. $TABLETRACK_DOWNLOADS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  19. $TABLETRACK_UPLOADS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
  20. $TABLETRACK_LINKS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  21. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  22. $TABLETRACK_LASTACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); //for "what's new" notification
  23. $TABLETRACK_DEFAULT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  24. /**
  25. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  26. * @desc Record information for open event (when homepage is opened)
  27. */
  28. function event_open()
  29. {
  30. global $_configuration;
  31. global $TABLETRACK_OPEN;
  32. // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information
  33. // $_SERVER['HTTP_USER_AGENT'] : will provide browser and os information
  34. // $_SERVER['HTTP_REFERER'] : provide information about refering url
  35. if (isset($_SERVER['HTT_REFERER'])) {
  36. $referer = Database::escape_string($_SERVER['HTTP_REFERER']);
  37. } else {
  38. $referer = '';
  39. }
  40. // record informations only if user comes from another site
  41. //if(!eregi($_configuration['root_web'],$referer))
  42. $pos = strpos($referer, $_configuration['root_web']);
  43. if ($pos === false && $referer != '') {
  44. $ip = api_get_real_ip();
  45. $remhost = @ getHostByAddr($ip);
  46. if ($remhost == $ip)
  47. $remhost = "Unknown"; // don't change this
  48. $reallyNow = api_get_utc_datetime();
  49. $sql = "INSERT INTO ".$TABLETRACK_OPEN."
  50. (open_remote_host,
  51. open_agent,
  52. open_referer,
  53. open_date)
  54. VALUES
  55. ('".$remhost."',
  56. '".Database::escape_string($_SERVER['HTTP_USER_AGENT'])."', '".Database::escape_string($referer)."', '$reallyNow')";
  57. $res = Database::query($sql);
  58. }
  59. return 1;
  60. }
  61. /**
  62. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  63. * @desc Record information for login event
  64. * (when an user identifies himself with username & password)
  65. */
  66. function event_login()
  67. {
  68. global $TABLETRACK_LOGIN;
  69. global $_user;
  70. // @todo use api_get_user_info();
  71. //$userInfo = api_get_user_info();
  72. $userInfo = $_user;
  73. if (empty($userInfo)) {
  74. return false;
  75. }
  76. $userId = api_get_user_id();
  77. $reallyNow = api_get_utc_datetime();
  78. $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, login_ip, login_date, logout_date) VALUES
  79. ('".$userId."',
  80. '".Database::escape_string(api_get_real_ip())."',
  81. '".$reallyNow."',
  82. '".$reallyNow."'
  83. )";
  84. Database::query($sql);
  85. // Auto subscribe
  86. $user_status = $userInfo['status'] == SESSIONADMIN ? 'sessionadmin' :
  87. $userInfo['status'] == COURSEMANAGER ? 'teacher' :
  88. $userInfo['status'] == DRH ? 'DRH' : 'student';
  89. $autoSubscribe = api_get_setting($user_status.'_autosubscribe');
  90. if ($autoSubscribe) {
  91. $autoSubscribe = explode('|', $autoSubscribe);
  92. foreach ($autoSubscribe as $code) {
  93. if (CourseManager::course_exists($code)) {
  94. CourseManager::subscribe_user($userId, $code);
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  101. * @desc Record information for access event for courses
  102. */
  103. function event_access_course() {
  104. global $TABLETRACK_ACCESS, $TABLETRACK_LASTACCESS;
  105. $id_session = api_get_session_id();
  106. $now = api_get_utc_datetime();
  107. $_cid = api_get_course_id();
  108. $user_id = api_get_user_id();
  109. if ($user_id) {
  110. $user_id = "'".$user_id."'";
  111. } else {
  112. $user_id = "0"; // no one
  113. }
  114. $sql = "INSERT INTO ".$TABLETRACK_ACCESS." (access_user_id, access_cours_code, access_date, access_session_id) VALUES
  115. (".$user_id.", '".$_cid."', '".$now."','".$id_session."')";
  116. Database::query($sql);
  117. // added for "what's new" notification
  118. $sql = "UPDATE $TABLETRACK_LASTACCESS SET access_date = '$now'
  119. WHERE access_user_id = $user_id AND access_cours_code = '$_cid' AND access_tool IS NULL AND access_session_id=".$id_session;
  120. Database::query($sql);
  121. if (Database::affected_rows() == 0) {
  122. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, access_cours_code, access_date, access_session_id)
  123. VALUES (".$user_id.", '".$_cid."', '$now', '".$id_session."')";
  124. Database::query($sql);
  125. }
  126. // end "what's new" notification
  127. return 1;
  128. }
  129. /**
  130. * @param string $tool tool name of the tool (name in mainDb.accueil table)
  131. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  132. * @desc Record information for access event for tools
  133. *
  134. * $tool can take this values :
  135. * Links, Calendar, Document, Announcements,
  136. * Group, Video, Works, Users, Exercices, Course Desc
  137. * ...
  138. * Values can be added if new modules are created (15char max)
  139. * I encourage to use $nameTool as $tool when calling this function
  140. *
  141. * Functionality for "what's new" notification is added by Toon Van Hoecke
  142. */
  143. function event_access_tool($tool, $id_session = 0)
  144. {
  145. global $_configuration;
  146. global $_cid;
  147. global $TABLETRACK_ACCESS;
  148. global $_course;
  149. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  150. $id_session = api_get_session_id();
  151. $tool = Database::escape_string($tool);
  152. $reallyNow = api_get_utc_datetime();
  153. $user_id = api_get_user_id();
  154. // record information
  155. // only if user comes from the course $_cid
  156. //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
  157. //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
  158. $coursePath = isset($_course['path']) ? $_course['path'] : null;
  159. $pos = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$coursePath)) : false;
  160. // added for "what's new" notification
  161. $pos2 = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower($_configuration['root_web']."index")) : false;
  162. // end "what's new" notification
  163. if ($pos !== false || $pos2 !== false) {
  164. $sql = "INSERT INTO ".$TABLETRACK_ACCESS."
  165. (access_user_id,
  166. access_cours_code,
  167. access_tool,
  168. access_date,
  169. access_session_id
  170. )
  171. VALUES
  172. (".$user_id.",".// Don't add ' ' around value, it's already done.
  173. "'".$_cid."' ,
  174. '".$tool."',
  175. '".$reallyNow."',
  176. '".$id_session."')";
  177. Database::query($sql);
  178. }
  179. // "what's new" notification
  180. $sql = "UPDATE $TABLETRACK_LASTACCESS
  181. SET access_date = '$reallyNow'
  182. WHERE access_user_id = ".$user_id." AND access_cours_code = '".$_cid."' AND access_tool = '".$tool."' AND access_session_id=".$id_session;
  183. Database::query($sql);
  184. if (Database::affected_rows() == 0) {
  185. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id,access_cours_code,access_tool, access_date, access_session_id)
  186. VALUES (".$user_id.", '".$_cid."' , '$tool', '$reallyNow', $id_session)";
  187. Database::query($sql);
  188. }
  189. return 1;
  190. }
  191. /**
  192. * @param doc_id id of document (id in mainDb.document table)
  193. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  194. * @desc Record information for download event
  195. * (when an user click to d/l a document)
  196. * it will be used in a redirection page
  197. * bug fixed: Roan Embrechts
  198. * Roan:
  199. * The user id is put in single quotes,
  200. * (why? perhaps to prevent sql insertion hacks?)
  201. * and later again.
  202. * Doing this twice causes an error, I remove one of them.
  203. */
  204. function event_download($doc_url)
  205. {
  206. $tbl_stats_downloads = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  207. $doc_url = Database::escape_string($doc_url);
  208. $reallyNow = api_get_utc_datetime();
  209. $user_id = "'".api_get_user_id()."'";
  210. $_cid = api_get_course_id();
  211. $sql = "INSERT INTO $tbl_stats_downloads (
  212. down_user_id,
  213. down_cours_id,
  214. down_doc_path,
  215. down_date,
  216. down_session_id
  217. )
  218. VALUES (
  219. ".$user_id.",
  220. '".$_cid."',
  221. '".$doc_url."',
  222. '".$reallyNow."',
  223. '".api_get_session_id()."'
  224. )";
  225. Database::query($sql);
  226. return 1;
  227. }
  228. /**
  229. * @param link_id (id in coursDb liens table)
  230. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  231. * @desc Record information for link event (when an user click on an added link)
  232. * it will be used in a redirection page
  233. */
  234. function event_link($link_id)
  235. {
  236. global $TABLETRACK_LINKS;
  237. $reallyNow = api_get_utc_datetime();
  238. $user_id = api_get_user_id();
  239. $sql = "INSERT INTO ".$TABLETRACK_LINKS."
  240. ( links_user_id,
  241. links_cours_id,
  242. links_link_id,
  243. links_date,
  244. links_session_id
  245. ) VALUES (
  246. ".$user_id.",
  247. '".api_get_course_id()."',
  248. '".Database::escape_string($link_id)."',
  249. '".$reallyNow."',
  250. '".api_get_session_id()."'
  251. )";
  252. Database::query($sql);
  253. return 1;
  254. }
  255. /**
  256. * Update the TRACK_E_EXERCICES exercises
  257. *
  258. * @param int exeid id of the attempt
  259. * @param int exo_id exercise id
  260. * @param mixed result score
  261. * @param int weighting ( higher score )
  262. * @param int duration ( duration of the attempt in seconds )
  263. * @param int session_id
  264. * @param int learnpath_id (id of the learnpath)
  265. * @param int learnpath_item_id (id of the learnpath_item)
  266. *
  267. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  268. * @author Julio Montoya Armas <gugli100@gmail.com> Reworked 2010
  269. * @desc Record result of user when an exercice was done
  270. */
  271. function update_event_exercice(
  272. $exeid,
  273. $exo_id,
  274. $score,
  275. $weighting,
  276. $session_id,
  277. $learnpath_id = 0,
  278. $learnpath_item_id = 0,
  279. $learnpath_item_view_id = 0,
  280. $duration = 0,
  281. $question_list = array(),
  282. $status = '',
  283. $remind_list = array(),
  284. $end_date = null
  285. ) {
  286. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  287. global $debug;
  288. if ($debug) error_log('Called to update_event_exercice');
  289. if ($debug) error_log('duration:' . $duration);
  290. if ($exeid != '') {
  291. /*
  292. * Code commented due BT#8423 do not change the score to 0.
  293. *
  294. * Validation in case of fraud with actived control time
  295. if (!exercise_time_control_is_valid($exo_id, $learnpath_id, $learnpath_item_id)) {
  296. $score = 0;
  297. }
  298. */
  299. if (!isset($status) || empty($status)) {
  300. $status = '';
  301. } else {
  302. $status = Database::escape_string($status);
  303. }
  304. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  305. if (!empty($question_list)) {
  306. $question_list = array_map('intval', $question_list);
  307. }
  308. if (!empty($remind_list)) {
  309. $remind_list = array_map('intval', $remind_list);
  310. $remind_list = array_filter($remind_list);
  311. $remind_list = implode(",", $remind_list);
  312. } else {
  313. $remind_list = '';
  314. }
  315. if (empty($end_date)) {
  316. $end_date = api_get_utc_datetime();
  317. }
  318. $sql = "UPDATE $TABLETRACK_EXERCICES SET
  319. exe_exo_id = '".Database::escape_string($exo_id)."',
  320. exe_result = '".Database::escape_string($score)."',
  321. exe_weighting = '".Database::escape_string($weighting)."',
  322. session_id = '".Database::escape_string($session_id)."',
  323. orig_lp_id = '".Database::escape_string($learnpath_id)."',
  324. orig_lp_item_id = '".Database::escape_string($learnpath_item_id)."',
  325. orig_lp_item_view_id = '".Database::escape_string($learnpath_item_view_id)."',
  326. exe_duration = '".Database::escape_string($duration)."',
  327. exe_date = '".$end_date."',
  328. status = '".$status."',
  329. questions_to_check = '".$remind_list."',
  330. data_tracking = '".implode(',', $question_list)."'
  331. WHERE exe_id = '".Database::escape_string($exeid)."'";
  332. $res = Database::query($sql);
  333. if ($debug) error_log('update_event_exercice called');
  334. if ($debug) error_log("$sql");
  335. //Deleting control time session track
  336. //exercise_time_control_delete($exo_id);
  337. return $res;
  338. } else {
  339. return false;
  340. }
  341. }
  342. /**
  343. * This function creates an empty Exercise in STATISTIC_TRACK_E_EXERCICES table.
  344. * After that in exercise_result.php we call the update_event_exercice() to update the exercise
  345. * @return $id the last id registered, or false on error
  346. * @author Julio Montoya <gugli100@gmail.com>
  347. * @desc Record result of user when an exercice was done
  348. * @deprecated this function seems to be deprecated
  349. */
  350. function create_event_exercice($exo_id)
  351. {
  352. if (empty($exo_id) or (intval($exo_id)!=$exo_id)) {
  353. return false;
  354. }
  355. $tbl_track_exe = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  356. $tbl_exe = Database::get_course_table(TABLE_QUIZ_TEST);
  357. $uid = api_get_user_id();
  358. $course_id = api_get_course_int_id();
  359. // First, check the exercise exists
  360. $sql_exe_id="SELECT exercises.id FROM $tbl_exe as exercises WHERE c_id = $course_id AND exercises.id=$exo_id";
  361. $res_exe_id=Database::query($sql_exe_id);
  362. if ($res_exe_id === false) { return false; } //sql error
  363. if (Database::num_rows($res_exe_id)<1) { return false;} //exe not found
  364. $row_exe_id=Database::fetch_row($res_exe_id);
  365. $exercise_id = intval($row_exe_id[0]);
  366. // Second, check if the record exists in the database (looking for incomplete records)
  367. $sql = "SELECT exe_id FROM $tbl_track_exe WHERE exe_exo_id = $exo_id AND " .
  368. "exe_user_id = $uid AND " .
  369. "exe_cours_id = '".api_get_course_id()."' AND " .
  370. "status = 'incomplete' AND ".
  371. "session_id = ".api_get_session_id();
  372. $res = Database::query($sql);
  373. if ($res === false) {return false;}
  374. if (Database::num_rows($res) > 0) {
  375. $row = Database::fetch_array($res);
  376. return $row['exe_id'];
  377. }
  378. // No record was found, so create one
  379. // get expire time to insert into the tracking record
  380. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  381. $current_expired_time_key = get_time_control_key($exercise_id);
  382. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  383. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  384. } else {
  385. $expired_date = '0000-00-00 00:00:00';
  386. }
  387. $sql = "INSERT INTO $tbl_track_exe ( exe_user_id, exe_cours_id, expired_time_control, exe_exo_id, session_id)
  388. VALUES ( $uid, '".api_get_course_id()."' ,'$expired_date','$exo_id','".api_get_session_id()."')";
  389. Database::query($sql);
  390. $id= Database::insert_id();
  391. return $id;
  392. }
  393. /**
  394. * Record an event for this attempt at answering an exercise
  395. * @param float Score achieved
  396. * @param string Answer given
  397. * @param integer Question ID
  398. * @param integer Exercise ID
  399. * @param integer Position
  400. * @return boolean Result of the insert query
  401. */
  402. function exercise_attempt($score, $answer, $question_id, $exe_id, $position, $exercise_id = 0, $nano = null)
  403. {
  404. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  405. global $debug, $learnpath_id, $learnpath_item_id;
  406. $score = Database::escape_string($score);
  407. $answer = Database::escape_string($answer);
  408. $question_id = Database::escape_string($question_id);
  409. $exe_id = Database::escape_string($exe_id);
  410. $position = Database::escape_string($position);
  411. $now = api_get_utc_datetime();
  412. $user_id = api_get_user_id();
  413. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  414. if ($debug) error_log("----- entering exercise_attempt() function ------");
  415. if ($debug) error_log("answer: $answer");
  416. if ($debug) error_log("score: $score");
  417. if ($debug) error_log("question_id : $question_id");
  418. if ($debug) error_log("position: $position");
  419. //Validation in case of fraud with actived control time
  420. if (!exercise_time_control_is_valid($exercise_id, $learnpath_id, $learnpath_item_id)) {
  421. if ($debug) error_log("exercise_time_control_is_valid is false");
  422. /*$score = 0;
  423. $answer = 0;*/
  424. }
  425. if (!empty($user_id)) {
  426. $user_id = "'".$user_id."'";
  427. } else {
  428. // anonymous
  429. $user_id = api_get_anonymous_id();
  430. }
  431. $file = '';
  432. if (isset($nano)) {
  433. $file = Database::escape_string(basename($nano->load_filename_if_exists(false)));
  434. }
  435. $course_code = api_get_course_id();
  436. $session_id = api_get_session_id();
  437. if (!empty($question_id) && !empty($exe_id) && !empty($user_id)) {
  438. // Check if attempt exists
  439. $sql = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT
  440. WHERE
  441. course_code = '$course_code' AND
  442. session_id = $session_id AND
  443. exe_id = $exe_id AND
  444. user_id = $user_id AND
  445. question_id = $question_id AND
  446. position = $position";
  447. $result = Database::query($sql);
  448. if (Database::num_rows($result)) {
  449. if ($debug) error_log("Attempt already exist: exe_id: $exe_id - user_id:$user_id - question_id:$question_id");
  450. //The attempt already exist do not update use update_event_exercice() instead
  451. return false;
  452. }
  453. $sql = "INSERT INTO $TBL_TRACK_ATTEMPT (exe_id, user_id, question_id, answer, marks, course_code, session_id, position, tms, filename)
  454. VALUES (
  455. ".$exe_id.",
  456. ".$user_id.",
  457. '".$question_id."',
  458. '".$answer."',
  459. '".$score."',
  460. '".$course_code."',
  461. '".$session_id."',
  462. '".$position."',
  463. '".$now."',
  464. '".$file."'
  465. )";
  466. if ($debug) error_log("Saving question attempt: ");
  467. if ($debug) error_log($sql);
  468. $res = Database::query($sql);
  469. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  470. $recording_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  471. if ($debug) error_log("Saving e attempt recording ");
  472. $recording_changes = "INSERT INTO $recording_table (exe_id, question_id, marks, insert_date, author, session_id)
  473. VALUES ('$exe_id','$question_id','$score','".api_get_utc_datetime()."','', '".api_get_session_id()."') ";
  474. Database::query($recording_changes);
  475. }
  476. return $res;
  477. } else {
  478. return false;
  479. }
  480. }
  481. /**
  482. * Record an hotspot spot for this attempt at answering an hotspot question
  483. * @param int Exercise ID
  484. * @param int Question ID
  485. * @param int Answer ID
  486. * @param int Whether this answer is correct (1) or not (0)
  487. * @param string Coordinates of this point (e.g. 123;324)
  488. * @return boolean Result of the insert query
  489. * @uses Course code and user_id from global scope $_cid and $_user
  490. */
  491. function exercise_attempt_hotspot($exe_id, $question_id, $answer_id, $correct, $coords, $exerciseId = 0)
  492. {
  493. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  494. global $safe_lp_id, $safe_lp_item_id;
  495. //Validation in case of fraud with actived control time
  496. if (!exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
  497. //$correct = 0;
  498. }
  499. $tbl_track_e_hotspot = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  500. $sql = "INSERT INTO $tbl_track_e_hotspot (hotspot_user_id, hotspot_course_code, hotspot_exe_id, hotspot_question_id, hotspot_answer_id, hotspot_correct, hotspot_coordinate)".
  501. " VALUES ('" . api_get_user_id() . "'," .
  502. " '" . api_get_course_id() . "', " .
  503. " '" . Database :: escape_string($exe_id) . "', " .
  504. " '" . Database :: escape_string($question_id) . "'," .
  505. " '" . Database :: escape_string($answer_id) . "'," .
  506. " '" . Database :: escape_string($correct) . "'," .
  507. " '" . Database :: escape_string($coords) . "')";
  508. return $result = Database::query($sql);
  509. }
  510. /**
  511. * Records information for common (or admin) events (in the track_e_default table)
  512. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  513. * @param string Type of event
  514. * @param string Type of value
  515. * @param string Value
  516. * @param string Timestamp (defaults to null)
  517. * @param integer User ID (defaults to null)
  518. * @param string Course code (defaults to null)
  519. * @param integer Session ID (defaults to null - only stored from 1.10 onwards)
  520. * @assert ('','','') === false
  521. */
  522. function event_system(
  523. $event_type,
  524. $event_value_type,
  525. $event_value,
  526. $datetime = null,
  527. $user_id = null,
  528. $course_code = null,
  529. $session_id = null
  530. ) {
  531. global $TABLETRACK_DEFAULT;
  532. if (empty($event_type)) {
  533. return false;
  534. }
  535. $event_type = Database::escape_string($event_type);
  536. $event_value_type = Database::escape_string($event_value_type);
  537. // Clean the user_info.
  538. if ($event_value_type == LOG_USER_OBJECT) {
  539. if (is_array($event_value)) {
  540. unset($event_value['complete_name']);
  541. unset($event_value['firstName']);
  542. unset($event_value['lastName']);
  543. unset($event_value['avatar_small']);
  544. unset($event_value['avatar']);
  545. unset($event_value['password']);
  546. unset($event_value['lastLogin']);
  547. unset($event_value['picture_uri']);
  548. }
  549. }
  550. if (in_array(
  551. $event_type,
  552. array(
  553. LOG_SESSION_ADD_USER_COURSE,
  554. LOG_SESSION_DELETE_USER_COURSE,
  555. LOG_SESSION_DELETE_USER,
  556. LOG_SESSION_ADD_COURSE,
  557. LOG_SESSION_DELETE_COURSE,
  558. )
  559. )
  560. ){
  561. $event_value = array(
  562. 'user_id' => $event_value,
  563. 'course_code' => $course_code,
  564. 'session_id' => $session_id,
  565. );
  566. if (in_array($event_type, array(LOG_SESSION_ADD_COURSE, LOG_SESSION_DELETE_COURSE))) {
  567. $event_value['user_id'] = api_get_user_id();
  568. }
  569. }
  570. if (is_array($event_value)) {
  571. $event_value = serialize($event_value);
  572. }
  573. $event_value = Database::escape_string($event_value);
  574. $course_info = api_get_course_info($course_code);
  575. if (!empty($course_info)) {
  576. $course_id = $course_info['real_id'];
  577. $course_code = $course_info['code'];
  578. $course_code = Database::escape_string($course_code);
  579. } else {
  580. $course_id = null;
  581. $course_code = null;
  582. }
  583. if (!isset($datetime)) {
  584. $datetime = api_get_utc_datetime();
  585. }
  586. $datetime = Database::escape_string($datetime);
  587. if (!isset($user_id)) {
  588. $user_id = api_get_user_id();
  589. }
  590. $user_id = intval($user_id);
  591. $sql = "INSERT INTO $TABLETRACK_DEFAULT (default_user_id, default_cours_code, c_id, default_date, default_event_type, default_value_type, default_value)
  592. VALUES ('$user_id', '$course_code', '$course_id', '$datetime', '$event_type', '$event_value_type', '$event_value')";
  593. Database::query($sql);
  594. return true;
  595. }
  596. /**
  597. * Get every email stored in the database
  598. *
  599. * @param int $etId
  600. * @return array
  601. * @assert () !== false
  602. */
  603. function get_all_event_types() {
  604. global $event_config;
  605. $sql = 'SELECT etm.id, event_type_name, activated, language_id, message, subject, dokeos_folder
  606. FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' etm
  607. INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l
  608. ON etm.language_id = l.id';
  609. $events_types = Database::store_result(Database::query($sql), 'ASSOC');
  610. $to_return = array();
  611. foreach ($events_types as $et) {
  612. $et['nameLangVar'] = $event_config[$et["event_type_name"]]["name_lang_var"];
  613. $et['descLangVar'] = $event_config[$et["event_type_name"]]["desc_lang_var"];
  614. $to_return[] = $et;
  615. }
  616. return $to_return;
  617. }
  618. /**
  619. * Get users linked to an event
  620. *
  621. * @param string $event_name
  622. *
  623. * @return string
  624. */
  625. function get_users_subscribed_to_event($event_name) {
  626. $event_name = Database::escape_string($event_name);
  627. $sql = 'SELECT u.*
  628. FROM '.Database::get_main_table(TABLE_MAIN_USER).' u,'
  629. .Database::get_main_table(TABLE_MAIN_EVENT_TYPE).' e,'
  630. .Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' ue
  631. WHERE ue.user_id = u.user_id
  632. AND e.name = "'.$event_name.'"
  633. AND e.id = ue.event_type_id';
  634. $return = Database::store_result(Database::query($sql),'ASSOC');
  635. return json_encode($return);
  636. }
  637. /**
  638. * Get the users related to one event
  639. *
  640. * @param string $event_name
  641. */
  642. function get_event_users($event_name)
  643. {
  644. $event_name = Database::escape_string($event_name);
  645. $sql = 'SELECT user.user_id, user.firstname, user.lastname
  646. FROM '.Database::get_main_table(TABLE_MAIN_USER).' user
  647. JOIN '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' relUser
  648. ON relUser.user_id = user.user_id
  649. WHERE user.status <> '.ANONYMOUS.' AND relUser.event_type_name = "'.$event_name.'"';
  650. $user_list = Database::store_result(Database::query($sql), 'ASSOC');
  651. return json_encode($user_list);
  652. }
  653. /**
  654. * @param int $user_id
  655. * @param string $event_type
  656. * @return array|bool
  657. */
  658. function get_events_by_user_and_type($user_id, $event_type) {
  659. global $TABLETRACK_DEFAULT;
  660. $user_id = intval($user_id);
  661. $event_type = Database::escape_string($event_type);
  662. $sql = "SELECT * FROM $TABLETRACK_DEFAULT
  663. WHERE default_value_type = 'user_id' AND
  664. default_value = $user_id AND
  665. default_event_type = '$event_type'
  666. ORDER BY default_date ";
  667. $result = Database::query($sql);
  668. if ($result) {
  669. return Database::store_result($result, 'ASSOC');
  670. }
  671. return false;
  672. }
  673. /**
  674. * @param int $user_id
  675. * @param string $event_type
  676. * @return mixed
  677. */
  678. function get_latest_event_by_user_and_type($user_id, $event_type) {
  679. $result = get_events_by_user_and_type($user_id, $event_type);
  680. if ($result && !empty($result)) {
  681. return $result[0];
  682. }
  683. }
  684. /**
  685. * Save the new message for one event and for one language
  686. *
  687. * @param string $eventName
  688. * @param array $users
  689. * @param string $message
  690. * @param string $subject
  691. * @param string $eventMessageLanguage
  692. * @param int $activated
  693. */
  694. function save_event_type_message($event_name, $users, $message, $subject, $event_message_language, $activated)
  695. {
  696. $event_name = Database::escape_string($event_name);
  697. $activated = intval($activated);
  698. $event_message_language = Database::escape_string($event_message_language);
  699. // Deletes then re-adds the users linked to the event
  700. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_name = "'.$event_name.'" ';
  701. Database::query($sql);
  702. foreach ($users as $user) {
  703. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_name) VALUES('.intval($user).',"'. $event_name.'")';
  704. Database::query($sql);
  705. }
  706. $language_id = api_get_language_id($event_message_language);
  707. // check if this template in this language already exists or not
  708. $sql = 'SELECT COUNT(id) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  709. WHERE event_type_name = "'.$event_name.'" AND language_id = '.$language_id;
  710. $sql = Database::store_result(Database::query($sql),'ASSOC');
  711. // if already exists, we update
  712. if ($sql[0]["total"] > 0) {
  713. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  714. SET message = "'.Database::escape_string($message).'",
  715. subject = "'.Database::escape_string($subject).'",
  716. activated = '.$activated.'
  717. WHERE event_type_name = "'.$event_name.'" AND language_id = (SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  718. WHERE dokeos_folder = "'.$event_message_language.'")';
  719. Database::query($sql);
  720. } else { // else we create a new record
  721. // gets the language_-_id
  722. $lang_id = '(SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  723. WHERE dokeos_folder = "'.$event_message_language.'")';
  724. $lang_id = Database::store_result(Database::query($lang_id),'ASSOC');
  725. if (!empty($lang_id[0]["id"])) {
  726. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' (event_type_name, language_id, message, subject, activated)
  727. VALUES("'.$event_name.'", '.$lang_id[0]["id"].', "'.Database::escape_string($message).'", "'.Database::escape_string($subject).'", '.$activated.')';
  728. Database::query($sql);
  729. }
  730. }
  731. // set activated at every save
  732. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  733. SET activated = '.$activated.'
  734. WHERE event_type_name = "'.$event_name.'"';
  735. Database::query($sql);
  736. }
  737. /**
  738. * @param $etId
  739. * @param $users
  740. * @param $message
  741. * @param $subject
  742. */
  743. function eventType_mod($etId, $users, $message, $subject) {
  744. $etId = intval($etId);
  745. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_id = '.$etId.' ';
  746. Database::query($sql);
  747. foreach ($users as $user) {
  748. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_id)
  749. VALUES('.intval($user).','.$etId.') ';
  750. Database::query($sql);
  751. }
  752. $sql = 'UPDATE '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_MESSAGE).'
  753. SET message = "'.Database::escape_string($message).'",
  754. subject = "'.Database::escape_string($subject).'"
  755. WHERE event_type_id = '.$etId.'';
  756. Database::query($sql);
  757. }
  758. /**
  759. * Gets the last attempt of an exercise based in the exe_id
  760. *
  761. * @param $exe_id
  762. * @return mixed
  763. */
  764. function get_last_attempt_date_of_exercise($exe_id)
  765. {
  766. $exe_id = intval($exe_id);
  767. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  768. $sql = 'SELECT max(tms) as last_attempt_date FROM '.$track_attempts.'
  769. WHERE exe_id='.$exe_id;
  770. $rs_last_attempt = Database::query($sql);
  771. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  772. //Get the date of last attempt
  773. $last_attempt_date = $row_last_attempt['last_attempt_date'];
  774. return $last_attempt_date;
  775. }
  776. /**
  777. * Gets how many attempts exists by user, exercise, learning path
  778. * @param int user id
  779. * @param int exercise id
  780. * @param int lp id
  781. * @param int lp item id
  782. * @param int lp item view id
  783. */
  784. function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id,$lp_item_view_id) {
  785. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  786. $user_id = intval($user_id);
  787. $exerciseId = intval($exerciseId);
  788. $lp_id = intval($lp_id);
  789. $lp_item_id = intval($lp_item_id);
  790. $lp_item_view_id= intval($lp_item_view_id);
  791. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  792. exe_exo_id = $exerciseId AND
  793. exe_user_id = $user_id AND
  794. status != 'incomplete' AND
  795. orig_lp_id = $lp_id AND
  796. orig_lp_item_id = $lp_item_id AND
  797. orig_lp_item_view_id = $lp_item_view_id AND
  798. exe_cours_id = '".api_get_course_id()."' AND
  799. session_id = '" . api_get_session_id() . "'";
  800. $query = Database::query($sql);
  801. if (Database::num_rows($query) > 0 ) {
  802. $attempt = Database :: fetch_array($query,'ASSOC');
  803. return $attempt['count'];
  804. } else {
  805. return 0;
  806. }
  807. }
  808. /**
  809. * @param int $user_id
  810. * @param int $exerciseId
  811. * @param int $lp_id
  812. * @param int $lp_item_id
  813. * @return int
  814. */
  815. function get_attempt_count_not_finished($user_id, $exerciseId, $lp_id, $lp_item_id)
  816. {
  817. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  818. $user_id = intval($user_id);
  819. $exerciseId = intval($exerciseId);
  820. $lp_id = intval($lp_id);
  821. $lp_item_id = intval($lp_item_id);
  822. //$lp_item_view_id= intval($lp_item_view_id);
  823. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  824. exe_exo_id = $exerciseId AND
  825. exe_user_id = $user_id AND
  826. status != 'incomplete' AND
  827. orig_lp_id = $lp_id AND
  828. orig_lp_item_id = $lp_item_id AND
  829. exe_cours_id = '".api_get_course_id()."' AND
  830. session_id = '" . api_get_session_id() . "'";
  831. $query = Database::query($sql);
  832. if (Database::num_rows($query) > 0 ) {
  833. $attempt = Database :: fetch_array($query,'ASSOC');
  834. return $attempt['count'];
  835. } else {
  836. return 0;
  837. }
  838. }
  839. /**
  840. * Deletes a learning path view for a student
  841. * @param int $user_id
  842. * @param int $lp_id
  843. * @param array $course
  844. * @param int $session_id
  845. */
  846. function delete_student_lp_events($user_id, $lp_id, $course, $session_id)
  847. {
  848. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  849. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  850. $lpInteraction = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  851. $lpObjective = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  852. $course_id = $course['real_id'];
  853. if (empty($course_id)) {
  854. $course_id = api_get_course_int_id();
  855. }
  856. $track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  857. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  858. $recording_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  859. $user_id = intval($user_id);
  860. $lp_id = intval($lp_id);
  861. $session_id = intval($session_id);
  862. //Make sure we have the exact lp_view_id
  863. $sql = "SELECT id FROM $lp_view_table
  864. WHERE
  865. c_id = $course_id AND
  866. user_id = $user_id AND
  867. lp_id = $lp_id AND
  868. session_id = $session_id ";
  869. $result = Database::query($sql);
  870. if (Database::num_rows($result)) {
  871. $view = Database::fetch_array($result, 'ASSOC');
  872. $lp_view_id = $view['id'];
  873. $sql = "DELETE FROM $lp_item_view_table
  874. WHERE c_id = $course_id AND lp_view_id = $lp_view_id ";
  875. Database::query($sql);
  876. $sql = "DELETE FROM $lpInteraction
  877. WHERE c_id = $course_id AND lp_iv_id = $lp_view_id ";
  878. Database::query($sql);
  879. $sql = "DELETE FROM $lpObjective
  880. WHERE c_id = $course_id AND lp_iv_id = $lp_view_id ";
  881. Database::query($sql);
  882. }
  883. $sql = "DELETE FROM $lp_view_table
  884. WHERE
  885. c_id = $course_id AND
  886. user_id = $user_id AND
  887. lp_id= $lp_id AND
  888. session_id = $session_id
  889. ";
  890. Database::query($sql);
  891. $sql = "SELECT exe_id FROM $track_e_exercises
  892. WHERE
  893. exe_user_id = $user_id AND
  894. session_id = $session_id AND
  895. exe_cours_id = '{$course['code']}' AND
  896. orig_lp_id = $lp_id
  897. ";
  898. $result = Database::query($sql);
  899. $exe_list = array();
  900. while ($row = Database::fetch_array($result, 'ASSOC')) {
  901. $exe_list[] = $row['exe_id'];
  902. }
  903. if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) {
  904. $sql = "DELETE FROM $track_e_exercises WHERE exe_id IN (".implode(',',$exe_list).")";
  905. Database::query($sql);
  906. $sql = "DELETE FROM $track_attempts WHERE exe_id IN (".implode(',',$exe_list).")";
  907. Database::query($sql);
  908. $sql = "DELETE FROM $recording_table WHERE exe_id IN (".implode(',',$exe_list).")";
  909. Database::query($sql);
  910. }
  911. event_system(
  912. LOG_LP_ATTEMPT_DELETE,
  913. LOG_LP_ID,
  914. $lp_id,
  915. null,
  916. null,
  917. $course['code'],
  918. $session_id
  919. );
  920. }
  921. /**
  922. * Delete all exercise attempts (included in LP or not)
  923. *
  924. * @param int user id
  925. * @param int exercise id
  926. * @param string course code
  927. * @param int session id
  928. */
  929. function delete_all_incomplete_attempts($user_id, $exercise_id, $course_code, $session_id = 0)
  930. {
  931. $track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  932. $user_id = intval($user_id);
  933. $exercise_id = intval($exercise_id);
  934. $course_code = Database::escape_string($course_code);
  935. $session_id = intval($session_id);
  936. if (!empty($user_id) && !empty($exercise_id) && !empty($course_code)) {
  937. $sql = "DELETE FROM $track_e_exercises
  938. WHERE exe_user_id = $user_id AND exe_exo_id = $exercise_id AND exe_cours_id = '$course_code' AND session_id = $session_id AND status = 'incomplete' ";
  939. Database::query($sql);
  940. }
  941. event_system(LOG_EXERCISE_RESULT_DELETE, LOG_EXERCISE_AND_USER_ID, $exercise_id.'-'.$user_id, null, null, $course_code, $session_id);
  942. }
  943. /**
  944. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  945. * @param int exercise id
  946. * @param string course code
  947. * @param int session id
  948. * @return array with the results
  949. */
  950. function get_all_exercise_results($exercise_id, $course_code, $session_id = 0, $load_question_list = true, $user_id = null) {
  951. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  952. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  953. $course_code = Database::escape_string($course_code);
  954. $exercise_id = intval($exercise_id);
  955. $session_id = intval($session_id);
  956. $user_condition = null;
  957. if (!empty($user_id)) {
  958. $user_id = intval($user_id);
  959. $user_condition = "AND exe_user_id = $user_id ";
  960. }
  961. $sql = "SELECT * FROM $TABLETRACK_EXERCICES
  962. WHERE
  963. status = '' AND
  964. exe_cours_id = '$course_code' AND
  965. exe_exo_id = '$exercise_id' AND
  966. session_id = $session_id AND
  967. orig_lp_id =0 AND
  968. orig_lp_item_id = 0
  969. $user_condition
  970. ORDER BY exe_id";
  971. $res = Database::query($sql);
  972. $list = array();
  973. while($row = Database::fetch_array($res,'ASSOC')) {
  974. $list[$row['exe_id']] = $row;
  975. if ($load_question_list) {
  976. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  977. $res_question = Database::query($sql);
  978. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  979. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  980. }
  981. }
  982. }
  983. return $list;
  984. }
  985. /**
  986. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  987. * @param string course code
  988. * @param int session id
  989. * @return array with the results
  990. *
  991. */
  992. function get_all_exercise_results_by_course($course_code, $session_id = 0, $get_count = true)
  993. {
  994. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  995. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  996. $course_code = Database::escape_string($course_code);
  997. $session_id = intval($session_id);
  998. $select = '*';
  999. if ($get_count) {
  1000. $select = 'count(*) as count';
  1001. }
  1002. $sql = "SELECT $select FROM $table_track_exercises
  1003. WHERE status = '' AND exe_cours_id = '$course_code' AND session_id = $session_id AND orig_lp_id = 0 AND orig_lp_item_id = 0
  1004. ORDER BY exe_id";
  1005. $res = Database::query($sql);
  1006. if ($get_count) {
  1007. $row = Database::fetch_array($res,'ASSOC');
  1008. return $row['count'];
  1009. } else {
  1010. $list = array();
  1011. while($row = Database::fetch_array($res,'ASSOC')) {
  1012. $list[$row['exe_id']] = $row;
  1013. }
  1014. return $list;
  1015. }
  1016. }
  1017. /**
  1018. * Gets all exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1019. * @param int exercise id
  1020. * @param string course code
  1021. * @param int session id
  1022. * @return array with the results
  1023. *
  1024. */
  1025. function get_all_exercise_results_by_user($user_id, $course_code, $session_id = 0)
  1026. {
  1027. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1028. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1029. $course_code = Database::escape_string($course_code);
  1030. //$exercise_id = intval($exercise_id);
  1031. $session_id = intval($session_id);
  1032. $user_id = intval($user_id);
  1033. $sql = "SELECT * FROM $table_track_exercises
  1034. WHERE
  1035. status = '' AND
  1036. exe_user_id = $user_id AND
  1037. exe_cours_id = '$course_code' AND
  1038. session_id = $session_id AND
  1039. orig_lp_id = 0 AND
  1040. orig_lp_item_id = 0
  1041. ORDER by exe_id";
  1042. $res = Database::query($sql);
  1043. $list = array();
  1044. while($row = Database::fetch_array($res,'ASSOC')) {
  1045. $list[$row['exe_id']] = $row;
  1046. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1047. $res_question = Database::query($sql);
  1048. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1049. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1050. }
  1051. }
  1052. return $list;
  1053. }
  1054. /**
  1055. * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1056. * @param int exercise id
  1057. * @param string course code
  1058. * @param int session id
  1059. * @return array with the results
  1060. *
  1061. */
  1062. function get_exercise_results_by_attempt($exe_id)
  1063. {
  1064. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1065. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1066. $table_track_attempt_recording = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1067. $exe_id = intval($exe_id);
  1068. $sql = "SELECT * FROM $table_track_exercises WHERE status = '' AND exe_id = $exe_id";
  1069. $res = Database::query($sql);
  1070. $list = array();
  1071. if (Database::num_rows($res)) {
  1072. $row = Database::fetch_array($res,'ASSOC');
  1073. // Checking if this attempt was revised by a teacher
  1074. $sql_revised = 'SELECT exe_id FROM ' . $table_track_attempt_recording . '
  1075. WHERE author != "" AND exe_id = '.$exe_id.' LIMIT 1';
  1076. $res_revised = Database::query($sql_revised);
  1077. $row['attempt_revised'] = 0;
  1078. if (Database::num_rows($res_revised) > 0) {
  1079. $row['attempt_revised'] = 1;
  1080. }
  1081. $list[$exe_id] = $row;
  1082. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY tms ASC";
  1083. $res_question = Database::query($sql);
  1084. while ($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1085. $list[$exe_id]['question_list'][$row_q['question_id']] = $row_q;
  1086. }
  1087. }
  1088. // echo '<pre>'; print_r($list); echo "</pre>";
  1089. return $list;
  1090. }
  1091. /**
  1092. * Gets exercise results (NO Exercises in LPs) from a given user,
  1093. * exercise id, course, session, lp_id, lp_item_id
  1094. * @param int user id
  1095. * @param int exercise id
  1096. * @param string course code
  1097. * @param int session id
  1098. * @param int lp id
  1099. * @param int lp item id
  1100. * @param string order asc or desc
  1101. * @return array with the results
  1102. *
  1103. */
  1104. function get_exercise_results_by_user(
  1105. $user_id,
  1106. $exercise_id,
  1107. $course_code,
  1108. $session_id = 0,
  1109. $lp_id = 0,
  1110. $lp_item_id = 0,
  1111. $order = null
  1112. ) {
  1113. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1114. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1115. $table_track_attempt_recording = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1116. $course_code = Database::escape_string($course_code);
  1117. $exercise_id = intval($exercise_id);
  1118. $session_id = intval($session_id);
  1119. $user_id = intval($user_id);
  1120. $lp_id = intval($lp_id);
  1121. $lp_item_id = intval($lp_item_id);
  1122. if (!in_array(strtolower($order), array('asc', 'desc'))) {
  1123. $order = 'asc';
  1124. }
  1125. $sql = "SELECT * FROM $table_track_exercises
  1126. WHERE status = '' AND
  1127. exe_user_id = $user_id AND
  1128. exe_cours_id = '$course_code' AND
  1129. exe_exo_id = $exercise_id AND
  1130. session_id = $session_id AND
  1131. orig_lp_id = $lp_id AND
  1132. orig_lp_item_id = $lp_item_id
  1133. ORDER by exe_id $order ";
  1134. $res = Database::query($sql);
  1135. $list = array();
  1136. while($row = Database::fetch_array($res,'ASSOC')) {
  1137. //Checking if this attempt was revised by a teacher
  1138. $sql_revised = 'SELECT exe_id FROM ' . $table_track_attempt_recording . '
  1139. WHERE author != "" AND exe_id = '.$row['exe_id'].' LIMIT 1';
  1140. $res_revised = Database::query($sql_revised);
  1141. $row['attempt_revised'] = 0;
  1142. if (Database::num_rows($res_revised) > 0) {
  1143. $row['attempt_revised'] = 1;
  1144. }
  1145. $list[$row['exe_id']] = $row;
  1146. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1147. $res_question = Database::query($sql);
  1148. while ($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1149. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1150. }
  1151. }
  1152. return $list;
  1153. }
  1154. /**
  1155. * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session
  1156. * @param int exercise id
  1157. * @param string course code
  1158. * @param int session id
  1159. * @return array with the results
  1160. *
  1161. */
  1162. function count_exercise_attempts_by_user($user_id, $exercise_id, $course_code, $session_id = 0)
  1163. {
  1164. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1165. $course_code = Database::escape_string($course_code);
  1166. $exercise_id = intval($exercise_id);
  1167. $session_id = intval($session_id);
  1168. $user_id = intval($user_id);
  1169. $sql = "SELECT count(*) as count FROM $TABLETRACK_EXERCICES
  1170. WHERE status = '' AND exe_user_id = '$user_id' AND exe_cours_id = '$course_code' AND exe_exo_id = '$exercise_id' AND session_id = $session_id AND orig_lp_id =0 AND orig_lp_item_id = 0 ORDER BY exe_id";
  1171. $res = Database::query($sql);
  1172. $result = 0;
  1173. if (Database::num_rows($res) > 0 ) {
  1174. $row = Database::fetch_array($res,'ASSOC');
  1175. $result = $row['count'];
  1176. }
  1177. return $result;
  1178. }
  1179. /**
  1180. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1181. * @param int exercise id
  1182. * @param string course code
  1183. * @param int session id
  1184. * @return array with the results
  1185. * @todo rename this function
  1186. *
  1187. */
  1188. function get_best_exercise_results_by_user($exercise_id, $course_code, $session_id = 0)
  1189. {
  1190. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1191. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1192. $course_code = Database::escape_string($course_code);
  1193. $exercise_id = intval($exercise_id);
  1194. $session_id = intval($session_id);
  1195. $sql = "SELECT * FROM $table_track_exercises
  1196. WHERE status = '' AND exe_cours_id = '$course_code' AND exe_exo_id = '$exercise_id' AND session_id = $session_id AND orig_lp_id =0 AND orig_lp_item_id = 0 ORDER BY exe_id";
  1197. $res = Database::query($sql);
  1198. $list = array();
  1199. while($row = Database::fetch_array($res,'ASSOC')) {
  1200. $list[$row['exe_id']] = $row;
  1201. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1202. $res_question = Database::query($sql);
  1203. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1204. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1205. }
  1206. }
  1207. //Getting the best results of every student
  1208. $best_score_return = array();
  1209. foreach ($list as $student_result) {
  1210. $user_id = $student_result['exe_user_id'];
  1211. $current_best_score[$user_id] = $student_result['exe_result'];
  1212. //echo $current_best_score[$user_id].' - '.$best_score_return[$user_id]['exe_result'].'<br />';
  1213. if (isset($current_best_score[$user_id]) &&
  1214. isset($best_score_return[$user_id]['exe_result']) &&
  1215. $current_best_score[$user_id] > $best_score_return[$user_id]['exe_result']
  1216. ) {
  1217. $best_score_return[$user_id] = $student_result;
  1218. }
  1219. }
  1220. return $best_score_return;
  1221. }
  1222. /**
  1223. * @param int $user_id
  1224. * @param int $exercise_id
  1225. * @param string $course_code
  1226. * @param int $session_id
  1227. * @return array
  1228. */
  1229. function get_best_attempt_exercise_results_per_user($user_id, $exercise_id, $course_code, $session_id = 0)
  1230. {
  1231. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1232. $course_code = Database::escape_string($course_code);
  1233. $exercise_id = intval($exercise_id);
  1234. $session_id = intval($session_id);
  1235. $user_id = intval($user_id);
  1236. $sql = "SELECT * FROM $table_track_exercises
  1237. WHERE
  1238. status = '' AND
  1239. exe_cours_id = '$course_code' AND
  1240. exe_exo_id = '$exercise_id' AND
  1241. session_id = $session_id AND
  1242. exe_user_id = $user_id AND
  1243. orig_lp_id =0 AND
  1244. orig_lp_item_id = 0
  1245. ORDER BY exe_id";
  1246. $res = Database::query($sql);
  1247. $list = array();
  1248. while ($row = Database::fetch_array($res,'ASSOC')) {
  1249. $list[$row['exe_id']] = $row;
  1250. }
  1251. //Getting the best results of every student
  1252. $best_score_return = array();
  1253. $best_score_return['exe_result'] = 0;
  1254. foreach ($list as $result) {
  1255. $current_best_score = $result;
  1256. if ($current_best_score['exe_result'] > $best_score_return['exe_result']) {
  1257. $best_score_return = $result;
  1258. }
  1259. }
  1260. if (!isset($best_score_return['exe_weighting'])) {
  1261. $best_score_return = array();
  1262. }
  1263. return $best_score_return;
  1264. }
  1265. /**
  1266. * @param int $exercise_id
  1267. * @param string $course_code
  1268. * @param int $session_id
  1269. * @return mixed
  1270. */
  1271. function count_exercise_result_not_validated($exercise_id, $course_code, $session_id = 0)
  1272. {
  1273. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1274. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1275. $course_code = Database::escape_string($course_code);
  1276. $session_id = intval($session_id);
  1277. $exercise_id = intval($exercise_id);
  1278. $sql = "SELECT count(e.exe_id) as count FROM $table_track_exercises e
  1279. LEFT JOIN $table_track_attempt a ON e.exe_id = a.exe_id
  1280. WHERE exe_exo_id = $exercise_id AND
  1281. exe_cours_id = '$course_code' AND
  1282. e.session_id = $session_id AND
  1283. orig_lp_id = 0 AND
  1284. marks IS NULL AND
  1285. status = '' AND
  1286. orig_lp_item_id = 0 ORDER BY e.exe_id";
  1287. $res = Database::query($sql);
  1288. $row = Database::fetch_array($res,'ASSOC');
  1289. return $row['count'];
  1290. }
  1291. /**
  1292. * Gets all exercise BEST results attempts (NO Exercises in LPs )
  1293. * from a given exercise id, course, session per user
  1294. * @param string course code
  1295. * @param int session id
  1296. * @return array with the results
  1297. *
  1298. */
  1299. function get_count_exercises_attempted_by_course($course_code, $session_id = 0)
  1300. {
  1301. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1302. $course_code = Database::escape_string($course_code);
  1303. $session_id = intval($session_id);
  1304. $sql = "SELECT DISTINCT exe_exo_id, exe_user_id
  1305. FROM $table_track_exercises
  1306. WHERE status = '' AND exe_cours_id = '$course_code' AND session_id = $session_id AND orig_lp_id =0 AND orig_lp_item_id = 0
  1307. ORDER BY exe_id";
  1308. $res = Database::query($sql);
  1309. $count = 0;
  1310. if (Database::num_rows($res) > 0) {
  1311. $count = Database::num_rows($res);
  1312. }
  1313. return $count;
  1314. }
  1315. /**
  1316. * Gets all exercise events from a Learning Path within a Course nd Session
  1317. * @param int exercise id
  1318. * @param string course_code
  1319. * @param int session id
  1320. * @return array
  1321. */
  1322. function get_all_exercise_event_from_lp($exercise_id, $course_code, $session_id = 0)
  1323. {
  1324. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1325. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1326. $course_code = Database::escape_string($course_code);
  1327. $exercise_id = intval($exercise_id);
  1328. $session_id = intval($session_id);
  1329. $sql = "SELECT * FROM $table_track_exercises
  1330. WHERE status = '' AND exe_cours_id = '$course_code' AND exe_exo_id = '$exercise_id' AND session_id = $session_id AND orig_lp_id !=0 AND orig_lp_item_id != 0";
  1331. $res = Database::query($sql);
  1332. $list = array();
  1333. while($row = Database::fetch_array($res,'ASSOC')) {
  1334. $list[$row['exe_id']] = $row;
  1335. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1336. $res_question = Database::query($sql);
  1337. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1338. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1339. }
  1340. }
  1341. return $list;
  1342. }
  1343. /**
  1344. * @param int $lp_id
  1345. * @param int $course_id
  1346. * @return array
  1347. */
  1348. function get_all_exercises_from_lp($lp_id, $course_id)
  1349. {
  1350. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  1351. $course_id = intval($course_id);
  1352. $lp_id = intval($lp_id);
  1353. $sql = "SELECT * FROM $lp_item_table
  1354. WHERE c_id = $course_id AND lp_id = '".$lp_id."'
  1355. ORDER BY parent_item_id, display_order";
  1356. $res = Database::query($sql);
  1357. $my_exercise_list = array();
  1358. while($row = Database::fetch_array($res,'ASSOC')) {
  1359. if ($row['item_type'] == 'quiz') {
  1360. $my_exercise_list[] = $row;
  1361. }
  1362. }
  1363. return $my_exercise_list;
  1364. }
  1365. /**
  1366. * This function gets the comments of an exercise
  1367. *
  1368. * @param int $id
  1369. * @param int $question_id
  1370. * @return string the comment
  1371. */
  1372. function get_comments($exe_id, $question_id)
  1373. {
  1374. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1375. $sql = "SELECT teacher_comment
  1376. FROM ".$table_track_attempt."
  1377. WHERE exe_id='".Database::escape_string($exe_id)."' AND question_id = '".Database::escape_string($question_id)."'
  1378. ORDER by question_id";
  1379. $sqlres = Database::query($sql);
  1380. $comm = Database::result($sqlres, 0, "teacher_comment");
  1381. return $comm;
  1382. }
  1383. /**
  1384. * @param int $exe_id
  1385. * @return array
  1386. */
  1387. function get_all_exercise_event_by_exe_id($exe_id)
  1388. {
  1389. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1390. $exe_id = intval($exe_id);
  1391. $sql = "SELECT * FROM $table_track_attempt
  1392. WHERE exe_id = $exe_id
  1393. ORDER BY position";
  1394. $res_question = Database::query($sql);
  1395. $list = array();
  1396. if (Database::num_rows($res_question)) {
  1397. while ($row = Database::fetch_array($res_question, 'ASSOC')) {
  1398. $list[$row['question_id']][] = $row;
  1399. }
  1400. }
  1401. return $list;
  1402. }
  1403. /**
  1404. * @param int $exe_id
  1405. * @param int $user_id
  1406. * @param string $course_code
  1407. * @param int $session_id
  1408. * @param int $question_id
  1409. */
  1410. function delete_attempt($exe_id, $user_id, $course_code, $session_id, $question_id)
  1411. {
  1412. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1413. $exe_id = intval($exe_id);
  1414. $user_id = intval($user_id);
  1415. $course_code = Database::escape_string($course_code);
  1416. $session_id = intval($session_id);
  1417. $question_id = intval($question_id);
  1418. $sql = "DELETE FROM $table_track_attempt
  1419. WHERE exe_id = $exe_id AND user_id = $user_id AND course_code = '$course_code' AND session_id = $session_id AND question_id = $question_id ";
  1420. Database::query($sql);
  1421. event_system(
  1422. LOG_QUESTION_RESULT_DELETE,
  1423. LOG_EXERCISE_ATTEMPT_QUESTION_ID,
  1424. $exe_id . '-' . $question_id,
  1425. null,
  1426. null,
  1427. $course_code,
  1428. $session_id
  1429. );
  1430. }
  1431. /**
  1432. * @param int $exe_id
  1433. * @param int $user_id
  1434. * @param string $course_code
  1435. * @param int $question_id
  1436. * @todo add session_id for 10
  1437. */
  1438. function delete_attempt_hotspot($exe_id, $user_id, $course_code, $session_id = 0, $question_id) {
  1439. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1440. $exe_id = intval($exe_id);
  1441. $user_id = intval($user_id);
  1442. $course_code = Database::escape_string($course_code);
  1443. $session_id = intval($session_id);
  1444. $question_id = intval($question_id);
  1445. $sql = "DELETE FROM $table_track_attempt
  1446. WHERE hotspot_exe_id = $exe_id AND hotspot_user_id = $user_id AND hotspot_course_code = '$course_code' AND hotspot_question_id = $question_id ";
  1447. Database::query($sql);
  1448. event_system(
  1449. LOG_QUESTION_RESULT_DELETE,
  1450. LOG_EXERCISE_ATTEMPT_QUESTION_ID,
  1451. $exe_id . '-' . $question_id,
  1452. null,
  1453. null,
  1454. $course_code,
  1455. $session_id
  1456. );
  1457. }
  1458. /**
  1459. * User logs in for the first time to a course
  1460. * @param string $course_code
  1461. * @param int $user_id
  1462. * @param int $session_id
  1463. */
  1464. function event_course_login($course_code, $user_id, $session_id)
  1465. {
  1466. $course_tracking_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1467. //@todo use api_get_utc_datetime
  1468. $time = api_get_utc_datetime();
  1469. $course_code = Database::escape_string($course_code);
  1470. $user_id = intval($user_id);
  1471. $session_id = intval($session_id);
  1472. $session_lifetime = 3600;
  1473. //We select the last record for the current course in the course tracking table
  1474. $sql = "SELECT course_access_id
  1475. FROM $course_tracking_table
  1476. WHERE
  1477. user_id = $user_id AND
  1478. course_code = '$course_code' AND
  1479. session_id = $session_id AND
  1480. login_course_date > '$time' - INTERVAL $session_lifetime SECOND
  1481. ORDER BY login_course_date DESC LIMIT 0,1";
  1482. $result = Database::query($sql);
  1483. if (Database::num_rows($result) > 0) {
  1484. $i_course_access_id = Database::result($result,0,0);
  1485. //We update the course tracking table
  1486. $sql = "UPDATE $course_tracking_table SET logout_course_date = '$time', counter = counter+1
  1487. WHERE course_access_id = ".intval($i_course_access_id)." AND session_id = ".$session_id;
  1488. Database::query($sql);
  1489. } else {
  1490. $sql="INSERT INTO $course_tracking_table (course_code, user_id, login_course_date, logout_course_date, counter, session_id)" .
  1491. "VALUES('".$course_code."', '".$user_id."', '$time', '$time', '1','".$session_id."')";
  1492. Database::query($sql);
  1493. }
  1494. // Course catalog stats modifications see #4191
  1495. CourseManager::update_course_ranking(null, null, null, null, true, false);
  1496. }
  1497. /**
  1498. * For the sake of genericity, this function is a switch.
  1499. * It's called by EventsDispatcher and fires the good function
  1500. * with the good require_once.
  1501. *
  1502. * @param string $event_name
  1503. * @param array $params
  1504. */
  1505. function event_send_mail($event_name, $params)
  1506. {
  1507. EventsMail::send_mail($event_name, $params);
  1508. }
  1509. /**
  1510. * Internal function checking if the mail was already sent from that user to that user
  1511. * @param string $event_name
  1512. * @param int $user_from
  1513. * @param int $user_to
  1514. * @return boolean
  1515. */
  1516. function check_if_mail_already_sent($event_name, $user_from, $user_to = null) {
  1517. $event_name = Database::escape_string($event_name);
  1518. $user_to = intval($user_to);
  1519. $user_from = intval($user_from);
  1520. if ($user_to == null) {
  1521. $sql = 'SELECT COUNT(*) as total FROM ' . Database::get_main_table(TABLE_EVENT_SENT) . '
  1522. WHERE user_from = '.$user_from.' AND event_type_name = "'.$event_name.'"';
  1523. } else {
  1524. $sql = 'SELECT COUNT(*) as total FROM ' . Database::get_main_table(TABLE_EVENT_SENT) . '
  1525. WHERE user_from = '.$user_from.' AND user_to = '.$user_to.' AND event_type_name = "'.$event_name.'"';
  1526. }
  1527. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1528. return $result[0]["total"];
  1529. }
  1530. /*
  1531. * Filter EventEmailTemplate Filters see the main/inc/conf/events.conf.dist.php
  1532. */
  1533. /**
  1534. * Basic template event message filter (to be used by other filters as default)
  1535. * @param array $values (passing by reference)
  1536. * @return boolean True if everything is OK, false otherwise
  1537. */
  1538. function _event_send_mail_filter_func(&$values) {
  1539. return true;
  1540. }
  1541. /**
  1542. * user_registration - send_mail filter
  1543. * @param array $values (passing by reference)
  1544. * @return boolean True if everything is OK, false otherwise
  1545. */
  1546. function user_registration_event_send_mail_filter_func(&$values) {
  1547. $res = _event_send_mail_filter_func($values);
  1548. // proper logic for this filter
  1549. return $res;
  1550. }
  1551. /**
  1552. * portal_homepage_edited - send_mail filter
  1553. * @param array $values (passing by reference)
  1554. * @return boolean True if everything is OK, false otherwise
  1555. */
  1556. function portal_homepage_edited_event_send_mail_filter_func(&$values) {
  1557. $res = _event_send_mail_filter_func($values);
  1558. // proper logic for this filter
  1559. return $res;
  1560. }
  1561. /* End of filters */
  1562. /**
  1563. * Gets the last attempt of an exercise based in the exe_id
  1564. * @param int $exe_id
  1565. * @return mixed
  1566. */
  1567. function getLatestQuestionIdFromAttempt($exe_id)
  1568. {
  1569. $exe_id = intval($exe_id);
  1570. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1571. $sql = 'SELECT question_id FROM '.$track_attempts.'
  1572. WHERE exe_id='.$exe_id.'
  1573. ORDER BY tms DESC
  1574. LIMIT 1';
  1575. $result = Database::query($sql);
  1576. if (Database::num_rows($result)) {
  1577. $row = Database::fetch_array($result);
  1578. return $row['question_id'];
  1579. } else {
  1580. return false;
  1581. }
  1582. }