Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 105 additions & 40 deletions public/main/admin/filler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,49 +46,114 @@

// Displaying the header
Display::display_header($nameTools);

$result = '';
if (count($output) > 0) {
$result = '<div class="filler-report">'."\n";
$result .= '<table>';
foreach ($output as $line) {
$result .= '<tr>';
$result .= '<td class="filler-report-data-init">'.$line['line-init'].' </td>
<td class="filler-report-data">'.$line['line-info'].'</td>';
$result .= '</tr>';
}
$result .= '</table>';
$result .= '</div>';
echo Display::return_message($output[0]['title'], 'normal', false);
echo $result;
}
?>
<div id="datafiller" class="card">
<div class="card-body">
<h4><?php
echo Display::getMdiIcon(ActionIcon::FILL, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Data filler')).' '.get_lang('Data filler');
?>
</h4>
<div class="description"><?php echo get_lang('This section is only visible on installations from source code, not in packaged versions of the platform. It will allow you to quickly populate your platform with test data. Use with care (data is really inserted) and only on development or testing installations.'); ?></div>
<ul class="fillers">
<li>
<a href="filler.php?fill=users">
<div class="w-full max-w-none px-6 py-6">

<?php if (!empty($output)) : ?>
<?php
echo Display::getMdiIcon(ObjectIcon::USER, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Fill users')).
' '.get_lang('Fill users');
$reportTitle = $output[0]['title'] ?? get_lang('Report');
?>
</a></li>
<li>
<a href="filler.php?fill=courses">
<?php
echo Display::getMdiIcon(ObjectIcon::COURSE, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Fill courses')).
' '.get_lang('Fill courses');
?>
</a>
</li>
</ul>

<div class="mb-4 rounded-xl border border-info/30 bg-info/10 px-4 py-3">
<p class="font-medium text-gray-90"><?php echo htmlspecialchars($reportTitle); ?></p>
</div>

<div class="overflow-hidden rounded-2xl border border-gray-25 bg-white shadow-sm">
<?php foreach ($output as $i => $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;
}
?>
<div class="flex items-center justify-between border-b border-gray-25 px-4 py-3 last:border-b-0">
<div class="flex min-w-0 items-start gap-3">
<span class="mt-1 inline-flex h-2.5 w-2.5 flex-none rounded-full <?php echo $dot; ?>"></span>
<span class="truncate font-medium text-gray-90"><?php echo htmlspecialchars($title); ?></span>
</div>
<span class="ml-4 inline-flex items-center rounded-full border px-3 py-1 text-sm <?php echo $badge; ?>">
<?php echo htmlspecialchars($statusText); ?>
</span>
</div>
<?php endforeach; ?>

</div>
<?php endif; ?>

<section class="mt-8 rounded-2xl border border-gray-25 bg-white p-6 shadow-sm">
<div class="mb-4 flex items-center gap-3">
<div class="flex h-10 w-10 items-center justify-center rounded-full bg-support-2">
<?php echo Display::getMdiIcon(ActionIcon::FILL, 'h-6 w-6 text-info', null, ICON_SIZE_SMALL, get_lang('Data filler')); ?>
</div>
<div>
<h4 class="text-lg font-semibold text-gray-90"><?php echo get_lang('Data filler'); ?></h4>
<p class="mt-1 text-sm text-gray-50">
<?php echo get_lang('This section is only visible on installations from source code, not in packaged versions of the platform. It will allow you to quickly populate your platform with test data. Use with care (data is really inserted) and only on development or testing installations.'); ?>
</p>
</div>
</div>

<div class="grid gap-4 sm:grid-cols-2">
<a href="filler.php?fill=users"
class="group flex items-center justify-between rounded-xl border border-gray-25 bg-white p-4 hover:bg-gray-10">
<div class="flex items-center gap-3">
<?php
echo Display::getMdiIcon(
ObjectIcon::USER,
'h-5 w-5 text-primary',
null,
ICON_SIZE_SMALL,
get_lang('Fill users')
);
?>
<span class="font-medium text-gray-90"><?php echo get_lang('Fill users'); ?></span>
</div>
<span class="rounded-md bg-primary/10 px-2 py-1 text-xs font-medium text-primary">
<?php echo get_lang('Run'); ?>
</span>
</a>

<a href="filler.php?fill=courses"
class="group flex items-center justify-between rounded-xl border border-gray-25 bg-white p-4 hover:bg-gray-10">
<div class="flex items-center gap-3">
<?php
echo Display::getMdiIcon(
ObjectIcon::COURSE,
'h-5 w-5 text-secondary',
null,
ICON_SIZE_SMALL,
get_lang('Fill courses')
);
?>
<span class="font-medium text-gray-90"><?php echo get_lang('Fill courses'); ?></span>
</div>
<span class="rounded-md bg-secondary/10 px-2 py-1 text-xs font-medium text-secondary">
<?php echo get_lang('Run'); ?>
</span>
</a>
</div>
</section>
</div>
</div>

<?php
/* FOOTER */
Display::display_footer();
71 changes: 53 additions & 18 deletions tests/datafiller/fill_courses.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,70 @@
*
*/

use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\Helpers\CourseHelper;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;

/**
* Loads the data and injects it into the Chamilo database, using the Chamilo
* internal functions.
* @return array List of user IDs for the users that have just been inserted
*/
function fill_courses()
function fill_courses(): array
{
$courses = array(); // declare only to avoid parsing notice
require_once 'data_courses.php'; // fill the $courses array
$output = array();
$output[] = array('title'=>'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;
Expand Down
Loading