events.lib.inc.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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." (access_user_id, access_cours_code, access_date, access_session_id) VALUES
  113. (".$user_id.", '".$_cid."', '".$reallyNow."','".$id_session."')";
  114. $res = Database::query($sql);
  115. // added for "what's new" notification
  116. $sql = "UPDATE $TABLETRACK_LASTACCESS SET access_date = '$reallyNow'
  117. WHERE access_user_id = $user_id AND access_cours_code = '$_cid' AND access_tool IS NULL AND access_session_id=".$id_session;
  118. $res = Database::query($sql);
  119. if (Database::affected_rows() == 0) {
  120. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, access_cours_code, access_date, access_session_id)
  121. VALUES (".$user_id.", '".$_cid."', '$reallyNow', '".$id_session."')";
  122. $res = Database::query($sql);
  123. }
  124. // end "what's new" notification
  125. return 1;
  126. }
  127. /**
  128. * @param tool name of the tool (name in mainDb.accueil table)
  129. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  130. * @desc Record information for access event for tools
  131. *
  132. * $tool can take this values :
  133. * Links, Calendar, Document, Announcements,
  134. * Group, Video, Works, Users, Exercices, Course Desc
  135. * ...
  136. * Values can be added if new modules are created (15char max)
  137. * I encourage to use $nameTool as $tool when calling this function
  138. *
  139. * Functionality for "what's new" notification is added by Toon Van Hoecke
  140. */
  141. function event_access_tool($tool, $id_session=0) {
  142. global $_configuration;
  143. // if tracking is disabled record nothing
  144. // if( ! $_configuration['tracking_enabled'] ) return 0; //commented because "what's new" notification must always occur
  145. global $_user;
  146. global $_cid;
  147. global $TABLETRACK_ACCESS;
  148. global $_configuration;
  149. global $_course;
  150. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  151. $id_session = api_get_session_id();
  152. $tool = Database::escape_string($tool);
  153. $reallyNow = api_get_utc_datetime();
  154. $user_id = $_user['user_id'] ? "'".$_user['user_id']."'" : "0"; // no one
  155. // record information
  156. // only if user comes from the course $_cid
  157. //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
  158. //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
  159. $pos = strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$_course['path']));
  160. // added for "what's new" notification
  161. $pos2 = strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower($_configuration['root_web']."index"));
  162. // end "what's new" notification
  163. if ($_configuration['tracking_enabled'] && ($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. $res = 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. $res = 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. $res = 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. global $_configuration, $_user, $_cid;
  206. $tbl_stats_downloads = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  207. // if tracking is disabled record nothing
  208. if (!$_configuration['tracking_enabled']) {
  209. return 0;
  210. }
  211. $doc_url = Database::escape_string($doc_url);
  212. $reallyNow = api_get_utc_datetime();
  213. if ($_user['user_id']) {
  214. $user_id = "'".$_user['user_id']."'";
  215. } else {
  216. $user_id = "0";
  217. }
  218. $sql = "INSERT INTO $tbl_stats_downloads (
  219. down_user_id,
  220. down_cours_id,
  221. down_doc_path,
  222. down_date,
  223. down_session_id
  224. )
  225. VALUES (
  226. ".$user_id.",
  227. '".$_cid."',
  228. '".$doc_url."',
  229. '".$reallyNow."',
  230. '".api_get_session_id()."'
  231. )";
  232. $res = Database::query($sql);
  233. return 1;
  234. }
  235. /**
  236. * @param doc_id id of document (id in mainDb.document table)
  237. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  238. * @desc Record information for upload event
  239. * used in the works tool to record informations when
  240. * an user upload 1 work
  241. */
  242. function event_upload($doc_id) {
  243. global $_configuration;
  244. global $_user;
  245. global $_cid;
  246. global $TABLETRACK_UPLOADS;
  247. // if tracking is disabled record nothing
  248. if (!$_configuration['tracking_enabled']) {
  249. return 0;
  250. }
  251. $reallyNow = api_get_utc_datetime();
  252. if (isset($_user['user_id']) && $_user['user_id']!='') {
  253. $user_id = "'".$_user['user_id']."'";
  254. } else {
  255. $user_id = "0"; // anonymous
  256. }
  257. $sql = "INSERT INTO ".$TABLETRACK_UPLOADS."
  258. ( upload_user_id,
  259. upload_cours_id,
  260. upload_work_id,
  261. upload_date,
  262. upload_session_id
  263. )
  264. VALUES (
  265. ".$user_id.",
  266. '".$_cid."',
  267. '".$doc_id."',
  268. '".$reallyNow."',
  269. '".api_get_session_id()."'
  270. )";
  271. $res = Database::query($sql);
  272. return 1;
  273. }
  274. /**
  275. * @param link_id (id in coursDb liens table)
  276. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  277. * @desc Record information for link event (when an user click on an added link)
  278. * it will be used in a redirection page
  279. */
  280. function event_link($link_id) {
  281. global $_configuration, $_user, $TABLETRACK_LINKS;
  282. // if tracking is disabled record nothing
  283. if (!$_configuration['tracking_enabled']) {
  284. return 0;
  285. }
  286. $reallyNow = api_get_utc_datetime();
  287. if (isset($_user['user_id']) && $_user['user_id']!='') {
  288. $user_id = "'".Database::escape_string($_user['user_id'])."'";
  289. } else {
  290. // anonymous
  291. $user_id = "0";
  292. }
  293. $sql = "INSERT INTO ".$TABLETRACK_LINKS."
  294. ( links_user_id,
  295. links_cours_id,
  296. links_link_id,
  297. links_date,
  298. links_session_id
  299. ) VALUES (
  300. ".$user_id.",
  301. '".api_get_course_id()."',
  302. '".Database::escape_string($link_id)."',
  303. '".$reallyNow."',
  304. '".api_get_session_id()."'
  305. )";
  306. $res = Database::query($sql);
  307. return 1;
  308. }
  309. /**
  310. * Update the TRACK_E_EXERCICES exercises
  311. *
  312. * @param int exeid id of the attempt
  313. * @param int exo_id exercise id
  314. * @param mixed result score
  315. * @param int weighting ( higher score )
  316. * @param int duration ( duration of the attempt, in seconds )
  317. * @param int session_id
  318. * @param int learnpath_id (id of the learnpath)
  319. * @param int learnpath_item_id (id of the learnpath_item)
  320. *
  321. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  322. * @author Julio Montoya Armas <gugli100@gmail.com> Reworked 2010
  323. * @desc Record result of user when an exercice was done
  324. */
  325. function update_event_exercice($exeid, $exo_id, $score, $weighting,$session_id,$learnpath_id=0, $learnpath_item_id=0, $learnpath_item_view_id = 0, $duration) {
  326. if ($exeid!='') {
  327. // Validation in case of fraud with actived control time
  328. if (!exercise_time_control_is_valid($exo_id)) {
  329. $score = 0;
  330. }
  331. $now = time();
  332. //Validation in case of wrong start_date
  333. if (isset($_SESSION['exercice_start_date'])) {
  334. $start_date = $_SESSION['exercice_start_date'];
  335. $diff = abs($start_date - $now);
  336. if ($diff > 14400) { // 14400 = 4h*60*60 more than 4h of diff
  337. $start_date = $now - 1800; // Now - 30min
  338. }
  339. }
  340. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  341. $sql = "UPDATE $TABLETRACK_EXERCICES SET
  342. exe_exo_id = '".Database::escape_string($exo_id)."',
  343. exe_result = '".Database::escape_string($score)."',
  344. exe_weighting = '".Database::escape_string($weighting)."',
  345. session_id = '".Database::escape_string($session_id)."',
  346. orig_lp_id = '".Database::escape_string($learnpath_id)."',
  347. orig_lp_item_id = '".Database::escape_string($learnpath_item_id)."',
  348. orig_lp_item_view_id = '".Database::escape_string($learnpath_item_view_id)."',
  349. exe_duration = '".Database::escape_string($duration)."',
  350. exe_date = '".api_get_utc_datetime()."',
  351. status = '',
  352. start_date = '".api_get_utc_datetime($start_date)."'
  353. WHERE exe_id = '".Database::escape_string($exeid)."'";
  354. $res = @Database::query($sql);
  355. //Deleting control time session track
  356. exercise_time_control_delete($exo_id);
  357. //error_log('update_event_exercice');
  358. //error_log($sql);
  359. return $res;
  360. } else
  361. return false;
  362. }
  363. /**
  364. * This function creates an empty Exercise in STATISTIC_TRACK_E_EXERCICES table.
  365. * After that in exercise_result.php we call the update_event_exercice() to update the exercise
  366. * @return $id the last id registered
  367. * @author Julio Montoya <gugli100@gmail.com>
  368. * @desc Record result of user when an exercice was done
  369. */
  370. function create_event_exercice($exo_id) {
  371. global $_user, $_configuration;
  372. error_log('create_event_exercice');
  373. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  374. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  375. $reallyNow = time();
  376. if (isset($_user['user_id']) && $_user['user_id']!='') {
  377. $user_id = "'".$_user['user_id']."'";
  378. } else {
  379. // anonymous
  380. $user_id = "0";
  381. }
  382. //@todo who did this? should be remove, need other function instead
  383. if(defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  384. $condition = ' WHERE ' .
  385. 'exe_exo_id = '."'".Database::escape_string($exo_id)."'".' AND ' .
  386. 'exe_user_id = '."'".api_get_user_id()."'".' AND ' .
  387. 'exe_cours_id = '."'".api_get_course_id()."'".' AND ' .
  388. 'status = '."'incomplete'".' AND '.
  389. 'session_id = '."'".api_get_session_id()."'";
  390. $sql = Database::query('SELECT exe_id FROM '.$TABLETRACK_EXERCICES.$condition);
  391. $row = Database::fetch_array($sql);
  392. return $row['exe_id'];
  393. }
  394. // get exercise id
  395. $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).'"';
  396. $res_exe_id=Database::query($sql_exe_id);
  397. $row_exe_id=Database::fetch_row($res_exe_id);
  398. $exercise_id = intval($row_exe_id[0]);
  399. // get expired_date
  400. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  401. $current_expired_time_key = get_time_control_key($exercise_id);
  402. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  403. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  404. } else {
  405. $expired_date = '0000-00-00 00:00:00';
  406. }
  407. $sql = "INSERT INTO $TABLETRACK_EXERCICES ( exe_user_id, exe_cours_id, expired_time_control, exe_exo_id, session_id)
  408. VALUES ( ".$user_id.", '".api_get_course_id()."' ,'".$expired_date."','".$exo_id."','".api_get_session_id()."')";
  409. $res = Database::query($sql);
  410. $id= Database::insert_id();
  411. return $id;
  412. }
  413. /**
  414. * Record an event for this attempt at answering an exercise
  415. * @param float Score achieved
  416. * @param string Answer given
  417. * @param integer Question ID
  418. * @param integer Exercise ID
  419. * @param integer Position
  420. * @return boolean Result of the insert query
  421. */
  422. function exercise_attempt($score, $answer, $quesId, $exeId, $j, $exercise_id = 0) {
  423. global $_configuration;
  424. // If tracking is disabled record nothing
  425. if (!$_configuration['tracking_enabled']) {
  426. return 0;
  427. }
  428. $score = Database::escape_string($score);
  429. $answer = Database::escape_string($answer);
  430. $quesId = Database::escape_string($quesId);
  431. $exeId = Database::escape_string($exeId);
  432. $j = Database::escape_string($j);
  433. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  434. //Validation in case of fraud with actived control time
  435. if (!exercise_time_control_is_valid($exercise_id)) {
  436. $score = 0;
  437. $answer = 0;
  438. $j = 0;
  439. }
  440. $reallyNow = api_get_utc_datetime();
  441. $user_id = api_get_user_id();
  442. if (!empty($user_id)) {
  443. $user_id = "'".$user_id."'";
  444. } else {
  445. // anonymous
  446. $user_id = api_get_anonymous_id();
  447. }
  448. $sql = "INSERT INTO $TBL_TRACK_ATTEMPT (exe_id, user_id, question_id, answer, marks, course_code, position, tms )
  449. VALUES (
  450. ".$exeId.",
  451. ".$user_id.",
  452. '".$quesId."',
  453. '".$answer."',
  454. '".$score."',
  455. '".api_get_course_id()."',
  456. '".$j."',
  457. '".$reallyNow."'
  458. )";
  459. if (!empty($quesId) && !empty($exeId) && !empty($user_id)) {
  460. $res = Database::query($sql);
  461. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  462. $TBL_RECORDING = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  463. $recording_changes = "INSERT INTO $TBL_RECORDING (exe_id, question_id, marks, insert_date, author) VALUES ('$exeId','$quesId','$score','".api_get_utc_datetime()."','') ";
  464. Database::query($recording_changes);
  465. }
  466. return $res;
  467. } else {
  468. return false;
  469. }
  470. }
  471. /**
  472. * Record an hotspot spot for this attempt at answering an hotspot question
  473. * @param int Exercise ID
  474. * @param int Question ID
  475. * @param int Answer ID
  476. * @param int Whether this answer is correct (1) or not (0)
  477. * @param string Coordinates of this point (e.g. 123;324)
  478. * @return boolean Result of the insert query
  479. * @uses Course code and user_id from global scope $_cid and $_user
  480. */
  481. function exercise_attempt_hotspot($exe_id, $question_id, $answer_id, $correct, $coords, $exerciseId = 0) {
  482. global $_configuration, $_user;
  483. // if tracking is disabled record nothing
  484. if (!$_configuration['tracking_enabled']) {
  485. return 0;
  486. }
  487. //Validation in case of fraud with actived control time
  488. if (!exercise_time_control_is_valid($exerciseId)) {
  489. $correct = 0;
  490. }
  491. $tbl_track_e_hotspot = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  492. $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)".
  493. " VALUES ('" . Database :: escape_string($_user['user_id']) . "'," .
  494. " '" . api_get_course_id() . "', " .
  495. " '" . Database :: escape_string($exe_id) . "', " .
  496. " '" . Database :: escape_string($question_id) . "'," .
  497. " '" . Database :: escape_string($answer_id) . "'," .
  498. " '" . Database :: escape_string($correct) . "'," .
  499. " '" . Database :: escape_string($coords) . "')";
  500. return $result = Database::query($sql);
  501. }
  502. /**
  503. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  504. * @desc Record information for common (or admin) events (in the track_e_default table)
  505. * @param string Type of event
  506. * @param string Type of value
  507. * @param string Value
  508. * @param string Timestamp (defaults to null)
  509. * @param integer User ID (defaults to null)
  510. * @param string Course code (defaults to null)
  511. */
  512. function event_system($event_type, $event_value_type, $event_value, $timestamp = null, $user_id=null, $course_code=null) {
  513. global $_configuration;
  514. global $_user;
  515. global $TABLETRACK_DEFAULT;
  516. $event_type = Database::escape_string($event_type);
  517. $event_value_type = Database::escape_string($event_value_type);
  518. $event_value = Database::escape_string($event_value);
  519. $timestamp = Database::escape_string($timestamp);
  520. $user_id = Database::escape_string($user_id);
  521. $course_code = Database::escape_string($course_code);
  522. // if tracking is disabled record nothing
  523. if (!$_configuration['tracking_enabled']) {
  524. return 0;
  525. }
  526. if(!isset($timestamp)) {
  527. $timestamp = api_get_utc_datetime();
  528. }
  529. if(!isset($user_id)) {
  530. $user_id = 0;
  531. }
  532. if(!isset($course_code)) {
  533. $course_code = '';
  534. }
  535. $sql = "INSERT INTO $TABLETRACK_DEFAULT
  536. (default_user_id,
  537. default_cours_code,
  538. default_date,
  539. default_event_type,
  540. default_value_type,
  541. default_value
  542. )
  543. VALUES
  544. ('$user_id.',
  545. '$course_code',
  546. '$timestamp',
  547. '$event_type',
  548. '$event_value_type',
  549. '$event_value')";
  550. $res = Database::query($sql);
  551. return true;
  552. }
  553. /**
  554. * Gets the last attempt of an exercise based in the exe_id
  555. */
  556. function get_last_attempt_date_of_exercise($exe_id) {
  557. $exe_id = intval($exe_id);
  558. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  559. $track_exercises = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  560. $sql_track_attempt = 'SELECT max(tms) as last_attempt_date FROM '.$track_attempts.' WHERE exe_id='.$exe_id;
  561. $rs_last_attempt = Database::query($sql_track_attempt);
  562. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  563. $last_attempt_date = $row_last_attempt['last_attempt_date'];//Get the date of last attempt
  564. return $last_attempt_date;
  565. }
  566. /**
  567. * Gets how many attempts exists by user, exercise, learning path
  568. * @param int user id
  569. * @param int exercise id
  570. * @param int lp id
  571. * @param int lp item id
  572. * @param int lp item view id
  573. */
  574. function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id,$lp_item_view_id) {
  575. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  576. $user_id = intval($user_id);
  577. $exerciseId = intval($exerciseId);
  578. $lp_id = intval($lp_id);
  579. $lp_item_id = intval($lp_item_id);
  580. $lp_item_view_id = intval($lp_item_view_id);
  581. $sql = "SELECT count(*) as count FROM $stat_table WHERE exe_exo_id = '$exerciseId'
  582. AND exe_user_id = '$user_id' AND status != 'incomplete'
  583. AND orig_lp_id = $lp_id AND orig_lp_item_id = $lp_item_id AND orig_lp_item_view_id = $lp_item_view_id AND exe_cours_id = '".api_get_course_id()."' AND session_id = '" . api_get_session_id() . "'";
  584. $query = Database::query($sql);
  585. if (Database::num_rows($query) > 0 ) {
  586. $attempt = Database :: fetch_array($query,'ASSOC');
  587. return $attempt['count'];
  588. } else {
  589. return 0;
  590. }
  591. }
  592. function delete_student_lp_events($user_id, $lp_id, $course, $session_id) {
  593. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW, $course['dbName']);
  594. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW, $course['dbName']);
  595. $track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  596. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  597. $TBL_RECORDING = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  598. $user_id = intval($user_id);
  599. $lp_id = intval($lp_id);
  600. $session_id = intval($session_id);
  601. //make sure we have the exact lp_view_id
  602. $sqlview = "SELECT id FROM $lp_view_table WHERE user_id = $user_id AND lp_id = $lp_id AND session_id = $session_id ";
  603. $resultview = Database::query($sqlview);
  604. $view = Database::fetch_array($resultview, 'ASSOC');
  605. $lp_view_id = $view['id'];
  606. $sql_delete = "DELETE FROM $lp_item_view_table WHERE lp_view_id = $view_id ";
  607. $result = Database::query($sql_delete);
  608. $sql_delete = "DELETE FROM $lp_view_table WHERE user_id = $user_id AND lp_id= $lp_id AND session_id= $session_id ";
  609. $result = Database::query($sql_delete);
  610. $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";
  611. $result = Database::query($select_all_attempts);
  612. $exe_list = array();
  613. while ($row = Database::fetch_array($result, 'ASSOC')) {
  614. $exe_list[] = $row['exe_id'];
  615. }
  616. if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 1) {
  617. $sql_delete = "DELETE FROM $track_e_exercises WHERE exe_id IN (".implode(',',$exe_list).")";
  618. $result = Database::query($sql_delete);
  619. $sql_delete = "DELETE FROM $track_attempts WHERE exe_id IN (".implode(',',$exe_list).")";
  620. $result = Database::query($sql_delete);
  621. $sql_delete = "DELETE FROM $TBL_RECORDING WHERE exe_id IN (".implode(',',$exe_list).")";
  622. $result = Database::query($sql_delete);
  623. }
  624. }
  625. /**
  626. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  627. * @param int exercise id
  628. * @param string course code
  629. * @param int session id
  630. * @return array with the results
  631. *
  632. */
  633. function get_all_exercise_results($exercise_id, $course_code, $session_id = 0) {
  634. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  635. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  636. $course_code = Database::escape_string($course_code);
  637. $exercise_id = intval($exercise_id);
  638. $session_id = intval($session_id);
  639. $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";
  640. $res = Database::query($sql);
  641. $list = array();
  642. while($row = Database::fetch_array($res,'ASSOC')) {
  643. $list[$row['exe_id']] = $row;
  644. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  645. $res_question = Database::query($sql);
  646. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  647. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  648. }
  649. }
  650. //echo '<pre>'; print_r($list);
  651. return $list;
  652. }
  653. /**
  654. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  655. * @param int exercise id
  656. * @param string course code
  657. * @param int session id
  658. * @return array with the results
  659. *
  660. */
  661. function get_all_exercise_results_by_course($course_code, $session_id = 0, $get_count = true) {
  662. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  663. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  664. $course_code = Database::escape_string($course_code);
  665. $session_id = intval($session_id);
  666. $select = '*';
  667. if ($get_count) {
  668. $select = 'count(*) as count';
  669. }
  670. $sql = "SELECT $select FROM $TABLETRACK_EXERCICES WHERE status = '' AND exe_cours_id = '$course_code' AND session_id = $session_id AND orig_lp_id =0 AND orig_lp_item_id = 0 ORDER BY exe_id";
  671. $res = Database::query($sql);
  672. if ($get_count) {
  673. $row = Database::fetch_array($res,'ASSOC');
  674. return $row['count'];
  675. } else {
  676. $list = array();
  677. while($row = Database::fetch_array($res,'ASSOC')) {
  678. $list[$row['exe_id']] = $row;
  679. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  680. $res_question = Database::query($sql);
  681. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  682. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  683. }
  684. }
  685. return $list;
  686. }
  687. }
  688. /**
  689. * Gets all exercise results (NO Exercises in LPs) from a given exercise id, course, session
  690. * @param int exercise id
  691. * @param string course code
  692. * @param int session id
  693. * @return array with the results
  694. *
  695. */
  696. function get_all_exercise_results_by_user($user_id, $exercise_id, $course_code, $session_id = 0) {
  697. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  698. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  699. $course_code = Database::escape_string($course_code);
  700. $exercise_id = intval($exercise_id);
  701. $session_id = intval($session_id);
  702. $user_id = intval($user_id);
  703. $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 AND exe_user_id = $user_id ORDER by exe_id";
  704. $res = Database::query($sql);
  705. $list = array();
  706. while($row = Database::fetch_array($res,'ASSOC')) {
  707. $list[$row['exe_id']] = $row;
  708. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  709. $res_question = Database::query($sql);
  710. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  711. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  712. }
  713. }
  714. //echo '<pre>'; print_r($list);
  715. return $list;
  716. }
  717. /**
  718. * Gets all exercise events from a Learning Path within a Course nd Session
  719. * @param int exercise id
  720. * @param string course_code
  721. * @param int session id
  722. * @return array
  723. */
  724. function get_all_exercise_event_from_lp($exercise_id, $course_code, $session_id = 0) {
  725. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  726. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  727. $course_code = Database::escape_string($course_code);
  728. $exercise_id = intval($exercise_id);
  729. $session_id = intval($session_id);
  730. $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";
  731. $res = Database::query($sql);
  732. $list = array();
  733. while($row = Database::fetch_array($res,'ASSOC')) {
  734. $list[$row['exe_id']] = $row;
  735. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  736. $res_question = Database::query($sql);
  737. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  738. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  739. }
  740. }
  741. return $list;
  742. }
  743. /*
  744. function get_all_exercise_event_from_lp($exercise_id, $course_db, $session_id ) {
  745. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM,$course_db);
  746. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW,$course_db);
  747. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW,$course_db);
  748. $exercise_id = intval($exercise_id);
  749. $session_id = intval($session_id);
  750. $sql = "SELECT title, user_id, score , iv.max_score, status, session_id
  751. 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
  752. WHERE path = $exercise_id AND status ='completed' AND session_id = $session_id";
  753. $res = Database::query($sql);
  754. $list = array();
  755. while($row = Database::fetch_array($res,'ASSOC')) {
  756. $list[$row['exe_id']]['question_list'][$row['question_id']] = $row;
  757. }
  758. //echo '<pre>'; print_r($list);
  759. return $list;
  760. }*/
  761. function get_all_exercises_from_lp($lp_id, $course_db) {
  762. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM,$course_db);
  763. $lp_id = intval($lp_id);
  764. $sql = "SELECT * FROM $lp_item_table WHERE lp_id = '".$lp_id."' ORDER BY parent_item_id, display_order";
  765. $res = Database::query($sql);
  766. $my_exercise_list = array();
  767. while($row = Database::fetch_array($res,'ASSOC')) {
  768. if ($row['item_type'] == 'quiz') {
  769. $my_exercise_list[] = $row;
  770. }
  771. }
  772. return $my_exercise_list;
  773. }
  774. /**
  775. * This function gets the comments of an exercise
  776. *
  777. * @param int $id
  778. * @param int $question_id
  779. * @return str the comment
  780. */
  781. function get_comments($id,$question_id) {
  782. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  783. $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";
  784. $sqlres = Database::query($sql);
  785. $comm = Database::result($sqlres,0,"teacher_comment");
  786. return $comm;
  787. }