events.lib.inc.php 63 KB

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