Admin: Optimize course export with extra fields to CSV/XML (adds "0" and "No" values when default values apply) - refs BT#20899

pull/4863/head
Yannick Warnier 2 years ago
parent 625ad5dba4
commit d4ed91abee
  1. 30
      main/admin/course_export.php
  2. 5
      main/inc/lib/extra_field.lib.php

@ -44,12 +44,14 @@ if (isset($_POST['formSent']) && $_POST['formSent']) {
if (!in_array($course['code'], $selected_courses)) {
continue;
}
$courses[] = $course;
$courses[$course['real_id']] = $course;
}
}
} else {
// Get all courses
$courses = $course_list;
foreach ($course_list as $course) {
$courses[$course['real_id']] = $course;
}
}
if (!empty($courses)) {
@ -74,7 +76,7 @@ if (isset($_POST['formSent']) && $_POST['formSent']) {
}
}
$dataToExport = [];
foreach ($courses as $course) {
foreach ($courses as $courseId => $course) {
$dataToExport['code'] = str_replace(';', ',', $course['code']);
$dataToExport['title'] = str_replace(';', ',', $course['title']);
$dataToExport['category_code'] = str_replace(';', ',', $course['category_code']);
@ -103,13 +105,25 @@ if (isset($_POST['formSent']) && $_POST['formSent']) {
$dataToExport['students'] = substr($dataToExport['students'], 0, -1);
$dataToExport['teachers'] = substr($dataToExport['teachers'], 0, -1);
}
if ($includeExtraFields) {
foreach ($allExtraFields as $extra) {
$extraValue = CourseManager::get_course_extra_field_value($extra['variable'], $course['code']);
$dataToExport[$extra['variable']] = isset($extraValue) ? $extraValue : '';
$listToExport[$courseId] = $dataToExport;
}
if ($includeExtraFields) {
foreach ($allExtraFields as $extra) {
$default = $extraField->getDefaultValueByFieldId($extra['id']);
$fieldValues = $extraField->getAllValuesByFieldId($extra['id']);
foreach ($listToExport as $courseId => &$values) {
if (isset($fieldValues[$courseId])) {
if (is_array($fieldValues[$courseId])) {
$values['extra_'.$extra['variable']] = $fieldValues[$courseId];
} else {
$values[$extra['variable']] = $fieldValues[$courseId];
}
} else {
$values[$extra['variable']] = $default;
}
}
}
$listToExport[] = $dataToExport;
}
switch ($file_type) {

@ -3209,8 +3209,11 @@ JAVASCRIPT;
// If the entry exists but is NULL, consider it an empty string (to reproduce the behaviour of UserManager::get_extra_user_data()
$values[$row['item_id']] = '';
} else {
if ($type == UserManager::USER_FIELD_TYPE_SELECT_MULTIPLE) {
if ($type == self::FIELD_TYPE_SELECT_MULTIPLE) {
$values[$row['item_id']] = explode(';', $row['value']);
} elseif (empty($row['value'])) {
// Avoid "0" values when no value should be set
$values[$row['item_id']] = null;
} else {
$values[$row['item_id']] = $row['value'];
}

Loading…
Cancel
Save