From 4f89e2a3c42917224afeaeacce7da00d2ed74ce9 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 2 Dec 2020 13:16:10 +0100 Subject: [PATCH] Course copy: Add page to move users from a base course to a session BT#18119 --- main/admin/index.php | 6 + main/admin/user_move_stats.php | 14 +- .../move_users_from_course_to_session.php | 132 ++++++++++++++++++ main/inc/lib/tracking.lib.php | 84 ++++++----- main/lang/english/trad4all.inc.php | 1 + main/lang/french/trad4all.inc.php | 1 + main/lang/spanish/trad4all.inc.php | 1 + 7 files changed, 200 insertions(+), 39 deletions(-) create mode 100644 main/coursecopy/move_users_from_course_to_session.php diff --git a/main/admin/index.php b/main/admin/index.php index 7eb11a0808..415bfca722 100644 --- a/main/admin/index.php +++ b/main/admin/index.php @@ -523,6 +523,12 @@ if (api_is_platform_admin() || ($allowCareer && api_is_session_admin())) { 'label' => get_lang('MoveUserStats'), ]; } + + $items[] = [ + 'url' => '../coursecopy/move_users_from_course_to_session.php', + 'label' => get_lang('MoveUsersFromCourseToSession'), + ]; + $items[] = [ 'url' => 'career_dashboard.php', 'label' => get_lang('CareersAndPromotions'), diff --git a/main/admin/user_move_stats.php b/main/admin/user_move_stats.php index fc85582253..40b8af3d24 100755 --- a/main/admin/user_move_stats.php +++ b/main/admin/user_move_stats.php @@ -52,17 +52,18 @@ if (isset($_REQUEST['load_ajax'])) { $update_database = false; } - //Check if the same course exist in the session destination + // Check if the same course exist in the session destination if ($course_founded) { $result = SessionManager::get_users_by_session($new_session_id); if (empty($result) || !in_array($user_id, array_keys($result))) { if ($debug) { echo 'User added to the session'; } - //Registering user to the new session + // Registering user to the new session SessionManager::subscribeUsersToSession( $new_session_id, [$user_id], + false, false ); } @@ -116,14 +117,12 @@ $htmlHeadXtra[] = ''; + '; function get_courses_list_by_user_id_based_in_exercises($user_id) { $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); - $user_id = intval($user_id); + $user_id = (int) $user_id; $sql = "SELECT DISTINCT exe_user_id, c_id, session_id FROM $TABLETRACK_EXERCICES WHERE exe_user_id = $user_id @@ -147,7 +146,8 @@ Display::addFlash( ); Display::display_header(get_lang('MoveUserStats')); echo '
'; -echo ''.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM).''; +echo ''. + Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM).''; echo '
'; // Some pagination diff --git a/main/coursecopy/move_users_from_course_to_session.php b/main/coursecopy/move_users_from_course_to_session.php new file mode 100644 index 0000000000..e06fff84a5 --- /dev/null +++ b/main/coursecopy/move_users_from_course_to_session.php @@ -0,0 +1,132 @@ + Lots of bug fixes/improvements + */ +$cidReset = true; +require_once __DIR__.'/../inc/global.inc.php'; +require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php'; +$current_course_tool = TOOL_COURSE_MAINTENANCE; + +api_protect_admin_script(); +api_protect_limit_for_session_admin(); +api_set_more_memory_and_time_limits(); + +$courseId = isset($_REQUEST['course_id']) ? (int) $_REQUEST['course_id'] : 0; +$sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : 0; + +$courseOptions = []; +$defaults = []; +$courseInfo = []; +if (!empty($courseId)) { + $defaults['course_id'] = $defaults; + $courseInfo = api_get_course_info_by_id($courseId); + $courseOptions[$courseId] = $courseInfo['name']; +} +Display::display_header(); +$form = new FormValidator('course', 'GET', api_get_self()); +$form->addHeader(get_lang('MoveUsersFromCourseToSession')); +$form->addSelectAjax( + 'course_id', + get_lang('Course'), + $courseOptions, + [ + 'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course', + ] +); +$form->addButtonSearch(get_lang('Search')); +$content = ''; +if ($form->validate()) { + $values = $form->getSubmitValues(); + if (!empty($courseId)) { + $sessions = SessionManager::get_session_by_course($courseId); + if (!empty($sessions)) { + $sessions = array_column($sessions, 'name', 'id'); + $form->addSelect( + 'session_id', + get_lang('Session'), + $sessions + ); + + $form->addButtonSearch(get_lang('CompareStats'), 'compare'); + $form->addButtonCopy(get_lang('Move'), 'move'); + } + } + + $count = CourseManager::get_user_list_from_course_code($courseInfo['code'], 0, null, null, STUDENT, true); + $students = []; + if (isset($values['compare']) || isset($values['move'])) { + $default = 20; + $page = 1; + if (isset($_GET['page']) && !empty($_GET['page'])) { + $page = (int) $_GET['page']; + } + $nro_pages = round($count / $default) + 1; + $begin = $default * ($page - 1); + $end = $default * $page; + + if ($count > $default) { + $navigation = "$begin - $end / $count
"; + if ($page > 1) { + $navigation .= ''.get_lang('Previous').''; + } else { + $navigation .= get_lang('Previous'); + } + $navigation .= ' '; + $page++; + if ($page < $nro_pages) { + $navigation .= ''.get_lang('Next').''; + } else { + $navigation .= get_lang('Next'); + } + + $content .= $navigation; + } + + $limit = "LIMIT $begin, $default"; + $students = CourseManager::get_user_list_from_course_code($courseInfo['code'], 0, $limit, null, STUDENT); + foreach ($students as $student) { + $studentId = $student['user_id']; + $name = $student['firstname'].' '.$student['lastname']; + $content .= "

$name #$studentId

"; + + $subscribed = SessionManager::isUserSubscribedAsStudent($sessionId, $studentId); + + if ($subscribed) { + $content .= Display::return_message(get_lang('AlreadySubscribed')); + continue; + } + + if (isset($values['move'])) { + // Registering user to the new session. + SessionManager::subscribeUsersToSession( + $sessionId, + [$studentId], + false, + false + ); + } + + ob_start(); + Tracking::processUserDataMove( + $studentId, + $courseInfo, + 0, + $sessionId, + isset($values['move']) + ); + $tableResult = ob_get_contents(); + ob_get_clean(); + $content .= $tableResult; + } + } +} + +$form->display(); +echo $content; +Display::display_footer(); diff --git a/main/inc/lib/tracking.lib.php b/main/inc/lib/tracking.lib.php index 3c00bda830..b8a73ee428 100755 --- a/main/inc/lib/tracking.lib.php +++ b/main/inc/lib/tracking.lib.php @@ -7056,23 +7056,28 @@ class Tracking return (int) $result['c']; } - public static function processUserDataMove($user_id, $course_info, $origin_session_id, $new_session_id, $update_database, $debug = false) - { + public static function processUserDataMove( + $user_id, + $course_info, + $origin_session_id, + $new_session_id, + $update_database, + $debug = false + ) { // Begin with the import process $origin_course_code = $course_info['code']; $course_id = $course_info['real_id']; $user_id = (int) $user_id; $origin_session_id = (int) $origin_session_id; $new_session_id = (int) $new_session_id; - $session = api_get_session_entity($new_session_id); $em = Database::getManager(); $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); + $attemptRecording = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING); $TBL_TRACK_E_COURSE_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); $TBL_TRACK_E_LAST_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); - $TBL_LP_VIEW = Database::get_course_table(TABLE_LP_VIEW); $TBL_NOTEBOOK = Database::get_course_table(TABLE_NOTEBOOK); $TBL_STUDENT_PUBLICATION = Database::get_course_table(TABLE_STUDENT_PUBLICATION); @@ -7094,12 +7099,23 @@ class Tracking } $result_message = []; + $result_message_compare = []; if (!empty($list)) { foreach ($list as $exe_id => $data) { if ($update_database) { $sql = "UPDATE $TABLETRACK_EXERCICES SET session_id = '$new_session_id' WHERE exe_id = $exe_id"; Database::query($sql); - $result_message['TABLETRACK_EXERCICES']++; + + $sql = "UPDATE $TBL_TRACK_ATTEMPT SET session_id = '$new_session_id' WHERE exe_id = $exe_id"; + Database::query($sql); + + $sql = "UPDATE $attemptRecording SET session_id = '$new_session_id' WHERE exe_id = $exe_id"; + Database::query($sql); + + if (!isset($result_message[$TABLETRACK_EXERCICES])) { + $result_message[$TABLETRACK_EXERCICES] = 0; + } + $result_message[$TABLETRACK_EXERCICES]++; } else { if (!empty($data['orig_lp_id']) && !empty($data['orig_lp_item_id'])) { $result_message['TRACK_E_EXERCISES'][$exe_id] = $data; @@ -7146,7 +7162,7 @@ class Tracking // Nothing to do because there are not relationship with a session // 3. track_e_course_access $sql = "SELECT * FROM $TBL_TRACK_E_COURSE_ACCESS - WHERE c_id = $course_id AND session_id = $origin_session_id AND user_id = $user_id "; + WHERE c_id = $course_id AND session_id = $origin_session_id AND user_id = $user_id "; $res = Database::query($sql); $list = []; while ($row = Database::fetch_array($res, 'ASSOC')) { @@ -7163,12 +7179,15 @@ class Tracking echo $sql; } Database::query($sql); + if (!isset($result_message[$TBL_TRACK_E_COURSE_ACCESS])) { + $result_message[$TBL_TRACK_E_COURSE_ACCESS] = 0; + } $result_message[$TBL_TRACK_E_COURSE_ACCESS]++; } } } - //4. track_e_lastaccess + // 4. track_e_lastaccess $sql = "SELECT access_id FROM $TBL_TRACK_E_LAST_ACCESS WHERE c_id = $course_id AND @@ -7191,6 +7210,9 @@ class Tracking } Database::query($sql); //if ($debug) var_dump($res); + if (!isset($result_message[$TBL_TRACK_E_LAST_ACCESS])) { + $result_message[$TBL_TRACK_E_LAST_ACCESS] = 0; + } $result_message[$TBL_TRACK_E_LAST_ACCESS]++; } } @@ -7205,7 +7227,6 @@ class Tracking // Getting the list of LPs in the new session $lp_list = new LearnpathList($user_id, $course_info, $new_session_id); $flat_list = $lp_list->get_flat_list(); - $list = []; while ($row = Database::fetch_array($res, 'ASSOC')) { // Checking if the LP exist in the new session @@ -7218,8 +7239,8 @@ class Tracking foreach ($list as $id => $data) { if ($update_database) { $sql = "UPDATE $TBL_LP_VIEW - SET session_id = $new_session_id - WHERE c_id = $course_id AND id = $id "; + SET session_id = $new_session_id + WHERE c_id = $course_id AND id = $id "; if ($debug) { var_dump($sql); } @@ -7229,14 +7250,14 @@ class Tracking } $result_message[$TBL_LP_VIEW]++; } else { - //Getting all information of that lp_item_id - $score = Tracking::get_avg_student_score( + // Getting all information of that lp_item_id + $score = self::get_avg_student_score( $user_id, $origin_course_code, [$data['lp_id']], $origin_session_id ); - $progress = Tracking::get_avg_student_progress( + $progress = self::get_avg_student_progress( $user_id, $origin_course_code, [$data['lp_id']], @@ -7269,14 +7290,14 @@ class Tracking if (!empty($list)) { foreach ($list as $id => $data) { - //Getting all information of that lp_item_id - $score = Tracking::get_avg_student_score( + // Getting all information of that lp_item_id + $score = self::get_avg_student_score( $user_id, $origin_course_code, [$data['lp_id']], $new_session_id ); - $progress = Tracking::get_avg_student_progress( + $progress = self::get_avg_student_progress( $user_id, $origin_course_code, [$data['lp_id']], @@ -7290,8 +7311,8 @@ class Tracking } } - //6. Agenda - //calendar_event_attachment no problems no session_id + // 6. Agenda + // calendar_event_attachment no problems no session_id $sql = "SELECT ref FROM $TBL_ITEM_PROPERTY WHERE tool = 'calendar_event' AND insert_user_id = $user_id AND c_id = $course_id "; $res = Database::query($sql); @@ -7310,8 +7331,8 @@ class Tracking } } - //7. Forum ?? So much problems when trying to import data - //8. Student publication - Works + // 7. Forum ?? So much problems when trying to import data + // 8. Student publication - Works $sql = "SELECT ref FROM $TBL_ITEM_PROPERTY WHERE tool = 'work' AND insert_user_id = $user_id AND c_id = $course_id"; if ($debug) { @@ -7420,10 +7441,10 @@ class Tracking Database::query($sql_add_publication); $id = Database::insert_id(); - $sql_update = "UPDATE ".$TBL_STUDENT_PUBLICATION." SET ". - "has_properties = '".$id."', - view_properties = '1' - WHERE id = ".$new_parent_id; + $sql_update = "UPDATE $TBL_STUDENT_PUBLICATION + SET has_properties = '".$id."', + view_properties = '1' + WHERE id = ".$new_parent_id; if ($debug) { echo $sql_update; } @@ -7439,7 +7460,7 @@ class Tracking $new_url = str_replace($parent_data['url'], $created_dir, $doc_url); if ($update_database) { - //Creating a new work + // Creating a new work $data['sent_date'] = new DateTime($data['sent_date'], new DateTimeZone('UTC')); $publication = new \Chamilo\CourseBundle\Entity\CStudentPublication(); @@ -7548,32 +7569,31 @@ class Tracking }*/ if ($update_database) { - echo '

