events.lib.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Sebastien Piraux
  9. Copyright (c) Toon Van Hoecke
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * EVENTS LIBRARY
  23. *
  24. * This is the events library for Dokeos.
  25. * Include/require it in your code to use its functionality.
  26. * Functions of this library are used to record informations when some kind
  27. * of event occur. Each event has his own types of informations then each event
  28. * use its own function.
  29. *
  30. * @package dokeos.library
  31. * @todo convert queries to use Database API
  32. ==============================================================================
  33. */
  34. /*
  35. ==============================================================================
  36. INIT SECTION
  37. ==============================================================================
  38. */
  39. // REGROUP TABLE NAMES FOR MAINTENANCE PURPOSE
  40. $TABLETRACK_LOGIN = $_configuration['statistics_database']."`.`track_e_login";
  41. $TABLETRACK_OPEN = $_configuration['statistics_database']."`.`track_e_open";
  42. $TABLETRACK_ACCESS = $_configuration['statistics_database']."`.`track_e_access";
  43. $TABLETRACK_DOWNLOADS = $_configuration['statistics_database']."`.`track_e_downloads";
  44. $TABLETRACK_UPLOADS = $_configuration['statistics_database']."`.`track_e_uploads";
  45. $TABLETRACK_LINKS = $_configuration['statistics_database']."`.`track_e_links";
  46. $TABLETRACK_EXERCICES = $_configuration['statistics_database']."`.`track_e_exercices";
  47. $TABLETRACK_SUBSCRIPTIONS = $_configuration['statistics_database']."`.`track_e_subscriptions";
  48. $TABLETRACK_LASTACCESS = $_configuration['statistics_database']."`.`track_e_lastaccess"; //for "what's new" notification
  49. /*
  50. ==============================================================================
  51. FUNCTIONS
  52. ==============================================================================
  53. */
  54. /**
  55. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  56. * @desc Record information for open event (when homepage is opened)
  57. */
  58. function event_open()
  59. {
  60. global $_configuration;
  61. global $TABLETRACK_OPEN;
  62. // if tracking is disabled record nothing
  63. if (!$_configuration['tracking_enabled'])
  64. {
  65. return 0;
  66. }
  67. // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information
  68. // $_SERVER['HTTP_USER_AGENT'] : will provide browser and os information
  69. // $_SERVER['HTTP_REFERER'] : provide information about refering url
  70. $referer = $_SERVER['HTTP_REFERER'];
  71. // record informations only if user comes from another site
  72. //if(!eregi($_configuration['root_web'],$referer))
  73. $pos = strpos($referer, $_configuration['root_web']);
  74. if ($pos === false)
  75. {
  76. $remhost = @ getHostByAddr($_SERVER['REMOTE_ADDR']);
  77. if ($remhost == $_SERVER['REMOTE_ADDR'])
  78. $remhost = "Unknown"; // don't change this
  79. $reallyNow = time();
  80. $sql = "INSERT INTO `".$TABLETRACK_OPEN."`
  81. (`open_remote_host`,
  82. `open_agent`,
  83. `open_referer`,
  84. `open_date`)
  85. VALUES
  86. ('".$remhost."',
  87. '".$_SERVER['HTTP_USER_AGENT']."', '".$referer."', FROM_UNIXTIME($reallyNow) )";
  88. $res = api_sql_query($sql,__FILE__,__LINE__);
  89. //$mysql_query($sql);
  90. }
  91. return 1;
  92. }
  93. /**
  94. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  95. * @desc Record information for login event
  96. * (when an user identifies himself with username & password)
  97. */
  98. function event_login()
  99. {
  100. global $_configuration;
  101. global $_user;
  102. global $TABLETRACK_LOGIN;
  103. // if tracking is disabled record nothing
  104. if (!$_configuration['tracking_enabled'])
  105. {
  106. return 0;
  107. }
  108. $reallyNow = time();
  109. $sql = "INSERT INTO `".$TABLETRACK_LOGIN."`
  110. (`login_user_id`,
  111. `login_ip`,
  112. `login_date`)
  113. VALUES
  114. ('".$_user['user_id']."',
  115. '".$_SERVER['REMOTE_ADDR']."',
  116. FROM_UNIXTIME(".$reallyNow."))";
  117. $res = api_sql_query($sql,__FILE__,__LINE__);
  118. //$mysql_query($sql);
  119. //return 0;
  120. }
  121. /**
  122. * @param tool name of the tool (name in mainDb.accueil table)
  123. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  124. * @desc Record information for access event for courses
  125. */
  126. function event_access_course()
  127. {
  128. global $_configuration;
  129. global $_user;
  130. global $_cid;
  131. global $TABLETRACK_ACCESS;
  132. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  133. // if tracking is disabled record nothing
  134. if (!$_configuration['tracking_enabled'])
  135. {
  136. return 0;
  137. }
  138. if(api_get_setting('use_session_mode')=='true' && isset($_SESSION['id_session']))
  139. {
  140. $id_session = intval($_SESSION['id_session']);
  141. }
  142. else
  143. {
  144. $id_session = 0;
  145. }
  146. $reallyNow = time();
  147. if ($_user['user_id'])
  148. {
  149. $user_id = "'".$_user['user_id']."'";
  150. }
  151. else // anonymous
  152. {
  153. $user_id = "NULL";
  154. }
  155. $sql = "INSERT INTO `".$TABLETRACK_ACCESS."`
  156. (`access_user_id`,
  157. `access_cours_code`,
  158. `access_date`)
  159. VALUES
  160. (".$user_id.",
  161. '".$_cid."',
  162. FROM_UNIXTIME(".$reallyNow."))";
  163. $res = api_sql_query($sql,__FILE__,__LINE__);
  164. // added for "what's new" notification
  165. $sql = " UPDATE `$TABLETRACK_LASTACCESS`
  166. SET access_date = FROM_UNIXTIME($reallyNow)
  167. WHERE `access_user_id` = ".$user_id." AND `access_cours_code` = '".$_cid."' AND `access_tool` IS NULL AND `access_session_id`=".$id_session;
  168. $res = api_sql_query($sql,__FILE__,__LINE__);
  169. if (mysql_affected_rows() == 0)
  170. {
  171. $sql = " INSERT INTO `$TABLETRACK_LASTACCESS`
  172. (`access_user_id`,`access_cours_code`,`access_date`, access_session_id)
  173. VALUES
  174. (".$user_id.", '".$_cid."', FROM_UNIXTIME($reallyNow), ".$id_session.")";
  175. $res = api_sql_query($sql,__FILE__,__LINE__);
  176. }
  177. // end "what's new" notification
  178. return 1;
  179. }
  180. /**
  181. * @param tool name of the tool (name in mainDb.accueil table)
  182. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  183. * @desc Record information for access event for tools
  184. *
  185. * $tool can take this values :
  186. * Links, Calendar, Document, Announcements,
  187. * Group, Video, Works, Users, Exercices, Course Desc
  188. * ...
  189. * Values can be added if new modules are created (15char max)
  190. * I encourage to use $nameTool as $tool when calling this function
  191. *
  192. * Functionality for "what's new" notification is added by Toon Van Hoecke
  193. */
  194. function event_access_tool($tool, $id_session=0)
  195. {
  196. global $_configuration;
  197. // if tracking is disabled record nothing
  198. // if( ! $_configuration['tracking_enabled'] ) return 0; //commented because "what's new" notification must always occur
  199. global $_user;
  200. global $_cid;
  201. global $TABLETRACK_ACCESS;
  202. global $_configuration;
  203. global $_course;
  204. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  205. if(api_get_setting('use_session_mode')=='true' && isset($_SESSION['id_session']))
  206. {
  207. $id_session = intval($_SESSION['id_session']);
  208. }
  209. else
  210. {
  211. $id_session = 0;
  212. }
  213. $reallyNow = time();
  214. $user_id = $_user['user_id'] ? "'".$_user['user_id']."'" : "NULL"; // "NULL" is anonymous
  215. // record information
  216. // only if user comes from the course $_cid
  217. //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
  218. //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
  219. $pos = strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$_course['path']));
  220. // added for "what's new" notification
  221. $pos2 = strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower($_configuration['root_web']."index"));
  222. // end "what's new" notification
  223. if ($_configuration['tracking_enabled'] && ($pos !== false || $pos2 !== false))
  224. {
  225. $sql = "INSERT INTO `".$TABLETRACK_ACCESS."`
  226. (`access_user_id`,
  227. `access_cours_code`,
  228. `access_tool`,
  229. `access_date`)
  230. VALUES
  231. (".$user_id.",".// Don't add ' ' around value, it's already done.
  232. "'".$_cid."' ,
  233. '".htmlspecialchars($tool, ENT_QUOTES)."',
  234. FROM_UNIXTIME(".$reallyNow."))";
  235. $res = api_sql_query($sql,__FILE__,__LINE__);
  236. }
  237. // "what's new" notification
  238. $sql = " UPDATE `$TABLETRACK_LASTACCESS`
  239. SET access_date = FROM_UNIXTIME($reallyNow)
  240. WHERE `access_user_id` = ".$user_id." AND `access_cours_code` = '".$_cid."' AND `access_tool` = '".htmlspecialchars($tool, ENT_QUOTES)."' AND `access_session_id`=".$id_session;
  241. $res = api_sql_query($sql,__FILE__,__LINE__);
  242. if (mysql_affected_rows() == 0)
  243. {
  244. $sql = "INSERT INTO `$TABLETRACK_LASTACCESS`
  245. (`access_user_id`,`access_cours_code`,`access_tool`, `access_date`, `access_session_id`)
  246. VALUES
  247. (".$user_id.", '".$_cid."' , '".htmlspecialchars($tool, ENT_QUOTES)."', FROM_UNIXTIME($reallyNow), $id_session)";
  248. $res = api_sql_query($sql,__FILE__,__LINE__);
  249. }
  250. return 1;
  251. }
  252. /**
  253. * @param doc_id id of document (id in mainDb.document table)
  254. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  255. * @desc Record information for download event
  256. * (when an user click to d/l a document)
  257. * it will be used in a redirection page
  258. * bug fixed: Roan Embrechts
  259. * Roan:
  260. * The user id is put in single quotes,
  261. * (why? perhaps to prevent sql insertion hacks?)
  262. * and later again.
  263. * Doing this twice causes an error, I remove one of them.
  264. */
  265. function event_download($doc_url)
  266. {
  267. global $_configuration;
  268. global $_user;
  269. global $_cid;
  270. global $TABLETRACK_DOWNLOADS;
  271. // if tracking is disabled record nothing
  272. if (!$_configuration['tracking_enabled'])
  273. {
  274. return 0;
  275. }
  276. $reallyNow = time();
  277. if ($_user['user_id'])
  278. {
  279. $user_id = "'".$_user['user_id']."'";
  280. }
  281. else // anonymous
  282. {
  283. $user_id = "NULL";
  284. }
  285. $sql = "INSERT INTO `".$TABLETRACK_DOWNLOADS."`
  286. (
  287. `down_user_id`,
  288. `down_cours_id`,
  289. `down_doc_path`,
  290. `down_date`
  291. )
  292. VALUES
  293. (
  294. ".$user_id.",
  295. '".$_cid."',
  296. '".htmlspecialchars($doc_url, ENT_QUOTES)."',
  297. FROM_UNIXTIME(".$reallyNow.")
  298. )";
  299. $res = api_sql_query($sql,__FILE__,__LINE__);
  300. //$mysql_query($sql);
  301. return 1;
  302. }
  303. /**
  304. * @param doc_id id of document (id in mainDb.document table)
  305. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  306. * @desc Record information for upload event
  307. * used in the works tool to record informations when
  308. * an user upload 1 work
  309. */
  310. function event_upload($doc_id)
  311. {
  312. global $_configuration;
  313. global $_user;
  314. global $_cid;
  315. global $TABLETRACK_UPLOADS;
  316. // if tracking is disabled record nothing
  317. if (!$_configuration['tracking_enabled'])
  318. {
  319. return 0;
  320. }
  321. $reallyNow = time();
  322. if ($_user['user_id'])
  323. {
  324. $user_id = "'".$_user['user_id']."'";
  325. }
  326. else // anonymous
  327. {
  328. $user_id = "NULL";
  329. }
  330. $sql = "INSERT INTO `".$TABLETRACK_UPLOADS."`
  331. (
  332. `upload_user_id`,
  333. `upload_cours_id`,
  334. `upload_work_id`,
  335. `upload_date`
  336. )
  337. VALUES
  338. (
  339. ".$user_id.",
  340. '".$_cid."',
  341. '".$doc_id."',
  342. FROM_UNIXTIME(".$reallyNow.")
  343. )";
  344. $res = api_sql_query($sql,__FILE__,__LINE__);
  345. //$mysql_query($sql);
  346. return 1;
  347. }
  348. /**
  349. * @param link_id (id in coursDb liens table)
  350. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  351. * @desc Record information for link event (when an user click on an added link)
  352. * it will be used in a redirection page
  353. */
  354. function event_link($link_id)
  355. {
  356. global $_configuration;
  357. global $_user;
  358. global $_cid;
  359. global $TABLETRACK_LINKS;
  360. // if tracking is disabled record nothing
  361. if (!$_configuration['tracking_enabled'])
  362. {
  363. return 0;
  364. }
  365. $reallyNow = time();
  366. if ($_user['user_id'])
  367. {
  368. $user_id = "'".$_user['user_id']."'";
  369. }
  370. else // anonymous
  371. {
  372. $user_id = "NULL";
  373. }
  374. $sql = "INSERT INTO `".$TABLETRACK_LINKS."`
  375. (
  376. `links_user_id`,
  377. `links_cours_id`,
  378. `links_link_id`,
  379. `links_date`
  380. )
  381. VALUES
  382. (
  383. ".$user_id.",
  384. '".$_cid."',
  385. '".$link_id."',
  386. FROM_UNIXTIME(".$reallyNow.")
  387. )";
  388. $res = api_sql_query($sql,__FILE__,__LINE__);
  389. //$mysql_query($sql);
  390. return 1;
  391. }
  392. /**
  393. * @param exo_id ( id in courseDb exercices table )
  394. * @param result ( score @ exercice )
  395. * @param weighting ( higher score )
  396. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  397. * @desc Record result of user when an exercice was done
  398. */
  399. function event_exercice($exo_id, $score, $weighting)
  400. {
  401. global $_configuration;
  402. global $_user;
  403. global $_cid;
  404. global $TABLETRACK_EXERCICES;
  405. global $origin, $learnpath_id, $learnpath_item_id;
  406. // if tracking is disabled record nothing
  407. if (!$_configuration['tracking_enabled'])
  408. {
  409. return 0;
  410. }
  411. $reallyNow = time();
  412. if ($_user['user_id'])
  413. {
  414. $user_id = "'".$_user['user_id']."'";
  415. }
  416. else // anonymous
  417. {
  418. $user_id = "NULL";
  419. }
  420. $sql = "INSERT INTO `".$TABLETRACK_EXERCICES."`
  421. (
  422. `exe_user_id`,
  423. `exe_cours_id`,
  424. `exe_exo_id`,
  425. `exe_result`,
  426. `exe_weighting`,
  427. `exe_date`
  428. )
  429. VALUES
  430. (
  431. ".$user_id.",
  432. '".$_cid."',
  433. '".$exo_id."',
  434. '".$score."',
  435. '".$weighting."',
  436. FROM_UNIXTIME(".$reallyNow.")
  437. )";
  438. /* SEE WHAT WE DO WITH THAT
  439. $tbl_learnpath_user = Database::get_course_table(TABLE_LEARNPATH_USER);
  440. if ($origin == 'learnpath')
  441. {
  442. if ($user_id == "NULL")
  443. {
  444. $user_id = '0';
  445. }
  446. $sql2 = "update $tbl_learnpath_user set score='$score' where (user_id=$user_id and learnpath_id='$learnpath_id' and learnpath_item_id='$learnpath_item_id')";
  447. $res2 = api_sql_query($sql2,__FILE__,__LINE__);
  448. }
  449. */
  450. $res = api_sql_query($sql,__FILE__,__LINE__);
  451. //$mysql_query($sql);
  452. //return 0;
  453. }
  454. function exercise_attempt($score,$answer,$quesId,$exeId,$j)
  455. {
  456. global $_configuration;
  457. global $_user;
  458. global $_cid;
  459. global $TABLETRACK_ATTEMPT;
  460. global $origin, $learnpath_id, $learnpath_item_id;
  461. // if tracking is disabled record nothing
  462. if (!$_configuration['tracking_enabled'])
  463. {
  464. return 0;
  465. }
  466. $reallyNow = time();
  467. if ($_user['user_id'])
  468. {
  469. $user_id = "'".$_user['user_id']."'";
  470. }
  471. else // anonymous
  472. {
  473. $user_id = "NULL";
  474. }
  475. $sql = "INSERT INTO `".$TABLETRACK_ATTEMPT."`
  476. (`exe_id`,
  477. `user_id`,
  478. `question_id`,
  479. `answer`,
  480. `marks`,
  481. `course_code`,
  482. `position`
  483. )
  484. VALUES
  485. (
  486. ".$exeId.",
  487. ".$user_id.",
  488. '".$quesId."',
  489. '".addslashes($answer)."',
  490. '".$score."',
  491. '".$_cid."',
  492. '".$j."'
  493. )";
  494. /* SEE WHAT WE DO WITH THAT
  495. $tbl_learnpath_user = Database::get_course_table(TABLE_LEARNPATH_USER);
  496. if ($origin == 'learnpath')
  497. {
  498. if ($user_id == "NULL")
  499. {
  500. $user_id = '0';
  501. }
  502. $sql2 = "update $tbl_learnpath_user set score='$score' where (user_id=$user_id and learnpath_id='$learnpath_id' and learnpath_item_id='$learnpath_item_id')";
  503. $res2 = api_sql_query($sql2,__FILE__,__LINE__);
  504. }
  505. */
  506. //$res = api_sql_query($sql,__FILE__,__LINE__);
  507. $res = mysql_query($sql) or die(mysql_error());
  508. //return 0;
  509. }
  510. ?>