events.lib.inc.php 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * EVENTS LIBRARY
  5. * @ŧodo add listeners
  6. *
  7. * This is the events library for Chamilo.
  8. * Include/require it in your code to use its functionality.
  9. * Functions of this library are used to record informations when some kind
  10. * of event occur. Each event has his own types of informations then each event
  11. * use its own function.
  12. *
  13. * @package chamilo.library
  14. */
  15. use Chamilo\UserBundle\Entity\User;
  16. class Event
  17. {
  18. /**
  19. * @author Sebastien Piraux <piraux_seb@hotmail.com> old code
  20. * @author Julio Montoya 2013
  21. * @desc Record information for login event when an user identifies himself with username & password
  22. */
  23. function event_login(User $user)
  24. {
  25. $userId = $user->getUserId();
  26. $TABLETRACK_LOGIN = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  27. $reallyNow = api_get_utc_datetime();
  28. $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, login_ip, login_date, logout_date) VALUES
  29. ('".$userId."',
  30. '".Database::escape_string(api_get_real_ip())."',
  31. '".$reallyNow."',
  32. '".$reallyNow."'
  33. )";
  34. Database::query($sql);
  35. $roles = $user->getRoles();
  36. // auto subscribe
  37. foreach ($roles as $role) {
  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. public static function accessCourse()
  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. public static 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. public static function addEvent(
  590. $event_type,
  591. $event_value_type,
  592. $event_value,
  593. $datetime = null,
  594. $user_id = null,
  595. $course_code = null
  596. ) {
  597. $TABLETRACK_DEFAULT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  598. if (empty($event_type)) {
  599. return false;
  600. }
  601. $event_type = Database::escape_string($event_type);
  602. $event_value_type = Database::escape_string($event_value_type);
  603. //Clean the user_info
  604. if ($event_value_type == LOG_USER_OBJECT) {
  605. if (is_array($event_value)) {
  606. unset($event_value['complete_name']);
  607. unset($event_value['complete_name_with_username']);
  608. unset($event_value['firstName']);
  609. unset($event_value['lastName']);
  610. unset($event_value['avatar_small']);
  611. unset($event_value['avatar_sys_path']);
  612. unset($event_value['avatar']);
  613. unset($event_value['mail']);
  614. unset($event_value['password']);
  615. unset($event_value['lastLogin']);
  616. unset($event_value['picture_uri']);
  617. $event_value = serialize($event_value);
  618. }
  619. }
  620. $event_value = Database::escape_string($event_value);
  621. $course_info = api_get_course_info($course_code);
  622. if (!empty($course_info)) {
  623. $course_id = $course_info['real_id'];
  624. $course_code = $course_info['code'];
  625. $course_code = Database::escape_string($course_code);
  626. } else {
  627. $course_id = null;
  628. $course_code = null;
  629. }
  630. if (!isset($datetime)) {
  631. $datetime = api_get_utc_datetime();
  632. }
  633. $datetime = Database::escape_string($datetime);
  634. if (!isset($user_id)) {
  635. $user_id = api_get_user_id();
  636. }
  637. $user_id = intval($user_id);
  638. $sql = "INSERT INTO $TABLETRACK_DEFAULT
  639. (default_user_id,
  640. default_cours_code,
  641. c_id,
  642. default_date,
  643. default_event_type,
  644. default_value_type,
  645. default_value
  646. )
  647. VALUES('$user_id.',
  648. '$course_code',
  649. '$course_id',
  650. '$datetime',
  651. '$event_type',
  652. '$event_value_type',
  653. '$event_value')";
  654. Database::query($sql);
  655. return true;
  656. }
  657. /**
  658. * Get every email stored in the database
  659. *
  660. * @param int $etId
  661. * @return type
  662. * @assert () !== false
  663. */
  664. function get_all_event_types()
  665. {
  666. global $event_config;
  667. $sql = 'SELECT etm.id, event_type_name, activated, language_id, message, subject, dokeos_folder
  668. FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' etm
  669. INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l
  670. ON etm.language_id = l.id';
  671. $events_types = Database::store_result(Database::query($sql), 'ASSOC');
  672. $to_return = array();
  673. foreach ($events_types as $et) {
  674. $et['nameLangVar'] = $event_config[$et["event_type_name"]]["name_lang_var"];
  675. $et['descLangVar'] = $event_config[$et["event_type_name"]]["desc_lang_var"];
  676. $to_return[] = $et;
  677. }
  678. return $to_return;
  679. }
  680. /**
  681. * Get users linked to an event
  682. *
  683. * @param int $etId
  684. * @return type
  685. */
  686. function get_users_subscribed_to_event($event_name)
  687. {
  688. $event_name = Database::escape_string($event_name);
  689. $sql = 'SELECT u.* FROM '.Database::get_main_table(TABLE_MAIN_USER).' u,'
  690. .Database::get_main_table(TABLE_MAIN_EVENT_TYPE).' e,'
  691. .Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' ue
  692. WHERE ue.user_id = u.user_id
  693. AND e.name = "'.$event_name.'"
  694. AND e.id = ue.event_type_id';
  695. $return = Database::store_result(Database::query($sql), 'ASSOC');
  696. return json_encode($return);
  697. }
  698. /**
  699. * Get the users related to one event
  700. *
  701. * @param string $event_name
  702. */
  703. function get_event_users($event_name)
  704. {
  705. $event_name = Database::escape_string($event_name);
  706. $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
  707. ON relUser.user_id = user.user_id
  708. WHERE user.status <> '.ANONYMOUS.' AND relUser.event_type_name = "'.$event_name.'"';
  709. //For tests
  710. //$sql = 'SELECT user.user_id, user.firstname, user.lastname FROM '.Database::get_main_table(TABLE_MAIN_USER);
  711. $user_list = Database::store_result(Database::query($sql), 'ASSOC');
  712. return json_encode($user_list);
  713. }
  714. function get_events_by_user_and_type($user_id, $event_type)
  715. {
  716. $TABLETRACK_DEFAULT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  717. $user_id = intval($user_id);
  718. $event_type = Database::escape_string($event_type);
  719. $sql = "SELECT * FROM $TABLETRACK_DEFAULT
  720. WHERE default_value_type = 'user_id' AND
  721. default_value = $user_id AND
  722. default_event_type = '$event_type'
  723. ORDER BY default_date ";
  724. $result = Database::query($sql);
  725. if ($result) {
  726. return Database::store_result($result, 'ASSOC');
  727. }
  728. return false;
  729. }
  730. function get_latest_event_by_user_and_type($user_id, $event_type)
  731. {
  732. $result = get_events_by_user_and_type($user_id, $event_type);
  733. if ($result && !empty($result)) {
  734. return $result[0];
  735. }
  736. }
  737. /**
  738. * Save the new message for one event and for one language
  739. *
  740. * @param string $eventName
  741. * @param array $users
  742. * @param string $message
  743. * @param string $subject
  744. * @param string $eventMessageLanguage
  745. * @param int $activated
  746. */
  747. function save_event_type_message($event_name, $users, $message, $subject, $event_message_language, $activated)
  748. {
  749. $event_name = Database::escape_string($event_name);
  750. $activated = intval($activated);
  751. $event_message_language = Database::escape_string($event_message_language);
  752. // Deletes then re-adds the users linked to the event
  753. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_name = "'.$event_name.'" ';
  754. Database::query($sql);
  755. foreach ($users as $user) {
  756. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_name) VALUES('.intval($user).',"'.$event_name.'")';
  757. Database::query($sql);
  758. }
  759. $language_id = api_get_language_id($event_message_language);
  760. // check if this template in this language already exists or not
  761. $sql = 'SELECT COUNT(id) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  762. WHERE event_type_name = "'.$event_name.'" AND language_id = '.$language_id;
  763. $sql = Database::store_result(Database::query($sql), 'ASSOC');
  764. // if already exists, we update
  765. if ($sql[0]["total"] > 0) {
  766. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  767. SET message = "'.Database::escape_string($message).'",
  768. subject = "'.Database::escape_string($subject).'",
  769. activated = '.$activated.'
  770. WHERE event_type_name = "'.$event_name.'" AND language_id = (SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  771. WHERE dokeos_folder = "'.$event_message_language.'")';
  772. Database::query($sql);
  773. } else { // else we create a new record
  774. // gets the language_-_id
  775. $lang_id = '(SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  776. WHERE dokeos_folder = "'.$event_message_language.'")';
  777. $lang_id = Database::store_result(Database::query($lang_id), 'ASSOC');
  778. if (!empty($lang_id[0]["id"])) {
  779. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' (event_type_name, language_id, message, subject, activated)
  780. VALUES("'.$event_name.'", '.$lang_id[0]["id"].', "'.Database::escape_string($message).'", "'.Database::escape_string($subject).'", '.$activated.')';
  781. Database::query($sql);
  782. }
  783. }
  784. // set activated at every save
  785. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  786. SET activated = '.$activated.'
  787. WHERE event_type_name = "'.$event_name.'"';
  788. Database::query($sql);
  789. }
  790. function eventType_mod($etId, $users, $message, $subject)
  791. {
  792. $etId = intval($etId);
  793. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_id = '.$etId.' ';
  794. Database::query($sql);
  795. foreach ($users as $user) {
  796. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_id)
  797. VALUES('.intval($user).','.$etId.') ';
  798. Database::query($sql);
  799. }
  800. $sql = 'UPDATE '.Database::get_main_table(TABLE_MAIN_EVENT_TYPE_MESSAGE).'
  801. SET message = "'.Database::escape_string($message).'",
  802. subject = "'.Database::escape_string($subject).'"
  803. WHERE event_type_id = '.$etId.'';
  804. Database::query($sql);
  805. }
  806. /**
  807. * Gets the last attempt of an exercise based in the exe_id
  808. * @param int $exe_id
  809. * @return mixed
  810. */
  811. function getLastAttemptDateOfExercise($exe_id)
  812. {
  813. $exe_id = intval($exe_id);
  814. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  815. $sql_track_attempt = 'SELECT max(tms) as last_attempt_date FROM '.$track_attempts.' WHERE exe_id='.$exe_id;
  816. $rs_last_attempt = Database::query($sql_track_attempt);
  817. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  818. $last_attempt_date = $row_last_attempt['last_attempt_date']; //Get the date of last attempt
  819. return $last_attempt_date;
  820. }
  821. /**
  822. * Gets the last attempt of an exercise based in the exe_id
  823. * @param int $exe_id
  824. * @return mixed
  825. */
  826. function getLatestQuestionIdFromAttempt($exe_id)
  827. {
  828. $exe_id = intval($exe_id);
  829. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  830. $sql = 'SELECT question_id FROM '.$track_attempts.' WHERE exe_id='.$exe_id.' ORDER BY tms DESC LIMIT 1';
  831. $result = Database::query($sql);
  832. if (Database::num_rows($result)) {
  833. $row = Database::fetch_array($result);
  834. return $row['question_id'];
  835. } else {
  836. return false;
  837. }
  838. }
  839. /**
  840. * Gets how many attempts exists by user, exercise, learning path
  841. * @param int user id
  842. * @param int exercise id
  843. * @param int lp id
  844. * @param int lp item id
  845. * @param int lp item view id
  846. */
  847. function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id, $lp_item_view_id)
  848. {
  849. $stat_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  850. $user_id = intval($user_id);
  851. $exerciseId = intval($exerciseId);
  852. $lp_id = intval($lp_id);
  853. $lp_item_id = intval($lp_item_id);
  854. $lp_item_view_id = intval($lp_item_view_id);
  855. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  856. exe_exo_id = $exerciseId AND
  857. exe_user_id = $user_id AND
  858. status != 'incomplete' AND
  859. orig_lp_id = $lp_id AND
  860. orig_lp_item_id = $lp_item_id AND
  861. orig_lp_item_view_id = $lp_item_view_id AND
  862. c_id = '".api_get_course_int_id()."' AND
  863. session_id = '".api_get_session_id()."'";
  864. $query = Database::query($sql);
  865. if (Database::num_rows($query) > 0) {
  866. $attempt = Database :: fetch_array($query, 'ASSOC');
  867. return $attempt['count'];
  868. } else {
  869. return 0;
  870. }
  871. }
  872. function get_attempt_count_not_finished($user_id, $exerciseId, $lp_id, $lp_item_id)
  873. {
  874. $stat_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  875. $user_id = intval($user_id);
  876. $exerciseId = intval($exerciseId);
  877. $lp_id = intval($lp_id);
  878. $lp_item_id = intval($lp_item_id);
  879. //$lp_item_view_id = intval($lp_item_view_id);
  880. $sql = "SELECT count(*) as count FROM $stat_table WHERE
  881. exe_exo_id = $exerciseId AND
  882. exe_user_id = $user_id AND
  883. status != 'incomplete' AND
  884. orig_lp_id = $lp_id AND
  885. orig_lp_item_id = $lp_item_id AND
  886. c_id = '".api_get_course_int_id()."' AND
  887. session_id = '".api_get_session_id()."'";
  888. $query = Database::query($sql);
  889. if (Database::num_rows($query) > 0) {
  890. $attempt = Database :: fetch_array($query, 'ASSOC');
  891. return $attempt['count'];
  892. } else {
  893. return 0;
  894. }
  895. }
  896. /**
  897. * @param int $user_id
  898. * @param int $lp_id
  899. * @param array $course
  900. * @param int $session_id
  901. */
  902. function delete_student_lp_events($user_id, $lp_id, $course, $session_id)
  903. {
  904. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  905. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  906. $course_id = $course['real_id'];
  907. if (empty($course_id)) {
  908. $course_id = api_get_course_int_id();
  909. }
  910. $track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  911. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  912. $recording_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  913. $user_id = intval($user_id);
  914. $lp_id = intval($lp_id);
  915. $session_id = intval($session_id);
  916. //Make sure we have the exact lp_view_id
  917. $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 ";
  918. $result = Database::query($sql);
  919. if (Database::num_rows($result)) {
  920. $view = Database::fetch_array($result, 'ASSOC');
  921. $lp_view_id = $view['id'];
  922. $sql = "DELETE FROM $lp_item_view_table WHERE c_id = $course_id AND lp_view_id = $lp_view_id ";
  923. Database::query($sql);
  924. }
  925. $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 ";
  926. Database::query($sql);
  927. $sql = "SELECT exe_id FROM $track_e_exercises
  928. WHERE exe_user_id = $user_id AND
  929. session_id = $session_id AND
  930. c_id = $course_id AND
  931. orig_lp_id = $lp_id";
  932. $result = Database::query($sql);
  933. $exe_list = array();
  934. while ($row = Database::fetch_array($result, 'ASSOC')) {
  935. $exe_list[] = $row['exe_id'];
  936. }
  937. if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) {
  938. $sql_delete = "DELETE FROM $track_e_exercises WHERE exe_id IN (".implode(',', $exe_list).")";
  939. Database::query($sql_delete);
  940. $sql_delete = "DELETE FROM $track_attempts WHERE exe_id IN (".implode(',', $exe_list).")";
  941. Database::query($sql_delete);
  942. $sql_delete = "DELETE FROM $recording_table WHERE exe_id IN (".implode(',', $exe_list).")";
  943. Database::query($sql_delete);
  944. }
  945. }
  946. /**
  947. * Delete all exercise attempts (included in LP or not)
  948. *
  949. * @param int user id
  950. * @param int exercise id
  951. * @param string course code
  952. * @param int session id
  953. */
  954. function delete_all_incomplete_attempts($user_id, $exercise_id, $course_id, $session_id = 0)
  955. {
  956. $track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  957. $user_id = intval($user_id);
  958. $exercise_id = intval($exercise_id);
  959. $course_id = intval($course_id);
  960. $session_id = intval($session_id);
  961. if (!empty($user_id) && !empty($exercise_id) && !empty($course_code)) {
  962. $sql = "DELETE FROM $track_e_exercises
  963. WHERE exe_user_id = $user_id AND
  964. exe_exo_id = $exercise_id AND
  965. c_id = '$course_id' AND
  966. session_id = $session_id AND
  967. status = 'incomplete' ";
  968. Database::query($sql);
  969. }
  970. }
  971. /**
  972. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  973. * @param int exercise id
  974. * @param string course code
  975. * @param int session id
  976. * @return array with the results
  977. *
  978. */
  979. function get_all_exercise_results($exercise_id, $courseId, $session_id = 0, $load_question_list = true, $user_id = null)
  980. {
  981. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  982. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  983. $courseId = intval($courseId);
  984. $exercise_id = intval($exercise_id);
  985. $session_id = intval($session_id);
  986. $user_condition = null;
  987. if (!empty($user_id)) {
  988. $user_id = intval($user_id);
  989. $user_condition = "AND exe_user_id = $user_id ";
  990. }
  991. $sql = "SELECT * FROM $TABLETRACK_EXERCICES
  992. WHERE status = '' AND
  993. c_id = '$courseId' AND
  994. exe_exo_id = '$exercise_id' AND
  995. session_id = $session_id AND
  996. orig_lp_id =0 AND
  997. orig_lp_item_id = 0
  998. $user_condition
  999. ORDER BY exe_id";
  1000. $res = Database::query($sql);
  1001. $list = array();
  1002. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1003. $list[$row['exe_id']] = $row;
  1004. if ($load_question_list) {
  1005. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT WHERE exe_id = {$row['exe_id']}";
  1006. $res_question = Database::query($sql);
  1007. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1008. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1009. }
  1010. }
  1011. }
  1012. return $list;
  1013. }
  1014. /**
  1015. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  1016. * @param string course code
  1017. * @param int session id
  1018. * @return array with the results
  1019. *
  1020. */
  1021. function get_all_exercise_results_by_course($courseId, $session_id = 0, $get_count = true)
  1022. {
  1023. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1024. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1025. $courseId = intval($courseId);
  1026. $session_id = intval($session_id);
  1027. $select = '*';
  1028. if ($get_count) {
  1029. $select = 'count(*) as count';
  1030. }
  1031. $sql = "SELECT $select FROM $table_track_exercises
  1032. WHERE status = '' AND
  1033. c_id = '$courseId' AND
  1034. session_id = $session_id AND
  1035. orig_lp_id = 0 AND
  1036. orig_lp_item_id = 0
  1037. ORDER BY exe_id";
  1038. $res = Database::query($sql);
  1039. if ($get_count) {
  1040. $row = Database::fetch_array($res, 'ASSOC');
  1041. return $row['count'];
  1042. } else {
  1043. $list = array();
  1044. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1045. $list[$row['exe_id']] = $row;
  1046. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1047. $res_question = Database::query($sql);
  1048. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1049. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1050. }
  1051. }
  1052. return $list;
  1053. }
  1054. }
  1055. /**
  1056. * Gets all exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1057. * @param int exercise id
  1058. * @param string course code
  1059. * @param int session id
  1060. * @return array with the results
  1061. *
  1062. */
  1063. function get_all_exercise_results_by_user($user_id, $courseId, $session_id = 0)
  1064. {
  1065. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1066. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1067. $courseId = intval($courseId);
  1068. $session_id = intval($session_id);
  1069. $user_id = intval($user_id);
  1070. $sql = "SELECT * FROM $table_track_exercises
  1071. WHERE status = '' AND
  1072. exe_user_id = $user_id AND
  1073. c_id = '$courseId' AND
  1074. session_id = $session_id AND
  1075. orig_lp_id = 0 AND
  1076. orig_lp_item_id = 0
  1077. ORDER by exe_id";
  1078. $res = Database::query($sql);
  1079. $list = array();
  1080. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1081. $list[$row['exe_id']] = $row;
  1082. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1083. $res_question = Database::query($sql);
  1084. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1085. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1086. }
  1087. }
  1088. //echo '<pre>'; print_r($list);
  1089. return $list;
  1090. }
  1091. /**
  1092. * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1093. * @param int exercise id
  1094. * @param string course code
  1095. * @param int session id
  1096. * @return array with the results
  1097. *
  1098. */
  1099. function get_exercise_results_by_attempt($exe_id, $status = null)
  1100. {
  1101. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1102. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1103. $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1104. $exe_id = intval($exe_id);
  1105. $status = Database::escape_string($status);
  1106. $sql = "SELECT * FROM $table_track_exercises WHERE status = '".$status."' AND exe_id = $exe_id";
  1107. $res = Database::query($sql);
  1108. $list = array();
  1109. if (Database::num_rows($res)) {
  1110. $row = Database::fetch_array($res, 'ASSOC');
  1111. //Checking if this attempt was revised by a teacher
  1112. $sql_revised = 'SELECT exe_id FROM '.$table_track_attempt_recording.' WHERE author != "" AND exe_id = '.$exe_id.' LIMIT 1';
  1113. $res_revised = Database::query($sql_revised);
  1114. $row['attempt_revised'] = 0;
  1115. if (Database::num_rows($res_revised) > 0) {
  1116. $row['attempt_revised'] = 1;
  1117. }
  1118. $list[$exe_id] = $row;
  1119. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY tms ASC";
  1120. $res_question = Database::query($sql);
  1121. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1122. $list[$exe_id]['question_list'][$row_q['question_id']] = $row_q;
  1123. }
  1124. }
  1125. return $list;
  1126. }
  1127. /**
  1128. * Gets exercise results (NO Exercises in LPs) from a given user, exercise id, course, session, lp_id, lp_item_id
  1129. * @param int user id
  1130. * @param int exercise id
  1131. * @param string course code
  1132. * @param int session id
  1133. * @param int lp id
  1134. * @param int lp item id
  1135. * @param string order asc or desc
  1136. * @return array with the results
  1137. *
  1138. */
  1139. function getExerciseResultsByUser($user_id, $exercise_id, $courseId, $session_id = 0, $lp_id = 0, $lp_item_id = 0, $order = null)
  1140. {
  1141. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1142. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1143. $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1144. $courseId = intval($courseId);
  1145. $exercise_id = intval($exercise_id);
  1146. $session_id = intval($session_id);
  1147. $user_id = intval($user_id);
  1148. $lp_id = intval($lp_id);
  1149. $lp_item_id = intval($lp_item_id);
  1150. if (!in_array(strtolower($order), array('asc', 'desc'))) {
  1151. $order = 'asc';
  1152. }
  1153. $sql = "SELECT * FROM $table_track_exercises
  1154. WHERE status = '' AND
  1155. exe_user_id = $user_id AND
  1156. c_id = $courseId AND
  1157. exe_exo_id = $exercise_id AND
  1158. session_id = $session_id AND
  1159. orig_lp_id = $lp_id AND
  1160. orig_lp_item_id = $lp_item_id
  1161. ORDER by exe_id $order ";
  1162. $res = Database::query($sql);
  1163. $list = array();
  1164. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1165. //Checking if this attempt was revised by a teacher
  1166. $sql_revised = 'SELECT exe_id FROM '.$table_track_attempt_recording.' WHERE author != "" AND exe_id = '.$row['exe_id'].' LIMIT 1';
  1167. $res_revised = Database::query($sql_revised);
  1168. $row['attempt_revised'] = 0;
  1169. if (Database::num_rows($res_revised) > 0) {
  1170. $row['attempt_revised'] = 1;
  1171. }
  1172. $list[$row['exe_id']] = $row;
  1173. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1174. $res_question = Database::query($sql);
  1175. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1176. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1177. }
  1178. }
  1179. return $list;
  1180. }
  1181. /**
  1182. * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session
  1183. * @param int exercise id
  1184. * @param int course id
  1185. * @param int session id
  1186. * @return array with the results
  1187. *
  1188. */
  1189. function count_exercise_attempts_by_user($user_id, $exercise_id, $courseId, $session_id = 0)
  1190. {
  1191. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1192. $courseId = intval($courseId);
  1193. $exercise_id = intval($exercise_id);
  1194. $session_id = intval($session_id);
  1195. $user_id = intval($user_id);
  1196. $sql = "SELECT count(*) as count FROM $TABLETRACK_EXERCICES
  1197. WHERE status = '' AND
  1198. exe_user_id = '$user_id' AND
  1199. c_id = '$courseId' AND
  1200. exe_exo_id = '$exercise_id' AND
  1201. session_id = $session_id AND
  1202. orig_lp_id =0 AND
  1203. orig_lp_item_id = 0
  1204. ORDER BY exe_id";
  1205. $res = Database::query($sql);
  1206. $result = 0;
  1207. if (Database::num_rows($res) > 0) {
  1208. $row = Database::fetch_array($res, 'ASSOC');
  1209. $result = $row['count'];
  1210. }
  1211. return $result;
  1212. }
  1213. /**
  1214. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1215. * @param int exercise id
  1216. * @param int course id
  1217. * @param int session id
  1218. * @return array with the results
  1219. * @todo rename this function
  1220. *
  1221. */
  1222. function get_best_exercise_results_by_user($exercise_id, $courseId, $session_id = 0)
  1223. {
  1224. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1225. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1226. $courseId = intval($courseId);
  1227. $exercise_id = intval($exercise_id);
  1228. $session_id = intval($session_id);
  1229. $sql = "SELECT * FROM $table_track_exercises
  1230. WHERE status = '' AND
  1231. c_id = $courseId AND
  1232. exe_exo_id = '$exercise_id' AND
  1233. session_id = $session_id AND
  1234. orig_lp_id = 0 AND
  1235. orig_lp_item_id = 0
  1236. ORDER BY exe_id";
  1237. $res = Database::query($sql);
  1238. $list = array();
  1239. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1240. $list[$row['exe_id']] = $row;
  1241. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1242. $res_question = Database::query($sql);
  1243. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1244. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1245. }
  1246. }
  1247. //Getting the best results of every student
  1248. $best_score_return = array();
  1249. foreach ($list as $student_result) {
  1250. $user_id = $student_result['exe_user_id'];
  1251. $current_best_score[$user_id] = $student_result['exe_result'];
  1252. 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']) {
  1253. $best_score_return[$user_id] = $student_result;
  1254. }
  1255. }
  1256. return $best_score_return;
  1257. }
  1258. /**
  1259. * @param int $user_id
  1260. * @param int $exercise_id
  1261. * @param int $courseId
  1262. * @param int $session_id
  1263. * @return array
  1264. */
  1265. function get_best_attempt_exercise_results_per_user($user_id, $exercise_id, $courseId, $session_id = 0)
  1266. {
  1267. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1268. $courseId = intval($courseId);
  1269. $exercise_id = intval($exercise_id);
  1270. $session_id = intval($session_id);
  1271. $user_id = intval($user_id);
  1272. $sql = "SELECT * FROM $table_track_exercises
  1273. WHERE status = '' AND
  1274. c_id = '$courseId' AND
  1275. exe_exo_id = '$exercise_id' AND
  1276. session_id = $session_id AND
  1277. exe_user_id = $user_id AND
  1278. orig_lp_id =0 AND
  1279. orig_lp_item_id = 0
  1280. ORDER BY exe_id";
  1281. $res = Database::query($sql);
  1282. $list = array();
  1283. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1284. $list[$row['exe_id']] = $row; /*
  1285. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1286. $res_question = Database::query($sql);
  1287. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1288. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1289. } */
  1290. }
  1291. //Getting the best results of every student
  1292. $best_score_return = array();
  1293. $best_score_return['exe_result'] = 0;
  1294. foreach ($list as $result) {
  1295. $current_best_score = $result;
  1296. if ($current_best_score['exe_result'] > $best_score_return['exe_result']) {
  1297. $best_score_return = $result;
  1298. }
  1299. }
  1300. if (!isset($best_score_return['exe_weighting'])) {
  1301. $best_score_return = array();
  1302. }
  1303. return $best_score_return;
  1304. }
  1305. /**
  1306. * @param int $exercise_id
  1307. * @param int $courseId
  1308. * @param int $session_id
  1309. * @return mixed
  1310. */
  1311. function count_exercise_result_not_validated($exercise_id, $courseId, $session_id = 0)
  1312. {
  1313. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1314. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1315. $courseId = intval($courseId);
  1316. $session_id = intval($session_id);
  1317. $exercise_id = intval($exercise_id);
  1318. $sql = "SELECT count(e.exe_id) as count
  1319. FROM $table_track_exercises e LEFT JOIN $table_track_attempt a ON e.exe_id = a.exe_id
  1320. WHERE exe_exo_id = $exercise_id AND
  1321. c_id = '$courseId' AND
  1322. e.session_id = $session_id AND
  1323. orig_lp_id = 0 AND
  1324. marks IS NULL AND
  1325. status = '' AND
  1326. orig_lp_item_id = 0 ORDER BY e.exe_id";
  1327. $res = Database::query($sql);
  1328. $row = Database::fetch_array($res, 'ASSOC');
  1329. return $row['count'];
  1330. }
  1331. /**
  1332. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1333. * @param int exercise id
  1334. * @param int course id
  1335. * @param int session id
  1336. * @return array with the results
  1337. *
  1338. */
  1339. function get_count_exercises_attempted_by_course($courseId, $session_id = 0)
  1340. {
  1341. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1342. $courseId = intval($courseId);
  1343. $session_id = intval($session_id);
  1344. $sql = "SELECT DISTINCT exe_exo_id, exe_user_id
  1345. FROM $table_track_exercises
  1346. WHERE status = '' AND
  1347. c_id = '$courseId' AND
  1348. session_id = $session_id AND
  1349. orig_lp_id =0 AND
  1350. orig_lp_item_id = 0
  1351. ORDER BY exe_id";
  1352. $res = Database::query($sql);
  1353. $count = 0;
  1354. if (Database::num_rows($res) > 0) {
  1355. $count = Database::num_rows($res);
  1356. }
  1357. return $count;
  1358. }
  1359. /**
  1360. * Gets all exercise events from a Learning Path within a Course nd Session
  1361. * @param int exercise id
  1362. * @param string course_code
  1363. * @param int session id
  1364. * @return array
  1365. */
  1366. function get_all_exercise_event_from_lp($exercise_id, $courseId, $session_id = 0)
  1367. {
  1368. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1369. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1370. $courseId = intval($courseId);
  1371. $exercise_id = intval($exercise_id);
  1372. $session_id = intval($session_id);
  1373. $sql = "SELECT * FROM $table_track_exercises
  1374. WHERE status = '' AND
  1375. c_id = $courseId AND
  1376. exe_exo_id = '$exercise_id' AND
  1377. session_id = $session_id AND
  1378. orig_lp_id !=0 AND
  1379. orig_lp_item_id != 0";
  1380. $res = Database::query($sql);
  1381. $list = array();
  1382. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1383. $list[$row['exe_id']] = $row;
  1384. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1385. $res_question = Database::query($sql);
  1386. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1387. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1388. }
  1389. }
  1390. return $list;
  1391. }
  1392. function get_all_exercises_from_lp($lp_id, $course_id)
  1393. {
  1394. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  1395. $course_id = intval($course_id);
  1396. $lp_id = intval($lp_id);
  1397. $sql = "SELECT * FROM $lp_item_table WHERE c_id = $course_id AND lp_id = '".$lp_id."' ORDER BY parent_item_id, display_order";
  1398. $res = Database::query($sql);
  1399. $my_exercise_list = array();
  1400. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1401. if ($row['item_type'] == 'quiz') {
  1402. $my_exercise_list[] = $row;
  1403. }
  1404. }
  1405. return $my_exercise_list;
  1406. }
  1407. /**
  1408. * This function gets the comments of an exercise
  1409. *
  1410. * @param int $id
  1411. * @param int $question_id
  1412. * @return string the comment
  1413. */
  1414. function get_comments($exe_id, $question_id)
  1415. {
  1416. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1417. $sql = "SELECT teacher_comment FROM ".$table_track_attempt."
  1418. WHERE exe_id='".Database::escape_string($exe_id)."' AND question_id = '".Database::escape_string($question_id)."'
  1419. ORDER by question_id";
  1420. $sqlres = Database::query($sql);
  1421. $comm = Database::result($sqlres, 0, "teacher_comment");
  1422. return $comm;
  1423. }
  1424. /**
  1425. * @param int $exe_id
  1426. * @return array
  1427. */
  1428. function getAllExerciseEventByExeId($exe_id)
  1429. {
  1430. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1431. $exe_id = intval($exe_id);
  1432. $list = array();
  1433. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = $exe_id ORDER BY position";
  1434. $res_question = Database::query($sql);
  1435. if (Database::num_rows($res_question)) {
  1436. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1437. $list[$row_q['question_id']][] = $row_q;
  1438. }
  1439. }
  1440. return $list;
  1441. }
  1442. /**
  1443. *
  1444. * @param int $exe_id
  1445. * @param int $user_id
  1446. * @param int $courseId
  1447. * @param int $session_id
  1448. * @param int $question_id
  1449. */
  1450. function delete_attempt($exe_id, $user_id, $courseId, $session_id, $question_id)
  1451. {
  1452. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1453. $exe_id = intval($exe_id);
  1454. $user_id = intval($user_id);
  1455. $courseId = intval($courseId);
  1456. $session_id = intval($session_id);
  1457. $question_id = intval($question_id);
  1458. $sql = "DELETE FROM $table_track_attempt
  1459. WHERE exe_id = $exe_id AND
  1460. user_id = $user_id AND
  1461. c_id = $courseId AND
  1462. session_id = $session_id AND
  1463. question_id = $question_id ";
  1464. Database::query($sql);
  1465. }
  1466. /**
  1467. * @param $exe_id
  1468. * @param $user_id
  1469. * @param int $courseId
  1470. * @param $question_id
  1471. */
  1472. function delete_attempt_hotspot($exe_id, $user_id, $courseId, $question_id)
  1473. {
  1474. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1475. $exe_id = intval($exe_id);
  1476. $user_id = intval($user_id);
  1477. $courseId = intval($courseId);
  1478. $question_id = intval($question_id);
  1479. $sql = "DELETE FROM $table_track_attempt
  1480. WHERE hotspot_exe_id = $exe_id AND
  1481. hotspot_user_id = $user_id AND
  1482. c_id = $courseId AND
  1483. hotspot_question_id = $question_id ";
  1484. Database::query($sql);
  1485. }
  1486. function getAnsweredQuestionsFromAttempt($exe_id, $objExercise)
  1487. {
  1488. $attempt_list = getAllExerciseEventByExeId($exe_id);
  1489. $exercise_result = array();
  1490. if (!empty($attempt_list)) {
  1491. foreach ($attempt_list as $question_id => $options) {
  1492. foreach ($options as $item) {
  1493. $question_obj = Question::read($item['question_id']);
  1494. switch ($question_obj->type) {
  1495. case FILL_IN_BLANKS:
  1496. $item['answer'] = $objExercise->fill_in_blank_answer_to_string($item['answer']);
  1497. break;
  1498. case HOT_SPOT:
  1499. //var_dump($item['answer']);
  1500. break;
  1501. }
  1502. if ($item['answer'] != '0' && !empty($item['answer'])) {
  1503. $exercise_result[] = $question_id;
  1504. break;
  1505. }
  1506. }
  1507. }
  1508. }
  1509. return $exercise_result;
  1510. }
  1511. /**
  1512. * User logs in for the first time to a course
  1513. * @param $course_code
  1514. * @param $user_id
  1515. * @param $session_id
  1516. */
  1517. function event_course_login($courseId, $user_id, $session_id)
  1518. {
  1519. $course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1520. $time = api_get_datetime();
  1521. $courseId = Database::escape_string($courseId);
  1522. $user_id = Database::escape_string($user_id);
  1523. $session_id = Database::escape_string($session_id);
  1524. $sql = "INSERT INTO $course_tracking_table(c_id, user_id, login_course_date, logout_course_date, counter, session_id)
  1525. VALUES('".$courseId."', '".$user_id."', '$time', '$time', '1', '".$session_id."')";
  1526. Database::query($sql);
  1527. //Course catalog stats modifications see #4191
  1528. CourseManager::update_course_ranking(null, null, null, null, true, false);
  1529. }
  1530. /**
  1531. * For the sake of genericity, this function is a switch.
  1532. * It's called by EventsDispatcher and fires the good function
  1533. * with the good require_once.
  1534. *
  1535. * @param string $event_name
  1536. * @param array $params
  1537. */
  1538. function event_send_mail($event_name, $params)
  1539. {
  1540. EventsMail::send_mail($event_name, $params);
  1541. }
  1542. /**
  1543. * Internal function checking if the mail was already sent from that user to that user
  1544. * @param string $event_name
  1545. * @param int $user_from
  1546. * @param int $user_to
  1547. * @return boolean
  1548. */
  1549. function check_if_mail_already_sent($event_name, $user_from, $user_to = null)
  1550. {
  1551. if ($user_to == null) {
  1552. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1553. WHERE user_from = '.$user_from.' AND event_type_name = "'.$event_name.'"';
  1554. } else {
  1555. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1556. WHERE user_from = '.$user_from.' AND user_to = '.$user_to.' AND event_type_name = "'.$event_name.'"';
  1557. }
  1558. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1559. return $result[0]["total"];
  1560. }
  1561. /**
  1562. *
  1563. * Filter EventEmailTemplate Filters see the main/inc/conf/events.conf.dist.php
  1564. *
  1565. */
  1566. /**
  1567. * Basic template event message filter (to be used by other filters as default)
  1568. * @param array $values (passing by reference)
  1569. * @return boolean True if everything is OK, false otherwise
  1570. */
  1571. function _event_send_mail_filter_func(&$values)
  1572. {
  1573. return true;
  1574. }
  1575. /**
  1576. * user_registration - send_mail filter
  1577. * @param array $values (passing by reference)
  1578. * @return boolean True if everything is OK, false otherwise
  1579. */
  1580. function user_registration_event_send_mail_filter_func(&$values)
  1581. {
  1582. $res = _event_send_mail_filter_func($values);
  1583. // proper logic for this filter
  1584. return $res;
  1585. }
  1586. /**
  1587. * portal_homepage_edited - send_mail filter
  1588. * @param array $values (passing by reference)
  1589. * @return boolean True if everything is OK, false otherwise
  1590. */
  1591. function portal_homepage_edited_event_send_mail_filter_func(&$values)
  1592. {
  1593. $res = _event_send_mail_filter_func($values);
  1594. // proper logic for this filter
  1595. return $res;
  1596. }
  1597. }