Course copy: Add page to move users from a base course to a session

BT#18119
pull/3694/head
Julio Montoya 5 years ago
parent 90f8a9f4ae
commit 4f89e2a3c4
  1. 6
      main/admin/index.php
  2. 14
      main/admin/user_move_stats.php
  3. 132
      main/coursecopy/move_users_from_course_to_session.php
  4. 84
      main/inc/lib/tracking.lib.php
  5. 1
      main/lang/english/trad4all.inc.php
  6. 1
      main/lang/french/trad4all.inc.php
  7. 1
      main/lang/spanish/trad4all.inc.php

@ -523,6 +523,12 @@ if (api_is_platform_admin() || ($allowCareer && api_is_session_admin())) {
'label' => get_lang('MoveUserStats'), 'label' => get_lang('MoveUserStats'),
]; ];
} }
$items[] = [
'url' => '../coursecopy/move_users_from_course_to_session.php',
'label' => get_lang('MoveUsersFromCourseToSession'),
];
$items[] = [ $items[] = [
'url' => 'career_dashboard.php', 'url' => 'career_dashboard.php',
'label' => get_lang('CareersAndPromotions'), 'label' => get_lang('CareersAndPromotions'),

@ -52,17 +52,18 @@ if (isset($_REQUEST['load_ajax'])) {
$update_database = false; $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) { if ($course_founded) {
$result = SessionManager::get_users_by_session($new_session_id); $result = SessionManager::get_users_by_session($new_session_id);
if (empty($result) || !in_array($user_id, array_keys($result))) { if (empty($result) || !in_array($user_id, array_keys($result))) {
if ($debug) { if ($debug) {
echo 'User added to the session'; echo 'User added to the session';
} }
//Registering user to the new session // Registering user to the new session
SessionManager::subscribeUsersToSession( SessionManager::subscribeUsersToSession(
$new_session_id, $new_session_id,
[$user_id], [$user_id],
false,
false false
); );
} }
@ -116,14 +117,12 @@ $htmlHeadXtra[] = '<script>
} }
}); });
} }
</script>';
</script>';
function get_courses_list_by_user_id_based_in_exercises($user_id) function get_courses_list_by_user_id_based_in_exercises($user_id)
{ {
$TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); $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 $sql = "SELECT DISTINCT exe_user_id, c_id, session_id
FROM $TABLETRACK_EXERCICES FROM $TABLETRACK_EXERCICES
WHERE exe_user_id = $user_id WHERE exe_user_id = $user_id
@ -147,7 +146,8 @@ Display::addFlash(
); );
Display::display_header(get_lang('MoveUserStats')); Display::display_header(get_lang('MoveUserStats'));
echo '<div class="actions">'; echo '<div class="actions">';
echo '<a href="../admin/index.php">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM).'</a>'; echo '<a href="../admin/index.php">'.
Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM).'</a>';
echo '</div>'; echo '</div>';
// Some pagination // Some pagination

@ -0,0 +1,132 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Copy resources from one course in a session to another one.
*
* @author Christian Fasanando
* @author Julio Montoya <gugli100@gmail.com> 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<br />";
if ($page > 1) {
$navigation .= '<a href="'.api_get_self().'?page='.($page - 1).'">'.get_lang('Previous').'</a>';
} else {
$navigation .= get_lang('Previous');
}
$navigation .= '&nbsp;';
$page++;
if ($page < $nro_pages) {
$navigation .= '<a href="'.api_get_self().'?page='.$page.'">'.get_lang('Next').'</a>';
} 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 .= "<h2>$name #$studentId </h2>";
$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();

