Skip to content

Commit 75ab03c

Browse files
committed
Refactor CourseSelectForm to simplify conditional logic and improve readability
1 parent 22bb81d commit 75ab03c

File tree

1 file changed

+100
-114
lines changed

1 file changed

+100
-114
lines changed

src/Chamilo/CourseBundle/Component/CourseCopy/CourseSelectForm.php

Lines changed: 100 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
*/
2020
class CourseSelectForm
2121
{
22-
/**
23-
* @return array
24-
*/
25-
public static function getResourceTitleList()
22+
public static function getResourceTitleList(): array
2623
{
2724
$list = [];
2825
$list[RESOURCE_LEARNPATH_CATEGORY] = get_lang('Learnpath').' '.get_lang('Category');
@@ -281,7 +278,6 @@ class="resource_forum"
281278
}
282279

283280
if ($avoidCourseInForm === false) {
284-
/** @var Course $course */
285281
$courseSerialized = base64_encode(Course::serialize($course));
286282
echo '<input type="hidden" name="course" value="'.$courseSerialized.'"/>';
287283
}
@@ -292,29 +288,25 @@ class="resource_forum"
292288
}
293289
}
294290

295-
$recycleOption = isset($_POST['recycle_option']) ? true : false;
291+
$recycleOption = isset($_POST['recycle_option']);
296292
if (empty($element_count)) {
297293
echo Display::return_message(get_lang('NoDataAvailable'), 'warning');
294+
} elseif (!empty($hidden_fields['destination_session'])) {
295+
echo '<br />
296+
<button
297+
class="save"
298+
type="submit"
299+
onclick="javascript:if(!confirm(' . "'" . addslashes(api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES, $charset)) . "'" . ')) return false;" >' .
300+
get_lang('Ok') . '</button>';
301+
} elseif ($recycleOption) {
302+
echo '<br /><button class="save" type="submit">' . get_lang('Ok') . '</button>';
298303
} else {
299-
if (!empty($hidden_fields['destination_session'])) {
300-
echo '<br />
301-
<button
302-
class="save"
304+
echo '<br />
305+
<button
306+
class="save btn btn-primary"
303307
type="submit"
304-
onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES, $charset))."'".')) return false;" >'.
305-
get_lang('Ok').'</button>';
306-
} else {
307-
if ($recycleOption) {
308-
echo '<br /><button class="save" type="submit">'.get_lang('Ok').'</button>';
309-
} else {
310-
echo '<br />
311-
<button
312-
class="save btn btn-primary"
313-
type="submit"
314-
onclick="checkLearnPath(\''.addslashes(get_lang('DocumentsWillBeAddedToo')).'\')">'.
315-
get_lang('Ok').'</button>';
316-
}
317-
}
308+
onclick="checkLearnPath(\'' . addslashes(get_lang('DocumentsWillBeAddedToo')) . '\')">' .
309+
get_lang('Ok') . '</button>';
318310
}
319311

