diff --git a/assets/vue/views/course/CourseHome.vue b/assets/vue/views/course/CourseHome.vue index 1a611db9b4..a32fb4a61d 100644 --- a/assets/vue/views/course/CourseHome.vue +++ b/assets/vue/views/course/CourseHome.vue @@ -63,7 +63,7 @@ class="ml-auto" icon="plus" type="primary" - @click="addIntro(course, introTool)" + @click="addIntro(course, intro)" /> @@ -266,37 +266,24 @@ const toggleCourseTMenu = (event) => { }; async function getIntro() { - // Searching for the CTool called 'course_homepage'. - let currentIntroTool = course.value.tools.find((element) => element.name === "course_homepage"); - - if (!introTool.value) { - introTool.value = currentIntroTool; - - if (sessionId) { - createInSession.value = true; - } - - // Search CToolIntro for this - const filter = { - courseTool: currentIntroTool.iid, + axios.get('/course/'+courseId+'/getToolIntro', { + params: { cid: courseId, sid: sessionId, - }; - - try { - const response = await store.dispatch("ctoolintro/findAll", filter); - if (response) { - if (sessionId) { - createInSession.value = false; - } - // first item - intro.value = response[0]; - translateHtml(); - } - } catch (e) { - console.error(e); } - } + }). + then(response => { + intro.value = response.data; + introTool.value = response.data.c_tool; + + + console.log('response.data ',response.data); + + createInSession.value = response.data.createInSession; + translateHtml(); + }).catch(function (error) { + console.log(error); + }); } function addIntro(course, introTool) { @@ -307,6 +294,7 @@ function addIntro(course, introTool) { cid: courseId, sid: sessionId, parentResourceNodeId: course.resourceNode.id, + ctoolId: introTool.cToolId, }, }); } @@ -314,11 +302,13 @@ function addIntro(course, introTool) { function updateIntro(intro) { return router.push({ name: "ToolIntroUpdate", - params: { id: intro["@id"] }, + params: {'id': '/api/c_tool_intros/'+intro.iid }, query: { cid: courseId, sid: sessionId, - id: intro["@id"], + ctoolintroIid: intro.iid, + ctoolId: intro.c_tool.iid, + id: '/api/c_tool_intros/'+intro.iid, }, }); } diff --git a/assets/vue/views/ctoolintro/Create.vue b/assets/vue/views/ctoolintro/Create.vue index ddb7f115b2..a53f44beb8 100644 --- a/assets/vue/views/ctoolintro/Create.vue +++ b/assets/vue/views/ctoolintro/Create.vue @@ -66,6 +66,7 @@ export default { } let toolId = route.params.courseTool; + let ctoolId = route.query.ctoolId; // Get the current intro text. axios.get(ENTRYPOINT + 'c_tool_intros/' + toolId).then(response => { @@ -76,7 +77,7 @@ export default { }); item.value['parentResourceNodeId'] = Number(route.query.parentResourceNodeId); - item.value['courseTool'] = '/api/c_tools/'+toolId; + item.value['courseTool'] = '/api/c_tools/' + ctoolId; item.value['resourceLinkList'] = [{ sid: route.query.sid, @@ -86,7 +87,15 @@ export default { function onCreated(item) { showNotification(t('Updated')); - router.go(-1); + axios.post('/course/'+cid+'/addToolIntro', { + iid: item.iid, + cid: route.query.cid, + sid: route.query.sid + }).then(response => { + router.go(-1); + }).catch(function (error) { + console.log(error); + }); } return {v$: useVuelidate(), users, isLoadingSelect, item, onCreated}; diff --git a/assets/vue/views/ctoolintro/Update.vue b/assets/vue/views/ctoolintro/Update.vue index a868678f66..deb1cf2ffc 100644 --- a/assets/vue/views/ctoolintro/Update.vue +++ b/assets/vue/views/ctoolintro/Update.vue @@ -18,9 +18,15 @@ import ToolIntroForm from '../../components/ctoolintro/Form.vue'; import Loading from '../../components/Loading.vue'; import Toolbar from '../../components/Toolbar.vue'; import UpdateMixin from '../../mixins/UpdateMixin'; +import {computed, onMounted, reactive, ref, toRefs} from "vue"; import {useI18n} from "vue-i18n"; import {useRoute, useRouter} from "vue-router"; import toInteger from "lodash/toInteger"; +import useVuelidate from '@vuelidate/core' +import axios from 'axios' +import { ENTRYPOINT } from '../../config/entrypoint' +import { RESOURCE_LINK_PUBLISHED } from '../../components/resource_links/visibility' +import useNotification from '../../components/Notification' const servicePrefix = 'ctoolintro'; @@ -35,19 +41,42 @@ export default { }, setup() { const route = useRoute(); + const router = useRouter(); + const {showNotification} = useNotification(); const store = useStore(); + const item = ref({}); const cid = toInteger(route.query.cid); if (cid) { let courseIri = '/api/courses/' + cid; store.dispatch('course/findCourse', { id: courseIri }); } - /*function updated(val) { + let toolId = route.query.ctoolId; + let ctoolintroId = route.query.ctoolintroIid; + + // Get the current intro text. + axios.get(ENTRYPOINT + 'c_tool_intros/'+ctoolintroId).then(response => { + let data = response.data; + item.value['introText'] = data.introText; + }).catch(function (error) { + console.log(error); + }); + + item.value['parentResourceNodeId'] = Number(route.query.parentResourceNodeId); + item.value['courseTool'] = '/api/c_tools/'+toolId; + + item.value['resourceLinkList'] = [{ + sid: route.query.sid, + cid: route.query.cid, + visibility: RESOURCE_LINK_PUBLISHED, // visible by default + }]; + + function onUpdated(val) { showNotification(t('Updated')); router.go(-1); - }*/ - //return {updated}; - return; + } + + return {v$: useVuelidate(), item, onUpdated}; }, computed: { ...mapFields('ctoolintro', { diff --git a/src/CoreBundle/Controller/CourseController.php b/src/CoreBundle/Controller/CourseController.php index 68d1dd276e..4dd693cd49 100644 --- a/src/CoreBundle/Controller/CourseController.php +++ b/src/CoreBundle/Controller/CourseController.php @@ -8,7 +8,9 @@ namespace Chamilo\CoreBundle\Controller; use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\ExtraField; +use Chamilo\CoreBundle\Entity\Session; use Chamilo\CoreBundle\Entity\Tag; +use Chamilo\CoreBundle\Entity\Tool; use Chamilo\CoreBundle\Entity\User; use Chamilo\CoreBundle\Framework\Container; use Chamilo\CoreBundle\Repository\LanguageRepository; @@ -21,6 +23,7 @@ use Chamilo\CoreBundle\Tool\ToolChain; use Chamilo\CourseBundle\Controller\ToolBaseController; use Chamilo\CourseBundle\Entity\CCourseDescription; use Chamilo\CourseBundle\Entity\CTool; +use Chamilo\CourseBundle\Entity\CToolIntro; use Chamilo\CourseBundle\Repository\CCourseDescriptionRepository; use Chamilo\CourseBundle\Repository\CShortcutRepository; use Chamilo\CourseBundle\Repository\CToolRepository; @@ -34,6 +37,7 @@ use Event; use Exercise; use ExtraFieldValue; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -600,6 +604,132 @@ class CourseController extends ToolBaseController ]); } + #[Route('/{id}/getToolIntro', name: 'chamilo_core_course_gettoolintro')] + public function getToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response + { + $sessionId = (int) $request->get('sid'); + error_log('Session id -> '.$sessionId); + + //$session = $this->getSession(); + $responseData = []; + $ctoolRepo = $em->getRepository(CTool::class); + $sessionRepo = $em->getRepository(Session::class); + $createInSession = false; + + $session = null; + if (!empty($sessionId)) { + $session = $sessionRepo->find($sessionId); + } + + $qb = $em->createQueryBuilder(); + $query = $qb->select('ct') + ->from('Chamilo\CourseBundle\Entity\CTool', 'ct') + ->where('ct.course = :c_id') + ->andWhere('ct.name = :name') + ->andWhere( + $qb->expr()->orX( + $qb->expr()->eq('ct.session', ':session_id'), + $qb->expr()->isNull('ct.session') + ) + ) + ->setParameters([ + 'c_id' => $course->getId(), + 'name' => 'course_homepage', + 'session_id' => 0 + ]) + ->getQuery(); + + $ctool = $query->getOneOrNullResult(); + + if ($session) { + error_log('Session actual -> '.$session->getId()); + error_log('Course actual -> '.$course->getId()); + $ctoolSession = $ctoolRepo->findOneBy(['name' => 'course_homepage', 'course' => $course, 'session' => $session]); + if (!$ctoolSession) { + $createInSession = true; + } else { + $createInSession = false; + $ctool = $ctoolSession; + } + } + + + error_log('$ctool -> '.$ctool->getIid()); + error_log('$createInSession -> '.$createInSession); + + if ($ctool) { + $ctoolintroRepo = $em->getRepository(CToolIntro::class); + /** @var CToolIntro $ctoolintro */ + $ctoolintro = $ctoolintroRepo->findOneBy(['courseTool' => $ctool]); + if ($ctoolintro) { + $responseData = [ + 'iid' => $ctoolintro->getIid(), + 'introText' => $ctoolintro->getIntroText(), + 'createInSession' => $createInSession, + 'cToolId' => $ctool->getIid(), + ]; + } + $responseData['c_tool'] = $ctool; + } + + return new JsonResponse($responseData); + } + + #[Route('/{id}/addToolIntro', name: 'chamilo_core_course_addtoolintro')] + public function addToolIntro(Request $request, Course $course, EntityManagerInterface $em): Response + { + error_log('In addToolIntro'); + + $data = $request->getContent(); + $data = json_decode($data); + $ctoolintroId = $data->iid; + $sessionId = $data->sid; + + $sessionRepo = $em->getRepository(Session::class); + $session = null; + if (!empty($sessionId)) { + $session = $sessionRepo->find($sessionId); + } + + $ctool = $em->getRepository(CTool::class); + $check = $ctool->findOneBy(['name' => 'course_homepage', 'session' => $session]); + if (!$check) { + $toolRepo = $em->getRepository(Tool::class); + $toolEntity = $toolRepo->findOneBy(['name' => 'course_homepage']); + $courseTool = (new CTool()) + ->setTool($toolEntity) + ->setName('course_homepage') + ->setCourse($course) + ->setPosition(1) + ->setVisibility(true) + ->setParent($course) + ->setCreator($course->getCreator()) + ->setSession($session) + ->addCourseLink($course) + ; + $em->persist($courseTool); + $em->flush(); + if ($courseTool && !empty($ctoolintroId)) { + $ctoolintroRepo = Container::getToolIntroRepository(); + /** @var CToolIntro $ctoolintro */ + $ctoolintro = $ctoolintroRepo->find($ctoolintroId); + $ctoolintro->setCourseTool($courseTool); + $ctoolintroRepo->update($ctoolintro); + } + } + $responseData = []; + $json = $this->serializer->serialize( + $responseData, + 'json', + [ + 'groups' => ['course:read', 'ctool:read', 'tool:read', 'cshortcut:read'], + ] + ); + + return new JsonResponse($responseData); + + } + private function autoLaunch(): void { $autoLaunchWarning = '';