diff --git a/main/admin/filler.php b/main/admin/filler.php index 0f0b3fc810..926931e682 100644 --- a/main/admin/filler.php +++ b/main/admin/filler.php @@ -39,6 +39,10 @@ if (!empty($_GET['fill'])) { require api_get_path(SYS_TEST_PATH).'datafiller/fill_courses.php'; $output = fill_courses(); break; + case 'exe': + require api_get_path(SYS_TEST_PATH).'datafiller/fill_exe.php'; + $output = fill_exe(); + break; default: break; } @@ -68,8 +72,9 @@ if (count($output)>0) { '.Display :: return_icon('test_results.png', get_lang('Results'),'',ICON_SIZE_SMALL).''; @@ -610,6 +610,12 @@ if (!empty($exercise_list)) { } else { $actions .= Display::return_icon('delete_na.png', get_lang('ResourceLockedByGradebook'),'',ICON_SIZE_SMALL); } + if ($rowi > 50) { + // Showing the direct link to exercise settings only + // if there are more than 50 questions, so the list of + // questions does not slow down everything + $actions .= Display::url(Display::return_icon('settings.png',get_lang('EditSettings'),'',ICON_SIZE_SMALL), 'exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$row['id']); + } } // Number of questions @@ -882,4 +888,4 @@ if (empty($exercise_list) && $hotpotatoes_exist == false) { } if ($origin != 'learnpath') { //so we are not in learnpath tool Display :: display_footer(); -} \ No newline at end of file +} diff --git a/main/exercice/exercise.class.php b/main/exercice/exercise.class.php index 2dadc782a8..822a2e654c 100644 --- a/main/exercice/exercise.class.php +++ b/main/exercice/exercise.class.php @@ -636,7 +636,8 @@ class Exercise { /** * updates the exercise in the data base - * + * @param string If "simple", then only updates title, description and visibility. Otherwise updates all fields + * @return mixed False on error, void otherwise * @author - Olivier Brouckaert */ function save($type_e = '') { @@ -681,7 +682,6 @@ class Exercise { } else { $end_time = '0000-00-00 00:00:00'; } - // Exercise already exists if ($id) { $sql="UPDATE $TBL_EXERCICES SET @@ -708,7 +708,8 @@ class Exercise { results_disabled='".Database::escape_string($results_disabled)."'"; } $sql .= " WHERE c_id = ".$this->course_id." AND id='".Database::escape_string($id)."'"; - Database::query($sql); + $r = Database::query($sql); + if ($r === false) { return false; } // update into the item_property table api_item_property_update($_course, TOOL_QUIZ, $id,'QuizUpdated',api_get_user_id()); @@ -718,36 +719,37 @@ class Exercise { } } else { // creates a new exercise - $sql = "INSERT INTO $TBL_EXERCICES (c_id, start_time, end_time, title, description, sound, type, random, random_answers, active, - results_disabled, max_attempt, feedback_type, expired_time, session_id, review_answers, random_by_category, - text_when_finished, display_category_name, pass_percentage) - VALUES( - ".$this->course_id.", - '$start_time','$end_time', - '".Database::escape_string($exercise)."', - '".Database::escape_string($description)."', - '".Database::escape_string($sound)."', - '".Database::escape_string($type)."', - '".Database::escape_string($random)."', - '".Database::escape_string($random_answers)."', - '".Database::escape_string($active)."', - '".Database::escape_string($results_disabled)."', - '".Database::escape_string($attempts)."', - '".Database::escape_string($feedback_type)."', - '".Database::escape_string($expired_time)."', - '".Database::escape_string($session_id)."', - '".Database::escape_string($review_answers)."', - '".Database::escape_string($randomByCat)."', - '".Database::escape_string($text_when_finished)."', - '".Database::escape_string($display_category_name)."', - '".Database::escape_string($pass_percentage)."' - )"; - Database::query($sql); + $sql = "INSERT INTO $TBL_EXERCICES (c_id, start_time, end_time, title, description, sound, type, random, random_answers, active, ". + " results_disabled, max_attempt, feedback_type, expired_time, session_id, review_answers, random_by_category, ". + " text_when_finished, display_category_name, pass_percentage) ". + " VALUES( ". + $this->course_id.", ". + " '$start_time','$end_time', ". + " '".Database::escape_string($exercise)."', ". + " '".Database::escape_string($description)."', ". + " '".Database::escape_string($sound)."', ". + " '".Database::escape_string($type)."', ". + " '".Database::escape_string($random)."', ". + " '".Database::escape_string($random_answers)."', ". + " '".Database::escape_string($active)."', ". + " '".Database::escape_string($results_disabled)."', ". + " '".Database::escape_string($attempts)."', ". + " '".Database::escape_string($feedback_type)."', ". + " '".Database::escape_string($expired_time)."', ". + " '".Database::escape_string($session_id)."', ". + " '".Database::escape_string($review_answers)."', ". + " '".Database::escape_string($randomByCat)."', ". + " '".Database::escape_string($text_when_finished)."', ". + " '".Database::escape_string($display_category_name)."', ". + " '".Database::escape_string($pass_percentage)."' ". + " )"; + $r = Database::query($sql); + if ($r === false) { return false; } $this->id = Database::insert_id(); // insert into the item_property table api_item_property_update($this->course, TOOL_QUIZ, $this->id, 'QuizAdded', api_get_user_id()); - api_set_default_visibility($this->id, TOOL_QUIZ); + api_set_default_visibility($this->id, TOOL_QUIZ); if (api_get_setting('search_enabled')=='true' && extension_loaded('xapian')) { $this->search_engine_save(); diff --git a/main/exercice/question.class.php b/main/exercice/question.class.php index 2ded0109fa..1255ec15d7 100644 --- a/main/exercice/question.class.php +++ b/main/exercice/question.class.php @@ -744,18 +744,18 @@ abstract class Question $current_position = Database::result($result,0,0); $this->updatePosition($current_position+1); $position = $this->position; - $sql = "INSERT INTO $TBL_QUESTIONS (c_id, question, description, ponderation, position, type, picture, extra, level, parent_id) VALUES ( - $c_id, - '".Database::escape_string($question)."', - '".Database::escape_string($description)."', - '".Database::escape_string($weighting)."', - '".Database::escape_string($position)."', - '".Database::escape_string($type)."', - '".Database::escape_string($picture)."', - '".Database::escape_string($extra)."', - '".Database::escape_string($level)."', - '".$this->parent_id."' - )"; + $sql = "INSERT INTO $TBL_QUESTIONS (c_id, question, description, ponderation, position, type, picture, extra, level, parent_id) VALUES ( ". + " $c_id, ". + " '".Database::escape_string($question)."', ". + " '".Database::escape_string($description)."', ". + " '".Database::escape_string($weighting)."', ". + " '".Database::escape_string($position)."', ". + " '".Database::escape_string($type)."', ". + " '".Database::escape_string($picture)."', ". + " '".Database::escape_string($extra)."', ". + " '".Database::escape_string($level)."', ". + " '".$this->parent_id."' ". + " )"; Database::query($sql); $this->id = Database::insert_id(); diff --git a/main/exercice/unique_answer.class.php b/main/exercice/unique_answer.class.php index e778b5d082..2dfbfeafe9 100644 --- a/main/exercice/unique_answer.class.php +++ b/main/exercice/unique_answer.class.php @@ -365,11 +365,26 @@ class UniqueAnswer extends Question { $header .= ''; return $header; } - - function create_answer($id, $question_id, $answer_title, $comment, $score = 0, $correct = 0) { + /** + * Create database record for the given answer + * @param int The answer ID (technically 1 is OK if you don't know) + * @param int The question this answer is attached to + * @param string The answer itself + * @param string The answer comment (shown as feedback if enabled) + * @param float Score given by this answer if selected (can be negative) + * @param int Whether this answer is considered correct (1) or not (0) + * @param int The course ID - if not provided, will be guessed from the context + * @return void + * @assert (1,null,'a','',1,1,null) === false + * @assert (1,1,'','',1,1,null) === false + */ + function create_answer($id=1, $question_id, $answer_title, $comment = '', $score = 0, $correct = 0, $course_id = null) { + if (empty($question_id) or empty($answer_title)) { return false; } $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER); $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION); - $course_id = api_get_course_int_id(); + if (empty($course_id)) { + $course_id = api_get_course_int_id(); + } $position = 1; $question_id = filter_var($question_id,FILTER_SANITIZE_NUMBER_INT); $score = filter_var($score,FILTER_SANITIZE_NUMBER_FLOAT); @@ -391,6 +406,7 @@ class UniqueAnswer extends Question { $sql = "UPDATE $tbl_quiz_question " ." SET ponderation = (ponderation + $score) WHERE c_id = $course_id AND id = ".$question_id; $rs = Database::query($sql); + return $rs; } } } diff --git a/main/inc/lib/course.lib.php b/main/inc/lib/course.lib.php index 470a0e4e3e..81fa82a735 100644 --- a/main/inc/lib/course.lib.php +++ b/main/inc/lib/course.lib.php @@ -80,6 +80,8 @@ class CourseManager { * Creates a course * @param array with the columns in the main.course table * @param mixed false if the course was not created, array with the course info + * @return mixed Array with course details, or false on error + * @assert (null) === false */ static function create_course($params) { global $_configuration; @@ -4514,9 +4516,9 @@ class CourseManager { $default_document_array = array(); foreach ($folders_to_copy_from_default_course as $folder) { $default_course_folder_path = $default_course_path.$folder.'/'; - $files = browse_folders($default_course_folder_path, array(), $folder); - $sorted_array = sort_pictures($files, 'dir'); - $sorted_array = array_merge($sorted_array, sort_pictures($files, 'file')); + $files = self::browse_folders($default_course_folder_path, array(), $folder); + $sorted_array = self::sort_pictures($files, 'dir'); + $sorted_array = array_merge($sorted_array, self::sort_pictures($files, 'file')); $default_document_array[$folder] = $sorted_array; } diff --git a/main/inc/lib/main_api.lib.php b/main/inc/lib/main_api.lib.php index a7e75f0902..8a5c037fad 100644 --- a/main/inc/lib/main_api.lib.php +++ b/main/inc/lib/main_api.lib.php @@ -1942,17 +1942,17 @@ function api_get_session_visibility($session_id, $course_code = null, $ignore_vi $row = Database::fetch_array($result, 'ASSOC'); $visibility = $original_visibility = $row['visibility']; - //I don't care the field visibility + // If there are no start/end date, we don't care about the field visibility if ($row['access_start_date'] == '0000-00-00 00:00:00' && $row['access_end_date'] == '0000-00-00 00:00:00') { return SessionManager::DEFAULT_VISIBILITY; - } else { - - //If access_start_date is set + } else { + // If access_start_date is set if (!empty($row['access_start_date']) && $row['access_start_date'] != '0000-00-00 00:00:00') { if ($now > api_strtotime($row['access_start_date'], 'UTC')) { $visibility = SESSION_AVAILABLE; } else { - $visibility = SESSION_INVISIBLE; + //$visibility = SESSION_INVISIBLE; + $visibility = $original_visibility; } } @@ -1960,14 +1960,14 @@ function api_get_session_visibility($session_id, $course_code = null, $ignore_vi if (!empty($row['access_end_date']) && $row['access_end_date'] != '0000-00-00 00:00:00') { //only if access_end_date said that it was ok if ($visibility == SESSION_AVAILABLE) { - $visibility = $row['visibility']; + $visibility = $original_visibility; if ($now <= api_strtotime($row['access_end_date'], 'UTC')) { //date still available $visibility = SESSION_AVAILABLE; } else { //session ends - $visibility = $row['visibility']; + $visibility = $original_visibility; } } } @@ -1976,7 +1976,8 @@ function api_get_session_visibility($session_id, $course_code = null, $ignore_vi //If I'm a coach the visibility can change in my favor depending in the coach_access_start_date and coach_access_end_date values $is_coach = api_is_coach($session_id, $course_code); - if ($is_coach) { + // if the user is coach and the session is not visible to normal users, check for special dates + if ($is_coach && in_array($visibility,array(SESSION_VISIBLE_READ_ONLY, SESSION_INVISIBLE))) { //Test end date if (isset($row['access_end_date']) && !empty($row['access_end_date']) && $row['access_end_date'] != '0000-00-00 00:00:00' && @@ -1986,7 +1987,8 @@ function api_get_session_visibility($session_id, $course_code = null, $ignore_vi if ($now <= $end_date_extra_for_coach) { $visibility = SESSION_AVAILABLE; } else { - $visibility = SESSION_INVISIBLE; + //$visibility = SESSION_INVISIBLE; + $visibility = $original_visibility; } } @@ -1997,7 +1999,8 @@ function api_get_session_visibility($session_id, $course_code = null, $ignore_vi if ($now > $start_date_for_coach) { $visibility = SESSION_AVAILABLE; } else { - $visibility = SESSION_INVISIBLE; + //$visibility = SESSION_INVISIBLE; + $visibility = $original_visibility; } } } else { diff --git a/main/inc/lib/usermanager.lib.php b/main/inc/lib/usermanager.lib.php index 2115ed1d0a..89b399c8ab 100644 --- a/main/inc/lib/usermanager.lib.php +++ b/main/inc/lib/usermanager.lib.php @@ -2208,36 +2208,35 @@ class UserManager { } } - $select = "SELECT DISTINCT session.id, - session.name, + $select = "SELECT DISTINCT session.id, ". + " session.name, ". - access_start_date, - access_end_date, - coach_access_start_date, - coach_access_end_date, + " access_start_date, ". + " access_end_date, ". + " coach_access_start_date, ". + " coach_access_end_date, ". - session_category_id, - session_category.name as session_category_name, - session_category.date_start session_category_date_start, - session_category.date_end session_category_date_end, + " session_category_id, ". + " session_category.name as session_category_name, ". + " session_category.date_start session_category_date_start, ". + " session_category.date_end session_category_date_end, ". - moved_to, - moved_status, - id_coach, - scu.id_user"; + " moved_to, ". + " moved_status, ". + " id_coach, ". + " scu.id_user"; if ($get_count) { $select = "SELECT count(session.id) as total_rows "; } - $sql = " $select FROM $tbl_session as session LEFT JOIN $tbl_session_category session_category ON (session_category_id = session_category.id) - INNER JOIN $tbl_session_course_user as scu ON (scu.id_session = session.id) - LEFT JOIN $tbl_session_user su ON su.id_session = session.id AND su.id_user = scu.id_user - WHERE ( - scu.id_user = $user_id OR session.id_coach = $user_id - ) $condition_date_end - ORDER BY session_category_name, name"; - + $sql = " $select FROM $tbl_session as session LEFT JOIN $tbl_session_category session_category ON (session_category_id = session_category.id) ". + " INNER JOIN $tbl_session_course_user as scu ON (scu.id_session = session.id) ". + " LEFT JOIN $tbl_session_user su ON su.id_session = session.id AND su.id_user = scu.id_user ". + " WHERE ( ". + " scu.id_user = $user_id OR session.id_coach = $user_id ". + " ) $condition_date_end ". + " ORDER BY session_category_name, name"; $result = Database::query($sql); if (Database::num_rows($result) > 0) { @@ -2254,13 +2253,18 @@ class UserManager { //Checking session visibility $visibility = api_get_session_visibility($session_id, null, false); - //var_dump($visibility); - switch ($visibility) { + // full access (we are inside normal dates range) + case SESSION_AVAILABLE: + break; + // access restricted by dates - only show in history view case SESSION_VISIBLE_READ_ONLY: case SESSION_VISIBLE: - case SESSION_AVAILABLE: + if (!$is_time_over) { + continue(2); + } break; + // access totally blocked - do not show case SESSION_INVISIBLE: continue(2); } diff --git a/main/install/install.lib.php b/main/install/install.lib.php index 9df94e152a..79412f9a90 100755 --- a/main/install/install.lib.php +++ b/main/install/install.lib.php @@ -2626,6 +2626,9 @@ function create_course_tables($course_db_name = null) { )" . $charset_clause; Database::query($sql); + $sql = "ALTER TABLE `".$TABLEQUIZANSWERSLIST . "` ADD INDEX idx_quiz_answer_c_q (c_id, question_id)"; + Database::query($sql); + // Exercise tool - answer options $sql = " CREATE TABLE `".$TABLEQUIZQUESTIONOPTION . "` ( diff --git a/main/install/migrate-db-1.9.0-1.10.0-pre.sql b/main/install/migrate-db-1.9.0-1.10.0-pre.sql index 36bf1dc4b3..2a5b2012c4 100755 --- a/main/install/migrate-db-1.9.0-1.10.0-pre.sql +++ b/main/install/migrate-db-1.9.0-1.10.0-pre.sql @@ -101,5 +101,8 @@ INSERT INTO branch_transaction_status VALUES (1, 'To be executed'), (2, 'Execute CREATE TABLE branch_transaction (id bigint unsigned not null AUTO_INCREMENT, transaction_id bigint unsigned, branch_id inti unsigned not null default 0, action char(20), item_id char(36), orig_id char(36), dest_id char(36), status_id tinyint not null default 0, time_insert datetime NOT NULL DEFAULT '0000-00-00 00:00:00', time_update datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id, transaction_id, branch_id)); +ALTER TABLE c_quiz_answer ADD INDEX idx_quiz_answer_c_q (c_id, question_id); +ALTER TABLE settings_current ADD INDEX idx_settings_current_au_cat (access_url, category(5)); + -- Do not move this -UPDATE settings_current SET selected_value = '1.10.0.20655' WHERE variable = 'chamilo_database_version'; +UPDATE settings_current SET selected_value = '1.10.0.20672' WHERE variable = 'chamilo_database_version'; diff --git a/tests/datafiller/data_exe.php b/tests/datafiller/data_exe.php new file mode 100644 index 0000000000..b26043ed64 --- /dev/null +++ b/tests/datafiller/data_exe.php @@ -0,0 +1,62 @@ + + * + */ +/** + * Initialisation section + */ + +$courses = array(); +$courses[] = array( + 'code' => 'ENGLISH101', + 'title' => 'English for beginners', + 'description' => 'English course', + 'category_code' => 'PROJ', + 'course_language' => 'english', + 'user_id' => 1, + 'expiration_date' => '2020-09-01 00:00:00', + 'exemplary_content' => true, + 'exes' => array( + 'exe' => array( + 'title' => 'Demo', + 'time' => 180, + 'attempts' => 1, + 'random' => 100, + 'pass_percentage' => 70, + 'review_answers' => 1, + ), + ), +); +$qst = array( + 0 => array( + 'title' => 'Demo question', + 'desc' => 'What do you think about XYZ?', + 'type' => 1, //1=Unique answer, see question.class.php + 'answers' => array( + 0 => array ( + 'title' => 'A', + 'correct' => 1, + 'score' => 1, + ), + 1 => array ( + 'title' => 'B', + 'correct' => 0, + 'score' => 0, + ), + 2 => array ( + 'title' => 'C', + 'correct' => 0, + 'score' => 0, + ), + 3 => array ( + 'title' => 'D', + 'correct' => 0, + 'score' => 0, + ), + + ), + ), +); diff --git a/tests/datafiller/fill_exe.php b/tests/datafiller/fill_exe.php new file mode 100644 index 0000000000..41db447676 --- /dev/null +++ b/tests/datafiller/fill_exe.php @@ -0,0 +1,83 @@ + + * + */ +/** + * Initialisation section + */ +/** + * Loads the data and injects it into the Chamilo database, using the Chamilo + * internal functions. + * @return array List of user IDs for the users that have just been inserted + */ +function fill_exe() { + $qc = 5000; //how many questions to create per exercise + $eol = PHP_EOL; + $courses = array(); //declare only to avoid parsing notice + $qst = array(); + require_once 'data_exe.php'; //fill the $users array + $output = array(); + $output[] = array('title'=>'Exe Filling Report: '); + $i = 1; + $lib = api_get_path(SYS_CODE_PATH); + require_once $lib.'exercice/exercise.class.php'; + require_once $lib.'exercice/question.class.php'; + require_once $lib.'exercice/answer.class.php'; + require_once $lib.'exercice/exercise.lib.php'; + foreach ($courses as $i => $course) { + $res = 0; + //first check that the first item doesn't exist already + $output[$i]['line-init'] = $course['title']; + $res = CourseManager::create_course($course); + $output[$i]['line-info'] = ($res ? $res : get_lang('NotInserted')); + $i++; + if (is_array($res)) { + //now insert an exercise + foreach ($course['exes'] as $exe) { + $objExercise = new Exercise(); + $objExercise->id = 0; + $objExercise->course_id = $res['real_id']; + $objExercise->exercise = $exe['title']; + $objExercise->type = 1; + $objExercise->attempts = $exe['attempts']; + $objExercise->random = $exe['random']; + $objExercise->active = 1; + $objExercise->propagate_neg = 0; + $objExercise->pass_percentage = $exe['pass_percentage']; + $objExercise->session_id = 0; + $objExercise->results_disabled = 0; + $objExercise->expired_time = $exe['time']; + $objExercise->review_answers = $exe['review_answers']; + $objExercise->save(); + $id = $objExercise->id; + if (!empty($id)) { + $qi = 0; + while ($qi < $qc) { + foreach ($qst as $q) { + error_log('Created '.$qi.' questions'); + $question = Question::getInstance($q['type']); + $question->id = 0; + $question->question = $q['title'].' '.$qi; + $question->description = $q['desc']; + $question->type = $q['type']; + $question->course = $res; + $r = $question->save($id); + if ($r === false) { continue; } + $qid = $question->id; + $aid = 1; + foreach ($q['answers'] as $asw) { + $answer = new UniqueAnswer($qid); + $answer->create_answer($aid,$qid,$asw['title'],'',$asw['score'],$asw['correct'], $res['real_id']); + $aid++; + } + $qi++; + } + } + } + } + } + } + return $output; +} diff --git a/tests/migrate/migrate.php b/tests/migrate/migrate.php index 906642581d..2f8c2c008c 100644 --- a/tests/migrate/migrate.php +++ b/tests/migrate/migrate.php @@ -27,6 +27,8 @@ if (empty($servers)) { die("This script requires a servers array with the connection settings and the file to parse\n"); } +$data_list = array('users'=>array(),'courses'=>array(),'sessions'=>array()); + $start = time(); echo "\n-- Starting at ".date('h:i:s')." local server time\n"; if (!empty($servers)) { @@ -34,6 +36,7 @@ if (!empty($servers)) { if ($server_info['active']) { echo "\n---- Start loading server----- \n"; echo $server_info['name']."\n\n"; + error_log('Treating server '.$server_info['name']); //echo "---- ----------------------- \n"; $config_info = $server_info['connection']; diff --git a/tests/migrate/migration.class.php b/tests/migrate/migration.class.php index 4368b9a0da..6db6f65523 100644 --- a/tests/migrate/migration.class.php +++ b/tests/migrate/migration.class.php @@ -50,20 +50,6 @@ class Migration { */ public $odbrows = null; - /** - * Temporary holder for the list of users, courses and sessions and their - * data. Store values here (preferably using the same indexes as the - * destination database field names) until ready to insert into Chamilo. - */ - public $data_list = array( - 'boost_users' => false, - 'users' => array(), - 'boost_courses' => false, - 'courses' => array(), - 'boost_sessions' => false, - 'sessions' => array(), - ); - public $web_service_connection_info = array(); /** @@ -90,9 +76,10 @@ class Migration { // Set the boost level if set in config.php if (!empty($boost) && is_array($boost)) { + global $data_list; foreach ($boost as $item => $val) { if ($val == true) { - $this->data_list[$item] = true; + $data_list[$item] = true; } } } @@ -1014,6 +1001,7 @@ class Migration { $extra_field_value_obj = new ExtraFieldValue($table['dest_table']); } $extra_fields_to_insert = array(); + global $data_list; foreach ($table['fields_match'] as $id_field => $details) { //if ($table['dest_table'] == 'session') {error_log('Processing field '.$details['orig']);} @@ -1033,7 +1021,7 @@ class Migration { } else { // if an alteration function is defined, run it on the field //error_log(__FILE__.' '.__LINE__.' Preparing to treat field with '.$details['func']); - $dest_data = MigrationCustom::$details['func']($row[$details['orig']], $this->data_list, $row); + $dest_data = MigrationCustom::$details['func']($row[$details['orig']], $data_list, $row); } if (isset($dest_row[$details['dest']])) { @@ -1098,7 +1086,7 @@ class Migration { //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)); + $item_result = call_user_func_array($table['dest_func'], array($dest_row, $data_list)); /* if (isset($table['show_in_error_log']) && $table['show_in_error_log'] == false) { @@ -1107,12 +1095,12 @@ class Migration { } */ //error_log('Result of calling ' . $table['dest_func'] . ': ' . print_r($item_result, 1)); - //After the function was executed fill the $this->data_list array + //After the function was executed fill the $data_list array switch ($table['dest_table']) { case 'course': //Saving courses in array if ($item_result) { - //$this->data_list['courses'][$dest_row['uidIdCurso']] = $item_result; + //$data_list['courses'][$dest_row['uidIdCurso']] = $item_result; } else { error_log('Course Not FOUND'); error_log(print_r($item_result, 1)); @@ -1125,11 +1113,11 @@ class Migration { $handler_id = $item_result['user_id']; //error_log($dest_row['email'].' '.$dest_row['uidIdPersona']); if (isset($dest_row['uidIdAlumno'])) { - //$this->data_list['users_alumno'][$dest_row['uidIdAlumno']]['extra'] = $item_result; + //$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; + //$data_list['users_empleado'][$dest_row['uidIdEmpleado']]['extra'] = $item_result; } } else { global $api_failureList; @@ -1138,7 +1126,7 @@ class Migration { } break; case 'session': - //$this->data_list['sessions'][$dest_row['uidIdPrograma']] = $item_result; + //$data_list['sessions'][$dest_row['uidIdPrograma']] = $item_result; $handler_id = $item_result; //session_id break; } diff --git a/tests/migrate/migration.custom.class.php b/tests/migrate/migration.custom.class.php index aa8f43364c..ab79068940 100644 --- a/tests/migrate/migration.custom.class.php +++ b/tests/migrate/migration.custom.class.php @@ -11,12 +11,12 @@ * during the migration */ class MigrationCustom { - + const default_admin_id = 1; const TRANSACTION_STATUS_TO_BE_EXECUTED = 1; const TRANSACTION_STATUS_SUCCESSFUL = 2; const TRANSACTION_STATUS_DEPRECATED = 3; //?? - const TRANSACTION_STATUS_FAILED = 4; + const TRANSACTION_STATUS_FAILED = 4; /** * Types of transaction operations read from the external databases */ @@ -50,12 +50,12 @@ class MigrationCustom { const TRANSACTION_TYPE_ADD_FASE = 28; const TRANSACTION_TYPE_DEL_FASE = 29; const TRANSACTION_TYPE_EDIT_FASE = 30; - + static function get_transaction_status_list() { $table = Database::get_main_table(TABLE_MIGRATION_TRANSACTION_STATUS); return Database::select("*", $table); } - + /** * The only required method is the 'none' method, which will not trigger * any process at all @@ -65,18 +65,18 @@ class MigrationCustom { static function none($data) { return $data; } - + static function join_horario($data, &$omigrate, $row_data) { return '('.$row_data['chrIdHorario'].') '.$row_data['chrHoraInicial'].' '.$row_data['chrHoraFinal']; } - + /** - * Converts 2008-02-01 12:20:20.540 to 2008-02-01 12:20:20 + * Converts 2008-02-01 12:20:20.540 to 2008-02-01 12:20:20 */ static function clean_date_time($date) { return substr($date, 0, 19); } - + /* Converts 2009-09-30T00:00:00-05:00 to 2009-09-30 00:00:00*/ static function clean_date_time_from_ws($date) { $pre_clean = self::clean_date_time($date, 0, 19); @@ -89,7 +89,7 @@ class MigrationCustom { * @return string SQL select string to include in the final select */ static function sql_alter_unhash_50($field) { - $as_field = explode('.', $field); + $as_field = explode('.', $field); if (isset($as_field[1])) { $as_field = $as_field[1]; } else { @@ -97,135 +97,138 @@ class MigrationCustom { } return " cast( $field as varchar(50)) as $as_field "; } - + static function clean_utf8($value) { - return utf8_encode($value); + return utf8_encode($value); } - + static function make_sha1($value) { + return sha1($value); + } + static function add_meses_label_to_extra_field_fase($value, $data, $row_data) { $label = 'meses'; if ($row_data['chrOrdenFase'] == 1) { - $label = 'mes'; + $label = 'mes'; } $value = $row_data['vchNombreFase'] .' ['.trim($row_data['chrOrdenFase']).' '.$label.']'; return self::clean_utf8($value); } - - static function clean_session_name($value, $omigrate, $row_data) { - return self::clean_utf8($row_data['session_name']); + + static function clean_session_name($value, &$omigrate, $row_data) { + return self::clean_utf8($row_data['session_name']); } - - static function get_real_course_code($data, $omigrate=null) { - if (is_object($omigrate) && $omigrate->boost_courses) { - if (isset($omigrate->courses[$data])) { - return $omigrate->courses[$data]; + + static function get_real_course_code($data, &$omigrate=null) { + if (is_array($omigrate) && $omigrate['boost_courses']) { + if (isset($omigrate['courses'][$data])) { + return $omigrate['courses'][$data]; } } $extra_field = new ExtraFieldValue('course'); - $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdCurso', $data); + $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdCurso', $data); if ($values) { return $values['course_code']; } else { error_log("Course ".print_r($data,1)." not found in DB"); } } - - static function get_session_id_by_programa_id($uidIdPrograma, $omigrate=null) { - if (is_object($omigrate) && $omigrate->boost_sessions) { - if (isset($omigrate->sessions[$uidIdPrograma])) { - return $omigrate->sessions[$uidIdPrograma]; + + static function get_session_id_by_programa_id($uidIdPrograma, &$omigrate=null) { + if (is_array($omigrate) && $omigrate['boost_sessions']) { + if (isset($omigrate['sessions'][$uidIdPrograma])) { + return $omigrate['sessions'][$uidIdPrograma]; } } $extra_field = new ExtraFieldValue('session'); - $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdPrograma', $uidIdPrograma); + $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdPrograma', $uidIdPrograma); if ($values) { return $values['session_id']; } else { //error_log("session id not found in DB"); - } + } } - + /* Not used */ - static function get_user_id_by_persona_id($uidIdPersona, $omigrate=null) { - if (is_object($omigrate) && $omigrate->boost_users) { - if (isset($omigrate->users[$uidIdPersona])) { - return $omigrate->users[$uidIdPersona]; + static function get_user_id_by_persona_id($uidIdPersona, &$omigrate=null) { + if (is_array($omigrate) && $omigrate['boost_users']) { + if (isset($omigrate['users'][$uidIdPersona])) { + return $omigrate['users'][$uidIdPersona]; } } //error_log('get_user_id_by_persona_id'); $extra_field = new ExtraFieldValue('user'); - $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdPersona', $uidIdPersona); + $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdPersona', $uidIdPersona); if ($values) { return $values['user_id']; } else { return 0; } } - - static function get_real_teacher_id($uidIdPersona, $omigrate=null) { - $default_teacher_id = self::default_admin_id; + + static function get_real_teacher_id($uidIdPersona, &$omigrate=null) { + $default_teacher_id = self::default_admin_id; if (empty($uidIdPersona)) { //error_log('No teacher provided'); return $default_teacher_id; } - if (is_object($omigrate) && $omigrate->boost_users) { - if (isset($omigrate->users[$uidIdPersona])) { - return $omigrate->users[$uidIdPersona]; + if (is_array($omigrate) && $omigrate['boost_users']) { + if (isset($omigrate['users'][$uidIdPersona])) { + return $omigrate['users'][$uidIdPersona]; } } - + $extra_field = new ExtraFieldValue('user'); $values = $extra_field->get_item_id_from_field_variable_and_field_value('uidIdPersona', $uidIdPersona); - + if ($values) { return $values['user_id']; } else { - return $default_teacher_id; - } + return $default_teacher_id; + } } - + /** - * Manage the user creation, including checking if the user hasn't been + * 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) */ - static function create_user($data, $omigrate) { + static function create_user($data, &$omigrate=null) { //error_log('In create_user, receiving '.print_r($data,1)); if (empty($data['uidIdPersona'])) { error_log('User does not have a uidIdPersona'); - error_log(print_r($data, 1)); + error_log(print_r($data, 1)); exit; } - + $data['uidIdPersona'] = strtoupper($data['uidIdPersona']); - + $data['status'] = STUDENT; if (isset($data['uidIdEmpleado'])) { $data['status'] = COURSEMANAGER; - } - + } + if (!isset($data['username']) || empty($data['username'])) { - $data['firstname'] = (string) trim($data['firstname']); - $data['lastname'] = (string) trim($data['lastname']); - + $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); + //$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[0]); //print_r($extra_data); //error_log("User_already_added - {$user_info['user_id']} - {$user_info['username']} - {$user_info['firstname']} - {$user_info['lastname']}"); return $user_info; } - + if (UserManager::is_username_available($wanted_user_name)) { $data['username'] = $wanted_user_name; error_log("username available $wanted_user_name"); @@ -233,21 +236,21 @@ class MigrationCustom { //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']}"); + error_log("Skip user already added: {$user_info['username']}"); return $user_info; } else { - error_log("Homonym - wanted_username: $wanted_user_name - uidIdPersona: {$user_persona['uidIdPersona']} - username: {$user_info['username']}"); + error_log("Homonym - wanted_username: $wanted_user_name - uidIdPersona: {$user_persona['uidIdPersona']} - username: {$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']); + //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']); error_log("homonym username created ". $data['username']); } } - + if (empty($data['username'])) { - //Last chance to have a nice username + //Last chance to have a nice username if (empty($data['firstname']) && empty($data['lastname'])) { $data['username'] = UserManager::create_unique_username(uniqid()); error_log("username empty 1". $data['username']); @@ -263,32 +266,33 @@ class MigrationCustom { //the user already exists? $user_info = UserManager::get_user_info_simple($data['username']); $user_persona = UserManager::get_extra_user_data_by_field($user_info['user_id'], 'uidIdPersona'); - - + + if (isset($user_persona['uidIdPersona']) && (string)$data['uidIdPersona'] == (string)$user_persona['uidIdPersona']) { - //error_log("2 Skip user already added: {$user_info['username']}"); + //error_log("2 Skip user already added: {$user_info['username']}"); return $user_info; } else { //print_r($user_persona); - //error_log("2 homonym - wanted_username: {$data['username']} - uidIdPersona: {$user_persona['uidIdPersona']} - username: {$user_info['username']}"); + //error_log("2 homonym - wanted_username: {$data['username']} - uidIdPersona: {$user_persona['uidIdPersona']} - username: {$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']); - //error_log("2 homonym username created ". $data['username']); + //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']); + //error_log("2 homonym username created ". $data['username']); } } } - + if (empty($data['username'])) { error_log('No Username provided'); error_log(print_r($data, 1)); exit; } - $id_persona = $data['uidIdPersona']; + $id_persona = $data['uidIdPersona']; unset($data['uidIdPersona']); unset($data['uidIdAlumno']); unset($data['uidIdEmpleado']); - + $data['encrypt_method'] = 'sha1'; + global $api_failureList; $api_failureList = array(); //error_log(print_r($data, 1)); @@ -298,25 +302,25 @@ class MigrationCustom { } else { //error_log('User '.$id_persona.' was created as user '.$user_info['user_id']); } - if (is_object($omigrate) && isset($omigrate) && $omigrate->boost_users) { - $omigrate->users[$id_persona] = $user_info['user_id']; + if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_users']) { + $omigrate['users'][$id_persona] = $user_info['user_id']; } 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 */ - static function create_course($data, $omigrate) { + static function create_course($data, &$omigrate=null) { //error_log('In create_course, received '.print_r($data,1)); //Fixes wrong wanted codes $data['wanted_code'] = str_replace(array('-', '_'), '000', $data['wanted_code']); - + //Specific to customer, set the default language to English $data['language'] = 'english'; $data['visibility'] = COURSE_VISIBILITY_REGISTERED; - + //Creates an evaluation $data['create_gradebook_evaluation'] = false; /* @@ -326,54 +330,54 @@ class MigrationCustom { 'weight' => '20', 'max' => '20' );*/ - $course_data = CourseManager::create_course($data); - if (is_object($omigrate) && isset($omigrate) && $omigrate->boost_courses) { - $omigrate->courses[$data['uidIdCurso']] = $course_data['code']; + $course_data = CourseManager::create_course($data); + if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_courses']) { + $omigrate['courses'][$data['uidIdCurso']] = $course_data['code']; } return $course_data; } - + /** * Manages the session creation, based on data provided by the rules * in db_matches.php * @return int The created (or existing) session ID */ - static function create_session($data, $omigrate) { + static function create_session($data, &$omigrate) { //Hack to add the default gradebook course to the session course - $data['create_gradebook_evaluation'] = true; + $data['create_gradebook_evaluation'] = true; /*$data['gradebook_params'] = array( 'name' => 'General evaluation', 'user_id' => self::default_admin_id, 'weight' => '20', 'max' => '20' );*/ - + //Here the $data variable has $data['course_code'] that will be added when creating the session // If session already exists, it will return the existing session id $session_id = SessionManager::add($data, true); - //error_log('create_session'); + //error_log('create_session'); if (!$session_id) { error_log('Error: Failed to create_session '.$data['name']); } else{ $c = SessionManager::set_coach_to_course_session($data['id_coach'], $session_id, $data['course_code']); - if (is_object($omigrate) && isset($omigrate) && $omigrate->boost_sessions) { - $omigrate->sessions[$data['uidIdPrograma']] = $session_id; + if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_sessions']) { + $omigrate['sessions'][$data['uidIdPrograma']] = $session_id; } } return $session_id; } - + /** * Assigns a user to a session based on rules in db_matches.php */ - static function add_user_to_session($data) { + static function add_user_to_session($data, &$omigrate=null) { $session_id = null; $user_id = null; - if (is_object($omigrate) && $omigrate->boost_sessions) { - if (isset($omigrate->sessions[$data['uidIdPrograma']])) { - $session_id = $omigrate->sessions[$data['uidIdPrograma']]; + if (is_array($omigrate) && $omigrate['boost_sessions']) { + if (isset($omigrate['sessions'][$data['uidIdPrograma']])) { + $session_id = $omigrate['sessions'][$data['uidIdPrograma']]; } - } + } if (empty($session_id)) { $extra_field_value = new ExtraFieldValue('session'); $result = $extra_field_value->get_item_id_from_field_variable_and_field_value('uidIdPrograma', $data['uidIdPrograma']); @@ -382,71 +386,71 @@ class MigrationCustom { $session_id = $result['session_id']; } } - - if (is_object($omigrate) && $omigrate->boost_users) { - if (isset($omigrate->users[$data['uidIdPersona']])) { - $user_id = $omigrate->users[$data['uidIdPersona']]; + + if (is_array($omigrate) && $omigrate['boost_users']) { + if (isset($omigrate['users'][$data['uidIdPersona']])) { + $user_id = $omigrate['users'][$data['uidIdPersona']]; } } if (empty($user_id)) { $extra_field_value = new ExtraFieldValue('user'); $result = $extra_field_value->get_item_id_from_field_variable_and_field_value('uidIdPersona', $data['uidIdPersona']); //error_log('data[uidIdPersona] '.$data['uidIdPersona'].' returned $result[user_id]: '.$result['user_id']); - if ($result && $result['user_id']) { - $user_id = $result['user_id']; + if ($result && $result['user_id']) { + $user_id = $result['user_id']; } } - - if (!empty($session_id) && !empty($user_id)) { + + if (!empty($session_id) && !empty($user_id)) { //error_log('Called: add_user_to_session - Subscribing: session_id: '.$session_id. ' user_id: '.$user_id); - SessionManager::suscribe_users_to_session($session_id, array($user_id), SESSION_VISIBLE_READ_ONLY, false, false); + SessionManager::suscribe_users_to_session($session_id, array($user_id), SESSION_VISIBLE_READ_ONLY, false, false); //exit; - } else { - //error_log('Called: add_user_to_session - No idPrograma: '.$data['uidIdPrograma'].' - No uidIdPersona: '.$data['uidIdPersona']); - } + } else { + //error_log('Called: add_user_to_session - No idPrograma: '.$data['uidIdPrograma'].' - No uidIdPersona: '.$data['uidIdPersona']); + } } - - static function create_attendance($data) { + + static function create_attendance($data) { error_log('create_attendance'); $session_id = $data['session_id']; $user_id = $data['user_id']; - + if (!empty($session_id) && !empty($user_id)) { - $attendance = new Attendance(); + $attendance = new Attendance(); $course_list = SessionManager::get_course_list_by_session_id($session_id); $attendance_id = null; - + if (!empty($course_list)) { $course = current($course_list); - + //Creating attendance if (isset($course['code'])) { $course_info = api_get_course_info($course['code']); - + $attendance->set_course_id($course_info['code']); $attendance->set_course_int_id($course_info['real_id']); $attendance->set_session_id($session_id); - $attendance_list = $attendance->get_attendances_list($course_info['real_id'], $session_id); + $attendance_list = $attendance->get_attendances_list($course_info['real_id'], $session_id); if (empty($attendance_list)) { $attendance->set_name('Asistencia'); $attendance->set_description(''); //$attendance->set_attendance_qualify_title($_POST['attendance_qualify_title']); //$attendance->set_attendance_weight($_POST['attendance_weight']); - $link_to_gradebook = false; + $link_to_gradebook = false; //$attendance->category_id = $_POST['category_id']; - $attendance_id = $attendance->attendance_add($link_to_gradebook, self::default_admin_id); + $attendance_id = $attendance->attendance_add($link_to_gradebook, self::default_admin_id); error_log("Attendance added course code: {$course['code']} - session_id: $session_id"); - //only 1 course per session + //only 1 course per session } else { - $attendance_data = current($attendance_list); + $attendance_data = current($attendance_list); $attendance_id = $attendance_data['id']; error_log("Attendance found in attendance_id = $attendance_id - course code: {$course['code']} - session_id: $session_id"); } - + if ($attendance_id) { //Attendance date exists? - $cal_info = $attendance->get_attendance_calendar_data_by_date($attendance_id, $data['fecha']); + $cal_info = $attendance->get_attendance_calendar_data_by_date($attendance_id, $data['fecha']); if ($cal_info && isset($cal_info['id'])) { $cal_id = $cal_info['id']; } else { @@ -456,11 +460,9 @@ class MigrationCustom { error_log("Creating attendance calendar $cal_id"); } //Adding presence for the user (by default everybody is present) - $status = 1; - //See one of this states:-> //$attendance->get_attendance_states(); - $users_present = array($status => $user_id); + $users_present = array($user_id); $attendance->attendance_sheet_add($cal_id, $users_present, $attendance_id, false, false); - error_log("Adding calendar to user: $user_id to calendar: $cal_id"); + error_log("Adding calendar to user: $user_id to calendar: $cal_id"); } else { error_log('No attendance_id created'); } @@ -472,56 +474,56 @@ class MigrationCustom { error_log("Missing data: session: $session_id - user_id: $user_id"); } } - + static function create_thematic($data) { error_log('create_thematic'); $session_id = $data['session_id']; - + if (!empty($session_id)) { $course_list = SessionManager::get_course_list_by_session_id($session_id); - + if (!empty($course_list)) { $course_data = current($course_list); $course_info = api_get_course_info($course_data['code']); - + if (!empty($course_data)) { $thematic = new Thematic(); $thematic->set_course_int_id($course_info['real_id']); - $thematic->set_session_id($session_id); + $thematic->set_session_id($session_id); $thematic_info = $thematic->get_thematic_by_title($data['thematic']); - + if (empty($thematic_info)) { $thematic->set_thematic_attributes(null, $data['thematic'], null, $session_id); $thematic_id = $thematic->thematic_save(); error_log("Thematic added to course code: {$course_info['code']} - session_id: $session_id"); } else { - $thematic_id = isset($thematic_info['id']) ? $thematic_info['id'] : null; + $thematic_id = isset($thematic_info['id']) ? $thematic_info['id'] : null; error_log("Thematic id #$thematic_id found in course: {$course_info['code']} - session_id: $session_id"); } - - if ($thematic_id) { + + if ($thematic_id) { $thematic->set_thematic_plan_attributes($thematic_id, $data['thematic_plan'], null, 6); $thematic->thematic_plan_save(); error_log("Saving plan attributes: {$data['thematic_plan']}"); } error_log("Adding thematic id : $thematic_id to session: $session_id to course: {$course_info['code']} real_id: {$course_info['real_id']}"); - + if ($thematic_id) { error_log("Thematic saved: $thematic_id"); } else { error_log("Thematic NOT saved"); } } - + if ($course_info['code'] != 'B05') { //exit; } } else { - error_log("No courses in session $session_id "); + error_log("No courses in session $session_id "); } - } + } } - + static function add_evaluation_type($params) { $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION_TYPE); if (!empty($params['name']) && !empty($params['external_id'])) { @@ -529,25 +531,25 @@ class MigrationCustom { unset($params['return_item_if_already_exists']); } Database::insert($table, $params); - } + } } - + static function get_evaluation_type($external_id) { $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION_TYPE); $external_id = intval($external_id); $sql = "SELECT * FROM $table WHERE external_id = $external_id"; - $result = Database::query($sql); + $result = Database::query($sql); if (Database::num_rows($result)) { $result = Database::fetch_array($result, 'ASSOC'); return $result['id']; } return false; } - + static function create_gradebook_evaluation($data){ error_log('create_gradebook_evaluation'); $session_id = isset($data['session_id']) ? $data['session_id'] : null; - + if (!empty($session_id)) { $course_list = SessionManager::get_course_list_by_session_id($session_id); if (!empty($course_list)) { @@ -556,50 +558,50 @@ class MigrationCustom { //Get gradebook $gradebook = new Gradebook(); $gradebook = $gradebook->get_first(array('where' => array('course_code = ? AND session_id = ?' => array($course_data['code'], $session_id)))); - error_log("Looking gradebook in course code: {$course_data['code']} - session_id: $session_id"); - if (!empty($gradebook)) { + error_log("Looking gradebook in course code: {$course_data['code']} - session_id: $session_id"); + if (!empty($gradebook)) { //error_log("Gradebook exists {$gradebook['id']}"); //Check if gradebook exists - $eval = new Evaluation(); + $eval = new Evaluation(); $evals_found = $eval->load(null, null, $course_data['code'], null, null, null, $data['gradebook_description']); - - if (empty($evals_found)) { + + if (empty($evals_found)) { $eval->set_name($data['gradebook_description']); $eval->set_description($data['gradebook_description']); $eval->set_evaluation_type_id($data['gradebook_evaluation_type_id']); $eval->set_user_id(self::default_admin_id); - $eval->set_course_code($course_data['code']); + $eval->set_course_code($course_data['code']); $eval->set_category_id($gradebook['id']); //harcoded values - $eval->set_weight(20); + $eval->set_weight(20); $eval->set_max(20); $eval->set_visible(1); $eval->add(); error_log("Gradebook evaluation was created!!"); } else { error_log("Gradebook evaluation was already created :( "); - } + } } else { error_log("Gradebook does not exists"); - } + } } else { error_log("Something is wrong with the course "); - } + } } else { - error_log("NO course found for session id: $session_id"); + error_log("NO course found for session id: $session_id"); } - + } else { error_log("NO session id found: $session_id"); - } + } } - + static function add_gradebook_result($data) { error_log('add_gradebook_result'); $session_id = isset($data['session_id']) ? $data['session_id'] : null; - $user_id = isset($data['user_id']) ? $data['user_id'] : null; - + $user_id = isset($data['user_id']) ? $data['user_id'] : null; + if (!empty($session_id) && !empty($user_id)) { $course_list = SessionManager::get_course_list_by_session_id($session_id); if (!empty($course_list)) { @@ -608,20 +610,20 @@ class MigrationCustom { //Get gradebook $gradebook = new Gradebook(); $gradebook = $gradebook->get_first(array('where' => array('course_code = ? AND session_id = ?' => array($course_data['code'], $session_id)))); - error_log("Looking gradebook in course code: {$course_data['code']} - session_id: $session_id, user_id: $user_id"); - if (!empty($gradebook)) { + error_log("Looking gradebook in course code: {$course_data['code']} - session_id: $session_id, user_id: $user_id"); + if (!empty($gradebook)) { error_log("Gradebook exists: {$gradebook['id']}"); - + //Check if gradebook exists - $eval = new Evaluation(); + $eval = new Evaluation(); $evals_found = $eval->load(null, null, null, $gradebook['id'], null, null); - - if (!empty($evals_found)) { + + if (!empty($evals_found)) { $evaluation = current($evals_found); $eval_id = $evaluation->get_id(); - - //Eval found - $res = new Result(); + + //Eval found + $res = new Result(); $check_result = Result :: load (null, $user_id, $eval_id); if (empty($check_result)) { $res->set_evaluation_id($eval_id); @@ -638,27 +640,27 @@ class MigrationCustom { } } else { error_log("Gradebook does not exists"); - } + } } else { error_log("Something is wrong with the course "); - } + } } else { - error_log("NO course found for session id: $session_id"); + error_log("NO course found for session id: $session_id"); } - + } else { error_log("NO session id found: $session_id"); } } - + static function add_gradebook_result_with_evaluation($data) { error_log('add_gradebook_result_with_evaluation'); $session_id = isset($data['session_id']) ? $data['session_id'] : null; $user_id = isset($data['user_id']) ? $data['user_id'] : null; - + //Default evaluation title $title = 'Evaluación General'; - + if (!empty($session_id) && !empty($user_id)) { $course_list = SessionManager::get_course_list_by_session_id($session_id); if (!empty($course_list)) { @@ -667,39 +669,39 @@ class MigrationCustom { //Get gradebook $gradebook = new Gradebook(); $gradebook = $gradebook->get_first(array('where' => array('course_code = ? AND session_id = ?' => array($course_data['code'], $session_id)))); - error_log("Looking gradebook in course code: {$course_data['code']} - session_id: $session_id, user_id: $user_id"); - if (!empty($gradebook)) { + error_log("Looking gradebook in course code: {$course_data['code']} - session_id: $session_id, user_id: $user_id"); + if (!empty($gradebook)) { error_log("Gradebook exists: {$gradebook['id']}"); - - //Creates + + //Creates $eval = new Evaluation(); $evals_found = $eval->load(null, null, $course_data['code'], $gradebook['id'], null, null, $title); - + if (empty($evals_found)) { - $eval->set_name($title); + $eval->set_name($title); //$eval->set_evaluation_type_id($data['gradebook_evaluation_type_id']); $eval->set_user_id(self::default_admin_id); - $eval->set_course_code($course_data['code']); + $eval->set_course_code($course_data['code']); $eval->set_category_id($gradebook['id']); //harcoded values - $eval->set_weight(20); + $eval->set_weight(20); $eval->set_max(20); $eval->set_visible(1); $eval->add(); error_log("Gradebook evaluation was created!!"); $eval_id = $eval->get_id(); - error_log("eval id created: $eval_id"); + error_log("eval id created: $eval_id"); } else { $eval = current($evals_found); error_log("Gradebook evaluation already exists "); $eval_id = $eval->get_id(); - error_log("eval id loaded : $eval_id"); + error_log("eval id loaded : $eval_id"); } - + if ($eval_id) { $res = new Result(); - //Check if already exists + //Check if already exists $check_result = Result :: load (null, $user_id, $eval_id); if (empty($check_result)) { $res->set_evaluation_id($eval_id); @@ -710,38 +712,38 @@ class MigrationCustom { error_log("Result Added :)"); } else { error_log("Result already added "); - } + } } else { error_log("Evaluation not detected"); - } + } } else { error_log("Gradebook does not exists"); - } + } } else { error_log("Something is wrong with the course "); - } + } } else { - error_log("NO course found for session id: $session_id"); - } + error_log("NO course found for session id: $session_id"); + } } else { error_log("NO session id found: $session_id"); } } - - + + /* Transaction methods */ - + //añadir usuario: usuario_agregar UID //const TRANSACTION_TYPE_ADD_USER = 1; static function transaction_1($data, $web_service_details) { $uidIdPersonaId = $data['item_id']; - //Add user call the webservice + //Add user call the webservice $user_info = Migration::soap_call($web_service_details, 'usuarioDetalles', array('intIdSede'=> $data['branch_id'], 'uididpersona' => $uidIdPersonaId)); if ($user_info['error'] == false) { global $api_failureList; unset($user_info['error']); - $chamilo_user_info = UserManager::add($user_info); - + $chamilo_user_info = UserManager::add($user_info); + if ($chamilo_user_info && $chamilo_user_info['user_id']) { $chamilo_user_info = api_get_user_info($chamilo_user_info['user_id'], false, false, true); return array( @@ -759,16 +761,16 @@ class MigrationCustom { } } else { return $user_info; - } + } } - + //eliminar usuario usuario_eliminar UID //const TRANSACTION_TYPE_DEL_USER = 2; static function transaction_2($data) { - $uidIdPersonaId = strtoupper($data['item_id']); + $uidIdPersonaId = strtoupper($data['item_id']); $user_id = self::get_user_id_by_persona_id($uidIdPersonaId); if ($user_id) { - $chamilo_user_info_before = api_get_user_info($user_id, false, false, true); + $chamilo_user_info_before = api_get_user_info($user_id, false, false, true); $result = UserManager::delete_user($user_id); $chamilo_user_info = api_get_user_info($user_id, false, false, true); if ($result) { @@ -792,14 +794,14 @@ class MigrationCustom { ); } } - + //editar detalles de usuario (nombre/correo/contraseña) usuario_editar UID //const TRANSACTION_TYPE_EDIT_USER = 3; static function transaction_3($data, $web_service_details) { - $uidIdPersonaId = strtoupper($data['item_id']); + $uidIdPersonaId = strtoupper($data['item_id']); $user_id = self::get_user_id_by_persona_id($uidIdPersonaId); - if ($user_id) { - $user_info = Migration::soap_call($web_service_details, 'usuarioDetalles', array('intIdSede'=> $data['branch_id'], 'uididpersona' => $uidIdPersonaId)); + if ($user_id) { + $user_info = Migration::soap_call($web_service_details, 'usuarioDetalles', array('intIdSede'=> $data['branch_id'], 'uididpersona' => $uidIdPersonaId)); if ($user_info['error'] == false) { unset($user_info['error']); //Edit user @@ -824,7 +826,7 @@ class MigrationCustom { ); } } - + //cambiar usuario de progr. académ. (de A a B, de A a nada, de nada a A) (como estudiante o profesor) usuario_matricula UID ORIG DEST //const TRANSACTION_TYPE_SUB_USER = 4; //subscribe user to a session static function transaction_4($data) { @@ -832,60 +834,60 @@ class MigrationCustom { $uidIdPrograma = $data['orig_id']; $uidIdProgramaDestination = $data['dest_id']; $user_id = self::get_user_id_by_persona_id($uidIdPersona); - + if (empty($user_id)) { return array( 'message' => "User does not exists in DB: $uidIdPersona", 'status_id' => self::TRANSACTION_STATUS_FAILED ); } - + //Move A to B if (!empty($uidIdPrograma) && !empty($uidIdProgramaDestination)) { $session_id = self::get_session_id_by_programa_id($uidIdPrograma); $destination_session_id = self::get_session_id_by_programa_id($uidIdProgramaDestination); - + if (!empty($session_id) && !empty($destination_session_id)) { - + $before1 = SessionManager::get_user_status_in_session($session_id, $user_id); $before2 = SessionManager::get_user_status_in_session($destination_session_id, $user_id); - + /*SessionManager::unsubscribe_user_from_session($session_id, $user_id); SessionManager::suscribe_users_to_session($destination_session_id, array($user_id), SESSION_VISIBLE_READ_ONLY, false, false);*/ - + //Not sure what reason use /* $extra_field_value = new ExtraFieldValue('session'); - + SESSION_CHANGE_USER_REASON_SCHEDULE = 1; SESSION_CHANGE_USER_REASON_CLASSROOM = 2; SESSION_CHANGE_USER_REASON_LOCATION = 3; SESSION_CHANGE_USER_REASON_ENROLLMENT_ANNULATION = 4; - + $extra_field_value->compare_item_values($session_id, $destination_session_id, 'aula'); - + SessionManager::compare_extra_field($session_id, $session_id, 'aula'); SessionManager::detect_reason_by_extra_field($session_id, 'sede'); SessionManager::detect_reason_by_extra_field($session_id, 'horario'); SessionManager::detect_reason_by_extra_field($session_id, 'aula');*/ - + $reason_id = SESSION_CHANGE_USER_REASON_SCHEDULE; SessionManager::change_user_session($user_id, $session_id, $destination_session_id, $reason_id); - + $befores = array($before1, $before2); - - $message = "Move Session A to Session B"; - return self::check_if_user_is_subscribe_to_session($user_id, $destination_session_id, $message, $befores); + + $message = "Move Session A to Session B"; + return self::check_if_user_is_subscribe_to_session($user_id, $destination_session_id, $message, $befores); } else { - return array( + return array( 'message' => "Session ids were not correctly setup session_id 1: $session_id Session id 2 $uidIdProgramaDestination - Move Session A to Session B", 'status_id' => self::TRANSACTION_STATUS_FAILED ); } } - + //Move A to empty - if (!empty($uidIdPrograma) && empty($uidIdProgramaDestination)) { + if (!empty($uidIdPrograma) && empty($uidIdProgramaDestination)) { $session_id = self::get_session_id_by_programa_id($uidIdPrograma); if (!empty($session_id)) { $before = SessionManager::get_user_status_in_session($session_id, $user_id); @@ -900,11 +902,11 @@ class MigrationCustom { ); } } - + //Move empty to A if (empty($uidIdPrograma) && !empty($uidIdProgramaDestination)) { $session_id = self::get_session_id_by_programa_id($uidIdProgramaDestination); - if (!empty($session_id)) { + if (!empty($session_id)) { $before = SessionManager::get_user_status_in_session($session_id, $user_id); SessionManager::suscribe_users_to_session($session_id, array($user_id), SESSION_VISIBLE_READ_ONLY, false, false); $message = 'Move empty to Session'; @@ -917,7 +919,7 @@ class MigrationCustom { } } } - + static function check_if_user_is_subscribe_to_session($user_id, $session_id, $message = null, $before = array()) { $user_session_status = SessionManager::get_user_status_in_session($session_id, $user_id); if (!empty($user_session_status)) { @@ -935,17 +937,17 @@ class MigrationCustom { ); } } - + //Cursos //añadir curso curso_agregar CID //const TRANSACTION_TYPE_ADD_COURSE = 5; static function transaction_5($data, $web_service_details) { - $uidCursoId = $data['item_id']; - $course_info = Migration::soap_call($web_service_details, 'cursoDetalles', array('intIdSede'=> $data['branch_id'], 'uididcurso' => $uidCursoId)); + $uidCursoId = $data['item_id']; + $course_info = Migration::soap_call($web_service_details, 'cursoDetalles', array('intIdSede'=> $data['branch_id'], 'uididcurso' => $uidCursoId)); if ($course_info['error'] == false) { unset($course_info['error']); $course_info = CourseManager::create_course($course_info); - $course_info = api_get_course_info($course_info['code'], true); + $course_info = api_get_course_info($course_info['code'], true); if (!empty($course_info)) { return array( 'entity' => 'course', @@ -964,7 +966,7 @@ class MigrationCustom { return $course_info; } } - + //eliminar curso curso_eliminar CID //const TRANSACTION_TYPE_DEL_COURSE = 6; static function transaction_6($data) { @@ -986,25 +988,25 @@ class MigrationCustom { 'status_id' => self::TRANSACTION_STATUS_FAILED ); } - + } - + //editar detalles de curso curso_editar CID //const TRANSACTION_TYPE_EDIT_COURSE = 7; static function transaction_7($data, $web_service_details) { $uidCursoId = $data['item_id']; - $course_code = self::get_real_course_code($uidCursoId); - if (!empty($course_code)) { - $course_info = api_get_course_info($course_code, true); + $course_code = self::get_real_course_code($uidCursoId); + if (!empty($course_code)) { + $course_info = api_get_course_info($course_code, true); $data_to_update = Migration::soap_call($web_service_details, 'cursoDetalles', array('intIdSede'=> $data['branch_id'], 'uididcurso' => $uidCursoId)); - + if ($data_to_update['error'] == false) { //do some cleaning $data_to_update['code'] = $course_info['code']; - unset($data_to_update['error']); + unset($data_to_update['error']); CourseManager::update($data_to_update); $course_info_after = api_get_course_info($course_code, true); - + return array( 'entity' => 'course', 'before' => $course_info, @@ -1014,7 +1016,7 @@ class MigrationCustom { ); } else { return $data_to_update; - } + } } else { return array( 'message' => "couCoursese_code does not exists $course_code ", @@ -1022,7 +1024,7 @@ class MigrationCustom { ); } } - + /** * Cambiar curso de progr. académ. (de nada a A) curso_matricula CID ORIG DEST * @todo Unused ? @@ -1032,10 +1034,10 @@ class MigrationCustom { $course_code = self::get_real_course_code($data['item_id']); $uidIdPrograma = $data['orig_id']; $uidIdProgramaDestination = $data['dest_id']; - + $session_id = self::get_session_id_by_programa_id($uidIdPrograma); $destination_session_id = self::get_session_id_by_programa_id($uidIdProgramaDestination); - + if (!empty($course_code)) { if (empty($uidIdPrograma) && !empty($uidIdProgramaDestination) && !empty($destination_session_id)) { SessionManager::add_courses_to_session($destination_session_id, array($course_code)); @@ -1048,7 +1050,7 @@ class MigrationCustom { 'message' => "Session destination was not found - [dest_id] not found", 'status_id' => self::TRANSACTION_STATUS_FAILED ); - } + } } else { return array( 'message' => "Course does not exists $course_code", @@ -1056,13 +1058,13 @@ class MigrationCustom { ); } } - + //Programas académicos //añadir p.a. pa_agregar PID // const TRANSACTION_TYPE_ADD_SESS = 8; static function transaction_8($data, $web_service_details) { $session_info = Migration::soap_call($web_service_details, 'programaDetalles', array('intIdSede'=> $data['branch_id'], 'uididprograma' => $data['item_id'])); - + if ($session_info['error'] == false) { unset($session_info['error']); $session_id = SessionManager::add($session_info); @@ -1086,13 +1088,13 @@ class MigrationCustom { return $session_info; } } - - + + //eliminar p.a. pa_eliminar PID //const TRANSACTION_TYPE_DEL_SESS = 9; static function transaction_9($data) { - $uidIdPrograma = $data['item_id']; - $session_id = self::get_session_id_by_programa_id($uidIdPrograma); + $uidIdPrograma = $data['item_id']; + $session_id = self::get_session_id_by_programa_id($uidIdPrograma); if (!empty($session_id)) { $session_info_before = api_get_session_info($session_id, true); SessionManager::delete_session($session_id, true); @@ -1111,15 +1113,15 @@ class MigrationCustom { ); } } - + //editar detalles de p.a. pa_editar PID // const TRANSACTION_TYPE_EDIT_SESS = 10; - static function transaction_10($data, $web_service_details) { - $uidIdPrograma = $data['item_id']; + static function transaction_10($data, $web_service_details) { + $uidIdPrograma = $data['item_id']; $session_id = self::get_session_id_by_programa_id($uidIdPrograma); - if (!empty($session_id)) { + if (!empty($session_id)) { $session_info = Migration::soap_call($web_service_details, 'programaDetalles', array('intIdSede'=> $data['branch_id'], 'uididprograma' => $data['item_id'])); - if ($session_info['error'] == false) { + if ($session_info['error'] == false) { $session_info['id'] = $session_id; unset($session_info['error']); $session_info_before = api_get_session_info($session_id, true); @@ -1134,58 +1136,58 @@ class MigrationCustom { ); } else { return $session_info; - } + } } else { return array( 'message' => "Session does not exists $uidIdPrograma", 'status_id' => self::TRANSACTION_STATUS_FAILED ); - } + } } - + static function transaction_cambiar_generic($extra_field_variable, $data) { - $uidIdPrograma = $data['item_id']; + $uidIdPrograma = $data['item_id']; //$orig_id = $data['orig_id']; $destination_id = $data['dest_id']; - + $common_message = " - item_id: $uidIdPrograma, dest_id: $destination_id - looking for extra_field_variable: $extra_field_variable - with data ".print_r($data, 1); - - $session_id = self::get_session_id_by_programa_id($uidIdPrograma); + + $session_id = self::get_session_id_by_programa_id($uidIdPrograma); if (!empty($session_id)) { //?? $extra_field = new ExtraField('session'); $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($extra_field_variable); //horario, aula, etc - + if (empty($extra_field_info)) { return array( 'message' => "Extra field $extra_field_variable doest not exists in the DB $common_message", 'status_id' => self::TRANSACTION_STATUS_FAILED ); } - + //check if option exists $extra_field_option = new ExtraFieldOption('session'); $extra_field_option_info = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $destination_id); //horario, aula, etc - + if ($extra_field_option_info) { $extra_field_value = new ExtraFieldValue('session'); - + //Getting info before $info_before = $extra_field_value->get_values_by_handler_and_field_id($session_id, $extra_field_info['id']); - + //Delete previous extra field value $extra_field_value->delete_values_by_handler_and_field_id($session_id, $extra_field_info['id']); - + $params = array( 'session_id' => $session_id, 'field_id' => $extra_field_info['id'], 'field_value' => $destination_id, - ); + ); $extra_field_value->save($params); - + //Getting info after $info_after = $extra_field_value->get_values_by_handler_and_field_id($session_id, $extra_field_info['id']); - + return array( 'entity' => $extra_field_variable, 'before' => $info_before, @@ -1193,7 +1195,7 @@ class MigrationCustom { 'message' => "Extra field $extra_field_variable saved with params: ".print_r($params, 1), 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL ); - } else { + } else { return array( 'message' => "Option does not exists dest_id: $destination_id $common_message", 'status_id' => self::TRANSACTION_STATUS_FAILED @@ -1204,40 +1206,40 @@ class MigrationCustom { 'message' => "Session does not exists: $uidIdPrograma $common_message", 'status_id' => self::TRANSACTION_STATUS_FAILED ); - } + } } - + //cambiar aula pa_cambiar_aula PID ORIG DEST //const TRANSACTION_TYPE_UPD_ROOM = 11; static function transaction_11($data) { return self::transaction_cambiar_generic('aula', $data); } - + //cambiar horario pa_cambiar_horario PID ORIG DEST //const TRANSACTION_TYPE_UPD_SCHED = 12; static function transaction_12($data) { return self::transaction_cambiar_generic('horario', $data); - } - - //cambiar sede pa_cambiar_sede PID ORIG DEST + } + + //cambiar sede pa_cambiar_sede PID ORIG DEST //no se usa (se declara el p.a. en otra sede, nada más) static function transaction_pa_cambiar_sede($data) { return self::transaction_cambiar_generic('sede', $data); } - + //cambiar intensidad pa_cambiar_fase_intensidad CID ORIG DEST (id de "intensidadFase") //no se usa (se declara el p.a. en otra sede, nada más) static function transaction_cambiar_pa_fase($data) { return self::transaction_cambiar_generic('fase', $data); } - + //no se usa (se declara el p.a. en otra sede, nada más) static function transaction_cambiar_pa_intensidad($data) { return self::transaction_cambiar_generic('intensidad', $data); } - + //------- - + static function transaction_extra_field_agregar_generic($extra_field_variable, $original_data, $web_service_details, $type='session') { $function_name = $extra_field_variable."Detalles"; $data = Migration::soap_call($web_service_details, $function_name, array('intIdSede'=> $original_data['branch_id'], "uidid".$extra_field_variable => $original_data['item_id'])); @@ -1247,7 +1249,7 @@ class MigrationCustom { $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($extra_field_variable); if ($extra_field_info) { $extra_field_option = new ExtraFieldOption($type); - + $info_before = $extra_field_option->get_field_options_by_field($extra_field_info['id']); $params = array( @@ -1258,9 +1260,9 @@ class MigrationCustom { ); $result = $extra_field_option->save_one_item($params); - + $info_after = $extra_field_option->get_field_options_by_field($extra_field_info['id']); - + if ($result) { return array( 'entity' => $extra_field_variable, @@ -1274,7 +1276,7 @@ class MigrationCustom { 'message' => "Extra field option added - $extra_field_variable was NOT saved with data: ".print_r($params,1), 'status_id' => self::TRANSACTION_STATUS_FAILED ); - } + } } else { return array( 'message' => "Extra field was not found: $extra_field_variable", @@ -1284,8 +1286,8 @@ class MigrationCustom { } else { return $data; } - } - + } + static function transaction_extra_field_editar_generic($extra_field_variable, $original_data, $web_service_details, $type='session') { $extra_field = new ExtraField($type); $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($extra_field_variable); @@ -1295,30 +1297,30 @@ class MigrationCustom { 'status_id' => self::TRANSACTION_STATUS_FAILED ); } - - $extra_field_option = new ExtraFieldOption($type); + + $extra_field_option = new ExtraFieldOption($type); $extra_field_option_info = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $original_data['item_id']); - + $function_name = $extra_field_variable."Detalles"; - + $params = array('intIdSede'=> $original_data['branch_id'], "uidid".$extra_field_variable => $original_data['item_id']); - - $data = Migration::soap_call($web_service_details, $function_name, $params); + + $data = Migration::soap_call($web_service_details, $function_name, $params); if ($data['error'] == false) { - + //Update 1 item if (!empty($extra_field_option_info)) { - + $info_before = $extra_field_option->get_field_options_by_field($extra_field_info['id']); - + if (count($extra_field_option_info) > 1) { //var_dump($extra_field_option_info); - //Take the first one + //Take the first one error_log('Warning! There are several options with the same key. You should delete doubles. Check your DB with this query:'); error_log("SELECT * FROM ".$type."_field_options WHERE field_id = {$extra_field_info['id']} AND option_value = '{$original_data['item_id']}' "); error_log('All options are going to be updated'); } - + $options_updated = array(); foreach ($extra_field_option_info as $option) { $extra_field_option_info = array( @@ -1327,16 +1329,16 @@ class MigrationCustom { 'option_value' => $original_data['item_id'], 'option_display_text' => $data['name'], 'option_order' => null - ); + ); $extra_field_option->update($extra_field_option_info); error_log('Editing extra field: '.print_r($extra_field_option_info,1)); $options_updated[] = $option['id']; } - + $info_after = $extra_field_option->get_field_options_by_field($extra_field_info['id']); $options_updated = implode(',', $options_updated); - + return array( 'entity' => $extra_field_variable, 'before' => $info_before, @@ -1352,21 +1354,21 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); } } else { return $data; - } + } } - - /* Delete all options with option_value = item_id */ - static function transaction_extra_field_eliminar_generic($extra_field_variable, $original_data, $web_service_details, $type='session') { //horario + + /* Delete all options with option_value = item_id */ + static function transaction_extra_field_eliminar_generic($extra_field_variable, $original_data, $web_service_details, $type='session') { //horario $extra_field = new ExtraField($type); $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($extra_field_variable); - - $extra_field_option = new ExtraFieldOption($type); + + $extra_field_option = new ExtraFieldOption($type); $extra_field_option_info = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $original_data['item_id']); - + if (!empty($extra_field_option_info)) { - + $info_before = $extra_field_option->get_field_options_by_field($extra_field_info['id']); - + $deleting_option_ids = array(); foreach ($extra_field_option_info as $option) { //@todo Delete all horario in sessions? @@ -1376,7 +1378,7 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); } } $info_after = $extra_field_option->get_field_options_by_field($extra_field_info['id']); - + if (!empty($deleting_option_ids)) { $deleting_option_ids = implode(',', $deleting_option_ids); return array( @@ -1392,7 +1394,7 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); 'status_id' => self::TRANSACTION_STATUS_FAILED ); } - } else { + } else { return array( 'message' => "Extra field option was NOT deleted - Extra field not found in DB. Trying to locate field_variable: '$extra_field_variable' with id: '{$original_data['item_id']}'", 'status_id' => self::TRANSACTION_STATUS_FAILED @@ -1400,33 +1402,33 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); } } - + // Horario - // añadir horario_agregar HID + // añadir horario_agregar HID // const TRANSACTION_TYPE_ADD_SCHED = 13; static function transaction_13($data, $web_service_details) { - return self::transaction_extra_field_agregar_generic('horario', $data, $web_service_details); + return self::transaction_extra_field_agregar_generic('horario', $data, $web_service_details); } - + // eliminar horario_eliminar HID // const TRANSACTION_TYPE_DEL_SCHED = 14; static function transaction_14($data, $web_service_details) { return self::transaction_extra_field_eliminar_generic('horario', $data, $web_service_details); } - + // editar horario_editar HID // const TRANSACTION_TYPE_EDIT_SCHED = 15; static function transaction_15($data, $web_service_details) { return self::transaction_extra_field_editar_generic('horario', $data, $web_service_details); } - + // Aula // añadir aula_agregar AID // const TRANSACTION_TYPE_ADD_ROOM = 16; static function transaction_16($data, $web_service_details) { return self::transaction_extra_field_agregar_generic('aula', $data, $web_service_details); } - + // eliminar aula_eliminar AID // const TRANSACTION_TYPE_DEL_ROOM = 17; static function transaction_17($data, $web_service_details) { @@ -1484,7 +1486,7 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); static function transaction_26($data, $web_service_details) { return self::transaction_extra_field_eliminar_generic('intensidad', $data, $web_service_details, 'course'); } - + // editar intfase_editar IID // const TRANSACTION_TYPE_EDIT_INTENS = 27; static function transaction_27($data, $web_service_details) { @@ -1496,42 +1498,42 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); static function transaction_28($data, $web_service_details) { return self::transaction_extra_field_agregar_generic('fase', $data, $web_service_details, 'course'); } - + // eliminar fase_eliminar IID // const TRANSACTION_TYPE_DEL_FASE = 29; static function transaction_29($data, $web_service_details) { return self::transaction_extra_field_eliminar_generic('fase', $data, $web_service_details, 'course'); } - + // editar fase_editar IID // const TRANSACTION_TYPE_EDIT_FASE = 30; static function transaction_30($data, $web_service_details) { return self::transaction_extra_field_editar_generic('fase', $data, $web_service_details, 'course'); } - + //custom class moved here - - static function transacciones($data) { + + static function transacciones($data) { if ($data) { - $xml = $data->transaccionesResult->any; + $xml = $data->transaccionesResult->any; // Cut the invalid XML and extract the valid chunk with the data $stripped_xml = strstr($xml, 'NewDataSet)) { + if (!empty($xml->NewDataSet)) { $transaction_list = array(); foreach ($xml->NewDataSet->Table as $item) { //this is a "Table" object - $item = (array) $item; + $item = (array) $item; $transaction_list[] = $item; } return $transaction_list; } else { error_log('No transactions found'); - } + } } else { error_log('Data is not valid'); } } - + /* object(SimpleXMLElement)#11 (5) { ["idt"]=> string(6) "354913" @@ -1546,19 +1548,19 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); } */ static function process_transactions($params, $web_service_details) { - $transactions = Migration::soap_call($web_service_details, 'transacciones', $params); + $transactions = Migration::soap_call($web_service_details, 'transacciones', $params); $transaction_status_list = self::get_transaction_status_list(); - - if (isset($transactions) && isset($transactions['error']) && $transactions['error'] == true) { + + if (isset($transactions) && isset($transactions['error']) && $transactions['error'] == true) { error_log($transactions['message']); } else { $counter = count($transactions); error_log("Processing ".$counter." transaction(s)"); $count = 1; if (!empty($transactions)) { - foreach ($transactions as $transaction_info) { + foreach ($transactions as $transaction_info) { //Add transactions here - error_log("Processing transaction: ".$count); + error_log("Processing transaction: ".$count); $result = self::process_transaction($transaction_info, $transaction_status_list); $count++; if ($result['error'] == true) { @@ -1572,11 +1574,11 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); } } } - + static function validate_transaction($transaction_info) { if (empty($transaction_info) || empty($transaction_info['transaction_id']) || empty($transaction_info['action']) || empty($transaction_info['branch_id'])) { return array( - 'id' => null, + 'id' => null, 'error' => true, 'message' => "Transaction could not be added there are some missing params: ".print_r($transaction_info, 1) ); @@ -1584,42 +1586,42 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); return true; } } - + /** - * + * * @param array simple return of the webservice transaction * @return int */ static function process_transaction($transaction_info, $transaction_status_list = array(), $forced = false) { - if ($transaction_info) { + if ($transaction_info) { if (empty($transaction_status_list)) { $transaction_status_list = self::get_transaction_status_list(); } - + $params = array( - 'transaction_id' => isset($transaction_info['idt']) ? $transaction_info['idt'] : null, - 'action' => isset($transaction_info['ida']) ? $transaction_info['ida'] : null, + 'transaction_id' => isset($transaction_info['idt']) ? $transaction_info['idt'] : null, + 'action' => isset($transaction_info['ida']) ? $transaction_info['ida'] : null, 'item_id' => isset($transaction_info['id']) ? strtoupper($transaction_info['id']) : null, 'orig_id' => isset($transaction_info['orig']) ? $transaction_info['orig'] : null, 'branch_id' => isset($transaction_info['idsede']) ? $transaction_info['idsede'] : null, 'dest_id' => isset($transaction_info['dest']) ? $transaction_info['dest'] : null, 'status_id' => 0 ); - + $validate = self::validate_transaction($params); - + if (isset($validate['error']) && $validate['error']) { return $validate; - } - + } + if ($forced) { //Delete transaction - Migration::delete_transaction_by_transaction_id($params['transaction_id'], $params['branch_id']); + Migration::delete_transaction_by_transaction_id($params['transaction_id'], $params['branch_id']); } - + //what to do if transaction already exists? $transaction_info = Migration::get_transaction_by_transaction_id($params['transaction_id'], $params['branch_id']); - + if (empty($transaction_info)) { $transaction_id = Migration::add_transaction($params); if ($transaction_id) { @@ -1636,21 +1638,21 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); ); } } else { - //only process transaction if it was failed or to be executed or is 0 registered - if (in_array($transaction_info['status_id'], array(0, MigrationCustom::TRANSACTION_STATUS_FAILED, MigrationCustom::TRANSACTION_STATUS_TO_BE_EXECUTED))) { + //only process transaction if it was failed or to be executed or is 0 registered + if (in_array($transaction_info['status_id'], array(0, MigrationCustom::TRANSACTION_STATUS_FAILED, MigrationCustom::TRANSACTION_STATUS_TO_BE_EXECUTED))) { return array( - 'id' => $transaction_info['id'], + 'id' => $transaction_info['id'], 'error' => false, 'message' => "Third party transaction id #{$params['transaction_id']} was already added to Chamilo with id #{$transaction_info['id']}. Trying to execute because transaction has status: {$transaction_status_list[$transaction_info['status_id']]['title']}" ); } else { return array( - 'id' => null, + 'id' => null, 'error' => true, 'message' => "Third party transaction id #{$params['transaction_id']} was already added to Chamilo with id #{$transaction_info['id']}. Transaction can't be executed twice. Transacion status_id = {$transaction_status_list[$transaction_info['status_id']]['title']}" ); } - } + } return array( 'id' => null, 'error' => true, @@ -1664,31 +1666,31 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); ); } } - + static function genericDetalles($data, $result_name, $params = array()) { error_log("Calling $result_name "); $original_webservice_name = $result_name; - + $result_name = $result_name.'Result'; $xml = $data->$result_name->any; - + // Cut the invalid XML and extract the valid chunk with the data - $stripped_xml = strstr($xml, 'NewDataSet)) { - $item = (array)$xml->NewDataSet->Table; + $item = (array)$xml->NewDataSet->Table; $item['error'] = false; if (isset($item['uididsede'])) { $item['uididsede'] = strtoupper($item['uididsede']); } - + if (isset($item['uididhorario'])) { $item['uididhorario'] = strtoupper($item['uididhorario']); } - - return $item; - } else { + + return $item; + } else { return array( 'error' => true, 'message' => "No data when calling web service *$original_webservice_name* Result:".print_r($data, 1)." with params: ".print_r($params, 1), @@ -1696,7 +1698,7 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); ); } } - + /* Returns an obj with this params object(SimpleXMLElement)#11 (7) { ["rol"]=> @@ -1721,15 +1723,15 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); if ($result['error'] == true) { return $result; } - - $result['status'] = $result['rol'] == 'profesor' ? COURSEMANAGER : STUDENT; + + $result['status'] = $result['rol'] == 'profesor' ? COURSEMANAGER : STUDENT; $result['phone'] = (string)$result['phone']; $result['active'] = (int)$result['bitvigencia']; $result['extra_uidIdPersona'] = strtoupper($params['uididpersona']); unset($result['rol']); return $result; } - + /* ["uididsede"]=> string(36) "7379a7d3-6dc5-42ca-9ed4-97367519f1d9" @@ -1745,32 +1747,32 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); string(25) "2009-09-30T00:00:00-05:00" ["access_end_date"]=> string(25) "2009-10-26T00:00:00-05:00" - + ["course_code"]=> string(36) "5b7e9b5a-5145-4a42-be48-223a70d9ad52" ["id_coach"]=> string(36) "26dbb1c1-32b7-4cf8-a81f-43d1cb231abe" - * + * */ - static function programaDetalles($data, $params) { + static function programaDetalles($data, $params) { $result = self::genericDetalles($data, __FUNCTION__, $params); if ($result['error'] == true) { return $result; } - + //Searching course code $course_code = MigrationCustom::get_real_course_code($result['course_code']); $result['course_code'] = $course_code; - + $course_info = api_get_course_info($course_code); - + //Getting sede $extra_field = new ExtraField('session'); $extra_field_option = new ExtraFieldOption('session'); - - $extra_field_info = $extra_field->get_handler_field_info_by_field_variable('sede'); + + $extra_field_info = $extra_field->get_handler_field_info_by_field_variable('sede'); $extra_field_option_info_sede = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $result['uididsede']); - + $sede_name = null; if (isset($extra_field_option_info_sede[0]) && !empty($extra_field_option_info_sede[0]['option_display_text'])) { $sede_name = $extra_field_option_info_sede[0]['option_display_text']; @@ -1778,47 +1780,47 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); $extra_field_info = $extra_field->get_handler_field_info_by_field_variable('aula'); $extra_field_option_info_aula = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $result['uididaula']); - + $aula_name = null; if (isset($extra_field_option_info_aula[0]) && !empty($extra_field_option_info_sede[0]['option_display_text'])) { $aula_name = $extra_field_option_info_aula[0]['option_display_text']; } - + //Getting horario - $extra_field_info = $extra_field->get_handler_field_info_by_field_variable('horario'); + $extra_field_info = $extra_field->get_handler_field_info_by_field_variable('horario'); $extra_field_option_info_horario = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $result['uididhorario']); - + $horario_name = null; if (isset($extra_field_option_info_horario[0]) && !empty($extra_field_option_info_horario[0]['option_display_text'])) { $horario_name = $extra_field_option_info_horario[0]['option_display_text']; - } - + } + //Setting the session name $result['name'] = substr($sede_name,13).' - '.$result['chrperiodo']." - ".$course_info['title'].' '.$horario_name.' '.$aula_name; - + $result['extra_uidIdPrograma'] = strtoupper($params['uididprograma']); $result['extra_horario'] = strtoupper($result['uididhorario']); $result['extra_sede'] = strtoupper($result['uididsede']); $result['extra_aula'] = strtoupper($result['uididaula']); $result['extra_periodo'] = strtoupper($result['chrperiodo']); - + $result['display_start_date'] = MigrationCustom::clean_date_time_from_ws($result['display_start_date']); $result['display_end_date'] = MigrationCustom::clean_date_time_from_ws($result['display_end_date']); $result['access_start_date'] = MigrationCustom::clean_date_time_from_ws($result['access_start_date']); $result['access_end_date'] = MigrationCustom::clean_date_time_from_ws($result['access_end_date']); //$result['estado'] = intval($result['estado']); - + //Searching id_coach $result['id_coach'] = MigrationCustom::get_user_id_by_persona_id($result['id_coach']); - + unset($result['uididprograma']); unset($result['uididsede']); unset($result['uididhorario']); unset($result['chrperiodo']); - + return $result; } - + /** ["name"]=> string(42) "(A02SA) Pronunciacion Two Sabado Acelerado" @@ -1831,29 +1833,29 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); ["meses"]=> string(3) "2 " */ - static function cursoDetalles($data, $params) { + static function cursoDetalles($data, $params) { $result = self::genericDetalles($data, __FUNCTION__, $params); if ($result['error'] == true) { return $result; } - + $result['title'] = $result['name']; $result['extra_frecuencia'] = strtoupper($result['frecuencia']); $result['extra_intensidad'] = strtoupper($result['intensidad']); $result['extra_fase'] = strtoupper($result['fase']); $result['extra_meses'] = strtoupper($result['meses']); $result['extra_uidIdCurso'] = strtoupper($params['uididcurso']); - + unset($result['frecuencia']); unset($result['intensidad']); unset($result['fase']); unset($result['name']); unset($result['meses']); - + return $result; } - - /*Calling frecuenciaDetalles + + /*Calling frecuenciaDetalles array(1) { ["name"]=> string(8) "Sabatino" @@ -1862,19 +1864,19 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); $result = self::genericDetalles($data, __FUNCTION__, $params); if ($result['error'] == true) { return $result; - } - return $result; + } + return $result; } - + static function frecuenciaDetalles($data, $params) { - $result = self::genericDetalles($data, __FUNCTION__, $params); + $result = self::genericDetalles($data, __FUNCTION__, $params); if ($result['error'] == true) { return $result; - } + } return $result; } - - /*Calling intensidadDetalles + + /*Calling intensidadDetalles array(1) { ["name"]=> string(6) "Normal" @@ -1884,13 +1886,13 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); $result = self::genericDetalles($data, __FUNCTION__, $params); if ($result['error'] == true) { return $result; - } + } //$result['option_value'] = $params['uididintensidad']; return $result; } - + /*Calling mesesDetalles - Calling mesesDetalles + Calling mesesDetalles array(1) { ["name"]=> string(3) "4 " @@ -1899,18 +1901,18 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); $result = self::genericDetalles($data, __FUNCTION__, $params); if ($result['error'] == true) { return $result; - } + } return $result; } - + static function aulaDetalles($data, $params) { $result = self::genericDetalles($data, __FUNCTION__, $params); if ($result['error'] == true) { return $result; - } - return $result; + } + return $result; } - /*Calling sedeDetalles + /*Calling sedeDetalles array(1) { ["name"]=> string(23) "Sede Miraflores" @@ -1920,12 +1922,12 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); $result = self::genericDetalles($data, __FUNCTION__, $params); if ($result['error'] == true) { return $result; - } + } return $result; } - + /* - Calling horarioDetalles + Calling horarioDetalles array(3) { ["start"]=> string(5) "08:45" @@ -1934,14 +1936,14 @@ error_log('Editing extra field: '.print_r($extra_field_option_info,1)); ["id"]=> string(2) "62" }*/ - static function horarioDetalles($data, $params) { - $result = self::genericDetalles($data, __FUNCTION__, $params); + static function horarioDetalles($data, $params) { + $result = self::genericDetalles($data, __FUNCTION__, $params); if ($result['error'] == true) { return $result; } - + $result['name'] = $result['id'].' '.$result['start'].' '.$result['end']; - //$result['option_value'] = $params['uididhorario']; + //$result['option_value'] = $params['uididhorario']; unset($result['id']); unset($result['start']); unset($result['end']); diff --git a/tests/migrate/migration.mssql.class.php b/tests/migrate/migration.mssql.class.php index 1e3af7ee65..bdbcbc1d75 100644 --- a/tests/migrate/migration.mssql.class.php +++ b/tests/migrate/migration.mssql.class.php @@ -35,10 +35,13 @@ class MigrationMSSQL extends Migration { $top = null; // $top = " TOP 100000 "; if (in_array($table, array('Empleado', 'Alumno'))) { -// $top = " TOP 2000 "; + //$top = " TOP 2000 "; } - - if (in_array($table, array('ProgramaAcademico', 'Matricula'))) { + + if ($table == 'ProgramaAcademico') { + //$top = ' TOP 1000 '; + } + if ($table == 'Matricula') { //$top = " TOP 1000 "; }