Session: Allow to don't include session users when exporting session list - refs BT#20364

pull/4488/head
Angel Fernando Quiroz Campos 3 years ago
parent 9bef48d9f9
commit 012b4aea39
  1. 75
      main/session/session_export.php

@ -33,8 +33,10 @@ $interbreadcrumb[] = ['url' => 'session_list.php', 'name' => get_lang('SessionLi
set_time_limit(0); set_time_limit(0);
if (isset($_POST['formSent'])) { if (isset($_POST['formSent'])) {
$formSent = $_POST['formSent']; $formSent = $_POST['formSent'];
$file_type = isset($_POST['file_type']) ? $_POST['file_type'] : 'csv'; $file_type = $_POST['file_type'] ?? 'csv';
$session_id = $_POST['session_id']; $session_id = $_POST['session_id'];
$includeUsers = !isset($_POST['no_include_users']);
if (empty($session_id)) { if (empty($session_id)) {
$sql = "SELECT $sql = "SELECT
s.id, s.id,
@ -64,22 +66,22 @@ if (isset($_POST['formSent'])) {
} }
} }
$result = Database::query($sql);
} else { } else {
$sql = "SELECT s.id,name,username,access_start_date,access_end_date,visibility,session_category_id $sql = "SELECT s.id,name,username,access_start_date,access_end_date,visibility,session_category_id
FROM $tbl_session s FROM $tbl_session s
INNER JOIN $tbl_user INNER JOIN $tbl_user
ON $tbl_user.user_id = s.id_coach ON $tbl_user.user_id = s.id_coach
WHERE s.id='$session_id'"; WHERE s.id='$session_id'";
$result = Database::query($sql);
} }
$result = Database::query($sql);
if (Database::num_rows($result)) { if (Database::num_rows($result)) {
$sessionListToExport = []; $sessionListToExport = [];
if (in_array($file_type, ['csv', 'xls'])) { if (in_array($file_type, ['csv', 'xls'])) {
$archiveFile = 'export_sessions_'.$session_id.'_'.api_get_local_time(); $archiveFile = 'export_sessions_'.$session_id.'_'.api_get_local_time();
$cvs = true; $cvs = true;
$sessionListToExport[] = [ $exportHeaders = [
'SessionId', 'SessionId',
'SessionName', 'SessionName',
'Coach', 'Coach',
@ -87,9 +89,15 @@ if (isset($_POST['formSent'])) {
'DateEnd', 'DateEnd',
'Visibility', 'Visibility',
'SessionCategory', 'SessionCategory',
'Users',
'Courses',
]; ];
if ($includeUsers) {
$exportHeaders[] = 'Users';
}
$exportHeaders[] = 'Courses';
$sessionListToExport[] = $exportHeaders;
} else { } else {
if (!file_exists($archivePath)) { if (!file_exists($archivePath)) {
mkdir($archivePath, api_get_permissions_for_new_directories(), true); mkdir($archivePath, api_get_permissions_for_new_directories(), true);
@ -119,19 +127,22 @@ if (isset($_POST['formSent'])) {
$row['visibility'] = str_replace(';', ',', $row['visibility']); $row['visibility'] = str_replace(';', ',', $row['visibility']);
$row['session_category'] = str_replace(';', ',', $row['session_category_id']); $row['session_category'] = str_replace(';', ',', $row['session_category_id']);
// users // users
$users = '';
if ($includeUsers) {
$sql = "SELECT DISTINCT $tbl_user.username $sql = "SELECT DISTINCT $tbl_user.username
FROM $tbl_user FROM $tbl_user
INNER JOIN $tbl_session_user INNER JOIN $tbl_session_user
ON ON
$tbl_user.user_id = $tbl_session_user.user_id AND $tbl_user.user_id = $tbl_session_user.user_id AND
$tbl_session_user.relation_type<>".SESSION_RELATION_TYPE_RRHH." AND $tbl_session_user.relation_type<>" . SESSION_RELATION_TYPE_RRHH . " AND
$tbl_session_user.session_id = '".$row['id']."'"; $tbl_session_user.session_id = '" . $row['id'] . "'";
$rsUsers = Database::query($sql); $rsUsers = Database::query($sql);
$users = '';
while ($rowUsers = Database::fetch_array($rsUsers)) { while ($rowUsers = Database::fetch_array($rsUsers)) {
if ($cvs) { if ($cvs) {
$users .= str_replace(';', ',', $rowUsers['username']).'|'; $users .= str_replace(';', ',', $rowUsers['username']) . '|';
} else { } else {
$users .= "\t\t<User>$rowUsers[username]</User>\n"; $users .= "\t\t<User>$rowUsers[username]</User>\n";
} }
@ -140,6 +151,7 @@ if (isset($_POST['formSent'])) {
if (!empty($users) && $cvs) { if (!empty($users) && $cvs) {
$users = api_substr($users, 0, api_strlen($users) - 1); $users = api_substr($users, 0, api_strlen($users) - 1);
} }
}
// Courses // Courses
$sql = "SELECT DISTINCT c.code, sc.id, c_id $sql = "SELECT DISTINCT c.code, sc.id, c_id
@ -171,13 +183,20 @@ if (isset($_POST['formSent'])) {
if ($cvs) { if ($cvs) {
$courses .= str_replace(';', ',', $rowCourses['code']); $courses .= str_replace(';', ',', $rowCourses['code']);
$courses .= '['.str_replace(';', ',', $coachs).']['; $courses .= '['.str_replace(';', ',', $coachs).']';
if ($includeUsers) {
$courses .= '[';
}
} else { } else {
$courses .= "\t\t<Course>\n"; $courses .= "\t\t<Course>\n";
$courses .= "\t\t\t<CourseCode>$rowCourses[code]</CourseCode>\n"; $courses .= "\t\t\t<CourseCode>$rowCourses[code]</CourseCode>\n";
$courses .= "\t\t\t<Coach>$coachs</Coach>\n"; $courses .= "\t\t\t<Coach>$coachs</Coach>\n";
} }
$userscourse = '';
if ($includeUsers) {
// rel user courses // rel user courses
$sql = "SELECT DISTINCT u.username $sql = "SELECT DISTINCT u.username
FROM $tbl_session_course_user scu FROM $tbl_session_course_user scu
@ -193,7 +212,6 @@ if (isset($_POST['formSent'])) {
scu.session_id='".$row['id']."'"; scu.session_id='".$row['id']."'";
$rsUsersCourse = Database::query($sql); $rsUsersCourse = Database::query($sql);
$userscourse = '';
while ($rowUsersCourse = Database::fetch_array($rsUsersCourse)) { while ($rowUsersCourse = Database::fetch_array($rsUsersCourse)) {
if ($cvs) { if ($cvs) {
$userscourse .= str_replace(';', ',', $rowUsersCourse['username']).','; $userscourse .= str_replace(';', ',', $rowUsersCourse['username']).',';
@ -211,7 +229,12 @@ if (isset($_POST['formSent'])) {
); );
} }
$courses .= $userscourse.']|'; $courses .= $userscourse.']';
}
}
if ($cvs) {
$courses .= '|';
} else { } else {
$courses .= "\t\t</Course>\n"; $courses .= "\t\t</Course>\n";
} }
@ -223,7 +246,7 @@ if (isset($_POST['formSent'])) {
$add = $courses; $add = $courses;
if (in_array($file_type, ['csv', 'xls'])) { if (in_array($file_type, ['csv', 'xls'])) {
$sessionListToExport[] = [ $exportContent = [
$row['id'], $row['id'],
$row['name'], $row['name'],
$row['username'], $row['username'],
@ -231,9 +254,14 @@ if (isset($_POST['formSent'])) {
$row['access_end_date'], $row['access_end_date'],
$row['visibility'], $row['visibility'],
$row['session_category'], $row['session_category'],
$users,
$courses,
]; ];
if ($includeUsers) {
$exportContent[] = $users;
}
$exportContent[] = $courses;
$sessionListToExport[] = $exportContent;
} else { } else {
$add = "\t<Session>\n" $add = "\t<Session>\n"
."\t\t<SessionId>$row[id]</SessionId>\n" ."\t\t<SessionId>$row[id]</SessionId>\n"
@ -242,11 +270,9 @@ if (isset($_POST['formSent'])) {
."\t\t<DateStart>$row[access_start_date]</DateStart>\n" ."\t\t<DateStart>$row[access_start_date]</DateStart>\n"
."\t\t<DateEnd>$row[access_end_date]</DateEnd>\n" ."\t\t<DateEnd>$row[access_end_date]</DateEnd>\n"
."\t\t<Visibility>$row[visibility]</Visibility>\n" ."\t\t<Visibility>$row[visibility]</Visibility>\n"
."\t\t<SessionCategory>$row[session_category]</SessionCategory>\n"; ."\t\t<SessionCategory>$row[session_category]</SessionCategory>\n"
} ."\t</Session>\n";
if (!$cvs) {
$add .= "\t</Session>\n";
fputs($fp, $add); fputs($fp, $add);
} }
} }
@ -264,7 +290,6 @@ if (isset($_POST['formSent'])) {
case 'xls': case 'xls':
Export::arrayToXls($sessionListToExport, $archiveFile); Export::arrayToXls($sessionListToExport, $archiveFile);
exit; exit;
break;
} }
} }
} }
@ -310,6 +335,14 @@ foreach ($Sessions as $enreg) {
} }
$form->addElement('select', 'session_id', get_lang('WhichSessionToExport'), $options); $form->addElement('select', 'session_id', get_lang('WhichSessionToExport'), $options);
$form->addCheckBox(
'no_include_users',
[
get_lang('Users'),
get_lang('ReportDoesNotIncludeListOfUsersNeitherForSessionNorForEachCourse')
],
get_lang('DoNotIncludeUsers')
);
$form->addButtonExport(get_lang('ExportSession')); $form->addButtonExport(get_lang('ExportSession'));
$defaults = []; $defaults = [];

Loading…
Cancel
Save