events.lib.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. <?php // $Id: events.lib.inc.php 17972 2009-01-23 20:11:22Z juliomontoya $
  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. if(isset($_SERVER['HTT_REFERER']))
  55. {
  56. $referer = Database::escape_string($_SERVER['HTTP_REFERER']);
  57. }
  58. else
  59. {
  60. $referer = '';
  61. }
  62. // record informations only if user comes from another site
  63. //if(!eregi($_configuration['root_web'],$referer))
  64. $pos = strpos($referer, $_configuration['root_web']);
  65. if ($pos === false && $referer != '')
  66. {
  67. $remhost = @ getHostByAddr($_SERVER['REMOTE_ADDR']);
  68. if ($remhost == $_SERVER['REMOTE_ADDR'])
  69. $remhost = "Unknown"; // don't change this
  70. $reallyNow = time();
  71. $sql = "INSERT INTO ".$TABLETRACK_OPEN."
  72. (open_remote_host,
  73. open_agent,
  74. open_referer,
  75. open_date)
  76. VALUES
  77. ('".$remhost."',
  78. '".Database::escape_string($_SERVER['HTTP_USER_AGENT'])."', '".$referer."', FROM_UNIXTIME($reallyNow) )";
  79. $res = api_sql_query($sql,__FILE__,__LINE__);
  80. }
  81. return 1;
  82. }
  83. /**
  84. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  85. * @desc Record information for login event
  86. * (when an user identifies himself with username & password)
  87. */
  88. function event_login()
  89. {
  90. global $_configuration;
  91. global $_user;
  92. global $TABLETRACK_LOGIN;
  93. // if tracking is disabled record nothing
  94. if (!$_configuration['tracking_enabled'])
  95. {
  96. return 0;
  97. }
  98. $reallyNow = time();
  99. $sql = "INSERT INTO ".$TABLETRACK_LOGIN."
  100. (login_user_id,
  101. login_ip,
  102. login_date)
  103. VALUES
  104. ('".$_user['user_id']."',
  105. '".Database::escape_string($_SERVER['REMOTE_ADDR'])."',
  106. FROM_UNIXTIME(".$reallyNow."))";
  107. $res = api_sql_query($sql,__FILE__,__LINE__);
  108. //$mysql_query($sql);
  109. //return 0;
  110. }
  111. /**
  112. * @param tool name of the tool (name in mainDb.accueil table)
  113. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  114. * @desc Record information for access event for courses
  115. */
  116. function event_access_course()
  117. {
  118. global $_configuration;
  119. global $_user;
  120. global $_cid;
  121. global $TABLETRACK_ACCESS;
  122. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  123. // if tracking is disabled record nothing
  124. if (!$_configuration['tracking_enabled'])
  125. {
  126. return 0;
  127. }
  128. if(api_get_setting('use_session_mode')=='true' && isset($_SESSION['id_session']))
  129. {
  130. $id_session = intval($_SESSION['id_session']);
  131. }
  132. else
  133. {
  134. $id_session = 0;
  135. }
  136. $reallyNow = time();
  137. if ($_user['user_id'])
  138. {
  139. $user_id = "'".$_user['user_id']."'";
  140. }
  141. else // anonymous
  142. {
  143. $user_id = "NULL";
  144. }
  145. $sql = "INSERT INTO ".$TABLETRACK_ACCESS."
  146. (access_user_id,
  147. access_cours_code,
  148. access_date)
  149. VALUES
  150. (".$user_id.",
  151. '".$_cid."',
  152. FROM_UNIXTIME(".$reallyNow."))";
  153. $res = api_sql_query($sql,__FILE__,__LINE__);
  154. // added for "what's new" notification
  155. $sql = " UPDATE $TABLETRACK_LASTACCESS
  156. SET access_date = FROM_UNIXTIME($reallyNow)
  157. WHERE access_user_id = ".$user_id." AND access_cours_code = '".$_cid."' AND access_tool IS NULL AND access_session_id=".$id_session;
  158. $res = api_sql_query($sql,__FILE__,__LINE__);
  159. if (Database::affected_rows() == 0)
  160. {
  161. $sql = " INSERT INTO $TABLETRACK_LASTACCESS
  162. (access_user_id,access_cours_code,access_date, access_session_id)
  163. VALUES
  164. (".$user_id.", '".$_cid."', FROM_UNIXTIME($reallyNow), ".$id_session.")";
  165. $res = api_sql_query($sql,__FILE__,__LINE__);
  166. }
  167. // end "what's new" notification
  168. return 1;
  169. }
  170. /**
  171. * @param tool name of the tool (name in mainDb.accueil table)
  172. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  173. * @desc Record information for access event for tools
  174. *
  175. * $tool can take this values :
  176. * Links, Calendar, Document, Announcements,
  177. * Group, Video, Works, Users, Exercices, Course Desc
  178. * ...
  179. * Values can be added if new modules are created (15char max)
  180. * I encourage to use $nameTool as $tool when calling this function
  181. *
  182. * Functionality for "what's new" notification is added by Toon Van Hoecke
  183. */
  184. function event_access_tool($tool, $id_session=0)
  185. {
  186. global $_configuration;
  187. // if tracking is disabled record nothing
  188. // if( ! $_configuration['tracking_enabled'] ) return 0; //commented because "what's new" notification must always occur
  189. global $_user;
  190. global $_cid;
  191. global $TABLETRACK_ACCESS;
  192. global $_configuration;
  193. global $_course;
  194. global $TABLETRACK_LASTACCESS; //for "what's new" notification
  195. if(api_get_setting('use_session_mode')=='true' && isset($_SESSION['id_session']))
  196. {
  197. $id_session = intval($_SESSION['id_session']);
  198. }
  199. else
  200. {
  201. $id_session = 0;
  202. }
  203. $reallyNow = time();
  204. $user_id = $_user['user_id'] ? "'".$_user['user_id']."'" : "NULL"; // "NULL" is anonymous
  205. // record information
  206. // only if user comes from the course $_cid
  207. //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
  208. //$pos = strpos($_SERVER['HTTP_REFERER'],$_configuration['root_web'].$_cid);
  209. $pos = strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower(api_get_path(WEB_COURSE_PATH).$_course['path']));
  210. // added for "what's new" notification
  211. $pos2 = strpos(strtolower($_SERVER['HTTP_REFERER']), strtolower($_configuration['root_web']."index"));
  212. // end "what's new" notification
  213. if ($_configuration['tracking_enabled'] && ($pos !== false || $pos2 !== false))
  214. {
  215. $sql = "INSERT INTO ".$TABLETRACK_ACCESS."
  216. (access_user_id,
  217. access_cours_code,
  218. access_tool,
  219. access_date)
  220. VALUES
  221. (".$user_id.",".// Don't add ' ' around value, it's already done.
  222. "'".$_cid."' ,
  223. '".htmlspecialchars($tool, ENT_QUOTES)."',
  224. FROM_UNIXTIME(".$reallyNow."))";
  225. $res = api_sql_query($sql,__FILE__,__LINE__);
  226. }
  227. // "what's new" notification
  228. $sql = " UPDATE $TABLETRACK_LASTACCESS
  229. SET access_date = FROM_UNIXTIME($reallyNow)
  230. WHERE access_user_id = ".$user_id." AND access_cours_code = '".$_cid."' AND access_tool = '".htmlspecialchars($tool, ENT_QUOTES)."' AND access_session_id=".$id_session;
  231. $res = api_sql_query($sql,__FILE__,__LINE__);
  232. if (Database::affected_rows() == 0)
  233. {
  234. $sql = "INSERT INTO $TABLETRACK_LASTACCESS
  235. (access_user_id,access_cours_code,access_tool, access_date, access_session_id)
  236. VALUES
  237. (".$user_id.", '".$_cid."' , '".htmlspecialchars($tool, ENT_QUOTES)."', FROM_UNIXTIME($reallyNow), $id_session)";
  238. $res = api_sql_query($sql,__FILE__,__LINE__);
  239. }
  240. return 1;
  241. }
  242. /**
  243. * @param doc_id id of document (id in mainDb.document table)
  244. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  245. * @desc Record information for download event
  246. * (when an user click to d/l a document)
  247. * it will be used in a redirection page
  248. * bug fixed: Roan Embrechts
  249. * Roan:
  250. * The user id is put in single quotes,
  251. * (why? perhaps to prevent sql insertion hacks?)
  252. * and later again.
  253. * Doing this twice causes an error, I remove one of them.
  254. */
  255. function event_download($doc_url)
  256. {
  257. global $_configuration;
  258. global $_user;
  259. global $_cid;
  260. global $TABLETRACK_DOWNLOADS;
  261. // if tracking is disabled record nothing
  262. if (!$_configuration['tracking_enabled'])
  263. {
  264. return 0;
  265. }
  266. $reallyNow = time();
  267. if ($_user['user_id'])
  268. {
  269. $user_id = "'".$_user['user_id']."'";
  270. }
  271. else // anonymous
  272. {
  273. $user_id = "NULL";
  274. }
  275. $sql = "INSERT INTO ".$TABLETRACK_DOWNLOADS."
  276. (
  277. down_user_id,
  278. down_cours_id,
  279. down_doc_path,
  280. down_date
  281. )
  282. VALUES
  283. (
  284. ".$user_id.",
  285. '".$_cid."',
  286. '".htmlspecialchars($doc_url, ENT_QUOTES)."',
  287. FROM_UNIXTIME(".$reallyNow.")
  288. )";
  289. $res = api_sql_query($sql,__FILE__,__LINE__);
  290. //$mysql_query($sql);
  291. return 1;
  292. }
  293. /**
  294. * @param doc_id id of document (id in mainDb.document table)
  295. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  296. * @desc Record information for upload event
  297. * used in the works tool to record informations when
  298. * an user upload 1 work
  299. */
  300. function event_upload($doc_id)
  301. {
  302. global $_configuration;
  303. global $_user;
  304. global $_cid;
  305. global $TABLETRACK_UPLOADS;
  306. // if tracking is disabled record nothing
  307. if (!$_configuration['tracking_enabled'])
  308. {
  309. return 0;
  310. }
  311. $reallyNow = time();
  312. if ($_user['user_id'])
  313. {
  314. $user_id = "'".$_user['user_id']."'";
  315. }
  316. else // anonymous
  317. {
  318. $user_id = "NULL";
  319. }
  320. $sql = "INSERT INTO ".$TABLETRACK_UPLOADS."
  321. (
  322. upload_user_id,
  323. upload_cours_id,
  324. upload_work_id,
  325. upload_date
  326. )
  327. VALUES
  328. (
  329. ".$user_id.",
  330. '".$_cid."',
  331. '".$doc_id."',
  332. FROM_UNIXTIME(".$reallyNow.")
  333. )";
  334. $res = api_sql_query($sql,__FILE__,__LINE__);
  335. //$mysql_query($sql);
  336. return 1;
  337. }
  338. /**
  339. * @param link_id (id in coursDb liens table)
  340. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  341. * @desc Record information for link event (when an user click on an added link)
  342. * it will be used in a redirection page
  343. */
  344. function event_link($link_id)
  345. {
  346. global $_configuration;
  347. global $_user;
  348. global $_cid;
  349. global $TABLETRACK_LINKS;
  350. // if tracking is disabled record nothing
  351. if (!$_configuration['tracking_enabled'])
  352. {
  353. return 0;
  354. }
  355. $reallyNow = time();
  356. if ($_user['user_id'])
  357. {
  358. $user_id = "'".$_user['user_id']."'";
  359. }
  360. else // anonymous
  361. {
  362. $user_id = "NULL";
  363. }
  364. $sql = "INSERT INTO ".$TABLETRACK_LINKS."
  365. (
  366. links_user_id,
  367. links_cours_id,
  368. links_link_id,
  369. links_date
  370. )
  371. VALUES
  372. (
  373. ".$user_id.",
  374. '".$_cid."',
  375. '".$link_id."',
  376. FROM_UNIXTIME(".$reallyNow.")
  377. )";
  378. $res = api_sql_query($sql,__FILE__,__LINE__);
  379. //$mysql_query($sql);
  380. return 1;
  381. }
  382. /**
  383. * @param exeid ( id of the exercise)
  384. * @param exo_id ( id in courseDb exercices table )
  385. * @param result ( score @ exercice )
  386. * @param weighting ( higher score )
  387. * @param duration ( duration of the attempt, in seconds )
  388. * @param session_id
  389. * @param learnpath_id (id of the learnpath)
  390. * @param learnpath_item_id (id of the learnpath_item)
  391. * @author Sebastien Piraux <piraux_seb@hotmail.com>
  392. * @author Julio Montoya Armas <gugli100@gmail.com>
  393. * @desc Record result of user when an exercice was done
  394. */
  395. function update_event_exercice($exeid,$exo_id, $score, $weighting,$session_id,$learnpath_id=0,$learnpath_item_id=0, $duration)
  396. {
  397. if ($exeid!='')
  398. {
  399. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  400. $reallyNow = time();
  401. $sql = "UPDATE $TABLETRACK_EXERCICES SET
  402. exe_exo_id = '".$exo_id."',
  403. exe_result = '".$score."',
  404. exe_weighting = '".$weighting."',
  405. session_id = '".$session_id."',
  406. orig_lp_id = '".$learnpath_id."',
  407. orig_lp_item_id = '".$learnpath_item_id."',
  408. exe_duration = '".$duration."',
  409. exe_date= FROM_UNIXTIME(".$reallyNow."),status = '', data_tracking='',start_date ='".$_SESSION['exercice_start_date']."'
  410. WHERE exe_id = '".$exeid."'";
  411. $res = @api_sql_query($sql,__FILE__,__LINE__);
  412. return $res;
  413. }
  414. else
  415. return false;
  416. }
  417. /**
  418. * This function creates an empty Exercise in STATISTIC_TRACK_E_EXERCICES table.
  419. * After that in exercise_result.php we call the update_event_exercice() to update the exercise
  420. * @return $id the last id registered
  421. * @author Julio Montoya <gugli100@gmail.com>
  422. * @desc Record result of user when an exercice was done
  423. */
  424. function create_event_exercice($exo_id)
  425. {
  426. global $_user, $_cid, $_configuration;
  427. $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  428. $reallyNow = time();
  429. if ($_user['user_id'])
  430. {
  431. $user_id = "'".$_user['user_id']."'";
  432. }
  433. else // anonymous
  434. {
  435. $user_id = "NULL";
  436. }
  437. if(defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  438. $condition = ' WHERE ' .
  439. 'exe_exo_id = '."'".$exo_id."'".' AND ' .
  440. 'exe_user_id = '."'".api_get_user_id()."'".' AND ' .
  441. 'exe_cours_id = '."'".$_cid."'".' AND ' .
  442. 'status = '."'incomplete'".' AND '.
  443. 'session_id = '."'".api_get_session_id()."'";
  444. $sql = api_sql_query('SELECT exe_id FROM '.$TABLETRACK_EXERCICES.$condition,__FILE__,__LINE__);
  445. $row = Database::fetch_array($sql);
  446. return $row['exe_id'];
  447. }
  448. $sql = "INSERT INTO $TABLETRACK_EXERCICES
  449. (
  450. exe_user_id,
  451. exe_cours_id
  452. )
  453. VALUES
  454. (
  455. ".$user_id.",
  456. '".$_cid."'
  457. )";
  458. $res = @api_sql_query($sql,__FILE__,__LINE__);
  459. $id= Database::get_last_insert_id();
  460. return $id;
  461. }
  462. /**
  463. * Record an event for this attempt at answering an exercise
  464. * @param float Score achieved
  465. * @param string Answer given
  466. * @param integer Question ID
  467. * @param integer Exercise ID
  468. * @param integer Position
  469. * @return boolean Result of the insert query
  470. */
  471. function exercise_attempt($score,$answer,$quesId,$exeId,$j)
  472. {
  473. global $_configuration, $_user, $_cid;
  474. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  475. // if tracking is disabled record nothing
  476. if (!$_configuration['tracking_enabled'])
  477. {
  478. return 0;
  479. }
  480. $reallyNow = time();
  481. if ($_user['user_id'])
  482. {
  483. $user_id = "'".$_user['user_id']."'";
  484. }
  485. else // anonymous
  486. {
  487. $user_id = "NULL";
  488. }
  489. $sql = "INSERT INTO $TBL_TRACK_ATTEMPT
  490. (
  491. exe_id,
  492. user_id,
  493. question_id,
  494. answer,
  495. marks,
  496. course_code,
  497. position,
  498. tms
  499. )
  500. VALUES
  501. (
  502. ".$exeId.",
  503. ".$user_id.",
  504. '".$quesId."',
  505. '".addslashes($answer)."',
  506. '".$score."',
  507. '".$_cid."',
  508. '".$j."',
  509. FROM_UNIXTIME(".$reallyNow.")
  510. )";
  511. if(defined('ENABLED_LIVE_EXERCISE_TRACKING')){
  512. $TBL_RECORDING = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  513. $recording_changes = 'INSERT INTO '.$TBL_RECORDING.' ' .
  514. '(exe_id,
  515. question_id,
  516. marks,
  517. insert_date,
  518. author)
  519. VALUES
  520. ('."'$exeId','".$quesId."','$score','".date('Y-m-d H:i:s')."',''".')';
  521. api_sql_query($recording_changes,__FILE__,__LINE__);
  522. }
  523. $res = @api_sql_query($sql,__FILE__,__LINE__);
  524. return $res;
  525. }
  526. /**
  527. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  528. * @desc Record information for common (or admin) events (in the track_e_default table)
  529. * @param string Type of event
  530. * @param string Type of value
  531. * @param string Value
  532. * @param string Timestamp (defaults to null)
  533. * @param integer User ID (defaults to null)
  534. * @param string Course code (defaults to null)
  535. */
  536. function event_system($event_type, $event_value_type, $event_value, $timestamp = null, $user_id=null, $course_code=null)
  537. {
  538. global $_configuration;
  539. global $_user;
  540. global $TABLETRACK_DEFAULT;
  541. // if tracking is disabled record nothing
  542. if (!$_configuration['tracking_enabled'])
  543. {
  544. return 0;
  545. }
  546. if(!isset($timestamp))
  547. {
  548. $timestamp = time();
  549. }
  550. if(!isset($user_id))
  551. {
  552. $user_id = 0;
  553. }
  554. if(!isset($course_code))
  555. {
  556. $course_code = '';
  557. }
  558. $sql = "INSERT INTO ".$TABLETRACK_DEFAULT."
  559. (default_user_id,
  560. default_cours_code,
  561. default_date, .
  562. default_event_type,
  563. default_value_type,
  564. default_value
  565. )
  566. VALUES
  567. ('".$user_id."',
  568. '".$course_code."',
  569. FROM_UNIXTIME(".$timestamp.")," .
  570. "'$event_type'," .
  571. "'$event_value_type'," .
  572. "'$event_value')";
  573. $res = api_sql_query($sql,__FILE__,__LINE__);
  574. return true;
  575. }
  576. ?>