Browse Source

Reformat code, adding comments and moving db tables inside functions in events.lib.inc.php

Julio Montoya 12 years ago
parent
commit
e707fd5c2f
4 changed files with 57 additions and 103 deletions
  1. 0 1
      main/inc/global.inc.php
  2. 20 45
      main/inc/lib/events.lib.inc.php
  3. 8 23
      main/inc/lib/main_api.lib.php
  4. 29 34
      main/inc/services.php

+ 0 - 1
main/inc/global.inc.php

@@ -345,7 +345,6 @@ if ($alreadyInstalled && $checkConnection) {
         }
     }
 }
-
 $app['plugins'] = $_plugins;
 
 // Section (tabs in the main chamilo menu)

+ 20 - 45
main/inc/lib/events.lib.inc.php

@@ -11,19 +11,6 @@
  *
  * @package chamilo.library
  */
-/* 	   INIT SECTION */
-
-$TABLETRACK_OPEN = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_OPEN);
-$TABLETRACK_ACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
-
-$TABLETRACK_UPLOADS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
-
-$TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
-$TABLETRACK_LASTACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); //for "what's new" notification
-$TABLETRACK_DEFAULT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
-
-
-/* FUNCTIONS */
 
 /**
  * @author Sebastien Piraux <piraux_seb@hotmail.com>
@@ -32,7 +19,7 @@ $TABLETRACK_DEFAULT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DEFA
 function event_open()
 {
     global $_configuration;
-    global $TABLETRACK_OPEN;
+    $TABLETRACK_OPEN = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_OPEN);
 
     // @getHostByAddr($_SERVER['REMOTE_ADDR']) : will provide host and country information
     // $_SERVER['HTTP_USER_AGENT'] :  will provide browser and os information
@@ -105,7 +92,8 @@ function event_login()
  */
 function event_access_course()
 {
-    global $TABLETRACK_ACCESS, $TABLETRACK_LASTACCESS;
+    $TABLETRACK_ACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
+    $TABLETRACK_LASTACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); //for "what's new" notification
 
     $id_session = api_get_session_id();
     $now = api_get_utc_datetime();
@@ -152,17 +140,16 @@ function event_access_course()
 function event_access_tool($tool, $id_session = 0)
 {
     global $_configuration;
-    global $_user;
-    global $TABLETRACK_ACCESS;
-    $_course = api_get_course_info();
-    global $TABLETRACK_LASTACCESS; //for "what's new" notification
+    $TABLETRACK_ACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
+    $TABLETRACK_LASTACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); //for "what's new" notification
 
+    $_course = api_get_course_info();
     $courseId = api_get_course_int_id();
-
     $id_session = api_get_session_id();
     $tool = Database::escape_string($tool);
     $reallyNow = api_get_utc_datetime();
-    $user_id = $_user['user_id'] ? "'".$_user['user_id']."'" : "0"; // no one
+    $user_id = api_get_user_id();
+
     // record information
     // only if user comes from the course $_cid
     //if( eregi($_configuration['root_web'].$_cid,$_SERVER['HTTP_REFERER'] ) )
@@ -250,16 +237,11 @@ function event_download($doc_url)
  */
 function event_upload($doc_id)
 {
-    global $_user;
-    global $_cid;
-    global $TABLETRACK_UPLOADS;
-
+    $TABLETRACK_UPLOADS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
+    $courseCode = api_get_course_id();
     $reallyNow = api_get_utc_datetime();
-    if (isset($_user['user_id']) && $_user['user_id'] != '') {
-        $user_id = "'".$_user['user_id']."'";
-    } else {
-        $user_id = "0"; // anonymous
-    }
+    $user_id = api_get_user_id();
+
     $sql = "INSERT INTO ".$TABLETRACK_UPLOADS."
         		( upload_user_id,
         		  upload_cours_id,
@@ -269,12 +251,12 @@ function event_upload($doc_id)
         		)
         		VALUES (
         		 ".$user_id.",
-        		 '".$_cid."',
+        		 '".$courseCode."',
         		 '".$doc_id."',
         		 '".$reallyNow."',
         		 '".api_get_session_id()."'
         		)";
-    $res = Database::query($sql);
+    Database::query($sql);
     return 1;
 }
 
