diff --git a/public/main/admin/filler.php b/public/main/admin/filler.php
index 68225c835fc..de386a7a7ab 100644
--- a/public/main/admin/filler.php
+++ b/public/main/admin/filler.php
@@ -46,49 +46,114 @@
// Displaying the header
Display::display_header($nameTools);
-
-$result = '';
-if (count($output) > 0) {
- $result = '
'."\n";
- $result .= '
';
- foreach ($output as $line) {
- $result .= '';
- $result .= '| '.$line['line-init'].' |
- '.$line['line-info'].' | ';
- $result .= '
';
- }
- $result .= '
';
- $result .= '
';
- echo Display::return_message($output[0]['title'], 'normal', false);
- echo $result;
-}
?>
-
-
+
'Courses Filling Report: ');
- $languages = array_column(
- SubLanguageManager::getAllLanguages(true),
- 'isocode'
- );
+ $courses = [];
+ require_once 'data_courses.php'; // populates $courses
+
+ $output = [];
+ $output[] = ['title' => 'Courses Filling Report: '];
+
+ $languages = array_column(SubLanguageManager::getAllLanguages(true), 'isocode');
+
+ /** @var CourseHelper $courseHelper */
+ $courseHelper = Container::$container->get(CourseHelper::class);
+ /** @var CourseRepository $courseRepo */
+ $courseRepo = Container::$container->get(CourseRepository::class);
+
$i = 1;
- foreach ($courses as $i => $course) {
- // First check that the first item doesn't exist already
- $output[$i]['line-init'] = $course['title'];
- // The wanted code is necessary to avoid interpretation
+ foreach ($courses as $course) {
+ // Keep original title for the report row
+ $output[$i]['line-init'] = $course['title'];
+
+ // Ensure required keys expected by CourseHelper
$course['wanted_code'] = $course['code'];
- // Make sure the language defaults to English if others are disabled
if (!in_array($course['course_language'], $languages)) {
$course['course_language'] = 'en_US';
}
- // Effectively create the course
- $res = CourseManager::create_course($course);
- $output[$i]['line-info'] = null !== $res ? get_lang('Added') : get_lang('Not inserted');
- $i++;
+
+ if ($courseRepo->courseCodeExists($course['wanted_code'])) {
+ $output[$i]['line-info'] = get_lang('The course code already exists');
+ $output[$i]['status'] = 'exists';
+ $i++;
+ continue;
+ }
+
+ // Create with the Symfony helper
+ try {
+ $created = $courseHelper->createCourse([
+ 'title' => $course['title'],
+ 'wanted_code' => $course['wanted_code'],
+ 'course_language' => $course['course_language'],
+ 'exemplary_content' => $course['exemplary_content'] ?? false,
+ ]);
+
+ if ($created) {
+ $output[$i]['line-info'] = get_lang('Added');
+ $output[$i]['status'] = 'ok';
+ } else {
+ $output[$i]['line-info'] = get_lang('Not inserted');
+ $output[$i]['status'] = 'error';
+ }
+ } catch (Throwable $e) {
+ $output[$i]['line-info'] = get_lang('Not inserted');
+ $output[$i]['status'] = 'error';
+ }
+
+ $i++;
}
return $output;