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 .= ' - '; - $result .= ''; - } - $result .= '
'.$line['line-init'].' '.$line['line-info'].'
'; - $result .= '
'; - echo Display::return_message($output[0]['title'], 'normal', false); - echo $result; -} ?> -
-
-

-

-
- + +
+

+
+ +
+ $line) : + if ($i === 0) { continue; } + $title = $line['line-init'] ?? ''; + $statusText = (string)($line['line-info'] ?? ''); + $statusCode = $line['status'] ?? null; + + if ($statusCode === null) { + $isOk = preg_match('/(Ajout|Added|AƱadid|Agregad|OK|Success)/i', $statusText); + $isErr = preg_match('/(Non|Not|Error|Erreur|No|Failed|Existe|already)/i', $statusText); + $statusCode = $isOk ? 'ok' : ($isErr ? 'error' : 'exists'); + } + switch ($statusCode) { + case 'ok': + $badge = 'bg-success/10 text-success border-success/30'; + $dot = 'bg-success'; + break; + case 'error': + $badge = 'bg-danger/10 text-danger border-danger/30'; + $dot = 'bg-danger'; + break; + case 'exists': + default: + $badge = 'bg-warning/10 text-warning border-warning/30'; + $dot = 'bg-warning'; + break; + } + ?> +
+
+ + +
+ + + +
+ + +
+ + +
+
+
+ +
+
+

+

+ +

+
+
+ + +
-
+ '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;