@ -7056,23 +7056,28 @@ class Tracking
return (int) $result['c']; 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 // Begin with the import process
$origin_course_code = $course_info['code']; $origin_course_code = $course_info['code'];
$course_id = $course_info['real_id']; $course_id = $course_info['real_id'];
$user_id = (int) $user_id; $user_id = (int) $user_id;
$origin_session_id = (int) $origin_session_id; $origin_session_id = (int) $origin_session_id;
$new_session_id = (int) $new_session_id; $new_session_id = (int) $new_session_id;
$session = api_get_session_entity($new_session_id); $session = api_get_session_entity($new_session_id);
$em = Database::getManager(); $em = Database::getManager();
$TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
$TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); $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_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_TRACK_E_LAST_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
$TBL_LP_VIEW = Database::get_course_table(TABLE_LP_VIEW); $TBL_LP_VIEW = Database::get_course_table(TABLE_LP_VIEW);
$TBL_NOTEBOOK = Database::get_course_table(TABLE_NOTEBOOK); $TBL_NOTEBOOK = Database::get_course_table(TABLE_NOTEBOOK);
$TBL_STUDENT_PUBLICATION = Database::get_course_table(TABLE_STUDENT_PUBLICATION); $TBL_STUDENT_PUBLICATION = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
@ -7094,12 +7099,23 @@ class Tracking
} }
$result_message = []; $result_message = [];
$result_message_compare = [];
if (!empty($list)) { if (!empty($list)) {
foreach ($list as $exe_id => $data) { foreach ($list as $exe_id => $data) {
if ($update_database) { if ($update_database) {
$sql = "UPDATE $TABLETRACK_EXERCICES SET session_id = '$new_session_id' WHERE exe_id = $exe_id"; $sql = "UPDATE $TABLETRACK_EXERCICES SET session_id = '$new_session_id' WHERE exe_id = $exe_id";
Database::query($sql); 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 { } else {
if (!empty($data['orig_lp_id']) && !empty($data['orig_lp_item_id'])) { if (!empty($data['orig_lp_id']) && !empty($data['orig_lp_item_id'])) {
$result_message['TRACK_E_EXERCISES'][$exe_id] = $data; $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 // Nothing to do because there are not relationship with a session
// 3. track_e_course_access // 3. track_e_course_access
$sql = "SELECT * FROM $TBL_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); $res = Database::query($sql);
$list = []; $list = [];
while ($row = Database::fetch_array($res, 'ASSOC')) { while ($row = Database::fetch_array($res, 'ASSOC')) {
@ -7163,12 +7179,15 @@ class Tracking
echo $sql; echo $sql;
} }
Database::query($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]++; $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 $sql = "SELECT access_id FROM $TBL_TRACK_E_LAST_ACCESS
WHERE WHERE
c_id = $course_id AND c_id = $course_id AND
@ -7191,6 +7210,9 @@ class Tracking
} }
Database::query($sql); Database::query($sql);
//if ($debug) var_dump($res); //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]++; $result_message[$TBL_TRACK_E_LAST_ACCESS]++;
} }
} }
@ -7205,7 +7227,6 @@ class Tracking
// Getting the list of LPs in the new session // Getting the list of LPs in the new session
$lp_list = new LearnpathList($user_id, $course_info, $new_session_id); $lp_list = new LearnpathList($user_id, $course_info, $new_session_id);
$flat_list = $lp_list->get_flat_list(); $flat_list = $lp_list->get_flat_list();
$list = []; $list = [];
while ($row = Database::fetch_array($res, 'ASSOC')) { while ($row = Database::fetch_array($res, 'ASSOC')) {
// Checking if the LP exist in the new session // Checking if the LP exist in the new session
@ -7218,8 +7239,8 @@ class Tracking
foreach ($list as $id => $data) { foreach ($list as $id => $data) {
if ($update_database) { if ($update_database) {
$sql = "UPDATE $TBL_LP_VIEW $sql = "UPDATE $TBL_LP_VIEW
SET session_id = $new_session_id SET session_id = $new_session_id
WHERE c_id = $course_id AND id = $id "; WHERE c_id = $course_id AND id = $id ";
if ($debug) { if ($debug) {
var_dump($sql); var_dump($sql);
} }
@ -7229,14 +7250,14 @@ class Tracking
} }
$result_message[$TBL_LP_VIEW]++; $result_message[$TBL_LP_VIEW]++;
} else { } else {
//Getting all information of that lp_item_id // Getting all information of that lp_item_id
$score = Tracking::get_avg_student_score( $score = self::get_avg_student_score(
$user_id, $user_id,
$origin_course_code, $origin_course_code,
[$data['lp_id']], [$data['lp_id']],
$origin_session_id $origin_session_id
); );
$progress = Tracking::get_avg_student_progress( $progress = self::get_avg_student_progress(
$user_id, $user_id,
$origin_course_code, $origin_course_code,
[$data['lp_id']], [$data['lp_id']],
@ -7269,14 +7290,14 @@ class Tracking
if (!empty($list)) { if (!empty($list)) {
foreach ($list as $id => $data) { foreach ($list as $id => $data) {
//Getting all information of that lp_item_id // Getting all information of that lp_item_id
$score = Tracking::get_avg_student_score( $score = self::get_avg_student_score(
$user_id, $user_id,
$origin_course_code, $origin_course_code,
[$data['lp_id']], [$data['lp_id']],
$new_session_id $new_session_id
); );
$progress = Tracking::get_avg_student_progress( $progress = self::get_avg_student_progress(
$user_id, $user_id,
$origin_course_code, $origin_course_code,
[$data['lp_id']], [$data['lp_id']],
@ -7290,8 +7311,8 @@ class Tracking
} }
} }
//6. Agenda // 6. Agenda
//calendar_event_attachment no problems no session_id // calendar_event_attachment no problems no session_id
$sql = "SELECT ref FROM $TBL_ITEM_PROPERTY $sql = "SELECT ref FROM $TBL_ITEM_PROPERTY
WHERE tool = 'calendar_event' AND insert_user_id = $user_id AND c_id = $course_id "; WHERE tool = 'calendar_event' AND insert_user_id = $user_id AND c_id = $course_id ";
$res = Database::query($sql); $res = Database::query($sql);
@ -7310,8 +7331,8 @@ class Tracking
} }
} }
//7. Forum ?? So much problems when trying to import data // 7. Forum ?? So much problems when trying to import data
//8. Student publication - Works // 8. Student publication - Works
$sql = "SELECT ref FROM $TBL_ITEM_PROPERTY $sql = "SELECT ref FROM $TBL_ITEM_PROPERTY
WHERE tool = 'work' AND insert_user_id = $user_id AND c_id = $course_id"; WHERE tool = 'work' AND insert_user_id = $user_id AND c_id = $course_id";
if ($debug) { if ($debug) {
@ -7420,10 +7441,10 @@ class Tracking
Database::query($sql_add_publication); Database::query($sql_add_publication);
$id = Database::insert_id(); $id = Database::insert_id();
$sql_update = "UPDATE ".$TBL_STUDENT_PUBLICATION." SET ". $sql_update = "UPDATE $TBL_STUDENT_PUBLICATION
"has_properties = '".$id."', SET has_properties = '".$id."',
view_properties = '1' view_properties = '1'
WHERE id = ".$new_parent_id; WHERE id = ".$new_parent_id;
if ($debug) { if ($debug) {
echo $sql_update; echo $sql_update;
} }
@ -7439,7 +7460,7 @@ class Tracking
$new_url = str_replace($parent_data['url'], $created_dir, $doc_url); $new_url = str_replace($parent_data['url'], $created_dir, $doc_url);
if ($update_database) { if ($update_database) {
//Creating a new work // Creating a new work
$data['sent_date'] = new DateTime($data['sent_date'], new DateTimeZone('UTC')); $data['sent_date'] = new DateTime($data['sent_date'], new DateTimeZone('UTC'));
$publication = new \Chamilo\CourseBundle\Entity\CStudentPublication(); $publication = new \Chamilo\CourseBundle\Entity\CStudentPublication();
@ -7548,32 +7569,31 @@ class Tracking
}*/ }*/
if ($update_database) { if ($update_database) {
echo '<h2>'.get_lang('StatsMoved').'</h2>'; echo Display::return_message(get_lang('StatsMoved'));
if (is_array($result_message)) { if (is_array($result_message)) {
foreach ($result_message as $table => $times) { foreach ($result_message as $table => $times) {
echo 'Table '.$table.' - '.$times.' records updated <br />'; echo 'Table '.$table.' - '.$times.' records updated <br />';
} }
} }
} else { } else {
echo '<h2>'.get_lang('UserInformationOfThisCourse').'</h2>'; echo '<h4>'.get_lang('UserInformationOfThisCourse').'</h4>';
echo '<br />'; echo '<br />';
echo '<table class="table" width="100%">'; echo '<table class="table" width="100%">';
echo '<tr>'; echo '<tr>';
echo '<td width="50%" valign="top">'; echo '<td width="50%" valign="top">';
if ($origin_session_id == 0) { if ($origin_session_id == 0) {
echo '<h4>'.get_lang('OriginCourse').'</h4>'; echo '<h5>'.get_lang('OriginCourse').'</h5>';
} else { } else {
echo '<h4>'.get_lang('OriginSession').' #'.$origin_session_id.'</h4>'; echo '<h5>'.get_lang('OriginSession').' #'.$origin_session_id.'</h5>';
} }
self::compareUserData($result_message); self::compareUserData($result_message);
echo '</td>'; echo '</td>';
echo '<td width="50%" valign="top">'; echo '<td width="50%" valign="top">';
if ($new_session_id == 0) { if ($new_session_id == 0) {
echo '<h4>'.get_lang('DestinyCourse').'</h4>'; echo '<h5>'.get_lang('DestinyCourse').'</h5>';
} else { } else {
echo '<h4>'.get_lang('DestinySession').' #'.$new_session_id.'</h4>'; echo '<h5>'.get_lang('DestinySession').' #'.$new_session_id.'</h5>';
} }
self::compareUserData($result_message_compare); self::compareUserData($result_message_compare);
echo '</td>'; echo '</td>';

@ -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."; $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"; $QuestionAlsoUsedInTheFollowingTests = "This question is also used in the following tests";
$LinkToTestEdition = "Link to test edition"; $LinkToTestEdition = "Link to test edition";
$MoveUsersFromCourseToSession = "Move users from base course to a session";
?> ?>

@ -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."; $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"; $QuestionAlsoUsedInTheFollowingTests = "Question utilisée également dans les exercices suivants";
$LinkToTestEdition = "Lien vers l'édition de l'exercice"; $LinkToTestEdition = "Lien vers l'édition de l'exercice";
$MoveUsersFromCourseToSession = "Déplacer les résultats utilisateurs dans/vers une session";
?> ?>

@ -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."; $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"; $QuestionAlsoUsedInTheFollowingTests = "Esta pregunta se usa también en los ejercicios siguientes";
$LinkToTestEdition = "Vínculo a la edición del ejercicio"; $LinkToTestEdition = "Vínculo a la edición del ejercicio";
$MoveUsersFromCourseToSession = "Mover usuarios del curso base a una sesión";
?> ?>
Loading…
Cancel
Save