events.lib.inc.php 41 KB

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