events.lib.php 68 KB

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