events.lib.inc.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. <?php
  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. global $_configuration;
  34. global $TABLETRACK_OPEN;
  35. // if tracking is disabled record nothing
  36. if (!$_configuration['tracking_enabled']) {
  37. return 0;
  38. }
  39. // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information
  40. // $_SERVER['HTTP_USER_AGENT'] : will provide browser and os information
  41. // $_SERVER['HTTP_REFERER'] : provide information about refering url
  42. if(isset($_SERVER['HTT_REFERER']))
  43. {
  44. $referer = Database::escape_string($_SERVER['HTTP_REFERER']);
  45. } else {
  46. $referer = '';
  47. }
  48. // record informations only if user comes from another site
  49. //if(!eregi($_configuration['root_web'],$referer))
  50. $pos = strpos($referer, $_configuration['root_web']);
  51. if ($pos === false && $referer != '') {
  52. $remhost = @ getHostByAddr($_SERVER['REMOTE_ADDR']);
  53. if ($remhost == $_SERVER['REMOTE_ADDR'])
  54. $remhost = "Unknown"; // don't change this
  55. $reallyNow = api_get_utc_datetime();
  56. $sql = "INSERT INTO ".$TABLETRACK_OPEN."
  57. (open_remote_host,
  58. open_agent,
  59. open_referer,
  60. open_date)
  61. VALUES
  62. ('".$remhost."',
  63. '".Database::escape_string($_SERVER['HTTP_USER_AGENT'])."', '".Database::escape_string($referer)."', '$reallyNow')";
  64. $res = Database::query($sql);
  65. }
  66. return 1;
  67. }
  68. /**
  69. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  70. * @desc Record information for login event
  71. * (when an user identifies himself with username & password)
  72. */
  73. function event_login() {
  74. global $_configuration;
  75. global $_user;
  76. global $TABLETRACK_LOGIN;
  77. // if tracking is disabled record nothing
  78. if (!$_configuration['tracking_enabled']) {
  79. return 0;
  80. }
  81. $reallyNow = api_get_utc_datetime();
  82. $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, login_ip, login_date, logout_date)
  83. VALUES ('".$_user['user_id']."',
  84. '".Database::escape_string($_SERVER['REMOTE_ADDR'])."',
  85. '".$reallyNow."',
  86. '".$reallyNow."'
  87. )";
  88. $res = Database::query($sql);
  89. }
  90. /**
  91. * @param tool name of the tool (name in mainDb.accueil table)
  92. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  93. * @desc Record information for access event for courses
  94. */
  95. function event_access_course() {
  96. global $_configuration;
  97. global $_user;
  98. global $_cid;
  99. global $TABLETRACK_ACCESS;
  100. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  101. // if tracking is disabled record nothing
  102. if (!$_configuration['tracking_enabled']){
  103. return 0;
  104. }
  105. $id_session = api_get_session_id();
  106. $reallyNow = api_get_utc_datetime();
  107. if ($_user['user_id']) {
  108. $user_id = "'".$_user['user_id']."'";
  109. } else {
  110. $user_id = "0"; // no one
  111. }
  112. $sql = "INSERT INTO ".$TABLETRACK_ACCESS."
  113. (access_user_id,
  114. access_cours_code,
  115. access_date,
  116. access_session_id)
  117. VALUES
  118. (".$user_id.",
  119. '".$_cid."',
  120. '".$reallyNow."',
  121. '".$id_session."')";
  122. $res = Database::query($sql);
  123. // added for "what's new" notification
  124. $sql = "UPDATE $TABLETRACK_LASTACCESS
  125. SET access_date = '$reallyNow'
  126. WHERE access_user_id = $user_id AND access_cours_code = '$_cid' AND access_tool IS NULL AND access_session_id=".$id_session;
  127. $res = Database::query($sql);
  128. if (Database::affected_rows() == 0) {
  129. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, access_cours_code, access_date, access_session_id)
  130. VALUES (".$user_id.", '".$_cid."', '$reallyNow', '".$id_session."')";
  131. $res = Database::query($sql);
  132. }
  133. // end "what's new" notification
  134. return 1;
  135. }
  136. /**
  137. * @param tool name of the tool (name in mainDb.accueil table)
  138. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  139. * @desc Record information for access event for tools
  140. *
  141. * $tool can take this values :
  142. * Links, Calendar, Document, Announcements,
  143. * Group, Video, Works, Users, Exercices, Course Desc
  144. * ...
  145. * Values can be added if new modules are created (15char max)
  146. * I encourage to use $nameTool as $tool when calling this function
  147. *
  148. * Functionality for "what's new" notification is added by Toon Van Hoecke
  149. */
  150. function event_access_tool($tool, $id_session=0) {
  151. global $_configuration;
  152. // if tracking is disabled record nothing
  153. // if( ! $_configuration['tracking_enabled'] ) return 0; //commented because "what's new" notification must always occur
  154. global $_user;
  155. global $_cid;
  156. global $TABLETRACK_ACCESS;
  157. global $_configuration;
  158. global $_course;
  159. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  160. $id_session = api_get_session_id();
  161. $tool = Database::escape_string($tool);
  162. $reallyNow = api_get_utc_datetime();
  163. $user_id = $_user['user_id'] ? "'".$_user['user_id']."'" : "0"; // no one
  164. // record information
  165. // only if user comes from the course $_cid
  166. //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
  167. //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
  168. $pos = strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$_course['path']));
  169. // added for "what's new" notification
  170. $pos2 = strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower($_configuration['root_web']."index"));
  171. // end "what's new" notification
  172. if ($_configuration['tracking_enabled'] && ($pos !== false || $pos2 !== false)) {
  173. $sql = "INSERT INTO ".$TABLETRACK_ACCESS."
  174. (access_user_id,
  175. access_cours_code,
  176. access_tool,
  177. access_date,
  178. access_session_id
  179. )
  180. VALUES
  181. (".$user_id.",".// Don't add ' ' around value, it's already done.
  182. "'".$_cid."' ,
  183. '".$tool."',
  184. '".$reallyNow."',
  185. '".$id_session."')";
  186. $res = Database::query($sql);
  187. }
  188. // "what's new" notification
  189. $sql = "UPDATE $TABLETRACK_LASTACCESS
  190. SET access_date = '$reallyNow'
  191. WHERE access_user_id = ".$user_id." AND access_cours_code = '".$_cid."' AND access_tool = '".$tool."' AND access_session_id=".$id_session;
  192. $res = Database::query($sql);
  193. if (Database::affected_rows() == 0) {
  194. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id,access_cours_code,access_tool, access_date, access_session_id)
  195. VALUES (".$user_id.", '".$_cid."' , '$tool', '$reallyNow', $id_session)";
  196. $res = Database::query($sql);
  197. }
  198. return 1;
  199. }
  200. /**
  201. * @param doc_id id of document (id in mainDb.document table)
  202. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  203. * @desc Record information for download event
  204. * (when an user click to d/l a document)
  205. * it will be used in a redirection page
  206. * bug fixed: Roan Embrechts
  207. * Roan:
  208. * The user id is put in single quotes,
  209. * (why? perhaps to prevent sql insertion hacks?)
  210. * and later again.
  211. * Doing this twice causes an error, I remove one of them.
  212. */
  213. function event_download($doc_url) {
  214. global $_configuration, $_user, $_cid;
  215. $tbl_stats_downloads = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  216. // if tracking is disabled record nothing
  217. if (!$_configuration['tracking_enabled']) {
  218. return 0;
  219. }
  220. $doc_url = Database::escape_string($doc_url);
  221. $reallyNow = api_get_utc_datetime();
  222. if ($_user['user_id']) {
  223. $user_id = "'".$_user['user_id']."'";
  224. } else {
  225. $user_id = "0";
  226. }
  227. $sql = "INSERT INTO $tbl_stats_downloads (
  228. down_user_id,
  229. down_cours_id,
  230. down_doc_path,
  231. down_date,
  232. down_session_id
  233. )
  234. VALUES (
  235. ".$user_id.",
  236. '".$_cid."',
  237. '".$doc_url."',
  238. '".$reallyNow."',
  239. '".api_get_session_id()."'
  240. )";
  241. $res = Database::query($sql);
  242. return 1;
  243. }
  244. /**
  245. * @param doc_id id of document (id in mainDb.document table)
  246. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  247. * @desc Record information for upload event
  248. * used in the works tool to record informations when
  249. * an user upload 1 work
  250. */
  251. function event_upload($doc_id) {
  252. global $_configuration;
  253. global $_user;
  254. global $_cid;
  255. global $TABLETRACK_UPLOADS;
  256. // if tracking is disabled record nothing
  257. if (!$_configuration['tracking_enabled']) {
  258. return 0;
  259. }
  260. $reallyNow = api_get_utc_datetime();
  261. if (isset($_user['user_id']) && $_user['user_id']!='') {
  262. $user_id = "'".$_user['user_id']."'";
  263. } else {
  264. $user_id = "0"; // anonymous
  265. }
  266. $sql = "INSERT INTO ".$TABLETRACK_UPLOADS."
  267. ( upload_user_id,
  268. upload_cours_id,
  269. upload_work_id,
  270. upload_date,
  271. upload_session_id
  272. )
  273. VALUES (
  274. ".$user_id.",
  275. '".$_cid."',
  276. '".$doc_id."',
  277. '".$reallyNow."',
  278. '".api_get_session_id()."'
  279. )";
  280. $res = Database::query($sql);
  281. return 1;
  282. }
  283. /**
  284. * @param link_id (id in coursDb liens table)
  285. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  286. * @desc Record information for link event (when an user click on an added link)
  287. * it will be used in a redirection page
  288. */
  289. function event_link($link_id) {
  290. global $_configuration, $_user, $TABLETRACK_LINKS;
  291. // if tracking is disabled record nothing
  292. if (!$_configuration['tracking_enabled']) {
  293. return 0;
  294. }
  295. $reallyNow = api_get_utc_datetime();
  296. if (isset($_user['user_id']) && $_user['user_id']!='') {
  297. $user_id = "'".Database::escape_string($_user['user_id'])."'";
  298. } else {
  299. // anonymous
  300. $user_id = "0";
  301. }
  302. $sql = "INSERT INTO ".$TABLETRACK_LINKS."
  303. ( links_user_id,
  304. links_cours_id,
  305. links_link_id,
  306. links_date,
  307. links_session_id
  308. ) VALUES (
  309. ".$user_id.",
  310. '".api_get_course_id()."',
  311. '".Database::escape_string($link_id)."',
  312. '".$reallyNow."',
  313. '".api_get_session_id()."'
  314. )";
  315. $res = Database::query($sql);
  316. return 1;
  317. }
  318. /**
  319. * Update the TRACK_E_EXERCICES exercises
  320. *
  321. * @param int exeid id of the attempt
  322. * @param int exo_id exercise id
  323. * @param mixed result score
  324. * @param int weighting ( higher score )
  325. * @param int duration ( duration of the attempt, in seconds )
  326. * @param int session_id
  327. * @param int learnpath_id (id of the learnpath)
  328. * @param int learnpath_item_id (id of the learnpath_item)
  329. *
  330. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  331. * @author Julio Montoya Armas <gugli100@gmail.com> Reworked 2010
  332. * @desc Record result of user when an exercice was done
  333. */
  334. function update_event_exercice($exeid, $exo_id, $score, $weighting,$session_id,$learnpath_id=0, $learnpath_item_id=0, $learnpath_item_view_id = 0, $duration) {
  335. if ($exeid!='') {
  336. // Validation in case of fraud with actived control time
  337. if (!exercise_time_control_is_valid($exo_id)) {
  338. $score = 0;
  339. }
  340. $now = time();
  341. //Validation in case of wrong start_date
  342. if (isset($_SESSION['exercice_start_date'])) {
  343. $start_date = $_SESSION['exercice_start_date'];
  344. $diff = abs($start_date - $now);
  345. if ($diff > 14400) { // 14400 = 4h*60*60 more than 4h of diff
  346. $start_date = $now - 1800; // Now - 30min
  347. }
  348. }
  349. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  350. $sql = "UPDATE $TABLETRACK_EXERCICES SET
  351. exe_exo_id = '".Database::escape_string($exo_id)."',
  352. exe_result = '".Database::escape_string($score)."',
  353. exe_weighting = '".Database::escape_string($weighting)."',
  354. session_id = '".Database::escape_string($session_id)."',
  355. orig_lp_id = '".Database::escape_string($learnpath_id)."',
  356. orig_lp_item_id = '".Database::escape_string($learnpath_item_id)."',
  357. orig_lp_item_view_id = '".Database::escape_string($learnpath_item_view_id)."',
  358. exe_duration = '".Database::escape_string($duration)."',
  359. exe_date = '".api_get_utc_datetime()."',
  360. status = '',
  361. start_date = '".api_get_utc_datetime($start_date)."'
  362. WHERE exe_id = '".Database::escape_string($exeid)."'";
  363. $res = @Database::query($sql);
  364. //Deleting control time session track
  365. exercise_time_control_delete($exo_id);
  366. error_log('update_event_exercice');
  367. error_log($sql);
  368. return $res;
  369. } else
  370. return false;
  371. }
  372. /**
  373. * This function creates an empty Exercise in STATISTIC_TRACK_E_EXERCICES table.
  374. * After that in exercise_result.php we call the update_event_exercice() to update the exercise
  375. * @return $id the last id registered
  376. * @author Julio Montoya <gugli100@gmail.com>
  377. * @desc Record result of user when an exercice was done
  378. */
  379. function create_event_exercice($exo_id) {
  380. global $_user, $_configuration;
  381. error_log('create_event_exercice');
  382. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  383. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  384. $reallyNow = time();
  385. if (isset($_user['user_id']) && $_user['user_id']!='') {
  386. $user_id = "'".$_user['user_id']."'";
  387. } else {
  388. // anonymous
  389. $user_id = "0";
  390. }
  391. //@todo who did this? should be remove, need other function instead
  392. if(defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  393. $condition = ' WHERE ' .
  394. 'exe_exo_id = '."'".Database::escape_string($exo_id)."'".' AND ' .
  395. 'exe_user_id = '."'".api_get_user_id()."'".' AND ' .
  396. 'exe_cours_id = '."'".api_get_course_id()."'".' AND ' .
  397. 'status = '."'incomplete'".' AND '.
  398. 'session_id = '."'".api_get_session_id()."'";
  399. $sql = Database::query('SELECT exe_id FROM '.$TABLETRACK_EXERCICES.$condition);
  400. $row = Database::fetch_array($sql);
  401. return $row['exe_id'];
  402. }
  403. // get exercise id
  404. $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).'"';
  405. $res_exe_id=Database::query($sql_exe_id);
  406. $row_exe_id=Database::fetch_row($res_exe_id);
  407. $exercise_id = intval($row_exe_id[0]);
  408. // get expired_date
  409. $current_expired_time_key = get_time_control_key($exercise_id);
  410. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  411. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  412. } else {
  413. $expired_date = '0000-00-00 00:00:00';
  414. }
  415. $sql = "INSERT INTO $TABLETRACK_EXERCICES ( exe_user_id, exe_cours_id, expired_time_control, exe_exo_id, session_id)
  416. VALUES ( ".$user_id.", '".api_get_course_id()."' ,'".$expired_date."','".$exo_id."','".api_get_session_id()."')";
  417. $res = Database::query($sql);
  418. $id= Database::insert_id();
  419. return $id;
  420. }
  421. /**
  422. * Record an event for this attempt at answering an exercise
  423. * @param float Score achieved
  424. * @param string Answer given
  425. * @param integer Question ID
  426. * @param integer Exercise ID
  427. * @param integer Position
  428. * @return boolean Result of the insert query
  429. */
  430. function exercise_attempt($score, $answer, $quesId, $exeId, $j, $exercise_id = 0) {
  431. global $_configuration;
  432. // If tracking is disabled record nothing
  433. if (!$_configuration['tracking_enabled']) {
  434. return 0;
  435. }
  436. $score = Database::escape_string($score);
  437. $answer = Database::escape_string($answer);
  438. $quesId = Database::escape_string($quesId);
  439. $exeId = Database::escape_string($exeId);
  440. $j = Database::escape_string($j);
  441. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  442. //Validation in case of fraud with actived control time
  443. if (!exercise_time_control_is_valid($exercise_id)) {
  444. $score = 0;
  445. $answer = 0;
  446. $j = 0;
  447. }
  448. $reallyNow = api_get_utc_datetime();
  449. $user_id = api_get_user_id();
  450. if (!empty($user_id)) {
  451. $user_id = "'".$user_id."'";
  452. } else {
  453. // anonymous
  454. $user_id = api_get_anonymous_id();
  455. }
  456. $sql = "INSERT INTO $TBL_TRACK_ATTEMPT (exe_id, user_id, question_id, answer, marks, course_code, position, tms )
  457. VALUES (
  458. ".$exeId.",
  459. ".$user_id.",
  460. '".$quesId."',
  461. '".$answer."',
  462. '".$score."',
  463. '".api_get_course_id()."',
  464. '".$j."',
  465. '".$reallyNow."'
  466. )";
  467. error_log($sql);
  468. if (!empty($quesId) && !empty($exeId) && !empty($user_id)) {
  469. $res = Database::query($sql);
  470. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  471. $TBL_RECORDING = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  472. $recording_changes = "INSERT INTO $TBL_RECORDING (exe_id, question_id, marks, insert_date, author) VALUES ('$exeId','$quesId','$score','".api_get_utc_datetime()."','') ";
  473. Database::query($recording_changes);
  474. }
  475. return $res;
  476. } else {
  477. return false;
  478. }
  479. }
  480. /**
  481. * Record an hotspot spot for this attempt at answering an hotspot question
  482. * @param int Exercise ID
  483. * @param int Question ID
  484. * @param int Answer ID
  485. * @param int Whether this answer is correct (1) or not (0)
  486. * @param string Coordinates of this point (e.g. 123;324)
  487. * @return boolean Result of the insert query
  488. * @uses Course code and user_id from global scope $_cid and $_user
  489. */
  490. function exercise_attempt_hotspot($exe_id, $question_id, $answer_id, $correct, $coords, $exerciseId = 0) {
  491. global $_configuration, $_user;
  492. // if tracking is disabled record nothing
  493. if (!$_configuration['tracking_enabled']) {
  494. return 0;
  495. }
  496. //Validation in case of fraud with actived control time
  497. if (!exercise_time_control_is_valid($exerciseId)) {
  498. $correct = 0;
  499. }
  500. $tbl_track_e_hotspot = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  501. $sql = "INSERT INTO $tbl_track_e_hotspot " .
  502. "(hotspot_user_id, hotspot_course_code, hotspot_exe_id, hotspot_question_id, hotspot_answer_id, hotspot_correct, hotspot_coordinate)".
  503. " VALUES ('" . Database :: escape_string($_user['user_id']) . "'," .
  504. " '" . api_get_course_id() . "', " .
  505. " '" . Database :: escape_string($exe_id) . "', " .
  506. " '" . Database :: escape_string($question_id) . "'," .
  507. " '" . Database :: escape_string($answer_id) . "'," .
  508. " '" . Database :: escape_string($correct) . "'," .
  509. " '" . Database :: escape_string($coords) . "')";
  510. return $result = Database::query($sql);
  511. }
  512. /**
  513. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  514. * @desc Record information for common (or admin) events (in the track_e_default table)
  515. * @param string Type of event
  516. * @param string Type of value
  517. * @param string Value
  518. * @param string Timestamp (defaults to null)
  519. * @param integer User ID (defaults to null)
  520. * @param string Course code (defaults to null)
  521. */
  522. function event_system($event_type, $event_value_type, $event_value, $timestamp = null, $user_id=null, $course_code=null) {
  523. global $_configuration;
  524. global $_user;
  525. global $TABLETRACK_DEFAULT;
  526. $event_type = Database::escape_string($event_type);
  527. $event_value_type = Database::escape_string($event_value_type);
  528. $event_value = Database::escape_string($event_value);
  529. $timestamp = Database::escape_string($timestamp);
  530. $user_id = Database::escape_string($user_id);
  531. $course_code = Database::escape_string($course_code);
  532. // if tracking is disabled record nothing
  533. if (!$_configuration['tracking_enabled']) {
  534. return 0;
  535. }
  536. if(!isset($timestamp)) {
  537. $timestamp = api_get_utc_datetime();
  538. }
  539. if(!isset($user_id)) {
  540. $user_id = 0;
  541. }
  542. if(!isset($course_code)) {
  543. $course_code = '';
  544. }
  545. $sql = "INSERT INTO $TABLETRACK_DEFAULT
  546. (default_user_id,
  547. default_cours_code,
  548. default_date,
  549. default_event_type,
  550. default_value_type,
  551. default_value
  552. )
  553. VALUES
  554. ('$user_id.',
  555. '$course_code',
  556. '$timestamp',
  557. '$event_type',
  558. '$event_value_type',
  559. '$event_value')";
  560. $res = Database::query($sql);
  561. return true;
  562. }
  563. function get_last_attempt_date_of_exercise($exe_id) {
  564. $exe_id = intval($exe_id);
  565. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  566. $track_exercises = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  567. $sql_track_attempt = 'SELECT max(tms) as last_attempt_date FROM '.$track_attempts.' WHERE exe_id='.$exe_id;
  568. $rs_last_attempt = Database::query($sql_track_attempt);
  569. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  570. $last_attempt_date = $row_last_attempt['last_attempt_date'];//Get the date of last attempt
  571. return $last_attempt_date;
  572. }
  573. function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id) {
  574. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  575. $user_id = intval($user_id);
  576. $exerciseId = intval($exerciseId);
  577. $lp_id = intval($lp_id);
  578. $lp_item_id = intval($lp_item_id);
  579. $sql = "SELECT count(*) as count FROM $stat_table WHERE exe_exo_id = '$exerciseId'
  580. AND exe_user_id = '$user_id' AND status != 'incomplete'
  581. AND orig_lp_id = $lp_id AND orig_lp_item_id = $lp_item_id AND exe_cours_id = '".api_get_course_id()."' AND session_id = '" . api_get_session_id() . "'";
  582. $query = Database::query($sql);
  583. if (Database::num_rows($query) > 0 ) {
  584. $attempt = Database :: fetch_array($query,'ASSOC');
  585. return $attempt['count'];
  586. } else {
  587. return 0;
  588. }
  589. }
  590. function delete_student_lp_events($user_id, $lp_id, $course, $session_id) {
  591. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW, $course['dbName']);
  592. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW, $course['dbName']);
  593. $track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  594. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  595. $TBL_RECORDING = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  596. $user_id = intval($user_id);
  597. $lp_id = intval($lp_id);
  598. $session_id = intval($session_id);
  599. //make sure we have the exact lp_view_id
  600. $sqlview = "SELECT id FROM $lp_view_table WHERE user_id = $user_id AND lp_id = $lp_id AND session_id = $session_id ";
  601. $resultview = Database::query($sqlview);
  602. $view = Database::fetch_array($resultview, 'ASSOC');
  603. $lp_view_id = $view['id'];
  604. $sql_delete = "DELETE FROM $lp_item_view_table WHERE lp_view_id = $view_id ";
  605. $result = Database::query($sql_delete);
  606. $sql_delete = "DELETE FROM $lp_view_table WHERE user_id = $user_id AND lp_id= $lp_id AND session_id= $session_id ";
  607. $result = Database::query($sql_delete);
  608. $select_all_attempts = "SELECT exe_id FROM $track_e_exercises WHERE exe_user_id = $user_id AND session_id= $session_id AND exe_cours_id = '{$course['id']}' AND orig_lp_id = $lp_id";
  609. $result = Database::query($select_all_attempts);
  610. $exe_list = array();
  611. while ($row = Database::fetch_array($result, 'ASSOC')) {
  612. $exe_list[] = $row['exe_id'];
  613. }
  614. if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 1) {
  615. $sql_delete = "DELETE FROM $track_e_exercises WHERE exe_id IN (".implode(',',$exe_list).")";
  616. $result = Database::query($sql_delete);
  617. $sql_delete = "DELETE FROM $track_attempts WHERE exe_id IN (".implode(',',$exe_list).")";
  618. $result = Database::query($sql_delete);
  619. $sql_delete = "DELETE FROM $TBL_RECORDING WHERE exe_id IN (".implode(',',$exe_list).")";
  620. $result = Database::query($sql_delete);
  621. }
  622. }
  623. function get_all_exercise_event($exercise_id, $course_code, $session_id = 0) {
  624. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  625. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  626. $course_code = Database::escape_string($course_code);
  627. $exercise_id = intval($exercise_id);
  628. $session_id = intval($session_id);
  629. $sql = "SELECT * FROM $TABLETRACK_EXERCICES 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";
  630. $res = api_sql_query($sql,__FILE__,__LINE__);
  631. $list = array();
  632. while($row = Database::fetch_array($res,'ASSOC')) {
  633. $list[$row['exe_id']] = $row;
  634. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  635. $res_question = api_sql_query($sql,__FILE__,__LINE__);
  636. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  637. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  638. }
  639. }
  640. //echo '<pre>'; print_r($list);
  641. return $list;
  642. }
  643. function get_all_exercise_event_from_lp($exercise_id, $course_code, $session_id = 0) {
  644. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  645. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  646. $course_code = Database::escape_string($course_code);
  647. $exercise_id = intval($exercise_id);
  648. $session_id = intval($session_id);
  649. $sql = "SELECT * FROM $TABLETRACK_EXERCICES 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";
  650. $res = api_sql_query($sql,__FILE__,__LINE__);
  651. $list = array();
  652. while($row = Database::fetch_array($res,'ASSOC')) {
  653. $list[$row['exe_id']] = $row;
  654. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  655. $res_question = api_sql_query($sql,__FILE__,__LINE__);
  656. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  657. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  658. }
  659. }
  660. return $list;
  661. }
  662. /*
  663. function get_all_exercise_event_from_lp($exercise_id, $course_db, $session_id ) {
  664. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM,$course_db);
  665. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW,$course_db);
  666. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW,$course_db);
  667. $exercise_id = intval($exercise_id);
  668. $session_id = intval($session_id);
  669. $sql = "SELECT title, user_id, score , iv.max_score, status, session_id
  670. FROM $lp_item_table as i INNER JOIN $lp_item_view_table iv ON (i.id = iv.lp_item_id ) INNER JOIN $lp_view_table v ON iv.lp_view_id = v.id
  671. WHERE path = $exercise_id AND status ='completed' AND session_id = $session_id";
  672. $res = api_sql_query($sql,__FILE__,__LINE__);
  673. $list = array();
  674. while($row = Database::fetch_array($res,'ASSOC')) {
  675. $list[$row['exe_id']]['question_list'][$row['question_id']] = $row;
  676. }
  677. //echo '<pre>'; print_r($list);
  678. return $list;
  679. }*/
  680. function get_all_exercises_from_lp($lp_id, $course_db) {
  681. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM,$course_db);
  682. $lp_id = intval($lp_id);
  683. $sql = "SELECT * FROM $lp_item_table WHERE lp_id = '".$lp_id."' ORDER BY parent_item_id, display_order";
  684. $res = api_sql_query($sql, __FILE__, __LINE__);
  685. $my_exercise_list = array();
  686. while($row = Database::fetch_array($res,'ASSOC')) {
  687. if ($row['item_type'] == 'quiz') {
  688. $my_exercise_list[] = $row;
  689. }
  690. }
  691. return $my_exercise_list;
  692. }
  693. /**
  694. * This function gets the comments of an exercise
  695. *
  696. * @param int $id
  697. * @param int $question_id
  698. * @return str the comment
  699. */
  700. function get_comments($id,$question_id)
  701. {
  702. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  703. $sql = "SELECT teacher_comment FROM ".$TBL_TRACK_ATTEMPT." where exe_id='".Database::escape_string($id)."' and question_id = '".Database::escape_string($question_id)."' ORDER by question_id";
  704. $sqlres = Database::query($sql);
  705. $comm = Database::result($sqlres,0,"teacher_comment");
  706. return $comm;
  707. }