320312
self::display_hidden_quiz_questions($course);
@@ -385,22 +377,16 @@ public static function parseResources(
385377

386378
if ($type == RESOURCE_LEARNPATH) {
387379
echo Display::return_message(
388-
get_lang(
389-
'ToExportLearnpathWithQuizYouHaveToSelectQuiz'
390-
),
380+
get_lang('ToExportLearnpathWithQuizYouHaveToSelectQuiz'),
391381
'warning'
392382
);
393383
echo Display::return_message(
394-
get_lang(
395-
'IfYourLPsHaveAudioFilesIncludedYouShouldSelectThemFromTheDocuments'
396-
),
384+
get_lang('IfYourLPsHaveAudioFilesIncludedYouShouldSelectThemFromTheDocuments'),
397385
'warning'
398386
);
399387
if ($enableScormSelection) {
400388
echo Display::return_message(
401-
get_lang(
402-
'IfYourLPsAreScormsYouShouldSelectThemFromTheScorms'
403-
),
389+
get_lang('IfYourLPsAreScormsYouShouldSelectThemFromTheScorms'),
404390
'warning'
405391
);
406392
}
@@ -409,9 +395,7 @@ public static function parseResources(
409395
if ($type == RESOURCE_DOCUMENT) {
410396
if (api_get_setting('show_glossary_in_documents') != 'none') {
411397
echo Display::return_message(
412-
get_lang(
413-
'ToExportDocumentsWithGlossaryYouHaveToSelectGlossary'
414-
),
398+
get_lang('ToExportDocumentsWithGlossaryYouHaveToSelectGlossary'),
415399
'warning'
416400
);
417401
}
@@ -473,20 +457,23 @@ public static function parseResources(
473457
*/
474458
public static function display_hidden_quiz_questions($course)
475459
{
476-
if (is_array($course->resources)) {
477-
foreach ($course->resources as $type => $resources) {
478-
if (!empty($resources) && count($resources) > 0) {
479-
switch ($type) {
480-
case RESOURCE_QUIZQUESTION:
481-
foreach ($resources as $id => $resource) {
482-
echo '<input
483-
type="hidden"
484-
name="resource['.RESOURCE_QUIZQUESTION.']['.$id.']"
485-
id="resource['.RESOURCE_QUIZQUESTION.']['.$id.']" value="On" />';
486-
}
487-
break;
460+
if (!is_array($course->resources)) {
461+
return;
462+
}
463+
464+
foreach ($course->resources as $type => $resources) {
465+
if (empty($resources) || count($resources) <= 0) {
466+
continue;
467+
}
468+
switch ($type) {
469+
case RESOURCE_QUIZQUESTION:
470+
foreach ($resources as $id => $resource) {
471+
echo '<input
472+
type="hidden"
473+
name="resource['.RESOURCE_QUIZQUESTION.']['.$id.']"
474+
id="resource['.RESOURCE_QUIZQUESTION.']['.$id.']" value="On" />';
488475
}
489-
}
476+
break;
490477
}
491478
}
492479
}
@@ -496,21 +483,23 @@ public static function display_hidden_quiz_questions($course)
496483
*/
497484
public static function display_hidden_scorm_directories($course)
498485
{
499-
if (!api_get_configuration_value('course_backup_allow_scorm_selection_in_select_form')) {
500-
if (is_array($course->resources)) {
501-
foreach ($course->resources as $type => $resources) {
502-
if (!empty($resources) && count($resources) > 0) {
503-
switch ($type) {
504-
case RESOURCE_SCORM:
505-
foreach ($resources as $id => $resource) {
506-
echo '<input
507-
type="hidden"
508-
name="resource['.RESOURCE_SCORM.']['.$id.']"
509-
id="resource['.RESOURCE_SCORM.']['.$id.']" value="On" />';
510-
}
511-
break;
486+
if (!api_get_configuration_value('course_backup_allow_scorm_selection_in_select_form')
487+
&& is_array($course->resources)
488+
) {
489+
foreach ($course->resources as $type => $resources) {
490+
if (empty($resources) || count($resources) <= 0) {
491+
continue;
492+
}
493+
494+
switch ($type) {
495+
case RESOURCE_SCORM:
496+
foreach ($resources as $id => $resource) {
497+
echo '<input
498+
type="hidden"
499+
name="resource['.RESOURCE_SCORM.']['.$id.']"
500+
id="resource['.RESOURCE_SCORM.']['.$id.']" value="On" />';
512501
}
513-
}
502+
break;
514503
}
515504
}
516505
}
@@ -552,57 +541,53 @@ public static function get_posted_course(?string $from = '', $session_id = 0, $c
552541

553542
// Searching the documents resource that have been set to null because
554543
// $avoidSerialize is true in the display_form() function
555-
if ($from === 'copy_course') {
556-
if (is_array($resource)) {
557-
$resource = array_keys($resource);
558-
foreach ($resource as $resource_item) {
559-
$whereConditions = [
560-
'd.c_id = ?' => [$course_id],
561-
'tool = ?' => [TOOL_DOCUMENT],
562-
'p.visibility <> ?' => [2],
563-
'd.id = ?' => [$resource_item],
564-
];
565-
566-
if (!empty($session_id)) {
567-
$session_id = (int) $session_id;
568-
$whereConditions['d.session_id = ?'] = [$session_id];
569-
}
544+
if ($from === 'copy_course' && is_array($resource)) {
545+
$resource = array_keys($resource);
546+
foreach ($resource as $resource_item) {
547+
$whereConditions = [
548+
'd.c_id = ?' => [$course_id],
549+
'tool = ?' => [TOOL_DOCUMENT],
550+
'p.visibility <> ?' => [2],
551+
'd.id = ?' => [$resource_item],
552+
];
553+
554+
if (!empty($session_id)) {
555+
$session_id = (int) $session_id;
556+
$whereConditions['d.session_id = ?'] = [$session_id];
557+
}
570558

571-
$db_result = Database::select(
572-
['d.id', 'd.path', 'd.comment', 'd.title', 'd.filetype', 'd.size'],
573-
'FROM '.$table_doc.' d INNER JOIN '.$table_prop.' p ON (d.c_id = p.c_id AND p.ref = d.id)',
559+
$db_result = Database::select(
560+
['d.id', 'd.path', 'd.comment', 'd.title', 'd.filetype', 'd.size'],
561+
'FROM '.$table_doc.' d INNER JOIN '.$table_prop.' p ON (d.c_id = p.c_id AND p.ref = d.id)',
562+
[
563+
'where' => $whereConditions,
564+
'order' => 'path',
565+
]
566+
);
567+
568+
foreach ($db_result as $obj) {
569+
$doc = new Document(
570+
$obj['id'],
571+
$obj['path'],
572+
$obj['comment'],
573+
$obj['title'],
574+
$obj['filetype'],
575+
$obj['size']
576+
);
577+
$course->add_resource($doc);
578+
// adding item property
579+
$all_properties = Database::select(
580+
'*',
581+
$table_prop,
574582
[
575-
'where' => $whereConditions,
576-
'order' => 'path',
583+
'where' => [
584+
'c_id = ?' => $course_id,
585+
'tool = ?' => RESOURCE_DOCUMENT,
586+
'ref = ?' => $resource_item,
587+
],
577588
]
578589
);
579-
580-
foreach ($db_result as $obj) {
581-
$doc = new Document(
582-
$obj['id'],
583-
$obj['path'],
584-
$obj['comment'],
585-
$obj['title'],
586-
$obj['filetype'],
587-
$obj['size']
588-
);
589-
if ($doc) {
590-
$course->add_resource($doc);
591-
// adding item property
592-
$all_properties = Database::select(
593-
'*',
594-
$table_prop,
595-
[
596-
'where' => [
597-
'c_id = ?' => $course_id,
598-
'tool = ?' => RESOURCE_DOCUMENT,
599-
'ref = ?' => $resource_item,
600-
],
601-
]
602-
);
603-
$course->resources[RESOURCE_DOCUMENT][$resource_item]->item_properties = $all_properties;
604-
}
605-
}
590+
$course->resources[RESOURCE_DOCUMENT][$resource_item]->item_properties = $all_properties;
606591
}
607592
}
608593
}
@@ -716,10 +701,11 @@ public static function get_posted_course(?string $from = '', $session_id = 0, $c
716701
}
717702
}
718703
// quiz question can be, not attached to an exercise
719-
if ($type != RESOURCE_QUIZQUESTION) {
720-
if (!isset($_POST['resource'][$type][$id]) && !$resource_is_used_elsewhere) {
721-
unset($course->resources[$type][$id]);
722-
}
704+
if ($type != RESOURCE_QUIZQUESTION
705+
&& !isset($_POST['resource'][$type][$id])
706+
&& !$resource_is_used_elsewhere
707+
) {
708+
unset($course->resources[$type][$id]);
723709
}
724710
}
725711
}

0 commit comments

Comments
 (0)