events.lib.inc.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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_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. {
  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['HTTP_REFERER'])) {
  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. $ip = api_get_real_ip();
  47. $remhost = @ getHostByAddr($ip);
  48. if ($remhost == $ip)
  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. {
  70. global $_user;
  71. global $TABLETRACK_LOGIN;
  72. $reallyNow = api_get_utc_datetime();
  73. $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, login_ip, login_date, logout_date) VALUES
  74. ('".$_user['user_id']."',
  75. '".Database::escape_string(api_get_real_ip())."',
  76. '".$reallyNow."',
  77. '".$reallyNow."'
  78. )";
  79. Database::query($sql);
  80. // auto subscribe
  81. $user_status = $_user['status'];
  82. $user_status = $_user['status'] == SESSIONADMIN ? 'sessionadmin' :
  83. $_user['status'] == COURSEMANAGER ? 'teacher' :
  84. $_user['status'] == DRH ? 'DRH' : '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. {
  102. global $TABLETRACK_ACCESS, $TABLETRACK_LASTACCESS;
  103. $id_session = api_get_session_id();
  104. $now = api_get_utc_datetime();
  105. $_cid = api_get_course_id();
  106. $user_id = api_get_user_id();
  107. if ($user_id) {
  108. $user_id = "'".$user_id."'";
  109. } else {
  110. $user_id = "0"; // no one
  111. }
  112. $sql = "INSERT INTO ".$TABLETRACK_ACCESS." (access_user_id, access_cours_code, access_date, access_session_id) VALUES
  113. (".$user_id.", '".$_cid."', '".$now."','".$id_session."')";
  114. $res = Database::query($sql);
  115. // added for "what's new" notification
  116. $sql = "UPDATE $TABLETRACK_LASTACCESS SET access_date = '$now'
  117. WHERE access_user_id = $user_id AND access_cours_code = '$_cid' AND access_tool IS NULL AND access_session_id=".$id_session;
  118. $res = Database::query($sql);
  119. if (Database::affected_rows() == 0) {
  120. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, access_cours_code, access_date, access_session_id)
  121. VALUES (".$user_id.", '".$_cid."', '$now', '".$id_session."')";
  122. $res = Database::query($sql);
  123. }
  124. // end "what's new" notification
  125. return 1;
  126. }
  127. /**
  128. * @param tool name of the tool (name in mainDb.accueil table)
  129. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  130. * @desc Record information for access event for tools
  131. *
  132. * $tool can take this values :
  133. * Links, Calendar, Document, Announcements,
  134. * Group, Video, Works, Users, Exercices, Course Desc
  135. * ...
  136. * Values can be added if new modules are created (15char max)
  137. * I encourage to use $nameTool as $tool when calling this function
  138. *
  139. * Functionality for "what's new" notification is added by Toon Van Hoecke
  140. */
  141. function event_access_tool($tool, $id_session = 0)
  142. {
  143. global $_configuration;
  144. global $_user;
  145. global $_cid;
  146. global $TABLETRACK_ACCESS;
  147. global $_course;
  148. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  149. $id_session = api_get_session_id();
  150. $tool = Database::escape_string($tool);
  151. $reallyNow = api_get_utc_datetime();
  152. $user_id = $_user['user_id'] ? "'".$_user['user_id']."'" : "0"; // no one
  153. // record information
  154. // only if user comes from the course $_cid
  155. //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
  156. //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
  157. $pos = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$_course['path'])) : false;
  158. // added for "what's new" notification
  159. $pos2 = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower($_configuration['root_web']."index")) : false;
  160. // end "what's new" notification
  161. if ($pos !== false || $pos2 !== false) {
  162. $sql = "INSERT INTO ".$TABLETRACK_ACCESS."
  163. (access_user_id,
  164. access_cours_code,
  165. access_tool,
  166. access_date,
  167. access_session_id
  168. )
  169. VALUES
  170. (".$user_id.",".// Don't add ' ' around value, it's already done.
  171. "'".$_cid."' ,
  172. '".$tool."',
  173. '".$reallyNow."',
  174. '".$id_session."')";
  175. $res = Database::query($sql);
  176. }
  177. // "what's new" notification
  178. $sql = "UPDATE $TABLETRACK_LASTACCESS
  179. SET access_date = '$reallyNow'
  180. WHERE access_user_id = ".$user_id." AND access_cours_code = '".$_cid."' AND access_tool = '".$tool."' AND access_session_id=".$id_session;
  181. $res = Database::query($sql);
  182. if (Database::affected_rows() == 0) {
  183. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id,access_cours_code,access_tool, access_date, access_session_id)
  184. VALUES (".$user_id.", '".$_cid."' , '$tool', '$reallyNow', $id_session)";
  185. $res = Database::query($sql);
  186. }
  187. return 1;
  188. }
  189. /**
  190. * @param doc_id id of document (id in mainDb.document table)
  191. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  192. * @desc Record information for download event
  193. * (when an user click to d/l a document)
  194. * it will be used in a redirection page
  195. * bug fixed: Roan Embrechts
  196. * Roan:
  197. * The user id is put in single quotes,
  198. * (why? perhaps to prevent sql insertion hacks?)
  199. * and later again.
  200. * Doing this twice causes an error, I remove one of them.
  201. */
  202. function event_download($doc_url)
  203. {
  204. $tbl_stats_downloads = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  205. $doc_url = Database::escape_string($doc_url);
  206. $reallyNow = api_get_utc_datetime();
  207. $user_id = "'".api_get_user_id()."'";
  208. $_cid = api_get_course_id();
  209. $sql = "INSERT INTO $tbl_stats_downloads (
  210. down_user_id,
  211. down_cours_id,
  212. down_doc_path,
  213. down_date,
  214. down_session_id
  215. )
  216. VALUES (
  217. ".$user_id.",
  218. '".$_cid."',
  219. '".$doc_url."',
  220. '".$reallyNow."',
  221. '".api_get_session_id()."'
  222. )";
  223. Database::query($sql);
  224. return 1;
  225. }
  226. /**
  227. * @param doc_id id of document (id in mainDb.document table)
  228. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  229. * @desc Record information for upload event
  230. * used in the works tool to record informations when
  231. * an user upload 1 work
  232. */
  233. function event_upload($doc_id)
  234. {
  235. global $_user;
  236. global $_cid;
  237. global $TABLETRACK_UPLOADS;
  238. $reallyNow = api_get_utc_datetime();
  239. if (isset($_user['user_id']) && $_user['user_id'] != '') {
  240. $user_id = "'".$_user['user_id']."'";
  241. } else {
  242. $user_id = "0"; // anonymous
  243. }
  244. $sql = "INSERT INTO ".$TABLETRACK_UPLOADS."
  245. ( upload_user_id,
  246. upload_cours_id,
  247. upload_work_id,
  248. upload_date,
  249. upload_session_id
  250. )
  251. VALUES (
  252. ".$user_id.",
  253. '".$_cid."',
  254. '".$doc_id."',
  255. '".$reallyNow."',
  256. '".api_get_session_id()."'
  257. )";
  258. $res = Database::query($sql);
  259. return 1;
  260. }
  261. /**
  262. * @param link_id (id in coursDb liens table)
  263. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  264. * @desc Record information for link event (when an user click on an added link)
  265. * it will be used in a redirection page
  266. */
  267. function event_link($link_id)
  268. {
  269. global $_user, $TABLETRACK_LINKS;
  270. $reallyNow = api_get_utc_datetime();
  271. if (isset($_user['user_id']) && $_user['user_id'] != '') {
  272. $user_id = "'".Database::escape_string($_user['user_id'])."'";
  273. } else {
  274. // anonymous
  275. $user_id = "0";
  276. }
  277. $sql = "INSERT INTO ".$TABLETRACK_LINKS."
  278. ( links_user_id,
  279. links_cours_id,
  280. links_link_id,
  281. links_date,
  282. links_session_id
  283. ) VALUES (
  284. ".$user_id.",
  285. '".api_get_course_id()."',
  286. '".Database::escape_string($link_id)."',
  287. '".$reallyNow."',
  288. '".api_get_session_id()."'
  289. )";
  290. $res = Database::query($sql);
  291. return 1;
  292. }
  293. /**
  294. * Update the TRACK_E_EXERCICES exercises
  295. *
  296. * @param int exeid id of the attempt
  297. * @param int exo_id exercise id
  298. * @param mixed result score
  299. * @param int weighting ( higher score )
  300. * @param int duration ( duration of the attempt in seconds )
  301. * @param int session_id
  302. * @param int learnpath_id (id of the learnpath)
  303. * @param int learnpath_item_id (id of the learnpath_item)
  304. *
  305. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  306. * @author Julio Montoya Armas <gugli100@gmail.com> Reworked 2010
  307. * @desc Record result of user when an exercice was done
  308. */
  309. function update_event_exercice($exeid, $exo_id, $score, $weighting, $session_id, $learnpath_id = 0, $learnpath_item_id = 0, $learnpath_item_view_id = 0, $duration = 0, $status = '', $remind_list = array(), $end_date = null)
  310. {
  311. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  312. global $debug;
  313. if ($debug) {
  314. error_log('Called to update_event_exercice');
  315. error_log('duration:'.$duration);
  316. }
  317. if ($exeid != '') {
  318. // Validation in case of fraud with actived control time
  319. if (!exercise_time_control_is_valid($exo_id, $learnpath_id, $learnpath_item_id)) {
  320. $score = 0;
  321. }
  322. if (!isset($status) || empty($status)) {
  323. $status = '';
  324. } else {
  325. $status = Database::escape_string($status);
  326. }
  327. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  328. 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. if (empty($end_date)) {
  336. $end_date = api_get_utc_datetime();
  337. }
  338. $sql = "UPDATE $TABLETRACK_EXERCICES SET
  339. exe_exo_id = '".Database::escape_string($exo_id)."',
  340. exe_result = '".Database::escape_string($score)."',
  341. exe_weighting = '".Database::escape_string($weighting)."',
  342. session_id = '".Database::escape_string($session_id)."',
  343. orig_lp_id = '".Database::escape_string($learnpath_id)."',
  344. orig_lp_item_id = '".Database::escape_string($learnpath_item_id)."',
  345. orig_lp_item_view_id = '".Database::escape_string($learnpath_item_view_id)."',
  346. exe_duration = '".Database::escape_string($duration)."',
  347. exe_date = '".$end_date."',
  348. status = '".$status."',
  349. questions_to_check = '".$remind_list."'
  350. WHERE exe_id = '".Database::escape_string($exeid)."'";
  351. $res = Database::query($sql);
  352. if ($debug)
  353. error_log('update_event_exercice called');
  354. if ($debug)
  355. error_log("$sql");
  356. //Deleting control time session track
  357. //exercise_time_control_delete($exo_id);
  358. return $res;
  359. } else {
  360. return false;
  361. }
  362. }
  363. /**
  364. * This function creates an empty Exercise in STATISTIC_TRACK_E_EXERCICES table.
  365. * After that in exercise_result.php we call the update_event_exercice() to update the exercise
  366. * @return $id the last id registered, or false on error
  367. * @author Julio Montoya <gugli100@gmail.com>
  368. * @desc Record result of user when an exercice was done
  369. * @deprecated this function seems to be deprecated
  370. */
  371. function create_event_exercice($exo_id)
  372. {
  373. if (empty($exo_id) or (intval($exo_id) != $exo_id)) {
  374. return false;
  375. }
  376. //error_log('create_event_exercice');
  377. $tbl_track_exe = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  378. $tbl_exe = Database::get_course_table(TABLE_QUIZ_TEST);
  379. $uid = api_get_user_id();
  380. $course_id = api_get_course_int_id();
  381. // First, check the exercise exists
  382. $sql_exe_id = "SELECT exercises.id FROM $tbl_exe as exercises WHERE c_id = $course_id AND exercises.id=$exo_id";
  383. $res_exe_id = Database::query($sql_exe_id);
  384. if ($res_exe_id === false) {
  385. return false;
  386. } //sql error
  387. if (Database::num_rows($res_exe_id) < 1) {
  388. return false;
  389. } //exe not found
  390. $row_exe_id = Database::fetch_row($res_exe_id);
  391. $exercise_id = intval($row_exe_id[0]);
  392. // Second, check if the record exists in the database (looking for incomplete records)
  393. $sql = "SELECT exe_id FROM $tbl_track_exe WHERE exe_exo_id = $exo_id AND ".
  394. "exe_user_id = $uid AND ".
  395. "exe_cours_id = '".api_get_course_id()."' AND ".
  396. "status = 'incomplete' AND ".
  397. "session_id = ".api_get_session_id();
  398. $res = Database::query($sql);
  399. if ($res === false) {
  400. return false;
  401. }
  402. if (Database::num_rows($res) > 0) {
  403. $row = Database::fetch_array($res);
  404. return $row['exe_id'];
  405. }
  406. // No record was found, so create one
  407. // get expire time to insert into the tracking record
  408. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  409. $current_expired_time_key = get_time_control_key($exercise_id);
  410. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  411. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  412. } else {
  413. $expired_date = '0000-00-00 00:00:00';
  414. }
  415. $sql = "INSERT INTO $tbl_track_exe ( exe_user_id, exe_cours_id, expired_time_control, exe_exo_id, session_id)
  416. VALUES ( $uid, '".api_get_course_id()."' ,'$expired_date','$exo_id','".api_get_session_id()."')";
  417. $res = Database::query($sql);
  418. $id = Database::insert_id();
  419. return $id;
  420. }
  421. /**
  422. * Record an event for this attempt at answering an exercise
  423. * @param float Score achieved
  424. * @param string Answer given
  425. * @param integer Question ID
  426. * @param integer Exercise ID
  427. * @param integer Position
  428. * @return boolean Result of the insert query
  429. */
  430. function exercise_attempt($score, $answer, $question_id, $exe_id, $position, $exercise_id = 0, $nano = null)
  431. {
  432. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  433. global $debug, $learnpath_id, $learnpath_item_id;
  434. $score = Database::escape_string($score);
  435. $answer = Database::escape_string($answer);
  436. $question_id = Database::escape_string($question_id);
  437. $exe_id = Database::escape_string($exe_id);
  438. $position = Database::escape_string($position);
  439. $now = api_get_utc_datetime();
  440. $user_id = api_get_user_id();
  441. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  442. if ($debug)
  443. error_log("----- entering exercise_attempt() function ------");
  444. if ($debug)
  445. error_log("answer: $answer");
  446. if ($debug)
  447. error_log("score: $score");
  448. if ($debug)
  449. error_log("question_id : $question_id");
  450. if ($debug)
  451. error_log("position: $position");
  452. //Validation in case of fraud with actived control time
  453. if (!exercise_time_control_is_valid($exercise_id, $learnpath_id, $learnpath_item_id)) {
  454. if ($debug)
  455. error_log("exercise_time_control_is_valid is false");
  456. $score = 0;
  457. $answer = 0;
  458. }
  459. if (!empty($user_id)) {
  460. $user_id = "'".$user_id."'";
  461. } else {
  462. // anonymous
  463. $user_id = api_get_anonymous_id();
  464. }
  465. $file = '';
  466. if (isset($nano)) {
  467. $file = Database::escape_string(basename($nano->load_filename_if_exists(false)));
  468. }
  469. $course_code = api_get_course_id();
  470. $session_id = api_get_session_id();
  471. if (!empty($question_id) && !empty($exe_id) && !empty($user_id)) {
  472. //Check if attempt exists
  473. $sql = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT
  474. WHERE course_code = '$course_code' AND session_id = $session_id AND exe_id = $exe_id AND user_id = $user_id AND question_id = $question_id AND position = $position";
  475. $result = Database::query($sql);
  476. if (Database::num_rows($result)) {
  477. if ($debug)
  478. error_log("Attempt already exist: exe_id: $exe_id - user_id:$user_id - question_id:$question_id");
  479. //The attempt already exist do not update use update_event_exercice() instead
  480. return false;
  481. }
  482. $sql = "INSERT INTO $TBL_TRACK_ATTEMPT (exe_id, user_id, question_id, answer, marks, course_code, session_id, position, tms, filename)
  483. VALUES (
  484. ".$exe_id.",
  485. ".$user_id.",
  486. '".$question_id."',
  487. '".$answer."',
  488. '".$score."',
  489. '".$course_code."',
  490. '".$session_id."',
  491. '".$position."',
  492. '".$now."',
  493. '".$file."'
  494. )";
  495. if ($debug)
  496. error_log("Saving question attempt: ");
  497. if ($debug)
  498. error_log($sql);
  499. $res = Database::query($sql);
  500. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
  501. $recording_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  502. if ($debug)
  503. error_log("Saving e attempt recording ");
  504. $recording_changes = "INSERT INTO $recording_table (exe_id, question_id, marks, insert_date, author, session_id) VALUES ('$exe_id','$question_id','$score','".api_get_utc_datetime()."','', '".api_get_session_id()."') ";
  505. Database::query($recording_changes);
  506. }
  507. return $res;
  508. } else {
  509. return false;
  510. }
  511. }
  512. /**
  513. * Record an hotspot spot for this attempt at answering an hotspot question
  514. * @param int Exercise ID
  515. * @param int Question ID
  516. * @param int Answer ID
  517. * @param int Whether this answer is correct (1) or not (0)
  518. * @param string Coordinates of this point (e.g. 123;324)
  519. * @return boolean Result of the insert query
  520. * @uses Course code and user_id from global scope $_cid and $_user
  521. */
  522. function exercise_attempt_hotspot($exe_id, $question_id, $answer_id, $correct, $coords, $exerciseId = 0)
  523. {
  524. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  525. global $safe_lp_id, $safe_lp_item_id;
  526. //Validation in case of fraud with actived control time
  527. if (!exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
  528. $correct = 0;
  529. }
  530. $tbl_track_e_hotspot = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  531. $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)".
  532. " VALUES ('".api_get_user_id()."',".
  533. " '".api_get_course_id()."', ".
  534. " '".Database :: escape_string($exe_id)."', ".
  535. " '".Database :: escape_string($question_id)."',".
  536. " '".Database :: escape_string($answer_id)."',".
  537. " '".Database :: escape_string($correct)."',".
  538. " '".Database :: escape_string($coords)."')";
  539. return $result = Database::query($sql);
  540. }
  541. /**
  542. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  543. * @desc Record information for common (or admin) events (in the track_e_default table)
  544. * @param string Type of event
  545. * @param string Type of value
  546. * @param string Value
  547. * @param string Timestamp (defaults to null)
  548. * @param integer User ID (defaults to null)
  549. * @param string Course code (defaults to null)
  550. */
  551. function event_system($event_type, $event_value_type, $event_value, $datetime = null, $user_id = null, $course_code = null)
  552. {
  553. global $TABLETRACK_DEFAULT;
  554. $event_type = Database::escape_string($event_type);
  555. $event_value_type = Database::escape_string($event_value_type);
  556. //Clean the user_info
  557. if ($event_value_type == LOG_USER_OBJECT) {
  558. if (is_array($event_value)) {
  559. unset($event_value['complete_name']);
  560. unset($event_value['complete_name_with_username']);
  561. unset($event_value['firstName']);
  562. unset($event_value['lastName']);
  563. unset($event_value['avatar_small']);
  564. unset($event_value['avatar_sys_path']);
  565. unset($event_value['avatar']);
  566. unset($event_value['mail']);
  567. unset($event_value['password']);
  568. unset($event_value['lastLogin']);
  569. unset($event_value['picture_uri']);
  570. $event_value = serialize($event_value);
  571. }
  572. }
  573. $event_value = Database::escape_string($event_value);
  574. $course_info = api_get_course_info($course_code);
  575. if (!empty($course_info)) {
  576. $course_id = $course_info['real_id'];
  577. $course_code = $course_info['code'];
  578. $course_code = Database::escape_string($course_code);
  579. } else {
  580. $course_id = null;
  581. $course_code = null;
  582. }
  583. if (!isset($datetime)) {
  584. $datetime = api_get_utc_datetime();
  585. }
  586. $datetime = Database::escape_string($datetime);
  587. if (!isset($user_id)) {
  588. $user_id = api_get_user_id();
  589. }
  590. $user_id = intval($user_id);
  591. $sql = "INSERT INTO $TABLETRACK_DEFAULT
  592. (default_user_id,
  593. default_cours_code,
  594. c_id,
  595. default_date,
  596. default_event_type,
  597. default_value_type,
  598. default_value
  599. )
  600. VALUES('$user_id.',
  601. '$course_code',
  602. '$course_id',
  603. '$datetime',
  604. '$event_type',
  605. '$event_value_type',
  606. '$event_value')";
  607. Database::query($sql);
  608. return true;
  609. }
  610. /**
  611. * Get every email stored in the database
  612. *
  613. * @param int $etId
  614. * @return type
  615. */
  616. function get_all_event_types()
  617. {
  618. global $event_config;
  619. $sql = 'SELECT etm.id, event_type_name, activated, language_id, message, subject, dokeos_folder
  620. FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' etm
  621. INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l
  622. ON etm.language_id = l.id';
  623. $events_types = Database::store_result(Database::query($sql), 'ASSOC');
  624. $to_return = array();
  625. foreach ($events_types as $et) {
  626. $et['nameLangVar'] = $event_config[$et["event_type_name"]]["name_lang_var"];
  627. $et['descLangVar'] = $event_config[$et["event_type_name"]]["desc_lang_var"];
  628. $to_return[] = $et;
  629. }
  630. return $to_return;
  631. }
  632. /**
  633. * Get users linked to an event
  634. *
  635. * @param int $etId
  636. * @return type
  637. */
  638. function get_users_subscribed_to_event($event_name)
  639. {
  640. $event_name = Database::escape_string($event_name);
  641. $sql = 'SELECT u.* FROM '.Database::get_main_table(TABLE_MAIN_USER).' u,'
  642. .Database::get_main_table(TABLE_MAIN_EVENT_TYPE).' e,'
  643. .Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' ue
  644. WHERE ue.user_id = u.user_id
  645. AND e.name = "'.$event_name.'"
  646. AND e.id = ue.event_type_id';
  647. $return = Database::store_result(Database::query($sql), 'ASSOC');
  648. return json_encode($return);
  649. }
  650. /**
  651. * Get the users related to one event
  652. *
  653. * @param string $event_name
  654. */
  655. function get_event_users($event_name)
  656. {
  657. $event_name = Database::escape_string($event_name);
  658. $sql = 'SELECT user.user_id, user.firstname, user.lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' user JOIN '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' relUser
  659. ON relUser.user_id = user.user_id
  660. WHERE user.status <> '.ANONYMOUS.' AND relUser.event_type_name = "'.$event_name.'"';
  661. //For tests
  662. //$sql = 'SELECT user.user_id, user.firstname, user.lastname FROM '.Database::get_main_table(TABLE_MAIN_USER);
  663. $user_list = Database::store_result(Database::query($sql), 'ASSOC');
  664. return json_encode($user_list);
  665. }
  666. function get_events_by_user_and_type($user_id, $event_type)
  667. {
  668. global $TABLETRACK_DEFAULT;
  669. $user_id = intval($user_id);
  670. $event_type = Database::escape_string($event_type);
  671. $sql = "SELECT * FROM $TABLETRACK_DEFAULT
  672. WHERE default_value_type = 'user_id' AND
  673. default_value = $user_id AND
  674. default_event_type = '$event_type'
  675. ORDER BY default_date ";
  676. $result = Database::query($sql);
  677. if ($result) {
  678. return Database::store_result($result, 'ASSOC');
  679. }
  680. return false;
  681. }
  682. function get_latest_event_by_user_and_type($user_id, $event_type)
  683. {
  684. $result = get_events_by_user_and_type($user_id, $event_type);
  685. if ($result && !empty($result)) {
  686. return $result[0];
  687. }
  688. }
  689. /**
  690. * Save the new message for one event and for one language
  691. *
  692. * @param string $eventName
  693. * @param array $users
  694. * @param string $message
  695. * @param string $subject
  696. * @param string $eventMessageLanguage
  697. * @param int $activated
  698. */
  699. function save_event_type_message($event_name, $users, $message, $subject, $event_message_language, $activated)
  700. {
  701. $event_name = Database::escape_string($event_name);
  702. $activated = intval($activated);
  703. $event_message_language = Database::escape_string($event_message_language);
  704. // Deletes then re-adds the users linked to the event
  705. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_name = "'.$event_name.'" ';
  706. Database::query($sql);
  707. foreach ($users as $user) {
  708. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_name) VALUES('.intval($user).',"'.$event_name.'")';
  709. Database::query($sql);
  710. }
  711. $language_id = api_get_language_id($event_message_language);
  712. // check if this template in this language already exists or not
  713. $sql = 'SELECT COUNT(id) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  714. WHERE event_type_name = "'.$event_name.'" AND language_id = '.$language_id;
  715. $sql = Database::store_result(Database::query($sql), 'ASSOC');
  716. // if already exists, we update
  717. if ($sql[0]["total"] > 0) {
  718. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  719. SET message = "'.Database::escape_string($message).'",
  720. subject = "'.Database::escape_string($subject).'",
  721. activated = '.$activated.'
  722. WHERE event_type_name = "'.$event_name.'" AND language_id = (SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  723. WHERE dokeos_folder = "'.$event_message_language.'")';
  724. Database::query($sql);
  725. } else { // else we create a new record
  726. // gets the language_-_id
  727. $lang_id = '(SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  728. WHERE dokeos_folder = "'.$event_message_language.'")';
  729. $lang_id = Database::store_result(Database::query($lang_id), 'ASSOC');
  730. if (!empty($lang_id[0]["id"])) {
  731. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' (event_type_name, language_id, message, subject, activated)
  732. VALUES("'.$event_name.'", '.$lang_id[0]["id"].', "'.Database::escape_string($message).'", "'.Database::escape_string($subject).'", '.$activated.')';
  733. Database::query($sql);
  734. }
  735. }
  736. // set activated at every save
  737. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  738. SET activated = '.$activated.'
  739. WHERE event_type_name = "'.$event_name.'"';
  740. Database::query($sql);
  741. }
  742. function eventType_mod($etId, $users, $message, $subject)
  743. {
  744. $etId = intval($etId);
  745. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_id = '.$etId.' ';
  746. Database::query($sql);
  747. foreach ($users as $user) {
  748. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_id)
  749. VALUES('.intval($user).','.$etId.') ';
  750. Database::query($sql);
  751. }
  752. $sql = 'UPDATE '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_MESSAGE).'
  753. SET message = "'.Database::escape_string($message).'",
  754. subject = "'.Database::escape_string($subject).'"
  755. WHERE event_type_id = '.$etId.'';
  756. Database::query($sql);
  757. }
  758. /**
  759. * Gets the last attempt of an exercise based in the exe_id
  760. */
  761. function get_last_attempt_date_of_exercise($exe_id)
  762. {
  763. $exe_id = intval($exe_id);
  764. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  765. $sql_track_attempt = 'SELECT max(tms) as last_attempt_date FROM '.$track_attempts.' WHERE exe_id='.$exe_id;
  766. $rs_last_attempt = Database::query($sql_track_attempt);
  767. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  768. $last_attempt_date = $row_last_attempt['last_attempt_date']; //Get the date of last attempt
  769. return $last_attempt_date;
  770. }
  771. /**
  772. * Gets how many attempts exists by user, exercise, learning path
  773. * @param int user id
  774. * @param int exercise id
  775. * @param int lp id
  776. * @param int lp item id
  777. * @param int lp item view id
  778. */
  779. function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id, $lp_item_view_id)
  780. {
  781. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  782. $user_id = intval($user_id);
  783. $exerciseId = intval($exerciseId);
  784. $lp_id = intval($lp_id);
  785. $lp_item_id = intval($lp_item_id);
  786. $lp_item_view_id = intval($lp_item_view_id);
  787. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  788. exe_exo_id = $exerciseId AND
  789. exe_user_id = $user_id AND
  790. status != 'incomplete' AND
  791. orig_lp_id = $lp_id AND
  792. orig_lp_item_id = $lp_item_id AND
  793. orig_lp_item_view_id = $lp_item_view_id AND
  794. exe_cours_id = '".api_get_course_id()."' AND
  795. session_id = '".api_get_session_id()."'";
  796. $query = Database::query($sql);
  797. if (Database::num_rows($query) > 0) {
  798. $attempt = Database :: fetch_array($query, 'ASSOC');
  799. return $attempt['count'];
  800. } else {
  801. return 0;
  802. }
  803. }
  804. function get_attempt_count_not_finished($user_id, $exerciseId, $lp_id, $lp_item_id)
  805. {
  806. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  807. $user_id = intval($user_id);
  808. $exerciseId = intval($exerciseId);
  809. $lp_id = intval($lp_id);
  810. $lp_item_id = intval($lp_item_id);
  811. $lp_item_view_id = intval($lp_item_view_id);
  812. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  813. exe_exo_id = $exerciseId AND
  814. exe_user_id = $user_id AND
  815. status != 'incomplete' AND
  816. orig_lp_id = $lp_id AND
  817. orig_lp_item_id = $lp_item_id AND
  818. exe_cours_id = '".api_get_course_id()."' AND
  819. session_id = '".api_get_session_id()."'";
  820. $query = Database::query($sql);
  821. if (Database::num_rows($query) > 0) {
  822. $attempt = Database :: fetch_array($query, 'ASSOC');
  823. return $attempt['count'];
  824. } else {
  825. return 0;
  826. }
  827. }
  828. function delete_student_lp_events($user_id, $lp_id, $course, $session_id)
  829. {
  830. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  831. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  832. $course_id = $course['real_id'];
  833. if (empty($course_id)) {
  834. $course_id = api_get_course_int_id();
  835. }
  836. $track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  837. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  838. $recording_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  839. $user_id = intval($user_id);
  840. $lp_id = intval($lp_id);
  841. $session_id = intval($session_id);
  842. //Make sure we have the exact lp_view_id
  843. $sql = "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 ";
  844. $result = Database::query($sql);
  845. if (Database::num_rows($result)) {
  846. $view = Database::fetch_array($result, 'ASSOC');
  847. $lp_view_id = $view['id'];
  848. $sql = "DELETE FROM $lp_item_view_table WHERE c_id = $course_id AND lp_view_id = $lp_view_id ";
  849. Database::query($sql);
  850. }
  851. $sql = "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 ";
  852. Database::query($sql);
  853. $sql = "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";
  854. $result = Database::query($sql);
  855. $exe_list = array();
  856. while ($row = Database::fetch_array($result, 'ASSOC')) {
  857. $exe_list[] = $row['exe_id'];
  858. }
  859. if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) {
  860. $sql_delete = "DELETE FROM $track_e_exercises WHERE exe_id IN (".implode(',', $exe_list).")";
  861. Database::query($sql_delete);
  862. $sql_delete = "DELETE FROM $track_attempts WHERE exe_id IN (".implode(',', $exe_list).")";
  863. Database::query($sql_delete);
  864. $sql_delete = "DELETE FROM $recording_table WHERE exe_id IN (".implode(',', $exe_list).")";
  865. Database::query($sql_delete);
  866. }
  867. }
  868. /**
  869. * Delete all exercise attempts (included in LP or not)
  870. *
  871. * @param int user id
  872. * @param int exercise id
  873. * @param string course code
  874. * @param int session id
  875. */
  876. function delete_all_incomplete_attempts($user_id, $exercise_id, $course_code, $session_id = 0)
  877. {
  878. $track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  879. $user_id = intval($user_id);
  880. $exercise_id = intval($exercise_id);
  881. $course_code = Database::escape_string($course_code);
  882. $session_id = intval($session_id);
  883. if (!empty($user_id) && !empty($exercise_id) && !empty($course_code)) {
  884. $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' ";
  885. Database::query($sql);
  886. }
  887. }
  888. /**
  889. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  890. * @param int exercise id
  891. * @param string course code
  892. * @param int session id
  893. * @return array with the results
  894. *
  895. */
  896. function get_all_exercise_results($exercise_id, $course_code, $session_id = 0, $load_question_list = true, $user_id = null)
  897. {
  898. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  899. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  900. $course_code = Database::escape_string($course_code);
  901. $exercise_id = intval($exercise_id);
  902. $session_id = intval($session_id);
  903. $user_condition = null;
  904. if (!empty($user_id)) {
  905. $user_id = intval($user_id);
  906. $user_condition = "AND exe_user_id = $user_id ";
  907. }
  908. $sql = "SELECT * FROM $TABLETRACK_EXERCICES
  909. WHERE status = '' AND
  910. exe_cours_id = '$course_code' AND
  911. exe_exo_id = '$exercise_id' AND
  912. session_id = $session_id AND
  913. orig_lp_id =0 AND
  914. orig_lp_item_id = 0
  915. $user_condition
  916. ORDER BY exe_id";
  917. $res = Database::query($sql);
  918. $list = array();
  919. while ($row = Database::fetch_array($res, 'ASSOC')) {
  920. $list[$row['exe_id']] = $row;
  921. if ($load_question_list) {
  922. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  923. $res_question = Database::query($sql);
  924. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  925. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  926. }
  927. }
  928. }
  929. return $list;
  930. }
  931. /**
  932. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  933. * @param string course code
  934. * @param int session id
  935. * @return array with the results
  936. *
  937. */
  938. function get_all_exercise_results_by_course($course_code, $session_id = 0, $get_count = true)
  939. {
  940. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  941. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  942. $course_code = Database::escape_string($course_code);
  943. $session_id = intval($session_id);
  944. $select = '*';
  945. if ($get_count) {
  946. $select = 'count(*) as count';
  947. }
  948. $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";
  949. $res = Database::query($sql);
  950. if ($get_count) {
  951. $row = Database::fetch_array($res, 'ASSOC');
  952. return $row['count'];
  953. } else {
  954. $list = array();
  955. while ($row = Database::fetch_array($res, 'ASSOC')) {
  956. $list[$row['exe_id']] = $row;
  957. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  958. $res_question = Database::query($sql);
  959. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  960. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  961. }
  962. }
  963. return $list;
  964. }
  965. }
  966. /**
  967. * Gets all exercise results (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 get_all_exercise_results_by_user($user_id, $course_code, $session_id = 0)
  975. {
  976. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  977. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  978. $course_code = Database::escape_string($course_code);
  979. $exercise_id = intval($exercise_id);
  980. $session_id = intval($session_id);
  981. $user_id = intval($user_id);
  982. $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";
  983. $res = Database::query($sql);
  984. $list = array();
  985. while ($row = Database::fetch_array($res, 'ASSOC')) {
  986. $list[$row['exe_id']] = $row;
  987. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  988. $res_question = Database::query($sql);
  989. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  990. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  991. }
  992. }
  993. //echo '<pre>'; print_r($list);
  994. return $list;
  995. }
  996. /**
  997. * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session
  998. * @param int exercise id
  999. * @param string course code
  1000. * @param int session id
  1001. * @return array with the results
  1002. *
  1003. */
  1004. function get_exercise_results_by_attempt($exe_id)
  1005. {
  1006. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1007. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1008. $table_track_attempt_recording = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1009. $exe_id = intval($exe_id);
  1010. $sql = "SELECT * FROM $table_track_exercises WHERE status = '' AND exe_id = $exe_id";
  1011. $res = Database::query($sql);
  1012. $list = array();
  1013. if (Database::num_rows($res)) {
  1014. $row = Database::fetch_array($res, 'ASSOC');
  1015. //Checking if this attempt was revised by a teacher
  1016. $sql_revised = 'SELECT exe_id FROM '.$table_track_attempt_recording.' WHERE author != "" AND exe_id = '.$exe_id.' LIMIT 1';
  1017. $res_revised = Database::query($sql_revised);
  1018. $row['attempt_revised'] = 0;
  1019. if (Database::num_rows($res_revised) > 0) {
  1020. $row['attempt_revised'] = 1;
  1021. }
  1022. $list[$exe_id] = $row;
  1023. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY tms ASC";
  1024. $res_question = Database::query($sql);
  1025. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1026. $list[$exe_id]['question_list'][$row_q['question_id']] = $row_q;
  1027. }
  1028. }
  1029. // echo '<pre>'; print_r($list); echo "</pre>";
  1030. return $list;
  1031. }
  1032. /**
  1033. * Gets exercise results (NO Exercises in LPs) from a given user, exercise id, course, session, lp_id, lp_item_id
  1034. * @param int user id
  1035. * @param int exercise id
  1036. * @param string course code
  1037. * @param int session id
  1038. * @param int lp id
  1039. * @param int lp item id
  1040. * @param string order asc or desc
  1041. * @return array with the results
  1042. *
  1043. */
  1044. function get_exercise_results_by_user($user_id, $exercise_id, $course_code, $session_id = 0, $lp_id = 0, $lp_item_id = 0, $order = null)
  1045. {
  1046. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1047. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1048. $table_track_attempt_recording = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1049. $course_code = Database::escape_string($course_code);
  1050. $exercise_id = intval($exercise_id);
  1051. $session_id = intval($session_id);
  1052. $user_id = intval($user_id);
  1053. $lp_id = intval($lp_id);
  1054. $lp_item_id = intval($lp_item_id);
  1055. if (!in_array(strtolower($order), array('asc', 'desc'))) {
  1056. $order = 'asc';
  1057. }
  1058. $sql = "SELECT * FROM $table_track_exercises
  1059. WHERE status = '' AND
  1060. exe_user_id = $user_id AND
  1061. exe_cours_id = '$course_code' AND
  1062. exe_exo_id = $exercise_id AND
  1063. session_id = $session_id AND
  1064. orig_lp_id = $lp_id AND
  1065. orig_lp_item_id = $lp_item_id
  1066. ORDER by exe_id $order ";
  1067. $res = Database::query($sql);
  1068. $list = array();
  1069. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1070. //Checking if this attempt was revised by a teacher
  1071. $sql_revised = 'SELECT exe_id FROM '.$table_track_attempt_recording.' WHERE author != "" AND exe_id = '.$row['exe_id'].' LIMIT 1';
  1072. $res_revised = Database::query($sql_revised);
  1073. $row['attempt_revised'] = 0;
  1074. if (Database::num_rows($res_revised) > 0) {
  1075. $row['attempt_revised'] = 1;
  1076. }
  1077. $list[$row['exe_id']] = $row;
  1078. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1079. $res_question = Database::query($sql);
  1080. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1081. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1082. }
  1083. }
  1084. return $list;
  1085. }
  1086. /**
  1087. * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session
  1088. * @param int exercise id
  1089. * @param string course code
  1090. * @param int session id
  1091. * @return array with the results
  1092. *
  1093. */
  1094. function count_exercise_attempts_by_user($user_id, $exercise_id, $course_code, $session_id = 0)
  1095. {
  1096. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1097. $course_code = Database::escape_string($course_code);
  1098. $exercise_id = intval($exercise_id);
  1099. $session_id = intval($session_id);
  1100. $user_id = intval($user_id);
  1101. $sql = "SELECT count(*) as count FROM $TABLETRACK_EXERCICES
  1102. 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";
  1103. $res = Database::query($sql);
  1104. $result = 0;
  1105. if (Database::num_rows($res) > 0) {
  1106. $row = Database::fetch_array($res, 'ASSOC');
  1107. $result = $row['count'];
  1108. }
  1109. return $result;
  1110. }
  1111. /**
  1112. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1113. * @param int exercise id
  1114. * @param string course code
  1115. * @param int session id
  1116. * @return array with the results
  1117. * @todo rename this function
  1118. *
  1119. */
  1120. function get_best_exercise_results_by_user($exercise_id, $course_code, $session_id = 0)
  1121. {
  1122. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1123. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1124. $course_code = Database::escape_string($course_code);
  1125. $exercise_id = intval($exercise_id);
  1126. $session_id = intval($session_id);
  1127. $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";
  1128. $res = Database::query($sql);
  1129. $list = array();
  1130. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1131. $list[$row['exe_id']] = $row;
  1132. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1133. $res_question = Database::query($sql);
  1134. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1135. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1136. }
  1137. }
  1138. //Getting the best results of every student
  1139. $best_score_return = array();
  1140. foreach ($list as $student_result) {
  1141. $user_id = $student_result['exe_user_id'];
  1142. $current_best_score[$user_id] = $student_result['exe_result'];
  1143. //echo $current_best_score[$user_id].' - '.$best_score_return[$user_id]['exe_result'].'<br />';
  1144. if ($current_best_score[$user_id] > $best_score_return[$user_id]['exe_result']) {
  1145. $best_score_return[$user_id] = $student_result;
  1146. }
  1147. }
  1148. return $best_score_return;
  1149. }
  1150. function get_best_attempt_exercise_results_per_user($user_id, $exercise_id, $course_code, $session_id = 0)
  1151. {
  1152. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1153. //$table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1154. $course_code = Database::escape_string($course_code);
  1155. $exercise_id = intval($exercise_id);
  1156. $session_id = intval($session_id);
  1157. $user_id = intval($user_id);
  1158. $sql = "SELECT * FROM $table_track_exercises
  1159. WHERE status = '' AND
  1160. exe_cours_id = '$course_code' AND
  1161. exe_exo_id = '$exercise_id' AND
  1162. session_id = $session_id AND
  1163. exe_user_id = $user_id AND
  1164. orig_lp_id =0 AND
  1165. orig_lp_item_id = 0
  1166. ORDER BY exe_id";
  1167. $res = Database::query($sql);
  1168. $list = array();
  1169. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1170. $list[$row['exe_id']] = $row; /*
  1171. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1172. $res_question = Database::query($sql);
  1173. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1174. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1175. } */
  1176. }
  1177. //Getting the best results of every student
  1178. $best_score_return = array();
  1179. $best_score_return['exe_result'] = 0;
  1180. foreach ($list as $result) {
  1181. $current_best_score = $result;
  1182. if ($current_best_score['exe_result'] > $best_score_return['exe_result']) {
  1183. $best_score_return = $result;
  1184. }
  1185. }
  1186. if (!isset($best_score_return['exe_weighting'])) {
  1187. $best_score_return = array();
  1188. }
  1189. return $best_score_return;
  1190. }
  1191. function count_exercise_result_not_validated($exercise_id, $course_code, $session_id = 0)
  1192. {
  1193. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1194. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1195. $course_code = Database::escape_string($course_code);
  1196. $session_id = intval($session_id);
  1197. $exercise_id = intval($exercise_id);
  1198. $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
  1199. WHERE exe_exo_id = $exercise_id AND
  1200. exe_cours_id = '$course_code' AND
  1201. e.session_id = $session_id AND
  1202. orig_lp_id = 0 AND
  1203. marks IS NULL AND
  1204. status = '' AND
  1205. orig_lp_item_id = 0 ORDER BY e.exe_id";
  1206. $res = Database::query($sql);
  1207. $row = Database::fetch_array($res, 'ASSOC');
  1208. return $row['count'];
  1209. }
  1210. /**
  1211. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1212. * @param int exercise id
  1213. * @param string course code
  1214. * @param int session id
  1215. * @return array with the results
  1216. *
  1217. */
  1218. function get_count_exercises_attempted_by_course($course_code, $session_id = 0)
  1219. {
  1220. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1221. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1222. $course_code = Database::escape_string($course_code);
  1223. $session_id = intval($session_id);
  1224. $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";
  1225. $res = Database::query($sql);
  1226. $count = 0;
  1227. if (Database::num_rows($res) > 0) {
  1228. $count = Database::num_rows($res);
  1229. }
  1230. return $count;
  1231. }
  1232. /**
  1233. * Gets all exercise events from a Learning Path within a Course nd Session
  1234. * @param int exercise id
  1235. * @param string course_code
  1236. * @param int session id
  1237. * @return array
  1238. */
  1239. function get_all_exercise_event_from_lp($exercise_id, $course_code, $session_id = 0)
  1240. {
  1241. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1242. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1243. $course_code = Database::escape_string($course_code);
  1244. $exercise_id = intval($exercise_id);
  1245. $session_id = intval($session_id);
  1246. $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";
  1247. $res = Database::query($sql);
  1248. $list = array();
  1249. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1250. $list[$row['exe_id']] = $row;
  1251. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1252. $res_question = Database::query($sql);
  1253. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1254. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1255. }
  1256. }
  1257. return $list;
  1258. }
  1259. function get_all_exercises_from_lp($lp_id, $course_id)
  1260. {
  1261. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  1262. $course_id = intval($course_id);
  1263. $lp_id = intval($lp_id);
  1264. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$lp_id."' ORDER BY parent_item_id, display_order";
  1265. $res = Database::query($sql);
  1266. $my_exercise_list = array();
  1267. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1268. if ($row['item_type'] == 'quiz') {
  1269. $my_exercise_list[] = $row;
  1270. }
  1271. }
  1272. return $my_exercise_list;
  1273. }
  1274. /**
  1275. * This function gets the comments of an exercise
  1276. *
  1277. * @param int $id
  1278. * @param int $question_id
  1279. * @return str the comment
  1280. */
  1281. function get_comments($exe_id, $question_id)
  1282. {
  1283. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1284. $sql = "SELECT teacher_comment FROM ".$table_track_attempt."
  1285. WHERE exe_id='".Database::escape_string($exe_id)."' AND question_id = '".Database::escape_string($question_id)."' ORDER by question_id";
  1286. $sqlres = Database::query($sql);
  1287. $comm = Database::result($sqlres, 0, "teacher_comment");
  1288. return $comm;
  1289. }
  1290. function get_all_exercise_event_by_exe_id($exe_id)
  1291. {
  1292. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1293. $exe_id = intval($exe_id);
  1294. $list = array();
  1295. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY position";
  1296. $res_question = Database::query($sql);
  1297. if (Database::num_rows($res_question))
  1298. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1299. $list[$row_q['question_id']][] = $row_q;
  1300. }
  1301. return $list;
  1302. }
  1303. function delete_attempt($exe_id, $user_id, $course_code, $session_id, $question_id)
  1304. {
  1305. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1306. $exe_id = intval($exe_id);
  1307. $user_id = intval($user_id);
  1308. $course_code = Database::escape_string($course_code);
  1309. $session_id = intval($session_id);
  1310. $question_id = intval($question_id);
  1311. $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 ";
  1312. Database::query($sql);
  1313. }
  1314. function delete_attempt_hotspot($exe_id, $user_id, $course_code, $question_id)
  1315. {
  1316. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1317. $exe_id = intval($exe_id);
  1318. $user_id = intval($user_id);
  1319. $course_code = Database::escape_string($course_code);
  1320. //$session_id = intval($session_id);
  1321. $question_id = intval($question_id);
  1322. $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 ";
  1323. Database::query($sql);
  1324. }
  1325. /**
  1326. * User logs in for the first time to a course
  1327. */
  1328. function event_course_login($course_code, $user_id, $session_id)
  1329. {
  1330. global $course_tracking_table;
  1331. //@todo use api_get_utc_datetime
  1332. $time = api_get_datetime();
  1333. $course_code = Database::escape_string($course_code);
  1334. $user_id = Database::escape_string($user_id);
  1335. $session_id = Database::escape_string($session_id);
  1336. $sql = "INSERT INTO $course_tracking_table(course_code, user_id, login_course_date, logout_course_date, counter, session_id)
  1337. VALUES('".$course_code."', '".$user_id."', '$time', '$time', '1', '".$session_id."')";
  1338. Database::query($sql);
  1339. //Course catalog stats modifications see #4191
  1340. CourseManager::update_course_ranking(null, null, null, null, true, false);
  1341. }
  1342. /**
  1343. * For the sake of genericity, this function is a switch.
  1344. * It's called by EventsDispatcher and fires the good function
  1345. * with the good require_once.
  1346. *
  1347. * @param string $event_name
  1348. * @param array $params
  1349. */
  1350. function event_send_mail($event_name, $params)
  1351. {
  1352. EventsMail::send_mail($event_name, $params);
  1353. }
  1354. /**
  1355. * Internal function checking if the mail was already sent from that user to that user
  1356. * @param string $event_name
  1357. * @param int $user_from
  1358. * @param int $user_to
  1359. * @return boolean
  1360. */
  1361. function check_if_mail_already_sent($event_name, $user_from, $user_to = null)
  1362. {
  1363. if ($user_to == null) {
  1364. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1365. WHERE user_from = '.$user_from.' AND event_type_name = "'.$event_name.'"';
  1366. } else {
  1367. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1368. WHERE user_from = '.$user_from.' AND user_to = '.$user_to.' AND event_type_name = "'.$event_name.'"';
  1369. }
  1370. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1371. return $result[0]["total"];
  1372. }
  1373. /*
  1374. *
  1375. * Filter EventEmailTemplate Filters see the main/inc/conf/events.conf.dist.php
  1376. *
  1377. */
  1378. /**
  1379. * Basic template event message filter (to be used by other filters as default)
  1380. * @param array $values (passing by reference)
  1381. * @return boolean True if everything is OK, false otherwise
  1382. */
  1383. function _event_send_mail_filter_func(&$values)
  1384. {
  1385. return true;
  1386. }
  1387. /**
  1388. * user_registration - send_mail filter
  1389. * @param array $values (passing by reference)
  1390. * @return boolean True if everything is OK, false otherwise
  1391. */
  1392. function user_registration_event_send_mail_filter_func(&$values)
  1393. {
  1394. $res = _event_send_mail_filter_func($values);
  1395. // proper logic for this filter
  1396. return $res;
  1397. }
  1398. /**
  1399. * portal_homepage_edited - send_mail filter
  1400. * @param array $values (passing by reference)
  1401. * @return boolean True if everything is OK, false otherwise
  1402. */
  1403. function portal_homepage_edited_event_send_mail_filter_func(&$values)
  1404. {
  1405. $res = _event_send_mail_filter_func($values);
  1406. // proper logic for this filter
  1407. return $res;
  1408. }
  1409. /**
  1410. *
  1411. */
  1412. /* End of filters */