events.lib.php 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  1. <?php
  2. /* See license terms in /license.txt */
  3. //use Chamilo\UserBundle\Entity\User;
  4. use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
  5. /**
  6. * Class Event
  7. * Functions of this library are used to record informations when some kind
  8. * of event occur. Each event has his own types of informations then each event
  9. * use its own function.
  10. */
  11. class Event
  12. {
  13. /**
  14. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  15. * @desc Record information for open event (when homepage is opened)
  16. */
  17. public static function event_open()
  18. {
  19. global $_configuration;
  20. global $TABLETRACK_OPEN;
  21. // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information
  22. // $_SERVER['HTTP_USER_AGENT'] : will provide browser and os information
  23. // $_SERVER['HTTP_REFERER'] : provide information about refering url
  24. if (isset($_SERVER['HTT_REFERER'])) {
  25. $referer = Database::escape_string($_SERVER['HTTP_REFERER']);
  26. } else {
  27. $referer = '';
  28. }
  29. // record informations only if user comes from another site
  30. //if(!eregi($_configuration['root_web'],$referer))
  31. $pos = strpos($referer, $_configuration['root_web']);
  32. if ($pos === false && $referer != '') {
  33. $ip = api_get_real_ip();
  34. $remhost = @ getHostByAddr($ip);
  35. if ($remhost == $ip) {
  36. $remhost = "Unknown";
  37. } // don't change this
  38. $reallyNow = api_get_utc_datetime();
  39. $params = [
  40. 'open_remote_host' => Database::escape_string($remhost),
  41. 'open_agent' => $_SERVER['HTTP_USER_AGENT'],
  42. 'open_referer' => $referer,
  43. 'open_date' => $reallyNow,
  44. ];
  45. Database::insert($TABLETRACK_OPEN, $params);
  46. }
  47. return 1;
  48. }
  49. /**
  50. * @author Sebastien Piraux <piraux_seb@hotmail.com> old code
  51. * @author Julio Montoya 2013
  52. * @desc Record information for login event when an user identifies himself with username & password
  53. */
  54. public static function event_login($userId)
  55. {
  56. $userInfo = api_get_user_info($userId);
  57. $userId = intval($userId);
  58. if (empty($userInfo)) {
  59. return false;
  60. }
  61. $TABLETRACK_LOGIN = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  62. $reallyNow = api_get_utc_datetime();
  63. $sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, user_ip, login_date, logout_date) VALUES
  64. ('".$userId."',
  65. '".Database::escape_string(api_get_real_ip())."',
  66. '".$reallyNow."',
  67. '".$reallyNow."'
  68. )";
  69. Database::query($sql);
  70. // Auto subscribe
  71. $user_status = $userInfo['status'] == SESSIONADMIN ? 'sessionadmin' :
  72. $userInfo['status'] == COURSEMANAGER ? 'teacher' :
  73. $userInfo['status'] == DRH ? 'DRH' : 'student';
  74. $autoSubscribe = api_get_setting($user_status.'_autosubscribe');
  75. if ($autoSubscribe) {
  76. $autoSubscribe = explode('|', $autoSubscribe);
  77. foreach ($autoSubscribe as $code) {
  78. if (CourseManager::course_exists($code)) {
  79. CourseManager::subscribe_user($userId, $code);
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * @param tool name of the tool (name in mainDb.accueil table)
  86. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  87. * @desc Record information for access event for courses
  88. */
  89. public static function accessCourse()
  90. {
  91. $TABLETRACK_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  92. //for "what's new" notification
  93. $TABLETRACK_LASTACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  94. $id_session = api_get_session_id();
  95. $now = api_get_utc_datetime();
  96. $courseId = api_get_course_int_id();
  97. $user_id = api_get_user_id();
  98. $ip = Database::escape_string(api_get_real_ip());
  99. if ($user_id) {
  100. $user_id = "'".$user_id."'";
  101. } else {
  102. $user_id = "0"; // no one
  103. }
  104. $sql = "INSERT INTO ".$TABLETRACK_ACCESS." (user_ip, access_user_id, c_id, access_date, access_session_id) VALUES
  105. ('".$ip."', ".$user_id.", '".$courseId."', '".$now."','".$id_session."')";
  106. Database::query($sql);
  107. // added for "what's new" notification
  108. $sql = "UPDATE $TABLETRACK_LASTACCESS SET access_date = '$now'
  109. WHERE access_user_id = $user_id AND c_id = '$courseId' AND access_tool IS NULL AND access_session_id=".$id_session;
  110. $result = Database::query($sql);
  111. if (Database::affected_rows($result) == 0) {
  112. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, c_id, access_date, access_session_id)
  113. VALUES (".$user_id.", '".$courseId."', '$now', '".$id_session."')";
  114. Database::query($sql);
  115. }
  116. return 1;
  117. }
  118. /**
  119. * @param tool name of the tool (name in mainDb.accueil table)
  120. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  121. * @desc Record information for access event for tools
  122. *
  123. * $tool can take this values :
  124. * Links, Calendar, Document, Announcements,
  125. * Group, Video, Works, Users, Exercices, Course Desc
  126. * ...
  127. * Values can be added if new modules are created (15char max)
  128. * I encourage to use $nameTool as $tool when calling this function
  129. *
  130. * Functionality for "what's new" notification is added by Toon Van Hoecke
  131. */
  132. public static function event_access_tool($tool, $id_session = 0)
  133. {
  134. if (empty($tool)) {
  135. return false;
  136. }
  137. $TABLETRACK_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  138. //for "what's new" notification
  139. $TABLETRACK_LASTACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  140. $_course = api_get_course_info();
  141. $courseId = api_get_course_int_id();
  142. $id_session = api_get_session_id();
  143. $tool = Database::escape_string($tool);
  144. $reallyNow = api_get_utc_datetime();
  145. $user_id = api_get_user_id();
  146. // record information
  147. // only if user comes from the course $_cid
  148. //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
  149. //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
  150. $coursePath = isset($_course['path']) ? $_course['path'] : null;
  151. $pos = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$coursePath)) : false;
  152. // added for "what's new" notification
  153. $pos2 = isset($_SERVER['HTTP_REFERER']) ? strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_PATH)."index")) : false;
  154. // end "what's new" notification
  155. if ($pos !== false || $pos2 !== false) {
  156. $params = [
  157. 'access_user_id' => $user_id,
  158. 'c_id' => $courseId,
  159. 'access_tool' => $tool,
  160. 'access_date' => $reallyNow,
  161. 'access_session_id' => $id_session,
  162. 'user_ip' => Database::escape_string(api_get_real_ip())
  163. ];
  164. Database::insert($TABLETRACK_ACCESS, $params);
  165. }
  166. // "what's new" notification
  167. $sql = "UPDATE $TABLETRACK_LASTACCESS
  168. SET access_date = '$reallyNow'
  169. WHERE access_user_id = ".$user_id." AND c_id = '".$courseId."' AND access_tool = '".$tool."' AND access_session_id=".$id_session;
  170. $result = Database::query($sql);
  171. if (Database::affected_rows($result) == 0) {
  172. $sql = "INSERT INTO $TABLETRACK_LASTACCESS (access_user_id, c_id, access_tool, access_date, access_session_id)
  173. VALUES (".$user_id.", '".$courseId."' , '$tool', '$reallyNow', $id_session)";
  174. Database::query($sql);
  175. }
  176. return 1;
  177. }
  178. /**
  179. * @param doc_id id of document (id in mainDb.document table)
  180. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  181. * @desc Record information for download event
  182. * (when an user click to d/l a document)
  183. * it will be used in a redirection page
  184. * bug fixed: Roan Embrechts
  185. * Roan:
  186. * The user id is put in single quotes,
  187. * (why? perhaps to prevent sql insertion hacks?)
  188. * and later again.
  189. * Doing this twice causes an error, I remove one of them.
  190. */
  191. public static function event_download($doc_url)
  192. {
  193. $tbl_stats_downloads = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  194. $doc_url = Database::escape_string($doc_url);
  195. $reallyNow = api_get_utc_datetime();
  196. $user_id = "'".api_get_user_id()."'";
  197. $_cid = api_get_course_int_id();
  198. $sql = "INSERT INTO $tbl_stats_downloads (
  199. down_user_id,
  200. c_id,
  201. down_doc_path,
  202. down_date,
  203. down_session_id
  204. )
  205. VALUES (
  206. ".$user_id.",
  207. '".$_cid."',
  208. '".$doc_url."',
  209. '".$reallyNow."',
  210. '".api_get_session_id()."'
  211. )";
  212. Database::query($sql);
  213. return 1;
  214. }
  215. /**
  216. * @param doc_id id of document (id in mainDb.document table)
  217. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  218. * @desc Record information for upload event
  219. * used in the works tool to record informations when
  220. * an user upload 1 work
  221. */
  222. public static function event_upload($doc_id)
  223. {
  224. $TABLETRACK_UPLOADS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
  225. $courseId = api_get_course_int_id();
  226. $reallyNow = api_get_utc_datetime();
  227. $user_id = api_get_user_id();
  228. $doc_id = intval($doc_id);
  229. $sql = "INSERT INTO ".$TABLETRACK_UPLOADS."
  230. ( upload_user_id,
  231. c_id,
  232. upload_cours_id,
  233. upload_work_id,
  234. upload_date,
  235. upload_session_id
  236. )
  237. VALUES (
  238. ".$user_id.",
  239. '".$courseId."',
  240. '',
  241. '".$doc_id."',
  242. '".$reallyNow."',
  243. '".api_get_session_id()."'
  244. )";
  245. Database::query($sql);
  246. return 1;
  247. }
  248. /**
  249. * @param link_id (id in coursDb liens table)
  250. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  251. * @desc Record information for link event (when an user click on an added link)
  252. * it will be used in a redirection page
  253. */
  254. public static function event_link($link_id)
  255. {
  256. $TABLETRACK_LINKS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS);
  257. $reallyNow = api_get_utc_datetime();
  258. $user_id = api_get_user_id();
  259. $sql = "INSERT INTO ".$TABLETRACK_LINKS."
  260. ( links_user_id,
  261. c_id,
  262. links_link_id,
  263. links_date,
  264. links_session_id
  265. ) VALUES (
  266. ".$user_id.",
  267. '".api_get_course_int_id()."',
  268. '".Database::escape_string($link_id)."',
  269. '".$reallyNow."',
  270. '".api_get_session_id()."'
  271. )";
  272. Database::query($sql);
  273. return 1;
  274. }
  275. /**
  276. * Update the TRACK_E_EXERCICES exercises
  277. *
  278. * @param int exeid id of the attempt
  279. * @param int exo_id exercise id
  280. * @param mixed result score
  281. * @param int weighting ( higher score )
  282. * @param int duration ( duration of the attempt in seconds )
  283. * @param int session_id
  284. * @param int learnpath_id (id of the learnpath)
  285. * @param int learnpath_item_id (id of the learnpath_item)
  286. *
  287. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  288. * @author Julio Montoya Armas <gugli100@gmail.com> Reworked 2010
  289. * @desc Record result of user when an exercice was done
  290. */
  291. public static function update_event_exercice(
  292. $exeid,
  293. $exo_id,
  294. $score,
  295. $weighting,
  296. $session_id,
  297. $learnpath_id = 0,
  298. $learnpath_item_id = 0,
  299. $learnpath_item_view_id = 0,
  300. $duration = 0,
  301. $question_list = array(),
  302. $status = '',
  303. $remind_list = array(),
  304. $end_date = null
  305. ) {
  306. global $debug;
  307. if ($debug) error_log('Called to update_event_exercice');
  308. if ($debug) error_log('duration:' . $duration);
  309. if ($exeid != '') {
  310. /*
  311. * Code commented due BT#8423 do not change the score to 0.
  312. *
  313. * Validation in case of fraud with actived control time
  314. if (!ExerciseLib::exercise_time_control_is_valid($exo_id, $learnpath_id, $learnpath_item_id)) {
  315. $score = 0;
  316. }
  317. */
  318. if (!isset($status) || empty($status)) {
  319. $status = '';
  320. } else {
  321. $status = Database::escape_string($status);
  322. }
  323. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  324. if (!empty($question_list)) {
  325. $question_list = array_map('intval', $question_list);
  326. }
  327. if (!empty($remind_list)) {
  328. $remind_list = array_map('intval', $remind_list);
  329. $remind_list = array_filter($remind_list);
  330. $remind_list = implode(",", $remind_list);
  331. } else {
  332. $remind_list = '';
  333. }
  334. if (empty($end_date)) {
  335. $end_date = api_get_utc_datetime();
  336. }
  337. $sql = "UPDATE $TABLETRACK_EXERCICES SET
  338. exe_exo_id = '".Database::escape_string($exo_id)."',
  339. exe_result = '".Database::escape_string($score)."',
  340. exe_weighting = '".Database::escape_string($weighting)."',
  341. session_id = '".Database::escape_string($session_id)."',
  342. orig_lp_id = '".Database::escape_string($learnpath_id)."',
  343. orig_lp_item_id = '".Database::escape_string($learnpath_item_id)."',
  344. orig_lp_item_view_id = '".Database::escape_string($learnpath_item_view_id)."',
  345. exe_duration = '".Database::escape_string($duration)."',
  346. exe_date = '".$end_date."',
  347. status = '".$status."',
  348. questions_to_check = '".$remind_list."',
  349. data_tracking = '".implode(',', $question_list)."',
  350. user_ip = '" . Database::escape_string(api_get_real_ip()) . "'
  351. WHERE exe_id = '".Database::escape_string($exeid)."'";
  352. $res = Database::query($sql);
  353. if ($debug) error_log('update_event_exercice called');
  354. if ($debug) error_log("$sql");
  355. //Deleting control time session track
  356. //ExerciseLib::exercise_time_control_delete($exo_id);
  357. return $res;
  358. } else {
  359. return false;
  360. }
  361. }
  362. /**
  363. * Record an event for this attempt at answering an exercise
  364. * @param float Score achieved
  365. * @param string Answer given
  366. * @param integer Question ID
  367. * @param integer Exercise attempt ID a.k.a exe_id (from track_e_exercise)
  368. * @param integer Position
  369. * @param integer Exercise ID (from c_quiz)
  370. * @param bool update results?
  371. * @param string Filename (for audio answers - using nanogong)
  372. * @param integer User ID The user who's going to get this score. Default value of null means "get from context".
  373. * @param integer Course ID (from the "id" column of course table). Default value of null means "get from context".
  374. * @param integer Session ID (from the session table). Default value of null means "get from context".
  375. * @param integer Learnpath ID (from c_lp table). Default value of null means "get from context".
  376. * @param integer Learnpath item ID (from the c_lp_item table). Default value of null means "get from context".
  377. * @return boolean Result of the insert query
  378. */
  379. public static function saveQuestionAttempt(
  380. $score,
  381. $answer,
  382. $question_id,
  383. $exe_id,
  384. $position,
  385. $exercise_id = 0,
  386. $updateResults = false,
  387. $nano = null,
  388. $user_id = null,
  389. $course_id = null,
  390. $session_id = null,
  391. $learnpath_id = null,
  392. $learnpath_item_id = null
  393. ) {
  394. global $debug;
  395. $question_id = Database::escape_string($question_id);
  396. $exe_id = Database::escape_string($exe_id);
  397. $position = Database::escape_string($position);
  398. $now = api_get_utc_datetime();
  399. // check user_id or get from context
  400. if (empty($user_id)) {
  401. $user_id = api_get_user_id();
  402. // anonymous
  403. if (empty($user_id)) {
  404. $user_id = api_get_anonymous_id();
  405. }
  406. }
  407. // check course_id or get from context
  408. if (empty($course_id) or intval($course_id) != $course_id) {
  409. $course_id = api_get_course_int_id();
  410. }
  411. // check session_id or get from context
  412. if (empty($session_id)) {
  413. $session_id = api_get_session_id();
  414. }
  415. // check learnpath_id or get from context
  416. if (empty($learnpath_id)) {
  417. global $learnpath_id;
  418. }
  419. // check learnpath_item_id or get from context
  420. if (empty($learnpath_item_id)) {
  421. global $learnpath_item_id;
  422. }
  423. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  424. if ($debug) {
  425. error_log("----- entering saveQuestionAttempt() function ------");
  426. error_log("answer: $answer");
  427. error_log("score: $score");
  428. error_log("question_id : $question_id");
  429. error_log("position: $position");
  430. }
  431. //Validation in case of fraud with active control time
  432. if (!ExerciseLib::exercise_time_control_is_valid($exercise_id, $learnpath_id, $learnpath_item_id)) {
  433. if ($debug) {
  434. error_log("exercise_time_control_is_valid is false");
  435. }
  436. $score = 0;
  437. $answer = 0;
  438. }
  439. $file = '';
  440. if (isset($nano)) {
  441. $file = Database::escape_string(basename($nano->load_filename_if_exists(false)));
  442. }
  443. $session_id = api_get_session_id();
  444. if (!empty($question_id) && !empty($exe_id) && !empty($user_id)) {
  445. $attempt = array(
  446. 'user_id' => $user_id,
  447. 'question_id' => $question_id,
  448. 'answer' => $answer,
  449. 'marks' => $score,
  450. 'c_id' => $course_id,
  451. 'session_id' => $session_id,
  452. 'position' => $position,
  453. 'tms' => $now,
  454. 'filename' => $file,
  455. );
  456. // Check if attempt exists.
  457. $sql = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT
  458. WHERE
  459. c_id = $course_id AND
  460. session_id = $session_id AND
  461. exe_id = $exe_id AND
  462. user_id = $user_id AND
  463. question_id = $question_id AND
  464. position = $position";
  465. $result = Database::query($sql);
  466. if (Database::num_rows($result)) {
  467. if ($debug) {
  468. error_log("Attempt already exist: exe_id: $exe_id - user_id:$user_id - question_id:$question_id");
  469. }
  470. if ($updateResults == false) {
  471. //The attempt already exist do not update use update_event_exercise() instead
  472. return false;
  473. }
  474. } else {
  475. $attempt['exe_id'] = $exe_id;
  476. }
  477. if ($debug) {
  478. error_log("updateResults : $updateResults");
  479. error_log("Saving question attempt: ");
  480. error_log($sql);
  481. }
  482. $recording_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  483. if ($updateResults == false) {
  484. $attempt_id = Database::insert($TBL_TRACK_ATTEMPT, $attempt);
  485. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
  486. if ($debug) {
  487. error_log("Saving e attempt recording ");
  488. }
  489. $attempt_recording = array(
  490. 'exe_id' => $attempt_id,
  491. 'question_id' => $question_id,
  492. 'marks' => $score,
  493. 'insert_date' => $now,
  494. 'author' => '',
  495. 'session_id' => $session_id,
  496. );
  497. Database::insert($recording_table, $attempt_recording);
  498. }
  499. } else {
  500. Database::update($TBL_TRACK_ATTEMPT, $attempt,
  501. array('exe_id = ? AND question_id = ? AND user_id = ? ' => array($exe_id, $question_id, $user_id)));
  502. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
  503. $attempt_recording = array(
  504. 'exe_id' => $exe_id,
  505. 'question_id' => $question_id,
  506. 'marks' => $score,
  507. 'insert_date' => $now,
  508. 'author' => '',
  509. 'session_id' => $session_id,
  510. );
  511. Database::update($recording_table, $attempt_recording,
  512. array('exe_id = ? AND question_id = ? AND session_id = ? ' => array($exe_id, $question_id, $session_id))
  513. );
  514. }
  515. $attempt_id = $exe_id;
  516. }
  517. return $attempt_id;
  518. } else {
  519. return false;
  520. }
  521. }
  522. /**
  523. * Record an hotspot spot for this attempt at answering an hotspot question
  524. * @param int Exercise ID
  525. * @param int Question ID
  526. * @param int Answer ID
  527. * @param int Whether this answer is correct (1) or not (0)
  528. * @param string Coordinates of this point (e.g. 123;324)
  529. * @param bool update results?
  530. * @return boolean Result of the insert query
  531. * @uses Course code and user_id from global scope $_cid and $_user
  532. */
  533. public static function saveExerciseAttemptHotspot($exe_id, $question_id, $answer_id, $correct, $coords, $updateResults = false, $exerciseId = 0)
  534. {
  535. global $safe_lp_id, $safe_lp_item_id;
  536. if ($updateResults == false) {
  537. // Validation in case of fraud with activated control time
  538. if (!ExerciseLib::exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
  539. $correct = 0;
  540. }
  541. }
  542. $tbl_track_e_hotspot = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  543. if ($updateResults) {
  544. $params = array(
  545. 'hotspot_correct' => $correct,
  546. 'hotspot_coordinate' => $coords
  547. );
  548. Database::update($tbl_track_e_hotspot, $params,
  549. array('hotspot_user_id = ? AND hotspot_exe_id = ? AND hotspot_question_id = ? AND hotspot_answer_id = ? ' => array(
  550. api_get_user_id(),
  551. $exe_id,
  552. $question_id,
  553. $answer_id,
  554. $answer_id
  555. )));
  556. } else {
  557. return Database::insert(
  558. $tbl_track_e_hotspot,
  559. [
  560. 'hotspot_course_code' => api_get_course_id(),
  561. 'hotspot_user_id' => api_get_user_id(),
  562. 'c_id' => api_get_course_int_id(),
  563. 'hotspot_exe_id' => $exe_id,
  564. 'hotspot_question_id' => $question_id,
  565. 'hotspot_answer_id' => $answer_id,
  566. 'hotspot_correct' => $correct,
  567. 'hotspot_coordinate' => $coords
  568. ]
  569. );
  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['last_login']);
  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. return $list;
  1116. }
  1117. /**
  1118. * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1119. * @param int exercise id
  1120. * @param string course code
  1121. * @param int session id
  1122. * @return array with the results
  1123. *
  1124. */
  1125. public static function get_exercise_results_by_attempt($exe_id, $status = null)
  1126. {
  1127. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1128. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1129. $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1130. $exe_id = intval($exe_id);
  1131. $status = Database::escape_string($status);
  1132. $sql = "SELECT * FROM $table_track_exercises
  1133. WHERE status = '".$status."' AND exe_id = $exe_id";
  1134. $res = Database::query($sql);
  1135. $list = array();
  1136. if (Database::num_rows($res)) {
  1137. $row = Database::fetch_array($res, 'ASSOC');
  1138. //Checking if this attempt was revised by a teacher
  1139. $sql_revised = 'SELECT exe_id FROM '.$table_track_attempt_recording.'
  1140. WHERE author != "" AND exe_id = '.$exe_id.' LIMIT 1';
  1141. $res_revised = Database::query($sql_revised);
  1142. $row['attempt_revised'] = 0;
  1143. if (Database::num_rows($res_revised) > 0) {
  1144. $row['attempt_revised'] = 1;
  1145. }
  1146. $list[$exe_id] = $row;
  1147. $sql = "SELECT * FROM $table_track_attempt
  1148. WHERE exe_id = $exe_id ORDER BY tms ASC";
  1149. $res_question = Database::query($sql);
  1150. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1151. $list[$exe_id]['question_list'][$row_q['question_id']] = $row_q;
  1152. }
  1153. }
  1154. return $list;
  1155. }
  1156. /**
  1157. * Gets exercise results (NO Exercises in LPs) from a given user, exercise id, course, session, lp_id, lp_item_id
  1158. * @param int user id
  1159. * @param int exercise id
  1160. * @param string course code
  1161. * @param int session id
  1162. * @param int lp id
  1163. * @param int lp item id
  1164. * @param string order asc or desc
  1165. * @return array with the results
  1166. *
  1167. */
  1168. public static function getExerciseResultsByUser($user_id, $exercise_id, $courseId, $session_id = 0, $lp_id = 0, $lp_item_id = 0, $order = null)
  1169. {
  1170. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1171. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1172. $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1173. $courseId = intval($courseId);
  1174. $exercise_id = intval($exercise_id);
  1175. $session_id = intval($session_id);
  1176. $user_id = intval($user_id);
  1177. $lp_id = intval($lp_id);
  1178. $lp_item_id = intval($lp_item_id);
  1179. if (!in_array(strtolower($order), array('asc', 'desc'))) {
  1180. $order = 'asc';
  1181. }
  1182. $sql = "SELECT * FROM $table_track_exercises
  1183. WHERE
  1184. status = '' AND
  1185. exe_user_id = $user_id AND
  1186. c_id = $courseId AND
  1187. exe_exo_id = $exercise_id AND
  1188. session_id = $session_id AND
  1189. orig_lp_id = $lp_id AND
  1190. orig_lp_item_id = $lp_item_id
  1191. ORDER by exe_id $order ";
  1192. $res = Database::query($sql);
  1193. $list = array();
  1194. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1195. //Checking if this attempt was revised by a teacher
  1196. $sql = 'SELECT exe_id FROM '.$table_track_attempt_recording.'
  1197. WHERE author != "" AND exe_id = '.$row['exe_id'].'
  1198. LIMIT 1';
  1199. $res_revised = Database::query($sql);
  1200. $row['attempt_revised'] = 0;
  1201. if (Database::num_rows($res_revised) > 0) {
  1202. $row['attempt_revised'] = 1;
  1203. }
  1204. $list[$row['exe_id']] = $row;
  1205. $sql = "SELECT * FROM $table_track_attempt
  1206. WHERE exe_id = {$row['exe_id']}";
  1207. $res_question = Database::query($sql);
  1208. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1209. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1210. }
  1211. }
  1212. return $list;
  1213. }
  1214. /**
  1215. * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session
  1216. * @param int $user_id
  1217. * @param int exercise id
  1218. * @param int $courseId
  1219. * @param int session id
  1220. * @return array with the results
  1221. *
  1222. */
  1223. public static function count_exercise_attempts_by_user($user_id, $exercise_id, $courseId, $session_id = 0)
  1224. {
  1225. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1226. $courseId = intval($courseId);
  1227. $exercise_id = intval($exercise_id);
  1228. $session_id = intval($session_id);
  1229. $user_id = intval($user_id);
  1230. $sql = "SELECT count(*) as count FROM $TABLETRACK_EXERCICES
  1231. WHERE status = '' AND
  1232. exe_user_id = '$user_id' AND
  1233. c_id = '$courseId' AND
  1234. exe_exo_id = '$exercise_id' AND
  1235. session_id = $session_id AND
  1236. orig_lp_id =0 AND
  1237. orig_lp_item_id = 0
  1238. ORDER BY exe_id";
  1239. $res = Database::query($sql);
  1240. $result = 0;
  1241. if (Database::num_rows($res) > 0) {
  1242. $row = Database::fetch_array($res, 'ASSOC');
  1243. $result = $row['count'];
  1244. }
  1245. return $result;
  1246. }
  1247. /**
  1248. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1249. * @param int $exercise_id
  1250. * @param int $courseId
  1251. * @param int $session_id
  1252. * @return array with the results
  1253. * @todo rename this function
  1254. */
  1255. public static function get_best_exercise_results_by_user($exercise_id, $courseId, $session_id = 0)
  1256. {
  1257. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1258. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1259. $courseId = intval($courseId);
  1260. $exercise_id = intval($exercise_id);
  1261. $session_id = intval($session_id);
  1262. $sql = "SELECT * FROM $table_track_exercises
  1263. WHERE
  1264. status = '' AND
  1265. c_id = $courseId AND
  1266. exe_exo_id = '$exercise_id' AND
  1267. session_id = $session_id AND
  1268. orig_lp_id = 0 AND
  1269. orig_lp_item_id = 0
  1270. ORDER BY exe_id";
  1271. $res = Database::query($sql);
  1272. $list = array();
  1273. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1274. $list[$row['exe_id']] = $row;
  1275. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1276. $res_question = Database::query($sql);
  1277. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1278. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1279. }
  1280. }
  1281. // Getting the best results of every student
  1282. $best_score_return = array();
  1283. foreach ($list as $student_result) {
  1284. $user_id = $student_result['exe_user_id'];
  1285. $current_best_score[$user_id] = $student_result['exe_result'];
  1286. //echo $current_best_score[$user_id].' - '.$best_score_return[$user_id]['exe_result'].'<br />';
  1287. if (!isset($best_score_return[$user_id]['exe_result'])) {
  1288. $best_score_return[$user_id] = $student_result;
  1289. }
  1290. if ($current_best_score[$user_id] > $best_score_return[$user_id]['exe_result']) {
  1291. $best_score_return[$user_id] = $student_result;
  1292. }
  1293. }
  1294. return $best_score_return;
  1295. }
  1296. /**
  1297. * @param int $user_id
  1298. * @param int $exercise_id
  1299. * @param int $courseId
  1300. * @param int $session_id
  1301. * @return array
  1302. */
  1303. public static function get_best_attempt_exercise_results_per_user($user_id, $exercise_id, $courseId, $session_id = 0)
  1304. {
  1305. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1306. $courseId = intval($courseId);
  1307. $exercise_id = intval($exercise_id);
  1308. $session_id = intval($session_id);
  1309. $user_id = intval($user_id);
  1310. $sql = "SELECT * FROM $table_track_exercises
  1311. WHERE
  1312. status = '' AND
  1313. c_id = '$courseId' AND
  1314. exe_exo_id = '$exercise_id' AND
  1315. session_id = $session_id AND
  1316. exe_user_id = $user_id AND
  1317. orig_lp_id =0 AND
  1318. orig_lp_item_id = 0
  1319. ORDER BY exe_id";
  1320. $res = Database::query($sql);
  1321. $list = array();
  1322. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1323. $list[$row['exe_id']] = $row; /*
  1324. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1325. $res_question = Database::query($sql);
  1326. while($row_q = Database::fetch_array($res_question,'ASSOC')) {
  1327. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1328. } */
  1329. }
  1330. //Getting the best results of every student
  1331. $best_score_return = array();
  1332. $best_score_return['exe_result'] = 0;
  1333. foreach ($list as $result) {
  1334. $current_best_score = $result;
  1335. if ($current_best_score['exe_result'] > $best_score_return['exe_result']) {
  1336. $best_score_return = $result;
  1337. }
  1338. }
  1339. if (!isset($best_score_return['exe_weighting'])) {
  1340. $best_score_return = array();
  1341. }
  1342. return $best_score_return;
  1343. }
  1344. /**
  1345. * @param int $exercise_id
  1346. * @param int $courseId
  1347. * @param int $session_id
  1348. * @return mixed
  1349. */
  1350. public static function count_exercise_result_not_validated($exercise_id, $courseId, $session_id = 0)
  1351. {
  1352. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1353. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1354. $courseId = intval($courseId);
  1355. $session_id = intval($session_id);
  1356. $exercise_id = intval($exercise_id);
  1357. $sql = "SELECT count(e.exe_id) as count
  1358. FROM $table_track_exercises e
  1359. LEFT JOIN $table_track_attempt a
  1360. ON e.exe_id = a.exe_id
  1361. WHERE
  1362. exe_exo_id = $exercise_id AND
  1363. c_id = '$courseId' AND
  1364. e.session_id = $session_id AND
  1365. orig_lp_id = 0 AND
  1366. marks IS NULL AND
  1367. status = '' AND
  1368. orig_lp_item_id = 0
  1369. ORDER BY e.exe_id";
  1370. $res = Database::query($sql);
  1371. $row = Database::fetch_array($res, 'ASSOC');
  1372. return $row['count'];
  1373. }
  1374. /**
  1375. * Gets all exercise BEST results attempts (NO Exercises in LPs ) from a given exercise id, course, session per user
  1376. * @param int exercise id
  1377. * @param int course id
  1378. * @param int session id
  1379. * @return array with the results
  1380. *
  1381. */
  1382. public static function get_count_exercises_attempted_by_course($courseId, $session_id = 0)
  1383. {
  1384. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1385. $courseId = intval($courseId);
  1386. $session_id = intval($session_id);
  1387. $sql = "SELECT DISTINCT exe_exo_id, exe_user_id
  1388. FROM $table_track_exercises
  1389. WHERE
  1390. status = '' AND
  1391. c_id = '$courseId' AND
  1392. session_id = $session_id AND
  1393. orig_lp_id =0 AND
  1394. orig_lp_item_id = 0
  1395. ORDER BY exe_id";
  1396. $res = Database::query($sql);
  1397. $count = 0;
  1398. if (Database::num_rows($res) > 0) {
  1399. $count = Database::num_rows($res);
  1400. }
  1401. return $count;
  1402. }
  1403. /**
  1404. * Gets all exercise events from a Learning Path within a Course nd Session
  1405. * @param int exercise id
  1406. * @param int $courseId
  1407. * @param int session id
  1408. * @return array
  1409. */
  1410. public static function get_all_exercise_event_from_lp($exercise_id, $courseId, $session_id = 0)
  1411. {
  1412. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1413. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1414. $courseId = intval($courseId);
  1415. $exercise_id = intval($exercise_id);
  1416. $session_id = intval($session_id);
  1417. $sql = "SELECT * FROM $table_track_exercises
  1418. WHERE
  1419. status = '' AND
  1420. c_id = $courseId AND
  1421. exe_exo_id = '$exercise_id' AND
  1422. session_id = $session_id AND
  1423. orig_lp_id !=0 AND
  1424. orig_lp_item_id != 0";
  1425. $res = Database::query($sql);
  1426. $list = array();
  1427. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1428. $list[$row['exe_id']] = $row;
  1429. $sql = "SELECT * FROM $table_track_attempt WHERE exe_id = {$row['exe_id']}";
  1430. $res_question = Database::query($sql);
  1431. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1432. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1433. }
  1434. }
  1435. return $list;
  1436. }
  1437. /**
  1438. * @param int $lp_id
  1439. * @param int $course_id
  1440. *
  1441. * @return array
  1442. */
  1443. public static function get_all_exercises_from_lp($lp_id, $course_id)
  1444. {
  1445. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  1446. $course_id = intval($course_id);
  1447. $lp_id = intval($lp_id);
  1448. $sql = "SELECT * FROM $lp_item_table
  1449. WHERE
  1450. c_id = $course_id AND
  1451. lp_id = '".$lp_id."' AND
  1452. item_type = 'quiz'
  1453. ORDER BY parent_item_id, display_order";
  1454. $res = Database::query($sql);
  1455. $my_exercise_list = array();
  1456. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1457. $my_exercise_list[] = $row;
  1458. }
  1459. return $my_exercise_list;
  1460. }
  1461. /**
  1462. * This function gets the comments of an exercise
  1463. *
  1464. * @param int $exe_id
  1465. * @param int $question_id
  1466. *
  1467. * @return string the comment
  1468. */
  1469. public static function get_comments($exe_id, $question_id)
  1470. {
  1471. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1472. $sql = "SELECT teacher_comment FROM ".$table_track_attempt."
  1473. WHERE
  1474. exe_id='".Database::escape_string($exe_id)."' AND
  1475. question_id = '".Database::escape_string($question_id)."'
  1476. ORDER by question_id";
  1477. $sqlres = Database::query($sql);
  1478. $comm = Database::result($sqlres, 0, "teacher_comment");
  1479. return $comm;
  1480. }
  1481. /**
  1482. * @param int $exe_id
  1483. *
  1484. * @return array
  1485. */
  1486. public static function getAllExerciseEventByExeId($exe_id)
  1487. {
  1488. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1489. $exe_id = intval($exe_id);
  1490. $list = array();
  1491. $sql = "SELECT * FROM $table_track_attempt
  1492. WHERE exe_id = $exe_id
  1493. ORDER BY position";
  1494. $res_question = Database::query($sql);
  1495. if (Database::num_rows($res_question)) {
  1496. while ($row = Database::fetch_array($res_question, 'ASSOC')) {
  1497. $list[$row['question_id']][] = $row;
  1498. }
  1499. }
  1500. return $list;
  1501. }
  1502. /**
  1503. *
  1504. * @param int $exe_id
  1505. * @param int $user_id
  1506. * @param int $courseId
  1507. * @param int $session_id
  1508. * @param int $question_id
  1509. */
  1510. public static function delete_attempt($exe_id, $user_id, $courseId, $session_id, $question_id)
  1511. {
  1512. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1513. $exe_id = intval($exe_id);
  1514. $user_id = intval($user_id);
  1515. $courseId = intval($courseId);
  1516. $session_id = intval($session_id);
  1517. $question_id = intval($question_id);
  1518. $sql = "DELETE FROM $table_track_attempt
  1519. WHERE
  1520. exe_id = $exe_id AND
  1521. user_id = $user_id AND
  1522. c_id = $courseId AND
  1523. session_id = $session_id AND
  1524. question_id = $question_id ";
  1525. Database::query($sql);
  1526. Event::addEvent(
  1527. LOG_QUESTION_RESULT_DELETE,
  1528. LOG_EXERCISE_ATTEMPT_QUESTION_ID,
  1529. $exe_id . '-' . $question_id,
  1530. null,
  1531. null,
  1532. $courseId,
  1533. $session_id
  1534. );
  1535. }
  1536. /**
  1537. * @param $exe_id
  1538. * @param $user_id
  1539. * @param int $courseId
  1540. * @param $question_id
  1541. * @param int $sessionId
  1542. */
  1543. public static function delete_attempt_hotspot($exe_id, $user_id, $courseId, $question_id, $sessionId = null)
  1544. {
  1545. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1546. $exe_id = intval($exe_id);
  1547. $user_id = intval($user_id);
  1548. $courseId = intval($courseId);
  1549. $question_id = intval($question_id);
  1550. if (!isset($sessionId)) {
  1551. $sessionId = api_get_session_id();
  1552. }
  1553. $sql = "DELETE FROM $table_track_attempt
  1554. WHERE hotspot_exe_id = $exe_id AND
  1555. hotspot_user_id = $user_id AND
  1556. c_id = $courseId AND
  1557. hotspot_question_id = $question_id ";
  1558. Database::query($sql);
  1559. Event::addEvent(
  1560. LOG_QUESTION_RESULT_DELETE,
  1561. LOG_EXERCISE_ATTEMPT_QUESTION_ID,
  1562. $exe_id . '-' . $question_id,
  1563. null,
  1564. null,
  1565. $courseId,
  1566. $sessionId
  1567. );
  1568. }
  1569. /**
  1570. * Registers in track_e_course_access when user logs in for the first time to a course
  1571. * @param int $courseId ID of the course
  1572. * @param int $user_id ID of the user
  1573. * @param int $session_id ID of the session (if any)
  1574. */
  1575. public static function event_course_login($courseId, $user_id, $session_id)
  1576. {
  1577. $course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1578. $loginDate = $logoutDate = api_get_utc_datetime();
  1579. //$counter represents the number of time this record has been refreshed
  1580. $counter = 1;
  1581. $courseId = intval($courseId);
  1582. $user_id = intval($user_id);
  1583. $session_id = intval($session_id);
  1584. $ip = Database::escape_string(api_get_real_ip());
  1585. $sql = "INSERT INTO $course_tracking_table(c_id, user_ip, user_id, login_course_date, logout_course_date, counter, session_id)
  1586. VALUES('".$courseId."', '".$ip."', '".$user_id."', '$loginDate', '$logoutDate', $counter, '".$session_id."')";
  1587. Database::query($sql);
  1588. // Course catalog stats modifications see #4191
  1589. CourseManager::update_course_ranking(null, null, null, null, true, false);
  1590. }
  1591. /**
  1592. * Register a "fake" time spent on the platform, for example to match the
  1593. * estimated time he took to author an assignment/work, see configuration
  1594. * setting considered_working_time.
  1595. * This assumes there is already some connection of the student to the
  1596. * course, otherwise he wouldn't be able to upload an assignment.
  1597. * This works by creating a new record, copy of the current one, then
  1598. * updating the current one to be just the considered_working_time and
  1599. * end at the same second as the user connected to the course.
  1600. * @param int $courseId The course in which to add the time
  1601. * @param int $userId The user for whom to add the time
  1602. * @param int $sessionId The session in which to add the time (if any)
  1603. * @param string $virtualTime The amount of time to be added, in a hh:mm:ss format. If int, we consider it is expressed in hours.
  1604. * @param string $ip IP address to go on record for this time record
  1605. *
  1606. * @return True on successful insertion, false otherwise
  1607. */
  1608. public static function eventAddVirtualCourseTime(
  1609. $courseId,
  1610. $userId,
  1611. $sessionId,
  1612. $virtualTime = '',
  1613. $ip = ''
  1614. ) {
  1615. $courseTrackingTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1616. $time = $loginDate = $logoutDate = api_get_utc_datetime();
  1617. $courseId = intval($courseId);
  1618. $userId = intval($userId);
  1619. $sessionId = intval($sessionId);
  1620. $ip = Database::escape_string($ip);
  1621. // Get the current latest course connection register. We need that
  1622. // record to re-use the data and create a new record.
  1623. $sql = "SELECT *
  1624. FROM $courseTrackingTable
  1625. WHERE
  1626. user_id = ".$userId." AND
  1627. c_id = ".$courseId." AND
  1628. session_id = ".$sessionId." AND
  1629. login_course_date > '$time' - INTERVAL 3600 SECOND
  1630. ORDER BY login_course_date DESC LIMIT 0,1";
  1631. $result = Database::query($sql);
  1632. // Ignore if we didn't find any course connection record in the last
  1633. // hour. In this case it wouldn't be right to add a "fake" time record.
  1634. if (Database::num_rows($result) > 0) {
  1635. // Found the latest connection
  1636. $row = Database::fetch_row($result);
  1637. $courseAccessId = $row[0];
  1638. $courseAccessLoginDate = $row[3];
  1639. $counter = $row[5];
  1640. $counter = $counter ? $counter : 0;
  1641. // Insert a new record, copy of the current one (except the logout
  1642. // date that we update to the current time)
  1643. $sql = "INSERT INTO $courseTrackingTable(
  1644. c_id,
  1645. user_ip,
  1646. user_id,
  1647. login_course_date,
  1648. logout_course_date,
  1649. counter,
  1650. session_id
  1651. ) VALUES(
  1652. $courseId,
  1653. '$ip',
  1654. $userId,
  1655. '$courseAccessLoginDate',
  1656. '$logoutDate',
  1657. $counter,
  1658. $sessionId
  1659. )";
  1660. Database::query($sql);
  1661. $loginDate = ChamiloApi::addOrSubTimeToDateTime(
  1662. $virtualTime,
  1663. $courseAccessLoginDate,
  1664. false
  1665. );
  1666. // We update the course tracking table
  1667. $sql = "UPDATE $courseTrackingTable
  1668. SET
  1669. login_course_date = '$loginDate',
  1670. logout_course_date = '$courseAccessLoginDate',
  1671. counter = 0
  1672. WHERE
  1673. course_access_id = ".intval($courseAccessId)." AND
  1674. session_id = ".$sessionId;
  1675. Database::query($sql);
  1676. return true;
  1677. }
  1678. return false;
  1679. }
  1680. /**
  1681. * Removes a "fake" time spent on the platform, for example to match the
  1682. * estimated time he took to author an assignment/work, see configuration
  1683. * setting considered_working_time.
  1684. * This method should be called when something that generated a fake
  1685. * time record is removed. Given the database link is weak (no real
  1686. * relationship kept between the deleted item and this record), this
  1687. * method just looks for the latest record that has the same time as the
  1688. * item's fake time, is in the past and in this course+session. If such a
  1689. * record cannot be found, it doesn't do anything.
  1690. * The IP address is not considered a useful filter here.
  1691. * @param int $courseId The course in which to add the time
  1692. * @param int $userId The user for whom to add the time
  1693. * @param $sessionId The session in which to add the time (if any)
  1694. * @param string $virtualTime The amount of time to be added, in a hh:mm:ss format. If int, we consider it is expressed in hours.
  1695. * @return True on successful removal, false otherwise
  1696. */
  1697. public static function eventRemoveVirtualCourseTime($courseId, $userId, $sessionId = 0, $virtualTime = '')
  1698. {
  1699. if (empty($virtualTime)) {
  1700. return false;
  1701. }
  1702. $courseTrackingTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1703. $time = $loginDate = $logoutDate = api_get_utc_datetime();
  1704. $courseId = intval($courseId);
  1705. $userId = intval($userId);
  1706. $sessionId = intval($sessionId);
  1707. // Change $virtualTime format from hh:mm:ss to hhmmss which is the
  1708. // format returned by SQL for a subtraction of two datetime values
  1709. // @todo make sure this is portable between DBMSes
  1710. if (preg_match('/:/', $virtualTime)) {
  1711. list($h, $m, $s) = preg_split('/:/', $virtualTime);
  1712. $virtualTime = $h * 3600 + $m * 60 + $s;
  1713. } else {
  1714. $virtualTime *= 3600;
  1715. }
  1716. // Get the current latest course connection register. We need that
  1717. // record to re-use the data and create a new record.
  1718. $sql = "SELECT course_access_id
  1719. FROM $courseTrackingTable
  1720. WHERE
  1721. user_id = $userId AND
  1722. c_id = $courseId AND
  1723. session_id = $sessionId AND
  1724. counter = 0 AND
  1725. (UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date)) = '$virtualTime'
  1726. ORDER BY login_course_date DESC LIMIT 0,1";
  1727. $result = Database::query($sql);
  1728. // Ignore if we didn't find any course connection record in the last
  1729. // hour. In this case it wouldn't be right to add a "fake" time record.
  1730. if (Database::num_rows($result) > 0) {
  1731. // Found the latest connection
  1732. $row = Database::fetch_row($result);
  1733. $courseAccessId = $row[0];
  1734. $sql = "DELETE FROM $courseTrackingTable WHERE course_access_id = $courseAccessId";
  1735. $result = Database::query($sql);
  1736. return $result;
  1737. }
  1738. return false;
  1739. }
  1740. /**
  1741. * For the sake of genericity, this function is a switch.
  1742. * It's called by EventsDispatcher and fires the good function
  1743. * with the good require_once.
  1744. *
  1745. * @param string $event_name
  1746. * @param array $params
  1747. */
  1748. public static function event_send_mail($event_name, $params)
  1749. {
  1750. EventsMail::send_mail($event_name, $params);
  1751. }
  1752. /**
  1753. * Internal function checking if the mail was already sent from that user to that user
  1754. * @param string $event_name
  1755. * @param int $user_from
  1756. * @param int $user_to
  1757. * @return boolean
  1758. */
  1759. public static function check_if_mail_already_sent($event_name, $user_from, $user_to = null)
  1760. {
  1761. if ($user_to == null) {
  1762. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1763. WHERE user_from = '.$user_from.' AND event_type_name = "'.$event_name.'"';
  1764. } else {
  1765. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1766. WHERE user_from = '.$user_from.' AND user_to = '.$user_to.' AND event_type_name = "'.$event_name.'"';
  1767. }
  1768. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1769. return $result[0]["total"];
  1770. }
  1771. /**
  1772. *
  1773. * Filter EventEmailTemplate Filters see the main/inc/conf/events.conf.dist.php
  1774. *
  1775. */
  1776. /**
  1777. * Basic template event message filter (to be used by other filters as default)
  1778. * @param array $values (passing by reference)
  1779. * @return boolean True if everything is OK, false otherwise
  1780. */
  1781. public function event_send_mail_filter_func(&$values)
  1782. {
  1783. return true;
  1784. }
  1785. /**
  1786. * user_registration - send_mail filter
  1787. * @param array $values (passing by reference)
  1788. * @return boolean True if everything is OK, false otherwise
  1789. */
  1790. public function user_registration_event_send_mail_filter_func(&$values)
  1791. {
  1792. $res = self::event_send_mail_filter_func($values);
  1793. // proper logic for this filter
  1794. return $res;
  1795. }
  1796. /**
  1797. * portal_homepage_edited - send_mail filter
  1798. * @param array $values (passing by reference)
  1799. * @return boolean True if everything is OK, false otherwise
  1800. */
  1801. public function portal_homepage_edited_event_send_mail_filter_func(&$values)
  1802. {
  1803. $res = self::event_send_mail_filter_func($values);
  1804. // proper logic for this filter
  1805. return $res;
  1806. }
  1807. }