Adjustments to migration classes: setting courses to English, increasing the limits, creating an extra-fields creation method

skala
Yannick Warnier 13 years ago
parent 2997b97a63
commit d57f2c0b9b
  1. 99
      tests/migrate/migration.class.php
  2. 41
      tests/migrate/migration.custom.class.php
  3. 11
      tests/migrate/migration.mssql.class.php

@ -99,54 +99,21 @@ class Migration {
$extra_fields = array();
// Browsing through 1st-level arrays in db_matches.php
foreach ($matches as $table) {
error_log('Found table ' . $table['orig_table']);
$build_only = false;
if (empty($table['dest_table'])) {
//If there is no destination for this table, report
error_log(' ... which is just for data building');
$build_only = true;
}
//Creating extra fields
// Creating extra fields if necessary inside Chamilo (to store
// original fields)
if (isset($table['extra_fields']) && in_array($table['dest_table'], array('course', 'user', 'session'))) {
error_log('Inserting (if exists) extra fields for : ' . $table['dest_table']." \n");
foreach ($table['extra_fields'] as $extra_field) {
$options = $extra_field['options'];
unset($extra_field['options']);
$extra_field_obj = new ExtraField($table['dest_table']);
$extra_field_id = $extra_field_obj->save($extra_field);
$selected_fields = self::prepare_field_match($options);
//error_log('$selected_fields: ' . print_r($selected_fields, 1));
//Adding 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();
if ($num_rows) {
$data_to_insert = array();
$data_to_insert['field_id'] = $extra_field_id;
while ($row = $this->fetch_array()) {
$data = self::execute_field_match($options, $row);
$data_to_insert = array_merge($data_to_insert, $data);
$extra_field_option_obj->save_one_item($data_to_insert, false, false);
//error_log(print_r($extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']], 1));
$extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']]['options'][] = $data_to_insert;
$extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']]['field_type'] = $extra_field['field_type'];
}
//$extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']]['selected_option'] =
//error_log('$data: ' . print_r($data_to_insert, 1));
}
} else {
$extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']] = $extra_field_id;
}
}
$extra_fields = self::_create_extra_fields($table);
}
// Process the migration of fields from the given table
@ -178,9 +145,9 @@ class Migration {
}
//Stop here (only for tests)
if ($table['orig_table'] == 'Matricula') {
exit;
}
// if ($table['orig_table'] == 'Matricula') {
// exit;
// }
}
}
@ -217,7 +184,7 @@ class Migration {
}
$extra_fields_to_insert = array();
foreach ($table['fields_match'] as $id_field => $details) {
foreach ($table['fields_match'] as $id_field => $details) {
if ($id_field == 0) {
$first_field = $details['dest'];
}
@ -339,6 +306,52 @@ class Migration {
} else {
$this->errors_stack[] = "No destination data dest_func found. Abandoning data with first field $first_field = " . $dest_row[$first_field];
}
unset($extra_fields_to_insert); //remove to free up memory
return $dest_row;
}
}
/**
* Helper function to create extra fields in the Chamilo database
* @param Array An array containing an 'extra_fields' entry with details about the required extra fields
* @return void
*/
private function _create_extra_fields(&$table) {
error_log('Inserting (if exists) extra fields for : ' . $table['dest_table']." \n");
foreach ($table['extra_fields'] as $extra_field) {
error_log('Preparing for insertion of extra field '.$extra_field['field_display_text']."\n");
$options = $extra_field['options'];
unset($extra_field['options']);
$extra_field_obj = new ExtraField($table['dest_table']);
$extra_field_id = $extra_field_obj->save($extra_field);
$selected_fields = self::prepare_field_match($options);
//error_log('$selected_fields: ' . print_r($selected_fields, 1));
//Adding 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();
if ($num_rows) {
$data_to_insert = array();
$data_to_insert['field_id'] = $extra_field_id;
while ($row = $this->fetch_array()) {
$data = self::execute_field_match($options, $row);
$data_to_insert = array_merge($data_to_insert, $data);
$extra_field_option_obj->save_one_item($data_to_insert, false, false);
//error_log(print_r($extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']], 1));
$extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']]['options'][] = $data_to_insert;
$extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']]['field_type'] = $extra_field['field_type'];
}
//$extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']]['selected_option'] =
//error_log('$data: ' . print_r($data_to_insert, 1));
}
} else {
$extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']] = $extra_field_id;
}
}
return $extra_fields;
}
}

