events.lib.inc.php 61 KB

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