events.lib.inc.php 51 KB

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