events.lib.php 68 KB

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