events.lib.inc.php 62 KB

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