@ -25,7 +25,7 @@ class MigrationCustom {
}
public function join_horario($data, &$omigrate, $row_data) {
return $row_data['chrHoraInicial'].' '.$row_data['chrHoraFinal'];
return '('.$row_data['chrIdHorario'].') '.$row_data['chrHoraInicial'].' '.$row_data['chrHoraFinal'];
}
/**
@ -47,8 +47,9 @@ class MigrationCustom {
* Log data from the original users table
*/
public function log_original_user_unique_id($data, &$omigrate, $row_data) {
$row = array('uidIdPersona' => $row_data['uidIdPersona'], 'uidIdAlumno' => $row_data['uidIdAlumno']);
$omigrate['users_alumno'][$row_data['uidIdAlumno']] = $row;
//Temporarily commented (see how it goes without that)
//$row = array('uidIdPersona' => $row_data['uidIdPersona'], 'uidIdAlumno' => $row_data['uidIdAlumno']);
//$omigrate['users_alumno'][$row_data['uidIdAlumno']] = $row;
return $row_data['uidIdAlumno'];
}
@ -61,6 +62,7 @@ class MigrationCustom {
}
public function log_original_persona_unique_id($data, &$omigrate, $row_data) {
/* Temporarily commented
if (isset($omigrate['users_persona'][$row_data['uidIdPersona']])) {
$omigrate['users_persona'][$row_data['uidIdPersona']][] = $omigrate['users_persona'][$row_data['uidIdPersona']];
//$omigrate['users_persona'][$row_data['uidIdPersona']][] = $row_data;
@ -71,6 +73,7 @@ class MigrationCustom {
//$omigrate['users_persona'][$row_data['uidIdPersona']] = $row_data;
$omigrate['users_persona'][$row_data['uidIdPersona']] = $row_data['uidIdPersona'];
}
*/
return $data;
}
@ -159,6 +162,13 @@ class MigrationCustom {
}
}
/**
* Manage the user creation, including checking if the user hasn't been
* created previously
* @param array User data
* @param object List of migrated things
* @return array User info (from Chamilo DB)
*/
public function create_user($data, $omigrate) {
//error_log(print_r($data, 1));
@ -175,10 +185,7 @@ class MigrationCustom {
//error_log('teacher');
//$data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);
$data['status'] = COURSEMANAGER;
}
//Is a student
if (isset($omigrate['users_alumno'][$data['uidIdAlumno']])) {
} else {
$data['status'] = STUDENT;
}
@ -260,7 +267,7 @@ class MigrationCustom {
error_log(print_r($data, 1));
exit;
}
$id_persona = $data['uidIdPersona'];
unset($data['uidIdPersona']);
unset($data['uidIdAlumno']);
unset($data['uidIdEmpleado']);
@ -272,24 +279,32 @@ class MigrationCustom {
if (!$user_info) {
echo 'error';
}
UserManager::update_extra_field_value($user_info['user_id'],'uidIdPersona',$id_persona);
return $user_info;
}
/**
* Manages the course creation based on the rules in db_matches.php
*/
public function create_course($data) {
//Fixes wrong wanted codes
$data['wanted_code'] = str_replace(array('-', '_'), '000', $data['wanted_code']);
//Creates an evaluation
$data['create_gradebook_evaluation'] = true;
$data['language'] = 'spanish';
//Specific to ICPNA, set the default language to English
$data['language'] = 'english';
$data['gradebook_params'] = array(
'name' => 'Evaluación general',
'name' => 'General evaluation',
'user_id' => self::default_admin_id,
'weight' => '20',
'max' => '20'
);
return CourseManager::create_course($data);
}
/**
* Manages the session creation, based on data provided by the rules
* in db_matches.php
*/
public function create_session($data) {
$session_id = SessionManager::add($data);
if (!$session_id) {
@ -298,7 +313,9 @@ class MigrationCustom {
}
return $session_id;
}
/**
* Assigns a user to a session based on rules in db_matches.php
*/
public function add_user_to_session($data) {
//error_log('add_user_to_session');
///print_r($data);

@ -32,17 +32,18 @@ class MigrationMSSQL extends Migration {
}
//In order to process X item of each table add TOP X
$top = " TOP 100 ";
$top = null;
$top = " TOP 1000 ";
if (in_array($table, array('Empleado', 'Alumno'))) {
$top = " TOP 100 ";
$top = " TOP 1000 ";
}
if (in_array($table, array('ProgramaAcademico', 'Matricula'))) {
$top = " TOP 100 ";
$top = " TOP 1000 ";
}
//$top = null;
//$top = " TOP 2500 ";
// $top = " TOP 25000 ";
$extra = null;
if (isset($options) && !empty($options['inner_join'])) {
$extra = ' '.$options['alias_orig_table'].' INNER JOIN '.$options['inner_join'].' '.$options['alias_join_table'].' ON '.$options['on'];
@ -75,4 +76,4 @@ class MigrationMSSQL extends Migration {
public function num_rows() {
return mssql_num_rows($this->rows_iterator);
}
}
}

Loading…
Cancel
Save