events.lib.inc.php 15 KB

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