Ver Fonte

More changes added in test/migrate see BT#4875

Julio Montoya há 12 anos atrás
pai
commit
218658cc65

+ 4 - 0
main/inc/lib/course.lib.php

@@ -83,6 +83,7 @@ class CourseManager {
      */
     static function create_course($params) {
         global $_configuration;
+        
         // Check portal limits
         $access_url_id = 1;
         if (api_get_multiple_access_url()) {
@@ -137,6 +138,8 @@ class CourseManager {
                     return $course_info;
                 }
             } else {
+                //error_log('Course already exists with params: '.$params['code']);
+                //error_log(print_r($params, 1));
                 //Course already exists
                 if (isset($params['return_item_if_already_exists']) && $params['return_item_if_already_exists']) {
                     return $course_info;
@@ -4809,6 +4812,7 @@ class CourseManager {
                         unsubscribe     = '".intval($unsubscribe) . "',
                         visual_code     = '".Database :: escape_string($visual_code) . "'";
             Database::query($sql);
+            //error_log($sql);
 
             $course_id  = Database::get_last_insert_id();
 

+ 44 - 38
tests/migrate/migration.class.php

@@ -123,7 +123,7 @@ class Migration {
                     //error_log('$selected_fields: ' . print_r($selected_fields, 1));
                     
                     //Adding options
-                    if (!empty($options)) {                    
+                    if (!empty($options)) {       
                         $extra_field_option_obj = new ExtraFieldOption($table['dest_table']);
                         $this->select_all($options['orig_table'], $selected_fields);                        
                         $num_rows = $this->num_rows();
@@ -149,11 +149,6 @@ class Migration {
                 }
             }
             
-            if ($table['orig_table'] == 'ProgramaAcademico') {
-                 //error_log(print_r($extra_fields, 1));              
-                 //exit;
-            }            
-            
             // Process the migration of fields from the given table
             $sql_select_fields = self::prepare_field_match($table);
             $this->select_all($table['orig_table'], $sql_select_fields, $table);
@@ -177,15 +172,14 @@ class Migration {
                     }
                     $item++;
                 }                
-                error_log('Finished processing table ' . $table['orig_table']."\n\n");
+                error_log('Finished processing table ' . $table['orig_table']." \n\n");
             } else {
                 error_log('No records found');
             }
             
-            //Stop here
-            if ($table['orig_table'] == 'Matricula')  {
-                //error_log(print_r($this->data_list['sessions'], 1));
-                exit;
+            //Stop here (only for tests)
+            if ($table['orig_table'] == 'ProgramaAcademico')  {
+                exit;          
             }
         }
     }
@@ -209,7 +203,7 @@ class Migration {
         return $sql_select_fields;     
     }
     
-    function execute_field_match($table, $row, $extra_fields) {
+    function execute_field_match($table, $row, $extra_fields) {        
         //error_log('execute_field_match');
         $dest_row = array();
         $first_field = '';
@@ -227,6 +221,14 @@ class Migration {
             if ($id_field == 0) {
                 $first_field = $details['dest'];
             }
+            
+            if (isset($details['orig'])) {
+                $field_exploded = explode('.', $details['orig']);
+                if (isset($field_exploded[1])) {
+                    $details['orig'] = $field_exploded[1];
+                }
+            }
+            
             // process the fields one by one
             if ($details['func'] == 'none' || empty($details['func'])) {
                 $dest_data = $row[$details['orig']];
@@ -248,9 +250,7 @@ class Migration {
                 if (isset($extra_field['options'])) {
                     $options = $extra_field['options'];
                     $field_type = $extra_field['field_type'];
-                    //error_log(print_r($options, 1));
-                    //error_log(print_r($dest_row, 1));
-                    
+                                        
                     if (!empty($options)) {
                         foreach ($options as $option) {
                             foreach ($option as $key => $value) {
@@ -259,9 +259,7 @@ class Migration {
                                     $value = $option['option_display_text'];                                    
                                     if ($field_type == Extrafield::FIELD_TYPE_SELECT) {
                                         $value = $option['option_display_text'];    
-                                    }
-                                    //error_log('asi -> ');
-                                    //error_log(print_r($option, 1));
+                                    }                                    
                                     $params = array(                
                                         'field_id'      => $option['field_id'],
                                         'field_value'   => $value,
@@ -277,44 +275,54 @@ class Migration {
                         'field_value'   => $dest_row[$details['dest']],
                     );
                 }
-  //              error_log('adding $extra_fields_to_insert');
-//                error_log(print_r($params, 1));
                 if (!empty($params)) {
                     $extra_fields_to_insert[] = $params;                
                 }
                 unset($dest_row[$details['dest']]);
             }
         }
-        
+                
         if (!empty($table['dest_func'])) {
             //error_log('Calling '.$table['dest_func'].' on data recovered: '.print_r($dest_row, 1));            
-            $dest_row['return_item_if_already_exists'] = true;            
-            $item_result = call_user_func_array($table['dest_func'], array($dest_row, $this->data_list));
+            $dest_row['return_item_if_already_exists'] = true;
             
-            error_log('Result of calling ' . $table['dest_func'] . ': ' . print_r($item_result, 1));
+            $item_result = call_user_func_array($table['dest_func'], array($dest_row, $this->data_list));
             
+            if ($table['show_in_error_log'] == false) {
+                
+            } else {
+                error_log('Result of calling ' . $table['dest_func'] . ': ' . print_r($item_result, 1));
+            }
+            //After the function was executed fill the $this->data_list array
             switch ($table['dest_table']) {
                 case 'course':
                     //Saving courses in array
-                    $this->data_list['courses'][$dest_row['uidIdCurso']] = $item_result;
+                    if ($item_result) {                        
+                        $this->data_list['courses'][$dest_row['uidIdCurso']] = $item_result;        
+                    } else {
+                        error_log('Course Not FOUND');                        
+                        error_log(print_r($item_result, 1));
+                        exit;
+                    }
                     $handler_id = $item_result['code'];                  
                     break;
                 case 'user':
                     if (!empty($item_result)) {                        
                         $handler_id = $item_result['user_id'];
                         //error_log($dest_row['email'].' '.$dest_row['uidIdPersona']);
-                        if (isset($this->data_list['users_persona'][$dest_row['uidIdPersona']])) {
-                            $this->data_list['users_persona'][$dest_row['uidIdPersona']]['extra'] = $item_result;
-                            //error_log(print_r($this->data_list['users_persona'][$dest_row['uidIdPersona']],1));                            
-                        }                        
+                        if (isset($dest_row['uidIdAlumno'])) {
+                            $this->data_list['users_alumno'][$dest_row['uidIdAlumno']]['extra'] = $item_result;
+                        }
+                        if (isset($dest_row['uidIdEmpleado'])) {
+                            //print_r($dest_row['uidIdEmpleado']);exit;                           
+                            $this->data_list['users_empleado'][$dest_row['uidIdEmpleado']]['extra'] = $item_result;
+                        }                                          
                     } else {
                         global $api_failureList;
                         error_log(print_r($api_failureList, 1));
                         //error_log("User can't be generated");
                     }
-                    
-                    //error_log('lols');
-                    //error_log(print_r($dest_row, 1));                    
+                    //error_log(print_r($dest_row, 1));
                     //error_log(print_r($this->data_list['users_persona'][$dest_row['uidIdPersona']],1));
                     //error_log(print_r($item_result, 1));
                     if (!empty($dest_row) && $table['orig_table'] == 'Persona' && !empty($dest_row['username'])) {
@@ -326,13 +334,11 @@ class Migration {
                     $handler_id = $item_result['session_id'];
                     break;
             }
-            //error_log('-->');
-            //error_log(print_r($extra_fields_to_insert, 1));
+            
+            //Saving extra fields of the element
             if (!empty($extra_fields_to_insert)) {
-                foreach ($extra_fields_to_insert as $params) {
-                    //error_log($extra_field_value_obj->handler_id);
-                    $params[$extra_field_value_obj->handler_id] =  $handler_id;                         
-                    //error_log('$extra_field_value_obj params: ' . print_r($params, 1));
+                foreach ($extra_fields_to_insert as $params) {                    
+                    $params[$extra_field_value_obj->handler_id] =  $handler_id;                                             
                     $extra_field_value_obj->save($params);    
                 }
             }

+ 104 - 54
tests/migrate/migration.custom.class.php

@@ -28,14 +28,25 @@ class MigrationCustom {
      * @return string SQL select string to include in the final select
      */
     public function sql_alter_unhash_50($field) {
-        return "cast( $field  as varchar(50)) as $field ";
+        $as_field = explode('.', $field);     
+        if (isset($as_field[1])) {
+            $as_field = $as_field[1];
+        } else {
+            $as_field = $field;
+        }
+        return " cast( $field  as varchar(50)) as $as_field ";
     }
-
+ 
     /**
      * Log data from the original users table
      */
-    public function log_original_user_unique_id($data, &$omigrate, $row_data) {                
-        $omigrate['users_alumno'][$row_data['uidIdPersona']] = $row_data;        
+    public function log_original_user_unique_id($data, &$omigrate, $row_data) {        
+        $omigrate['users_alumno'][$row_data['uidIdAlumno']] = $row_data;
+        return $data;
+    }
+    
+    public function clean_utf8($value) {
+        return utf8_encode($value);        
     }
     
     public function log_original_persona_unique_id($data, &$omigrate, $row_data) {  
@@ -48,10 +59,12 @@ class MigrationCustom {
         } else {
             $omigrate['users_persona'][$row_data['uidIdPersona']] = $row_data;
         }
+        return $data;
     }
     
     public function log_original_teacher_unique_id($data, &$omigrate, $row_data) {        
-        $omigrate['users_empleado'][$row_data['uidIdPersona']] = $row_data;                
+        $omigrate['users_empleado'][$row_data['uidIdEmpleado']] = $row_data;
+        return $row_data['uidIdEmpleado'];               
     }
 
     /**
@@ -110,27 +123,28 @@ class MigrationCustom {
     }
     
     public function get_real_teacher_id($data, &$omigrate, $row_data) {
+        $default_teacher_id = 1;
         //error_log('get_real_teacher_id');
         //error_log(print_r($data, 1));                
         //error_log(print_r($omigrate['users_empleado'], 1));        
         //error_log('get_real_teacher_id');
-        //error_log($data);               
-        if (empty($omigrate['users_empleado'][$data])) {
-            //error_log('not set');
-            return 1;
-        } else {            
-            $persona_id = $omigrate['users_empleado'][$data]['uidIdPersona'];  
-            if (!empty($persona_id)) {
-                return $omigrate['users_persona'][$persona_id]['user_id'];    
-            }
-        }        
-        return $omigrate['users_empleado'][$data]['user_id'];
-    }
-    
-    public function store_user_data($data = array()) {
+        //error_log($data);             
+        if (empty($data)) {
+            error_log('No teacher provided');
+            return $default_teacher_id;
+        }
         
+        if (!isset($omigrate['users_empleado'][$data])) {
+            error_log('Teacher not found big problem!');    
+            echo $data;
+            print_r($omigrate['users_empleado'][$data]);
+            //echo $data;exit;
+            return $default_teacher_id;            
+        } else {
+            return isset($omigrate['users_empleado'][$data]['extra']) ? $omigrate['users_empleado'][$data]['extra']['user_id'] : $default_teacher_id;        
+        }        
     }
-    
+     
     public function create_user($data, $omigrate) {       
         //error_log(print_r($data, 1));  
         
@@ -139,37 +153,66 @@ class MigrationCustom {
             error_log(print_r($data, 1));    
             exit;
         }
-        
-        if (isset($omigrate['users_persona'][$data['uidIdPersona']])) {
-            ///error_log('persona!');
-        }
-        
+            
         //Is a teacher
-        if (isset($omigrate['users_empleado'][$data['uidIdPersona']])) {
+        if (isset($omigrate['users_empleado'][$data['uidIdEmpleado']])) {
             //error_log(print_r($omigrate['users_empleado'][$data['uidIdPersona']], 1));  
             //error_log(print_r($data, 1));
             //error_log('teacher');
-            $data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);
+            //$data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);        
             $data['status'] = COURSEMANAGER;                
         }
         
         //Is a student
-        if (isset($omigrate['users_alumno'][$data['uidIdPersona']])) {
-            $uidIdPersona = $data['uidIdPersona'];            
-            $persona_info = $omigrate['users_alumno'][$uidIdPersona];
-            //error_log(print_r($omigrate['users_alumno'][$data['uidIdPersona']], 1));  
-            //error_log(print_r($data, 1));
-            //error_log(print_r($uidIdPersona, 1));
-            //error_log(print_r($persona_info, 1));
-            $data['username'] = strtolower($persona_info['vchCodal']);
-            $data['password'] = $persona_info['chrPasswordT'];
-            $data['status'] = STUDENT;
-            //error_log(print_r($data, 1));error_log('student');            
+        if (isset($omigrate['users_alumno'][$data['uidIdAlumno']])) {
+            $data['status'] = STUDENT;            
         }
         
-        if (empty($data['username'])) {
-            //Last chance to have a nice username
-            $data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);
+        if (!isset($data['username']) || empty($data['username'])) {
+            $data['firstname'] = (string) trim($data['firstname']); 
+            $data['lastname'] = (string) trim($data['lastname']); 
+            
+            if (empty($data['firstname']) && empty($data['lastname'])) {
+                $wanted_user_name = UserManager::purify_username($data['uidIdPersona']);
+                //$wanted_user_name = UserManager::create_unique_username(null, null);                
+            } else {
+                $wanted_user_name = UserManager::create_username($data['firstname'], $data['lastname']);
+            }
+            
+            $extra_data = UserManager::get_extra_user_data_by_value('uidIdPersona', $data['uidIdPersona']);
+            
+            if ($extra_data) {
+                $user_info = api_get_user_info($extra_data['user_id']);
+                error_log("User_already_added {$user_info['username']}");
+                return $user_info;
+            }
+            
+            if (UserManager::is_username_available($wanted_user_name)) {
+                $data['username'] = $wanted_user_name;
+                error_log("username available  $wanted_user_name");
+            } else {
+                //the user already exists?
+                $user_info = UserManager::get_user_info_simple($wanted_user_name);
+                $user_persona = UserManager::get_extra_user_data_by_field($user_info['user_id'], 'uidIdPersona');
+                if (isset($user_persona['uidIdPersona']) && $data['uidIdPersona'] == $user_persona['uidIdPersona']) {
+                    error_log("Skip user already added: {$user_info['username']}");                    
+                    return $user_info;
+                } else {
+                    error_log("homonym $wanted_user_name - {$user_persona['uidIdPersona']} - {$user_info['username']}");       
+                    print_r($data);
+                     //The user has the same firstname and lastname but it has another uiIdPersona could by an homonym  
+                    $data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);                    
+                }
+            }
+            
+            if (empty($data['username'])) {
+                //Last chance to have a nice username   
+                if (empty($data['firstname']) && empty($data['lastname'])) {
+                    $data['username'] = UserManager::create_unique_username(uniqid());
+                } else {
+                    $data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);
+                }
+            }
         }
                 
         if (empty($data['username'])) {
@@ -179,26 +222,33 @@ class MigrationCustom {
         }
         
         unset($data['uidIdPersona']);
+        unset($data['uidIdAlumno']);
+        unset($data['uidIdEmpleado']);
         
         global $api_failureList;
         $api_failureList = array();
         //error_log(print_r($data, 1));
-        $user_info = UserManager::add($data);        
-        if (isset($omigrate['users_empleado'][$data['uidIdPersona']])) {
-            
+        $user_info = UserManager::add($data);
+        if (!$user_info) {
+            echo 'error';
         }
         return $user_info;
     }
     
-    public function add_user_to_session($data, &$data_list, $row_data) {
-         error_log('add_user_to_session');
-        error_log(print_r($data, 1));
-        error_log(print_r($row_data, 1));
-        exit;
-        //error_log(print_r($data_list['user'], 1));        
-        //$user_id = UserManager::add($data);  
-         
-        //SessionManager::suscribe_users_to_session($data['session_id'], array($data['user_id']));
-        return $user_id;
+    public function create_course($data) {
+        //Fixes wrong wanted codes
+        $data['wanted_code'] = str_replace(array('-', '_'), '000', $data['wanted_code']);
+        return CourseManager::create_course($data);
+    }
+    public function add_user_to_session($data, $data_list) {
+        error_log('add_user_to_session');
+        
+        if (!empty($data['session_id']) && !empty($data['user_id'])) {
+            SessionManager::suscribe_users_to_session($data['session_id'], array($data['user_id']));
+        } else {
+            error_log('missing data');
+            error_log(print_r($data, 1));                
+            exit;    
+        }        
     }
 }

+ 7 - 5
tests/migrate/migration.mssql.class.php

@@ -33,18 +33,20 @@ class MigrationMSSQL extends Migration {
         //In order to process X item of each table add TOP X
         
         $top = " TOP 1000 ";
-        if (in_array($table, array('Persona', 'Alumno', 'Empleado'))) {
-            $top = " TOP 100 ";            
+        if (in_array($table, array('Empleado', 'Alumno'))) {
+            $top = " TOP 5000 ";            
         }
         if ($table == 'ProgramaAcademico') {
             $top = "  TOP 1000 ";    
         }
-        
+       //$top = null;
+        //$top = " TOP 2500 ";  
         $extra = null;
         if (isset($options) && !empty($options['inner_join'])) {
-            $extra = ' INNER JOIN '.$options['inner_join'].' ON '.$options['on'];
+            $extra = ' '.$options['alias_orig_table'].' INNER JOIN '.$options['inner_join'].' '.$options['alias_join_table'].' ON '.$options['on'];
         }
-        $sql = "SELECT $top $fields_sql FROM $table $extra ";
+        $order = isset($options['order']) ? $options['order'] : null;
+        $sql = "SELECT $top $fields_sql FROM $table $extra $order";
         if (!empty($extra)) {
             error_log(print_r($options,1));
             error_log($sql);