Skip to content

Commit dd6ecf8

Browse files
Merge pull request #5302 from christianbeeznest/fixes-updates16
Course: Refactor course creation service
2 parents edb333e + 576b2a4 commit dd6ecf8

File tree

6 files changed

+878
-37
lines changed

6 files changed

+878
-37
lines changed

assets/vue/components/course/Form.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
v-model="fillDemoContent"
4646
name=""
4747
/>
48-
<BaseAutocomplete
48+
<!--BaseAutocomplete
4949
id="template"
5050
v-model="courseTemplate"
5151
:label="t('Select Template')"
5252
:search="searchTemplates"
53-
/>
53+
/-->
5454
</div>
5555
<!-- Form Footer -->
5656
<div class="form-footer">

assets/vue/views/course/Create.vue

+15-12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { useRouter } from "vue-router"
2929
import Message from 'primevue/message'
3030
import courseService from "../../services/courseService"
3131
import { useI18n } from "vue-i18n"
32+
import { useNotification } from "../../composables/notification"
3233
3334
const store = useStore()
3435
const item = ref({})
@@ -38,24 +39,26 @@ const { t } = useI18n()
3839
const isLoading = computed(() => store.getters['course/getField']('isLoading'))
3940
const violations = computed(() => store.getters['course/getField']('violations'))
4041
const courseData = ref({})
42+
const { showSuccessNotification, showErrorNotification } = useNotification()
4143
4244
const submitCourse = async (formData) => {
4345
isLoading.value = true
4446
try {
45-
let tempResponse = await courseService.createCourse(formData)
46-
if (tempResponse.success) {
47-
const courseId = tempResponse.courseId
48-
const sessionId = 0
49-
await router.push(`/course/${courseId}/home?sid=${sessionId}`)
50-
} else {
51-
console.error(tempResponse.message)
52-
}
47+
const response = await courseService.createCourse(formData)
48+
const courseId = response.courseId
49+
const sessionId = 0
50+
showSuccessNotification(t('Course created successfully.'))
51+
await router.push(`/course/${courseId}/home?sid=${sessionId}`)
5352
} catch (error) {
5453
console.error(error)
55-
if (error.response && error.response.data) {
56-
violations.value = error.response.data
57-
} else {
58-
console.error('An unexpected error occurred.')
54+
55+
const errorMessage = error.response && error.response.data && error.response.data.message
56+
? error.response.data.message
57+
: t('An unexpected error occurred.')
58+
showErrorNotification(errorMessage)
59+
60+
if (error.response && error.response.data && error.response.data.violations) {
61+
violations.value = error.response.data.violations
5962
}
6063
} finally {
6164
isLoading.value = false

src/CoreBundle/Controller/CourseController.php

+27-22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
2424
use Chamilo\CoreBundle\Repository\TagRepository;
2525
use Chamilo\CoreBundle\Security\Authorization\Voter\CourseVoter;
26+
use Chamilo\CoreBundle\Service\CourseService;
2627
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
2728
use Chamilo\CoreBundle\Settings\SettingsManager;
2829
use Chamilo\CoreBundle\Tool\ToolChain;
@@ -721,7 +722,7 @@ public function searchCourseTemplates(
721722
foreach ($courseList as $course) {
722723
$title = $course['title'];
723724
$results['items'][] = [
724-
'id' => $course['code'],
725+
'id' => $course['id'],
725726
'name' => $title.' ('.$course['code'].') ',
726727
];
727728
}
@@ -730,43 +731,47 @@ public function searchCourseTemplates(
730731
}
731732

732733
#[Route('/create', name: 'chamilo_core_course_create')]
733-
public function createCourse(Request $request, TranslatorInterface $translator): JsonResponse
734-
{
734+
public function createCourse(
735+
Request $request,
736+
TranslatorInterface $translator,
737+
CourseService $courseService
738+
): JsonResponse {
735739
$courseData = json_decode($request->getContent(), true);
736740

741+
$title = $courseData['name'] ?? null;
737742
$wantedCode = $courseData['code'] ?? null;
743+
$courseLanguage = $courseData['language']['id'] ?? null;
738744
$categoryCode = $courseData['category'] ?? null;
739-
$title = $courseData['name'];
740-
$courseLanguage = isset($courseData['language']) ? $courseData['language']['id'] : '';
741745
$exemplaryContent = $courseData['fillDemoContent'] ?? false;
742746
$template = $courseData['template'] ?? '';
743747

744-
if (empty($wantedCode)) {
745-
$wantedCode = CourseManager::generate_course_code(substr($title, 0, CourseManager::MAX_COURSE_LENGTH_CODE));
746-
}
747-
748-
$courseCodeOk = !CourseManager::course_code_exists($wantedCode);
749-
if ($courseCodeOk) {
750-
$params = [
751-
'title' => $title,
752-
'exemplary_content' => $exemplaryContent,
753-
'wanted_code' => $wantedCode,
754-
'course_language' => $courseLanguage,
755-
'course_template' => $template,
756-
];
748+
$params = [
749+
'title' => $title,
750+
'wanted_code' => $wantedCode,
751+
'course_language' => $courseLanguage,
752+
'exemplary_content' => $exemplaryContent,
753+
'course_template' => $template,
754+
];
757755

758-
if ($categoryCode) {
759-
$params['course_categories'] = $categoryCode;
760-
}
756+
if ($categoryCode) {
757+
$params['course_categories'] = $categoryCode;
758+
}
761759

762-
$course = CourseManager::create_course($params);
760+
try {
761+
$course = $courseService->createCourse($params);
763762
if ($course) {
764763
return new JsonResponse([
765764
'success' => true,
766765
'message' => $translator->trans('Course created successfully.'),
767766
'courseId' => $course->getId(),
768767
]);
769768
}
769+
} catch (\Exception $e) {
770+
771+
return new JsonResponse([
772+
'success' => false,
773+
'message' => $translator->trans($e->getMessage())
774+
], Response::HTTP_BAD_REQUEST);
770775
}
771776

772777
return new JsonResponse(['success' => false, 'message' => $translator->trans('An error occurred while creating the course.')]);

src/CoreBundle/Repository/Node/CourseRepository.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
use Chamilo\CoreBundle\Entity\AccessUrl;
1111
use Chamilo\CoreBundle\Entity\Course;
1212
use Chamilo\CoreBundle\Entity\CourseRelUser;
13-
use Chamilo\CoreBundle\Entity\ResourceNode;
1413
use Chamilo\CoreBundle\Entity\User;
1514
use Chamilo\CoreBundle\Repository\ResourceRepository;
1615
use Doctrine\Common\Collections\Criteria;
16+
use Doctrine\ORM\AbstractQuery;
1717
use Doctrine\ORM\Query\Expr\Join;
1818
use Doctrine\ORM\QueryBuilder;
1919
use Doctrine\Persistence\ManagerRegistry;
@@ -207,4 +207,27 @@ public function getSubscribedUsersByStatus(Course $course, int $status)
207207

208208
return $queryBuilder;
209209
}
210+
211+
public function courseCodeExists(string $code): bool
212+
{
213+
$qb = $this->createQueryBuilder('c')
214+
->select('count(c.id)')
215+
->where('c.code = :code OR c.visualCode = :code')
216+
->setParameter('code', $code)
217+
->getQuery();
218+
219+
return (int) $qb->getSingleScalarResult() > 0;
220+
}
221+
222+
public function findCourseAsArray($id)
223+
{
224+
$qb = $this->createQueryBuilder('c')
225+
->select('c.id, c.code, c.title, c.visualCode, c.courseLanguage, c.departmentUrl, c.departmentName')
226+
->where('c.id = :id')
227+
->setParameter('id', $id);
228+
229+
$query = $qb->getQuery();
230+
231+
return $query->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY);
232+
}
210233
}

0 commit comments

Comments
 (0)