events.lib.php 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  1. <?php
  2. /* See license terms in /license.txt */
  3. //use Chamilo\UserBundle\Entity\User;
  4. /**
  5. * Class Event
  6. * Functions of this library are used to record informations when some kind
  7. * of event occur. Each event has his own types of informations then each event
  8. * use its own function.
  9. */
  10. class Event
  11. {
  12. /**
  13. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  14. * @desc Record information for open event (when homepage is opened)
  15. */
  16. public static function event_open()
  17. {
  18. global $_configuration;
  19. global $TABLETRACK_OPEN;
  20. // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information
  21. // $_SERVER['HTTP_USER_AGENT'] : will provide browser and os information
  22. // $_SERVER['HTTP_REFERER'] : provide information about refering url
  23. if (isset($_SERVER['HTT_REFERER'])) {
  24. $referer = Database::escape_string($_SERVER['HTTP_REFERER']);
  25. } else {
  26. $referer = '';
  27. }
  28. // record informations only if user comes from another site
  29. //if(!eregi($_configuration['root_web'],$referer))
  30. $pos = strpos($referer, $_configuration['root_web']);
  31. if ($pos === false && $referer != '') {
  32. $ip = api_get_real_ip();
  33. $remhost = @ getHostByAddr($ip);
  34. if ($remhost == $ip)
  35. $remhost = "Unknown"; // don't change this
  36. $reallyNow = api_get_utc_datetime();
  37. $sql = "INSERT INTO ".$TABLETRACK_OPEN."
  38. (open_remote_host,
  39. open_agent,
  40. open_referer,
  41. open_date)
  42. VALUES
  43. ('".$remhost."',
  44. '".Database::escape_string($_SERVER['HTTP_USER_AGENT'])."', '".Database::escape_string($referer)."', '$reallyNow')";
  45. $res = Database::query($sql);
  46. }
  47. return 1;
  48. }
  49. /**
  50. * @author Sebastien Piraux <piraux_seb@hotmail.com> old code
  51. * @author Julio Montoya 2013
  52. * @desc Record information for login event when an user identifies himself with username & password
  53. */
  54. public static function event_login($userId)
  55. {
  56. $userInfo = api_get_user_info($userId);
  57. $userId = intval($userId);
  58. if (empty($userInfo)) {
  59. return false;
  60. }
  61. $TABLETRACK_LOGIN = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  62. $reallyNow = api_get_utc_datetime();
  63. $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, user_ip, login_date, logout_date) VALUES
  64. ('".$userId."',
  65. '".Database::escape_string(api_get_real_ip())."',
  66. '".$reallyNow."',
  67. '".$reallyNow."'
  68. )";
  69. Database::query($sql);
  70. // Auto subscribe
  71. $user_status = $userInfo['status'] == SESSIONADMIN ? 'sessionadmin' :
  72. $userInfo['status'] == COURSEMANAGER ? 'teacher' :
  73. $userInfo['status'] == DRH ? 'DRH' : 'student';
  74. $autoSubscribe = api_get_setting($user_status.'_autosubscribe');
  75. if ($autoSubscribe) {
  76. $autoSubscribe = explode('|', $autoSubscribe);
  77. foreach ($autoSubscribe as $code) {
  78. if (CourseManager::course_exists($code)) {
  79. CourseManager::subscribe_user($userId, $code);
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * @param tool name of the tool (name in mainDb.accueil table)
  86. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  87. * @desc Record information for access event for courses
  88. */
  89. public static function accessCourse()
  90. {
  91. $TABLETRACK_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  92. //for "what's new" notification
  93. $TABLETRACK_LASTACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  94. $id_session = api_get_session_id();
  95. $now = api_get_utc_datetime();
  96. $courseId = api_get_course_int_id();
  97. $user_id = api_get_user_id();
  98. if ($user_id) {
  99. $user_id = "'".$user_id."'";
  100. } else {
  101. $user_id = "0"; // no one
  102. }
  103. $sql = "INSERT INTO ".$TABLETRACK_ACCESS." (access_user_id, c_id, access_date, access_session_id) VALUES
  104. (".$user_id.", '".$courseId."', '".$now."','".$id_session."')";
  105. Database::query($sql);
  106. // added for "what's new" notification
  107. $sql = "UPDATE $TABLETRACK_LASTACCESS SET access_date = '$now'
  108. WHERE access_user_id = $user_id AND c_id = '$courseId' AND access_tool IS NULL AND access_session_id=".$id_session;
  109. $result = Database::query($sql);
  110. if (Database::affected_rows($result) == 0) {
  111. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, c_id, access_date, access_session_id)
  112. VALUES (".$user_id.", '".$courseId."', '$now', '".$id_session."')";
  113. Database::query($sql);
  114. }
  115. return 1;
  116. }
  117. /**
  118. * @param tool name of the tool (name in mainDb.accueil table)
  119. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  120. * @desc Record information for access event for tools
  121. *
  122. * $tool can take this values :
  123. * Links, Calendar, Document, Announcements,
  124. * Group, Video, Works, Users, Exercices, Course Desc
  125. * ...
  126. * Values can be added if new modules are created (15char max)
  127. * I encourage to use $nameTool as $tool when calling this function
  128. *
  129. * Functionality for "what's new" notification is added by Toon Van Hoecke
  130. */
  131. public static function event_access_tool($tool, $id_session = 0)
  132. {
  133. if (empty($tool)) {
  134. return false;
  135. }
  136. $TABLETRACK_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  137. $TABLETRACK_LASTACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); //for "what's new" notification
  138. $_course = api_get_course_info();
  139. $courseId = api_get_course_int_id();
  140. $id_session = api_get_session_id();
  141. $tool = Database::escape_string($tool);
  142. $reallyNow = api_get_utc_datetime();
  143. $user_id = api_get_user_id();
  144. // record information
  145. // only if user comes from the course $_cid
  146. //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
  147. //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
  148. $coursePath = isset($_course['path']) ? $_course['path'] : null;
  149. $pos = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$coursePath)) : false;
  150. // added for "what's new" notification
  151. $pos2 = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_PATH)."index")) : false;
  152. // end "what's new" notification
  153. if ($pos !== false || $pos2 !== false) {
  154. $sql = "INSERT INTO ".$TABLETRACK_ACCESS."
  155. (access_user_id,
  156. c_id,
  157. access_tool,
  158. access_date,
  159. access_session_id
  160. )
  161. VALUES
  162. (".$user_id.",".// Don't add ' ' around value, it's already done.
  163. "'".$courseId."' ,
  164. '".$tool."',
  165. '".$reallyNow."',
  166. '".$id_session."')";
  167. Database::query($sql);
  168. }
  169. // "what's new" notification
  170. $sql = "UPDATE $TABLETRACK_LASTACCESS
  171. SET access_date = '$reallyNow'
  172. WHERE access_user_id = ".$user_id." AND c_id = '".$courseId."' AND access_tool = '".$tool."' AND access_session_id=".$id_session;
  173. $result = Database::query($sql);
  174. if (Database::affected_rows($result) == 0) {
  175. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, c_id, access_tool, access_date, access_session_id)
  176. VALUES (".$user_id.", '".$courseId."' , '$tool', '$reallyNow', $id_session)";
  177. Database::query($sql);
  178. }
  179. return 1;
  180. }
  181. /**
  182. * @param doc_id id of document (id in mainDb.document table)
  183. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  184. * @desc Record information for download event
  185. * (when an user click to d/l a document)
  186. * it will be used in a redirection page
  187. * bug fixed: Roan Embrechts
  188. * Roan:
  189. * The user id is put in single quotes,
  190. * (why? perhaps to prevent sql insertion hacks?)
  191. * and later again.
  192. * Doing this twice causes an error, I remove one of them.
  193. */
  194. public static function event_download($doc_url)
  195. {
  196. $tbl_stats_downloads = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  197. $doc_url = Database::escape_string($doc_url);
  198. $reallyNow = api_get_utc_datetime();
  199. $user_id = "'".api_get_user_id()."'";
  200. $_cid = api_get_course_int_id();
  201. $sql = "INSERT INTO $tbl_stats_downloads (
  202. down_user_id,
  203. c_id,
  204. down_doc_path,
  205. down_date,
  206. down_session_id
  207. )
  208. VALUES (
  209. ".$user_id.",
  210. '".$_cid."',
  211. '".$doc_url."',
  212. '".$reallyNow."',
  213. '".api_get_session_id()."'
  214. )";
  215. Database::query($sql);
  216. return 1;
  217. }
  218. /**
  219. * @param doc_id id of document (id in mainDb.document table)
  220. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  221. * @desc Record information for upload event
  222. * used in the works tool to record informations when
  223. * an user upload 1 work
  224. */
  225. public static function event_upload($doc_id)
  226. {
  227. $TABLETRACK_UPLOADS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
  228. $courseId = api_get_course_int_id();
  229. $reallyNow = api_get_utc_datetime();
  230. $user_id = api_get_user_id();
  231. $doc_id = intval($doc_id);
  232. $sql = "INSERT INTO ".$TABLETRACK_UPLOADS."
  233. ( upload_user_id,
  234. c_id,
  235. upload_work_id,
  236. upload_date,
  237. upload_session_id
  238. )
  239. VALUES (
  240. ".$user_id.",
  241. '".$courseId."',
  242. '".$doc_id."',
  243. '".$reallyNow."',
  244. '".api_get_session_id()."'
  245. )";
  246. Database::query($sql);
  247. return 1;
  248. }
  249. /**
  250. * @param link_id (id in coursDb liens table)
  251. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  252. * @desc Record information for link event (when an user click on an added link)
  253. * it will be used in a redirection page
  254. */
  255. public static function event_link($link_id)
  256. {
  257. $TABLETRACK_LINKS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS);
  258. $reallyNow = api_get_utc_datetime();
  259. $user_id = api_get_user_id();
  260. $sql = "INSERT INTO ".$TABLETRACK_LINKS."
  261. ( links_user_id,
  262. c_id,
  263. links_link_id,
  264. links_date,
  265. links_session_id
  266. ) VALUES (
  267. ".$user_id.",
  268. '".api_get_course_int_id()."',
  269. '".Database::escape_string($link_id)."',
  270. '".$reallyNow."',
  271. '".api_get_session_id()."'
  272. )";
  273. Database::query($sql);
  274. return 1;
  275. }
  276. /**
  277. * Update the TRACK_E_EXERCICES exercises
  278. *
  279. * @param int exeid id of the attempt
  280. * @param int exo_id exercise id
  281. * @param mixed result score
  282. * @param int weighting ( higher score )
  283. * @param int duration ( duration of the attempt in seconds )
  284. * @param int session_id
  285. * @param int learnpath_id (id of the learnpath)
  286. * @param int learnpath_item_id (id of the learnpath_item)
  287. *
  288. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  289. * @author Julio Montoya Armas <gugli100@gmail.com> Reworked 2010
  290. * @desc Record result of user when an exercice was done
  291. */
  292. public static function update_event_exercice(
  293. $exeid,
  294. $exo_id,
  295. $score,
  296. $weighting,
  297. $session_id,
  298. $learnpath_id = 0,
  299. $learnpath_item_id = 0,
  300. $learnpath_item_view_id = 0,
  301. $duration = 0,
  302. $question_list = array(),
  303. $status = '',
  304. $remind_list = array(),
  305. $end_date = null
  306. ) {
  307. global $debug;
  308. if ($debug) error_log('Called to update_event_exercice');
  309. if ($debug) error_log('duration:' . $duration);
  310. if ($exeid != '') {
  311. /*
  312. * Code commented due BT#8423 do not change the score to 0.
  313. *
  314. * Validation in case of fraud with actived control time
  315. if (!ExerciseLib::exercise_time_control_is_valid($exo_id, $learnpath_id, $learnpath_item_id)) {
  316. $score = 0;
  317. }
  318. */
  319. if (!isset($status) || empty($status)) {
  320. $status = '';
  321. } else {
  322. $status = Database::escape_string($status);
  323. }
  324. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  325. if (!empty($question_list)) {
  326. $question_list = array_map('intval', $question_list);
  327. }
  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. data_tracking = '".implode(',', $question_list)."',
  351. user_ip = '" . Database::escape_string(api_get_real_ip()) . "'
  352. WHERE exe_id = '".Database::escape_string($exeid)."'";
  353. $res = Database::query($sql);
  354. if ($debug) error_log('update_event_exercice called');
  355. if ($debug) error_log("$sql");
  356. //Deleting control time session track
  357. //ExerciseLib::exercise_time_control_delete($exo_id);
  358. return $res;
  359. } else {
  360. return false;
  361. }
  362. }
  363. /**
  364. * Record an event for this attempt at answering an exercise
  365. * @param float Score achieved
  366. * @param string Answer given
  367. * @param integer Question ID
  368. * @param integer Exercise attempt ID a.k.a exe_id (from track_e_exercise)
  369. * @param integer Position
  370. * @param integer Exercise ID (from c_quiz)
  371. * @param bool update results?
  372. * @param string Filename (for audio answers - using nanogong)
  373. * @param integer User ID The user who's going to get this score. Default value of null means "get from context".
  374. * @param integer Course ID (from the "id" column of course table). Default value of null means "get from context".
  375. * @param integer Session ID (from the session table). Default value of null means "get from context".
  376. * @param integer Learnpath ID (from c_lp table). Default value of null means "get from context".
  377. * @param integer Learnpath item ID (from the c_lp_item table). Default value of null means "get from context".
  378. * @return boolean Result of the insert query
  379. */
  380. public static function saveQuestionAttempt(
  381. $score,
  382. $answer,
  383. $question_id,
  384. $exe_id,
  385. $position,
  386. $exercise_id = 0,
  387. $updateResults = false,
  388. $nano = null,
  389. $user_id = null,
  390. $course_id = null,
  391. $session_id = null,
  392. $learnpath_id = null,
  393. $learnpath_item_id = null
  394. ) {
  395. global $debug;
  396. $question_id = Database::escape_string($question_id);
  397. $exe_id = Database::escape_string($exe_id);
  398. $position = Database::escape_string($position);
  399. $now = api_get_utc_datetime();
  400. // check user_id or get from context
  401. if (empty($user_id)) {
  402. $user_id = api_get_user_id();
  403. // anonymous
  404. if (empty($user_id)) {
  405. $user_id = api_get_anonymous_id();
  406. }
  407. }
  408. // check course_id or get from context
  409. if (empty($course_id) or intval($course_id) != $course_id) {
  410. $course_id = api_get_course_int_id();
  411. }
  412. // check session_id or get from context
  413. if (empty($session_id)) {
  414. $session_id = api_get_session_id();
  415. }
  416. // check learnpath_id or get from context
  417. if (empty($learnpath_id)) {
  418. global $learnpath_id;
  419. }
  420. // check learnpath_item_id or get from context
  421. if (empty($learnpath_item_id)) {
  422. global $learnpath_item_id;
  423. }
  424. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  425. if ($debug) {
  426. error_log("----- entering saveQuestionAttempt() function ------");
  427. error_log("answer: $answer");
  428. error_log("score: $score");
  429. error_log("question_id : $question_id");
  430. error_log("position: $position");
  431. }
  432. //Validation in case of fraud with active control time
  433. if (!ExerciseLib::exercise_time_control_is_valid($exercise_id, $learnpath_id, $learnpath_item_id)) {
  434. if ($debug) {
  435. error_log("exercise_time_control_is_valid is false");
  436. }
  437. $score = 0;
  438. $answer = 0;
  439. }
  440. $file = '';
  441. if (isset($nano)) {
  442. $file = Database::escape_string(basename($nano->load_filename_if_exists(false)));
  443. }
  444. $session_id = api_get_session_id();
  445. $courseInfo = api_get_course_info_by_id($course_id);
  446. if (!empty($question_id) && !empty($exe_id) && !empty($user_id)) {
  447. $attempt = array(
  448. 'user_id' => $user_id,
  449. 'question_id' => $question_id,
  450. 'answer' => $answer,
  451. 'marks' => $score,
  452. 'c_id' => $course_id,
  453. 'session_id' => $session_id,
  454. 'position' => $position,
  455. 'tms' => $now,
  456. 'filename' => $file,
  457. );
  458. // Check if attempt exists.
  459. $sql = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT
  460. WHERE
  461. c_id = $course_id AND
  462. session_id = $session_id AND
  463. exe_id = $exe_id AND
  464. user_id = $user_id AND
  465. question_id = $question_id AND
  466. position = $position";
  467. $result = Database::query($sql);
  468. if (Database::num_rows($result)) {
  469. if ($debug) {
  470. error_log("Attempt already exist: exe_id: $exe_id - user_id:$user_id - question_id:$question_id");
  471. }
  472. if ($updateResults == false) {
  473. //The attempt already exist do not update use update_event_exercise() instead
  474. return false;
  475. }
  476. } else {
  477. $attempt['exe_id'] = $exe_id;
  478. }
  479. if ($debug) {
  480. error_log("updateResults : $updateResults");
  481. error_log("Saving question attempt: ");
  482. error_log($sql);
  483. }
  484. $recording_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  485. if ($updateResults == false) {
  486. $attempt_id = Database::insert($TBL_TRACK_ATTEMPT, $attempt);
  487. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
  488. if ($debug) {
  489. error_log("Saving e attempt recording ");
  490. }
  491. $attempt_recording = array(
  492. 'exe_id' => $attempt_id,
  493. 'question_id' => $question_id,
  494. 'marks' => $score,
  495. 'insert_date' => $now,
  496. 'author' => '',
  497. 'session_id' => $session_id,
  498. );
  499. Database::insert($recording_table, $attempt_recording);
  500. }
  501. } else {
  502. Database::update($TBL_TRACK_ATTEMPT, $attempt,
  503. array('exe_id = ? AND question_id = ? AND user_id = ? ' => array($exe_id, $question_id, $user_id)));
  504. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
  505. $attempt_recording = array(
  506. 'exe_id' => $exe_id,
  507. 'question_id' => $question_id,
  508. 'marks' => $score,
  509. 'insert_date' => $now,
  510. 'author' => '',
  511. 'session_id' => $session_id,
  512. );
  513. Database::update($recording_table, $attempt_recording,
  514. array('exe_id = ? AND question_id = ? AND session_id = ? ' => array($exe_id, $question_id, $session_id))
  515. );
  516. }
  517. $attempt_id = $exe_id;
  518. }
  519. return $attempt_id;
  520. } else {
  521. return false;
  522. }
  523. }
  524. /**
  525. * Record an hotspot spot for this attempt at answering an hotspot question
  526. * @param int Exercise ID
  527. * @param int Question ID
  528. * @param int Answer ID
  529. * @param int Whether this answer is correct (1) or not (0)
  530. * @param string Coordinates of this point (e.g. 123;324)
  531. * @param bool update results?
  532. * @return boolean Result of the insert query
  533. * @uses Course code and user_id from global scope $_cid and $_user
  534. */
  535. public static function saveExerciseAttemptHotspot($exe_id, $question_id, $answer_id, $correct, $coords, $updateResults = false, $exerciseId = 0)
  536. {
  537. global $safe_lp_id, $safe_lp_item_id;
  538. if ($updateResults == false) {
  539. // Validation in case of fraud with activated control time
  540. if (!ExerciseLib::exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
  541. $correct = 0;
  542. }
  543. }
  544. $tbl_track_e_hotspot = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  545. if ($updateResults) {
  546. $params = array(
  547. 'hotspot_correct' => $correct,
  548. 'hotspot_coordinate' => $coords
  549. );
  550. Database::update($tbl_track_e_hotspot, $params,
  551. array('hotspot_user_id = ? AND hotspot_exe_id = ? AND hotspot_question_id = ? AND hotspot_answer_id = ? ' => array(
  552. api_get_user_id(),
  553. $exe_id,
  554. $question_id,
  555. $answer_id,
  556. $answer_id
  557. )));
  558. } else {
  559. $sql = "INSERT INTO $tbl_track_e_hotspot (hotspot_course_code, hotspot_user_id, c_id, hotspot_exe_id, hotspot_question_id, hotspot_answer_id, hotspot_correct, hotspot_coordinate)
  560. VALUES (
  561. '".api_get_course_id()."',
  562. ".api_get_user_id()."',".
  563. " ".api_get_course_int_id().", ".
  564. " '".Database :: escape_string($exe_id)."', ".
  565. " '".Database :: escape_string($question_id)."',".
  566. " '".Database :: escape_string($answer_id)."',".
  567. " '".Database :: escape_string($correct)."',".
  568. " '".Database :: escape_string($coords)."')";
  569. return $result = Database::query($sql);
  570. }
  571. }
  572. /**
  573. * Records information for common (or admin) events (in the track_e_default table)
  574. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  575. * @param string $event_type Type of event
  576. * @param string $event_value_type Type of value
  577. * @param string $event_value Value
  578. * @param string $datetime Datetime (UTC) (defaults to null)
  579. * @param int $user_id User ID (defaults to null)
  580. * @param int $course_id Course ID (defaults to null)
  581. * @param int $sessionId Session ID
  582. * @return bool
  583. * @assert ('','','') === false
  584. */
  585. public static function addEvent(
  586. $event_type,
  587. $event_value_type,
  588. $event_value,
  589. $datetime = null,
  590. $user_id = null,
  591. $course_id = null,
  592. $sessionId = 0
  593. ) {
  594. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  595. if (empty($event_type)) {
  596. return false;
  597. }
  598. $event_type = Database::escape_string($event_type);
  599. $event_value_type = Database::escape_string($event_value_type);
  600. if (!empty($course_id)) {
  601. $course_id = intval($course_id);
  602. } else {
  603. $course_id = api_get_course_int_id();
  604. }
  605. if (!empty($sessionId)) {
  606. $sessionId = intval($sessionId);
  607. } else {
  608. $sessionId = api_get_session_id();
  609. }
  610. //Clean the user_info
  611. if ($event_value_type == LOG_USER_OBJECT) {
  612. if (is_array($event_value)) {
  613. unset($event_value['complete_name']);
  614. unset($event_value['complete_name_with_username']);
  615. unset($event_value['firstName']);
  616. unset($event_value['lastName']);
  617. unset($event_value['avatar_small']);
  618. unset($event_value['avatar']);
  619. unset($event_value['mail']);
  620. unset($event_value['password']);
  621. unset($event_value['lastLogin']);
  622. unset($event_value['picture_uri']);
  623. $event_value = serialize($event_value);
  624. }
  625. }
  626. $event_value = Database::escape_string($event_value);
  627. $sessionId = empty($sessionId) ? api_get_session_id() : intval($sessionId);
  628. if (!isset($datetime)) {
  629. $datetime = api_get_utc_datetime();
  630. }
  631. $datetime = Database::escape_string($datetime);
  632. if (!isset($user_id)) {
  633. $user_id = api_get_user_id();
  634. }
  635. $params = array(
  636. 'default_user_id' => $user_id,
  637. 'c_id' => $course_id,
  638. 'default_date' => $datetime,
  639. 'default_event_type' => $event_type,
  640. 'default_value_type' => $event_value_type,
  641. 'default_value' => $event_value,
  642. 'session_id' => $sessionId
  643. );
  644. Database::insert($table, $params);
  645. return true;
  646. }
  647. /**
  648. * Get every email stored in the database
  649. *
  650. * @param int $etId
  651. * @return type
  652. * @assert () !== false
  653. */
  654. public static function get_all_event_types()
  655. {
  656. global $event_config;
  657. $sql = 'SELECT etm.id, event_type_name, activated, language_id, message, subject, dokeos_folder
  658. FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' etm
  659. INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l
  660. ON etm.language_id = l.id';
  661. $events_types = Database::store_result(Database::query($sql), 'ASSOC');
  662. $to_return = array();
  663. foreach ($events_types as $et) {
  664. $et['nameLangVar'] = $event_config[$et["event_type_name"]]["name_lang_var"];
  665. $et['descLangVar'] = $event_config[$et["event_type_name"]]["desc_lang_var"];
  666. $to_return[] = $et;
  667. }
  668. return $to_return;
  669. }
  670. /**
  671. * Get the users related to one event
  672. *
  673. * @param string $event_name
  674. */
  675. public static function get_event_users($event_name)
  676. {
  677. $event_name = Database::escape_string($event_name);
  678. $sql = 'SELECT user.user_id, user.firstname, user.lastname
  679. FROM '.Database::get_main_table(TABLE_MAIN_USER).' user
  680. JOIN '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' relUser
  681. ON relUser.user_id = user.user_id
  682. WHERE user.status <> '.ANONYMOUS.' AND relUser.event_type_name = "'.$event_name.'"';
  683. $user_list = Database::store_result(Database::query($sql), 'ASSOC');
  684. return json_encode($user_list);
  685. }
  686. /**
  687. * @param int $user_id
  688. * @param string $event_type
  689. * @return array|bool
  690. */
  691. public static function get_events_by_user_and_type($user_id, $event_type)
  692. {
  693. $TABLETRACK_DEFAULT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  694. $user_id = intval($user_id);
  695. $event_type = Database::escape_string($event_type);
  696. $sql = "SELECT * FROM $TABLETRACK_DEFAULT
  697. WHERE default_value_type = 'user_id' AND
  698. default_value = $user_id AND
  699. default_event_type = '$event_type'
  700. ORDER BY default_date ";
  701. $result = Database::query($sql);
  702. if ($result) {
  703. return Database::store_result($result, 'ASSOC');
  704. }
  705. return false;
  706. }
  707. /**
  708. * @deprecated seems not to be use.
  709. * @param $user_id
  710. * @param $event_type
  711. * @return mixed
  712. */
  713. function get_latest_event_by_user_and_type($user_id, $event_type)
  714. {
  715. $result = self::get_events_by_user_and_type($user_id, $event_type);
  716. if ($result && !empty($result)) {
  717. return $result[0];
  718. }
  719. }
  720. /**
  721. * Save the new message for one event and for one language
  722. *
  723. * @param string $eventName
  724. * @param array $users
  725. * @param string $message
  726. * @param string $subject
  727. * @param string $eventMessageLanguage
  728. * @param int $activated
  729. */
  730. public static function save_event_type_message($event_name, $users, $message, $subject, $event_message_language, $activated)
  731. {
  732. $event_name = Database::escape_string($event_name);
  733. $activated = intval($activated);
  734. $event_message_language = Database::escape_string($event_message_language);
  735. // Deletes then re-adds the users linked to the event
  736. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_name = "'.$event_name.'" ';
  737. Database::query($sql);
  738. foreach ($users as $user) {
  739. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_name) VALUES('.intval($user).',"'.$event_name.'")';
  740. Database::query($sql);
  741. }
  742. $language_id = api_get_language_id($event_message_language);
  743. // check if this template in this language already exists or not
  744. $sql = 'SELECT COUNT(id) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  745. WHERE event_type_name = "'.$event_name.'" AND language_id = '.$language_id;
  746. $sql = Database::store_result(Database::query($sql), 'ASSOC');
  747. // if already exists, we update
  748. if ($sql[0]["total"] > 0) {
  749. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  750. SET message = "'.Database::escape_string($message).'",
  751. subject = "'.Database::escape_string($subject).'",
  752. activated = '.$activated.'
  753. WHERE event_type_name = "'.$event_name.'" AND language_id = (SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  754. WHERE dokeos_folder = "'.$event_message_language.'")';
  755. Database::query($sql);
  756. } else { // else we create a new record
  757. // gets the language_-_id
  758. $lang_id = '(SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  759. WHERE dokeos_folder = "'.$event_message_language.'")';
  760. $lang_id = Database::store_result(Database::query($lang_id), 'ASSOC');
  761. if (!empty($lang_id[0]["id"])) {
  762. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' (event_type_name, language_id, message, subject, activated)
  763. VALUES("'.$event_name.'", '.$lang_id[0]["id"].', "'.Database::escape_string($message).'", "'.Database::escape_string($subject).'", '.$activated.')';
  764. Database::query($sql);
  765. }
  766. }
  767. // set activated at every save
  768. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  769. SET activated = '.$activated.'
  770. WHERE event_type_name = "'.$event_name.'"';
  771. Database::query($sql);
  772. }
  773. /**
  774. * @param $etId
  775. * @param $users
  776. * @param $message
  777. * @param $subject
  778. * @deprecated seem not to be use
  779. */
  780. public static function eventTypeMod($etId, $users, $message, $subject) {
  781. $etId = intval($etId);
  782. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_id = '.$etId.' ';
  783. Database::query($sql);
  784. foreach ($users as $user) {
  785. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_id)
  786. VALUES('.intval($user).','.$etId.') ';
  787. Database::query($sql);
  788. }
  789. $sql = 'UPDATE '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_MESSAGE).'
  790. SET message = "'.Database::escape_string($message).'",
  791. subject = "'.Database::escape_string($subject).'"
  792. WHERE event_type_id = '.$etId.'';
  793. Database::query($sql);
  794. }
  795. /**
  796. * Gets the last attempt of an exercise based in the exe_id
  797. * @param int $exe_id
  798. * @return mixed
  799. */
  800. public static function getLastAttemptDateOfExercise($exe_id)
  801. {
  802. $exe_id = intval($exe_id);
  803. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  804. $sql_track_attempt = 'SELECT max(tms) as last_attempt_date FROM '.$track_attempts.' WHERE exe_id='.$exe_id;
  805. $rs_last_attempt = Database::query($sql_track_attempt);
  806. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  807. $last_attempt_date = $row_last_attempt['last_attempt_date']; //Get the date of last attempt
  808. return $last_attempt_date;
  809. }
  810. /**
  811. * Gets the last attempt of an exercise based in the exe_id
  812. * @param int $exe_id
  813. * @return mixed
  814. */
  815. public static function getLatestQuestionIdFromAttempt($exe_id)
  816. {
  817. $exe_id = intval($exe_id);
  818. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  819. $sql = 'SELECT question_id FROM '.$track_attempts.'
  820. WHERE exe_id='.$exe_id.'
  821. ORDER BY tms DESC
  822. LIMIT 1';
  823. $result = Database::query($sql);
  824. if (Database::num_rows($result)) {
  825. $row = Database::fetch_array($result);
  826. return $row['question_id'];
  827. } else {
  828. return false;
  829. }
  830. }
  831. /**
  832. * Gets how many attempts exists by user, exercise, learning path
  833. * @param int user id
  834. * @param int exercise id
  835. * @param int lp id
  836. * @param int lp item id
  837. * @param int lp item view id
  838. */
  839. public static function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id, $lp_item_view_id)
  840. {
  841. $stat_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  842. $user_id = intval($user_id);
  843. $exerciseId = intval($exerciseId);
  844. $lp_id = intval($lp_id);
  845. $lp_item_id = intval($lp_item_id);
  846. $lp_item_view_id = intval($lp_item_view_id);
  847. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  848. exe_exo_id = $exerciseId AND
  849. exe_user_id = $user_id AND
  850. status != 'incomplete' AND
  851. orig_lp_id = $lp_id AND
  852. orig_lp_item_id = $lp_item_id AND
  853. orig_lp_item_view_id = $lp_item_view_id AND
  854. c_id = '".api_get_course_int_id()."' AND
  855. session_id = '".api_get_session_id()."'";
  856. $query = Database::query($sql);
  857. if (Database::num_rows($query) > 0) {
  858. $attempt = Database :: fetch_array($query, 'ASSOC');
  859. return $attempt['count'];
  860. } else {
  861. return 0;
  862. }
  863. }
  864. /**
  865. * @param $user_id
  866. * @param $exerciseId
  867. * @param $lp_id
  868. * @param $lp_item_id
  869. * @return int
  870. */
  871. public static function get_attempt_count_not_finished($user_id, $exerciseId, $lp_id, $lp_item_id)
  872. {
  873. $stat_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  874. $user_id = intval($user_id);
  875. $exerciseId = intval($exerciseId);
  876. $lp_id = intval($lp_id);
  877. $lp_item_id = intval($lp_item_id);
  878. //$lp_item_view_id = intval($lp_item_view_id);
  879. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  880. exe_exo_id = $exerciseId AND
  881. exe_user_id = $user_id AND
  882. status != 'incomplete' AND
  883. orig_lp_id = $lp_id AND
  884. orig_lp_item_id = $lp_item_id AND
  885. c_id = '".api_get_course_int_id()."' AND
  886. session_id = '".api_get_session_id()."'";
  887. $query = Database::query($sql);
  888. if (Database::num_rows($query) > 0) {
  889. $attempt = Database :: fetch_array($query, 'ASSOC');
  890. return $attempt['count'];
  891. } else {
  892. return 0;
  893. }
  894. }
  895. /**
  896. * @param int $user_id
  897. * @param int $lp_id
  898. * @param array $course
  899. * @param int $session_id
  900. */
  901. public static function delete_student_lp_events($user_id, $lp_id, $course, $session_id)
  902. {
  903. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  904. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  905. $lpInteraction = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  906. $lpObjective = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  907. $course_id = $course['real_id'];
  908. if (empty($course_id)) {
  909. $course_id = api_get_course_int_id();
  910. }
  911. $track_e_exercises = Database::get_main_table(
  912. TABLE_STATISTIC_TRACK_E_EXERCISES
  913. );
  914. $track_attempts = Database::get_main_table(
  915. TABLE_STATISTIC_TRACK_E_ATTEMPT
  916. );
  917. $recording_table = Database::get_main_table(
  918. TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING
  919. );
  920. $user_id = intval($user_id);
  921. $lp_id = intval($lp_id);
  922. $session_id = intval($session_id);
  923. //Make sure we have the exact lp_view_id
  924. $sql = "SELECT id FROM $lp_view_table
  925. WHERE
  926. c_id = $course_id AND
  927. user_id = $user_id AND
  928. lp_id = $lp_id AND
  929. session_id = $session_id ";
  930. $result = Database::query($sql);
  931. if (Database::num_rows($result)) {
  932. $view = Database::fetch_array($result, 'ASSOC');
  933. $lp_view_id = $view['id'];
  934. $sql = "DELETE FROM $lp_item_view_table WHERE c_id = $course_id AND lp_view_id = $lp_view_id ";
  935. Database::query($sql);
  936. $sql = "DELETE FROM $lpInteraction
  937. WHERE c_id = $course_id AND lp_iv_id = $lp_view_id ";
  938. Database::query($sql);
  939. $sql = "DELETE FROM $lpObjective
  940. WHERE c_id = $course_id AND lp_iv_id = $lp_view_id ";
  941. Database::query($sql);
  942. }
  943. $sql = "DELETE FROM $lp_view_table
  944. WHERE
  945. c_id = $course_id AND
  946. user_id = $user_id AND
  947. lp_id= $lp_id AND
  948. session_id = $session_id
  949. ";
  950. Database::query($sql);
  951. $sql = "SELECT exe_id FROM $track_e_exercises
  952. WHERE exe_user_id = $user_id AND
  953. session_id = $session_id AND
  954. c_id = $course_id AND
  955. orig_lp_id = $lp_id";
  956. $result = Database::query($sql);
  957. $exe_list = array();
  958. while ($row = Database::fetch_array($result, 'ASSOC')) {
  959. $exe_list[] = $row['exe_id'];
  960. }
  961. if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) {
  962. $sql = "DELETE FROM $track_e_exercises
  963. WHERE exe_id IN (" . implode(',', $exe_list) . ")";
  964. Database::query($sql);
  965. $sql = "DELETE FROM $track_attempts
  966. WHERE exe_id IN (" . implode(',', $exe_list) . ")";
  967. Database::query($sql);
  968. $sql = "DELETE FROM $recording_table
  969. WHERE exe_id IN (" . implode(',', $exe_list) . ")";
  970. Database::query($sql);
  971. }
  972. Event::addEvent(
  973. LOG_LP_ATTEMPT_DELETE,
  974. LOG_LP_ID,
  975. $lp_id,
  976. null,
  977. null,
  978. $course_id,
  979. $session_id
  980. );
  981. }
  982. /**
  983. * Delete all exercise attempts (included in LP or not)
  984. *
  985. * @param int user id
  986. * @param int exercise id
  987. * @param int $course_id
  988. * @param int session id
  989. */
  990. public static function delete_all_incomplete_attempts($user_id, $exercise_id, $course_id, $session_id = 0)
  991. {
  992. $track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  993. $user_id = intval($user_id);
  994. $exercise_id = intval($exercise_id);
  995. $course_id = intval($course_id);
  996. $session_id = intval($session_id);
  997. if (!empty($user_id) && !empty($exercise_id) && !empty($course_code)) {
  998. $sql = "DELETE FROM $track_e_exercises
  999. WHERE
  1000. exe_user_id = $user_id AND
  1001. exe_exo_id = $exercise_id AND
  1002. c_id = '$course_id' AND
  1003. session_id = $session_id AND
  1004. status = 'incomplete' ";
  1005. Database::query($sql);
  1006. Event::addEvent(
  1007. LOG_EXERCISE_RESULT_DELETE,
  1008. LOG_EXERCISE_AND_USER_ID,
  1009. $exercise_id . '-' . $user_id,
  1010. null,
  1011. null,
  1012. $course_id,
  1013. $session_id
  1014. );
  1015. }
  1016. }
  1017. /**
  1018. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  1019. * @param int exercise id
  1020. * @param int $courseId
  1021. * @param int session id
  1022. * @return array with the results
  1023. *
  1024. */
  1025. public static function get_all_exercise_results($exercise_id, $courseId, $session_id = 0, $load_question_list = true, $user_id = null)
  1026. {
  1027. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1028. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1029. $courseId = intval($courseId);
  1030. $exercise_id = intval($exercise_id);
  1031. $session_id = intval($session_id);
  1032. $user_condition = null;
  1033. if (!empty($user_id)) {
  1034. $user_id = intval($user_id);
  1035. $user_condition = "AND exe_user_id = $user_id ";
  1036. }
  1037. $sql = "SELECT * FROM $TABLETRACK_EXERCICES
  1038. WHERE
  1039. status = '' AND
  1040. c_id = '$courseId' AND
  1041. exe_exo_id = '$exercise_id' AND
  1042. session_id = $session_id AND
  1043. orig_lp_id =0 AND
  1044. orig_lp_item_id = 0
  1045. $user_condition
  1046. ORDER BY exe_id";
  1047. $res = Database::query($sql);
  1048. $list = array();
  1049. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1050. $list[$row['exe_id']] = $row;
  1051. if ($load_question_list) {
  1052. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  1053. $res_question = Database::query($sql);
  1054. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1055. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1056. }
  1057. }
  1058. }
  1059. return $list;
  1060. }
  1061. /**
  1062. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  1063. * @param int $courseId
  1064. * @param int session id
  1065. * @return array with the results
  1066. *
  1067. */
  1068. public static function get_all_exercise_results_by_course($courseId, $session_id = 0, $get_count = true)
  1069. {
  1070. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1071. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1072. $courseId = intval($courseId);
  1073. $session_id = intval($session_id);
  1074. $select = '*';
  1075. if ($get_count) {
  1076. $select = 'count(*) as count';
  1077. }
  1078. $sql = "SELECT $select FROM $table_track_exercises
  1079. WHERE status = '' AND
  1080. c_id = '$courseId' AND
  1081. session_id = $session_id AND
  1082. orig_lp_id = 0 AND
  1083. orig_lp_item_id = 0
  1084. ORDER BY exe_id";
  1085. $res = Database::query($sql);
  1086. if ($get_count) {
  1087. $row = Database::fetch_array($res, 'ASSOC');
  1088. return $row['count'];
  1089. } else {
  1090. $list = array();
  1091. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1092. $list[$row['exe_id']] = $row;
  1093. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1094. $res_question = Database::query($sql);
  1095. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1096. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1097. }
  1098. }
  1099. return $list;
  1100. }
  1101. }
  1102. /**
  1103. * Gets all exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1104. * @param int exercise id
  1105. * @param int $courseId
  1106. * @param int session id
  1107. * @return array with the results
  1108. *
  1109. */
  1110. public static function get_all_exercise_results_by_user($user_id, $courseId, $session_id = 0)
  1111. {
  1112. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1113. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1114. $courseId = intval($courseId);
  1115. $session_id = intval($session_id);
  1116. $user_id = intval($user_id);
  1117. $sql = "SELECT * FROM $table_track_exercises
  1118. WHERE status = '' AND
  1119. exe_user_id = $user_id AND
  1120. c_id = '$courseId' AND
  1121. session_id = $session_id AND
  1122. orig_lp_id = 0 AND
  1123. orig_lp_item_id = 0
  1124. ORDER by exe_id";
  1125. $res = Database::query($sql);
  1126. $list = array();
  1127. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1128. $list[$row['exe_id']] = $row;
  1129. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1130. $res_question = Database::query($sql);
  1131. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1132. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1133. }
  1134. }
  1135. //echo '<pre>'; print_r($list);
  1136. return $list;
  1137. }
  1138. /**
  1139. * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1140. * @param int exercise id
  1141. * @param string course code
  1142. * @param int session id
  1143. * @return array with the results
  1144. *
  1145. */
  1146. public static function get_exercise_results_by_attempt($exe_id, $status = null)
  1147. {
  1148. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1149. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1150. $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1151. $exe_id = intval($exe_id);
  1152. $status = Database::escape_string($status);
  1153. $sql = "SELECT * FROM $table_track_exercises WHERE status = '".$status."' AND exe_id = $exe_id";
  1154. $res = Database::query($sql);
  1155. $list = array();
  1156. if (Database::num_rows($res)) {
  1157. $row = Database::fetch_array($res, 'ASSOC');
  1158. //Checking if this attempt was revised by a teacher
  1159. $sql_revised = 'SELECT exe_id FROM '.$table_track_attempt_recording.' WHERE author != "" AND exe_id = '.$exe_id.' LIMIT 1';
  1160. $res_revised = Database::query($sql_revised);
  1161. $row['attempt_revised'] = 0;
  1162. if (Database::num_rows($res_revised) > 0) {
  1163. $row['attempt_revised'] = 1;
  1164. }
  1165. $list[$exe_id] = $row;
  1166. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY tms ASC";
  1167. $res_question = Database::query($sql);
  1168. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1169. $list[$exe_id]['question_list'][$row_q['question_id']] = $row_q;
  1170. }
  1171. }
  1172. return $list;
  1173. }
  1174. /**
  1175. * Gets exercise results (NO Exercises in LPs) from a given user, exercise id, course, session, lp_id, lp_item_id
  1176. * @param int user id
  1177. * @param int exercise id
  1178. * @param string course code
  1179. * @param int session id
  1180. * @param int lp id
  1181. * @param int lp item id
  1182. * @param string order asc or desc
  1183. * @return array with the results
  1184. *
  1185. */
  1186. public static function getExerciseResultsByUser($user_id, $exercise_id, $courseId, $session_id = 0, $lp_id = 0, $lp_item_id = 0, $order = null)
  1187. {
  1188. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1189. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1190. $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1191. $courseId = intval($courseId);
  1192. $exercise_id = intval($exercise_id);
  1193. $session_id = intval($session_id);
  1194. $user_id = intval($user_id);
  1195. $lp_id = intval($lp_id);
  1196. $lp_item_id = intval($lp_item_id);
  1197. if (!in_array(strtolower($order), array('asc', 'desc'))) {
  1198. $order = 'asc';
  1199. }
  1200. $sql = "SELECT * FROM $table_track_exercises
  1201. WHERE status = '' AND
  1202. exe_user_id = $user_id AND
  1203. c_id = $courseId AND
  1204. exe_exo_id = $exercise_id AND
  1205. session_id = $session_id AND
  1206. orig_lp_id = $lp_id AND
  1207. orig_lp_item_id = $lp_item_id
  1208. ORDER by exe_id $order ";
  1209. $res = Database::query($sql);
  1210. $list = array();
  1211. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1212. //Checking if this attempt was revised by a teacher
  1213. $sql_revised = 'SELECT exe_id FROM '.$table_track_attempt_recording.' WHERE author != "" AND exe_id = '.$row['exe_id'].' LIMIT 1';
  1214. $res_revised = Database::query($sql_revised);
  1215. $row['attempt_revised'] = 0;
  1216. if (Database::num_rows($res_revised) > 0) {
  1217. $row['attempt_revised'] = 1;
  1218. }
  1219. $list[$row['exe_id']] = $row;
  1220. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1221. $res_question = Database::query($sql);
  1222. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1223. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1224. }
  1225. }
  1226. return $list;
  1227. }
  1228. /**
  1229. * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session
  1230. * @param int $user_id
  1231. * @param int exercise id
  1232. * @param int $courseId
  1233. * @param int session id
  1234. * @return array with the results
  1235. *
  1236. */
  1237. public static function count_exercise_attempts_by_user($user_id, $exercise_id, $courseId, $session_id = 0)
  1238. {
  1239. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1240. $courseId = intval($courseId);
  1241. $exercise_id = intval($exercise_id);
  1242. $session_id = intval($session_id);
  1243. $user_id = intval($user_id);
  1244. $sql = "SELECT count(*) as count FROM $TABLETRACK_EXERCICES
  1245. WHERE status = '' AND
  1246. exe_user_id = '$user_id' AND
  1247. c_id = '$courseId' AND
  1248. exe_exo_id = '$exercise_id' AND
  1249. session_id = $session_id AND
  1250. orig_lp_id =0 AND
  1251. orig_lp_item_id = 0
  1252. ORDER BY exe_id";
  1253. $res = Database::query($sql);
  1254. $result = 0;
  1255. if (Database::num_rows($res) > 0) {
  1256. $row = Database::fetch_array($res, 'ASSOC');
  1257. $result = $row['count'];
  1258. }
  1259. return $result;
  1260. }
  1261. /**
  1262. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1263. * @param int $exercise_id
  1264. * @param int $courseId
  1265. * @param int $session_id
  1266. * @return array with the results
  1267. * @todo rename this function
  1268. */
  1269. public static function get_best_exercise_results_by_user($exercise_id, $courseId, $session_id = 0)
  1270. {
  1271. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1272. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1273. $courseId = intval($courseId);
  1274. $exercise_id = intval($exercise_id);
  1275. $session_id = intval($session_id);
  1276. $sql = "SELECT * FROM $table_track_exercises
  1277. WHERE status = '' AND
  1278. c_id = $courseId AND
  1279. exe_exo_id = '$exercise_id' AND
  1280. session_id = $session_id AND
  1281. orig_lp_id = 0 AND
  1282. orig_lp_item_id = 0
  1283. ORDER BY exe_id";
  1284. $res = Database::query($sql);
  1285. $list = array();
  1286. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1287. $list[$row['exe_id']] = $row;
  1288. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1289. $res_question = Database::query($sql);
  1290. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1291. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1292. }
  1293. }
  1294. //Getting the best results of every student
  1295. $best_score_return = array();
  1296. foreach ($list as $student_result) {
  1297. $user_id = $student_result['exe_user_id'];
  1298. $current_best_score[$user_id] = $student_result['exe_result'];
  1299. if (isset($current_best_score[$user_id]) && isset($best_score_return[$user_id]) && $current_best_score[$user_id] > $best_score_return[$user_id]['exe_result']) {
  1300. $best_score_return[$user_id] = $student_result;
  1301. }
  1302. }
  1303. return $best_score_return;
  1304. }
  1305. /**
  1306. * @param int $user_id
  1307. * @param int $exercise_id
  1308. * @param int $courseId
  1309. * @param int $session_id
  1310. * @return array
  1311. */
  1312. public static function get_best_attempt_exercise_results_per_user($user_id, $exercise_id, $courseId, $session_id = 0)
  1313. {
  1314. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1315. $courseId = intval($courseId);
  1316. $exercise_id = intval($exercise_id);
  1317. $session_id = intval($session_id);
  1318. $user_id = intval($user_id);
  1319. $sql = "SELECT * FROM $table_track_exercises
  1320. WHERE status = '' AND
  1321. c_id = '$courseId' AND
  1322. exe_exo_id = '$exercise_id' AND
  1323. session_id = $session_id AND
  1324. exe_user_id = $user_id AND
  1325. orig_lp_id =0 AND
  1326. orig_lp_item_id = 0
  1327. ORDER BY exe_id";
  1328. $res = Database::query($sql);
  1329. $list = array();
  1330. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1331. $list[$row['exe_id']] = $row; /*
  1332. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1333. $res_question = Database::query($sql);
  1334. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1335. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1336. } */
  1337. }
  1338. //Getting the best results of every student
  1339. $best_score_return = array();
  1340. $best_score_return['exe_result'] = 0;
  1341. foreach ($list as $result) {
  1342. $current_best_score = $result;
  1343. if ($current_best_score['exe_result'] > $best_score_return['exe_result']) {
  1344. $best_score_return = $result;
  1345. }
  1346. }
  1347. if (!isset($best_score_return['exe_weighting'])) {
  1348. $best_score_return = array();
  1349. }
  1350. return $best_score_return;
  1351. }
  1352. /**
  1353. * @param int $exercise_id
  1354. * @param int $courseId
  1355. * @param int $session_id
  1356. * @return mixed
  1357. */
  1358. public static function count_exercise_result_not_validated($exercise_id, $courseId, $session_id = 0)
  1359. {
  1360. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1361. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1362. $courseId = intval($courseId);
  1363. $session_id = intval($session_id);
  1364. $exercise_id = intval($exercise_id);
  1365. $sql = "SELECT count(e.exe_id) as count
  1366. FROM $table_track_exercises e
  1367. LEFT JOIN $table_track_attempt a
  1368. ON e.exe_id = a.exe_id
  1369. WHERE
  1370. exe_exo_id = $exercise_id AND
  1371. c_id = '$courseId' AND
  1372. e.session_id = $session_id AND
  1373. orig_lp_id = 0 AND
  1374. marks IS NULL AND
  1375. status = '' AND
  1376. orig_lp_item_id = 0 ORDER BY e.exe_id";
  1377. $res = Database::query($sql);
  1378. $row = Database::fetch_array($res, 'ASSOC');
  1379. return $row['count'];
  1380. }
  1381. /**
  1382. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1383. * @param int exercise id
  1384. * @param int course id
  1385. * @param int session id
  1386. * @return array with the results
  1387. *
  1388. */
  1389. public static function get_count_exercises_attempted_by_course($courseId, $session_id = 0)
  1390. {
  1391. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1392. $courseId = intval($courseId);
  1393. $session_id = intval($session_id);
  1394. $sql = "SELECT DISTINCT exe_exo_id, exe_user_id
  1395. FROM $table_track_exercises
  1396. WHERE status = '' AND
  1397. c_id = '$courseId' AND
  1398. session_id = $session_id AND
  1399. orig_lp_id =0 AND
  1400. orig_lp_item_id = 0
  1401. ORDER BY exe_id";
  1402. $res = Database::query($sql);
  1403. $count = 0;
  1404. if (Database::num_rows($res) > 0) {
  1405. $count = Database::num_rows($res);
  1406. }
  1407. return $count;
  1408. }
  1409. /**
  1410. * Gets all exercise events from a Learning Path within a Course nd Session
  1411. * @param int exercise id
  1412. * @param int $courseId
  1413. * @param int session id
  1414. * @return array
  1415. */
  1416. public static function get_all_exercise_event_from_lp($exercise_id, $courseId, $session_id = 0)
  1417. {
  1418. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1419. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1420. $courseId = intval($courseId);
  1421. $exercise_id = intval($exercise_id);
  1422. $session_id = intval($session_id);
  1423. $sql = "SELECT * FROM $table_track_exercises
  1424. WHERE
  1425. status = '' AND
  1426. c_id = $courseId AND
  1427. exe_exo_id = '$exercise_id' AND
  1428. session_id = $session_id AND
  1429. orig_lp_id !=0 AND
  1430. orig_lp_item_id != 0";
  1431. $res = Database::query($sql);
  1432. $list = array();
  1433. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1434. $list[$row['exe_id']] = $row;
  1435. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1436. $res_question = Database::query($sql);
  1437. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1438. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1439. }
  1440. }
  1441. return $list;
  1442. }
  1443. /**
  1444. * @param int $lp_id
  1445. * @param $course_id
  1446. * @return array
  1447. */
  1448. public static function get_all_exercises_from_lp($lp_id, $course_id)
  1449. {
  1450. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  1451. $course_id = intval($course_id);
  1452. $lp_id = intval($lp_id);
  1453. $sql = "SELECT * FROM $lp_item_table
  1454. WHERE c_id = $course_id AND lp_id = '".$lp_id."'
  1455. ORDER BY parent_item_id, display_order";
  1456. $res = Database::query($sql);
  1457. $my_exercise_list = array();
  1458. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1459. if ($row['item_type'] == 'quiz') {
  1460. $my_exercise_list[] = $row;
  1461. }
  1462. }
  1463. return $my_exercise_list;
  1464. }
  1465. /**
  1466. * This function gets the comments of an exercise
  1467. *
  1468. * @param int $id
  1469. * @param int $question_id
  1470. * @return string the comment
  1471. */
  1472. public static function get_comments($exe_id, $question_id)
  1473. {
  1474. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1475. $sql = "SELECT teacher_comment FROM ".$table_track_attempt."
  1476. WHERE exe_id='".Database::escape_string($exe_id)."' AND question_id = '".Database::escape_string($question_id)."'
  1477. ORDER by question_id";
  1478. $sqlres = Database::query($sql);
  1479. $comm = Database::result($sqlres, 0, "teacher_comment");
  1480. return $comm;
  1481. }
  1482. /**
  1483. * @param int $exe_id
  1484. * @return array
  1485. */
  1486. public static function getAllExerciseEventByExeId($exe_id)
  1487. {
  1488. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1489. $exe_id = intval($exe_id);
  1490. $list = array();
  1491. $sql = "SELECT * FROM $table_track_attempt
  1492. WHERE exe_id = $exe_id ORDER BY position";
  1493. $res_question = Database::query($sql);
  1494. if (Database::num_rows($res_question)) {
  1495. while ($row = Database::fetch_array($res_question, 'ASSOC')) {
  1496. $list[$row['question_id']][] = $row;
  1497. }
  1498. }
  1499. return $list;
  1500. }
  1501. /**
  1502. *
  1503. * @param int $exe_id
  1504. * @param int $user_id
  1505. * @param int $courseId
  1506. * @param int $session_id
  1507. * @param int $question_id
  1508. */
  1509. public static function delete_attempt($exe_id, $user_id, $courseId, $session_id, $question_id)
  1510. {
  1511. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1512. $exe_id = intval($exe_id);
  1513. $user_id = intval($user_id);
  1514. $courseId = intval($courseId);
  1515. $session_id = intval($session_id);
  1516. $question_id = intval($question_id);
  1517. $sql = "DELETE FROM $table_track_attempt
  1518. WHERE
  1519. exe_id = $exe_id AND
  1520. user_id = $user_id AND
  1521. c_id = $courseId AND
  1522. session_id = $session_id AND
  1523. question_id = $question_id ";
  1524. Database::query($sql);
  1525. $courseInfo = api_get_course_info_by_id($courseId);
  1526. Event::addEvent(
  1527. LOG_QUESTION_RESULT_DELETE,
  1528. LOG_EXERCISE_ATTEMPT_QUESTION_ID,
  1529. $exe_id . '-' . $question_id,
  1530. null,
  1531. null,
  1532. $courseId,
  1533. $session_id
  1534. );
  1535. }
  1536. /**
  1537. * @param $exe_id
  1538. * @param $user_id
  1539. * @param int $courseId
  1540. * @param $question_id
  1541. * @param int $sessionId
  1542. */
  1543. public static function delete_attempt_hotspot($exe_id, $user_id, $courseId, $question_id, $sessionId = null)
  1544. {
  1545. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1546. $exe_id = intval($exe_id);
  1547. $user_id = intval($user_id);
  1548. $courseId = intval($courseId);
  1549. $question_id = intval($question_id);
  1550. if (!isset($sessionId)) {
  1551. $sessionId = api_get_session_id();
  1552. }
  1553. $sql = "DELETE FROM $table_track_attempt
  1554. WHERE hotspot_exe_id = $exe_id AND
  1555. hotspot_user_id = $user_id AND
  1556. c_id = $courseId AND
  1557. hotspot_question_id = $question_id ";
  1558. Database::query($sql);
  1559. Event::addEvent(
  1560. LOG_QUESTION_RESULT_DELETE,
  1561. LOG_EXERCISE_ATTEMPT_QUESTION_ID,
  1562. $exe_id . '-' . $question_id,
  1563. null,
  1564. null,
  1565. $courseId,
  1566. $sessionId
  1567. );
  1568. }
  1569. /**
  1570. * @param $exe_id
  1571. * @param $objExercise
  1572. * @return array
  1573. */
  1574. public static function getAnsweredQuestionsFromAttempt($exe_id, $objExercise)
  1575. {
  1576. $attempt_list = self::getAllExerciseEventByExeId($exe_id);
  1577. $exercise_result = array();
  1578. if (!empty($attempt_list)) {
  1579. foreach ($attempt_list as $question_id => $options) {
  1580. foreach ($options as $item) {
  1581. $question_obj = Question::read($item['question_id']);
  1582. switch ($question_obj->type) {
  1583. case FILL_IN_BLANKS:
  1584. $item['answer'] = $objExercise->fill_in_blank_answer_to_string($item['answer']);
  1585. break;
  1586. case HOT_SPOT:
  1587. break;
  1588. }
  1589. if ($item['answer'] != '0' && !empty($item['answer'])) {
  1590. $exercise_result[] = $question_id;
  1591. break;
  1592. }
  1593. }
  1594. }
  1595. }
  1596. return $exercise_result;
  1597. }
  1598. /**
  1599. * User logs in for the first time to a course
  1600. * @param int $courseId
  1601. * @param int $user_id
  1602. * @param int $session_id
  1603. */
  1604. public static function event_course_login($courseId, $user_id, $session_id)
  1605. {
  1606. $course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1607. $time = api_get_datetime();
  1608. $courseId = intval($courseId);
  1609. $user_id = intval($user_id);
  1610. $session_id = intval($session_id);
  1611. $sql = "INSERT INTO $course_tracking_table(c_id, user_id, login_course_date, logout_course_date, counter, session_id)
  1612. VALUES('".$courseId."', '".$user_id."', '$time', '$time', '1', '".$session_id."')";
  1613. Database::query($sql);
  1614. //Course catalog stats modifications see #4191
  1615. CourseManager::update_course_ranking(null, null, null, null, true, false);
  1616. }
  1617. /**
  1618. * For the sake of genericity, this function is a switch.
  1619. * It's called by EventsDispatcher and fires the good function
  1620. * with the good require_once.
  1621. *
  1622. * @param string $event_name
  1623. * @param array $params
  1624. */
  1625. public static function event_send_mail($event_name, $params)
  1626. {
  1627. EventsMail::send_mail($event_name, $params);
  1628. }
  1629. /**
  1630. * Internal function checking if the mail was already sent from that user to that user
  1631. * @param string $event_name
  1632. * @param int $user_from
  1633. * @param int $user_to
  1634. * @return boolean
  1635. */
  1636. public static function check_if_mail_already_sent($event_name, $user_from, $user_to = null)
  1637. {
  1638. if ($user_to == null) {
  1639. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1640. WHERE user_from = '.$user_from.' AND event_type_name = "'.$event_name.'"';
  1641. } else {
  1642. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1643. WHERE user_from = '.$user_from.' AND user_to = '.$user_to.' AND event_type_name = "'.$event_name.'"';
  1644. }
  1645. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1646. return $result[0]["total"];
  1647. }
  1648. /**
  1649. *
  1650. * Filter EventEmailTemplate Filters see the main/inc/conf/events.conf.dist.php
  1651. *
  1652. */
  1653. /**
  1654. * Basic template event message filter (to be used by other filters as default)
  1655. * @param array $values (passing by reference)
  1656. * @return boolean True if everything is OK, false otherwise
  1657. */
  1658. function event_send_mail_filter_func(&$values)
  1659. {
  1660. return true;
  1661. }
  1662. /**
  1663. * user_registration - send_mail filter
  1664. * @param array $values (passing by reference)
  1665. * @return boolean True if everything is OK, false otherwise
  1666. */
  1667. function user_registration_event_send_mail_filter_func(&$values)
  1668. {
  1669. $res = _event_send_mail_filter_func($values);
  1670. // proper logic for this filter
  1671. return $res;
  1672. }
  1673. /**
  1674. * portal_homepage_edited - send_mail filter
  1675. * @param array $values (passing by reference)
  1676. * @return boolean True if everything is OK, false otherwise
  1677. */
  1678. function portal_homepage_edited_event_send_mail_filter_func(&$values)
  1679. {
  1680. $res = self::event_send_mail_filter_func($values);
  1681. // proper logic for this filter
  1682. return $res;
  1683. }
  1684. }