events.lib.php 75 KB

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