events.lib.inc.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  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. $reallyNow = api_get_utc_datetime();
  39. $sql = "INSERT INTO ".$TABLETRACK_OPEN."
  40. (open_remote_host,
  41. open_agent,
  42. open_referer,
  43. open_date)
  44. VALUES
  45. ('".$remhost."',
  46. '".Database::escape_string($_SERVER['HTTP_USER_AGENT'])."', '".Database::escape_string($referer)."', '$reallyNow')";
  47. $res = Database::query($sql);
  48. }
  49. return 1;
  50. }
  51. /**
  52. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  53. * @desc Record information for login event
  54. * (when an user identifies himself with username & password)
  55. */
  56. function event_login()
  57. {
  58. $TABLETRACK_LOGIN = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  59. $_user = api_get_user_info();
  60. $reallyNow = api_get_utc_datetime();
  61. $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, login_ip, login_date, logout_date) VALUES
  62. ('".api_get_user_id()."',
  63. '".Database::escape_string(api_get_real_ip())."',
  64. '".$reallyNow."',
  65. '".$reallyNow."'
  66. )";
  67. Database::query($sql);
  68. // auto subscribe
  69. $user_status = $_user['status'] == SESSIONADMIN ? 'sessionadmin' :
  70. $_user['status'] == COURSEMANAGER ? 'teacher' :
  71. $_user['status'] == DRH ? 'DRH' : 'student';
  72. $autoSubscribe = api_get_setting($user_status.'_autosubscribe');
  73. if ($autoSubscribe) {
  74. $autoSubscribe = explode('|', $autoSubscribe);
  75. foreach ($autoSubscribe as $code) {
  76. if (CourseManager::course_exists($code)) {
  77. CourseManager::subscribe_user($_user['user_id'], $code);
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * @param tool name of the tool (name in mainDb.accueil table)
  84. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  85. * @desc Record information for access event for courses
  86. */
  87. function event_access_course()
  88. {
  89. $TABLETRACK_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  90. $TABLETRACK_LASTACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); //for "what's new" notification
  91. $id_session = api_get_session_id();
  92. $now = api_get_utc_datetime();
  93. $courseId = api_get_course_int_id();
  94. $user_id = api_get_user_id();
  95. if ($user_id) {
  96. $user_id = "'".$user_id."'";
  97. } else {
  98. $user_id = "0"; // no one
  99. }
  100. $sql = "INSERT INTO ".$TABLETRACK_ACCESS." (access_user_id, c_id, access_date, access_session_id) VALUES
  101. (".$user_id.", '".$courseId."', '".$now."','".$id_session."')";
  102. Database::query($sql);
  103. // added for "what's new" notification
  104. $sql = "UPDATE $TABLETRACK_LASTACCESS SET access_date = '$now'
  105. WHERE access_user_id = $user_id AND c_id = '$courseId' AND access_tool IS NULL AND access_session_id=".$id_session;
  106. $result = Database::query($sql);
  107. if (Database::affected_rows($result) == 0) {
  108. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, c_id, access_date, access_session_id)
  109. VALUES (".$user_id.", '".$courseId."', '$now', '".$id_session."')";
  110. Database::query($sql);
  111. }
  112. // end "what's new" notification
  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
  402. * @param string Filename (for audio answers - using nanogong)
  403. * @param integer User ID The user who's going to get this score. Default value of null means "get from context".
  404. * @param integer Course ID (from the "id" column of course table). Default value of null means "get from context".
  405. * @param integer Session ID (from the session table). Default value of null means "get from context".
  406. * @param integer Learnpath ID (from c_lp table). Default value of null means "get from context".
  407. * @param integer Learnpath item ID (from the c_lp_item table). Default value of null means "get from context".
  408. * @return boolean Result of the insert query
  409. */
  410. function saveExerciseAttempt(
  411. $score,
  412. $answer,
  413. $question_id,
  414. $exe_id,
  415. $position,
  416. $exercise_id = 0,
  417. $nano = null,
  418. $user_id = null,
  419. $course_id = null,
  420. $session_id = null,
  421. $learnpath_id = null,
  422. $learnpath_item_id = null
  423. ) {
  424. global $debug;
  425. $score = Database::escape_string($score);
  426. $answer = Database::escape_string($answer);
  427. $question_id = Database::escape_string($question_id);
  428. $exe_id = Database::escape_string($exe_id);
  429. $position = Database::escape_string($position);
  430. $now = api_get_utc_datetime();
  431. // check user_id or get from context
  432. if (empty($user_id) or intval($user_id) != $user_id) {
  433. $user_id = api_get_user_id();
  434. // anonymous
  435. if (empty($user_id)) {
  436. $user_id = api_get_anonymous_id();
  437. }
  438. }
  439. // check course_id or get from context
  440. if (empty($course_id) or intval($course_id) != $course_id) {
  441. $course_id = api_get_course_int_id();
  442. }
  443. // check session_id or get from context
  444. if (empty($session_id) or intval($session_id) != $session_id) {
  445. $session_id = api_get_session_id();
  446. }
  447. // check learnpath_id or get from context
  448. if (empty($learnpath_id)) {
  449. global $learnpath_id;
  450. }
  451. // check learnpath_item_id or get from context
  452. if (empty($learnpath_item_id)) {
  453. global $learnpath_item_id;
  454. }
  455. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  456. if ($debug) {
  457. error_log("----- entering saveExerciseAttempt() function ------");
  458. error_log("answer: $answer");
  459. error_log("score: $score");
  460. error_log("question_id : $question_id");
  461. error_log("position: $position");
  462. }
  463. //Validation in case of fraud with active control time
  464. if (!ExerciseLib::exercise_time_control_is_valid($exercise_id, $learnpath_id, $learnpath_item_id)) {
  465. if ($debug) {
  466. error_log("exercise_time_control_is_valid is false");
  467. }
  468. $score = 0;
  469. $answer = 0;
  470. }
  471. $file = '';
  472. if (isset($nano)) {
  473. $file = Database::escape_string(basename($nano->load_filename_if_exists(false)));
  474. }
  475. if (!empty($question_id) && !empty($exe_id) && !empty($user_id)) {
  476. $attempt = array(
  477. 'user_id' => $user_id,
  478. 'question_id' => $question_id,
  479. 'answer' => $answer,
  480. 'marks' => $score,
  481. 'c_id' => $course_id,
  482. 'session_id' => $session_id,
  483. 'position' => $position,
  484. 'tms' => $now,
  485. 'filename' => $file,
  486. );
  487. // Check if attempt exists.
  488. $sql = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT
  489. WHERE c_id = $course_id AND
  490. session_id = $session_id AND
  491. exe_id = $exe_id AND
  492. user_id = $user_id AND
  493. question_id = $question_id AND
  494. position = $position";
  495. $result = Database::query($sql);
  496. if (Database::num_rows($result)) {
  497. if ($debug) {
  498. error_log("Attempt already exist: exe_id: $exe_id - user_id:$user_id - question_id:$question_id");
  499. }
  500. //The attempt already exist do not update use update_event_exercise() instead
  501. return false;
  502. } else {
  503. $attempt['exe_id'] = $exe_id;
  504. }
  505. if ($debug) {
  506. error_log("Saving question attempt: ");
  507. error_log($sql);
  508. }
  509. $attempt_id = Database::insert($TBL_TRACK_ATTEMPT, $attempt);
  510. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
  511. $recording_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  512. if ($debug) {
  513. error_log("Saving e attempt recording ");
  514. }
  515. $attempt_recording = array(
  516. 'exe_id' => $attempt_id,
  517. 'question_id' => $question_id,
  518. 'marks' => $score,
  519. 'insert_date' => $now,
  520. 'author' => '',
  521. 'session_id' => $session_id,
  522. );
  523. Database::insert($recording_table, $attempt_recording);
  524. }
  525. return $attempt_id;
  526. } else {
  527. return false;
  528. }
  529. }
  530. /**
  531. * Record an hotspot spot for this attempt at answering an hotspot question
  532. * @param int Exercise ID
  533. * @param int Question ID
  534. * @param int Answer ID
  535. * @param int Whether this answer is correct (1) or not (0)
  536. * @param string Coordinates of this point (e.g. 123;324)
  537. * @return boolean Result of the insert query
  538. * @uses Course code and user_id from global scope $_cid and $_user
  539. */
  540. function saveExerciseAttemptHotspot($exe_id, $question_id, $answer_id, $correct, $coords, $exerciseId = 0)
  541. {
  542. global $safe_lp_id, $safe_lp_item_id;
  543. //Validation in case of fraud with actived control time
  544. if (!ExerciseLib::exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
  545. $correct = 0;
  546. }
  547. $tbl_track_e_hotspot = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  548. $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)".
  549. " VALUES ('".api_get_user_id()."',".
  550. " '".api_get_course_int_id()."', ".
  551. " '".Database :: escape_string($exe_id)."', ".
  552. " '".Database :: escape_string($question_id)."',".
  553. " '".Database :: escape_string($answer_id)."',".
  554. " '".Database :: escape_string($correct)."',".
  555. " '".Database :: escape_string($coords)."')";
  556. return $result = Database::query($sql);
  557. }
  558. /**
  559. * Records information for common (or admin) events (in the track_e_default table)
  560. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  561. * @param string Type of event
  562. * @param string Type of value
  563. * @param string Value
  564. * @param string Timestamp (defaults to null)
  565. * @param integer User ID (defaults to null)
  566. * @param string Course code (defaults to null)
  567. * @assert ('','','') === false
  568. */
  569. function event_system($event_type, $event_value_type, $event_value, $datetime = null, $user_id = null, $course_code = null)
  570. {
  571. $TABLETRACK_DEFAULT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  572. if (empty($event_type)) {
  573. return false;
  574. }
  575. $event_type = Database::escape_string($event_type);
  576. $event_value_type = Database::escape_string($event_value_type);
  577. //Clean the user_info
  578. if ($event_value_type == LOG_USER_OBJECT) {
  579. if (is_array($event_value)) {
  580. unset($event_value['complete_name']);
  581. unset($event_value['complete_name_with_username']);
  582. unset($event_value['firstName']);
  583. unset($event_value['lastName']);
  584. unset($event_value['avatar_small']);
  585. unset($event_value['avatar_sys_path']);
  586. unset($event_value['avatar']);
  587. unset($event_value['mail']);
  588. unset($event_value['password']);
  589. unset($event_value['lastLogin']);
  590. unset($event_value['picture_uri']);
  591. $event_value = serialize($event_value);
  592. }
  593. }
  594. $event_value = Database::escape_string($event_value);
  595. $course_info = api_get_course_info($course_code);
  596. if (!empty($course_info)) {
  597. $course_id = $course_info['real_id'];
  598. $course_code = $course_info['code'];
  599. $course_code = Database::escape_string($course_code);
  600. } else {
  601. $course_id = null;
  602. $course_code = null;
  603. }
  604. if (!isset($datetime)) {
  605. $datetime = api_get_utc_datetime();
  606. }
  607. $datetime = Database::escape_string($datetime);
  608. if (!isset($user_id)) {
  609. $user_id = api_get_user_id();
  610. }
  611. $user_id = intval($user_id);
  612. $sql = "INSERT INTO $TABLETRACK_DEFAULT
  613. (default_user_id,
  614. default_cours_code,
  615. c_id,
  616. default_date,
  617. default_event_type,
  618. default_value_type,
  619. default_value
  620. )
  621. VALUES('$user_id.',
  622. '$course_code',
  623. '$course_id',
  624. '$datetime',
  625. '$event_type',
  626. '$event_value_type',
  627. '$event_value')";
  628. Database::query($sql);
  629. return true;
  630. }
  631. /**
  632. * Get every email stored in the database
  633. *
  634. * @param int $etId
  635. * @return type
  636. * @assert () !== false
  637. */
  638. function get_all_event_types()
  639. {
  640. global $event_config;
  641. $sql = 'SELECT etm.id, event_type_name, activated, language_id, message, subject, dokeos_folder
  642. FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' etm
  643. INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l
  644. ON etm.language_id = l.id';
  645. $events_types = Database::store_result(Database::query($sql), 'ASSOC');
  646. $to_return = array();
  647. foreach ($events_types as $et) {
  648. $et['nameLangVar'] = $event_config[$et["event_type_name"]]["name_lang_var"];
  649. $et['descLangVar'] = $event_config[$et["event_type_name"]]["desc_lang_var"];
  650. $to_return[] = $et;
  651. }
  652. return $to_return;
  653. }
  654. /**
  655. * Get users linked to an event
  656. *
  657. * @param int $etId
  658. * @return type
  659. */
  660. function get_users_subscribed_to_event($event_name)
  661. {
  662. $event_name = Database::escape_string($event_name);
  663. $sql = 'SELECT u.* FROM '.Database::get_main_table(TABLE_MAIN_USER).' u,'
  664. .Database::get_main_table(TABLE_MAIN_EVENT_TYPE).' e,'
  665. .Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' ue
  666. WHERE ue.user_id = u.user_id
  667. AND e.name = "'.$event_name.'"
  668. AND e.id = ue.event_type_id';
  669. $return = Database::store_result(Database::query($sql), 'ASSOC');
  670. return json_encode($return);
  671. }
  672. /**
  673. * Get the users related to one event
  674. *
  675. * @param string $event_name
  676. */
  677. function get_event_users($event_name)
  678. {
  679. $event_name = Database::escape_string($event_name);
  680. $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
  681. ON relUser.user_id = user.user_id
  682. WHERE user.status <> '.ANONYMOUS.' AND relUser.event_type_name = "'.$event_name.'"';
  683. //For tests
  684. //$sql = 'SELECT user.user_id, user.firstname, user.lastname FROM '.Database::get_main_table(TABLE_MAIN_USER);
  685. $user_list = Database::store_result(Database::query($sql), 'ASSOC');
  686. return json_encode($user_list);
  687. }
  688. function get_events_by_user_and_type($user_id, $event_type)
  689. {
  690. $TABLETRACK_DEFAULT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  691. $user_id = intval($user_id);
  692. $event_type = Database::escape_string($event_type);
  693. $sql = "SELECT * FROM $TABLETRACK_DEFAULT
  694. WHERE default_value_type = 'user_id' AND
  695. default_value = $user_id AND
  696. default_event_type = '$event_type'
  697. ORDER BY default_date ";
  698. $result = Database::query($sql);
  699. if ($result) {
  700. return Database::store_result($result, 'ASSOC');
  701. }
  702. return false;
  703. }
  704. function get_latest_event_by_user_and_type($user_id, $event_type)
  705. {
  706. $result = get_events_by_user_and_type($user_id, $event_type);
  707. if ($result && !empty($result)) {
  708. return $result[0];
  709. }
  710. }
  711. /**
  712. * Save the new message for one event and for one language
  713. *
  714. * @param string $eventName
  715. * @param array $users
  716. * @param string $message
  717. * @param string $subject
  718. * @param string $eventMessageLanguage
  719. * @param int $activated
  720. */
  721. function save_event_type_message($event_name, $users, $message, $subject, $event_message_language, $activated)
  722. {
  723. $event_name = Database::escape_string($event_name);
  724. $activated = intval($activated);
  725. $event_message_language = Database::escape_string($event_message_language);
  726. // Deletes then re-adds the users linked to the event
  727. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_name = "'.$event_name.'" ';
  728. Database::query($sql);
  729. foreach ($users as $user) {
  730. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_name) VALUES('.intval($user).',"'.$event_name.'")';
  731. Database::query($sql);
  732. }
  733. $language_id = api_get_language_id($event_message_language);
  734. // check if this template in this language already exists or not
  735. $sql = 'SELECT COUNT(id) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  736. WHERE event_type_name = "'.$event_name.'" AND language_id = '.$language_id;
  737. $sql = Database::store_result(Database::query($sql), 'ASSOC');
  738. // if already exists, we update
  739. if ($sql[0]["total"] > 0) {
  740. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  741. SET message = "'.Database::escape_string($message).'",
  742. subject = "'.Database::escape_string($subject).'",
  743. activated = '.$activated.'
  744. WHERE event_type_name = "'.$event_name.'" AND language_id = (SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  745. WHERE dokeos_folder = "'.$event_message_language.'")';
  746. Database::query($sql);
  747. } else { // else we create a new record
  748. // gets the language_-_id
  749. $lang_id = '(SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  750. WHERE dokeos_folder = "'.$event_message_language.'")';
  751. $lang_id = Database::store_result(Database::query($lang_id), 'ASSOC');
  752. if (!empty($lang_id[0]["id"])) {
  753. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' (event_type_name, language_id, message, subject, activated)
  754. VALUES("'.$event_name.'", '.$lang_id[0]["id"].', "'.Database::escape_string($message).'", "'.Database::escape_string($subject).'", '.$activated.')';
  755. Database::query($sql);
  756. }
  757. }
  758. // set activated at every save
  759. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  760. SET activated = '.$activated.'
  761. WHERE event_type_name = "'.$event_name.'"';
  762. Database::query($sql);
  763. }
  764. function eventType_mod($etId, $users, $message, $subject)
  765. {
  766. $etId = intval($etId);
  767. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_id = '.$etId.' ';
  768. Database::query($sql);
  769. foreach ($users as $user) {
  770. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_id)
  771. VALUES('.intval($user).','.$etId.') ';
  772. Database::query($sql);
  773. }
  774. $sql = 'UPDATE '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_MESSAGE).'
  775. SET message = "'.Database::escape_string($message).'",
  776. subject = "'.Database::escape_string($subject).'"
  777. WHERE event_type_id = '.$etId.'';
  778. Database::query($sql);
  779. }
  780. /**
  781. * Gets the last attempt of an exercise based in the exe_id
  782. * @param int $exe_id
  783. * @return mixed
  784. */
  785. function getLastAttemptDateOfExercise($exe_id)
  786. {
  787. $exe_id = intval($exe_id);
  788. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  789. $sql_track_attempt = 'SELECT max(tms) as last_attempt_date FROM '.$track_attempts.' WHERE exe_id='.$exe_id;
  790. $rs_last_attempt = Database::query($sql_track_attempt);
  791. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  792. $last_attempt_date = $row_last_attempt['last_attempt_date']; //Get the date of last attempt
  793. return $last_attempt_date;
  794. }
  795. /**
  796. * Gets the last attempt of an exercise based in the exe_id
  797. * @param int $exe_id
  798. * @return mixed
  799. */
  800. function getLatestQuestionIdFromAttempt($exe_id)
  801. {
  802. $exe_id = intval($exe_id);
  803. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  804. $sql = 'SELECT question_id FROM '.$track_attempts.' WHERE exe_id='.$exe_id.' ORDER BY tms DESC LIMIT 1';
  805. $result = Database::query($sql);
  806. if (Database::num_rows($result)) {
  807. $row = Database::fetch_array($result);
  808. return $row['question_id'];
  809. } else {
  810. return false;
  811. }
  812. }
  813. /**
  814. * Gets how many attempts exists by user, exercise, learning path
  815. * @param int user id
  816. * @param int exercise id
  817. * @param int lp id
  818. * @param int lp item id
  819. * @param int lp item view id
  820. */
  821. function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id, $lp_item_view_id)
  822. {
  823. $stat_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  824. $user_id = intval($user_id);
  825. $exerciseId = intval($exerciseId);
  826. $lp_id = intval($lp_id);
  827. $lp_item_id = intval($lp_item_id);
  828. $lp_item_view_id = intval($lp_item_view_id);
  829. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  830. exe_exo_id = $exerciseId AND
  831. exe_user_id = $user_id AND
  832. status != 'incomplete' AND
  833. orig_lp_id = $lp_id AND
  834. orig_lp_item_id = $lp_item_id AND
  835. orig_lp_item_view_id = $lp_item_view_id AND
  836. c_id = '".api_get_course_int_id()."' AND
  837. session_id = '".api_get_session_id()."'";
  838. $query = Database::query($sql);
  839. if (Database::num_rows($query) > 0) {
  840. $attempt = Database :: fetch_array($query, 'ASSOC');
  841. return $attempt['count'];
  842. } else {
  843. return 0;
  844. }
  845. }
  846. function get_attempt_count_not_finished($user_id, $exerciseId, $lp_id, $lp_item_id)
  847. {
  848. $stat_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  849. $user_id = intval($user_id);
  850. $exerciseId = intval($exerciseId);
  851. $lp_id = intval($lp_id);
  852. $lp_item_id = intval($lp_item_id);
  853. //$lp_item_view_id = intval($lp_item_view_id);
  854. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  855. exe_exo_id = $exerciseId AND
  856. exe_user_id = $user_id AND
  857. status != 'incomplete' AND
  858. orig_lp_id = $lp_id AND
  859. orig_lp_item_id = $lp_item_id AND
  860. c_id = '".api_get_course_int_id()."' AND
  861. session_id = '".api_get_session_id()."'";
  862. $query = Database::query($sql);
  863. if (Database::num_rows($query) > 0) {
  864. $attempt = Database :: fetch_array($query, 'ASSOC');
  865. return $attempt['count'];
  866. } else {
  867. return 0;
  868. }
  869. }
  870. /**
  871. * @param int $user_id
  872. * @param int $lp_id
  873. * @param array $course
  874. * @param int $session_id
  875. */
  876. function delete_student_lp_events($user_id, $lp_id, $course, $session_id)
  877. {
  878. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  879. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  880. $course_id = $course['real_id'];
  881. if (empty($course_id)) {
  882. $course_id = api_get_course_int_id();
  883. }
  884. $track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  885. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  886. $recording_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  887. $user_id = intval($user_id);
  888. $lp_id = intval($lp_id);
  889. $session_id = intval($session_id);
  890. //Make sure we have the exact lp_view_id
  891. $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 ";
  892. $result = Database::query($sql);
  893. if (Database::num_rows($result)) {
  894. $view = Database::fetch_array($result, 'ASSOC');
  895. $lp_view_id = $view['id'];
  896. $sql = "DELETE FROM $lp_item_view_table WHERE c_id = $course_id AND lp_view_id = $lp_view_id ";
  897. Database::query($sql);
  898. }
  899. $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 ";
  900. Database::query($sql);
  901. $sql = "SELECT exe_id FROM $track_e_exercises
  902. WHERE exe_user_id = $user_id AND
  903. session_id = $session_id AND
  904. c_id = $course_id AND
  905. orig_lp_id = $lp_id";
  906. $result = Database::query($sql);
  907. $exe_list = array();
  908. while ($row = Database::fetch_array($result, 'ASSOC')) {
  909. $exe_list[] = $row['exe_id'];
  910. }
  911. if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) {
  912. $sql_delete = "DELETE FROM $track_e_exercises WHERE exe_id IN (".implode(',', $exe_list).")";
  913. Database::query($sql_delete);
  914. $sql_delete = "DELETE FROM $track_attempts WHERE exe_id IN (".implode(',', $exe_list).")";
  915. Database::query($sql_delete);
  916. $sql_delete = "DELETE FROM $recording_table WHERE exe_id IN (".implode(',', $exe_list).")";
  917. Database::query($sql_delete);
  918. }
  919. }
  920. /**
  921. * Delete all exercise attempts (included in LP or not)
  922. *
  923. * @param int user id
  924. * @param int exercise id
  925. * @param string course code
  926. * @param int session id
  927. */
  928. function delete_all_incomplete_attempts($user_id, $exercise_id, $course_id, $session_id = 0)
  929. {
  930. $track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  931. $user_id = intval($user_id);
  932. $exercise_id = intval($exercise_id);
  933. $course_id = intval($course_id);
  934. $session_id = intval($session_id);
  935. if (!empty($user_id) && !empty($exercise_id) && !empty($course_code)) {
  936. $sql = "DELETE FROM $track_e_exercises
  937. WHERE exe_user_id = $user_id AND
  938. exe_exo_id = $exercise_id AND
  939. c_id = '$course_id' AND
  940. session_id = $session_id AND
  941. status = 'incomplete' ";
  942. Database::query($sql);
  943. }
  944. }
  945. /**
  946. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  947. * @param int exercise id
  948. * @param string course code
  949. * @param int session id
  950. * @return array with the results
  951. *
  952. */
  953. function get_all_exercise_results($exercise_id, $courseId, $session_id = 0, $load_question_list = true, $user_id = null)
  954. {
  955. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  956. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  957. $courseId = intval($courseId);
  958. $exercise_id = intval($exercise_id);
  959. $session_id = intval($session_id);
  960. $user_condition = null;
  961. if (!empty($user_id)) {
  962. $user_id = intval($user_id);
  963. $user_condition = "AND exe_user_id = $user_id ";
  964. }
  965. $sql = "SELECT * FROM $TABLETRACK_EXERCICES
  966. WHERE status = '' AND
  967. c_id = '$courseId' AND
  968. exe_exo_id = '$exercise_id' AND
  969. session_id = $session_id AND
  970. orig_lp_id =0 AND
  971. orig_lp_item_id = 0
  972. $user_condition
  973. ORDER BY exe_id";
  974. $res = Database::query($sql);
  975. $list = array();
  976. while ($row = Database::fetch_array($res, 'ASSOC')) {
  977. $list[$row['exe_id']] = $row;
  978. if ($load_question_list) {
  979. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  980. $res_question = Database::query($sql);
  981. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  982. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  983. }
  984. }
  985. }
  986. return $list;
  987. }
  988. /**
  989. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  990. * @param string course code
  991. * @param int session id
  992. * @return array with the results
  993. *
  994. */
  995. function get_all_exercise_results_by_course($courseId, $session_id = 0, $get_count = true)
  996. {
  997. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  998. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  999. $courseId = intval($courseId);
  1000. $session_id = intval($session_id);
  1001. $select = '*';
  1002. if ($get_count) {
  1003. $select = 'count(*) as count';
  1004. }
  1005. $sql = "SELECT $select FROM $table_track_exercises
  1006. WHERE status = '' AND
  1007. c_id = '$courseId' AND
  1008. session_id = $session_id AND
  1009. orig_lp_id = 0 AND
  1010. orig_lp_item_id = 0
  1011. ORDER BY exe_id";
  1012. $res = Database::query($sql);
  1013. if ($get_count) {
  1014. $row = Database::fetch_array($res, 'ASSOC');
  1015. return $row['count'];
  1016. } else {
  1017. $list = array();
  1018. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1019. $list[$row['exe_id']] = $row;
  1020. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1021. $res_question = Database::query($sql);
  1022. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1023. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1024. }
  1025. }
  1026. return $list;
  1027. }
  1028. }
  1029. /**
  1030. * Gets all exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1031. * @param int exercise id
  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_user($user_id, $courseId, $session_id = 0)
  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. $user_id = intval($user_id);
  1044. $sql = "SELECT * FROM $table_track_exercises
  1045. WHERE status = '' AND
  1046. exe_user_id = $user_id AND
  1047. c_id = '$courseId' AND
  1048. session_id = $session_id AND
  1049. orig_lp_id = 0 AND
  1050. orig_lp_item_id = 0
  1051. ORDER by exe_id";
  1052. $res = Database::query($sql);
  1053. $list = array();
  1054. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1055. $list[$row['exe_id']] = $row;
  1056. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1057. $res_question = Database::query($sql);
  1058. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1059. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1060. }
  1061. }
  1062. //echo '<pre>'; print_r($list);
  1063. return $list;
  1064. }
  1065. /**
  1066. * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1067. * @param int exercise id
  1068. * @param string course code
  1069. * @param int session id
  1070. * @return array with the results
  1071. *
  1072. */
  1073. function get_exercise_results_by_attempt($exe_id)
  1074. {
  1075. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1076. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1077. $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1078. $exe_id = intval($exe_id);
  1079. $sql = "SELECT * FROM $table_track_exercises WHERE status = '' AND exe_id = $exe_id";
  1080. $res = Database::query($sql);
  1081. $list = array();
  1082. if (Database::num_rows($res)) {
  1083. $row = Database::fetch_array($res, 'ASSOC');
  1084. //Checking if this attempt was revised by a teacher
  1085. $sql_revised = 'SELECT exe_id FROM '.$table_track_attempt_recording.' WHERE author != "" AND exe_id = '.$exe_id.' LIMIT 1';
  1086. $res_revised = Database::query($sql_revised);
  1087. $row['attempt_revised'] = 0;
  1088. if (Database::num_rows($res_revised) > 0) {
  1089. $row['attempt_revised'] = 1;
  1090. }
  1091. $list[$exe_id] = $row;
  1092. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY tms ASC";
  1093. $res_question = Database::query($sql);
  1094. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1095. $list[$exe_id]['question_list'][$row_q['question_id']] = $row_q;
  1096. }
  1097. }
  1098. return $list;
  1099. }
  1100. /**
  1101. * Gets exercise results (NO Exercises in LPs) from a given user, exercise id, course, session, lp_id, lp_item_id
  1102. * @param int user id
  1103. * @param int exercise id
  1104. * @param string course code
  1105. * @param int session id
  1106. * @param int lp id
  1107. * @param int lp item id
  1108. * @param string order asc or desc
  1109. * @return array with the results
  1110. *
  1111. */
  1112. function getExerciseResultsByUser($user_id, $exercise_id, $courseId, $session_id = 0, $lp_id = 0, $lp_item_id = 0, $order = null)
  1113. {
  1114. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1115. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1116. $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1117. $courseId = intval($courseId);
  1118. $exercise_id = intval($exercise_id);
  1119. $session_id = intval($session_id);
  1120. $user_id = intval($user_id);
  1121. $lp_id = intval($lp_id);
  1122. $lp_item_id = intval($lp_item_id);
  1123. if (!in_array(strtolower($order), array('asc', 'desc'))) {
  1124. $order = 'asc';
  1125. }
  1126. $sql = "SELECT * FROM $table_track_exercises
  1127. WHERE status = '' AND
  1128. exe_user_id = $user_id AND
  1129. c_id = $courseId AND
  1130. exe_exo_id = $exercise_id AND
  1131. session_id = $session_id AND
  1132. orig_lp_id = $lp_id AND
  1133. orig_lp_item_id = $lp_item_id
  1134. ORDER by exe_id $order ";
  1135. $res = Database::query($sql);
  1136. $list = array();
  1137. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1138. //Checking if this attempt was revised by a teacher
  1139. $sql_revised = 'SELECT exe_id FROM '.$table_track_attempt_recording.' 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 int course id
  1158. * @param int session id
  1159. * @return array with the results
  1160. *
  1161. */
  1162. function count_exercise_attempts_by_user($user_id, $exercise_id, $courseId, $session_id = 0)
  1163. {
  1164. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1165. $courseId = intval($courseId);
  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
  1171. exe_user_id = '$user_id' AND
  1172. c_id = '$courseId' AND
  1173. exe_exo_id = '$exercise_id' AND
  1174. session_id = $session_id AND
  1175. orig_lp_id =0 AND
  1176. orig_lp_item_id = 0
  1177. ORDER BY exe_id";
  1178. $res = Database::query($sql);
  1179. $result = 0;
  1180. if (Database::num_rows($res) > 0) {
  1181. $row = Database::fetch_array($res, 'ASSOC');
  1182. $result = $row['count'];
  1183. }
  1184. return $result;
  1185. }
  1186. /**
  1187. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1188. * @param int exercise id
  1189. * @param int course id
  1190. * @param int session id
  1191. * @return array with the results
  1192. * @todo rename this function
  1193. *
  1194. */
  1195. function get_best_exercise_results_by_user($exercise_id, $courseId, $session_id = 0)
  1196. {
  1197. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1198. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1199. $courseId = intval($courseId);
  1200. $exercise_id = intval($exercise_id);
  1201. $session_id = intval($session_id);
  1202. $sql = "SELECT * FROM $table_track_exercises
  1203. WHERE status = '' AND
  1204. c_id = $courseId AND
  1205. exe_exo_id = '$exercise_id' AND
  1206. session_id = $session_id AND
  1207. orig_lp_id = 0 AND
  1208. orig_lp_item_id = 0
  1209. ORDER BY exe_id";
  1210. $res = Database::query($sql);
  1211. $list = array();
  1212. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1213. $list[$row['exe_id']] = $row;
  1214. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1215. $res_question = Database::query($sql);
  1216. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1217. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1218. }
  1219. }
  1220. //Getting the best results of every student
  1221. $best_score_return = array();
  1222. foreach ($list as $student_result) {
  1223. $user_id = $student_result['exe_user_id'];
  1224. $current_best_score[$user_id] = $student_result['exe_result'];
  1225. 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']) {
  1226. $best_score_return[$user_id] = $student_result;
  1227. }
  1228. }
  1229. return $best_score_return;
  1230. }
  1231. /**
  1232. * @param int $user_id
  1233. * @param int $exercise_id
  1234. * @param int $courseId
  1235. * @param int $session_id
  1236. * @return array
  1237. */
  1238. function get_best_attempt_exercise_results_per_user($user_id, $exercise_id, $courseId, $session_id = 0)
  1239. {
  1240. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1241. $courseId = intval($courseId);
  1242. $exercise_id = intval($exercise_id);
  1243. $session_id = intval($session_id);
  1244. $user_id = intval($user_id);
  1245. $sql = "SELECT * FROM $table_track_exercises
  1246. WHERE status = '' AND
  1247. c_id = '$courseId' AND
  1248. exe_exo_id = '$exercise_id' AND
  1249. session_id = $session_id AND
  1250. exe_user_id = $user_id AND
  1251. orig_lp_id =0 AND
  1252. orig_lp_item_id = 0
  1253. ORDER BY exe_id";
  1254. $res = Database::query($sql);
  1255. $list = array();
  1256. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1257. $list[$row['exe_id']] = $row; /*
  1258. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1259. $res_question = Database::query($sql);
  1260. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1261. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1262. } */
  1263. }
  1264. //Getting the best results of every student
  1265. $best_score_return = array();
  1266. $best_score_return['exe_result'] = 0;
  1267. foreach ($list as $result) {
  1268. $current_best_score = $result;
  1269. if ($current_best_score['exe_result'] > $best_score_return['exe_result']) {
  1270. $best_score_return = $result;
  1271. }
  1272. }
  1273. if (!isset($best_score_return['exe_weighting'])) {
  1274. $best_score_return = array();
  1275. }
  1276. return $best_score_return;
  1277. }
  1278. /**
  1279. * @param int $exercise_id
  1280. * @param int $courseId
  1281. * @param int $session_id
  1282. * @return mixed
  1283. */
  1284. function count_exercise_result_not_validated($exercise_id, $courseId, $session_id = 0)
  1285. {
  1286. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1287. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1288. $courseId = intval($courseId);
  1289. $session_id = intval($session_id);
  1290. $exercise_id = intval($exercise_id);
  1291. $sql = "SELECT count(e.exe_id) as count
  1292. FROM $table_track_exercises e LEFT JOIN $table_track_attempt a ON e.exe_id = a.exe_id
  1293. WHERE exe_exo_id = $exercise_id AND
  1294. c_id = '$courseId' AND
  1295. e.session_id = $session_id AND
  1296. orig_lp_id = 0 AND
  1297. marks IS NULL AND
  1298. status = '' AND
  1299. orig_lp_item_id = 0 ORDER BY e.exe_id";
  1300. $res = Database::query($sql);
  1301. $row = Database::fetch_array($res, 'ASSOC');
  1302. return $row['count'];
  1303. }
  1304. /**
  1305. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1306. * @param int exercise id
  1307. * @param int course id
  1308. * @param int session id
  1309. * @return array with the results
  1310. *
  1311. */
  1312. function get_count_exercises_attempted_by_course($courseId, $session_id = 0)
  1313. {
  1314. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1315. $courseId = intval($courseId);
  1316. $session_id = intval($session_id);
  1317. $sql = "SELECT DISTINCT exe_exo_id, exe_user_id
  1318. FROM $table_track_exercises
  1319. WHERE status = '' AND
  1320. c_id = '$courseId' AND
  1321. session_id = $session_id AND
  1322. orig_lp_id =0 AND
  1323. orig_lp_item_id = 0
  1324. ORDER BY exe_id";
  1325. $res = Database::query($sql);
  1326. $count = 0;
  1327. if (Database::num_rows($res) > 0) {
  1328. $count = Database::num_rows($res);
  1329. }
  1330. return $count;
  1331. }
  1332. /**
  1333. * Gets all exercise events from a Learning Path within a Course nd Session
  1334. * @param int exercise id
  1335. * @param string course_code
  1336. * @param int session id
  1337. * @return array
  1338. */
  1339. function get_all_exercise_event_from_lp($exercise_id, $courseId, $session_id = 0)
  1340. {
  1341. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1342. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1343. $courseId = intval($courseId);
  1344. $exercise_id = intval($exercise_id);
  1345. $session_id = intval($session_id);
  1346. $sql = "SELECT * FROM $table_track_exercises
  1347. WHERE status = '' AND
  1348. c_id = $courseId AND
  1349. exe_exo_id = '$exercise_id' AND
  1350. session_id = $session_id AND
  1351. orig_lp_id !=0 AND
  1352. orig_lp_item_id != 0";
  1353. $res = Database::query($sql);
  1354. $list = array();
  1355. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1356. $list[$row['exe_id']] = $row;
  1357. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1358. $res_question = Database::query($sql);
  1359. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1360. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1361. }
  1362. }
  1363. return $list;
  1364. }
  1365. function get_all_exercises_from_lp($lp_id, $course_id)
  1366. {
  1367. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  1368. $course_id = intval($course_id);
  1369. $lp_id = intval($lp_id);
  1370. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$lp_id."' ORDER BY parent_item_id, display_order";
  1371. $res = Database::query($sql);
  1372. $my_exercise_list = array();
  1373. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1374. if ($row['item_type'] == 'quiz') {
  1375. $my_exercise_list[] = $row;
  1376. }
  1377. }
  1378. return $my_exercise_list;
  1379. }
  1380. /**
  1381. * This function gets the comments of an exercise
  1382. *
  1383. * @param int $id
  1384. * @param int $question_id
  1385. * @return string the comment
  1386. */
  1387. function get_comments($exe_id, $question_id)
  1388. {
  1389. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1390. $sql = "SELECT teacher_comment FROM ".$table_track_attempt."
  1391. WHERE exe_id='".Database::escape_string($exe_id)."' AND question_id = '".Database::escape_string($question_id)."'
  1392. ORDER by question_id";
  1393. $sqlres = Database::query($sql);
  1394. $comm = Database::result($sqlres, 0, "teacher_comment");
  1395. return $comm;
  1396. }
  1397. /**
  1398. * @param int $exe_id
  1399. * @return array
  1400. */
  1401. function getAllExerciseEventByExeId($exe_id)
  1402. {
  1403. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1404. $exe_id = intval($exe_id);
  1405. $list = array();
  1406. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY position";
  1407. $res_question = Database::query($sql);
  1408. if (Database::num_rows($res_question)) {
  1409. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1410. $list[$row_q['question_id']][] = $row_q;
  1411. }
  1412. }
  1413. return $list;
  1414. }
  1415. /**
  1416. *
  1417. * @param int $exe_id
  1418. * @param int $user_id
  1419. * @param int $courseId
  1420. * @param int $session_id
  1421. * @param int $question_id
  1422. */
  1423. function delete_attempt($exe_id, $user_id, $courseId, $session_id, $question_id)
  1424. {
  1425. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1426. $exe_id = intval($exe_id);
  1427. $user_id = intval($user_id);
  1428. $courseId = intval($courseId);
  1429. $session_id = intval($session_id);
  1430. $question_id = intval($question_id);
  1431. $sql = "DELETE FROM $table_track_attempt
  1432. WHERE exe_id = $exe_id AND
  1433. user_id = $user_id AND
  1434. c_id = $courseId AND
  1435. session_id = $session_id AND
  1436. question_id = $question_id ";
  1437. Database::query($sql);
  1438. }
  1439. /**
  1440. * @param $exe_id
  1441. * @param $user_id
  1442. * @param int $courseId
  1443. * @param $question_id
  1444. */
  1445. function delete_attempt_hotspot($exe_id, $user_id, $courseId, $question_id)
  1446. {
  1447. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1448. $exe_id = intval($exe_id);
  1449. $user_id = intval($user_id);
  1450. $courseId = intval($courseId);
  1451. $question_id = intval($question_id);
  1452. $sql = "DELETE FROM $table_track_attempt
  1453. WHERE hotspot_exe_id = $exe_id AND
  1454. hotspot_user_id = $user_id AND
  1455. c_id = $courseId AND
  1456. hotspot_question_id = $question_id ";
  1457. Database::query($sql);
  1458. }
  1459. function getAnsweredQuestionsFromAttempt($exe_id, $objExercise)
  1460. {
  1461. $attempt_list = getAllExerciseEventByExeId($exe_id);
  1462. $exercise_result = array();
  1463. if (!empty($attempt_list)) {
  1464. foreach ($attempt_list as $question_id => $options) {
  1465. foreach ($options as $item) {
  1466. $question_obj = Question::read($item['question_id']);
  1467. switch ($question_obj->type) {
  1468. case FILL_IN_BLANKS:
  1469. $item['answer'] = $objExercise->fill_in_blank_answer_to_string($item['answer']);
  1470. break;
  1471. case HOT_SPOT:
  1472. //var_dump($item['answer']);
  1473. break;
  1474. }
  1475. if ($item['answer'] != '0' && !empty($item['answer'])) {
  1476. $exercise_result[] = $question_id;
  1477. break;
  1478. }
  1479. }
  1480. }
  1481. }
  1482. return $exercise_result;
  1483. }
  1484. /**
  1485. * User logs in for the first time to a course
  1486. * @param $course_code
  1487. * @param $user_id
  1488. * @param $session_id
  1489. */
  1490. function event_course_login($courseId, $user_id, $session_id)
  1491. {
  1492. $course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1493. $time = api_get_datetime();
  1494. $courseId = Database::escape_string($courseId);
  1495. $user_id = Database::escape_string($user_id);
  1496. $session_id = Database::escape_string($session_id);
  1497. $sql = "INSERT INTO $course_tracking_table(c_id, user_id, login_course_date, logout_course_date, counter, session_id)
  1498. VALUES('".$courseId."', '".$user_id."', '$time', '$time', '1', '".$session_id."')";
  1499. Database::query($sql);
  1500. //Course catalog stats modifications see #4191
  1501. CourseManager::update_course_ranking(null, null, null, null, true, false);
  1502. }
  1503. /**
  1504. * For the sake of genericity, this function is a switch.
  1505. * It's called by EventsDispatcher and fires the good function
  1506. * with the good require_once.
  1507. *
  1508. * @param string $event_name
  1509. * @param array $params
  1510. */
  1511. function event_send_mail($event_name, $params)
  1512. {
  1513. EventsMail::send_mail($event_name, $params);
  1514. }
  1515. /**
  1516. * Internal function checking if the mail was already sent from that user to that user
  1517. * @param string $event_name
  1518. * @param int $user_from
  1519. * @param int $user_to
  1520. * @return boolean
  1521. */
  1522. function check_if_mail_already_sent($event_name, $user_from, $user_to = null)
  1523. {
  1524. if ($user_to == null) {
  1525. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1526. WHERE user_from = '.$user_from.' AND event_type_name = "'.$event_name.'"';
  1527. } else {
  1528. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1529. WHERE user_from = '.$user_from.' AND user_to = '.$user_to.' AND event_type_name = "'.$event_name.'"';
  1530. }
  1531. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1532. return $result[0]["total"];
  1533. }
  1534. /**
  1535. *
  1536. * Filter EventEmailTemplate Filters see the main/inc/conf/events.conf.dist.php
  1537. *
  1538. */
  1539. /**
  1540. * Basic template event message filter (to be used by other filters as default)
  1541. * @param array $values (passing by reference)
  1542. * @return boolean True if everything is OK, false otherwise
  1543. */
  1544. function _event_send_mail_filter_func(&$values)
  1545. {
  1546. return true;
  1547. }
  1548. /**
  1549. * user_registration - send_mail filter
  1550. * @param array $values (passing by reference)
  1551. * @return boolean True if everything is OK, false otherwise
  1552. */
  1553. function user_registration_event_send_mail_filter_func(&$values)
  1554. {
  1555. $res = _event_send_mail_filter_func($values);
  1556. // proper logic for this filter
  1557. return $res;
  1558. }
  1559. /**
  1560. * portal_homepage_edited - send_mail filter
  1561. * @param array $values (passing by reference)
  1562. * @return boolean True if everything is OK, false otherwise
  1563. */
  1564. function portal_homepage_edited_event_send_mail_filter_func(&$values)
  1565. {
  1566. $res = _event_send_mail_filter_func($values);
  1567. // proper logic for this filter
  1568. return $res;
  1569. }