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