Skip to content

Commit

Permalink
Admin: Add visibility to course import/export/update through CSV/XML …
Browse files Browse the repository at this point in the history
…- refs BT#21415
  • Loading branch information
ywarnier committed Mar 18, 2024
1 parent bef68ff commit 503d9d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
3 changes: 3 additions & 0 deletions main/admin/course_export.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
'CourseCategoryName',
'Teacher',
'Language',
'Visibility',
];
if ($includeUsers) {
$listToExport[0][] = 'Users';
Expand All @@ -88,6 +89,8 @@
}
$dataToExport['tutor_name'] = str_replace(';', ',', $course['tutor_name']);
$dataToExport['course_language'] = str_replace(';', ',', $course['course_language']);
$dataToExport['visibility'] = str_replace(';', ',', $course['visibility']);

if ($includeUsers) {
$dataToExport['students'] = '';
$dataToExport['teachers'] = '';
Expand Down
11 changes: 7 additions & 4 deletions main/admin/course_import.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ function save_courses_data($courses)
$params['user_id'] = $creatorId;
$addMeAsTeacher = isset($_POST['add_me_as_teacher']) ? $_POST['add_me_as_teacher'] : false;
$params['add_user_as_teacher'] = $addMeAsTeacher;
if (isset($course['Visibility'])) {
$params['visibility'] = $course['Visibility'];
}

// Check if there is a course template stated for this course. In that case, we check if that code exists in DB:
if (array_key_exists('CourseTemplate', $course) && $course['CourseTemplate'] != '') {
Expand Down Expand Up @@ -267,10 +270,10 @@ function parse_csv_courses_data($file)

<blockquote>
<pre>
<strong>Code</strong>;<strong>Title</strong>;<strong>CourseCategory</strong>;<strong>CourseCategoryName</strong>;Teacher;Language;CourseTemplate;CloneHomepageTools
BIO0015;Biology;BIO;Science;teacher1;english;TEMPLATE1;true
BIO0016;Maths;MATH;Engineerng;teacher2|teacher3;english;;
BIO0017;Language;LANG;;;english;;
<strong>Code</strong>;<strong>Title</strong>;<strong>CourseCategory</strong>;<strong>CourseCategoryName</strong>;Teacher;Language;Visibility;CourseTemplate;CloneHomepageTools
BIO0015;Biology;BIO;Science;teacher1;english;1;TEMPLATE1;true
BIO0016;Maths;MATH;Engineerng;teacher2|teacher3;english;1;;
BIO0017;Language;LANG;;;english;1;;
</pre>
</blockquote>

Expand Down
24 changes: 16 additions & 8 deletions main/admin/course_update_import.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
*/
function generateCsvModel(array $fields): string
{
$headerCsv = "<strong>Code</strong>;Title;CourseCategory;Language;";
$headerCsv = "<strong>Code</strong>;Title;CourseCategory;Language;Visibility;";

$exampleCsv = "<b>COURSE001</b>;Introduction to Biology;BIO;english;";
$exampleCsv = "<b>COURSE001</b>;Introduction to Biology;BIO;english;1;";

foreach ($fields as $field) {
$fieldType = (int) $field['field_type'];
Expand Down Expand Up @@ -50,6 +50,7 @@ function generateXmlModel(array $fields): string
$modelXml .= " &lt;Title&gt;Introduction to Biology&lt;/Title&gt;\n";
$modelXml .= " &lt;CourseCategory&gt;BIO&lt;/CourseCategory&gt;\n";
$modelXml .= " &lt;Language&gt;english&lt;/Language&gt;\n";
$modelXml .= " &lt;Visibility&gt;1&lt;/Visibility&gt;\n";
foreach ($fields as $field) {
switch ($field['field_type']) {
case ExtraField::FIELD_TYPE_CHECKBOX:
Expand Down Expand Up @@ -102,12 +103,19 @@ function validateCourseData(array $courses): array
function updateCourse(array $courseData, int $courseId): void
{
$courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
$params = [
'title' => $courseData['Title'],
'course_language' => $courseData['Language'],
'category_code' => $courseData['CourseCategory'],
'visual_code' => $courseData['Code'],
];
$params = [];
if (isset($courseData['Title'])) {
$params['title'] = $courseData['Title'];
}
if (isset($courseData['Language'])) {
$params['course_language'] = $courseData['Language'];
}
if (isset($courseData['CourseCategory'])) {
$params['category_code'] = $courseData['CourseCategory'];
}
if (isset($courseData['Visibility'])) {
$params['visibility'] = $courseData['Visibility'];
}
Database::update($courseTable, $params, ['id = ?' => $courseId]);
$courseData['code'] = $courseData['Code'];
$courseData['item_id'] = $courseId;
Expand Down

0 comments on commit 503d9d0

Please sign in to comment.