'.get_lang('StatsMoved').'

'; + echo Display::return_message(get_lang('StatsMoved')); if (is_array($result_message)) { foreach ($result_message as $table => $times) { echo 'Table '.$table.' - '.$times.' records updated
'; } } } else { - echo '

'.get_lang('UserInformationOfThisCourse').'

'; - + echo '

'.get_lang('UserInformationOfThisCourse').'

'; echo '
'; echo ''; echo ''; echo ''; echo ''; diff --git a/main/lang/english/trad4all.inc.php b/main/lang/english/trad4all.inc.php index c12bafcc01..4c10a69f8c 100644 --- a/main/lang/english/trad4all.inc.php +++ b/main/lang/english/trad4all.inc.php @@ -8713,4 +8713,5 @@ $HideCategoryTable = "Hide category table"; $XQuestionsSelectedWithTotalScoreY = "Only %d questions will be selected based on the test configuration, for a total score of %s."; $QuestionAlsoUsedInTheFollowingTests = "This question is also used in the following tests"; $LinkToTestEdition = "Link to test edition"; +$MoveUsersFromCourseToSession = "Move users from base course to a session"; ?> \ No newline at end of file diff --git a/main/lang/french/trad4all.inc.php b/main/lang/french/trad4all.inc.php index 2359c2a44d..c17966d1ec 100644 --- a/main/lang/french/trad4all.inc.php +++ b/main/lang/french/trad4all.inc.php @@ -8645,4 +8645,5 @@ $HideCategoryTable = "Cacher le tableau de catégories"; $XQuestionsSelectedWithTotalScoreY = "Seules %d questions seront sélectionnées d'après la configuration de l'exercice, pour un score total de %s."; $QuestionAlsoUsedInTheFollowingTests = "Question utilisée également dans les exercices suivants"; $LinkToTestEdition = "Lien vers l'édition de l'exercice"; +$MoveUsersFromCourseToSession = "Déplacer les résultats utilisateurs dans/vers une session"; ?> \ No newline at end of file diff --git a/main/lang/spanish/trad4all.inc.php b/main/lang/spanish/trad4all.inc.php index 17730312b4..e1f03fc838 100644 --- a/main/lang/spanish/trad4all.inc.php +++ b/main/lang/spanish/trad4all.inc.php @@ -8741,4 +8741,5 @@ $HideCategoryTable = "Esconder la tabla de categorías"; $XQuestionsSelectedWithTotalScoreY = "Solo se seleccionarán %d preguntas según la configuración del ejercicio, para una puntuación total de %s."; $QuestionAlsoUsedInTheFollowingTests = "Esta pregunta se usa también en los ejercicios siguientes"; $LinkToTestEdition = "Vínculo a la edición del ejercicio"; +$MoveUsersFromCourseToSession = "Mover usuarios del curso base a una sesión"; ?> \ No newline at end of file
'; if ($origin_session_id == 0) { - echo '

'.get_lang('OriginCourse').'

'; + echo '
'.get_lang('OriginCourse').'
'; } else { - echo '

'.get_lang('OriginSession').' #'.$origin_session_id.'

'; + echo '
'.get_lang('OriginSession').' #'.$origin_session_id.'
'; } self::compareUserData($result_message); echo '
'; if ($new_session_id == 0) { - echo '

'.get_lang('DestinyCourse').'

'; + echo '
'.get_lang('DestinyCourse').'
'; } else { - echo '

'.get_lang('DestinySession').' #'.$new_session_id.'

'; + echo '
'.get_lang('DestinySession').' #'.$new_session_id.'
'; } self::compareUserData($result_message_compare); echo '