events.lib.inc.php 62 KB

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