events.lib.inc.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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. global $_configuration;
  32. global $TABLETRACK_OPEN;
  33. // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information
  34. // $_SERVER['HTTP_USER_AGENT'] : will provide browser and os information
  35. // $_SERVER['HTTP_REFERER'] : provide information about refering url
  36. if(isset($_SERVER['HTT_REFERER']))
  37. {
  38. $referer = Database::escape_string($_SERVER['HTTP_REFERER']);
  39. } else {
  40. $referer = '';
  41. }
  42. // record informations only if user comes from another site
  43. //if(!eregi($_configuration['root_web'],$referer))
  44. $pos = strpos($referer, $_configuration['root_web']);
  45. if ($pos === false && $referer != '') {
  46. $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. 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) VALUES
  73. ('".$_user['user_id']."',
  74. '".Database::escape_string(api_get_real_ip())."',
  75. '".$reallyNow."',
  76. '".$reallyNow."'
  77. )";
  78. Database::query($sql);
  79. // auto subscribe
  80. $user_status = $_user['status'];
  81. $user_status = $_user['status'] == SESSIONADMIN ? 'sessionadmin' :
  82. $_user['status'] == COURSEMANAGER ? 'teacher' :
  83. $_user['status'] == DRH ? 'DRH' : 'student';
  84. $autoSubscribe = api_get_setting($user_status.'_autosubscribe');
  85. if ($autoSubscribe) {
  86. $autoSubscribe = explode('|', $autoSubscribe);
  87. foreach ($autoSubscribe as $code) {
  88. if (CourseManager::course_exists($code)) {
  89. CourseManager::subscribe_user($_user['user_id'], $code);
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * @param tool name of the tool (name in mainDb.accueil table)
  96. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  97. * @desc Record information for access event for courses
  98. */
  99. function event_access_course() {
  100. global $TABLETRACK_ACCESS, $TABLETRACK_LASTACCESS;
  101. $id_session = api_get_session_id();
  102. $now = api_get_utc_datetime();
  103. $_cid = api_get_course_id();
  104. $user_id = api_get_user_id();
  105. if ($user_id) {
  106. $user_id = "'".$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. 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 = 0, $question_list = array(), $status = '', $remind_list = array() , $end_date = null) {
  304. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  305. global $debug;
  306. if ($debug) error_log('Called to update_event_exercice');
  307. if ($debug) error_log('duration:' . $duration);
  308. if ($exeid != '') {
  309. // Validation in case of fraud with actived control time
  310. if (!exercise_time_control_is_valid($exo_id, $learnpath_id, $learnpath_item_id)) {
  311. $score = 0;
  312. }
  313. if (!isset($status) || empty($status)) {
  314. $status = '';
  315. } else {
  316. $status = Database::escape_string($status);
  317. }
  318. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  319. if (!empty($question_list)) {
  320. $question_list = array_map('intval', $question_list);
  321. }
  322. if (!empty($remind_list)) {
  323. $remind_list = array_map('intval', $remind_list);
  324. $remind_list = array_filter($remind_list);
  325. $remind_list = implode(",", $remind_list);
  326. } else {
  327. $remind_list = '';
  328. }
  329. if (empty($end_date)) {
  330. $end_date = api_get_utc_datetime();
  331. }
  332. $sql = "UPDATE $TABLETRACK_EXERCICES SET
  333. exe_exo_id = '".Database::escape_string($exo_id)."',
  334. exe_result = '".Database::escape_string($score)."',
  335. exe_weighting = '".Database::escape_string($weighting)."',
  336. session_id = '".Database::escape_string($session_id)."',
  337. orig_lp_id = '".Database::escape_string($learnpath_id)."',
  338. orig_lp_item_id = '".Database::escape_string($learnpath_item_id)."',
  339. orig_lp_item_view_id = '".Database::escape_string($learnpath_item_view_id)."',
  340. exe_duration = '".Database::escape_string($duration)."',
  341. exe_date = '".$end_date."',
  342. status = '".$status."',
  343. questions_to_check = '".$remind_list."',
  344. data_tracking = '".implode(',', $question_list)."'
  345. WHERE exe_id = '".Database::escape_string($exeid)."'";
  346. $res = Database::query($sql);
  347. if ($debug) error_log('update_event_exercice called');
  348. if ($debug) error_log("$sql");
  349. //Deleting control time session track
  350. //exercise_time_control_delete($exo_id);
  351. return $res;
  352. } else {
  353. return false;
  354. }
  355. }
  356. /**
  357. * This function creates an empty Exercise in STATISTIC_TRACK_E_EXERCICES table.
  358. * After that in exercise_result.php we call the update_event_exercice() to update the exercise
  359. * @return $id the last id registered, or false on error
  360. * @author Julio Montoya <gugli100@gmail.com>
  361. * @desc Record result of user when an exercice was done
  362. * @deprecated this function seems to be deprecated
  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. $uid = api_get_user_id();
  370. $course_id = api_get_course_int_id();
  371. // First, check the exercise exists
  372. $sql_exe_id="SELECT exercises.id FROM $tbl_exe as exercises WHERE c_id = $course_id AND exercises.id=$exo_id";
  373. $res_exe_id=Database::query($sql_exe_id);
  374. if ($res_exe_id === false) { return false; } //sql error
  375. if (Database::num_rows($res_exe_id)<1) { return false;} //exe not found
  376. $row_exe_id=Database::fetch_row($res_exe_id);
  377. $exercise_id = intval($row_exe_id[0]);
  378. // Second, check if the record exists in the database (looking for incomplete records)
  379. $sql = "SELECT exe_id FROM $tbl_track_exe WHERE exe_exo_id = $exo_id AND " .
  380. "exe_user_id = $uid AND " .
  381. "exe_cours_id = '".api_get_course_id()."' AND " .
  382. "status = 'incomplete' AND ".
  383. "session_id = ".api_get_session_id();
  384. $res = Database::query($sql);
  385. if ($res === false) {return false;}
  386. if (Database::num_rows($res) > 0) {
  387. $row = Database::fetch_array($res);
  388. return $row['exe_id'];
  389. }
  390. // No record was found, so create one
  391. // get expire time to insert into the tracking record
  392. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  393. $current_expired_time_key = get_time_control_key($exercise_id);
  394. if (isset($_SESSION['expired_time'][$current_expired_time_key])) { //Only for exercice of type "One page"
  395. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  396. } else {
  397. $expired_date = '0000-00-00 00:00:00';
  398. }
  399. $sql = "INSERT INTO $tbl_track_exe ( exe_user_id, exe_cours_id, expired_time_control, exe_exo_id, session_id)
  400. VALUES ( $uid, '".api_get_course_id()."' ,'$expired_date','$exo_id','".api_get_session_id()."')";
  401. $res = Database::query($sql);
  402. $id= Database::insert_id();
  403. return $id;
  404. }
  405. /**
  406. * Record an event for this attempt at answering an exercise
  407. * @param float Score achieved
  408. * @param string Answer given
  409. * @param integer Question ID
  410. * @param integer Exercise ID
  411. * @param integer Position
  412. * @return boolean Result of the insert query
  413. */
  414. function exercise_attempt($score, $answer, $question_id, $exe_id, $position, $exercise_id = 0, $nano = null) {
  415. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  416. global $debug, $learnpath_id, $learnpath_item_id;
  417. $score = Database::escape_string($score);
  418. $answer = Database::escape_string($answer);
  419. $question_id = Database::escape_string($question_id);
  420. $exe_id = Database::escape_string($exe_id);
  421. $position = Database::escape_string($position);
  422. $now = api_get_utc_datetime();
  423. $user_id = api_get_user_id();
  424. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  425. if ($debug) error_log("----- entering exercise_attempt function ------");
  426. if ($debug) error_log("answer: $answer");
  427. if ($debug) error_log("score: $score");
  428. if ($debug) error_log("question_id : $question_id");
  429. if ($debug) error_log("position: $position");
  430. //Validation in case of fraud with actived control time
  431. if (!exercise_time_control_is_valid($exercise_id, $learnpath_id, $learnpath_item_id)) {
  432. if ($debug) error_log("exercise_time_control_is_valid is false");
  433. $score = 0;
  434. $answer = 0;
  435. }
  436. if (!empty($user_id)) {
  437. $user_id = "'".$user_id."'";
  438. } else {
  439. // anonymous
  440. $user_id = api_get_anonymous_id();
  441. }
  442. $file = '';
  443. if (isset($nano)) {
  444. $file = Database::escape_string(basename($nano->load_filename_if_exists(false)));
  445. }
  446. $sql = "INSERT INTO $TBL_TRACK_ATTEMPT (exe_id, user_id, question_id, answer, marks, course_code, session_id, position, tms, filename)
  447. VALUES (
  448. ".$exe_id.",
  449. ".$user_id.",
  450. '".$question_id."',
  451. '".$answer."',
  452. '".$score."',
  453. '".api_get_course_id()."',
  454. '".api_get_session_id()."',
  455. '".$position."',
  456. '".$now."',
  457. '".$file."'
  458. )";
  459. if ($debug) error_log("Saving question attempt: ");
  460. if ($debug) error_log($sql);
  461. if ($debug) error_log("");
  462. if (!empty($question_id) && !empty($exe_id) && !empty($user_id)) {
  463. $res = Database::query($sql);
  464. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  465. $recording_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  466. if ($debug) error_log("Saving e attempt recording ");
  467. $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()."') ";
  468. Database::query($recording_changes);
  469. }
  470. return $res;
  471. } else {
  472. return false;
  473. }
  474. }
  475. /**
  476. * Record an hotspot spot for this attempt at answering an hotspot question
  477. * @param int Exercise ID
  478. * @param int Question ID
  479. * @param int Answer ID
  480. * @param int Whether this answer is correct (1) or not (0)
  481. * @param string Coordinates of this point (e.g. 123;324)
  482. * @return boolean Result of the insert query
  483. * @uses Course code and user_id from global scope $_cid and $_user
  484. */
  485. function exercise_attempt_hotspot($exe_id, $question_id, $answer_id, $correct, $coords, $exerciseId = 0) {
  486. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  487. global $safe_lp_id, $safe_lp_item_id;
  488. //Validation in case of fraud with actived control time
  489. if (!exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
  490. $correct = 0;
  491. }
  492. $tbl_track_e_hotspot = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  493. $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)".
  494. " VALUES ('" . api_get_user_id() . "'," .
  495. " '" . api_get_course_id() . "', " .
  496. " '" . Database :: escape_string($exe_id) . "', " .
  497. " '" . Database :: escape_string($question_id) . "'," .
  498. " '" . Database :: escape_string($answer_id) . "'," .
  499. " '" . Database :: escape_string($correct) . "'," .
  500. " '" . Database :: escape_string($coords) . "')";
  501. return $result = Database::query($sql);
  502. }
  503. /**
  504. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  505. * @desc Record information for common (or admin) events (in the track_e_default table)
  506. * @param string Type of event
  507. * @param string Type of value
  508. * @param string Value
  509. * @param string Timestamp (defaults to null)
  510. * @param integer User ID (defaults to null)
  511. * @param string Course code (defaults to null)
  512. */
  513. function event_system($event_type, $event_value_type, $event_value, $datetime = null, $user_id = null, $course_code = null) {
  514. global $TABLETRACK_DEFAULT;
  515. $event_type = Database::escape_string($event_type);
  516. $event_value_type = Database::escape_string($event_value_type);
  517. //Clean the user_info
  518. if ($event_value_type == LOG_USER_OBJECT) {
  519. if (is_array($event_value)) {
  520. unset($event_value['complete_name']);
  521. unset($event_value['firstName']);
  522. unset($event_value['lastName']);
  523. unset($event_value['avatar_small']);
  524. unset($event_value['avatar']);
  525. unset($event_value['password']);
  526. unset($event_value['lastLogin']);
  527. unset($event_value['picture_uri']);
  528. $event_value = serialize($event_value);
  529. }
  530. }
  531. $event_value = Database::escape_string($event_value);
  532. $course_info = api_get_course_info($course_code);
  533. if (!empty($course_info)) {
  534. $course_id = $course_info['real_id'];
  535. $course_code = $course_info['code'];
  536. $course_code = Database::escape_string($course_code);
  537. } else {
  538. $course_id = null;
  539. $course_code = null;
  540. }
  541. if (!isset($datetime)) {
  542. $datetime = api_get_utc_datetime();
  543. }
  544. $datetime = Database::escape_string($datetime);
  545. if (!isset($user_id)) {
  546. $user_id = api_get_user_id();
  547. }
  548. $user_id = intval($user_id);
  549. $sql = "INSERT INTO $TABLETRACK_DEFAULT
  550. (default_user_id,
  551. default_cours_code,
  552. c_id,
  553. default_date,
  554. default_event_type,
  555. default_value_type,
  556. default_value
  557. )
  558. VALUES('$user_id.',
  559. '$course_code',
  560. '$course_id',
  561. '$datetime',
  562. '$event_type',
  563. '$event_value_type',
  564. '$event_value')";
  565. Database::query($sql);
  566. return true;
  567. }
  568. /**
  569. * Get every email stored in the database
  570. *
  571. * @param int $etId
  572. * @return type
  573. */
  574. function get_all_event_types() {
  575. global $event_config;
  576. $sql = 'SELECT etm.id, event_type_name, activated, language_id, message, subject, dokeos_folder
  577. FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' etm
  578. INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l
  579. ON etm.language_id = l.id';
  580. $events_types = Database::store_result(Database::query($sql),'ASSOC');
  581. $to_return = array();
  582. foreach ($events_types as $et) {
  583. $et['nameLangVar'] = $event_config[$et["event_type_name"]]["name_lang_var"];
  584. $et['descLangVar'] = $event_config[$et["event_type_name"]]["desc_lang_var"];
  585. $to_return[] = $et;
  586. }
  587. return $to_return;
  588. }
  589. /**
  590. * Get users linked to an event
  591. *
  592. * @param int $etId
  593. * @return type
  594. */
  595. function get_users_subscribed_to_event($event_name) {
  596. $event_name = Database::escape_string($event_name);
  597. $sql = 'SELECT u.* FROM '. Database::get_main_table(TABLE_MAIN_USER).' u,'
  598. .Database::get_main_table(TABLE_MAIN_EVENT_TYPE).' e,'
  599. .Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' ue
  600. WHERE ue.user_id = u.user_id
  601. AND e.name = "'.$event_name.'"
  602. AND e.id = ue.event_type_id';
  603. $return = Database::store_result(Database::query($sql),'ASSOC');
  604. return json_encode($return);
  605. }
  606. /**
  607. * Get the users related to one event
  608. *
  609. * @param string $event_name
  610. */
  611. function get_event_users($event_name) {
  612. $event_name = Database::escape_string($event_name);
  613. $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
  614. ON relUser.user_id = user.user_id
  615. WHERE user.status <> '.ANONYMOUS.' AND relUser.event_type_name = "'.$event_name.'"';
  616. //For tests
  617. //$sql = 'SELECT user.user_id, user.firstname, user.lastname FROM '.Database::get_main_table(TABLE_MAIN_USER);
  618. $user_list = Database::store_result(Database::query($sql), 'ASSOC');
  619. return json_encode($user_list);
  620. }
  621. /**
  622. * Save the new message for one event and for one language
  623. *
  624. * @param string $eventName
  625. * @param array $users
  626. * @param string $message
  627. * @param string $subject
  628. * @param string $eventMessageLanguage
  629. * @param int $activated
  630. */
  631. function save_event_type_message($event_name, $users, $message, $subject, $event_message_language, $activated) {
  632. $event_name = Database::escape_string($event_name);
  633. $activated = intval($activated);
  634. $event_message_language = Database::escape_string($event_message_language);
  635. // Deletes then re-adds the users linked to the event
  636. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_name = "'.$event_name.'" ';
  637. Database::query($sql);
  638. foreach ($users as $user) {
  639. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_name) VALUES('.intval($user).',"'. $event_name.'")';
  640. Database::query($sql);
  641. }
  642. $language_id = api_get_language_id($event_message_language);
  643. // check if this template in this language already exists or not
  644. $sql = 'SELECT COUNT(id) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  645. WHERE event_type_name = "'.$event_name.'" AND language_id = '.$language_id;
  646. $sql = Database::store_result(Database::query($sql),'ASSOC');
  647. // if already exists, we update
  648. if ($sql[0]["total"] > 0) {
  649. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  650. SET message = "'.Database::escape_string($message).'",
  651. subject = "'.Database::escape_string($subject).'",
  652. activated = '.$activated.'
  653. WHERE event_type_name = "'.$event_name.'" AND language_id = (SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  654. WHERE dokeos_folder = "'.$event_message_language.'")';
  655. Database::query($sql);
  656. } else { // else we create a new record
  657. // gets the language_-_id
  658. $lang_id = '(SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  659. WHERE dokeos_folder = "'.$event_message_language.'")';
  660. $lang_id = Database::store_result(Database::query($lang_id),'ASSOC');
  661. if (!empty($lang_id[0]["id"])) {
  662. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' (event_type_name, language_id, message, subject, activated)
  663. VALUES("'.$event_name.'", '.$lang_id[0]["id"].', "'.Database::escape_string($message).'", "'.Database::escape_string($subject).'", '.$activated.')';
  664. Database::query($sql);
  665. }
  666. }
  667. // set activated at every save
  668. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  669. SET activated = '.$activated.'
  670. WHERE event_type_name = "'.$event_name.'"';
  671. Database::query($sql);
  672. }
  673. function eventType_mod($etId, $users, $message, $subject) {
  674. $etId = intval($etId);
  675. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_id = '.$etId.' ';
  676. Database::query($sql);
  677. foreach ($users as $user) {
  678. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_id)
  679. VALUES('.intval($user).','.$etId.') ';
  680. Database::query($sql);
  681. }
  682. $sql = 'UPDATE '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_MESSAGE).'
  683. SET message = "'.Database::escape_string($message).'",
  684. subject = "'.Database::escape_string($subject).'"
  685. WHERE event_type_id = '.$etId.'';
  686. Database::query($sql);
  687. }
  688. /**
  689. * Gets the last attempt of an exercise based in the exe_id
  690. */
  691. function get_last_attempt_date_of_exercise($exe_id) {
  692. $exe_id = intval($exe_id);
  693. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  694. $sql_track_attempt = 'SELECT max(tms) as last_attempt_date FROM '.$track_attempts.' WHERE exe_id='.$exe_id;
  695. $rs_last_attempt = Database::query($sql_track_attempt);
  696. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  697. $last_attempt_date = $row_last_attempt['last_attempt_date'];//Get the date of last attempt
  698. return $last_attempt_date;
  699. }
  700. /**
  701. * Gets how many attempts exists by user, exercise, learning path
  702. * @param int user id
  703. * @param int exercise id
  704. * @param int lp id
  705. * @param int lp item id
  706. * @param int lp item view id
  707. */
  708. function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id,$lp_item_view_id) {
  709. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  710. $user_id = intval($user_id);
  711. $exerciseId = intval($exerciseId);
  712. $lp_id = intval($lp_id);
  713. $lp_item_id = intval($lp_item_id);
  714. $lp_item_view_id= intval($lp_item_view_id);
  715. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  716. exe_exo_id = $exerciseId AND
  717. exe_user_id = $user_id AND
  718. status != 'incomplete' AND
  719. orig_lp_id = $lp_id AND
  720. orig_lp_item_id = $lp_item_id AND
  721. orig_lp_item_view_id = $lp_item_view_id AND
  722. exe_cours_id = '".api_get_course_id()."' AND
  723. session_id = '" . api_get_session_id() . "'";
  724. $query = Database::query($sql);
  725. if (Database::num_rows($query) > 0 ) {
  726. $attempt = Database :: fetch_array($query,'ASSOC');
  727. return $attempt['count'];
  728. } else {
  729. return 0;
  730. }
  731. }
  732. function get_attempt_count_not_finished($user_id, $exerciseId, $lp_id, $lp_item_id) {
  733. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  734. $user_id = intval($user_id);
  735. $exerciseId = intval($exerciseId);
  736. $lp_id = intval($lp_id);
  737. $lp_item_id = intval($lp_item_id);
  738. $lp_item_view_id= intval($lp_item_view_id);
  739. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  740. exe_exo_id = $exerciseId AND
  741. exe_user_id = $user_id AND
  742. status != 'incomplete' AND
  743. orig_lp_id = $lp_id AND
  744. orig_lp_item_id = $lp_item_id AND
  745. exe_cours_id = '".api_get_course_id()."' AND
  746. session_id = '" . api_get_session_id() . "'";
  747. $query = Database::query($sql);
  748. if (Database::num_rows($query) > 0 ) {
  749. $attempt = Database :: fetch_array($query,'ASSOC');
  750. return $attempt['count'];
  751. } else {
  752. return 0;
  753. }
  754. }
  755. function delete_student_lp_events($user_id, $lp_id, $course, $session_id) {
  756. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  757. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  758. $course_id = $course['real_id'];
  759. if (empty($course_id)) {
  760. $course_id = api_get_course_int_id();
  761. }
  762. $track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  763. $track_attempts = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  764. $recording_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  765. $user_id = intval($user_id);
  766. $lp_id = intval($lp_id);
  767. $session_id = intval($session_id);
  768. //Make sure we have the exact lp_view_id
  769. $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 ";
  770. $result = Database::query($sql);
  771. if (Database::num_rows($result)) {
  772. $view = Database::fetch_array($result, 'ASSOC');
  773. $lp_view_id = $view['id'];
  774. $sql = "DELETE FROM $lp_item_view_table WHERE c_id = $course_id AND lp_view_id = $lp_view_id ";
  775. Database::query($sql);
  776. }
  777. $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 ";
  778. Database::query($sql);
  779. $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";
  780. $result = Database::query($sql);
  781. $exe_list = array();
  782. while ($row = Database::fetch_array($result, 'ASSOC')) {
  783. $exe_list[] = $row['exe_id'];
  784. }
  785. if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) {
  786. $sql_delete = "DELETE FROM $track_e_exercises WHERE exe_id IN (".implode(',',$exe_list).")";
  787. Database::query($sql_delete);
  788. $sql_delete = "DELETE FROM $track_attempts WHERE exe_id IN (".implode(',',$exe_list).")";
  789. Database::query($sql_delete);
  790. $sql_delete = "DELETE FROM $recording_table WHERE exe_id IN (".implode(',',$exe_list).")";
  791. Database::query($sql_delete);
  792. }
  793. }
  794. /**
  795. * Delete all exercise attempts (included in LP or not)
  796. *
  797. * @param int user id
  798. * @param int exercise id
  799. * @param string course code
  800. * @param int session id
  801. */
  802. function delete_all_incomplete_attempts($user_id, $exercise_id, $course_code, $session_id = 0) {
  803. $track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  804. $user_id = intval($user_id);
  805. $exercise_id = intval($exercise_id);
  806. $course_code = Database::escape_string($course_code);
  807. $session_id = intval($session_id);
  808. if (!empty($user_id) && !empty($exercise_id) && !empty($course_code)) {
  809. $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' ";
  810. Database::query($sql);
  811. }
  812. }
  813. /**
  814. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  815. * @param int exercise id
  816. * @param string course code
  817. * @param int session id
  818. * @return array with the results
  819. *
  820. */
  821. function get_all_exercise_results($exercise_id, $course_code, $session_id = 0, $load_question_list = true) {
  822. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  823. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  824. $course_code = Database::escape_string($course_code);
  825. $exercise_id = intval($exercise_id);
  826. $session_id = intval($session_id);
  827. $sql = "SELECT * FROM $TABLETRACK_EXERCICES
  828. 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";
  829. $res = Database::query($sql);
  830. $list = array();
  831. while($row = Database::fetch_array($res,'ASSOC')) {
  832. $list[$row['exe_id']] = $row;
  833. if ($load_question_list) {
  834. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  835. $res_question = Database::query($sql);
  836. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  837. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  838. }
  839. }
  840. }
  841. return $list;
  842. }
  843. /**
  844. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  845. * @param string course code
  846. * @param int session id
  847. * @return array with the results
  848. *
  849. */
  850. function get_all_exercise_results_by_course($course_code, $session_id = 0, $get_count = true) {
  851. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  852. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  853. $course_code = Database::escape_string($course_code);
  854. $session_id = intval($session_id);
  855. $select = '*';
  856. if ($get_count) {
  857. $select = 'count(*) as count';
  858. }
  859. $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";
  860. $res = Database::query($sql);
  861. if ($get_count) {
  862. $row = Database::fetch_array($res,'ASSOC');
  863. return $row['count'];
  864. } else {
  865. $list = array();
  866. while($row = Database::fetch_array($res,'ASSOC')) {
  867. $list[$row['exe_id']] = $row;
  868. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  869. $res_question = Database::query($sql);
  870. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  871. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  872. }
  873. }
  874. return $list;
  875. }
  876. }
  877. /**
  878. * Gets all exercise results (NO Exercises in LPs) from a given exercise id, course, session
  879. * @param int exercise id
  880. * @param string course code
  881. * @param int session id
  882. * @return array with the results
  883. *
  884. */
  885. function get_all_exercise_results_by_user($user_id, $course_code, $session_id = 0) {
  886. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  887. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  888. $course_code = Database::escape_string($course_code);
  889. $exercise_id = intval($exercise_id);
  890. $session_id = intval($session_id);
  891. $user_id = intval($user_id);
  892. $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";
  893. $res = Database::query($sql);
  894. $list = array();
  895. while($row = Database::fetch_array($res,'ASSOC')) {
  896. $list[$row['exe_id']] = $row;
  897. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  898. $res_question = Database::query($sql);
  899. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  900. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  901. }
  902. }
  903. //echo '<pre>'; print_r($list);
  904. return $list;
  905. }
  906. /**
  907. * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session
  908. * @param int exercise id
  909. * @param string course code
  910. * @param int session id
  911. * @return array with the results
  912. *
  913. */
  914. function get_exercise_results_by_attempt($exe_id) {
  915. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  916. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  917. $table_track_attempt_recording = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  918. $exe_id = intval($exe_id);
  919. $sql = "SELECT * FROM $table_track_exercises WHERE status = '' AND exe_id = $exe_id";
  920. $res = Database::query($sql);
  921. $list = array();
  922. if (Database::num_rows($res)) {
  923. $row = Database::fetch_array($res,'ASSOC');
  924. //Checking if this attempt was revised by a teacher
  925. $sql_revised = 'SELECT exe_id FROM ' . $table_track_attempt_recording . ' WHERE author != "" AND exe_id = '.$exe_id.' LIMIT 1';
  926. $res_revised = Database::query($sql_revised);
  927. $row['attempt_revised'] = 0;
  928. if (Database::num_rows($res_revised) > 0) {
  929. $row['attempt_revised'] = 1;
  930. }
  931. $list[$exe_id] = $row;
  932. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY tms ASC";
  933. $res_question = Database::query($sql);
  934. while ($row_q = Database::fetch_array($res_question,'ASSOC')) {
  935. $list[$exe_id]['question_list'][$row_q['question_id']] = $row_q;
  936. }
  937. }
  938. // echo '<pre>'; print_r($list); echo "</pre>";
  939. return $list;
  940. }
  941. /**
  942. * Gets exercise results (NO Exercises in LPs) from a given user, exercise id, course, session, lp_id, lp_item_id
  943. * @param int user id
  944. * @param int exercise id
  945. * @param string course code
  946. * @param int session id
  947. * @param int lp id
  948. * @param int lp item id
  949. * @param string order asc or desc
  950. * @return array with the results
  951. *
  952. */
  953. function get_exercise_results_by_user($user_id, $exercise_id, $course_code, $session_id = 0, $lp_id = 0, $lp_item_id = 0, $order = null) {
  954. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  955. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  956. $table_track_attempt_recording = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  957. $course_code = Database::escape_string($course_code);
  958. $exercise_id = intval($exercise_id);
  959. $session_id = intval($session_id);
  960. $user_id = intval($user_id);
  961. $lp_id = intval($lp_id);
  962. $lp_item_id = intval($lp_item_id);
  963. if (!in_array(strtolower($order), array('asc', 'desc'))) {
  964. $order = 'asc';
  965. }
  966. $sql = "SELECT * FROM $table_track_exercises
  967. WHERE status = '' AND
  968. exe_user_id = $user_id AND
  969. exe_cours_id = '$course_code' AND
  970. exe_exo_id = $exercise_id AND
  971. session_id = $session_id AND
  972. orig_lp_id = $lp_id AND
  973. orig_lp_item_id = $lp_item_id
  974. ORDER by exe_id $order ";
  975. $res = Database::query($sql);
  976. $list = array();
  977. while($row = Database::fetch_array($res,'ASSOC')) {
  978. //Checking if this attempt was revised by a teacher
  979. $sql_revised = 'SELECT exe_id FROM ' . $table_track_attempt_recording . ' WHERE author != "" AND exe_id = '.$row['exe_id'].' LIMIT 1';
  980. $res_revised = Database::query($sql_revised);
  981. $row['attempt_revised'] = 0;
  982. if (Database::num_rows($res_revised) > 0) {
  983. $row['attempt_revised'] = 1;
  984. }
  985. $list[$row['exe_id']] = $row;
  986. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  987. $res_question = Database::query($sql);
  988. while ($row_q = Database::fetch_array($res_question,'ASSOC')) {
  989. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  990. }
  991. }
  992. return $list;
  993. }
  994. /**
  995. * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session
  996. * @param int exercise id
  997. * @param string course code
  998. * @param int session id
  999. * @return array with the results
  1000. *
  1001. */
  1002. function count_exercise_attempts_by_user($user_id, $exercise_id, $course_code, $session_id = 0) {
  1003. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1004. $course_code = Database::escape_string($course_code);
  1005. $exercise_id = intval($exercise_id);
  1006. $session_id = intval($session_id);
  1007. $user_id = intval($user_id);
  1008. $sql = "SELECT count(*) as count FROM $TABLETRACK_EXERCICES
  1009. 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";
  1010. $res = Database::query($sql);
  1011. $result = 0;
  1012. if (Database::num_rows($res) > 0 ) {
  1013. $row = Database::fetch_array($res,'ASSOC');
  1014. $result = $row['count'];
  1015. }
  1016. return $result;
  1017. }
  1018. /**
  1019. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1020. * @param int exercise id
  1021. * @param string course code
  1022. * @param int session id
  1023. * @return array with the results
  1024. * @todo rename this function
  1025. *
  1026. */
  1027. function get_best_exercise_results_by_user($exercise_id, $course_code, $session_id = 0) {
  1028. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1029. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1030. $course_code = Database::escape_string($course_code);
  1031. $exercise_id = intval($exercise_id);
  1032. $session_id = intval($session_id);
  1033. $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";
  1034. $res = Database::query($sql);
  1035. $list = array();
  1036. while($row = Database::fetch_array($res,'ASSOC')) {
  1037. $list[$row['exe_id']] = $row;
  1038. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1039. $res_question = Database::query($sql);
  1040. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1041. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1042. }
  1043. }
  1044. //Getting the best results of every student
  1045. $best_score_return = array();
  1046. foreach($list as $student_result) {
  1047. $user_id = $student_result['exe_user_id'];
  1048. $current_best_score[$user_id] = $student_result['exe_result'];
  1049. //echo $current_best_score[$user_id].' - '.$best_score_return[$user_id]['exe_result'].'<br />';
  1050. if ($current_best_score[$user_id] > $best_score_return[$user_id]['exe_result']) {
  1051. $best_score_return[$user_id] = $student_result;
  1052. }
  1053. }
  1054. return $best_score_return;
  1055. }
  1056. function get_best_attempt_exercise_results_per_user($user_id, $exercise_id, $course_code, $session_id = 0) {
  1057. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1058. //$table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1059. $course_code = Database::escape_string($course_code);
  1060. $exercise_id = intval($exercise_id);
  1061. $session_id = intval($session_id);
  1062. $user_id = intval($user_id);
  1063. $sql = "SELECT * FROM $table_track_exercises
  1064. WHERE status = '' AND
  1065. exe_cours_id = '$course_code' AND
  1066. exe_exo_id = '$exercise_id' AND
  1067. session_id = $session_id AND
  1068. exe_user_id = $user_id AND
  1069. orig_lp_id =0 AND
  1070. orig_lp_item_id = 0
  1071. ORDER BY exe_id";
  1072. $res = Database::query($sql);
  1073. $list = array();
  1074. while($row = Database::fetch_array($res,'ASSOC')) {
  1075. $list[$row['exe_id']] = $row; /*
  1076. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1077. $res_question = Database::query($sql);
  1078. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1079. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1080. } */
  1081. }
  1082. //Getting the best results of every student
  1083. $best_score_return = array();
  1084. $best_score_return['exe_result'] = 0;
  1085. foreach($list as $result) {
  1086. $current_best_score = $result;
  1087. if ($current_best_score['exe_result'] > $best_score_return['exe_result']) {
  1088. $best_score_return = $result;
  1089. }
  1090. }
  1091. if (!isset($best_score_return['exe_weighting'])) {
  1092. $best_score_return = array();
  1093. }
  1094. return $best_score_return;
  1095. }
  1096. function count_exercise_result_not_validated($exercise_id, $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_RECORDING);
  1099. $course_code = Database::escape_string($course_code);
  1100. $session_id = intval($session_id);
  1101. $exercise_id = intval($exercise_id);
  1102. $status = Database::escape_string($status);
  1103. $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
  1104. WHERE exe_exo_id = $exercise_id AND
  1105. exe_cours_id = '$course_code' AND
  1106. e.session_id = $session_id AND
  1107. orig_lp_id = 0 AND
  1108. marks IS NULL AND
  1109. status = '' AND
  1110. orig_lp_item_id = 0 ORDER BY e.exe_id";
  1111. $res = Database::query($sql);
  1112. $row = Database::fetch_array($res,'ASSOC');
  1113. return $row['count'];
  1114. }
  1115. /**
  1116. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1117. * @param int exercise id
  1118. * @param string course code
  1119. * @param int session id
  1120. * @return array with the results
  1121. *
  1122. */
  1123. function get_count_exercises_attempted_by_course($course_code, $session_id = 0) {
  1124. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1125. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1126. $course_code = Database::escape_string($course_code);
  1127. $session_id = intval($session_id);
  1128. $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";
  1129. $res = Database::query($sql);
  1130. $count = 0;
  1131. if (Database::num_rows($res) > 0) {
  1132. $count = Database::num_rows($res);
  1133. }
  1134. return $count;
  1135. }
  1136. /**
  1137. * Gets all exercise events from a Learning Path within a Course nd Session
  1138. * @param int exercise id
  1139. * @param string course_code
  1140. * @param int session id
  1141. * @return array
  1142. */
  1143. function get_all_exercise_event_from_lp($exercise_id, $course_code, $session_id = 0) {
  1144. $table_track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1145. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1146. $course_code = Database::escape_string($course_code);
  1147. $exercise_id = intval($exercise_id);
  1148. $session_id = intval($session_id);
  1149. $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";
  1150. $res = Database::query($sql);
  1151. $list = array();
  1152. while($row = Database::fetch_array($res,'ASSOC')) {
  1153. $list[$row['exe_id']] = $row;
  1154. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1155. $res_question = Database::query($sql);
  1156. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1157. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1158. }
  1159. }
  1160. return $list;
  1161. }
  1162. function get_all_exercises_from_lp($lp_id, $course_id) {
  1163. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  1164. $course_id = intval($course_id);
  1165. $lp_id = intval($lp_id);
  1166. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$lp_id."' ORDER BY parent_item_id, display_order";
  1167. $res = Database::query($sql);
  1168. $my_exercise_list = array();
  1169. while($row = Database::fetch_array($res,'ASSOC')) {
  1170. if ($row['item_type'] == 'quiz') {
  1171. $my_exercise_list[] = $row;
  1172. }
  1173. }
  1174. return $my_exercise_list;
  1175. }
  1176. /**
  1177. * This function gets the comments of an exercise
  1178. *
  1179. * @param int $id
  1180. * @param int $question_id
  1181. * @return str the comment
  1182. */
  1183. function get_comments($id,$question_id) {
  1184. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1185. $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";
  1186. $sqlres = Database::query($sql);
  1187. $comm = Database::result($sqlres,0,"teacher_comment");
  1188. return $comm;
  1189. }
  1190. function get_all_exercise_event_by_exe_id($exe_id) {
  1191. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1192. $exe_id = intval($exe_id);
  1193. $list = array();
  1194. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY position";
  1195. $res_question = Database::query($sql);
  1196. if (Database::num_rows($res_question))
  1197. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1198. $list[$row_q['question_id']][] = $row_q;
  1199. }
  1200. return $list;
  1201. }
  1202. function delete_attempt($exe_id, $user_id, $course_code, $session_id, $question_id) {
  1203. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1204. $exe_id = intval($exe_id);
  1205. $user_id = intval($user_id);
  1206. $course_code = Database::escape_string($course_code);
  1207. $session_id = intval($session_id);
  1208. $question_id = intval($question_id);
  1209. $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 ";
  1210. Database::query($sql);
  1211. }
  1212. function delete_attempt_hotspot($exe_id, $user_id, $course_code, $question_id) {
  1213. $table_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1214. $exe_id = intval($exe_id);
  1215. $user_id = intval($user_id);
  1216. $course_code = Database::escape_string($course_code);
  1217. //$session_id = intval($session_id);
  1218. $question_id = intval($question_id);
  1219. $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 ";
  1220. Database::query($sql);
  1221. }
  1222. /**
  1223. * User logs in for the first time to a course
  1224. */
  1225. function event_course_login($course_code, $user_id, $session_id) {
  1226. global $course_tracking_table;
  1227. //@todo use api_get_utc_datetime
  1228. $time = api_get_datetime();
  1229. $course_code = Database::escape_string($course_code);
  1230. $user_id = Database::escape_string($user_id);
  1231. $session_id = Database::escape_string($session_id);
  1232. $sql = "INSERT INTO $course_tracking_table(course_code, user_id, login_course_date, logout_course_date, counter, session_id)
  1233. VALUES('".$course_code."', '".$user_id."', '$time', '$time', '1', '".$session_id."')";
  1234. Database::query($sql);
  1235. //Course catalog stats modifications see #4191
  1236. CourseManager::update_course_ranking(null, null, null, null, true, false);
  1237. }
  1238. /**
  1239. * For the sake of genericity, this function is a switch.
  1240. * It's called by EventsDispatcher and fires the good function
  1241. * with the good require_once.
  1242. *
  1243. * @param string $event_name
  1244. * @param array $params
  1245. */
  1246. function event_send_mail($event_name, $params) {
  1247. EventsMail::send_mail($event_name, $params);
  1248. }
  1249. /**
  1250. * Internal function checking if the mail was already sent from that user to that user
  1251. * @param string $event_name
  1252. * @param int $user_from
  1253. * @param int $user_to
  1254. * @return boolean
  1255. */
  1256. function check_if_mail_already_sent($event_name, $user_from, $user_to = null) {
  1257. if ($user_to == null) {
  1258. $sql = 'SELECT COUNT(*) as total FROM ' . Database::get_main_table(TABLE_EVENT_SENT) . '
  1259. WHERE user_from = '.$user_from.' AND event_type_name = "'.$event_name.'"';
  1260. } else {
  1261. $sql = 'SELECT COUNT(*) as total FROM ' . Database::get_main_table(TABLE_EVENT_SENT) . '
  1262. WHERE user_from = '.$user_from.' AND user_to = '.$user_to.' AND event_type_name = "'.$event_name.'"';
  1263. }
  1264. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1265. return $result[0]["total"];
  1266. }
  1267. /*
  1268. *
  1269. * Filter EventEmailTemplate Filters see the main/inc/conf/events.conf.dist.php
  1270. *
  1271. */
  1272. /**
  1273. * Basic template event message filter (to be used by other filters as default)
  1274. * @param array $values (passing by reference)
  1275. * @return boolean True if everything is OK, false otherwise
  1276. */
  1277. function _event_send_mail_filter_func(&$values) {
  1278. return true;
  1279. }
  1280. /**
  1281. * user_registration - send_mail filter
  1282. * @param array $values (passing by reference)
  1283. * @return boolean True if everything is OK, false otherwise
  1284. */
  1285. function user_registration_event_send_mail_filter_func(&$values) {
  1286. $res = _event_send_mail_filter_func($values);
  1287. // proper logic for this filter
  1288. return $res;
  1289. }
  1290. /**
  1291. * portal_homepage_edited - send_mail filter
  1292. * @param array $values (passing by reference)
  1293. * @return boolean True if everything is OK, false otherwise
  1294. */
  1295. function portal_homepage_edited_event_send_mail_filter_func(&$values) {
  1296. $res = _event_send_mail_filter_func($values);
  1297. // proper logic for this filter
  1298. return $res;
  1299. }
  1300. /**
  1301. *
  1302. */
  1303. /* End of filters */