events.lib.php 68 KB

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