events.lib.inc.php 62 KB

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