@@ -286,16 +268,9 @@ function event_upload($doc_id)
  */
 function event_link($link_id)
 {
-    global $_user;
     $TABLETRACK_LINKS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
-
     $reallyNow = api_get_utc_datetime();
-    if (isset($_user['user_id']) && $_user['user_id'] != '') {
-        $user_id = "'".Database::escape_string($_user['user_id'])."'";
-    } else {
-        // anonymous
-        $user_id = "0";
-    }
+    $user_id = api_get_user_id();
     $sql = "INSERT INTO ".$TABLETRACK_LINKS."
         		( links_user_id,
         		 c_id,
@@ -332,6 +307,8 @@ function event_link($link_id)
 function update_event_exercise($exeid, $exo_id, $score, $weight, $session_id, $learnpath_id = 0, $learnpath_item_id = 0, $learnpath_item_view_id = 0, $duration = 0, $status = '', $remind_list = array() , $end_date = null) {
     require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
     global $debug;
+    $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
+
     if ($debug) {
         error_log('Called to update_event_exercise');
         error_log('duration:'.$duration);
@@ -349,8 +326,6 @@ function update_event_exercise($exeid, $exo_id, $score, $weight, $session_id, $l
             $status = Database::escape_string($status);
         }
 
-        $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
-
         if (!empty($remind_list)) {
             $remind_list = array_map('intval', $remind_list);
             $remind_list = array_filter($remind_list);
@@ -577,6 +552,7 @@ function exercise_attempt_hotspot($exe_id, $question_id, $answer_id, $correct, $
 {
     require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
     global $safe_lp_id, $safe_lp_item_id;
+
     //Validation in case of fraud  with actived control time
     if (!exercise_time_control_is_valid($exerciseId, $safe_lp_id, $safe_lp_item_id)) {
         $correct = 0;
@@ -607,7 +583,7 @@ function exercise_attempt_hotspot($exe_id, $question_id, $answer_id, $correct, $
  */
 function event_system($event_type, $event_value_type, $event_value, $datetime = null, $user_id = null, $course_code = null)
 {
-    global $TABLETRACK_DEFAULT;
+    $TABLETRACK_DEFAULT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
 
     if (empty($event_type)) {
         return false;
@@ -743,7 +719,7 @@ function get_event_users($event_name)
 
 function get_events_by_user_and_type($user_id, $event_type)
 {
-    global $TABLETRACK_DEFAULT;
+    $TABLETRACK_DEFAULT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
     $user_id = intval($user_id);
     $event_type = Database::escape_string($event_type);
 
@@ -934,7 +910,6 @@ function get_attempt_count_not_finished($user_id, $exerciseId, $lp_id, $lp_item_
  */
 function delete_student_lp_events($user_id, $lp_id, $course, $session_id)
 {
-
     $lp_view_table = Database::get_course_table(TABLE_LP_VIEW);
     $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
     $course_id = $course['real_id'];

+ 8 - 23
main/inc/lib/main_api.lib.php

@@ -8,11 +8,11 @@
  * @package chamilo.library
  */
 
+use \ChamiloSession as Session;
 
 /**
  * Constants declaration
  */
-
 // PHP version requirement.
 define('REQUIRED_PHP_VERSION', '5.3');
 
@@ -20,8 +20,6 @@ define('REQUIRED_MIN_MEMORY_LIMIT',         '32');
 define('REQUIRED_MIN_UPLOAD_MAX_FILESIZE',  '10');
 define('REQUIRED_MIN_POST_MAX_SIZE',        '10');
 
-use \ChamiloSession as Session;
-
 // USER STATUS CONSTANTS
 /** global status of a user: course manager */
 define('COURSEMANAGER', 1);
@@ -36,10 +34,8 @@ define('ANONYMOUS', 6);
 /** global status of a user: low security, necessary for inserting data from
  * the teacher through HTMLPurifier */
 define('COURSEMANAGERLOWSECURITY', 10);
-
 define('TEACHER', 1);
 
-
 //Soft user status
 define('PLATFORM_ADMIN', 11);
 define('SESSION_COURSE_COACH', 12);
@@ -48,7 +44,6 @@ define('COURSE_STUDENT', 14);   //student subscribed in a course
 define('SESSION_STUDENT', 15);  //student subscribed in a session course
 define('COURSE_TUTOR', 16); // student is tutor of a course (NOT in session)
 
-
 // Table of status
 $_status_list[COURSEMANAGER]    = 'teacher';        // 1
 $_status_list[SESSIONADMIN]     = 'session_admin';  // 3
@@ -56,7 +51,6 @@ $_status_list[DRH]              = 'drh';            // 4
 $_status_list[STUDENT]          = 'user';           // 5
 $_status_list[ANONYMOUS]        = 'anonymous';      // 6
 
-
 // COURSE VISIBILITY CONSTANTS
 /** only visible for course admin */
 define('COURSE_VISIBILITY_CLOSED', 0);
@@ -67,14 +61,12 @@ define('COURSE_VISIBILITY_OPEN_PLATFORM', 2);
 /** Open for the whole world */
 define('COURSE_VISIBILITY_OPEN_WORLD', 3);
 
-
 // SESSION VISIBILITY CONSTANTS
 define('SESSION_VISIBLE_READ_ONLY', 1);
 define('SESSION_VISIBLE', 2);
 define('SESSION_INVISIBLE', 3); // not available
 define('SESSION_AVAILABLE', 4);
 
-
 define('SUBSCRIBE_ALLOWED', 1);
 define('SUBSCRIBE_NOT_ALLOWED', 0);
 define('UNSUBSCRIBE_ALLOWED', 1);
@@ -352,14 +344,9 @@ define('GROUP_IMAGE_SIZE_ORIGINAL',	1);
 define('GROUP_IMAGE_SIZE_BIG', 		2);
 define('GROUP_IMAGE_SIZE_MEDIUM', 	3);
 define('GROUP_IMAGE_SIZE_SMALL', 	4);
-
 define('GROUP_TITLE_LENGTH',       50);
 
-
 // Messages
-/*
- * @todo use constants!
- */
 define('MESSAGE_STATUS_NEW',                    '0');
 define('MESSAGE_STATUS_UNREAD',                 '1');
 //2 ??
@@ -380,7 +367,7 @@ define('TEACHER_HTML', 3);
 define('STUDENT_HTML_FULLPAGE', 4);
 define('TEACHER_HTML_FULLPAGE', 5);
 
-//Exercise
+// Exercise
 define('EXERCISE_NUMBER_OF_DECIMALS', 2);
 
 /* XML processing functions */
@@ -390,7 +377,6 @@ define('EXERCISE_NUMBER_OF_DECIMALS', 2);
 // http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss/
 define('_PCRE_XML_ENCODING', '/<\?xml.*encoding=[\'"](.*?)[\'"].*\?>/m');
 
-
 //Social PLUGIN PLACES
 define('SOCIAL_LEFT_PLUGIN',        1);
 define('SOCIAL_CENTER_PLUGIN',      2);
@@ -402,7 +388,6 @@ define ('SKILL_TYPE_REQUIREMENT',   'required');
 define ('SKILL_TYPE_ACQUIRED',      'acquired');
 define ('SKILL_TYPE_BOTH',          'both');
 
-
 /* PATHS & FILES - ROUTINES */
 
 /**
@@ -6409,7 +6394,8 @@ function api_get_js_simple($file) {
 }
 
 
-function api_set_settings_and_plugins() {
+function api_set_settings_and_plugins()
+{
     global $_configuration;
     $_setting = array();
     $_plugins = array();
@@ -6478,6 +6464,7 @@ function api_set_settings_and_plugins() {
     }
 
     $result = api_get_settings('Plugins', 'list', $access_url_id);
+
     foreach ($result as & $row) {
         $key = & $row['variable'];
         if (is_string($_setting[$key])) {
@@ -6486,8 +6473,8 @@ function api_set_settings_and_plugins() {
         $_setting[$key][] = $row['selected_value'];
         $_plugins[$key][] = $row['selected_value'];
     }
-    $_SESSION['_setting'] = $_setting;
-    $_SESSION['_plugins'] = $_plugins;
+    Session::write('_setting', $_setting);
+    Session::write('_plugins', $_plugins);
 }
 
 /**
@@ -6879,7 +6866,6 @@ function api_get_language_interface() {
 }
 
 function getConfigurationArray($confPath) {
-
 }
 
 function api_get_default_course_document()
@@ -6893,5 +6879,4 @@ function api_get_web_default_course_document()
 }
 
 function load_translations($app) {
-
-}
+}

+ 29 - 34
main/inc/services.php

@@ -209,8 +209,6 @@ if (isset($app['configuration']['main_database'])) {
     $tree = new Gedmo\Mapping\Annotation\TreeRoot(array());
     $tree = new Gedmo\Mapping\Annotation\TreeLevel(array());
 
-
-
     // Setting Doctrine2 extensions
     $timestampableListener = new \Gedmo\Timestampable\TimestampableListener();
     $app['db.event_manager']->addEventSubscriber($timestampableListener);
@@ -274,6 +272,10 @@ $app->register(new \Knp\Menu\Silex\KnpMenuServiceProvider());
 use FranMoreno\Silex\Provider\PagerfantaServiceProvider;
 $app->register(new PagerfantaServiceProvider());
 
+// Custom route params see https://github.com/franmomu/silex-pagerfanta-provider/pull/2
+//$app['pagerfanta.view.router.name']
+//$app['pagerfanta.view.router.params']
+
 $app['pagerfanta.view.options'] = array(
     'routeName'     => null,
     'routeParams'   => array(),
@@ -284,29 +286,6 @@ $app['pagerfanta.view.options'] = array(
     'default_view'  => 'twitter_bootstrap' // the pagination style
 );
 
-// Custom route params see https://github.com/franmomu/silex-pagerfanta-provider/pull/2
-//$app['pagerfanta.view.router.name']
-//$app['pagerfanta.view.router.params']
-
-// Monolog only available if the log dir is writable
-if (is_writable($app['temp.path'])) {
-
-    /*    Adding Monolog service provider
-    Monolog  use examples
-        $app['monolog']->addDebug('Testing the Monolog logging.');
-        $app['monolog']->addInfo('Testing the Monolog logging.');
-        $app['monolog']->addError('Testing the Monolog logging.');
-    */
-
-    $app->register(
-        new Silex\Provider\MonologServiceProvider(),
-        array(
-            'monolog.logfile' => $app['chamilo.log'],
-            'monolog.name' => 'chamilo',
-        )
-    );
-}
-
 // @todo use a app['image_processor'] setting
 define('IMAGE_PROCESSOR', 'gd'); // imagick or gd strings
 
@@ -329,18 +308,34 @@ if ($app['debug'] && isset($app['configuration']['main_database'])) {
     });
 }
 
-// Adding symfony2 web profiler (memory, time, logs, etc)
+// Developer tools
 if (is_writable($app['sys_temp_path'])) {
-    //if ($app['debug']) {
+    if ($app['debug']) {
+        // Adding symfony2 web profiler (memory, time, logs, etc)
+        if (api_get_setting('allow_web_profiler') == 'true') {
+            $app->register($p = new Silex\Provider\WebProfilerServiceProvider(), array(
+                    'profiler.cache_dir' => $app['profiler.cache_dir'],
+                ));
+            $app->mount('/_profiler', $p);
+        }
+
+        /** Adding Monolog service provider Monolog  use examples
+            $app['monolog']->addDebug('Testing the Monolog logging.');
+            $app['monolog']->addInfo('Testing the Monolog logging.');
+            $app['monolog']->addError('Testing the Monolog logging.');
+        */
+
+        $app->register(
+            new Silex\Provider\MonologServiceProvider(),
+            array(
+                'monolog.logfile' => $app['chamilo.log'],
+                'monolog.name' => 'chamilo',
+            )
+        );
 
-    if (api_get_setting('allow_web_profiler') == 'true') {
-        $app->register($p = new Silex\Provider\WebProfilerServiceProvider(), array(
-                'profiler.cache_dir' => $app['profiler.cache_dir'],
-            ));
-        $app->mount('/_profiler', $p);
+        //$app->register(new Whoops\Provider\Silex\WhoopsServiceProvider);
+        //}
     }
-    //$app->register(new Whoops\Provider\Silex\WhoopsServiceProvider);
-    //}
 }
 
 // Email service provider