events.lib.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php // $Id: events.lib.inc.php 22205 2009-07-17 21:11:52Z cfasanando $
  2. /* See license terms in /license.txt */
  3. /**
  4. * EVENTS LIBRARY
  5. *
  6. * This is the events library for Dokeos.
  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 dokeos.library
  13. * @todo convert queries to use Database API
  14. */
  15. /* INIT SECTION */
  16. // REGROUP TABLE NAMES FOR MAINTENANCE PURPOSE
  17. $TABLETRACK_LOGIN = $_configuration['statistics_database'].".track_e_login";
  18. $TABLETRACK_OPEN = $_configuration['statistics_database'].".track_e_open";
  19. $TABLETRACK_ACCESS = $_configuration['statistics_database'].".track_e_access";
  20. $TABLETRACK_DOWNLOADS = $_configuration['statistics_database'].".track_e_downloads";
  21. $TABLETRACK_UPLOADS = $_configuration['statistics_database'].".track_e_uploads";
  22. $TABLETRACK_LINKS = $_configuration['statistics_database'].".track_e_links";
  23. $TABLETRACK_EXERCICES = $_configuration['statistics_database'].".track_e_exercices";
  24. $TABLETRACK_SUBSCRIPTIONS = $_configuration['statistics_database'].".track_e_subscriptions";
  25. $TABLETRACK_LASTACCESS = $_configuration['statistics_database'].".track_e_lastaccess"; //for "what's new" notification
  26. $TABLETRACK_DEFAULT = $_configuration['statistics_database'].".track_e_default";
  27. /* FUNCTIONS */
  28. /**
  29. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  30. * @desc Record information for open event (when homepage is opened)
  31. */
  32. function event_open()
  33. {
  34. global $_configuration;
  35. global $TABLETRACK_OPEN;
  36. // if tracking is disabled record nothing
  37. if (!$_configuration['tracking_enabled'])
  38. {
  39. return 0;
  40. }
  41. // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information
  42. // $_SERVER['HTTP_USER_AGENT'] : will provide browser and os information
  43. // $_SERVER['HTTP_REFERER'] : provide information about refering url
  44. if(isset($_SERVER['HTT_REFERER']))
  45. {
  46. $referer = Database::escape_string($_SERVER['HTTP_REFERER']);
  47. }
  48. else
  49. {
  50. $referer = '';
  51. }
  52. // record informations only if user comes from another site
  53. //if(!eregi($_configuration['root_web'],$referer))
  54. $pos = strpos($referer, $_configuration['root_web']);
  55. if ($pos === false && $referer != '')
  56. {
  57. $remhost = @ getHostByAddr($_SERVER['REMOTE_ADDR']);
  58. if ($remhost == $_SERVER['REMOTE_ADDR'])
  59. $remhost = "Unknown"; // don't change this
  60. $reallyNow = time();
  61. $sql = "INSERT INTO ".$TABLETRACK_OPEN."
  62. (open_remote_host,
  63. open_agent,
  64. open_referer,
  65. open_date)
  66. VALUES
  67. ('".$remhost."',
  68. '".Database::escape_string($_SERVER['HTTP_USER_AGENT'])."', '".Database::escape_string($referer)."', FROM_UNIXTIME($reallyNow) )";
  69. $res = Database::query($sql);
  70. }
  71. return 1;
  72. }
  73. /**
  74. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  75. * @desc Record information for login event
  76. * (when an user identifies himself with username & password)
  77. */
  78. function event_login()
  79. {
  80. global $_configuration;
  81. global $_user;
  82. global $TABLETRACK_LOGIN;
  83. // if tracking is disabled record nothing
  84. if (!$_configuration['tracking_enabled']) {
  85. return 0;
  86. }
  87. $reallyNow = time();
  88. $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, login_ip, login_date, logout_date)
  89. VALUES ('".$_user['user_id']."',
  90. '".Database::escape_string($_SERVER['REMOTE_ADDR'])."',
  91. FROM_UNIXTIME(".$reallyNow."),
  92. FROM_UNIXTIME(".$reallyNow.")
  93. )";
  94. $res = Database::query($sql);
  95. }
  96. /**
  97. * @param tool name of the tool (name in mainDb.accueil table)
  98. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  99. * @desc Record information for access event for courses
  100. */
  101. function event_access_course()
  102. {
  103. global $_configuration;
  104. global $_user;
  105. global $_cid;
  106. global $TABLETRACK_ACCESS;
  107. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  108. // if tracking is disabled record nothing
  109. if (!$_configuration['tracking_enabled']){
  110. return 0;
  111. }
  112. $id_session = api_get_session_id();
  113. $reallyNow = time();
  114. if ($_user['user_id']) {
  115. $user_id = "'".$_user['user_id']."'";
  116. } else {
  117. $user_id = "0"; // no one
  118. }
  119. $sql = "INSERT INTO ".$TABLETRACK_ACCESS."
  120. (access_user_id,
  121. access_cours_code,
  122. access_date,
  123. access_session_id)
  124. VALUES
  125. (".$user_id.",
  126. '".$_cid."',
  127. FROM_UNIXTIME(".$reallyNow."),
  128. '".$id_session."')";
  129. $res = Database::query($sql);
  130. // added for "what's new" notification
  131. $sql = " UPDATE $TABLETRACK_LASTACCESS
  132. SET access_date = FROM_UNIXTIME($reallyNow)
  133. WHERE access_user_id = ".$user_id." AND access_cours_code = '".$_cid."' AND access_tool IS NULL AND access_session_id=".$id_session;
  134. $res = Database::query($sql);
  135. if (Database::affected_rows() == 0) {
  136. $sql = " INSERT INTO $TABLETRACK_LASTACCESS
  137. (access_user_id,access_cours_code,access_date, access_session_id)
  138. VALUES
  139. (".$user_id.", '".$_cid."', FROM_UNIXTIME($reallyNow), ".$id_session.")";
  140. $res = Database::query($sql);
  141. }
  142. // end "what's new" notification
  143. return 1;
  144. }
  145. /**
  146. * @param tool name of the tool (name in mainDb.accueil table)
  147. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  148. * @desc Record information for access event for tools
  149. *
  150. * $tool can take this values :
  151. * Links, Calendar, Document, Announcements,
  152. * Group, Video, Works, Users, Exercices, Course Desc
  153. * ...
  154. * Values can be added if new modules are created (15char max)
  155. * I encourage to use $nameTool as $tool when calling this function
  156. *
  157. * Functionality for "what's new" notification is added by Toon Van Hoecke
  158. */
  159. function event_access_tool($tool, $id_session=0)
  160. {
  161. global $_configuration;
  162. // if tracking is disabled record nothing
  163. // if( ! $_configuration['tracking_enabled'] ) return 0; //commented because "what's new" notification must always occur
  164. global $_user;
  165. global $_cid;
  166. global $TABLETRACK_ACCESS;
  167. global $_configuration;
  168. global $_course;
  169. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  170. $id_session = api_get_session_id();
  171. $reallyNow = time();
  172. $user_id = $_user['user_id'] ? "'".$_user['user_id']."'" : "0"; // no one
  173. // record information
  174. // only if user comes from the course $_cid
  175. //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
  176. //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
  177. $pos = strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$_course['path']));
  178. // added for "what's new" notification
  179. $pos2 = strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower($_configuration['root_web']."index"));
  180. // end "what's new" notification
  181. if ($_configuration['tracking_enabled'] && ($pos !== false || $pos2 !== false)) {
  182. $sql = "INSERT INTO ".$TABLETRACK_ACCESS."
  183. (access_user_id,
  184. access_cours_code,
  185. access_tool,
  186. access_date,
  187. access_session_id
  188. )
  189. VALUES
  190. (".$user_id.",".// Don't add ' ' around value, it's already done.
  191. "'".$_cid."' ,
  192. '".htmlspecialchars($tool, ENT_QUOTES)."',
  193. FROM_UNIXTIME(".$reallyNow."),
  194. '".$id_session."')";
  195. $res = Database::query($sql);
  196. }
  197. // "what's new" notification
  198. $sql = "UPDATE $TABLETRACK_LASTACCESS
  199. SET access_date = FROM_UNIXTIME($reallyNow)
  200. WHERE access_user_id = ".$user_id." AND access_cours_code = '".$_cid."' AND access_tool = '".htmlspecialchars($tool, ENT_QUOTES)."' AND access_session_id=".$id_session;
  201. $res = Database::query($sql);
  202. if (Database::affected_rows() == 0)
  203. {
  204. $sql = "INSERT INTO $TABLETRACK_LASTACCESS
  205. (access_user_id,access_cours_code,access_tool, access_date, access_session_id)
  206. VALUES
  207. (".$user_id.", '".$_cid."' , '".htmlspecialchars($tool, ENT_QUOTES)."', FROM_UNIXTIME($reallyNow), $id_session)";
  208. $res = Database::query($sql);
  209. }
  210. return 1;
  211. }
  212. /**
  213. * @param doc_id id of document (id in mainDb.document table)
  214. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  215. * @desc Record information for download event
  216. * (when an user click to d/l a document)
  217. * it will be used in a redirection page
  218. * bug fixed: Roan Embrechts
  219. * Roan:
  220. * The user id is put in single quotes,
  221. * (why? perhaps to prevent sql insertion hacks?)
  222. * and later again.
  223. * Doing this twice causes an error, I remove one of them.
  224. */
  225. function event_download($doc_url)
  226. {
  227. global $_configuration, $_user, $_cid;
  228. $tbl_stats_downloads = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  229. // if tracking is disabled record nothing
  230. if (!$_configuration['tracking_enabled']) {
  231. return 0;
  232. }
  233. $reallyNow = time();
  234. if ($_user['user_id']) {
  235. $user_id = "'".$_user['user_id']."'";
  236. } else {
  237. $user_id = "0";
  238. }
  239. $sql = "INSERT INTO $tbl_stats_downloads
  240. (
  241. down_user_id,
  242. down_cours_id,
  243. down_doc_path,
  244. down_date,
  245. down_session_id
  246. )
  247. VALUES
  248. (
  249. ".$user_id.",
  250. '".$_cid."',
  251. '".htmlspecialchars($doc_url, ENT_QUOTES)."',
  252. FROM_UNIXTIME(".$reallyNow."),
  253. '".api_get_session_id()."'
  254. )";
  255. $res = Database::query($sql);
  256. return 1;
  257. }
  258. /**
  259. * @param doc_id id of document (id in mainDb.document table)
  260. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  261. * @desc Record information for upload event
  262. * used in the works tool to record informations when
  263. * an user upload 1 work
  264. */
  265. function event_upload($doc_id)
  266. {
  267. global $_configuration;
  268. global $_user;
  269. global $_cid;
  270. global $TABLETRACK_UPLOADS;
  271. // if tracking is disabled record nothing
  272. if (!$_configuration['tracking_enabled']) {
  273. return 0;
  274. }
  275. $reallyNow = time();
  276. if (isset($_user['user_id']) && $_user['user_id']!='') {
  277. $user_id = "'".$_user['user_id']."'";
  278. } else {
  279. $user_id = "0"; // anonymous
  280. }
  281. $sql = "INSERT INTO ".$TABLETRACK_UPLOADS."
  282. ( upload_user_id,
  283. upload_cours_id,
  284. upload_work_id,
  285. upload_date,
  286. updload_session_id
  287. )
  288. VALUES (
  289. ".$user_id.",
  290. '".$_cid."',
  291. '".$doc_id."',
  292. FROM_UNIXTIME(".$reallyNow."),
  293. '".api_get_session_id()."'
  294. )";
  295. $res = Database::query($sql);
  296. return 1;
  297. }
  298. /**
  299. * @param link_id (id in coursDb liens table)
  300. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  301. * @desc Record information for link event (when an user click on an added link)
  302. * it will be used in a redirection page
  303. */
  304. function event_link($link_id)
  305. {
  306. global $_configuration, $_user, $_cid;
  307. global $TABLETRACK_LINKS;
  308. // if tracking is disabled record nothing
  309. if (!$_configuration['tracking_enabled']) {
  310. return 0;
  311. }
  312. $reallyNow = time();
  313. if (isset($_user['user_id']) && $_user['user_id']!='') {
  314. $user_id = "'".Database::escape_string($_user['user_id'])."'";
  315. } else {
  316. // anonymous
  317. $user_id = "0";
  318. }
  319. $sql = "INSERT INTO ".$TABLETRACK_LINKS."
  320. ( links_user_id,
  321. links_cours_id,
  322. links_link_id,
  323. links_date,
  324. links_session_id
  325. )
  326. VALUES
  327. (
  328. ".$user_id.",
  329. '".$_cid."',
  330. '".Database::escape_string($link_id)."',
  331. FROM_UNIXTIME(".$reallyNow."),
  332. '".api_get_session_id()."'
  333. )";
  334. $res = Database::query($sql);
  335. return 1;
  336. }
  337. /**
  338. * @param exeid ( id of the exercise)
  339. * @param exo_id ( id in courseDb exercices table )
  340. * @param result ( score @ exercice )
  341. * @param weighting ( higher score )
  342. * @param duration ( duration of the attempt, in seconds )
  343. * @param session_id
  344. * @param learnpath_id (id of the learnpath)
  345. * @param learnpath_item_id (id of the learnpath_item)
  346. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  347. * @author Julio Montoya Armas <gugli100@gmail.com>
  348. * @desc Record result of user when an exercice was done
  349. */
  350. function update_event_exercice($exeid,$exo_id, $score, $weighting,$session_id,$learnpath_id=0,$learnpath_item_id=0, $duration)
  351. {
  352. if ($exeid!='') {
  353. // Validation in case of fraud with actived control time
  354. $course_code = api_get_course_id();
  355. $current_expired_time_key = $course_code.'_'.$session_id.'_'.$exeid;
  356. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  357. $current_time = time();
  358. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  359. $expired_time = strtotime($expired_date);
  360. $total_time_allowed = $expired_time + 30;
  361. if ($total_time_allowed < $current_time) {
  362. $score = 0;
  363. }
  364. }
  365. $now = time();
  366. //Validation in case of wrong start_date
  367. if (isset($_SESSION['exercice_start_date'])) {
  368. $start_date = $_SESSION['exercice_start_date'];
  369. $diff = abs($start_date - $now);
  370. if ($diff > 14400) { // 14400 = 4h*60*60 more than 4h of diff
  371. $start_date = $now - 1800; // Now - 30min
  372. }
  373. }
  374. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  375. $sql = "UPDATE $TABLETRACK_EXERCICES SET
  376. exe_exo_id = '".Database::escape_string($exo_id)."',
  377. exe_result = '".Database::escape_string($score)."',
  378. exe_weighting = '".Database::escape_string($weighting)."',
  379. session_id = '".Database::escape_string($session_id)."',
  380. orig_lp_id = '".Database::escape_string($learnpath_id)."',
  381. orig_lp_item_id = '".Database::escape_string($learnpath_item_id)."',
  382. exe_duration = '".Database::escape_string($duration)."',
  383. exe_date= FROM_UNIXTIME(".$now."),status = '', data_tracking='', start_date = FROM_UNIXTIME(".Database::escape_string($start_date).")
  384. WHERE exe_id = '".Database::escape_string($exeid)."'";
  385. $res = @Database::query($sql);
  386. unset($_SESSION['expired_time'][$current_expired_time_key]);
  387. return $res;
  388. } else
  389. return false;
  390. }
  391. /**
  392. * This function creates an empty Exercise in STATISTIC_TRACK_E_EXERCICES table.
  393. * After that in exercise_result.php we call the update_event_exercice() to update the exercise
  394. * @return $id the last id registered
  395. * @author Julio Montoya <gugli100@gmail.com>
  396. * @desc Record result of user when an exercice was done
  397. */
  398. function create_event_exercice($exo_id)
  399. {
  400. global $_user, $_cid, $_configuration;
  401. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  402. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  403. $reallyNow = time();
  404. if (isset($_user['user_id']) && $_user['user_id']!='') {
  405. $user_id = "'".$_user['user_id']."'";
  406. } else {
  407. // anonymous
  408. $user_id = "0";
  409. }
  410. if(defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  411. $condition = ' WHERE ' .
  412. 'exe_exo_id = '."'".Database::escape_string($exo_id)."'".' AND ' .
  413. 'exe_user_id = '."'".api_get_user_id()."'".' AND ' .
  414. 'exe_cours_id = '."'".$_cid."'".' AND ' .
  415. 'status = '."'incomplete'".' AND '.
  416. 'session_id = '."'".api_get_session_id()."'";
  417. $sql = Database::query('SELECT exe_id FROM '.$TABLETRACK_EXERCICES.$condition);
  418. $row = Database::fetch_array($sql);
  419. return $row['exe_id'];
  420. }
  421. // get exercise id
  422. $sql_exe_id='SELECT exercises.id FROM '.$TBL_EXERCICES.' as exercises, '.$TABLETRACK_EXERCICES.' as track_exercises WHERE exercises.id=track_exercises.exe_exo_id AND track_exercises.exe_id="'.Database::escape_string($exo_id).'"';
  423. $res_exe_id=Database::query($sql_exe_id);
  424. $row_exe_id=Database::fetch_row($res_exe_id);
  425. $exercise_id = intval($row_exe_id[0]);
  426. // get expired_date
  427. $course_code = api_get_course_id();
  428. $session_id = api_get_session_id();
  429. $current_expired_time_key = $course_code.'_'.$session_id.'_'.$exercise_id;
  430. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  431. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  432. } else {
  433. $expired_date = '0000-00-00 00:00:00';
  434. }
  435. $sql = "INSERT INTO $TABLETRACK_EXERCICES ( exe_user_id, exe_cours_id,expired_time_control,exe_exo_id)
  436. VALUES ( ".$user_id.", '".$_cid."' ,'".$expired_date."','".$exo_id."')";
  437. $res = @Database::query($sql);
  438. $id= Database::insert_id();
  439. return $id;
  440. }
  441. /**
  442. * Record an event for this attempt at answering an exercise
  443. * @param float Score achieved
  444. * @param string Answer given
  445. * @param integer Question ID
  446. * @param integer Exercise ID
  447. * @param integer Position
  448. * @return boolean Result of the insert query
  449. */
  450. function exercise_attempt($score,$answer,$quesId,$exeId,$j)
  451. {
  452. $score = Database::escape_string($score);
  453. $answer = Database::escape_string($answer);
  454. $quesId = Database::escape_string($quesId);
  455. $exeId = Database::escape_string($exeId);
  456. $j = Database::escape_string($j);
  457. global $_configuration, $_user, $_cid;
  458. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  459. //Validation in case of fraud with actived control time
  460. $course_code = api_get_course_id();
  461. $session_id = api_get_session_id();
  462. $current_expired_time_key = $course_code.'_'.$session_id.'_'.$exeId;
  463. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  464. $current_time = time();
  465. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  466. $expired_time = strtotime($expired_date);
  467. $total_time_allowed = $expired_time + 30;
  468. if ($total_time_allowed < $current_time) {
  469. $score = 0;
  470. $answer = 0;
  471. $j = 0;
  472. }
  473. }
  474. // if tracking is disabled record nothing
  475. if (!$_configuration['tracking_enabled'])
  476. {
  477. return 0;
  478. }
  479. $reallyNow = time();
  480. if (isset($_user['user_id']) && $_user['user_id']!='')
  481. {
  482. $user_id = "'".$_user['user_id']."'";
  483. }
  484. else // anonymous
  485. {
  486. $user_id = api_get_anonymous_id();
  487. }
  488. $_SESSION['current_exercice_attempt'][$user_id] = $exeId;
  489. $sql = "INSERT INTO $TBL_TRACK_ATTEMPT
  490. (
  491. exe_id,
  492. user_id,
  493. question_id,
  494. answer,
  495. marks,
  496. course_code,
  497. position,
  498. tms
  499. )
  500. VALUES
  501. (
  502. ".$exeId.",
  503. ".$user_id.",
  504. '".$quesId."',
  505. '".addslashes($answer)."',
  506. '".$score."',
  507. '".$_cid."',
  508. '".$j."',
  509. FROM_UNIXTIME(".$reallyNow.")
  510. )";
  511. if(defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  512. $TBL_RECORDING = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  513. $recording_changes = 'INSERT INTO '.$TBL_RECORDING.' ' .
  514. '(exe_id,
  515. question_id,
  516. marks,
  517. insert_date,
  518. author)
  519. VALUES
  520. ('."'$exeId','".$quesId."','$score','".date('Y-m-d H:i:s')."',''".')';
  521. Database::query($recording_changes);
  522. }
  523. if (!empty($quesId) && !empty($exeId) && !empty($user_id)) {
  524. $res = Database::query($sql);
  525. return $res;
  526. } else {
  527. return false;
  528. }
  529. }
  530. /**
  531. * Record an hotspot spot for this attempt at answering an hotspot question
  532. * @param int Exercise ID
  533. * @param int Question ID
  534. * @param int Answer ID
  535. * @param int Whether this answer is correct (1) or not (0)
  536. * @param string Coordinates of this point (e.g. 123;324)
  537. * @return boolean Result of the insert query
  538. * @uses Course code and user_id from global scope $_cid and $_user
  539. */
  540. function exercise_attempt_hotspot($exe_id, $question_id, $answer_id, $correct, $coords)
  541. {
  542. global $_configuration, $_user, $_cid;
  543. // if tracking is disabled record nothing
  544. if (!$_configuration['tracking_enabled'])
  545. {
  546. return 0;
  547. }
  548. //Validation in case of fraud with actived control time
  549. $course_code = api_get_course_id();
  550. $session_id = api_get_session_id();
  551. $current_expired_time_key = $course_code.'_'.$session_id.'_'.$exe_id;
  552. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  553. $current_time = time();
  554. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  555. $expired_time = strtotime($expired_date);
  556. $total_time_allowed = $expired_time + 30;
  557. if ($total_time_allowed < $current_time) {
  558. $correct = 0;
  559. }
  560. }
  561. $tbl_track_e_hotspot = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  562. $sql = "INSERT INTO $tbl_track_e_hotspot " .
  563. "(hotspot_user_id, hotspot_course_code, hotspot_exe_id, hotspot_question_id, hotspot_answer_id, hotspot_correct, hotspot_coordinate)".
  564. " VALUES ('" . Database :: escape_string($_user['user_id']) . "'," .
  565. " '" . Database :: escape_string($_cid) . "', " .
  566. " '" . Database :: escape_string($exe_id) . "', " .
  567. " '" . Database :: escape_string($question_id) . "'," .
  568. " '" . Database :: escape_string($answer_id) . "'," .
  569. " '" . Database :: escape_string($correct) . "'," .
  570. " '" . Database :: escape_string($coords) . "')";
  571. return $result = Database::query($sql);
  572. }
  573. /**
  574. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  575. * @desc Record information for common (or admin) events (in the track_e_default table)
  576. * @param string Type of event
  577. * @param string Type of value
  578. * @param string Value
  579. * @param string Timestamp (defaults to null)
  580. * @param integer User ID (defaults to null)
  581. * @param string Course code (defaults to null)
  582. */
  583. function event_system($event_type, $event_value_type, $event_value, $timestamp = null, $user_id=null, $course_code=null)
  584. {
  585. global $_configuration;
  586. global $_user;
  587. global $TABLETRACK_DEFAULT;
  588. $event_type = Database::escape_string($event_type);
  589. $event_value_type = Database::escape_string($event_value_type);
  590. $event_value = Database::escape_string($event_value);
  591. $timestamp = Database::escape_string($timestamp);
  592. $user_id = Database::escape_string($user_id);
  593. $course_code = Database::escape_string($course_code);
  594. // if tracking is disabled record nothing
  595. if (!$_configuration['tracking_enabled'])
  596. {
  597. return 0;
  598. }
  599. if(!isset($timestamp))
  600. {
  601. $timestamp = time();
  602. }
  603. if(!isset($user_id))
  604. {
  605. $user_id = 0;
  606. }
  607. if(!isset($course_code))
  608. {
  609. $course_code = '';
  610. }
  611. $sql = "INSERT INTO $TABLETRACK_DEFAULT
  612. (default_user_id,
  613. default_cours_code,
  614. default_date,
  615. default_event_type,
  616. default_value_type,
  617. default_value
  618. )
  619. VALUES
  620. ('$user_id.',
  621. '$course_code',
  622. FROM_UNIXTIME($timestamp),
  623. '$event_type',
  624. '$event_value_type',
  625. '$event_value')";
  626. $res = Database::query($sql);
  627. return true;
  628. }
  629. ?>