events.lib.inc.php 59 KB

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