events.lib.inc.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * EVENTS LIBRARY
  5. *
  6. * This is the events library for Chamilo.
  7. * Include/require it in your code to use its functionality.
  8. * Functions of this library are used to record informations when some kind
  9. * of event occur. Each event has his own types of informations then each event
  10. * use its own function.
  11. *
  12. * @package chamilo.library
  13. */
  14. /* INIT SECTION */
  15. $TABLETRACK_LOGIN = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  16. $TABLETRACK_OPEN = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_OPEN);
  17. $TABLETRACK_ACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  18. $TABLETRACK_DOWNLOADS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  19. $TABLETRACK_UPLOADS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
  20. $TABLETRACK_LINKS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  21. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  22. //$TABLETRACK_SUBSCRIPTIONS = $_configuration['statistics_database'].".track_e_subscriptions"; // this table is never use
  23. $TABLETRACK_LASTACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); //for "what's new" notification
  24. $TABLETRACK_DEFAULT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  25. /* FUNCTIONS */
  26. /**
  27. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  28. * @desc Record information for open event (when homepage is opened)
  29. */
  30. function event_open() {
  31. global $_configuration;
  32. global $TABLETRACK_OPEN;
  33. // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information
  34. // $_SERVER['HTTP_USER_AGENT'] : will provide browser and os information
  35. // $_SERVER['HTTP_REFERER'] : provide information about refering url
  36. if(isset($_SERVER['HTT_REFERER']))
  37. {
  38. $referer = Database::escape_string($_SERVER['HTTP_REFERER']);
  39. } else {
  40. $referer = '';
  41. }
  42. // record informations only if user comes from another site
  43. //if(!eregi($_configuration['root_web'],$referer))
  44. $pos = strpos($referer, $_configuration['root_web']);
  45. if ($pos === false && $referer != '') {
  46. $remhost = @ getHostByAddr($_SERVER['REMOTE_ADDR']);
  47. if ($remhost == $_SERVER['REMOTE_ADDR'])
  48. $remhost = "Unknown"; // don't change this
  49. $reallyNow = api_get_utc_datetime();
  50. $sql = "INSERT INTO ".$TABLETRACK_OPEN."
  51. (open_remote_host,
  52. open_agent,
  53. open_referer,
  54. open_date)
  55. VALUES
  56. ('".$remhost."',
  57. '".Database::escape_string($_SERVER['HTTP_USER_AGENT'])."', '".Database::escape_string($referer)."', '$reallyNow')";
  58. $res = Database::query($sql);
  59. }
  60. return 1;
  61. }
  62. /**
  63. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  64. * @desc Record information for login event
  65. * (when an user identifies himself with username & password)
  66. */
  67. function event_login() {
  68. global $_user;
  69. global $TABLETRACK_LOGIN;
  70. $reallyNow = api_get_utc_datetime();
  71. $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, login_ip, login_date, logout_date)
  72. VALUES ('".$_user['user_id']."',
  73. '".Database::escape_string($_SERVER['REMOTE_ADDR'])."',
  74. '".$reallyNow."',
  75. '".$reallyNow."'
  76. )";
  77. $res = Database::query($sql);
  78. // autoSubscribe
  79. $user_status = $_user['status'];
  80. $user_status = $_user['status'] == SESSIONADMIN ? 'sessionadmin' :
  81. $_user['status'] == COURSEMANAGER ? 'teacher' :
  82. $_user['status'] == DRH ? 'DRH' :
  83. 'student';
  84. $autoSubscribe = api_get_setting($user_status.'_autosubscribe');
  85. if ($autoSubscribe) {
  86. $autoSubscribe = explode('|', $autoSubscribe);
  87. foreach ($autoSubscribe as $code) {
  88. if (CourseManager::course_exists($code)) {
  89. CourseManager::subscribe_user($_user['user_id'], $code);
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * @param tool name of the tool (name in mainDb.accueil table)
  96. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  97. * @desc Record information for access event for courses
  98. */
  99. function event_access_course() {
  100. global $_user;
  101. global $_cid;
  102. global $TABLETRACK_ACCESS;
  103. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  104. $id_session = api_get_session_id();
  105. $reallyNow = api_get_utc_datetime();
  106. if ($_user['user_id']) {
  107. $user_id = "'".$_user['user_id']."'";
  108. } else {
  109. $user_id = "0"; // no one
  110. }
  111. $sql = "INSERT INTO ".$TABLETRACK_ACCESS." (access_user_id, access_cours_code, access_date, access_session_id) VALUES
  112. (".$user_id.", '".$_cid."', '".$reallyNow."','".$id_session."')";
  113. $res = Database::query($sql);
  114. // added for "what's new" notification
  115. $sql = "UPDATE $TABLETRACK_LASTACCESS SET access_date = '$reallyNow'
  116. WHERE access_user_id = $user_id AND access_cours_code = '$_cid' AND access_tool IS NULL AND access_session_id=".$id_session;
  117. $res = Database::query($sql);
  118. if (Database::affected_rows() == 0) {
  119. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, access_cours_code, access_date, access_session_id)
  120. VALUES (".$user_id.", '".$_cid."', '$reallyNow', '".$id_session."')";
  121. $res = Database::query($sql);
  122. }
  123. // end "what's new" notification
  124. return 1;
  125. }
  126. /**
  127. * @param tool name of the tool (name in mainDb.accueil table)
  128. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  129. * @desc Record information for access event for tools
  130. *
  131. * $tool can take this values :
  132. * Links, Calendar, Document, Announcements,
  133. * Group, Video, Works, Users, Exercices, Course Desc
  134. * ...
  135. * Values can be added if new modules are created (15char max)
  136. * I encourage to use $nameTool as $tool when calling this function
  137. *
  138. * Functionality for "what's new" notification is added by Toon Van Hoecke
  139. */
  140. function event_access_tool($tool, $id_session=0) {
  141. global $_configuration;
  142. global $_user;
  143. global $_cid;
  144. global $TABLETRACK_ACCESS;
  145. global $_course;
  146. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  147. $id_session = api_get_session_id();
  148. $tool = Database::escape_string($tool);
  149. $reallyNow = api_get_utc_datetime();
  150. $user_id = $_user['user_id'] ? "'".$_user['user_id']."'" : "0"; // no one
  151. // record information
  152. // only if user comes from the course $_cid
  153. //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
  154. //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
  155. $pos = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$_course['path'])) : false;
  156. // added for "what's new" notification
  157. $pos2 = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower($_configuration['root_web']."index")) : false;
  158. // end "what's new" notification
  159. if ($pos !== false || $pos2 !== false) {
  160. $sql = "INSERT INTO ".$TABLETRACK_ACCESS."
  161. (access_user_id,
  162. access_cours_code,
  163. access_tool,
  164. access_date,
  165. access_session_id
  166. )
  167. VALUES
  168. (".$user_id.",".// Don't add ' ' around value, it's already done.
  169. "'".$_cid."' ,
  170. '".$tool."',
  171. '".$reallyNow."',
  172. '".$id_session."')";
  173. $res = Database::query($sql);
  174. }
  175. // "what's new" notification
  176. $sql = "UPDATE $TABLETRACK_LASTACCESS
  177. SET access_date = '$reallyNow'
  178. WHERE access_user_id = ".$user_id." AND access_cours_code = '".$_cid."' AND access_tool = '".$tool."' AND access_session_id=".$id_session;
  179. $res = Database::query($sql);
  180. if (Database::affected_rows() == 0) {
  181. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id,access_cours_code,access_tool, access_date, access_session_id)
  182. VALUES (".$user_id.", '".$_cid."' , '$tool', '$reallyNow', $id_session)";
  183. $res = Database::query($sql);
  184. }
  185. return 1;
  186. }
  187. /**
  188. * @param doc_id id of document (id in mainDb.document table)
  189. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  190. * @desc Record information for download event
  191. * (when an user click to d/l a document)
  192. * it will be used in a redirection page
  193. * bug fixed: Roan Embrechts
  194. * Roan:
  195. * The user id is put in single quotes,
  196. * (why? perhaps to prevent sql insertion hacks?)
  197. * and later again.
  198. * Doing this twice causes an error, I remove one of them.
  199. */
  200. function event_download($doc_url) {
  201. $tbl_stats_downloads = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  202. $doc_url = Database::escape_string($doc_url);
  203. $reallyNow = api_get_utc_datetime();
  204. $user_id = "'".api_get_user_id()."'";
  205. $_cid = api_get_course_id();
  206. $sql = "INSERT INTO $tbl_stats_downloads (
  207. down_user_id,
  208. down_cours_id,
  209. down_doc_path,
  210. down_date,
  211. down_session_id
  212. )
  213. VALUES (
  214. ".$user_id.",
  215. '".$_cid."',
  216. '".$doc_url."',
  217. '".$reallyNow."',
  218. '".api_get_session_id()."'
  219. )";
  220. $res = Database::query($sql);
  221. return 1;
  222. }
  223. /**
  224. * @param doc_id id of document (id in mainDb.document table)
  225. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  226. * @desc Record information for upload event
  227. * used in the works tool to record informations when
  228. * an user upload 1 work
  229. */
  230. function event_upload($doc_id) {
  231. global $_user;
  232. global $_cid;
  233. global $TABLETRACK_UPLOADS;
  234. $reallyNow = api_get_utc_datetime();
  235. if (isset($_user['user_id']) && $_user['user_id']!='') {
  236. $user_id = "'".$_user['user_id']."'";
  237. } else {
  238. $user_id = "0"; // anonymous
  239. }
  240. $sql = "INSERT INTO ".$TABLETRACK_UPLOADS."
  241. ( upload_user_id,
  242. upload_cours_id,
  243. upload_work_id,
  244. upload_date,
  245. upload_session_id
  246. )
  247. VALUES (
  248. ".$user_id.",
  249. '".$_cid."',
  250. '".$doc_id."',
  251. '".$reallyNow."',
  252. '".api_get_session_id()."'
  253. )";
  254. $res = Database::query($sql);
  255. return 1;
  256. }
  257. /**
  258. * @param link_id (id in coursDb liens table)
  259. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  260. * @desc Record information for link event (when an user click on an added link)
  261. * it will be used in a redirection page
  262. */
  263. function event_link($link_id) {
  264. global $_user, $TABLETRACK_LINKS;
  265. $reallyNow = api_get_utc_datetime();
  266. if (isset($_user['user_id']) && $_user['user_id']!='') {
  267. $user_id = "'".Database::escape_string($_user['user_id'])."'";
  268. } else {
  269. // anonymous
  270. $user_id = "0";
  271. }
  272. $sql = "INSERT INTO ".$TABLETRACK_LINKS."
  273. ( links_user_id,
  274. links_cours_id,
  275. links_link_id,
  276. links_date,
  277. links_session_id
  278. ) VALUES (
  279. ".$user_id.",
  280. '".api_get_course_id()."',
  281. '".Database::escape_string($link_id)."',
  282. '".$reallyNow."',
  283. '".api_get_session_id()."'
  284. )";
  285. $res = Database::query($sql);
  286. return 1;
  287. }
  288. /**
  289. * Update the TRACK_E_EXERCICES exercises
  290. *
  291. * @param int exeid id of the attempt
  292. * @param int exo_id exercise id
  293. * @param mixed result score
  294. * @param int weighting ( higher score )
  295. * @param int duration ( duration of the attempt, in seconds )
  296. * @param int session_id
  297. * @param int learnpath_id (id of the learnpath)
  298. * @param int learnpath_item_id (id of the learnpath_item)
  299. *
  300. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  301. * @author Julio Montoya Armas <gugli100@gmail.com> Reworked 2010
  302. * @desc Record result of user when an exercice was done
  303. */
  304. function update_event_exercice($exeid, $exo_id, $score, $weighting,$session_id,$learnpath_id=0, $learnpath_item_id=0, $learnpath_item_view_id = 0, $duration, $question_list, $status = '', $remind_list = array()) {
  305. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  306. if ($exeid != '') {
  307. // Validation in case of fraud with actived control time
  308. if (!exercise_time_control_is_valid($exo_id)) {
  309. $score = 0;
  310. }
  311. $now = time();
  312. /* start_date wouldn't be updated
  313. $start_date_condition = '';
  314. //Validation in case of wrong start_date
  315. if (isset($_SESSION['exercice_start_date'])) {
  316. $start_date = $_SESSION['exercice_start_date'];
  317. $diff = abs($start_date - $now);
  318. if ($diff > 14400) { // 14400 = 4h*60*60 more than 4h of diff
  319. $start_date = $now - 1800; // Now - 30min
  320. }
  321. }*/
  322. if (!isset($status) || empty($status)) {
  323. $status = '';
  324. } else {
  325. $status = Database::escape_string($status);
  326. }
  327. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  328. $question_list = array_map('intval', $question_list);
  329. if (!empty($remind_list)) {
  330. $remind_list = array_map('intval', $remind_list);
  331. $remind_list = array_filter($remind_list);
  332. $remind_list = implode(",", $remind_list);
  333. } else {
  334. $remind_list = '';
  335. }
  336. $sql = "UPDATE $TABLETRACK_EXERCICES SET
  337. exe_exo_id = '".Database::escape_string($exo_id)."',
  338. exe_result = '".Database::escape_string($score)."',
  339. exe_weighting = '".Database::escape_string($weighting)."',
  340. session_id = '".Database::escape_string($session_id)."',
  341. orig_lp_id = '".Database::escape_string($learnpath_id)."',
  342. orig_lp_item_id = '".Database::escape_string($learnpath_item_id)."',
  343. orig_lp_item_view_id = '".Database::escape_string($learnpath_item_view_id)."',
  344. exe_duration = '".Database::escape_string($duration)."',
  345. exe_date = '".api_get_utc_datetime()."',
  346. status = '".$status."',
  347. questions_to_check = '".$remind_list."',
  348. data_tracking = '".implode(',', $question_list)."'
  349. WHERE exe_id = '".Database::escape_string($exeid)."'";
  350. $res = Database::query($sql);
  351. //Deleting control time session track
  352. //exercise_time_control_delete($exo_id);
  353. return $res;
  354. } else {
  355. return false;
  356. }
  357. }
  358. /**
  359. * This function creates an empty Exercise in STATISTIC_TRACK_E_EXERCICES table.
  360. * After that in exercise_result.php we call the update_event_exercice() to update the exercise
  361. * @return $id the last id registered, or false on error
  362. * @author Julio Montoya <gugli100@gmail.com>
  363. * @desc Record result of user when an exercice was done
  364. */
  365. function create_event_exercice($exo_id) {
  366. if (empty($exo_id) or (intval($exo_id)!=$exo_id)) { return false; }
  367. //error_log('create_event_exercice');
  368. $tbl_track_exe = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  369. $tbl_exe = Database::get_course_table(TABLE_QUIZ_TEST);
  370. $now = api_get_utc_datetime();
  371. $uid = api_get_user_id();
  372. $course_id = api_get_course_int_id();
  373. // First, check the exercise exists
  374. $sql_exe_id="SELECT exercises.id FROM $tbl_exe as exercises WHERE c_id = $course_id AND exercises.id=$exo_id";
  375. $res_exe_id=Database::query($sql_exe_id);
  376. if ($res_exe_id === false) { return false; } //sql error
  377. if (Database::num_rows($res_exe_id)<1) { return false;} //exe not found
  378. $row_exe_id=Database::fetch_row($res_exe_id);
  379. $exercise_id = intval($row_exe_id[0]);
  380. // Second, check if the record exists in the database (looking for incomplete records)
  381. $sql = "SELECT exe_id FROM $tbl_track_exe ";
  382. $condition = " WHERE exe_exo_id = $exo_id AND " .
  383. "exe_user_id = $uid AND " .
  384. "exe_cours_id = '".api_get_course_id()."' AND " .
  385. "status = 'incomplete' AND ".
  386. "session_id = ".api_get_session_id();
  387. $res = Database::query($sql.$condition);
  388. if ($res === false) {return false;}
  389. if (Database::num_rows($res) > 0) {
  390. $row = Database::fetch_array($res);
  391. return $row['exe_id'];
  392. }
  393. // No record was found, so create one
  394. // get expire time to insert into the tracking record
  395. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  396. $current_expired_time_key = get_time_control_key($exercise_id);
  397. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  398. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  399. } else {
  400. $expired_date = '0000-00-00 00:00:00';
  401. }
  402. $sql = "INSERT INTO $tbl_track_exe ( exe_user_id, exe_cours_id, expired_time_control, exe_exo_id, session_id)
  403. VALUES ( $uid, '".api_get_course_id()."' ,'$expired_date','$exo_id','".api_get_session_id()."')";
  404. $res = Database::query($sql);
  405. $id= Database::insert_id();
  406. return $id;
  407. }
  408. /**
  409. * Record an event for this attempt at answering an exercise
  410. * @param float Score achieved
  411. * @param string Answer given
  412. * @param integer Question ID
  413. * @param integer Exercise ID
  414. * @param integer Position
  415. * @return boolean Result of the insert query
  416. */
  417. function exercise_attempt($score, $answer, $quesId, $exeId, $j, $exercise_id = 0) {
  418. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  419. $score = Database::escape_string($score);
  420. $answer = Database::escape_string($answer);
  421. $quesId = Database::escape_string($quesId);
  422. $exeId = Database::escape_string($exeId);
  423. $j = Database::escape_string($j);
  424. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  425. //Validation in case of fraud with actived control time
  426. if (!exercise_time_control_is_valid($exercise_id)) {
  427. $score = 0;
  428. $answer = 0;
  429. $j = 0;
  430. }
  431. $reallyNow = api_get_utc_datetime();
  432. $user_id = api_get_user_id();
  433. if (!empty($user_id)) {
  434. $user_id = "'".$user_id."'";
  435. } else {
  436. // anonymous
  437. $user_id = api_get_anonymous_id();
  438. }
  439. $sql = "INSERT INTO $TBL_TRACK_ATTEMPT (exe_id, user_id, question_id, answer, marks, course_code, session_id, position, tms )
  440. VALUES (
  441. ".$exeId.",
  442. ".$user_id.",
  443. '".$quesId."',
  444. '".$answer."',
  445. '".$score."',
  446. '".api_get_course_id()."',
  447. '".api_get_session_id()."',
  448. '".$j."',
  449. '".$reallyNow."'
  450. )";
  451. //error_log($sql);
  452. if (!empty($quesId) && !empty($exeId) && !empty($user_id)) {
  453. $res = Database::query($sql);
  454. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  455. $recording_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  456. $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()."') ";
  457. Database::query($recording_changes);
  458. }
  459. return $res;
  460. } else {
  461. return false;
  462. }
  463. }
  464. /**
  465. * Record an hotspot spot for this attempt at answering an hotspot question
  466. * @param int Exercise ID
  467. * @param int Question ID
  468. * @param int Answer ID
  469. * @param int Whether this answer is correct (1) or not (0)
  470. * @param string Coordinates of this point (e.g. 123;324)
  471. * @return boolean Result of the insert query
  472. * @uses Course code and user_id from global scope $_cid and $_user
  473. */
  474. function exercise_attempt_hotspot($exe_id, $question_id, $answer_id, $correct, $coords, $exerciseId = 0) {
  475. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  476. //Validation in case of fraud with actived control time
  477. if (!exercise_time_control_is_valid($exerciseId)) {
  478. $correct = 0;
  479. }
  480. $tbl_track_e_hotspot = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  481. $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)".
  482. " VALUES ('" . api_get_user_id() . "'," .
  483. " '" . api_get_course_id() . "', " .
  484. " '" . Database :: escape_string($exe_id) . "', " .
  485. " '" . Database :: escape_string($question_id) . "'," .
  486. " '" . Database :: escape_string($answer_id) . "'," .
  487. " '" . Database :: escape_string($correct) . "'," .
  488. " '" . Database :: escape_string($coords) . "')";
  489. return $result = Database::query($sql);
  490. }
  491. /**
  492. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  493. * @desc Record information for common (or admin) events (in the track_e_default table)
  494. * @param string Type of event
  495. * @param string Type of value
  496. * @param string Value
  497. * @param string Timestamp (defaults to null)
  498. * @param integer User ID (defaults to null)
  499. * @param string Course code (defaults to null)
  500. */
  501. function event_system($event_type, $event_value_type, $event_value, $datetime = null, $user_id=null, $course_code=null) {
  502. global $_user;
  503. global $TABLETRACK_DEFAULT;
  504. $event_type = Database::escape_string($event_type);
  505. $event_value_type = Database::escape_string($event_value_type);
  506. $event_value = Database::escape_string($event_value);
  507. $datetime = Database::escape_string($datetime);
  508. $user_id = Database::escape_string($user_id);
  509. $course_code = Database::escape_string($course_code);
  510. if(!isset($datetime)) {
  511. $datetime = api_get_utc_datetime();
  512. }
  513. if(!isset($user_id)) {
  514. $user_id = 0;
  515. }
  516. if(!isset($course_code)) {
  517. $course_code = '';
  518. }
  519. $sql = "INSERT INTO $TABLETRACK_DEFAULT
  520. (default_user_id,
  521. default_cours_code,
  522. default_date,
  523. default_event_type,
  524. default_value_type,
  525. default_value
  526. )
  527. VALUES
  528. ('$user_id.',
  529. '$course_code',
  530. '$datetime',
  531. '$event_type',
  532. '$event_value_type',
  533. '$event_value')";
  534. $res = Database::query($sql);
  535. //Sending notofications to users
  536. $send_event_setting = api_get_setting('activate_send_event_by_mail');
  537. if (!empty($send_event_setting) && $send_event_setting == 'true') {
  538. global $language_file;
  539. //prepare message
  540. list($message, $subject) = get_event_message_and_subject($event_type);
  541. $mail_body=$message;
  542. if ( is_array($notification_infos) ){
  543. foreach ($notification_infos as $variable => $value) {
  544. $mail_body = str_replace('%'.$variable.'%',$value,$mail_body);
  545. }
  546. }
  547. //prepare mail common variables
  548. if(empty($subject)) {
  549. $subject = $event_type;
  550. }
  551. $mail_subject = '['.api_get_setting('siteName').'] '.$subject;
  552. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  553. $email_admin = api_get_setting('emailAdministrator');
  554. $emailfromaddr = api_get_setting('emailAdministrator');
  555. $emailfromname = api_get_setting('siteName');
  556. //Send mail to all subscribed users
  557. $users_arr = get_users_subscribed_to_event($event_type);
  558. foreach ($users_arr as $user) {
  559. $recipient_name = api_get_person_name($user['firstname'], $user['lastname']);
  560. $email = $user['email'];
  561. @api_mail($recipient_name, $email, $mail_subject, $mail_body, $sender_name, $email_admin);
  562. }
  563. }
  564. return true;
  565. }
  566. function get_event_message_and_subject($event_name){
  567. $event_name = Database::escape_string($event_name);
  568. $sql = 'SELECT m.message, m.subject FROM '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE).' e,'
  569. .Database::get_main_table(TABLE_MAIN_EVENT_TYPE_MESSAGE).' m
  570. WHERE m.event_type_id = e.id '.
  571. "AND e.name = '$event_name'";
  572. $res = Database::store_result(Database::query($sql),'ASSOC');
  573. $ret = array();
  574. if ( isset($res[0]['message']) ) {
  575. $ret[0] = $res[0]['message'];
  576. }else {
  577. $ret[0] = '';
  578. }
  579. if ( isset($res[0]['subject']) ) {
  580. $ret[1] = $res[0]['subject'];
  581. }else {
  582. $ret[1] = '';
  583. }
  584. return $ret;
  585. }
  586. function eventType_getAll($etId=0) {
  587. $etId = intval($etId);
  588. $sql = 'SELECT et.id, et.name, et.desc_lang_var, et.name_lang_var, em.message,em.subject FROM '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE).' et
  589. LEFT JOIN '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_MESSAGE).' em ON em.event_type_id = et.id';
  590. //WHERE em.language_id = 10
  591. //';
  592. $eventsTypes = Database::store_result(Database::query($sql),'ASSOC');
  593. // echo $sql;
  594. $to_return = array();
  595. foreach ($eventsTypes as $et){
  596. $et['nameLangVar'] = get_lang($et['name_lang_var']);
  597. $et['descLangVar'] = get_lang($et['desc_lang_var']);
  598. $to_return[] = $et;
  599. }
  600. return $to_return;
  601. }
  602. function get_users_subscribed_to_event($event_name){
  603. $event_name = Database::escape_string($event_name);
  604. $sql = 'SELECT u.* FROM '. Database::get_main_table(TABLE_MAIN_USER).' u,'
  605. .Database::get_main_table(TABLE_MAIN_EVENT_TYPE).' e,'
  606. .Database::get_main_table(TABLE_MAIN_EVENT_TYPE_REL_USER).' ue
  607. WHERE ue.user_id = u.user_id
  608. AND e.name = \''.$event_name.'\'
  609. AND e.id = ue.event_type_id';
  610. return Database::store_result(Database::query($sql),'ASSOC');
  611. }
  612. function eventType_getUsers($etId) {
  613. $etId = intval($etId);
  614. $sql = 'SELECT user.* FROM '.Database::get_main_table(TABLE_MAIN_USER).' user
  615. JOIN '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_REL_USER).' relUser ON relUser.user_id = user.user_id
  616. JOIN '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE).' et ON relUser.event_type_id = et.id
  617. WHERE et.id = '.$etId.'
  618. ';
  619. $eventsTypes = Database::store_result(Database::query($sql),'ASSOC');
  620. return $eventsTypes;
  621. }
  622. function eventType_mod($etId,$users,$message,$subject) {
  623. $etId = intval($etId);
  624. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_REL_USER).'
  625. WHERE event_type_id = '.$etId.'
  626. ';
  627. Database::query($sql);
  628. foreach($users as $user) {
  629. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_REL_USER).'
  630. (user_id,event_type_id)
  631. VALUES('.intval($user).','.$etId.')
  632. ';
  633. Database::query($sql);
  634. }
  635. $sql = 'UPDATE '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_MESSAGE).'
  636. SET message = "'.Database::escape_string($message).'",
  637. subject = "'.Database::escape_string($subject).'"
  638. WHERE event_type_id = '.$etId.'
  639. ';
  640. Database::query($sql);
  641. }
  642. /**
  643. * Gets the last attempt of an exercise based in the exe_id
  644. */
  645. function get_last_attempt_date_of_exercise($exe_id) {
  646. $exe_id = intval($exe_id);
  647. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  648. $track_exercises = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  649. $sql_track_attempt = 'SELECT max(tms) as last_attempt_date FROM '.$track_attempts.' WHERE exe_id='.$exe_id;
  650. $rs_last_attempt = Database::query($sql_track_attempt);
  651. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  652. $last_attempt_date = $row_last_attempt['last_attempt_date'];//Get the date of last attempt
  653. return $last_attempt_date;
  654. }
  655. /**
  656. * Gets how many attempts exists by user, exercise, learning path
  657. * @param int user id
  658. * @param int exercise id
  659. * @param int lp id
  660. * @param int lp item id
  661. * @param int lp item view id
  662. */
  663. function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id,$lp_item_view_id) {
  664. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  665. $user_id = intval($user_id);
  666. $exerciseId = intval($exerciseId);
  667. $lp_id = intval($lp_id);
  668. $lp_item_id = intval($lp_item_id);
  669. $lp_item_view_id= intval($lp_item_view_id);
  670. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  671. exe_exo_id = $exerciseId AND
  672. exe_user_id = $user_id AND
  673. status != 'incomplete' AND
  674. orig_lp_id = $lp_id AND
  675. orig_lp_item_id = $lp_item_id AND
  676. orig_lp_item_view_id = $lp_item_view_id AND
  677. exe_cours_id = '".api_get_course_id()."' AND
  678. session_id = '" . api_get_session_id() . "'";
  679. $query = Database::query($sql);
  680. if (Database::num_rows($query) > 0 ) {
  681. $attempt = Database :: fetch_array($query,'ASSOC');
  682. return $attempt['count'];
  683. } else {
  684. return 0;
  685. }
  686. }
  687. function get_attempt_count_not_finished($user_id, $exerciseId, $lp_id, $lp_item_id) {
  688. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  689. $user_id = intval($user_id);
  690. $exerciseId = intval($exerciseId);
  691. $lp_id = intval($lp_id);
  692. $lp_item_id = intval($lp_item_id);
  693. $lp_item_view_id= intval($lp_item_view_id);
  694. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  695. exe_exo_id = $exerciseId AND
  696. exe_user_id = $user_id AND
  697. status != 'incomplete' AND
  698. orig_lp_id = $lp_id AND
  699. orig_lp_item_id = $lp_item_id AND
  700. exe_cours_id = '".api_get_course_id()."' AND
  701. session_id = '" . api_get_session_id() . "'";
  702. $query = Database::query($sql);
  703. if (Database::num_rows($query) > 0 ) {
  704. $attempt = Database :: fetch_array($query,'ASSOC');
  705. return $attempt['count'];
  706. } else {
  707. return 0;
  708. }
  709. }
  710. function delete_student_lp_events($user_id, $lp_id, $course, $session_id) {
  711. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  712. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  713. $course_id = $course['real_id'];
  714. if (empty($course_id)) {
  715. $course_id = api_get_course_int_id();
  716. }
  717. $track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  718. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  719. $recording_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  720. $user_id = intval($user_id);
  721. $lp_id = intval($lp_id);
  722. $session_id = intval($session_id);
  723. //make sure we have the exact lp_view_id
  724. $sqlview = "SELECT id FROM $lp_view_table WHERE c_id = $course_id AND user_id = $user_id AND lp_id = $lp_id AND session_id = $session_id ";
  725. $resultview = Database::query($sqlview);
  726. $view = Database::fetch_array($resultview, 'ASSOC');
  727. $lp_view_id = $view['id'];
  728. $sql_delete = "DELETE FROM $lp_item_view_table WHERE c_id = $course_id AND lp_view_id = $view_id ";
  729. $result = Database::query($sql_delete);
  730. $sql_delete = "DELETE FROM $lp_view_table WHERE c_id = $course_id AND user_id = $user_id AND lp_id= $lp_id AND session_id= $session_id ";
  731. $result = Database::query($sql_delete);
  732. $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";
  733. $result = Database::query($select_all_attempts);
  734. $exe_list = array();
  735. while ($row = Database::fetch_array($result, 'ASSOC')) {
  736. $exe_list[] = $row['exe_id'];
  737. }
  738. if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) {
  739. $sql_delete = "DELETE FROM $track_e_exercises WHERE exe_id IN (".implode(',',$exe_list).")";
  740. $result = Database::query($sql_delete);
  741. $sql_delete = "DELETE FROM $track_attempts WHERE exe_id IN (".implode(',',$exe_list).")";
  742. $result = Database::query($sql_delete);
  743. $sql_delete = "DELETE FROM $recording_table WHERE exe_id IN (".implode(',',$exe_list).")";
  744. $result = Database::query($sql_delete);
  745. }
  746. }
  747. /**
  748. * Delete all exercise attempts (included in LP or not)
  749. *
  750. * @param int user id
  751. * @param int exercise id
  752. * @param string course code
  753. * @param int session id
  754. */
  755. function delete_all_incomplete_attempts($user_id, $exercise_id, $course_code, $session_id = 0) {
  756. $track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  757. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  758. $user_id = intval($user_id);
  759. $exercise_id = intval($exercise_id);
  760. $course_code = Database::escape_string($course_code);
  761. $session_id = intval($session_id);
  762. if (!empty($user_id) && !empty($exercise_id) && !empty($course_code)) {
  763. $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' ";
  764. $result = Database::query($sql);
  765. }
  766. }
  767. /**
  768. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  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_all_exercise_results($exercise_id, $course_code, $session_id = 0, $load_question_list = true) {
  776. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  777. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  778. $course_code = Database::escape_string($course_code);
  779. $exercise_id = intval($exercise_id);
  780. $session_id = intval($session_id);
  781. $sql = "SELECT * FROM $TABLETRACK_EXERCICES
  782. 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";
  783. $res = Database::query($sql);
  784. $list = array();
  785. while($row = Database::fetch_array($res,'ASSOC')) {
  786. $list[$row['exe_id']] = $row;
  787. if ($load_question_list) {
  788. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  789. $res_question = Database::query($sql);
  790. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  791. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  792. }
  793. }
  794. }
  795. return $list;
  796. }
  797. /**
  798. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  799. * @param string course code
  800. * @param int session id
  801. * @return array with the results
  802. *
  803. */
  804. function get_all_exercise_results_by_course($course_code, $session_id = 0, $get_count = true) {
  805. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  806. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  807. $course_code = Database::escape_string($course_code);
  808. $session_id = intval($session_id);
  809. $select = '*';
  810. if ($get_count) {
  811. $select = 'count(*) as count';
  812. }
  813. $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";
  814. $res = Database::query($sql);
  815. if ($get_count) {
  816. $row = Database::fetch_array($res,'ASSOC');
  817. return $row['count'];
  818. } else {
  819. $list = array();
  820. while($row = Database::fetch_array($res,'ASSOC')) {
  821. $list[$row['exe_id']] = $row;
  822. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  823. $res_question = Database::query($sql);
  824. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  825. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  826. }
  827. }
  828. return $list;
  829. }
  830. }
  831. /**
  832. * Gets all exercise results (NO Exercises in LPs) from a given exercise id, course, session
  833. * @param int exercise id
  834. * @param string course code
  835. * @param int session id
  836. * @return array with the results
  837. *
  838. */
  839. function get_all_exercise_results_by_user($user_id, $course_code, $session_id = 0) {
  840. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  841. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  842. $course_code = Database::escape_string($course_code);
  843. $exercise_id = intval($exercise_id);
  844. $session_id = intval($session_id);
  845. $user_id = intval($user_id);
  846. $sql = "SELECT * FROM $table_track_exercises WHERE status = '' AND exe_user_id = $user_id 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";
  847. $res = Database::query($sql);
  848. $list = array();
  849. while($row = Database::fetch_array($res,'ASSOC')) {
  850. $list[$row['exe_id']] = $row;
  851. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  852. $res_question = Database::query($sql);
  853. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  854. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  855. }
  856. }
  857. //echo '<pre>'; print_r($list);
  858. return $list;
  859. }
  860. /**
  861. * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session
  862. * @param int exercise id
  863. * @param string course code
  864. * @param int session id
  865. * @return array with the results
  866. *
  867. */
  868. function get_exercise_results_by_attempt($exe_id) {
  869. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  870. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  871. $table_track_attempt_recording = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  872. $exe_id = intval($exe_id);
  873. $sql = "SELECT * FROM $table_track_exercises WHERE status = '' AND exe_id = $exe_id";
  874. $res = Database::query($sql);
  875. $list = array();
  876. if (Database::num_rows($res)) {
  877. $row = Database::fetch_array($res,'ASSOC');
  878. //Checking if this attempt was revised by a teacher
  879. $sql_revised = 'SELECT exe_id FROM ' . $table_track_attempt_recording . ' WHERE author != "" AND exe_id = '.$exe_id.' LIMIT 1';
  880. $res_revised = Database::query($sql_revised);
  881. $row['attempt_revised'] = 0;
  882. if (Database::num_rows($res_revised) > 0) {
  883. $row['attempt_revised'] = 1;
  884. }
  885. $list[$exe_id] = $row;
  886. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY tms ASC";
  887. $res_question = Database::query($sql);
  888. while ($row_q = Database::fetch_array($res_question,'ASSOC')) {
  889. $list[$exe_id]['question_list'][$row_q['question_id']] = $row_q;
  890. }
  891. }
  892. // echo '<pre>'; print_r($list); echo "</pre>";
  893. return $list;
  894. }
  895. /**
  896. * Gets exercise results (NO Exercises in LPs) from a given user, exercise id, course, session, lp_id, lp_item_id
  897. * @param int user id
  898. * @param int exercise id
  899. * @param string course code
  900. * @param int session id
  901. * @param int lp id
  902. * @param int lp item id
  903. * @param string order asc or desc
  904. * @return array with the results
  905. *
  906. */
  907. function get_exercise_results_by_user($user_id, $exercise_id, $course_code, $session_id = 0, $lp_id = 0, $lp_item_id = 0, $order = null) {
  908. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  909. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  910. $table_track_attempt_recording = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  911. $course_code = Database::escape_string($course_code);
  912. $exercise_id = intval($exercise_id);
  913. $session_id = intval($session_id);
  914. $user_id = intval($user_id);
  915. $lp_id = intval($lp_id);
  916. $lp_item_id = intval($lp_item_id);
  917. if (!in_array(strtolower($order), array('asc', 'desc'))) {
  918. $order = 'asc';
  919. }
  920. $sql = "SELECT * FROM $table_track_exercises
  921. WHERE status = '' AND
  922. exe_user_id = $user_id AND
  923. exe_cours_id = '$course_code' AND
  924. exe_exo_id = $exercise_id AND
  925. session_id = $session_id AND
  926. orig_lp_id = $lp_id AND
  927. orig_lp_item_id = $lp_item_id
  928. ORDER by exe_id $order ";
  929. $res = Database::query($sql);
  930. $list = array();
  931. while($row = Database::fetch_array($res,'ASSOC')) {
  932. //Checking if this attempt was revised by a teacher
  933. $sql_revised = 'SELECT exe_id FROM ' . $table_track_attempt_recording . ' WHERE author != "" AND exe_id = '.$row['exe_id'].' LIMIT 1';
  934. $res_revised = Database::query($sql_revised);
  935. $row['attempt_revised'] = 0;
  936. if (Database::num_rows($res_revised) > 0) {
  937. $row['attempt_revised'] = 1;
  938. }
  939. $list[$row['exe_id']] = $row;
  940. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  941. $res_question = Database::query($sql);
  942. while ($row_q = Database::fetch_array($res_question,'ASSOC')) {
  943. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  944. }
  945. }
  946. return $list;
  947. }
  948. /**
  949. * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session
  950. * @param int exercise id
  951. * @param string course code
  952. * @param int session id
  953. * @return array with the results
  954. *
  955. */
  956. function count_exercise_attempts_by_user($user_id, $exercise_id, $course_code, $session_id = 0) {
  957. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  958. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  959. $course_code = Database::escape_string($course_code);
  960. $exercise_id = intval($exercise_id);
  961. $session_id = intval($session_id);
  962. $user_id = intval($user_id);
  963. $sql = "SELECT count(*) as count FROM $TABLETRACK_EXERCICES
  964. 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";
  965. $res = Database::query($sql);
  966. $result = 0;
  967. if (Database::num_rows($res) > 0 ) {
  968. $row = Database::fetch_array($res,'ASSOC');
  969. $result = $row['count'];
  970. }
  971. return $result;
  972. }
  973. /**
  974. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  975. * @param int exercise id
  976. * @param string course code
  977. * @param int session id
  978. * @return array with the results
  979. *
  980. */
  981. function get_best_exercise_results_by_user($exercise_id, $course_code, $session_id = 0) {
  982. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  983. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  984. $course_code = Database::escape_string($course_code);
  985. $exercise_id = intval($exercise_id);
  986. $session_id = intval($session_id);
  987. $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";
  988. $res = Database::query($sql);
  989. $list = array();
  990. while($row = Database::fetch_array($res,'ASSOC')) {
  991. $list[$row['exe_id']] = $row;
  992. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  993. $res_question = Database::query($sql);
  994. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  995. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  996. }
  997. }
  998. //Getting the best results of every student
  999. $best_score_return = array();
  1000. foreach($list as $student_result) {
  1001. $user_id = $student_result['exe_user_id'];
  1002. $current_best_score[$user_id] = $student_result['exe_result'];
  1003. //echo $current_best_score[$user_id].' - '.$best_score_return[$user_id]['exe_result'].'<br />';
  1004. if ($current_best_score[$user_id] > $best_score_return[$user_id]['exe_result']) {
  1005. $best_score_return[$user_id] = $student_result;
  1006. }
  1007. }
  1008. return $best_score_return;
  1009. }
  1010. function count_exercise_result_not_validated($exercise_id, $course_code, $session_id = 0) {
  1011. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1012. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1013. $course_code = Database::escape_string($course_code);
  1014. $session_id = intval($session_id);
  1015. $exercise_id = intval($exercise_id);
  1016. $status = Database::escape_string($status);
  1017. $sql = "SELECT count(e.exe_id) as count FROM $table_track_exercises e LEFT JOIN $table_track_attempt a ON e.exe_id = a.exe_id
  1018. WHERE exe_exo_id = $exercise_id AND
  1019. exe_cours_id = '$course_code' AND
  1020. e.session_id = $session_id AND
  1021. orig_lp_id = 0 AND
  1022. marks IS NULL AND
  1023. status = '' AND
  1024. orig_lp_item_id = 0 ORDER BY e.exe_id";
  1025. $res = Database::query($sql);
  1026. $row = Database::fetch_array($res,'ASSOC');
  1027. return $row['count'];
  1028. }
  1029. /**
  1030. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1031. * @param int exercise id
  1032. * @param string course code
  1033. * @param int session id
  1034. * @return array with the results
  1035. *
  1036. */
  1037. function get_count_exercises_attempted_by_course($course_code, $session_id = 0) {
  1038. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1039. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1040. $course_code = Database::escape_string($course_code);
  1041. $session_id = intval($session_id);
  1042. $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";
  1043. $res = Database::query($sql);
  1044. $count = 0;
  1045. if (Database::num_rows($res) > 0) {
  1046. $count = Database::num_rows($res);
  1047. }
  1048. return $count;
  1049. }
  1050. /**
  1051. * Gets all exercise events from a Learning Path within a Course nd Session
  1052. * @param int exercise id
  1053. * @param string course_code
  1054. * @param int session id
  1055. * @return array
  1056. */
  1057. function get_all_exercise_event_from_lp($exercise_id, $course_code, $session_id = 0) {
  1058. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1059. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1060. $course_code = Database::escape_string($course_code);
  1061. $exercise_id = intval($exercise_id);
  1062. $session_id = intval($session_id);
  1063. $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";
  1064. $res = Database::query($sql);
  1065. $list = array();
  1066. while($row = Database::fetch_array($res,'ASSOC')) {
  1067. $list[$row['exe_id']] = $row;
  1068. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1069. $res_question = Database::query($sql);
  1070. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1071. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1072. }
  1073. }
  1074. return $list;
  1075. }
  1076. function get_all_exercises_from_lp($lp_id, $course_id) {
  1077. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  1078. $course_id = intval($course_id);
  1079. $lp_id = intval($lp_id);
  1080. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$lp_id."' ORDER BY parent_item_id, display_order";
  1081. $res = Database::query($sql);
  1082. $my_exercise_list = array();
  1083. while($row = Database::fetch_array($res,'ASSOC')) {
  1084. if ($row['item_type'] == 'quiz') {
  1085. $my_exercise_list[] = $row;
  1086. }
  1087. }
  1088. return $my_exercise_list;
  1089. }
  1090. /**
  1091. * This function gets the comments of an exercise
  1092. *
  1093. * @param int $id
  1094. * @param int $question_id
  1095. * @return str the comment
  1096. */
  1097. function get_comments($id,$question_id) {
  1098. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1099. $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";
  1100. $sqlres = Database::query($sql);
  1101. $comm = Database::result($sqlres,0,"teacher_comment");
  1102. return $comm;
  1103. }
  1104. function get_all_exercise_event_by_exe_id($exe_id) {
  1105. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1106. $exe_id = intval($exe_id);
  1107. $list = array();
  1108. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY position";
  1109. $res_question = Database::query($sql);
  1110. if (Database::num_rows($res_question))
  1111. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1112. $list[$row_q['question_id']][] = $row_q;
  1113. }
  1114. return $list;
  1115. }
  1116. function delete_attempt($exe_id, $user_id, $course_code, $session_id, $question_id) {
  1117. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1118. $exe_id = intval($exe_id);
  1119. $user_id = intval($user_id);
  1120. $course_code = Database::escape_string($course_code);
  1121. $session_id = intval($session_id);
  1122. $question_id = intval($question_id);
  1123. $sql = "DELETE FROM $table_track_attempt WHERE exe_id = $exe_id AND user_id = $user_id AND course_code = '$course_code' AND session_id = $session_id AND question_id = $question_id ";
  1124. Database::query($sql);
  1125. }
  1126. function delete_attempt_hotspot($exe_id, $user_id, $course_code, $question_id) {
  1127. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1128. $exe_id = intval($exe_id);
  1129. $user_id = intval($user_id);
  1130. $course_code = Database::escape_string($course_code);
  1131. //$session_id = intval($session_id);
  1132. $question_id = intval($question_id);
  1133. $sql = "DELETE FROM $table_track_attempt WHERE hotspot_exe_id = $exe_id AND hotspot_user_id = $user_id AND hotspot_course_code = '$course_code' AND hotspot_question_id = $question_id ";
  1134. Database::query($sql);
  1135. }