events.lib.inc.php 36 KB

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