events.lib.php 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  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' :
  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 = 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' => 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 exercise was done
  290. */
  291. public static function update_event_exercise(
  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 $fileName 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. $fileName = 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. $session_id = api_get_session_id();
  440. if (!empty($question_id) && !empty($exe_id) && !empty($user_id)) {
  441. if (is_null($answer)) {
  442. $answer = '';
  443. }
  444. $attempt = array(
  445. 'user_id' => $user_id,
  446. 'question_id' => $question_id,
  447. 'answer' => $answer,
  448. 'marks' => $score,
  449. 'c_id' => $course_id,
  450. 'session_id' => $session_id,
  451. 'position' => $position,
  452. 'tms' => $now,
  453. 'filename' => !empty($fileName) ? basename($fileName) : $fileName,
  454. 'teacher_comment' => ''
  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 ($debug) {
  486. error_log("Insert attempt with id #$attempt_id");
  487. }
  488. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
  489. if ($debug) {
  490. error_log("Saving e attempt recording ");
  491. }
  492. $attempt_recording = array(
  493. 'exe_id' => $attempt_id,
  494. 'question_id' => $question_id,
  495. 'marks' => $score,
  496. 'insert_date' => $now,
  497. 'author' => '',
  498. 'session_id' => $session_id,
  499. );
  500. Database::insert($recording_table, $attempt_recording);
  501. }
  502. } else {
  503. Database::update(
  504. $TBL_TRACK_ATTEMPT,
  505. $attempt,
  506. array('exe_id = ? AND question_id = ? AND user_id = ? ' => array($exe_id, $question_id, $user_id))
  507. );
  508. if (defined('ENABLED_LIVE_EXERCISE_TRACKING')) {
  509. $attempt_recording = array(
  510. 'exe_id' => $exe_id,
  511. 'question_id' => $question_id,
  512. 'marks' => $score,
  513. 'insert_date' => $now,
  514. 'author' => '',
  515. 'session_id' => $session_id,
  516. );
  517. Database::update(
  518. $recording_table,
  519. $attempt_recording,
  520. array('exe_id = ? AND question_id = ? AND session_id = ? ' => array($exe_id, $question_id, $session_id))
  521. );
  522. }
  523. $attempt_id = $exe_id;
  524. }
  525. return $attempt_id;
  526. } else {
  527. return false;
  528. }
  529. }
  530. /**
  531. * Record an hotspot spot for this attempt at answering an hotspot question
  532. * @param int $exeId
  533. * @param int $questionId Question ID
  534. * @param int $answerId Answer ID
  535. * @param int $correct
  536. * @param string $coords Coordinates of this point (e.g. 123;324)
  537. * @param bool $updateResults
  538. * @param int $exerciseId
  539. *
  540. * @return bool Result of the insert query
  541. * @uses Course code and user_id from global scope $_cid and $_user
  542. */
  543. public static function saveExerciseAttemptHotspot(
  544. $exeId,
  545. $questionId,
  546. $answerId,
  547. $correct,
  548. $coords,
  549. $updateResults = false,
  550. $exerciseId = 0
  551. ) {
  552. global $safe_lp_id, $safe_lp_item_id;
  553. if ($updateResults == false) {
  554. // Validation in case of fraud with activated control time
  555. if (!ExerciseLib::exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
  556. $correct = 0;
  557. }
  558. }
  559. if (empty($exeId)) {
  560. return false;
  561. }
  562. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  563. if ($updateResults) {
  564. $params = array(
  565. 'hotspot_correct' => $correct,
  566. 'hotspot_coordinate' => $coords
  567. );
  568. Database::update(
  569. $table,
  570. $params,
  571. array(
  572. 'hotspot_user_id = ? AND hotspot_exe_id = ? AND hotspot_question_id = ? AND hotspot_answer_id = ? ' => array(
  573. api_get_user_id(),
  574. $exeId,
  575. $questionId,
  576. $answerId
  577. )
  578. )
  579. );
  580. } else {
  581. return Database::insert(
  582. $table,
  583. [
  584. 'hotspot_course_code' => api_get_course_id(),
  585. 'hotspot_user_id' => api_get_user_id(),
  586. 'c_id' => api_get_course_int_id(),
  587. 'hotspot_exe_id' => $exeId,
  588. 'hotspot_question_id' => $questionId,
  589. 'hotspot_answer_id' => $answerId,
  590. 'hotspot_correct' => $correct,
  591. 'hotspot_coordinate' => $coords
  592. ]
  593. );
  594. }
  595. }
  596. /**
  597. * Records information for common (or admin) events (in the track_e_default table)
  598. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  599. * @param string $event_type Type of event
  600. * @param string $event_value_type Type of value
  601. * @param string $event_value Value
  602. * @param string $datetime Datetime (UTC) (defaults to null)
  603. * @param int $user_id User ID (defaults to null)
  604. * @param int $course_id Course ID (defaults to null)
  605. * @param int $sessionId Session ID
  606. * @return bool
  607. * @assert ('','','') === false
  608. */
  609. public static function addEvent(
  610. $event_type,
  611. $event_value_type,
  612. $event_value,
  613. $datetime = null,
  614. $user_id = null,
  615. $course_id = null,
  616. $sessionId = 0
  617. ) {
  618. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  619. if (empty($event_type)) {
  620. return false;
  621. }
  622. $event_type = Database::escape_string($event_type);
  623. $event_value_type = Database::escape_string($event_value_type);
  624. if (!empty($course_id)) {
  625. $course_id = intval($course_id);
  626. } else {
  627. $course_id = api_get_course_int_id();
  628. }
  629. if (!empty($sessionId)) {
  630. $sessionId = intval($sessionId);
  631. } else {
  632. $sessionId = api_get_session_id();
  633. }
  634. //Clean the user_info
  635. if ($event_value_type == LOG_USER_OBJECT) {
  636. if (is_array($event_value)) {
  637. unset($event_value['complete_name']);
  638. unset($event_value['complete_name_with_username']);
  639. unset($event_value['firstName']);
  640. unset($event_value['lastName']);
  641. unset($event_value['avatar_small']);
  642. unset($event_value['avatar']);
  643. unset($event_value['mail']);
  644. unset($event_value['password']);
  645. unset($event_value['last_login']);
  646. unset($event_value['picture_uri']);
  647. $event_value = serialize($event_value);
  648. }
  649. }
  650. // If event is an array then the $event_value_type should finish with
  651. // the suffix _array for example LOG_WORK_DATA = work_data_array
  652. if (is_array($event_value)) {
  653. $event_value = serialize($event_value);
  654. }
  655. $event_value = Database::escape_string($event_value);
  656. $sessionId = empty($sessionId) ? api_get_session_id() : intval($sessionId);
  657. if (!isset($datetime)) {
  658. $datetime = api_get_utc_datetime();
  659. }
  660. $datetime = Database::escape_string($datetime);
  661. if (!isset($user_id)) {
  662. $user_id = api_get_user_id();
  663. }
  664. $params = array(
  665. 'default_user_id' => $user_id,
  666. 'c_id' => $course_id,
  667. 'default_date' => $datetime,
  668. 'default_event_type' => $event_type,
  669. 'default_value_type' => $event_value_type,
  670. 'default_value' => $event_value,
  671. 'session_id' => $sessionId
  672. );
  673. Database::insert($table, $params);
  674. return true;
  675. }
  676. /**
  677. * Get every email stored in the database
  678. *
  679. * @return array
  680. * @assert () !== false
  681. */
  682. public static function get_all_event_types()
  683. {
  684. global $event_config;
  685. $sql = 'SELECT etm.id, event_type_name, activated, language_id, message, subject, dokeos_folder
  686. FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' etm
  687. INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l
  688. ON etm.language_id = l.id';
  689. $events_types = Database::store_result(Database::query($sql), 'ASSOC');
  690. $to_return = array();
  691. foreach ($events_types as $et) {
  692. $et['nameLangVar'] = $event_config[$et["event_type_name"]]["name_lang_var"];
  693. $et['descLangVar'] = $event_config[$et["event_type_name"]]["desc_lang_var"];
  694. $to_return[] = $et;
  695. }
  696. return $to_return;
  697. }
  698. /**
  699. * Get the users related to one event
  700. *
  701. * @param string $event_name
  702. */
  703. public static function get_event_users($event_name)
  704. {
  705. $event_name = Database::escape_string($event_name);
  706. $sql = 'SELECT user.user_id, user.firstname, user.lastname
  707. FROM '.Database::get_main_table(TABLE_MAIN_USER).' user
  708. JOIN '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' relUser
  709. ON relUser.user_id = user.user_id
  710. WHERE user.status <> '.ANONYMOUS.' AND relUser.event_type_name = "'.$event_name.'"';
  711. $user_list = Database::store_result(Database::query($sql), 'ASSOC');
  712. return json_encode($user_list);
  713. }
  714. /**
  715. * @param int $user_id
  716. * @param string $event_type
  717. * @return array|bool
  718. */
  719. public static function get_events_by_user_and_type($user_id, $event_type)
  720. {
  721. $TABLETRACK_DEFAULT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  722. $user_id = intval($user_id);
  723. $event_type = Database::escape_string($event_type);
  724. $sql = "SELECT * FROM $TABLETRACK_DEFAULT
  725. WHERE default_value_type = 'user_id' AND
  726. default_value = $user_id AND
  727. default_event_type = '$event_type'
  728. ORDER BY default_date ";
  729. $result = Database::query($sql);
  730. if ($result) {
  731. return Database::store_result($result, 'ASSOC');
  732. }
  733. return false;
  734. }
  735. /**
  736. * Save the new message for one event and for one language
  737. *
  738. * @param string $eventName
  739. * @param array $users
  740. * @param string $message
  741. * @param string $subject
  742. * @param string $eventMessageLanguage
  743. * @param int $activated
  744. */
  745. public static function save_event_type_message($event_name, $users, $message, $subject, $event_message_language, $activated)
  746. {
  747. $event_name = Database::escape_string($event_name);
  748. $activated = intval($activated);
  749. $event_message_language = Database::escape_string($event_message_language);
  750. // Deletes then re-adds the users linked to the event
  751. $sql = 'DELETE FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' WHERE event_type_name = "'.$event_name.'" ';
  752. Database::query($sql);
  753. foreach ($users as $user) {
  754. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' (user_id,event_type_name)
  755. VALUES('.intval($user).',"'.$event_name.'")';
  756. Database::query($sql);
  757. }
  758. $language_id = api_get_language_id($event_message_language);
  759. // check if this template in this language already exists or not
  760. $sql = 'SELECT COUNT(id) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  761. WHERE event_type_name = "'.$event_name.'" AND language_id = '.$language_id;
  762. $sql = Database::store_result(Database::query($sql), 'ASSOC');
  763. // if already exists, we update
  764. if ($sql[0]["total"] > 0) {
  765. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  766. SET message = "'.Database::escape_string($message).'",
  767. subject = "'.Database::escape_string($subject).'",
  768. activated = '.$activated.'
  769. WHERE event_type_name = "'.$event_name.'" AND language_id = (SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  770. WHERE dokeos_folder = "'.$event_message_language.'")';
  771. Database::query($sql);
  772. } else { // else we create a new record
  773. // gets the language_-_id
  774. $lang_id = '(SELECT id FROM '.Database::get_main_table(TABLE_MAIN_LANGUAGE).'
  775. WHERE dokeos_folder = "'.$event_message_language.'")';
  776. $lang_id = Database::store_result(Database::query($lang_id), 'ASSOC');
  777. if (!empty($lang_id[0]["id"])) {
  778. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' (event_type_name, language_id, message, subject, activated)
  779. VALUES("'.$event_name.'", '.$lang_id[0]["id"].', "'.Database::escape_string($message).'", "'.Database::escape_string($subject).'", '.$activated.')';
  780. Database::query($sql);
  781. }
  782. }
  783. // set activated at every save
  784. $sql = 'UPDATE '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).'
  785. SET activated = '.$activated.'
  786. WHERE event_type_name = "'.$event_name.'"';
  787. Database::query($sql);
  788. }
  789. /**
  790. * Gets the last attempt of an exercise based in the exe_id
  791. * @param int $exe_id
  792. * @return mixed
  793. */
  794. public static function getLastAttemptDateOfExercise($exe_id)
  795. {
  796. $exe_id = intval($exe_id);
  797. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  798. $sql = 'SELECT max(tms) as last_attempt_date
  799. FROM '.$track_attempts.'
  800. WHERE exe_id='.$exe_id;
  801. $rs_last_attempt = Database::query($sql);
  802. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  803. $last_attempt_date = $row_last_attempt['last_attempt_date']; //Get the date of last attempt
  804. return $last_attempt_date;
  805. }
  806. /**
  807. * Gets the last attempt of an exercise based in the exe_id
  808. * @param int $exe_id
  809. * @return mixed
  810. */
  811. public static function getLatestQuestionIdFromAttempt($exe_id)
  812. {
  813. $exe_id = intval($exe_id);
  814. $track_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  815. $sql = 'SELECT question_id FROM '.$track_attempts.'
  816. WHERE exe_id='.$exe_id.'
  817. ORDER BY tms DESC
  818. LIMIT 1';
  819. $result = Database::query($sql);
  820. if (Database::num_rows($result)) {
  821. $row = Database::fetch_array($result);
  822. return $row['question_id'];
  823. } else {
  824. return false;
  825. }
  826. }
  827. /**
  828. * Gets how many attempts exists by user, exercise, learning path
  829. * @param int user id
  830. * @param int exercise id
  831. * @param int lp id
  832. * @param int lp item id
  833. * @param int lp item view id
  834. */
  835. public static function get_attempt_count($user_id, $exerciseId, $lp_id, $lp_item_id, $lp_item_view_id)
  836. {
  837. $stat_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  838. $user_id = intval($user_id);
  839. $exerciseId = intval($exerciseId);
  840. $lp_id = intval($lp_id);
  841. $lp_item_id = intval($lp_item_id);
  842. $lp_item_view_id = intval($lp_item_view_id);
  843. $sql = "SELECT count(*) as count
  844. FROM $stat_table
  845. WHERE
  846. exe_exo_id = $exerciseId AND
  847. exe_user_id = $user_id AND
  848. status != 'incomplete' AND
  849. orig_lp_id = $lp_id AND
  850. orig_lp_item_id = $lp_item_id AND
  851. orig_lp_item_view_id = $lp_item_view_id AND
  852. c_id = '".api_get_course_int_id()."' AND
  853. session_id = '".api_get_session_id()."'";
  854. $query = Database::query($sql);
  855. if (Database::num_rows($query) > 0) {
  856. $attempt = Database::fetch_array($query, 'ASSOC');
  857. return $attempt['count'];
  858. } else {
  859. return 0;
  860. }
  861. }
  862. /**
  863. * @param $user_id
  864. * @param $exerciseId
  865. * @param $lp_id
  866. * @param $lp_item_id
  867. * @return int
  868. */
  869. public static function get_attempt_count_not_finished($user_id, $exerciseId, $lp_id, $lp_item_id)
  870. {
  871. $stat_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  872. $user_id = intval($user_id);
  873. $exerciseId = intval($exerciseId);
  874. $lp_id = intval($lp_id);
  875. $lp_item_id = intval($lp_item_id);
  876. //$lp_item_view_id = intval($lp_item_view_id);
  877. $sql = "SELECT count(*) as count
  878. FROM $stat_table
  879. WHERE
  880. exe_exo_id = $exerciseId AND
  881. exe_user_id = $user_id AND
  882. status != 'incomplete' AND
  883. orig_lp_id = $lp_id AND
  884. orig_lp_item_id = $lp_item_id AND
  885. c_id = '".api_get_course_int_id()."' AND
  886. session_id = '".api_get_session_id()."'";
  887. $query = Database::query($sql);
  888. if (Database::num_rows($query) > 0) {
  889. $attempt = Database::fetch_array($query, 'ASSOC');
  890. return $attempt['count'];
  891. } else {
  892. return 0;
  893. }
  894. }
  895. /**
  896. * @param int $user_id
  897. * @param int $lp_id
  898. * @param array $course
  899. * @param int $session_id
  900. */
  901. public static function delete_student_lp_events($user_id, $lp_id, $course, $session_id)
  902. {
  903. $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
  904. $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  905. $lpInteraction = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  906. $lpObjective = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  907. $course_id = $course['real_id'];
  908. if (empty($course_id)) {
  909. $course_id = api_get_course_int_id();
  910. }
  911. $track_e_exercises = Database::get_main_table(
  912. TABLE_STATISTIC_TRACK_E_EXERCISES
  913. );
  914. $track_attempts = Database::get_main_table(
  915. TABLE_STATISTIC_TRACK_E_ATTEMPT
  916. );
  917. $recording_table = Database::get_main_table(
  918. TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING
  919. );
  920. $user_id = intval($user_id);
  921. $lp_id = intval($lp_id);
  922. $session_id = intval($session_id);
  923. //Make sure we have the exact lp_view_id
  924. $sql = "SELECT id FROM $lp_view_table
  925. WHERE
  926. c_id = $course_id AND
  927. user_id = $user_id AND
  928. lp_id = $lp_id AND
  929. session_id = $session_id ";
  930. $result = Database::query($sql);
  931. if (Database::num_rows($result)) {
  932. $view = Database::fetch_array($result, 'ASSOC');
  933. $lp_view_id = $view['id'];
  934. $sql = "DELETE FROM $lp_item_view_table
  935. WHERE c_id = $course_id AND lp_view_id = $lp_view_id ";
  936. Database::query($sql);
  937. $sql = "DELETE FROM $lpInteraction
  938. WHERE c_id = $course_id AND lp_iv_id = $lp_view_id ";
  939. Database::query($sql);
  940. $sql = "DELETE FROM $lpObjective
  941. WHERE c_id = $course_id AND lp_iv_id = $lp_view_id ";
  942. Database::query($sql);
  943. }
  944. $sql = "DELETE FROM $lp_view_table
  945. WHERE
  946. c_id = $course_id AND
  947. user_id = $user_id AND
  948. lp_id= $lp_id AND
  949. session_id = $session_id
  950. ";
  951. Database::query($sql);
  952. $sql = "SELECT exe_id FROM $track_e_exercises
  953. WHERE exe_user_id = $user_id AND
  954. session_id = $session_id AND
  955. c_id = $course_id AND
  956. orig_lp_id = $lp_id";
  957. $result = Database::query($sql);
  958. $exe_list = array();
  959. while ($row = Database::fetch_array($result, 'ASSOC')) {
  960. $exe_list[] = $row['exe_id'];
  961. }
  962. if (!empty($exe_list) && is_array($exe_list) && count($exe_list) > 0) {
  963. $sql = "DELETE FROM $track_e_exercises
  964. WHERE exe_id IN (" . implode(',', $exe_list) . ")";
  965. Database::query($sql);
  966. $sql = "DELETE FROM $track_attempts
  967. WHERE exe_id IN (" . implode(',', $exe_list) . ")";
  968. Database::query($sql);
  969. $sql = "DELETE FROM $recording_table
  970. WHERE exe_id IN (" . implode(',', $exe_list) . ")";
  971. Database::query($sql);
  972. }
  973. self::addEvent(
  974. LOG_LP_ATTEMPT_DELETE,
  975. LOG_LP_ID,
  976. $lp_id,
  977. null,
  978. null,
  979. $course_id,
  980. $session_id
  981. );
  982. }
  983. /**
  984. * Delete all exercise attempts (included in LP or not)
  985. *
  986. * @param int user id
  987. * @param int exercise id
  988. * @param int $course_id
  989. * @param int session id
  990. */
  991. public static function delete_all_incomplete_attempts($user_id, $exercise_id, $course_id, $session_id = 0)
  992. {
  993. $track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  994. $user_id = intval($user_id);
  995. $exercise_id = intval($exercise_id);
  996. $course_id = intval($course_id);
  997. $session_id = intval($session_id);
  998. if (!empty($user_id) && !empty($exercise_id) && !empty($course_code)) {
  999. $sql = "DELETE FROM $track_e_exercises
  1000. WHERE
  1001. exe_user_id = $user_id AND
  1002. exe_exo_id = $exercise_id AND
  1003. c_id = '$course_id' AND
  1004. session_id = $session_id AND
  1005. status = 'incomplete' ";
  1006. Database::query($sql);
  1007. self::addEvent(
  1008. LOG_EXERCISE_RESULT_DELETE,
  1009. LOG_EXERCISE_AND_USER_ID,
  1010. $exercise_id . '-' . $user_id,
  1011. null,
  1012. null,
  1013. $course_id,
  1014. $session_id
  1015. );
  1016. }
  1017. }
  1018. /**
  1019. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  1020. * @param int exercise id
  1021. * @param int $courseId
  1022. * @param int session id
  1023. * @return array with the results
  1024. *
  1025. */
  1026. public static function get_all_exercise_results(
  1027. $exercise_id,
  1028. $courseId,
  1029. $session_id = 0,
  1030. $load_question_list = true,
  1031. $user_id = null
  1032. ) {
  1033. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1034. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1035. $courseId = intval($courseId);
  1036. $exercise_id = intval($exercise_id);
  1037. $session_id = intval($session_id);
  1038. $user_condition = null;
  1039. if (!empty($user_id)) {
  1040. $user_id = intval($user_id);
  1041. $user_condition = "AND exe_user_id = $user_id ";
  1042. }
  1043. $sql = "SELECT * FROM $TABLETRACK_EXERCICES
  1044. WHERE
  1045. status = '' AND
  1046. c_id = '$courseId' AND
  1047. exe_exo_id = '$exercise_id' AND
  1048. session_id = $session_id AND
  1049. orig_lp_id =0 AND
  1050. orig_lp_item_id = 0
  1051. $user_condition
  1052. ORDER BY exe_id";
  1053. $res = Database::query($sql);
  1054. $list = array();
  1055. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1056. $list[$row['exe_id']] = $row;
  1057. if ($load_question_list) {
  1058. $sql = "SELECT * FROM $TBL_TRACK_ATTEMPT
  1059. WHERE exe_id = {$row['exe_id']}";
  1060. $res_question = Database::query($sql);
  1061. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1062. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1063. }
  1064. }
  1065. }
  1066. return $list;
  1067. }
  1068. /**
  1069. * Gets all exercise results (NO Exercises in LPs ) from a given exercise id, course, session
  1070. * @param int $courseId
  1071. * @param int session id
  1072. * @return array with the results
  1073. *
  1074. */
  1075. public static function get_all_exercise_results_by_course($courseId, $session_id = 0, $get_count = true)
  1076. {
  1077. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1078. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1079. $courseId = intval($courseId);
  1080. $session_id = intval($session_id);
  1081. $select = '*';
  1082. if ($get_count) {
  1083. $select = 'count(*) as count';
  1084. }
  1085. $sql = "SELECT $select FROM $table_track_exercises
  1086. WHERE status = '' AND
  1087. c_id = '$courseId' AND
  1088. session_id = $session_id AND
  1089. orig_lp_id = 0 AND
  1090. orig_lp_item_id = 0
  1091. ORDER BY exe_id";
  1092. $res = Database::query($sql);
  1093. if ($get_count) {
  1094. $row = Database::fetch_array($res, 'ASSOC');
  1095. return $row['count'];
  1096. } else {
  1097. $list = array();
  1098. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1099. $list[$row['exe_id']] = $row;
  1100. }
  1101. return $list;
  1102. }
  1103. }
  1104. /**
  1105. * Gets all exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1106. * @param int exercise id
  1107. * @param int $courseId
  1108. * @param int session id
  1109. * @return array with the results
  1110. *
  1111. */
  1112. public static function get_all_exercise_results_by_user($user_id, $courseId, $session_id = 0)
  1113. {
  1114. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1115. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1116. $courseId = intval($courseId);
  1117. $session_id = intval($session_id);
  1118. $user_id = intval($user_id);
  1119. $sql = "SELECT * FROM $table_track_exercises
  1120. WHERE
  1121. status = '' AND
  1122. exe_user_id = $user_id AND
  1123. c_id = '$courseId' AND
  1124. session_id = $session_id AND
  1125. orig_lp_id = 0 AND
  1126. orig_lp_item_id = 0
  1127. ORDER by exe_id";
  1128. $res = Database::query($sql);
  1129. $list = array();
  1130. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1131. $list[$row['exe_id']] = $row;
  1132. $sql = "SELECT * FROM $table_track_attempt
  1133. WHERE exe_id = {$row['exe_id']}";
  1134. $res_question = Database::query($sql);
  1135. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1136. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1137. }
  1138. }
  1139. return $list;
  1140. }
  1141. /**
  1142. * Gets exercise results (NO Exercises in LPs) from a given exercise id, course, session
  1143. * @param int $exe_id exercise id
  1144. * @param string $status
  1145. * @return array with the results
  1146. *
  1147. */
  1148. public static function get_exercise_results_by_attempt($exe_id, $status = null)
  1149. {
  1150. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1151. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1152. $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1153. $exe_id = intval($exe_id);
  1154. $status = Database::escape_string($status);
  1155. $sql = "SELECT * FROM $table_track_exercises
  1156. WHERE status = '".$status."' AND exe_id = $exe_id";
  1157. $res = Database::query($sql);
  1158. $list = array();
  1159. if (Database::num_rows($res)) {
  1160. $row = Database::fetch_array($res, 'ASSOC');
  1161. //Checking if this attempt was revised by a teacher
  1162. $sql_revised = 'SELECT exe_id FROM '.$table_track_attempt_recording.'
  1163. WHERE author != "" AND exe_id = '.$exe_id.'
  1164. LIMIT 1';
  1165. $res_revised = Database::query($sql_revised);
  1166. $row['attempt_revised'] = 0;
  1167. if (Database::num_rows($res_revised) > 0) {
  1168. $row['attempt_revised'] = 1;
  1169. }
  1170. $list[$exe_id] = $row;
  1171. $sql = "SELECT * FROM $table_track_attempt
  1172. WHERE exe_id = $exe_id
  1173. ORDER BY tms ASC";
  1174. $res_question = Database::query($sql);
  1175. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1176. $list[$exe_id]['question_list'][$row_q['question_id']] = $row_q;
  1177. }
  1178. }
  1179. return $list;
  1180. }
  1181. /**
  1182. * Gets exercise results (NO Exercises in LPs) from a given user, exercise id, course, session, lp_id, lp_item_id
  1183. * @param int user id
  1184. * @param int exercise id
  1185. * @param string course code
  1186. * @param int session id
  1187. * @param int lp id
  1188. * @param int lp item id
  1189. * @param string order asc or desc
  1190. * @return array with the results
  1191. *
  1192. */
  1193. public static function getExerciseResultsByUser(
  1194. $user_id,
  1195. $exercise_id,
  1196. $courseId,
  1197. $session_id = 0,
  1198. $lp_id = 0,
  1199. $lp_item_id = 0,
  1200. $order = null
  1201. ) {
  1202. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1203. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1204. $table_track_attempt_recording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1205. $courseId = intval($courseId);
  1206. $exercise_id = intval($exercise_id);
  1207. $session_id = intval($session_id);
  1208. $user_id = intval($user_id);
  1209. $lp_id = intval($lp_id);
  1210. $lp_item_id = intval($lp_item_id);
  1211. if (!in_array(strtolower($order), array('asc', 'desc'))) {
  1212. $order = 'asc';
  1213. }
  1214. $sql = "SELECT * FROM $table_track_exercises
  1215. WHERE
  1216. status = '' AND
  1217. exe_user_id = $user_id AND
  1218. c_id = $courseId AND
  1219. exe_exo_id = $exercise_id AND
  1220. session_id = $session_id AND
  1221. orig_lp_id = $lp_id AND
  1222. orig_lp_item_id = $lp_item_id
  1223. ORDER by exe_id $order ";
  1224. $res = Database::query($sql);
  1225. $list = array();
  1226. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1227. // Checking if this attempt was revised by a teacher
  1228. $sql = 'SELECT exe_id FROM '.$table_track_attempt_recording.'
  1229. WHERE author != "" AND exe_id = '.$row['exe_id'].'
  1230. LIMIT 1';
  1231. $res_revised = Database::query($sql);
  1232. $row['attempt_revised'] = 0;
  1233. if (Database::num_rows($res_revised) > 0) {
  1234. $row['attempt_revised'] = 1;
  1235. }
  1236. $list[$row['exe_id']] = $row;
  1237. $sql = "SELECT * FROM $table_track_attempt
  1238. WHERE exe_id = {$row['exe_id']}";
  1239. $res_question = Database::query($sql);
  1240. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1241. $list[$row['exe_id']]['question_list'][$row_q['question_id']][] = $row_q;
  1242. }
  1243. }
  1244. return $list;
  1245. }
  1246. /**
  1247. * Count exercise attempts (NO Exercises in LPs ) from a given exercise id, course, session
  1248. * @param int $user_id
  1249. * @param int exercise id
  1250. * @param int $courseId
  1251. * @param int session id
  1252. * @return array with the results
  1253. *
  1254. */
  1255. public static function count_exercise_attempts_by_user($user_id, $exercise_id, $courseId, $session_id = 0)
  1256. {
  1257. $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1258. $courseId = intval($courseId);
  1259. $exercise_id = intval($exercise_id);
  1260. $session_id = intval($session_id);
  1261. $user_id = intval($user_id);
  1262. $sql = "SELECT count(*) as count FROM $TABLETRACK_EXERCICES
  1263. WHERE status = '' AND
  1264. exe_user_id = '$user_id' 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. $result = 0;
  1273. if (Database::num_rows($res) > 0) {
  1274. $row = Database::fetch_array($res, 'ASSOC');
  1275. $result = $row['count'];
  1276. }
  1277. return $result;
  1278. }
  1279. /**
  1280. * Gets all exercise BEST results attempts (NO Exercises in LPs) from a given exercise id, course, session per user
  1281. * @param int $exercise_id
  1282. * @param int $courseId
  1283. * @param int $session_id
  1284. * @return array with the results
  1285. * @todo rename this function
  1286. */
  1287. public static function get_best_exercise_results_by_user($exercise_id, $courseId, $session_id = 0)
  1288. {
  1289. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1290. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1291. $courseId = intval($courseId);
  1292. $exercise_id = intval($exercise_id);
  1293. $session_id = intval($session_id);
  1294. $sql = "SELECT * FROM $table_track_exercises
  1295. WHERE
  1296. status = '' AND
  1297. c_id = $courseId AND
  1298. exe_exo_id = '$exercise_id' AND
  1299. session_id = $session_id AND
  1300. orig_lp_id = 0 AND
  1301. orig_lp_item_id = 0
  1302. ORDER BY exe_id";
  1303. $res = Database::query($sql);
  1304. $list = array();
  1305. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1306. $list[$row['exe_id']] = $row;
  1307. $sql = "SELECT * FROM $table_track_attempt
  1308. WHERE exe_id = {$row['exe_id']}";
  1309. $res_question = Database::query($sql);
  1310. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1311. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1312. }
  1313. }
  1314. // Getting the best results of every student
  1315. $best_score_return = array();
  1316. foreach ($list as $student_result) {
  1317. $user_id = $student_result['exe_user_id'];
  1318. $current_best_score[$user_id] = $student_result['exe_result'];
  1319. //echo $current_best_score[$user_id].' - '.$best_score_return[$user_id]['exe_result'].'<br />';
  1320. if (!isset($best_score_return[$user_id]['exe_result'])) {
  1321. $best_score_return[$user_id] = $student_result;
  1322. }
  1323. if ($current_best_score[$user_id] > $best_score_return[$user_id]['exe_result']) {
  1324. $best_score_return[$user_id] = $student_result;
  1325. }
  1326. }
  1327. return $best_score_return;
  1328. }
  1329. /**
  1330. * @param int $user_id
  1331. * @param int $exercise_id
  1332. * @param int $courseId
  1333. * @param int $session_id
  1334. * @return array
  1335. */
  1336. public static function get_best_attempt_exercise_results_per_user($user_id, $exercise_id, $courseId, $session_id = 0)
  1337. {
  1338. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1339. $courseId = intval($courseId);
  1340. $exercise_id = intval($exercise_id);
  1341. $session_id = intval($session_id);
  1342. $user_id = intval($user_id);
  1343. $sql = "SELECT * FROM $table_track_exercises
  1344. WHERE
  1345. status = '' AND
  1346. c_id = '$courseId' AND
  1347. exe_exo_id = '$exercise_id' AND
  1348. session_id = $session_id AND
  1349. exe_user_id = $user_id AND
  1350. orig_lp_id =0 AND
  1351. orig_lp_item_id = 0
  1352. ORDER BY exe_id";
  1353. $res = Database::query($sql);
  1354. $list = array();
  1355. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1356. $list[$row['exe_id']] = $row;
  1357. }
  1358. //Getting the best results of every student
  1359. $best_score_return = array();
  1360. $best_score_return['exe_result'] = 0;
  1361. foreach ($list as $result) {
  1362. $current_best_score = $result;
  1363. if ($current_best_score['exe_result'] > $best_score_return['exe_result']) {
  1364. $best_score_return = $result;
  1365. }
  1366. }
  1367. if (!isset($best_score_return['exe_weighting'])) {
  1368. $best_score_return = array();
  1369. }
  1370. return $best_score_return;
  1371. }
  1372. /**
  1373. * @param int $exercise_id
  1374. * @param int $courseId
  1375. * @param int $session_id
  1376. * @return mixed
  1377. */
  1378. public static function count_exercise_result_not_validated($exercise_id, $courseId, $session_id = 0)
  1379. {
  1380. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1381. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1382. $courseId = intval($courseId);
  1383. $session_id = intval($session_id);
  1384. $exercise_id = intval($exercise_id);
  1385. $sql = "SELECT count(e.exe_id) as count
  1386. FROM $table_track_exercises e
  1387. LEFT JOIN $table_track_attempt a
  1388. ON e.exe_id = a.exe_id
  1389. WHERE
  1390. exe_exo_id = $exercise_id AND
  1391. c_id = '$courseId' AND
  1392. e.session_id = $session_id AND
  1393. orig_lp_id = 0 AND
  1394. marks IS NULL AND
  1395. status = '' AND
  1396. orig_lp_item_id = 0
  1397. ORDER BY e.exe_id";
  1398. $res = Database::query($sql);
  1399. $row = Database::fetch_array($res, 'ASSOC');
  1400. return $row['count'];
  1401. }
  1402. /**
  1403. * Gets all exercise BEST results attempts (NO Exercises in LPs) from a given exercise id, course, session per user
  1404. * @param int exercise id
  1405. * @param int course id
  1406. * @param int session id
  1407. * @return array with the results
  1408. *
  1409. */
  1410. public static function get_count_exercises_attempted_by_course($courseId, $session_id = 0)
  1411. {
  1412. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1413. $courseId = intval($courseId);
  1414. $session_id = intval($session_id);
  1415. $sql = "SELECT DISTINCT exe_exo_id, exe_user_id
  1416. FROM $table_track_exercises
  1417. WHERE
  1418. status = '' AND
  1419. c_id = '$courseId' AND
  1420. session_id = $session_id AND
  1421. orig_lp_id =0 AND
  1422. orig_lp_item_id = 0
  1423. ORDER BY exe_id";
  1424. $res = Database::query($sql);
  1425. $count = 0;
  1426. if (Database::num_rows($res) > 0) {
  1427. $count = Database::num_rows($res);
  1428. }
  1429. return $count;
  1430. }
  1431. /**
  1432. * Gets all exercise events from a Learning Path within a Course nd Session
  1433. * @param int $exercise_id
  1434. * @param int $courseId
  1435. * @param int $session_id
  1436. * @return array
  1437. */
  1438. public static function get_all_exercise_event_from_lp($exercise_id, $courseId, $session_id = 0)
  1439. {
  1440. $table_track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1441. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1442. $courseId = intval($courseId);
  1443. $exercise_id = intval($exercise_id);
  1444. $session_id = intval($session_id);
  1445. $sql = "SELECT * FROM $table_track_exercises
  1446. WHERE
  1447. status = '' AND
  1448. c_id = $courseId AND
  1449. exe_exo_id = '$exercise_id' AND
  1450. session_id = $session_id AND
  1451. orig_lp_id !=0 AND
  1452. orig_lp_item_id != 0";
  1453. $res = Database::query($sql);
  1454. $list = array();
  1455. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1456. $list[$row['exe_id']] = $row;
  1457. $sql = "SELECT * FROM $table_track_attempt
  1458. WHERE exe_id = {$row['exe_id']}";
  1459. $res_question = Database::query($sql);
  1460. while ($row_q = Database::fetch_array($res_question, 'ASSOC')) {
  1461. $list[$row['exe_id']]['question_list'][$row_q['question_id']] = $row_q;
  1462. }
  1463. }
  1464. return $list;
  1465. }
  1466. /**
  1467. * @param int $lp_id
  1468. * @param int $course_id
  1469. *
  1470. * @return array
  1471. */
  1472. public static function get_all_exercises_from_lp($lp_id, $course_id)
  1473. {
  1474. $lp_item_table = Database::get_course_table(TABLE_LP_ITEM);
  1475. $course_id = intval($course_id);
  1476. $lp_id = intval($lp_id);
  1477. $sql = "SELECT * FROM $lp_item_table
  1478. WHERE
  1479. c_id = $course_id AND
  1480. lp_id = '".$lp_id."' AND
  1481. item_type = 'quiz'
  1482. ORDER BY parent_item_id, display_order";
  1483. $res = Database::query($sql);
  1484. $my_exercise_list = array();
  1485. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1486. $my_exercise_list[] = $row;
  1487. }
  1488. return $my_exercise_list;
  1489. }
  1490. /**
  1491. * This function gets the comments of an exercise
  1492. *
  1493. * @param int $exe_id
  1494. * @param int $question_id
  1495. *
  1496. * @return string the comment
  1497. */
  1498. public static function get_comments($exe_id, $question_id)
  1499. {
  1500. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1501. $sql = "SELECT teacher_comment
  1502. FROM $table_track_attempt
  1503. WHERE
  1504. exe_id='".Database::escape_string($exe_id)."' AND
  1505. question_id = '".Database::escape_string($question_id)."'
  1506. ORDER by question_id";
  1507. $sqlres = Database::query($sql);
  1508. $comm = Database::result($sqlres, 0, 'teacher_comment');
  1509. return $comm;
  1510. }
  1511. /**
  1512. * @param int $exe_id
  1513. *
  1514. * @return array
  1515. */
  1516. public static function getAllExerciseEventByExeId($exe_id)
  1517. {
  1518. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1519. $exe_id = intval($exe_id);
  1520. $list = array();
  1521. $sql = "SELECT * FROM $table_track_attempt
  1522. WHERE exe_id = $exe_id
  1523. ORDER BY position";
  1524. $res_question = Database::query($sql);
  1525. if (Database::num_rows($res_question)) {
  1526. while ($row = Database::fetch_array($res_question, 'ASSOC')) {
  1527. $list[$row['question_id']][] = $row;
  1528. }
  1529. }
  1530. return $list;
  1531. }
  1532. /**
  1533. *
  1534. * @param int $exe_id
  1535. * @param int $user_id
  1536. * @param int $courseId
  1537. * @param int $session_id
  1538. * @param int $question_id
  1539. */
  1540. public static function delete_attempt($exe_id, $user_id, $courseId, $session_id, $question_id)
  1541. {
  1542. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1543. $exe_id = intval($exe_id);
  1544. $user_id = intval($user_id);
  1545. $courseId = intval($courseId);
  1546. $session_id = intval($session_id);
  1547. $question_id = intval($question_id);
  1548. $sql = "DELETE FROM $table_track_attempt
  1549. WHERE
  1550. exe_id = $exe_id AND
  1551. user_id = $user_id AND
  1552. c_id = $courseId AND
  1553. session_id = $session_id AND
  1554. question_id = $question_id ";
  1555. Database::query($sql);
  1556. self::addEvent(
  1557. LOG_QUESTION_RESULT_DELETE,
  1558. LOG_EXERCISE_ATTEMPT_QUESTION_ID,
  1559. $exe_id . '-' . $question_id,
  1560. null,
  1561. null,
  1562. $courseId,
  1563. $session_id
  1564. );
  1565. }
  1566. /**
  1567. * @param $exe_id
  1568. * @param $user_id
  1569. * @param int $courseId
  1570. * @param $question_id
  1571. * @param int $sessionId
  1572. */
  1573. public static function delete_attempt_hotspot($exe_id, $user_id, $courseId, $question_id, $sessionId = null)
  1574. {
  1575. $table_track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1576. $exe_id = intval($exe_id);
  1577. $user_id = intval($user_id);
  1578. $courseId = intval($courseId);
  1579. $question_id = intval($question_id);
  1580. if (!isset($sessionId)) {
  1581. $sessionId = api_get_session_id();
  1582. }
  1583. $sql = "DELETE FROM $table_track_attempt
  1584. WHERE
  1585. hotspot_exe_id = $exe_id AND
  1586. hotspot_user_id = $user_id AND
  1587. c_id = $courseId AND
  1588. hotspot_question_id = $question_id ";
  1589. Database::query($sql);
  1590. self::addEvent(
  1591. LOG_QUESTION_RESULT_DELETE,
  1592. LOG_EXERCISE_ATTEMPT_QUESTION_ID,
  1593. $exe_id . '-' . $question_id,
  1594. null,
  1595. null,
  1596. $courseId,
  1597. $sessionId
  1598. );
  1599. }
  1600. /**
  1601. * Registers in track_e_course_access when user logs in for the first time to a course
  1602. * @param int $courseId ID of the course
  1603. * @param int $user_id ID of the user
  1604. * @param int $session_id ID of the session (if any)
  1605. */
  1606. public static function event_course_login($courseId, $user_id, $session_id)
  1607. {
  1608. $course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1609. $loginDate = $logoutDate = api_get_utc_datetime();
  1610. //$counter represents the number of time this record has been refreshed
  1611. $counter = 1;
  1612. $courseId = intval($courseId);
  1613. $user_id = intval($user_id);
  1614. $session_id = intval($session_id);
  1615. $ip = api_get_real_ip();
  1616. $sql = "INSERT INTO $course_tracking_table(c_id, user_ip, user_id, login_course_date, logout_course_date, counter, session_id)
  1617. VALUES('".$courseId."', '".$ip."', '".$user_id."', '$loginDate', '$logoutDate', $counter, '".$session_id."')";
  1618. Database::query($sql);
  1619. // Course catalog stats modifications see #4191
  1620. CourseManager::update_course_ranking(
  1621. null,
  1622. null,
  1623. null,
  1624. null,
  1625. true,
  1626. false
  1627. );
  1628. }
  1629. /**
  1630. * Register a "fake" time spent on the platform, for example to match the
  1631. * estimated time he took to author an assignment/work, see configuration
  1632. * setting considered_working_time.
  1633. * This assumes there is already some connection of the student to the
  1634. * course, otherwise he wouldn't be able to upload an assignment.
  1635. * This works by creating a new record, copy of the current one, then
  1636. * updating the current one to be just the considered_working_time and
  1637. * end at the same second as the user connected to the course.
  1638. * @param int $courseId The course in which to add the time
  1639. * @param int $userId The user for whom to add the time
  1640. * @param int $sessionId The session in which to add the time (if any)
  1641. * @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.
  1642. * @param string $ip IP address to go on record for this time record
  1643. *
  1644. * @return True on successful insertion, false otherwise
  1645. */
  1646. public static function eventAddVirtualCourseTime(
  1647. $courseId,
  1648. $userId,
  1649. $sessionId,
  1650. $virtualTime = '',
  1651. $ip = ''
  1652. ) {
  1653. $courseTrackingTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1654. $time = $loginDate = $logoutDate = api_get_utc_datetime();
  1655. $courseId = intval($courseId);
  1656. $userId = intval($userId);
  1657. $sessionId = intval($sessionId);
  1658. $ip = Database::escape_string($ip);
  1659. // Get the current latest course connection register. We need that
  1660. // record to re-use the data and create a new record.
  1661. $sql = "SELECT *
  1662. FROM $courseTrackingTable
  1663. WHERE
  1664. user_id = ".$userId." AND
  1665. c_id = ".$courseId." AND
  1666. session_id = ".$sessionId." AND
  1667. login_course_date > '$time' - INTERVAL 3600 SECOND
  1668. ORDER BY login_course_date DESC LIMIT 0,1";
  1669. $result = Database::query($sql);
  1670. // Ignore if we didn't find any course connection record in the last
  1671. // hour. In this case it wouldn't be right to add a "fake" time record.
  1672. if (Database::num_rows($result) > 0) {
  1673. // Found the latest connection
  1674. $row = Database::fetch_row($result);
  1675. $courseAccessId = $row[0];
  1676. $courseAccessLoginDate = $row[3];
  1677. $counter = $row[5];
  1678. $counter = $counter ? $counter : 0;
  1679. // Insert a new record, copy of the current one (except the logout
  1680. // date that we update to the current time)
  1681. $sql = "INSERT INTO $courseTrackingTable(
  1682. c_id,
  1683. user_ip,
  1684. user_id,
  1685. login_course_date,
  1686. logout_course_date,
  1687. counter,
  1688. session_id
  1689. ) VALUES(
  1690. $courseId,
  1691. '$ip',
  1692. $userId,
  1693. '$courseAccessLoginDate',
  1694. '$logoutDate',
  1695. $counter,
  1696. $sessionId
  1697. )";
  1698. Database::query($sql);
  1699. $loginDate = ChamiloApi::addOrSubTimeToDateTime(
  1700. $virtualTime,
  1701. $courseAccessLoginDate,
  1702. false
  1703. );
  1704. // We update the course tracking table
  1705. $sql = "UPDATE $courseTrackingTable
  1706. SET
  1707. login_course_date = '$loginDate',
  1708. logout_course_date = '$courseAccessLoginDate',
  1709. counter = 0
  1710. WHERE
  1711. course_access_id = ".intval($courseAccessId)." AND
  1712. session_id = ".$sessionId;
  1713. Database::query($sql);
  1714. return true;
  1715. }
  1716. return false;
  1717. }
  1718. /**
  1719. * Removes a "fake" time spent on the platform, for example to match the
  1720. * estimated time he took to author an assignment/work, see configuration
  1721. * setting considered_working_time.
  1722. * This method should be called when something that generated a fake
  1723. * time record is removed. Given the database link is weak (no real
  1724. * relationship kept between the deleted item and this record), this
  1725. * method just looks for the latest record that has the same time as the
  1726. * item's fake time, is in the past and in this course+session. If such a
  1727. * record cannot be found, it doesn't do anything.
  1728. * The IP address is not considered a useful filter here.
  1729. * @param int $courseId The course in which to add the time
  1730. * @param int $userId The user for whom to add the time
  1731. * @param int $sessionId The session in which to add the time (if any)
  1732. * @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.
  1733. * @return True on successful removal, false otherwise
  1734. */
  1735. public static function eventRemoveVirtualCourseTime($courseId, $userId, $sessionId = 0, $virtualTime = '')
  1736. {
  1737. if (empty($virtualTime)) {
  1738. return false;
  1739. }
  1740. $courseTrackingTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1741. $time = $loginDate = $logoutDate = api_get_utc_datetime();
  1742. $courseId = intval($courseId);
  1743. $userId = intval($userId);
  1744. $sessionId = intval($sessionId);
  1745. // Change $virtualTime format from hh:mm:ss to hhmmss which is the
  1746. // format returned by SQL for a subtraction of two datetime values
  1747. // @todo make sure this is portable between DBMSes
  1748. if (preg_match('/:/', $virtualTime)) {
  1749. list($h, $m, $s) = preg_split('/:/', $virtualTime);
  1750. $virtualTime = $h * 3600 + $m * 60 + $s;
  1751. } else {
  1752. $virtualTime *= 3600;
  1753. }
  1754. // Get the current latest course connection register. We need that
  1755. // record to re-use the data and create a new record.
  1756. $sql = "SELECT course_access_id
  1757. FROM $courseTrackingTable
  1758. WHERE
  1759. user_id = $userId AND
  1760. c_id = $courseId AND
  1761. session_id = $sessionId AND
  1762. counter = 0 AND
  1763. (UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date)) = '$virtualTime'
  1764. ORDER BY login_course_date DESC LIMIT 0,1";
  1765. $result = Database::query($sql);
  1766. // Ignore if we didn't find any course connection record in the last
  1767. // hour. In this case it wouldn't be right to add a "fake" time record.
  1768. if (Database::num_rows($result) > 0) {
  1769. // Found the latest connection
  1770. $row = Database::fetch_row($result);
  1771. $courseAccessId = $row[0];
  1772. $sql = "DELETE FROM $courseTrackingTable WHERE course_access_id = $courseAccessId";
  1773. $result = Database::query($sql);
  1774. return $result;
  1775. }
  1776. return false;
  1777. }
  1778. /**
  1779. * For the sake of genericity, this function is a switch.
  1780. * It's called by EventsDispatcher and fires the good function
  1781. * with the good require_once.
  1782. *
  1783. * @param string $event_name
  1784. * @param array $params
  1785. */
  1786. public static function event_send_mail($event_name, $params)
  1787. {
  1788. EventsMail::send_mail($event_name, $params);
  1789. }
  1790. /**
  1791. * Internal function checking if the mail was already sent from that user to that user
  1792. * @param string $event_name
  1793. * @param int $user_from
  1794. * @param int $user_to
  1795. * @return boolean
  1796. */
  1797. public static function check_if_mail_already_sent($event_name, $user_from, $user_to = null)
  1798. {
  1799. if ($user_to == null) {
  1800. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1801. WHERE user_from = '.$user_from.' AND event_type_name = "'.$event_name.'"';
  1802. } else {
  1803. $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_SENT).'
  1804. WHERE user_from = '.$user_from.' AND user_to = '.$user_to.' AND event_type_name = "'.$event_name.'"';
  1805. }
  1806. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1807. return $result[0]["total"];
  1808. }
  1809. /**
  1810. *
  1811. * Filter EventEmailTemplate Filters see the main/inc/conf/events.conf.dist.php
  1812. *
  1813. */
  1814. /**
  1815. * Basic template event message filter (to be used by other filters as default)
  1816. * @param array $values (passing by reference)
  1817. * @return boolean True if everything is OK, false otherwise
  1818. */
  1819. public function event_send_mail_filter_func(&$values)
  1820. {
  1821. return true;
  1822. }
  1823. /**
  1824. * user_registration - send_mail filter
  1825. * @param array $values (passing by reference)
  1826. * @return boolean True if everything is OK, false otherwise
  1827. */
  1828. public function user_registration_event_send_mail_filter_func(&$values)
  1829. {
  1830. $res = self::event_send_mail_filter_func($values);
  1831. // proper logic for this filter
  1832. return $res;
  1833. }
  1834. /**
  1835. * portal_homepage_edited - send_mail filter
  1836. * @param array $values (passing by reference)
  1837. * @return boolean True if everything is OK, false otherwise
  1838. */
  1839. public function portal_homepage_edited_event_send_mail_filter_func(&$values)
  1840. {
  1841. $res = self::event_send_mail_filter_func($values);
  1842. // proper logic for this filter
  1843. return $res;
  1844. }
  1845. }