<?php
namespace EADPlataforma\Controller\Admin;
use EADPlataforma\Error\ActionInvalidException;
use EADPlataforma\Error\FieldException;
use EADPlataforma\Error\NotFoundException;
use EADPlataforma\Error\PermissionException;
use EADPlataforma\Error\AuthInvalidException;
use EADPlataforma\Response\HttpCreated;
use EADPlataforma\Response\HttpNoContent;
use EADPlataforma\Response\HttpOk;
use EADPlataforma\Services\EntityServices\CourseIndexService;
use EADPlataforma\Services\EntityServices\ExamUserAnswerService;
use EADPlataforma\Services\EntityServices\LibraryChapterService;
use EADPlataforma\Services\EntityServices\LessonService;
use EADPlataforma\Services\EntityServices\LessonModuleService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use EADPlataforma\Http\EADResponse;
use EADPlataforma\Entity\Course;
use EADPlataforma\Entity\CourseTeam;
use EADPlataforma\Entity\Category;
use EADPlataforma\Entity\Product;
use EADPlataforma\Entity\ProductOffer;
use EADPlataforma\Entity\ProductTeam;
use EADPlataforma\Entity\Lesson;
use EADPlataforma\Entity\LessonSupport;
use EADPlataforma\Entity\LessonLog;
use EADPlataforma\Entity\LessonLogOrigin;
use EADPlataforma\Entity\LessonModule;
use EADPlataforma\Entity\LessonAnnotation;
use EADPlataforma\Entity\Library;
use EADPlataforma\Entity\LessonXLibrary;
use EADPlataforma\Entity\LikeControl;
use EADPlataforma\Entity\CourseCertificateTemplate;
use EADPlataforma\Entity\Enrollment;
use EADPlataforma\Entity\Exam;
use EADPlataforma\Entity\ExamUser;
use EADPlataforma\Entity\ExamUserAnswer;
use EADPlataforma\Entity\ExamUserAnswerOption;
use EADPlataforma\Entity\Question;
use EADPlataforma\Entity\QuestionOption;
use EADPlataforma\Entity\CourseTestimonial;
use EADPlataforma\Entity\User;
use EADPlataforma\Entity\UserSubscription;
use EADPlataforma\Enum\CourseEnum;
use EADPlataforma\Enum\CourseTeamEnum;
use EADPlataforma\Enum\ProductEnum;
use EADPlataforma\Enum\EnrollmentEnum;
use EADPlataforma\Enum\ExamEnum;
use EADPlataforma\Enum\ExamUserEnum;
use EADPlataforma\Enum\LessonEnum;
use EADPlataforma\Enum\LessonSupportEnum;
use EADPlataforma\Enum\LibraryEnum;
use EADPlataforma\Enum\LikeControlEnum;
use EADPlataforma\Enum\LessonXLibraryEnum;
use EADPlataforma\Enum\LessonAnnotationEnum;
use EADPlataforma\Enum\LessonModuleEnum;
use EADPlataforma\Enum\ExamUserAnswerEnum;
use EADPlataforma\Enum\QuestionEnum;
use EADPlataforma\Enum\QuestionOptionEnum;
use EADPlataforma\Enum\CourseTestimonialEnum;
use EADPlataforma\Enum\NotificationEnum;
use EADPlataforma\Enum\WebhookEnum;
use EADPlataforma\Enum\TrashEnum;
use EADPlataforma\Enum\ErrorEnum;
/**
* @Route(
* path = "",
* schemes = {"http|https"}
* )
* @Cache(
* maxage = "0",
* smaxage = "0",
* expires = "now",
* public = false
* )
*/
class CourseIndexController extends AbstractController {
public function getEntityClass(){
return Course::class;
}
/**
* @Route(
* path = "/admin/v2/general/{id}",
* methods = {"GET"},
* requirements = { "id" = "\d+" }
* )
*
* @throws NotFoundException
* @throws ActionInvalidException
*/
public function getCourseNewGeneral(
Request $request,
CourseIndexService $courseIndexService
): JsonResponse
{
$courseId = $request->get('id');
$course = $courseIndexService->searchCourse($courseId);
if(!$course){
throw new NotFoundException(
$this->configuration->getLanguage('error_course_not_found', 'lesson_view_error')
);
}
$enrollment = $courseIndexService->searchEnrollment($course);
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_enrollment_not_found',
'lesson_view_error'
)
);
}
$isStudent = $courseIndexService->isStudent($course);
if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_course_not_published',
'lesson_view_error'
)
);
}
$data = $courseIndexService->getCourseNewGeneral($course, $enrollment);
return new HttpOk($data);
}
/**
* @Route(
* path = "/admin/v2/course/{id}",
* methods = {"GET"},
* requirements = { "id" = "\d+" }
* )
*
* @throws NotFoundException
* @throws ActionInvalidException
*/
public function getCourseNewIndex(
Request $request,
CourseIndexService $courseIndexService
): JsonResponse
{
$courseId = $request->get('id');
$course = $courseIndexService->searchCourse($courseId);
if(!$course){
throw new NotFoundException(
$this->configuration->getLanguage('error_course_not_found', 'lesson_view_error')
);
}
$discordService = $this->generalService->getService('DiscordService');
$discordService->setChannel('debug');
$host = $request->getHost();
if($host == 'local-dev.eadplataforma.com' || $host == 'culinayra.eadplataforma.app'){
$discordService->setMessage("User Controller: {$this->user->getId()}");
$discordService->sendDiscord();
}
$enrollment = $courseIndexService->searchEnrollment($course, $host);
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_enrollment_not_found',
'lesson_view_error'
)
);
}
$isStudent = $courseIndexService->isStudent($course);
if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_course_not_published',
'lesson_view_error'
)
);
}
$data = $courseIndexService->getCourseIndex($enrollment);
return new HttpOk($data);
}
/**
* @Route(
* path = "/admin/v2/module/{id}",
* methods = {"GET"},
* requirements = { "id" = "\d+" }
* )
*
* @throws NotFoundException
* @throws ActionInvalidException
*/
public function getModuleNewIndex(
Request $request,
CourseIndexService $courseIndexService,
LessonModuleService $lessonModuleService
): JsonResponse
{
$moduleId = $request->get('id');
$lessonModule = $lessonModuleService->searchModule($moduleId);
if(!$lessonModule){
throw new NotFoundException(
$this->configuration->getLanguage('error_module_not_found', 'lesson_view_error')
);
}
$course = $lessonModule->getCourse();
$enrollment = $courseIndexService->searchEnrollment($course);
if(!$enrollment){
throw new ActionInvalidException($this->configuration->getLanguage('error_enrollment_not_found', 'lesson_view_error'));
}
$isStudent = $courseIndexService->isStudent($course);
if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_course_not_published',
'lesson_view_error'
)
);
}
$data = $lessonModuleService->getModuleIndex(
$enrollment,
$lessonModule,
$isStudent
);
return new HttpOk($data);
}
/**
* @Route(
* path = "/admin/v2/course/{id}/modules",
* methods = {"GET"},
* requirements = { "id" = "\d+" }
* )
*
* @throws NotFoundException
* @throws ActionInvalidException
*/
public function getModules(
Request $request,
CourseIndexService $courseIndexService,
LessonModuleService $lessonModuleService
): JsonResponse
{
$courseId = $request->get('id');
$course = $courseIndexService->searchCourse($courseId);
if(!$course){
throw new NotFoundException(
$this->configuration->getLanguage('error_course_not_found', 'lesson_view_error')
);
}
$enrollment = $courseIndexService->searchEnrollment($course);
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_enrollment_not_found',
'lesson_view_error'
)
);
}
$isStudent = $courseIndexService->isStudent($course);
if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_course_not_published',
'lesson_view_error'
)
);
}
$searchText = $request->get('search');
$data = $lessonModuleService->getModulesIndex(
$enrollment,
$isStudent,
$searchText
);
return new HttpOk($data);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}",
* methods = {"GET"},
* name = "getViewLessonNew",
* requirements = { "id" = "\d+" }
* )
*
* @throws NotFoundException
* @throws ActionInvalidException
*/
public function getViewLessonNew(
Request $request,
CourseIndexService $courseIndexService,
LessonService $lessonService,
LibraryChapterService $chapterService
): JsonResponse
{
$lessonId = $request->get('id');
$chatVersion = (int)$request->get('chatVersion');
$lesson = $lessonService->searchLesson($lessonId);
if (!$lesson) {
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
$course = $lesson->getCourse();
if(
$courseIndexService->isEnrollment($course) &&
!$courseIndexService->isValidEnrollment($course)
){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_enrollment_expired', 'lesson_view_error'),
[
"expired" => true,
]
);
}
//check user can access lesson
$isStudent = $courseIndexService->isStudent($course);
if(!$lessonService->isLessonVisibleToStudent($lesson, $isStudent)){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error')
);
}
$enrollment = $courseIndexService->searchEnrollment($course);
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_enrollment_not_found',
'lesson_view_error'
)
);
}
$lessonIsAccessible = $lessonService->checkLessonIsAccessibleToUser(
$lesson,
$enrollment,
$isStudent
);
if(!$lessonIsAccessible->isAccessible){
throw new ActionInvalidException($lessonIsAccessible->message);
}
$data = $lessonService->getLessonIndex(
$lesson,
$enrollment,
$isStudent,
true
);
return new HttpOk($data);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}/content",
* methods = {"GET"},
* name = "getViewLessonNewContents",
* requirements = { "id" = "\d+" }
* )
*
* @throws NotFoundException
* @throws ActionInvalidException
*/
public function getViewLessonNewContents(
Request $request,
CourseIndexService $courseIndexService,
LessonService $lessonService,
LibraryChapterService $chapterService
): JsonResponse
{
$lessonId = $request->get('id');
$page = (int)$request->get('page');
$lesson = $lessonService->searchLesson($lessonId);
if (!$lesson) {
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
if($lesson->getType() != LessonEnum::CONTENT_MULTI){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error')
);
}
$course = $lesson->getCourse();
if(
$courseIndexService->isEnrollment($course) &&
!$courseIndexService->isValidEnrollment($course)
){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_enrollment_expired', 'lesson_view_error'),
[
"expired" => true,
]
);
}
//check user can access lesson
$isStudent = $courseIndexService->isStudent($course);
if(!$lessonService->isLessonVisibleToStudent($lesson, $isStudent)){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error')
);
}
$enrollment = $courseIndexService->searchEnrollment($course);
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_enrollment_not_found',
'lesson_view_error'
)
);
}
$lessonIsAccessible = $lessonService->checkLessonIsAccessibleToUser(
$lesson,
$enrollment,
$isStudent
);
if(!$lessonIsAccessible->isAccessible){
throw new ActionInvalidException($lessonIsAccessible->message);
}
$data = $lessonService->getLibrariesIndex(
$lesson,
$enrollment,
$isStudent,
$page
);
return new HttpOk($data);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}/content/{libraryId}",
* methods = {"GET"},
* name = "getOneViewLessonNewContent",
* requirements = { "id" = "\d+" }
* )
*
* @throws NotFoundException
* @throws ActionInvalidException
*/
public function getOneViewLessonNewContent(
Request $request,
CourseIndexService $courseIndexService,
LessonService $lessonService,
LibraryChapterService $chapterService
): JsonResponse
{
$lessonId = $request->get('id');
$libraryId = $request->get('libraryId');
$lesson = $lessonService->searchLesson($lessonId);
if (!$lesson) {
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
$course = $lesson->getCourse();
if(
$courseIndexService->isEnrollment($course) &&
!$courseIndexService->isValidEnrollment($course)
){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_enrollment_expired', 'lesson_view_error'),
[
"expired" => true,
]
);
}
//check user can access lesson
$isStudent = $courseIndexService->isStudent($course);
if(!$lessonService->isLessonVisibleToStudent($lesson, $isStudent)){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error')
);
}
$enrollment = $courseIndexService->searchEnrollment($course);
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_enrollment_not_found',
'lesson_view_error'
)
);
}
$lessonIsAccessible = $lessonService->checkLessonIsAccessibleToUser(
$lesson,
$enrollment,
$isStudent
);
if(!$lessonIsAccessible->isAccessible){
throw new ActionInvalidException($lessonIsAccessible->message);
}
$library = $lessonService->getLibrary($libraryId);
if(!$library){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error')
);
}
$data = $lessonService->getLibraryIndex(
$lesson,
$enrollment,
$isStudent,
true,
$library
);
return new HttpOk((array)$data);
}
/**
* @Route(
* path = "/admin/v2/{module}/{id}/{type}",
* methods = {"GET"},
* name = "getExamNewIndex",
* requirements = {"type"="exam|quiz"}
* )
*/
public function getExamNewIndex(Request $request)
{
$idModule = (int)$request->get('id');
$module = $request->get('module');
$type = $request->get('type');
if($type != 'quiz'){
if(!$this->configuration->isModuleActive("exam_module")){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_exam_unavailable', 'lesson_view_error')
);
}
}
$class = [
"lesson" => Lesson::class,
"module" => LessonModule::class,
"course" => Course::class,
];
if(!isset($class[$module])){
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$itemRepository = $this->em->getRepository($class[$module]);
$item = $itemRepository->findOneBy([
"id" => $idModule,
"deleted" => CourseEnum::ITEM_NO_DELETED
]);
//check item exist
if(!$item){
//redirect to index or home
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$course = $item;
if(!($course instanceof Course)){
$course = $item->getCourse();
}
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollment = $enrollmentRepository->findOneBy([
"user" => $this->user->getId(),
"course" => $course->getId(),
"deleted" => LessonEnum::ITEM_NO_DELETED
], [ "id" => "DESC" ]);
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_enrollment_not_found', 'lesson_view_error')
);
}
$isStudent = $this->repository->isStudent($course);
$filterExam = [
"course" => $course->getId(),
"deleted" => ExamEnum::ITEM_NO_DELETED
];
if($item instanceof Course){
$filterExam["type"] = ExamEnum::COURSE;
}else if($item instanceof LessonModule){
$filterExam["lessonModule"] = $item->getId();
$filterExam["type"] = ExamEnum::MODULE;
}else if($item instanceof Lesson){
//check user can access lesson
if(!$itemRepository->isLessonVisibleToStudent($item, false, $isStudent)){
if($type == 'quiz'){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_quiz_unavailable',
'lesson_view_error'
)
);
}
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error')
);
}
$lessonModule = $item->getLessonModule();
$filterExam["lesson"] = $item->getId();
$filterExam["lessonModule"] = $lessonModule->getId();
$filterExam["type"] = ($type == "quiz" ? ExamEnum::QUIZ : ExamEnum::LESSON );
}
if($isStudent){
$filterExam["status"] = ExamEnum::PUBLISHED;
}
$examRepository = $this->em->getRepository(Exam::class);
$examUserRepository = $this->em->getRepository(ExamUser::class);
$exam = $examRepository->findOneBy($filterExam);
if(!$exam){
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$examUser = $examUserRepository->findOneBy([
"user" => $this->user->getId(),
"exam" => $exam->getId(),
"inactive" => ExamUserEnum::NO,
"deleted" => ExamUserEnum::ITEM_NO_DELETED
]);
$infoAccess = $examRepository->checkExamIsAccessible($exam, $examUser);
if(!$infoAccess->isAccessible){
throw new ActionInvalidException($infoAccess->message);
}
$data = $examUserRepository->getDataIndexNew($exam, $enrollment);
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/{module}/{id}/exam/start",
* methods = {"PATCH"},
* name = "startExamNewIndex",
* requirements = { "id" = "\d+" }
* )
*/
public function startExamNewIndex(Request $request)
{
if(!$this->configuration->isModuleActive("exam_module")){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_exam_unavailable', 'lesson_view_error')
);
}
$idModule = (int)$request->get('id');
$module = $request->get('module');
$class = [
"lesson" => Lesson::class,
"module" => LessonModule::class,
"course" => Course::class,
];
if(!isset($class[$module])){
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$itemRepository = $this->em->getRepository($class[$module]);
$item = $itemRepository->findOneBy([
"id" => $idModule,
"deleted" => CourseEnum::ITEM_NO_DELETED
]);
//check item exist
if(!$item){
//redirect to index or home
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$course = $item;
if(!($course instanceof Course)){
$course = $item->getCourse();
}
$user = $this->user;
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollment = $enrollmentRepository->findOneBy([
"user" => $user->getId(),
"course" => $course->getId(),
"deleted" => LessonEnum::ITEM_NO_DELETED
], [ "id" => "DESC" ]);
if(!$enrollment){
throw new ActionInvalidException($this->configuration->getLanguage('error_enrollment_not_found', 'lesson_view_error'));
}
$filterExam = [
"course" => $course->getId(),
"deleted" => ExamEnum::ITEM_NO_DELETED
];
$isStudent = $this->repository->isStudent($course);
if($item instanceof Course){
$filterExam["type"] = ExamEnum::COURSE;
}else if($item instanceof LessonModule){
$filterExam["lessonModule"] = $item->getId();
$filterExam["type"] = ExamEnum::MODULE;
}else if($item instanceof Lesson){
//check user can access lesson
if(!$itemRepository->isLessonVisibleToStudent($item, false, $isStudent)){
throw new ActionInvalidException($this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error'));
}
$lessonModule = $item->getLessonModule();
$filterExam["lesson"] = $item->getId();
$filterExam["lessonModule"] = $lessonModule->getId();
$filterExam["type"] = ExamEnum::LESSON;
}
if($isStudent){
$filterExam["status"] = ExamEnum::PUBLISHED;
}
$examRepository = $this->em->getRepository(Exam::class);
$exam = $examRepository->findOneBy($filterExam);
if(!$exam){
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$examUserRepository = $this->em->getRepository(ExamUser::class);
$examUser = $examUserRepository->findOneBy([
"user" => $user->getId(),
"exam" => $exam->getId(),
"inactive" => ExamUserEnum::NO,
"deleted" => ExamUserEnum::ITEM_NO_DELETED
]);
$infoAccess = $examRepository->checkExamIsAccessible($exam, $examUser);
if(!$infoAccess->isAccessible){
throw new ActionInvalidException($infoAccess->message);
}
if($examUser){
if(
$examUser->getStatus() == ExamUserEnum::DISAPPROVED ||
$examUser->getStatus() == ExamUserEnum::TIMEOUT
){
$examUser = $examUserRepository->getValidExamUserById(
$examUser->getId(),
true
);
$attemptsInfo = $examUserRepository->getAttemptsInfo($exam, $examUser);
if($attemptsInfo->attempts == ExamEnum::YES){
$examUser->setInactive(ExamUserEnum::YES);
$this->em->flush();
$examUser = null;
}
}
}
if(!$examUser){
$examUser = new ExamUser();
$examUser->setExam($exam);
$examUser->setCourse($exam->getCourse());
$examUser->setUser($user);
$this->em->persist($examUser);
$this->em->flush();
}else{
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$data = $examUserRepository->getDataIndexNew($exam, $enrollment);
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/{module}/{id}/exam/{questionId}",
* methods = {"POST"},
* )
*/
public function registerAnswerExamQuestion(
Request $request,
ExamUserAnswerService $examUserAnswerService
) {
if(!$this->configuration->isModuleActive("exam_module")){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_exam_unavailable', 'lesson_view_error')
);
}
$this->requestUtil->setRequest($request)->setData();
$idModule = (int)$request->get('id');
$module = $request->get('module');
$questionId = (int)$request->get('questionId');
$class = [
"lesson" => Lesson::class,
"module" => LessonModule::class,
"course" => Course::class,
];
if(!isset($class[$module])){
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$examUserAnswerRepository = $this->em->getRepository(ExamUserAnswer::class);
$examUserRepository = $this->em->getRepository(ExamUser::class);
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$examRepository = $this->em->getRepository(Exam::class);
$questionRepository = $this->em->getRepository(Question::class);
$questionOptionRepository = $this->em->getRepository(QuestionOption::class);
$itemRepository = $this->em->getRepository($class[$module]);
$item = $itemRepository->findOneBy([
"id" => $idModule,
"deleted" => CourseEnum::ITEM_NO_DELETED
]);
//check item exist
if(!$item){
//redirect to index or home
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$course = $item;
if(!($course instanceof Course)){
$course = $item->getCourse();
}
$user = $this->user;
$enrollment = $enrollmentRepository->findOneBy([
"user" => $user->getId(),
"course" => $course->getId(),
"deleted" => LessonEnum::ITEM_NO_DELETED
], [ "id" => "DESC" ]);
if(!$enrollment){
throw new ActionInvalidException($this->configuration->getLanguage('error_enrollment_not_found', 'lesson_view_error'));
}
$filterExam = [
"course" => $course->getId(),
"deleted" => ExamEnum::ITEM_NO_DELETED
];
$isStudent = $this->repository->isStudent($course);
if($item instanceof Course){
$filterExam["type"] = ExamEnum::COURSE;
}else if($item instanceof LessonModule){
$filterExam["lessonModule"] = $item->getId();
$filterExam["type"] = ExamEnum::MODULE;
}else if($item instanceof Lesson){
//check user can access lesson
if(!$itemRepository->isLessonVisibleToStudent($item, false, $isStudent)){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_lesson_unavailable',
'lesson_view_error'
)
);
}
$lessonModule = $item->getLessonModule();
$filterExam["lesson"] = $item->getId();
$filterExam["lessonModule"] = $lessonModule->getId();
$filterExam["type"] = ExamEnum::LESSON;
}
if($isStudent){
$filterExam["status"] = ExamEnum::PUBLISHED;
}
$exam = $examRepository->findOneBy($filterExam);
if(!$exam){
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$question = $questionRepository->findOneBy([
"id" => $questionId,
"deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED
]);
if(!$question){
throw new NotFoundException(
$this->configuration->getLanguage('error_question_not_found', 'lesson_view_error')
);
}
$examUser = $examUserRepository->findOneBy([
"user" => $user->getId(),
"exam" => $exam->getId(),
"inactive" => ExamUserEnum::NO,
"deleted" => ExamUserEnum::ITEM_NO_DELETED
]);
$examUser = $examUserRepository->getValidExamUserById(null, true, $examUser);
if (!$examUser) {
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_not_found', 'lesson_view_error')
);
}
$expired = $examUserRepository->examUserIsExpired($examUser);
if($expired){
throw new NotFoundException(
$this->configuration->getLanguage('error_exam_expired', 'lesson_view_error')
);
}
//check exist answer to this question
$questionAnswer = $examUserAnswerRepository->findOneBy([
"deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
"examUser" => $examUser->getId(),
"question" => $questionId,
]);
if($questionAnswer){
throw new NotFoundException(
$this->configuration->getLanguage(
'error_answer_question_not_found',
'lesson_view_error'
)
);
}
$answer = $this->requestUtil->getField('answer');
if(empty($answer)){
throw new FieldException(
"FieldException",
[
"answer" => "Value not found",
]
);
}
$questionAnswer = new ExamUserAnswer();
$questionAnswer->setExamUser($examUser);
$questionAnswer->setAnswered(QuestionEnum::YES);
$questionAnswer->setQuestion($question);
if($question->getType() == QuestionEnum::DISSERTATION){
$questionAnswer->setAnswer($answer);
}
$errors = $this->validateEntity($questionAnswer);
if($errors){
throw new FieldException("FieldException", $errors);
}
$this->em->persist($questionAnswer);
$grade = ExamUserAnswerEnum::GRADE_INCORRECT;
if($question->getType() != QuestionEnum::DISSERTATION){
$questionAnswer->setEvaluated((int)$question->getAnswered());
$optionsCorrect = $questionOptionRepository->findBy([
"question" => $question->getId(),
"correct" => QuestionEnum::YES,
"deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
]);
$options = json_decode($this->requestUtil->getField('answer'));
foreach ($options as $key => $option) {
$questionOption = $questionOptionRepository->findOneBy([
"id" => $option,
"question" => $question->getId(),
"deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
]);
if($questionOption){
if($questionOption->getCorrect() == QuestionEnum::YES){
$questionAnswer->setEvaluated(QuestionEnum::YES);
$question->setAnswered(QuestionEnum::YES);
if($question->getType() == QuestionEnum::ALTERNATIVE){
$grade = ExamUserAnswerEnum::GRADE_CORRECT;
}
}
$questionAnswerOption = new ExamUserAnswerOption();
$questionAnswerOption->setMarked(QuestionEnum::YES);
$questionAnswerOption->setExamUserAnswer($questionAnswer);
$questionAnswerOption->setQuestionOption($questionOption);
$this->em->persist($questionAnswerOption);
}
}
if($question->getType() == QuestionEnum::MULTIPLE_ALTERNATIVE){
$grade = $examUserAnswerService->correctMultipleAlternatives(
$options,
$optionsCorrect
);
}
}else if(
$this->configuration->checkModuleIsAbleOnPlan('iadFunction') &&
$exam->getAutoCorrectTextQuestion() == ExamEnum::YES
){
$gpt = $this->generalService->getService('ChatGPTService');
$category = $course->getCategory();
$categoryTitle = ($category ? $category->getCategory() : null);
$respGPT = $gpt->answerExamQuestion(
$question->getQuestion(),
$answer,
$course->getTitle(),
$categoryTitle
);
if(isset($respGPT->grade) && isset($respGPT->description)){
$grade = $respGPT->grade;
$questionAnswer->setNote($respGPT->description);
$questionAnswer->setEvaluated(QuestionEnum::YES);
}
}
$questionAnswer->setGrade($grade);
$this->em->flush();
sleep(0.5);
$questionNumberAnswer = $examUserAnswerRepository->count([
"examUser" => $examUser->getId(),
"deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
"answered" => QuestionEnum::YES
]);
$questionNumber = $exam->getQuestionNumber();
$questionNumberReal = $exam->getQuestionNumberReal();
if($questionNumber > $questionNumberReal){
$questionNumber = $questionNumberReal;
}
if($exam->getQuestionOrder() == ExamEnum::SEQUENTIAL){
$questionNumber = count($exam->getLiveQuestion());
}
if($questionNumber <= $questionNumberAnswer){
$examUserRepository->updateExamUser($examUser);
}
$data = $questionAnswer->toReturn();
$this->userLogService->logInsert("exam_user_answer", $questionAnswer->getId(), $data);
$data = $examUserRepository->getDataIndexNew($exam, $enrollment);
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/{module}/{id}/exam/outside",
* methods = {"PATCH"},
* name = "outsideExam",
* requirements = { "id" = "\d+" }
* )
*/
public function outsideExam(Request $request)
{
if(!$this->configuration->isModuleActive("exam_module")){
throw new ActionInvalidException("ActionInvalidException");
}
$idModule = (int)$request->get('id');
$module = $request->get('module');
$class = [
"lesson" => Lesson::class,
"module" => LessonModule::class,
"course" => Course::class,
];
if(!isset($class[$module])){
throw new NotFoundException("NotFoundException");
}
$itemRepository = $this->em->getRepository($class[$module]);
$item = $itemRepository->findOneBy([
"id" => $idModule,
"deleted" => CourseEnum::ITEM_NO_DELETED
]);
//check item exist
if(!$item){
//redirect to index or home
throw new NotFoundException("NotFoundException");
}
$course = $item;
if(!($course instanceof Course)){
$course = $item->getCourse();
}
$user = $this->user;
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollment = $enrollmentRepository->findOneBy([
"user" => $user->getId(),
"course" => $course->getId(),
"deleted" => LessonEnum::ITEM_NO_DELETED
], [ "id" => "DESC" ]);
if(!$enrollment){
throw new ActionInvalidException("ActionInvalidException");
}
$filterExam = [
"course" => $course->getId(),
"deleted" => ExamEnum::ITEM_NO_DELETED
];
$isStudent = $this->repository->isStudent($course);
if($item instanceof Course){
$filterExam["type"] = ExamEnum::COURSE;
}else if($item instanceof LessonModule){
$filterExam["lessonModule"] = $item->getId();
$filterExam["type"] = ExamEnum::MODULE;
}else if($item instanceof Lesson){
//check user can access lesson
if(!$itemRepository->isLessonVisibleToStudent($item, false, $isStudent)){
throw new ActionInvalidException("ActionInvalidException");
}
$lessonModule = $item->getLessonModule();
$filterExam["lesson"] = $item->getId();
$filterExam["lessonModule"] = $lessonModule->getId();
$filterExam["type"] = ExamEnum::LESSON;
}
$exam = $this->em->getRepository(Exam::class)->findOneBy($filterExam);
if(!$exam){
throw new NotFoundException("NotFoundException");
}
if($exam->getControlTime() == ExamEnum::NO){
throw new NotFoundException("ActionInvalidException");
}
$examUserRepository = $this->em->getRepository(ExamUser::class);
$examUser = $examUserRepository->findOneBy([
"user" => $user->getId(),
"exam" => $exam->getId(),
"inactive" => ExamUserEnum::NO,
"deleted" => ExamUserEnum::ITEM_NO_DELETED
]);
if(!$examUser){
throw new ActionInvalidException("ActionInvalidException");
}
if($examUser->getStatus() !== ExamUserEnum::IN_PROGRESS){
throw new ActionInvalidException("ActionInvalidException");
}
$timeUtil = $this->generalService->getUtil('DateTimeUtil');
$examUser = $examUserRepository->getValidExamUserById(
$examUser->getId(),
true
);
$this->requestUtil->setRequest($request)->setData();
$outsideNumber = $examUser->getOutsideNumber();
$newOutsideNumber = $outsideNumber + 1;
$examUser->setOutsideNumber($newOutsideNumber);
$outsideTime = '00:00:00';
if(!empty($examUser->getOutsideTime())){
$outsideTime = $examUser->getOutsideTime();
}
if($this->requestUtil->issetField('outsideTime')){
$newTime = $this->requestUtil->getField('outsideTime');
$newTime = $timeUtil->timeToSec($newTime);
$oldTime = $timeUtil->timeToSec($outsideTime);
$sumTime = $oldTime + $newTime;
$time = $timeUtil->secToTime($sumTime);
$examUser->setOutsideTime($time);
}
$errors = $this->validateEntity($examUser);
if($errors){
throw new FieldException("FieldException", $errors);
}
$this->em->flush();
return $this->eadResponseNew([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}/quiz/{questionId}",
* methods = {"POST"}
* )
*/
public function getQuestionQuizAnswer(Request $request) {
$questionRepository = $this->em->getRepository(Question::class);
$questionOptionRepository = $this->em->getRepository(QuestionOption::class);
$libraryRepository = $this->em->getRepository(Library::class);
$this->requestUtil->setRequest($request)->setData();
$questionId = (int)$request->get('questionId');
$lessonId = (int)$request->get('id');
$lessonRepository = $this->em->getRepository(Lesson::class);
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
//check item exist
if(!$lesson){
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
$question = $questionRepository->findOneBy([
"id" => $questionId,
"deleted" => QuestionEnum::ITEM_NO_DELETED
]);
if(!$question){
throw new NotFoundException(
$this->configuration->getLanguage('error_question_not_found', 'lesson_view_error')
);
}
$optionCorrect = $questionOptionRepository->findOneBy([
"deleted" => QuestionOptionEnum::ITEM_NO_DELETED,
"question" => $questionId,
"correct" => QuestionOptionEnum::YES,
]);
$libraryReturn = null;
$library = $question->getLibrary();
if($library){
$libraryInfo = $libraryRepository->getContentInfo($library, false, $lesson);
$libraryReturn = (object)[
"id" => $library->getId(),
"title" => $library->getTitle(),
"type" => $library->getType(),
"link" => $libraryInfo->url,
"text" => $library->getText(),
"file" => $library->getId(),
"fileExtension" => $library->getFileExtension(),
"liveStart" => $library->getLiveStart(),
"liveEmbedId" => $libraryInfo->embedId,
"pagesNumber" => $library->getPagesNumber(),
"duration" => $library->getDuration(),
];
$showDocument = ($lesson->getControlShowDocument() == LibraryEnum::YES);
$drmVideo = $this->configuration->get("drm_video");
$hasLessonControl = $this->configuration->checkModuleIsAbleOnPlan(
'lessonControlFunction'
);
$annotate = [];
if($hasLessonControl && $showDocument && $drmVideo == LibraryEnum::YES){
$annotate = $libraryRepository->getVideoDRM($this->user);
}
$credentials = $libraryRepository->getVideoCredentials(
$library,
$annotate
);
if($credentials){
$libraryReturn->credentials = $credentials;
}
}
$data = [
"library" => $libraryReturn,
"optionCorrect" => ( $optionCorrect ? $optionCorrect->getId() : null ),
];
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/lesson/{lessonId}/end/live",
* methods = {"PATCH"}
* )
*/
public function liveLessonEnd(Request $request) {
$lessonRepository = $this->em->getRepository(Lesson::class);
$lessonId = $request->get('lessonId');
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
//check lesson exist
if (!$lesson) {
//redirect to index or home
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
$isStudent = $this->repository->isStudent($lesson->getCourse());
//check user can access lesson
if($isStudent){
throw new ActionInvalidException('ActionInvalidException');
}
$library = $lesson->getLibrary();
if(!$library){
//redirect to index or home
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
if(
$library->getType() != LibraryEnum::CONTENT_NEW_LIVE ||
empty($library->getLiveIdEvent())
){
throw new ActionInvalidException('ActionInvalidException');
}
$vdocipherService = $this->generalService->getService('VdocipherService');
$vdocipherService->endLive($library->getLiveIdEvent());
$library->setType(LibraryEnum::CONTENT_VIDEO_FILE);
$library->setLiveEnd(date('Y-m-d H:i:s'));
$library->setLiveEndUser($this->user);
$this->em->flush();
return new HttpNoContent;
}
/**
* @Route(
* path = "/admin/v2/lesson/{lessonId}/start/record",
* methods = {"PATCH"}
* )
*/
public function meetLessonStart(Request $request) {
$lessonRepository = $this->em->getRepository(Lesson::class);
$lessonId = $request->get('lessonId');
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
//check lesson exist
if (!$lesson) {
//redirect to index or home
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
$isStudent = $this->repository->isStudent($lesson->getCourse());
//check user can access lesson
if($isStudent){
throw new ActionInvalidException('ActionInvalidException');
}
$library = $lesson->getLibrary();
if(!$library){
//redirect to index or home
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
if($library->getRecord() == LibraryEnum::YES){
throw new ActionInvalidException('ActionInvalidException');
}
$library->setRecord(LibraryEnum::YES);
$library->setRecordUser($this->user);
$this->em->flush();
return new HttpNoContent;
}
/**
* @Route(
* path = "/admin/v2/lesson/{lessonId}/end/record",
* methods = {"PATCH"}
* )
*/
public function meetLessonEnd(Request $request) {
$lessonRepository = $this->em->getRepository(Lesson::class);
$lessonId = $request->get('lessonId');
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
//check lesson exist
if (!$lesson) {
//redirect to index or home
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
$isStudent = $this->repository->isStudent($lesson->getCourse());
//check user can access lesson
if($isStudent){
throw new ActionInvalidException('ActionInvalidException');
}
$library = $lesson->getLibrary();
if(!$library){
//redirect to index or home
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
if($library->getRecord() == LibraryEnum::NO){
throw new ActionInvalidException('ActionInvalidException');
}
$userRecord = $library->getRecordUser();
if(!$userRecord){
throw new ActionInvalidException('ActionInvalidException');
}
if($this->user->getId() != $userRecord->getId()){
throw new ActionInvalidException('ActionInvalidException');
}
$library->setRecord(LibraryEnum::NO);
$library->setRecordUser(null);
$this->em->flush();
return new HttpNoContent;
}
/**
* @Route(
* path = "/admin/v2/lesson/{lessonId}/files",
* methods = {"GET"}
* )
*/
public function getLessonFiles(Request $request) {
$lessonRepository = $this->em->getRepository(Lesson::class);
$lessonXLibraryRepository = $this->em->getRepository(LessonXLibrary::class);
$lessonId = $request->get('lessonId');
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
//check lesson exist
if (!$lesson) {
//redirect to index or home
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
//check user can access lesson
$isStudent = $this->repository->isStudent($lesson->getCourse());
if(!$lessonRepository->isLessonVisibleToStudent($lesson, false, $isStudent)){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_lesson_unavailable',
'lesson_view_error'
)
);
}
$files = $lessonXLibraryRepository->getLessonXLibraryFiles($lesson);
$data = [];
foreach ($files as $key => $file) {
$file = $file->toReturn();
$data[] = (object)[
"name" => $file->libraryTitle,
"size" => $file->libraryFileSize,
"type" => $file->libraryType,
"filename" => $file->libraryFile,
"id" => $file->id,
];
}
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/lesson/{lessonId}/files/{id}",
* methods = {"GET"},
* name = "downloadLessonLibraryNew",
* requirements = { "id" = "\d+" }
* )
*/
public function downloadLessonLibraryNew(Request $request) {
$lessonXLibraryId = $request->get('id');
$lessonXLibrary = $this->em->getRepository(LessonXLibrary::class)->findOneBy([
"id" => $lessonXLibraryId,
"type" => LessonXLibraryEnum::DOWNLOAD,
"deleted" => LessonXLibraryEnum::ITEM_NO_DELETED,
]);
if (!$lessonXLibrary) {
throw new NotFoundException($this->configuration->getLanguage('error_library_lesson_not_found', 'lesson_view_error'));
}
$library = $lessonXLibrary->getLibrary();
$lesson = $lessonXLibrary->getLesson();
$lessonRepository = $this->em->getRepository(Lesson::class);
//check user can access lesson
$isStudent = $this->repository->isStudent($lesson->getCourse());
if(!$lessonRepository->isLessonVisibleToStudent($lesson, false, $isStudent)){
throw new ActionInvalidException($this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error'));
}
if($library){
$url = null;
$type = $library->getType();
if($type == LibraryEnum::CONTENT_VIDEO_FILE ||
$type == LibraryEnum::CONTENT_AUDIO){
$vdocipherService = $this->generalService->getService('VdocipherService');
$url = $vdocipherService->getVideoDownloadLink(
$library->getVdocipherVideoId()
);
}else if($type == LibraryEnum::CONTENT_FILES){
$isPdf = ($library->getFileExtension() == "pdf");
$hasFunction = $this->configuration->checkModuleIsAbleOnPlan(
'lessonControlFunction'
);
$showDocument = (
$lessonXLibrary->getControlShowDocument() == LibraryEnum::YES
);
if($isPdf && $hasFunction && $showDocument){
$stringUtil = $this->generalService->getUtil('StringUtil');
$sessionHash = $this->user->getSession()->getToken();
$sessionHash = $stringUtil->encodeHex($sessionHash, false, false);
$path = $this->generalService->generateUrl(
"downloadPdfLessonLibrary",
[
"id" => $lessonXLibraryId,
"sessionHash" => $sessionHash
]
);
$domain = (
$request ? $request->getHost() : $this->client->getDomainPrimary()
);
$url = "https://{$domain}{$path}";
}else{
$fileName = LibraryEnum::PATH_LESSON_FILE . "/{$library->getFileName()}";
$this->fileService->setFile($fileName);
$url = $this->fileService->getFileUrlTemp();
}
}
if($url){
$oldDownload = $library->getDownloadNumber();
$newDownload = $oldDownload + 1;
$library->setDownloadNumber($newDownload);
$this->em->flush();
return $this->eadResponseNew([ "url" => $url ]);
}
}
throw new NotFoundException(
$this->configuration->getLanguage('error_library_lesson_not_found', 'lesson_view_error')
);
}
/**
* @Route(
* path = "/admin/v2/course/{courseId}/notes",
* name = "getCourseLessonNotesNew",
* methods = {"GET"},
* )
*/
public function getCourseLessonNotesNew(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$courseId = $request->get('courseId');
$course = $this->repository->findOneBy([
"id" => $courseId,
"deleted" => CourseEnum::ITEM_NO_DELETED
]);
if(!$course){
throw new NotFoundException(
$this->configuration->getLanguage('error_course_not_found', 'lesson_view_error')
);
}
$isInTeam = $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
$course,
$this->user
);
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollment = $enrollmentRepository->findOneBy([
"user" => $this->user->getId(),
"course" => $courseId,
"deleted" => EnrollmentEnum::ITEM_NO_DELETED,
], [ "id" => "DESC" ]);
$permission = $this->userPermissionUtil->getPermission("course", "see");
if(!$enrollment){
if($course->getFree() == CourseEnum::NO){
if($this->userPermissionUtil->isLow($permission)){
throw new PermissionException($this->configuration->getLanguage('error_user_not_permission', 'lesson_view_error'));
}
if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
throw new PermissionException($this->configuration->getLanguage('error_user_not_permission', 'lesson_view_error'));
}
}
$enrollmentService = $this->generalService->getService('EnrollmentService');
$info = $enrollmentService->enrollUser($this->user, $course);
if(!$info->errors){
$enrollment = $info->enrollment;
}
}
if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
if(!$enrollment){
throw new ActionInvalidException($this->configuration->getLanguage('error_enrollment_not_found', 'lesson_view_error'));
}
if($course->getStatus() == CourseEnum::DRAFT){
throw new ActionInvalidException($this->configuration->getLanguage('error_course_not_published', 'lesson_view_error'));
}
}
$searchText = $this->requestUtil->getField('search');
$lessonAnnotationRepository = $this->em->getRepository(LessonAnnotation::class);
$notes = $lessonAnnotationRepository->getLessonAnnotationsByCourse(
$course,
$this->user,
$searchText
);
$aux = [];
$auxLessons = [];
foreach ($notes as $key => $note) {
$lesson = $note->getLesson();
$lessonModule = $lesson->getLessonModule();
if(!isset($aux[$lessonModule->getId()])){
$aux[$lessonModule->getId()] = (object)[
"id" => $lessonModule->getId(),
"status" => $lessonModule->getStatus(),
"title" => $lessonModule->getTitle(),
"description" => $lessonModule->getDescription(),
"exam" => null,
"lessons" => [],
];
}
$library = $lesson->getLibrary();
$libraryRepository = $this->em->getRepository(Library::class);
$timeUtil = $this->generalService->getUtil('DateTimeUtil');
$duration = $library->getDuration();
if(!isset($aux[$lesson->getId()])){
$auxLessons[$lesson->getId()] = (object)[
"id" => $lesson->getId(),
"lessonModule" => $lessonModule->getId(),
"title" => $lesson->getTitle(),
"status" => $lesson->getStatus(),
"lessonIsAccessible" => true,
"acessMessage" => null,
"contentPagesNumber" => null,
"contentDuration" => $duration ? $timeUtil->timeToSec($duration) : null,
"contentType" => null,
"contentThumb" => $libraryRepository->getCover($library),
"notes" => [],
"exam" => null,
"quiz" => null,
"allowCheck" => EnrollmentEnum::YES,
];
}
$auxLessons[$lesson->getId()]->notes[] = (object)[
"id" => $note->getId(),
"annotation" => $note->getAnnotation(),
"options" => $note->getOptions(),
"time" => $note->getTime(),
"date" => $note->getDate(),
];
}
$data = [];
foreach ($auxLessons as $key => $value) {
if(isset($aux[$value->lessonModule])){
$aux[$value->lessonModule]->lessons[] = $value;
unset($value->lessonModule);
}
}
foreach ($aux as $key => $value) {
$data[] = $value;
}
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/lesson/{lessonId}/notes",
* name = "lessonAnnotationListNew",
* methods = {"GET"},
* )
*/
public function lessonAnnotationListNew(Request $request) {
$lessonId = $request->get('lessonId');
$lessonRepository = $this->em->getRepository(Lesson::class);
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
//check lesson exist
if (!$lesson) {
//redirect to index or home
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
//check user can access lesson
$isStudent = $this->repository->isStudent($lesson->getCourse());
if(!$lessonRepository->isLessonVisibleToStudent($lesson, false, $isStudent)){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_lesson_unavailable',
'lesson_view_error'
)
);
}
$lessonAnnotationRepository = $this->em->getRepository(LessonAnnotation::class);
$annotations = $lessonAnnotationRepository->findBy([
"deleted" => LessonAnnotationEnum::ITEM_NO_DELETED,
"lesson" => $lessonId,
"user" => $this->user->getId(),
], [ "id" => "DESC" ]);
$data = [];
foreach ($annotations as $key => $annotation) {
$data[] = (object)[
"id" => $annotation->getId(),
"annotation" => $annotation->getAnnotation(),
"options" => $annotation->getOptions(),
"time" => $annotation->getTime(),
"date" => $annotation->getDate(),
];
}
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/lesson/{lessonId}/notes",
* name = "registerLessonAnnotationNew",
* methods = {"POST"},
* )
*/
public function registerLessonAnnotationNew(Request $request) {
$lessonId = $request->get('lessonId');
$lessonRepository = $this->em->getRepository(Lesson::class);
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
//check lesson exist
if (!$lesson) {
//redirect to index or home
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
//check user can access lesson
$isStudent = $this->repository->isStudent($lesson->getCourse());
if(!$lessonRepository->isLessonVisibleToStudent($lesson, false, $isStudent)){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_lesson_unavailable',
'lesson_view_error'
)
);
}
$this->requestUtil->setRequest($request)->setData();
$lessonAnnotation = new LessonAnnotation();
if($this->requestUtil->issetField('annotation')){
$lessonAnnotation->setAnnotation($this->requestUtil->getField('annotation'));
}
if($this->requestUtil->issetField('options')){
$lessonAnnotation->setOptions($this->requestUtil->getField('options'));
}
if($this->requestUtil->issetField('time')){
$lessonAnnotation->setTime($this->requestUtil->getField('time'));
}
$lessonAnnotation->setLesson($lesson);
$lessonAnnotation->setUser($this->user);
$errors = $this->validateEntity($lessonAnnotation);
if($errors){
throw new FieldException("FieldException", $errors);
}
$this->em->persist($lessonAnnotation);
$this->em->flush();
return $this->eadResponseNew((object)[
"id" => $lessonAnnotation->getId(),
"annotation" => $lessonAnnotation->getAnnotation(),
"options" => $lessonAnnotation->getOptions(),
"time" => $lessonAnnotation->getTime(),
"date" => $lessonAnnotation->getDate(),
]);
}
/**
* @Route(
* path = "/admin/v2/lesson/{lessonId}/notes/{id}",
* name = "lessonAnnotationEditNew",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function editLessonAnnotationNew(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$lessonId = $request->get('lessonId');
$lessonAnnotationId = $request->get('id');
$lessonRepository = $this->em->getRepository(Lesson::class);
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
//check lesson exist
if (!$lesson) {
//redirect to index or home
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
//check user can access lesson
$isStudent = $this->repository->isStudent($lesson->getCourse());
if(!$lessonRepository->isLessonVisibleToStudent($lesson, false, $isStudent)){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_lesson_unavailable',
'lesson_view_error'
)
);
}
$lessonAnnotationRepository = $this->em->getRepository(lessonAnnotation::class);
$lessonAnnotation = $lessonAnnotationRepository->findOneBy([
"id" => $lessonAnnotationId,
"deleted" => LessonAnnotationEnum::ITEM_NO_DELETED
]);
if (!$lessonAnnotation) {
throw new NotFoundException(
$this->configuration->getLanguage('error_annotation_not_found', 'lesson_view_error')
);
}
if($this->requestUtil->issetField('annotation')){
$lessonAnnotation->setAnnotation($this->requestUtil->getField('annotation'));
}
if($this->requestUtil->issetField('time')){
$lessonAnnotation->setTime($this->requestUtil->getField('time'));
}
if($this->requestUtil->issetField('options')){
$lessonAnnotation->setOptions($this->requestUtil->getField('options'));
}
$errors = $this->validateEntity($lessonAnnotation);
if($errors){
throw new FieldException("FieldException", $errors);
}
$this->em->flush();
return $this->eadResponseNew((object)[
"id" => $lessonAnnotation->getId(),
"annotation" => $lessonAnnotation->getAnnotation(),
"options" => $lessonAnnotation->getOptions(),
"time" => $lessonAnnotation->getTime(),
"date" => $lessonAnnotation->getDate(),
]);
}
/**
* @Route(
* path = "/admin/v2/lesson/{lessonId}/notes/{id}",
* name = "lessonAnnotationDeleteNew",
* methods = {"DELETE"},
* requirements = { "id" = "\d+" }
* )
*/
public function deleteLessonAnnotationNew(Request $request) {
$lessonId = $request->get('lessonId');
$lessonAnnotationId = $request->get('id');
$lessonRepository = $this->em->getRepository(Lesson::class);
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
//check lesson exist
if (!$lesson) {
//redirect to index or home
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
//check user can access lesson
$isStudent = $this->repository->isStudent($lesson->getCourse());
if(!$lessonRepository->isLessonVisibleToStudent($lesson, false, $isStudent)){
throw new ActionInvalidException($this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error'));
}
$lessonAnnotationRepository = $this->em->getRepository(lessonAnnotation::class);
$lessonAnnotation = $lessonAnnotationRepository->findOneBy([
"id" => $lessonAnnotationId,
"deleted" => LessonAnnotationEnum::ITEM_NO_DELETED
]);
if (!$lessonAnnotation) {
throw new NotFoundException(
$this->configuration->getLanguage('error_annotation_not_found', 'lesson_view_error')
);
}
$lessonAnnotation->delete();
$this->em->flush();
return $this->eadResponseNew([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}",
* methods = {"PATCH"},
* name = "updateLessonLogNew",
* requirements = { "id" = "\d+" }
* )
*/
public function updateLessonLogNew(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$lessonId = $request->get('id');
$lessonRepository = $this->em->getRepository(Lesson::class);
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
//check lesson exist
if (!$lesson) {
//redirect to index or home
throw new ActionInvalidException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
//check user can access lesson
$course = $lesson->getCourse();
$isStudent = $this->repository->isStudent($course);
if(!$lessonRepository->isLessonVisibleToStudent($lesson, false, $isStudent)){
throw new ActionInvalidException($this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error'));
}
$lessonModule = $lesson->getLessonModule();
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollment = $enrollmentRepository->findOneBy([
"user" => $this->user->getId(),
"course" => $course->getId(),
"deleted" => LessonEnum::ITEM_NO_DELETED
], [ "id" => "DESC" ]);
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_enrollment_not_found', 'lesson_view_error')
);
}
$lessonLogRepository = $this->emEadmin->getRepository(LessonLog::class);
$logId = "{$course->getId()}#{$this->user->getId()}#{$lesson->getId()}";
$lessonLog = $lessonLogRepository->find($logId);
$new = false;
if(!$lessonLog){
$new = true;
$lessonLog = new LessonLog();
$lessonLog->setId($logId);
$lessonLog->setCourse($course);
$lessonLog->setLesson($lesson);
$lessonLog->setUser($this->user);
}
if($this->requestUtil->issetField('timeWatch')){
$timeWatch = $this->requestUtil->getField('timeWatch');
$lessonLog = $lessonLogRepository->updateLessonLogNew(
$lessonLog,
$timeWatch
);
}
if($this->requestUtil->issetField('rate')){
$rate = (int)$this->requestUtil->getField('rate');
$lessonLog->setRate($rate);
}
if($this->requestUtil->issetField('favorite')){
$favorite = (int)$this->requestUtil->getField('favorite');
$lessonLog->setFavorite($favorite);
}
if($this->requestUtil->issetField('completed')){
$complete = (int)$this->requestUtil->getField('completed');
$today = date('Y-m-d H:i:s');
$lessonLog->setDateAccess($today);
$lessonLog->setViewed($complete);
if(empty($lessonLog->getDateConclusion()) && $complete == LessonEnum::YES){
$lessonLog->setComplete($complete);
$lessonLog->setDateConclusion($today);
}
}
if($new){
$this->emEadmin->persist($lessonLog);
}
$this->em->getRepository(LessonLogOrigin::class)->copyFromDynamo($lessonLog);
$enrollmentRepository->updateDataAccessLog($enrollment);
$this->emEadmin->flush();
$this->em->flush();
$infoLog = $lessonLogRepository->getTimeInfo($lesson, $lessonLog);
$hasLessonControl = $this->configuration->checkModuleIsAbleOnPlan(
'lessonControlFunction'
);
$data = [
"nextContent" => $lessonRepository->getLessonNextContent($lesson, $lessonLog),
"lastContent" => $lessonRepository->getLessonLastContent($lesson),
"id" => $lesson->getId(),
"moduleId" => $lessonModule->getId(),
"courseId" => $course->getId(),
"title" => $lesson->getTitle(),
"description" => $lesson->getDescription(),
"controlTime" => $hasLessonControl ? $lesson->getControlTime() : LessonEnum::NO,
"controlViewLimit" => (
$hasLessonControl ?
$lesson->getControlViewLimit() :
LessonEnum::NO
),
"controlViewNumber" => (
$hasLessonControl ?
$lesson->getControlViewNumber() :
LessonEnum::NO
),
"controlPauseNumber" => (
$hasLessonControl ?
$lesson->getControlPauseNumber() :
LessonEnum::NO
),
"controlShowDocument" => (
$hasLessonControl ?
$lesson->getControlShowDocument() :
LessonEnum::NO
),
"showLiveChat" => $lessonRepository->getShowLiveChat($lesson, $isStudent),
"teacher" => $lessonRepository->getLessonTeacher($lesson),
"rates" => $lessonLogRepository->countLessonLogRate(
$course->getId(),
$lesson->getId()
),
"required" => $lesson->getControlRequirement(),
"rate" => ($lessonLog ? $lessonLog->getRate() : LessonEnum::NO),
"completed" => ($lessonLog ? $lessonLog->getViewed() : LessonEnum::NO),
"numberAccess" => ($lessonLog ? $lessonLog->getNumberAccess() : null),
"timeToStaySeconds" => $infoLog->timeToStaySeconds,
"timeRestToComplete" => $infoLog->timeRestToComplete,
"blockContent" => $lessonLogRepository->checkBlockView($lessonLog),
"chat" => null,
"library" => null,
"productOffers" => null,
"chapters" => null,
];
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/chat/v2/lesson/{id}/{fileName}",
* methods = {"POST"},
* name = "getSignedUploadUrlLessonChat",
* requirements = { "id" = "\d+" }
* )
*/
public function getSignedUploadUrlLessonChat(Request $request) {
$token = $request->headers->get('X-AUTH-TOKEN');
if($token != $this->generalService->getTokenCron()){
throw new AuthInvalidException("Token Invalid");
}
$this->requestUtil->setRequest($request)->setData();
$lessonId = $request->get('id');
$fileName = $request->get('fileName');
if(empty($fileName)){
throw new FieldException("Empty Fields", [ "fileName" => "Empty "]);
}
$pathDefault = LessonEnum::PATH_UPLOAD;
$clientConnection = $this->configuration->getClientConnection();
$serverUser = $clientConnection->getServerUser();
$basekey = "client/{$serverUser}{$pathDefault}chat/{$lessonId}/{$fileName}";
$s3 = $this->generalService->getService('Aws\\AwsS3');
$url = $s3->uploadItemSignedRequest($basekey, $fileName);
if(empty($url)){
}
$data = [
"urlUpload" => $url,
];
return new HttpOk($data);
}
/**
* @Route(
* path = "/chat/v2/lesson/{id}/{fileName}",
* methods = {"DELETE"},
* name = "deleteFileLessonChat",
* requirements = { "id" = "\d+" }
* )
*/
public function deleteFileLessonChat(Request $request) {
$token = $request->headers->get('X-AUTH-TOKEN');
if($token != $this->generalService->getTokenCron()){
throw new AuthInvalidException("Token Invalid");
}
$this->requestUtil->setRequest($request)->setData();
$lessonId = $request->get('id');
$fileName = $request->get('fileName');
if(empty($fileName)){
throw new FieldException("Empty Fields", [ "fileName" => "Empty "]);
}
$objectKey = "chat/{$lessonId}/{$fileName}";
$s3 = $this->generalService->getService('Aws\\AwsS3');
$s3->deleteObjectS3($objectKey);
return new HttpNoContent;
}
/**
* @Route(
* path = "/admin/v2/course/{courseId}/favorites",
* name = "getCourseLessonFavoritesNew",
* methods = {"GET"},
* )
*/
public function getCourseLessonFavoritesNew(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$courseId = $request->get('courseId');
$course = $this->repository->findOneBy([
"id" => $courseId,
"deleted" => CourseEnum::ITEM_NO_DELETED
]);
if(!$course){
throw new NotFoundException(
$this->configuration->getLanguage('error_course_not_found', 'lesson_view_error')
);
}
$isInTeam = $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
$course,
$this->user
);
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollment = $enrollmentRepository->findOneBy([
"user" => $this->user->getId(),
"course" => $courseId,
"deleted" => EnrollmentEnum::ITEM_NO_DELETED,
], [ "id" => "DESC" ]);
$permission = $this->userPermissionUtil->getPermission("course", "see");
if(!$enrollment){
if($course->getFree() == CourseEnum::NO){
if($this->userPermissionUtil->isLow($permission)){
throw new \EADPlataforma\Error\PermissionException("PermissionException");
}
if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
throw new \EADPlataforma\Error\PermissionException("PermissionException");
}
}
$enrollmentService = $this->generalService->getService('EnrollmentService');
$info = $enrollmentService->enrollUser($this->user, $course);
if(!$info->errors){
$enrollment = $info->enrollment;
}
}
if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_enrollment_not_found', 'lesson_view_error'
)
);
}
if($course->getStatus() == CourseEnum::DRAFT){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_course_not_published', 'lesson_view_error'
)
);
}
}
$searchText = $this->requestUtil->getField('search');
$lessonLogRepository = $this->emEadmin->getRepository(LessonLog::class);
$lessonIds = $lessonLogRepository->findFavoriteLessonIds(
$this->user->getId(),
$course->getId()
);
$lessonRepository = $this->em->getRepository(Lesson::class);
$lessons = [];
if(!empty($lessonIds) && is_array($lessonIds)){
$lessons = $lessonRepository->getCourseLessons(
$course,
null,
$lessonIds,
$searchText
);
}
$aux = [];
foreach ($lessons as $key => $lesson) {
$lessonModule = $lesson->getLessonModule();
if(!isset($aux[$lessonModule->getId()])){
$aux[$lessonModule->getId()] = (object)[
"id" => $lessonModule->getId(),
"status" => $lessonModule->getStatus(),
"title" => $lessonModule->getTitle(),
"description" => $lessonModule->getDescription(),
"exam" => null,
"lessons" => [],
];
}
$library = $lesson->getLibrary();
$libraryRepository = $this->em->getRepository(Library::class);
$timeUtil = $this->generalService->getUtil('DateTimeUtil');
$duration = $library->getDuration();
$aux[$lessonModule->getId()]->lessons[] = (object)[
"id" => $lesson->getId(),
"title" => $lesson->getTitle(),
"status" => $lesson->getStatus(),
"lessonIsAccessible" => true,
"acessMessage" => null,
"contentPagesNumber" => null,
"contentDuration" => $duration ? $timeUtil->timeToSec($duration) : null,
"contentType" => null,
"contentThumb" => $libraryRepository->getCover($library) ?? null,
"exam" => null,
"quiz" => null,
"allowCheck" => EnrollmentEnum::YES,
];
}
$data = [];
foreach ($aux as $key => $value) {
$data[] = $value;
}
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}/supports",
* methods = {"GET"}
* )
*/
public function getLessonSupportNew(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$lessonId = $request->get('id');
$lessonRepository = $this->em->getRepository(Lesson::class);
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
if(!$lesson){
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
//check user can access lesson
$course = $lesson->getCourse();
$isStudent = $this->repository->isStudent($course);
if(!$lessonRepository->isLessonVisibleToStudent($lesson, false, $isStudent)){
throw new ActionInvalidException($this->configuration->getLanguage('error_lesson_unavailable', 'lesson_view_error'));
}
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollment = $enrollmentRepository->findOneBy([
"user" => $this->user->getId(),
"course" => $course->getId(),
"deleted" => LessonEnum::ITEM_NO_DELETED
], [ "id" => "DESC" ]);
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_enrollment_not_found', 'lesson_view_error'
)
);
}
$orderType = (int)$this->requestUtil->getField('orderType');
$searchText = $this->requestUtil->getField('searchText');
$limit = (int)$this->requestUtil->getField('limit');
$offset = (int)$this->requestUtil->getField('offset');
$supportId = (int)$this->requestUtil->getField('supportId');
$lessonSupportRepository = $this->em->getRepository(LessonSupport::class);
$rows = $lessonSupportRepository->getLessonSupportOrderTypeNew(
$lesson->getId(),
$orderType,
$searchText,
$limit,
$offset
);
$total = $lessonSupportRepository->countLessonSupportOrderType(
$lessonId,
$orderType,
$searchText
);
$permission = $this->userPermissionUtil->getPermission(
"course", "support", "delete"
);
$isLessonTeacher = $lessonRepository->isLessonTeacher($lesson, $this->user);
$data = [
"allowDeleteSupport" => (
$isLessonTeacher || $this->userPermissionUtil->isHigh($permission)
),
"rowsTotal" => $total,
"rowsTotalDisplay" => count($rows),
"searchText" => $searchText,
"limit" => (!empty($limit) ? $limit : 10),
"offset" => $offset,
"orderType" => $orderType,
"rows" => $rows
];
$likeControlRepository = $this->em->getRepository(LikeControl::class);
$typesText = [
LessonSupportEnum::LESSON_STUDENT => $this->configuration->getLanguage(
'student', 'lesson_view_error'
),
LessonSupportEnum::COURSE_TEACHER => $this->configuration->getLanguage(
'teacher', 'lesson_view_error'
),
LessonSupportEnum::MODULE_TEACHER => $this->configuration->getLanguage(
'teacher_module', 'lesson_view_error'
),
LessonSupportEnum::LESSON_TEACHER => $this->configuration->getLanguage(
'coordinator', 'lesson_view_error'
),
LessonSupportEnum::LESSON_TUTOR => $this->configuration->getLanguage(
'tutor', 'lesson_view_error'
),
];
$firstRow = null;
foreach ($data['rows'] as $key => $item) {
$item = (object)$item;
$dataImg = [
"fileName" => $item->photo,
"pathConst" => LessonSupportEnum::PATH_PROFILES,
"option" => "l-user-profile-support",
"addUpload" => true,
"addStream" => true,
];
$item->photo = $this->fileService->getFilePathObj($dataImg);
$item->support = html_entity_decode($item->support);
$item->answers = $lessonSupportRepository->getLessonSupportAnswers($item->id);
$item->lessonUserType = (int)$item->lessonUserType;
$item->lessonUserTypeText = $typesText[$item->lessonUserType];
$likeControl = $likeControlRepository->findOneByEAD([
"userFrom" => $this->user->getId(),
"element" => $item->id,
"type" => LikeControlEnum::LESSON_SUPPORT,
]);
$item->allowLike = true;
if($likeControl){
$item->allowLike = false;
}
if($supportId == $item->id){
$firstRow = $item;
}else{
$data['rows'][$key] = $item;
}
}
if($firstRow){
array_unshift($data['rows'], $firstRow);
}
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}/supports/{lessonSupportId}/pin",
* methods = {"PUT"}
* )
*/
public function pinLessonSupport(Request $request) {
$permission = $this->userPermissionUtil->getPermission("course", "support", "finalize");
if($this->userPermissionUtil->isLow($permission)){
throw new PermissionException(
$this->configuration->getLanguage('error_user_not_permission', 'lesson_view_error')
);
}
$this->requestUtil->setRequest($request)->setData();
$lessonSupportId = (int)$request->get('lessonSupportId');
$lessonSupportRepository = $this->em->getRepository(LessonSupport::class);
$lessonSupport = $lessonSupportRepository->findOneBy([
"id" => $lessonSupportId,
"deleted" => LessonSupportEnum::ITEM_NO_DELETED
]);
if (!$lessonSupport) {
throw new NotFoundException(
$this->configuration->getLanguage('error_support_not_found', 'lesson_view_error')
);
}
$lesson = $lessonSupport->getLesson();
$isLessonTeacher = $this->em->getRepository(Lesson::class)->isLessonTeacher(
$lesson,
$this->user
);
if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
throw new PermissionException(
$this->configuration->getLanguage('error_user_not_permission', 'lesson_view_error')
);
}
$lessonSupport->setLessonFixed(LessonSupportEnum::YES);
$this->em->flush();
$data = $lessonSupport->toReturn();
return $this->eadResponseNew([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}/supports/{lessonSupportId}/unpin",
* methods = {"DELETE"}
* )
*/
public function unpinLessonSupport(Request $request) {
$permission = $this->userPermissionUtil->getPermission("course", "support", "finalize");
if($this->userPermissionUtil->isLow($permission)){
throw new PermissionException(
$this->configuration->getLanguage('error_user_not_permission', 'lesson_view_error')
);
}
$this->requestUtil->setRequest($request)->setData();
$lessonSupportId = (int)$request->get('lessonSupportId');
$lessonSupportRepository = $this->em->getRepository(LessonSupport::class);
$lessonSupport = $lessonSupportRepository->findOneBy([
"id" => $lessonSupportId,
"deleted" => LessonSupportEnum::ITEM_NO_DELETED
]);
if (!$lessonSupport) {
throw new NotFoundException(
$this->configuration->getLanguage('error_support_not_found', 'lesson_view_error')
);
}
$lesson = $lessonSupport->getLesson();
$isLessonTeacher = $this->em->getRepository(Lesson::class)->isLessonTeacher(
$lesson,
$this->user
);
if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
throw new PermissionException(
$this->configuration->getLanguage('error_user_not_permission', 'lesson_view_error')
);
}
$lessonSupport->setLessonFixed(LessonSupportEnum::NO);
$this->em->flush();
$data = $lessonSupport->toReturn();
return $this->eadResponseNew([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
* methods = {"PATCH"}
* )
*/
public function likeLessonSupport(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$lessonSupportId = (int)$request->get('lessonSupportId');
$lessonSupportRepository = $this->em->getRepository(LessonSupport::class);
$lessonSupport = $lessonSupportRepository->findOneBy([
"id" => $lessonSupportId,
"deleted" => LessonSupportEnum::ITEM_NO_DELETED
]);
if (!$lessonSupport) {
throw new NotFoundException(
$this->configuration->getLanguage('error_support_not_found', 'lesson_view_error')
);
}
$lesson = $lessonSupport->getLesson();
$likeControl = new LikeControl();
$type = LikeControlEnum::LESSON_SUPPORT;
if($lessonSupport->getLessonSupport()){
$type = LikeControlEnum::LESSON_SUPPORT_ANSWER;
}
$likeControl->setElement($lessonSupportId);
$likeControl->setType($type);
$likeControl->setUserTo($lessonSupport->getUser());
$likeControl->setUserFrom($this->user);
$errors = $this->validateEntity($likeControl);
if($errors){
throw new FieldException("FieldException", $errors);
}
$lessonSupport->setLikeNumber($lessonSupport->getLikeNumber() + 1);
$this->em->persist($likeControl);
$this->em->flush();
return $this->eadResponseNew([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}/supports",
* methods = {"POST"},
* )
*/
public function registerLessonSupport(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$lessonRepository = $this->em->getRepository(Lesson::class);
$lessonSupportRepository = $this->em->getRepository(LessonSupport::class);
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$lessonId = $request->get('id');
$lesson = $lessonRepository->findOneBy([
"id" => $lessonId,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
if(!$lesson){
throw new NotFoundException(
$this->configuration->getLanguage('error_lesson_not_found', 'lesson_view_error')
);
}
//check user can access lesson
$isStudent = $this->repository->isStudent($lesson->getCourse());
if(!$lessonRepository->isLessonVisibleToStudent($lesson, false, $isStudent)){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_lesson_unavailable', 'lesson_view_error'
)
);
}
$lessonSupport = new LessonSupport();
$lessonSupport->setUser($this->user);
$lessonSupport->setLesson($lesson);
//set LessonSupport in LessonSupport
if($this->requestUtil->issetField('lessonSupport')){
$lessonSupportId = (int)$this->requestUtil->getField('lessonSupport');
$lessonSupportParent = $lessonSupportRepository->findOneBy([
"id" => $lessonSupportId,
"deleted" => LessonSupportEnum::ITEM_NO_DELETED
]);
$lessonSupport->setLessonSupport($lessonSupportParent);
}
if($this->requestUtil->issetField('support')){
$lessonSupport->setSupport($this->requestUtil->getField('support'));
}
$errors = $this->validateEntity($lessonSupport);
if($errors){
throw new FieldException("FieldException", $errors);
}
$lessonSupportParent = $lessonSupport->getLessonSupport();
$lesson = $lessonSupport->getLesson();
$course = $lesson->getCourse();
$isLessonTeacher = $lessonRepository->isLessonTeacher(
$lesson,
$this->user
);
$isEnrolled = $enrollmentRepository->isValidEnrollmentByUser(
$this->user->getId(),
$course->getId()
);
$permission = $this->userPermissionUtil->getPermission("course", "support", "answer");
if($lessonSupportParent){
if(!$isEnrolled){
if($this->userPermissionUtil->isLow($permission)){
throw new PermissionException($this->configuration->getLanguage('error_user_not_permission', 'lesson_view_error'));
}
if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
throw new PermissionException($this->configuration->getLanguage('error_user_not_permission', 'lesson_view_error'));
}
}
}else{
if($course->getSupport() == LessonSupportEnum::NO){
throw new ActionInvalidException($this->configuration->getLanguage('error_course_not_support', 'lesson_view_error'));
}
if(!$isEnrolled && !$isLessonTeacher){
throw new ActionInvalidException(
$this->configuration->getLanguage('error_unavailable', 'lesson_view_error')
);
}
}
$this->em->persist($lessonSupport);
$this->em->flush();
$lessonSupportRepository->processNewLessonSupport($lessonSupport);
return $this->eadResponseNew([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
* methods = {"PUT"}
* )
*/
public function editLessonSupport(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$lessonSupportId = (int)$request->get('lessonSupportId');
$lessonSupportRepository = $this->em->getRepository(LessonSupport::class);
$lessonSupport = $lessonSupportRepository->findOneBy([
"id" => $lessonSupportId,
"deleted" => LessonSupportEnum::ITEM_NO_DELETED
]);
if (!$lessonSupport) {
throw new NotFoundException(
$this->configuration->getLanguage('error_support_not_found', 'lesson_view_error')
);
}
$lesson = $lessonSupport->getLesson();
if($this->requestUtil->issetField('support')){
$lessonSupport->setSupport($this->requestUtil->getField('support'));
}
$lessonSupport->setDateUpdate(date('Y-m-d H:i:s'));
$errors = $this->validateEntity($lessonSupport);
if($errors){
throw new FieldException("FieldException", $errors);
}
$this->em->flush();
return $this->eadResponseNew([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
* methods = {"DELETE"}
* )
*/
public function deleteLessonSupport(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$lessonSupportId = (int)$request->get('lessonSupportId');
$lessonSupportRepository = $this->em->getRepository(LessonSupport::class);
$lessonSupport = $lessonSupportRepository->findOneBy([
"id" => $lessonSupportId,
"deleted" => LessonSupportEnum::ITEM_NO_DELETED
]);
if (!$lessonSupport) {
throw new NotFoundException(
$this->configuration->getLanguage('error_support_not_found', 'lesson_view_error')
);
}
$lessonSupportRepository->delete(
$lessonSupport,
TrashEnum::LESSON_SUPPORT,
$this->userPermissionUtil->getPermission("course", "support", "delete"),
TrashEnum::NO
);
$this->em->flush();
return $this->eadResponseNew([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/v2/course/{id}/testimonial",
* methods = {"POST"},
* )
*/
public function registerCourseTestimonial(Request $request){
if($this->configuration->get("allow_testimonial") == CourseTestimonialEnum::NO){
throw new ActionInvalidException('ActionInvalidException');
}
$this->requestUtil->setRequest($request)->setData();
$courseId = $request->get('id');
$course = $this->repository->findOneBy([
"id" => $courseId,
"deleted" => CourseEnum::ITEM_NO_DELETED
]);
if(!$course){
throw new NotFoundException(
$this->configuration->getLanguage('error_course_not_found', 'lesson_view_error')
);
}
$isInTeam = $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
$course,
$this->user
);
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollment = $enrollmentRepository->findOneBy([
"user" => $this->user->getId(),
"course" => $courseId,
"deleted" => EnrollmentEnum::ITEM_NO_DELETED,
], [ "id" => "DESC" ]);
$permission = $this->userPermissionUtil->getPermission("course", "see");
if(!$enrollment){
if($course->getFree() == CourseEnum::NO){
if($this->userPermissionUtil->isLow($permission)){
throw new PermissionException($this->configuration->getLanguage('error_user_not_permission', 'lesson_view_error'));
}
if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
throw new PermissionException(
$this->configuration->getLanguage(
'error_user_not_permission',
'lesson_view_error'
)
);
}
}
$enrollmentService = $this->generalService->getService('EnrollmentService');
$info = $enrollmentService->enrollUser($this->user, $course);
if(!$info->errors){
$enrollment = $info->enrollment;
}
}
if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_enrollment_not_found',
'lesson_view_error'
)
);
}
if($course->getStatus() == CourseEnum::DRAFT){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_course_not_published',
'lesson_view_error'
)
);
}
}
$courseTestimonial = new CourseTestimonial();
$courseTestimonial->setDate(date('Y-m-d H:i:s'));
$courseTestimonial->setStatus(CourseTestimonialEnum::WAITING);
if($this->requestUtil->issetField('score')){
$courseTestimonial->setScore((int)$this->requestUtil->getField("score"));
}
if($this->requestUtil->issetField('testimonial')){
$courseTestimonial->setTestimonial($this->requestUtil->getField('testimonial'));
}
if(
$this->configuration->get("required_text_testimonial") == CourseEnum::YES &&
empty($courseTestimonial->getTestimonial())
){
throw new FieldException("FieldException", [ "testimonial" ]);
}
$courseTestimonial->setUser($this->user);
$courseTestimonial->setCourse($course);
$errors = $this->validateEntity($courseTestimonial);
if($errors){
throw new FieldException("FieldException", $errors);
}
$this->em->persist($courseTestimonial);
$this->em->flush();
$courseTestimonialRepository = $this->em->getRepository(CourseTestimonial::class);
$courseTestimonialRepository->autoEvaluateTestimonial($courseTestimonial);
$products = $course->getProduct();
foreach ($products as $key => $product){
$courseTestimonialRepository->getScoreByProduct($product, true);
}
$this->em->flush();
$userWebhook = $this->em->getRepository(User::class)->getToWebhook(
$courseTestimonial->getUser()
);
$dataObj= (object)[
"user" => $userWebhook,
"course"=> (object)[
"id" => (string)$courseTestimonial->getCourse()->getId(),
"name" => $courseTestimonial->getCourse()->getTitle(),
],
"testimonial"=> (object)[
"stars" => $courseTestimonial->getScore(),
"comment" => $courseTestimonial->getTestimonial(),
"dates" => (object)[
"created" => $courseTestimonial->getDate(),
],
],
"enrollment"=> $enrollment->toWebhook(),
];
$webhookService = $this->generalService->getService('WebhookService');
$webhookService->addItemList(WebhookEnum::TESTIMONIAL, $dataObj);
$notificationService = $this->generalService->getService(
'NotificationService'
);
if($this->user != $course->getUser()){
$notificationService->create(
$this->user,
$course->getUser(),
NotificationEnum::ORIGIN_COURSE_TESTIMONIAL_NEW,
$courseTestimonial->getId()
);
}
$userCourse = $course->getUser();
if($userCourse->getAllowNotifyNewSupportMessage() == CourseTestimonialEnum::YES){
$emailService = $this->generalService->getService('EmailService');
if($emailService->checkUserToSend($userCourse)){
$emailService->setToEmail($userCourse->getEmail());
$emailService->setToName($userCourse->getName());
$subText = $this->configuration->getLanguage(
'new_course_testimonial.subject', 'email'
);
$id = $courseTestimonial->getId();
$subject = "{$subText} - {$course->getTitle()}";
$emailService->setSubject($subject);
$emailService->setData([
"userName" => $userCourse->getName(),
"btnLink" => "{$this->adminLink}courses/testimonials/{$id}",
]);
$emailService->setTemplateBody("new_course_testimonial");
$emailService->send();
}
}
$data = $courseTestimonial->toReturn();
$this->userLogService->logInsert(
"course_testimonial",
$courseTestimonial->getId(),
$data
);
$testimonial = (object)[
"id" => $courseTestimonial->getId(),
"score" => $courseTestimonial->getScore(),
"testimonial" => $courseTestimonial->getTestimonial(),
];
return $this->eadResponseNew($testimonial);
}
/**
* @Route(
* path = "/admin/v2/course/{courseId}/testimonial/{id}",
* methods = {"PUT"}
* )
*/
public function editCourseTestimonial(Request $request){
if($this->configuration->get("allow_testimonial") == CourseTestimonialEnum::NO){
throw new ActionInvalidException('ActionInvalidException');
}
$courseId = $request->get('courseId');
$course = $this->repository->findOneBy([
"id" => $courseId,
"deleted" => CourseEnum::ITEM_NO_DELETED
]);
if(!$course){
throw new NotFoundException(
$this->configuration->getLanguage('error_course_not_found', 'lesson_view_error')
);
}
$isInTeam = $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
$course,
$this->user
);
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollment = $enrollmentRepository->findOneBy([
"user" => $this->user->getId(),
"course" => $courseId,
"deleted" => EnrollmentEnum::ITEM_NO_DELETED,
], [ "id" => "DESC" ]);
$permission = $this->userPermissionUtil->getPermission("course", "see");
if(!$enrollment){
if($course->getFree() == CourseEnum::NO){
if($this->userPermissionUtil->isLow($permission)){
throw new PermissionException(
$this->configuration->getLanguage(
'error_user_not_permission',
'lesson_view_error'
)
);
}
if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
throw new PermissionException(
$this->configuration->getLanguage(
'error_user_not_permission',
'lesson_view_error'
)
);
}
}
$enrollmentService = $this->generalService->getService('EnrollmentService');
$info = $enrollmentService->enrollUser($this->user, $course);
if(!$info->errors){
$enrollment = $info->enrollment;
}
}
if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
if(!$enrollment){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_enrollment_not_found',
'lesson_view_error'
)
);
}
if($course->getStatus() == CourseEnum::DRAFT){
throw new ActionInvalidException(
$this->configuration->getLanguage(
'error_course_not_published',
'lesson_view_error'
)
);
}
}
$testimonialId = $request->get('id');
$courseTestimonialRepository = $this->em->getRepository(CourseTestimonial::class);
$courseTestimonial = $courseTestimonialRepository->findOneBy([
"id" => $testimonialId,
"deleted" => CourseTestimonialEnum::ITEM_NO_DELETED
]);
if (!$courseTestimonial || empty($testimonialId)) {
throw new NotFoundException(
$this->configuration->getLanguage('error_testimonial_not_found', 'lesson_view_error')
);
}
$this->requestUtil->setRequest($request)->setData();
if($this->requestUtil->issetField('score')){
$courseTestimonial->setScore((int)$this->requestUtil->getField("score"));
}
if($this->requestUtil->issetField('testimonial')){
$courseTestimonial->setTestimonial($this->requestUtil->getField('testimonial'));
}
if(
$this->configuration->get("required_text_testimonial") == CourseEnum::YES &&
empty($courseTestimonial->getTestimonial())
){
throw new FieldException("FieldException", [ "testimonial" ]);
}
$courseTestimonial->setDate(date('Y-m-d H:i:s'));
$courseTestimonial->setStatus(CourseTestimonialEnum::WAITING);
$errors = $this->validateEntity($courseTestimonial);
if($errors){
throw new FieldException("FieldException", $errors);
}
$courseTestimonialRepository->autoEvaluateTestimonial($courseTestimonial);
$products = $course->getProduct();
foreach ($products as $key => $product){
$courseTestimonialRepository->getScoreByProduct($product, true);
}
$this->em->flush();
$notificationService = $this->generalService->getService('NotificationService');
$emailService = $this->generalService->getService('EmailService');
if($this->user != $course->getUser()){
$notificationService->create(
$this->user,
$course->getUser(),
NotificationEnum::ORIGIN_COURSE_TESTIMONIAL_CHANGE,
$courseTestimonial->getId()
);
}
$user = $course->getUser();
if($user->getAllowNotifyNewSupportMessage() == CourseTestimonialEnum::YES){
if($emailService->checkUserToSend($user)){
$emailService->setToEmail($user->getEmail());
$emailService->setToName($user->getName());
$subText = $this->configuration->getLanguage(
'new_course_testimonial.subject', 'email'
);
$subject = "{$subText} - {$course->getTitle()}";
$emailService->setSubject($subject);
$emailService->setData([
"userName" => $course->getUser()->getName(),
"btnLink" => "{$this->adminLink}courses/testimonials/{$testimonialId}",
]);
$emailService->setTemplateBody("new_course_testimonial");
$emailService->send();
}
}
$data = $courseTestimonial->toReturn();
$this->userLogService->logUpdate(
"course_testimonial",
$courseTestimonial->getId(),
$data
);
$testimonial = (object)[
"id" => $courseTestimonial->getId(),
"score" => $courseTestimonial->getScore(),
"testimonial" => $courseTestimonial->getTestimonial(),
];
return $this->eadResponseNew($testimonial);
}
}