Julio Montoya 12 years ago
parent
commit
1b5a786a0b
3 changed files with 48 additions and 24 deletions
  1. 8 4
      tests/migrate/count_report.php
  2. 7 5
      tests/migrate/migrate.php
  3. 33 15
      tests/migrate/migration.class.php

+ 8 - 4
tests/migrate/count_report.php

@@ -1,5 +1,9 @@
 <?php
 require '../../main/inc/global.inc.php';
+$eol = "<br />\n";
+if (PHP_SAPI == 'cli') {
+  $eol = "\n";
+}
 $tables = array(
   'user',
   'course',
@@ -8,16 +12,16 @@ $tables = array(
   'session_rel_course_rel_user',
   'gradebook_category',
 );
-echo "\n--- Post-migration count report ---\n";
+echo "$eol--- Post-migration count report ---$eol";
 foreach ($tables as $table) {
   $sql = "SELECT count(*) FROM $table";
   $res = Database::query($sql);
   if ($res === false) {
-    echo "SQL error in ".$sql."\n";
+    echo "SQL error in ".$sql.$eol;
   } else {
     $row = Database::fetch_array($res);
     $count = $row[0];
-    echo "Table $table has $count items after migration\n";
+    echo "Table $table has $count items after migration$eol";
   }
 }
-echo "\n";
+echo 'The end.'.$eol;

+ 7 - 5
tests/migrate/migrate.php

@@ -14,7 +14,7 @@ require_once api_get_path(LIBRARY_PATH).'attendance.lib.php';
 require_once api_get_path(LIBRARY_PATH).'thematic.lib.php';
 
 //error_reporting(-1);
-$action_type = ((isset($argv[1]) && $argv[1]=='migration')?'migration':'sync');
+$action_type = ((!empty($argv[1]) && $argv[1]=='migration')?'migration':'sync');
 
 if (is_file(dirname(__FILE__) . '/migration.custom.class.php')) {
     require_once 'migration.custom.class.php';
@@ -58,21 +58,23 @@ if (!empty($servers)) {
             include $server_info['filename'];
             
             if ($action_type == 'migration') {
+                error_log('Starting Migration');
                 //Default migration from MSSQL to Chamilo MySQL
                 $m->migrate($matches);
             } else {
+                error_log('Starting Synchronization');
                 //Getting transactions from MSSQL (via webservices)
              
                 if (isset($matches['web_service_calls']['filename'])) {                
                     require_once $matches['web_service_calls']['filename'];    
                 }
                 //This functions truncates the transaction lists!
-                $m->test_transactions($matches['web_service_calls']);
+                //$m->test_transactions($matches['web_service_calls']);
             
-                //$m->search_transactions($matches['web_service_calls']);
+                $m->search_transactions($matches['web_service_calls']);
             
-                //Load transactions saved before            
-                $m->load_transactions($matches);    
+                //Load transactions saved before
+                $m->load_transactions($matches);
             
                 //print_r($m->errors_stack);
             }

+ 33 - 15
tests/migrate/migration.class.php

@@ -192,7 +192,7 @@ class Migration {
         }
         
         if (!empty($data)) {
-            error_log("Calling {$web_service_params['class']}::$function_name  $url with params: ");
+            error_log("Calling {$web_service_params['class']}::$function_name  $url with params: ".print_r($params,1));
             return $web_service_params['class']::$function_name($data, $params);           
         } else {
             return array(
@@ -206,16 +206,19 @@ class Migration {
     /**
      * Test a series of hand-crafted transactions
      * @param array of parameters that would usually get passed to the web service
+     * @param bool Whether to truncate the transaction table before the test or not
      * @return void
      */
-    function test_transactions($web_service_params) {
-        error_log('search_transactions');
+    function test_transactions($web_service_params, $truncate = false) {
+        error_log('test_transactions');
         //Just for tests
         
         //Cleaning transaction table
-        $table = Database::get_main_table(TABLE_MIGRATION_TRANSACTION);
-        $sql = "TRUNCATE $table";
-        Database::query($sql);
+        if ($truncate) {
+            $table = Database::get_main_table(TABLE_MIGRATION_TRANSACTION);
+            $sql = "TRUNCATE $table";
+            Database::query($sql);
+        }
         
         $transaction_harcoded = array(
             array(
@@ -513,6 +516,7 @@ class Migration {
      * @return int The ID of the transaction row in Chamilo's table
      */
     static function add_transaction($params) {
+error_log('Request add_transaction of : '.print_r($params,1));
         $table = Database::get_main_table(TABLE_MIGRATION_TRANSACTION);
         if (isset($params['id'])) {
             unset($params['id']);
@@ -533,7 +537,16 @@ class Migration {
         $table = Database::get_main_table(TABLE_MIGRATION_TRANSACTION);
         $sql = "SELECT DISTINCT branch_id FROM $table ORDER BY branch_id";
         $result = Database::query($sql);
-        return Database::store_result($result, 'ASSOC');
+        if (Database::num_rows($result) > 0) {
+          return Database::store_result($result, 'ASSOC');
+        }
+        return array(
+          //0=>array('branch_id' => 1),
+          1=>array('branch_id' => 2),
+          //2=>array('branch_id' => 3),
+          //3=>array('branch_id' => 4),
+          //4=>array('branch_id' => 5),
+        );
     }
     
     /**
@@ -577,15 +590,16 @@ class Migration {
      * @return int The ID of the last transaction registered
      */
     static function get_latest_transaction_by_branch($branch_id) {
+        return 376011;
         $table = Database::get_main_table(TABLE_MIGRATION_TRANSACTION);
         $branch_id = intval($branch_id);
-        $sql = "SELECT id FROM $table WHERE branch_id = $branch_id ORDER BY id DESC  LIMIT 1";
+        $sql = "SELECT id FROM $table WHERE branch_id = $branch_id ORDER BY id DESC LIMIT 1";
         $result = Database::query($sql);
         if (Database::num_rows($result)) {
             $row = Database::fetch_array($result);
             return $row['id'];
         }
-        return 0;
+        return 376013; //current first entry
     }
     /**
      * Gets a specific transaction using select parameters
@@ -623,9 +637,10 @@ class Migration {
     /**
      * Search for new transactions through a web service call. Automatically insert them in the local transactions table.
      * @param array The web service parameters
+     * @param int An optional transaction ID to start from. If none provided, fetches the latest transaction available and add + 1
      * @return The operation results
      */
-    function search_transactions($web_service_params) {        
+    function search_transactions($web_service_params, $t_id = null) {
         error_log('search_transactions');        
         //Testing transactions        
         
@@ -641,11 +656,14 @@ class Migration {
         $result = self::soap_call($web_service_params, 'horarioDetalles', array('uididhorario' => 'E395895A-B480-456F-87F2-36B3A1EBB81C'));        
         $result = self::soap_call($web_service_params, 'transacciones', array('ultimo' => 354911, 'cantidad' => 2));         
         */
-        
-        $result = self::soap_call($web_service_params, 'transacciones', array('ultimo' => 354911, 'cantidad' => 2)); 
-        
-        //Calling a process to save transactions
-        $web_service_params['class']::process_transactions($web_service_params, array('ultimo' => 354911, 'cantidad' => 2));        
+        $branches = self::get_branches();
+        foreach ($branches as $branch) {
+            error_log('Treating transactions for branch '.$branch['branch_id']);
+            $last = self::get_latest_transaction_by_branch($branch['branch_id']);
+            $result = self::soap_call($web_service_params, 'transacciones', array('ultimo' => $last, 'cantidad' => 2));
+            //Calling a process to save transactions
+            $web_service_params['class']::process_transactions($web_service_params, array('ultimo' => $last, 'cantidad' => 2));
+        }
     }   
 
     /**