events.lib.php 68 KB

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