<?php
namespace EADPlataforma\Repository;
use EADPlataforma\Entity\Exam;
use EADPlataforma\Entity\ExamUser;
use EADPlataforma\Entity\ExamUserAnswer;
use EADPlataforma\Entity\Course;
use EADPlataforma\Entity\Product;
use EADPlataforma\Entity\LessonModule;
use EADPlataforma\Entity\Lesson;
use EADPlataforma\Entity\LessonLog;
use EADPlataforma\Entity\LessonLogOrigin;
use EADPlataforma\Entity\Enrollment;
use EADPlataforma\Entity\Question;
use EADPlataforma\Entity\QuestionOption;
use EADPlataforma\Entity\CourseCertificate;
use EADPlataforma\Entity\User;
use EADPlataforma\Entity\Trash;
use EADPlataforma\Enum\ExamEnum;
use EADPlataforma\Enum\ExamUserEnum;
use EADPlataforma\Enum\LessonEnum;
use EADPlataforma\Enum\LessonModuleEnum;
use EADPlataforma\Enum\QuestionEnum;
use EADPlataforma\Enum\TaskQueueEnum;
use EADPlataforma\Enum\UserEnum;
use EADPlataforma\Enum\EnrollmentEnum;
use EADPlataforma\Enum\CourseEnum;
/**
* @method Exam|null find($id, $lockMode = null, $lockVersion = null)
* @method Exam|null findOneBy(array $criteria, array $orderBy = null)
* @method Exam[] findAll()
* @method Exam[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ExamRepository extends AbstractRepository
{
public function getEntityClass(){
return Exam::class;
}
public function getConnectionName(){
return "school";
}
public function findItems()
{
return $this->createQueryBuilder('e')->getQuery()->getResult();
}
public function insertExam(
array $data,
int $type,
Course $course,
?LessonModule $lessonModule = null,
?Lesson $lesson = null
): ?Exam
{
$exam = new Exam;
$title = $data['title'] ?? null;
$status = $data['status'] ?? ExamEnum::DRAFT;
$requirement = $data['requirement'] ?? ExamEnum::NO;
$questionOrder = $data['questionOrder'] ?? ExamEnum::SEQUENTIAL;
$questionNumber = $data['questionNumber'] ?? null;
$examAverage = $data['examAverage'] ?? ExamEnum::DEFAULT_AVERAGE;
$examWeight = $data['examWeight'] ?? ExamEnum::NO;
$typeRelease = $data['typeRelease'] ?? ExamEnum::RELEASED;
$controlTime = $data['controlTime'] ?? ExamEnum::NO;
$periodStart = $data['periodStart'] ?? ExamEnum::NO;
$periodEnd = $data['periodEnd'] ?? ExamEnum::NO;
$dateStart = $data['dateStart'] ?? null;
$examTime = $data['examTime'] ?? null;
$attempts = $data['attempts'] ?? ExamEnum::NO;
$attemptsNumber = $data['attemptsNumber'] ?? ExamEnum::YES;
$attemptsAutoRelease = $data['attemptsAutoRelease'] ?? ExamEnum::YES;
$attemptsTypeRelease = $data['attemptsTypeRelease'] ?? ExamEnum::DAYS;
$attemptsTime = $data['attemptsTime'] ?? null;
$attemptsPeriod = $data['attemptsPeriod'] ?? null;
$showTemplate = $data['showTemplate'] ?? ExamEnum::NO;
$note = $data['note'] ?? null;
$questions = $data['question'] ?? null;
$notify = $data['notify'] ?? ExamEnum::NO;
if($questionOrder == ExamEnum::SEQUENTIAL){
$questionNumber = count($questions);
}
$exam->setTitle($title);
$exam->setStatus($status);
$exam->setRequirement($requirement);
$exam->setQuestionOrder($questionOrder);
$exam->setQuestionNumber($questionNumber);
$exam->setExamAverage($examAverage);
$exam->setExamWeight($examWeight);
$exam->setTypeRelease($typeRelease);
$exam->setControlTime($controlTime);
$exam->setPeriodStart($periodStart);
$exam->setPeriodEnd($periodEnd);
$exam->setDateStart($dateStart);
$exam->setExamTime($examTime);
$exam->setAttempts($attempts);
$exam->setAttemptsNumber($attemptsNumber);
$exam->setAttemptsAutoRelease($attemptsAutoRelease);
$exam->setAttemptsTypeRelease($attemptsTypeRelease);
$exam->setAttemptsTime($attemptsTime);
$exam->setAttemptsPeriod($attemptsPeriod);
$exam->setShowTemplate($showTemplate);
$exam->setNote($note);
if(!empty($questions) && is_array($questions)){
$questionRepository = $this->em->getRepository(Question::class);
foreach ($questions as $key => $questionId) {
$question = $questionRepository->findOneBy([
"id" => $questionId,
"deleted" => ExamEnum::ITEM_NO_DELETED
]);
if($question){
$exam->addQuestion($question);
}
}
}
$exam->setType($type);
$exam->setCourse($course);
$exam->setLessonModule($lessonModule);
$exam->setLesson($lesson);
$course->setDateUpdate(date('Y-m-d H:i:s'));
try {
$this->em->persist($exam);
$this->em->flush();
} catch (\Exception $e) {
return null;
}
if($notify == ExamEnum::YES){
$this->notifyStudentsExam($exam);
}
$data = $exam->toReturn();
$this->getLogService()->logInsert("exam", $exam->getId(), $data);
return $exam;
}
public function editExam(
Exam $exam,
array $data
): ?Exam
{
$title = $data['title'] ?? null;
$status = $data['status'] ?? ExamEnum::DRAFT;
$requirement = $data['requirement'] ?? ExamEnum::NO;
$questionOrder = $data['questionOrder'] ?? ExamEnum::SEQUENTIAL;
$questionNumber = $data['questionNumber'] ?? null;
$examAverage = $data['examAverage'] ?? ExamEnum::DEFAULT_AVERAGE;
$examWeight = $data['examWeight'] ?? ExamEnum::NO;
$typeRelease = $data['typeRelease'] ?? ExamEnum::RELEASED;
$controlTime = $data['controlTime'] ?? ExamEnum::NO;
$periodStart = $data['periodStart'] ?? ExamEnum::NO;
$periodEnd = $data['periodEnd'] ?? ExamEnum::NO;
$dateStart = $data['dateStart'] ?? null;
$examTime = $data['examTime'] ?? null;
$attempts = $data['attempts'] ?? ExamEnum::NO;
$attemptsNumber = $data['attemptsNumber'] ?? ExamEnum::YES;
$attemptsAutoRelease = $data['attemptsAutoRelease'] ?? ExamEnum::YES;
$attemptsTypeRelease = $data['attemptsTypeRelease'] ?? ExamEnum::DAYS;
$attemptsTime = $data['attemptsTime'] ?? null;
$attemptsPeriod = $data['attemptsPeriod'] ?? null;
$showTemplate = $data['showTemplate'] ?? ExamEnum::NO;
$note = $data['note'] ?? null;
$questions = $data['question'] ?? null;
$notify = $data['notify'] ?? ExamEnum::NO;
if($questionOrder == ExamEnum::SEQUENTIAL){
$questionNumber = count($questions);
}
$exam->setTitle($title);
$exam->setStatus($status);
$exam->setRequirement($requirement);
$exam->setQuestionOrder($questionOrder);
$exam->setQuestionNumber($questionNumber);
$exam->setExamAverage($examAverage);
$exam->setExamWeight($examWeight);
$exam->setTypeRelease($typeRelease);
$exam->setControlTime($controlTime);
$exam->setPeriodStart($periodStart);
$exam->setPeriodEnd($periodEnd);
$exam->setDateStart($dateStart);
$exam->setExamTime($examTime);
$exam->setAttempts($attempts);
$exam->setAttemptsNumber($attemptsNumber);
$exam->setAttemptsAutoRelease($attemptsAutoRelease);
$exam->setAttemptsTypeRelease($attemptsTypeRelease);
$exam->setAttemptsTime($attemptsTime);
$exam->setAttemptsPeriod($attemptsPeriod);
$exam->setShowTemplate($showTemplate);
$exam->setNote($note);
if(!empty($questions) && is_array($questions)){
$questionRepository = $this->em->getRepository(Question::class);
foreach ($questions as $key => $questionId) {
$question = $questionRepository->findOneBy([
"id" => $questionId,
"deleted" => ExamEnum::ITEM_NO_DELETED
]);
if($question){
$exam->addQuestion($question);
}
}
}
$course = $exam->getCourse();
$course->setDateUpdate(date('Y-m-d H:i:s'));
try {
$this->em->flush();
} catch (\Exception $e) {
return null;
}
if($notify == ExamEnum::YES){
$this->notifyStudentsExam($exam);
}
$data = $exam->toReturn();
$this->getLogService()->logUpdate("exam", $exam->getId(), $data);
return $exam;
}
public function getExamTypeText($type)
{
$examTypeText = '';
switch ($type) {
case ExamEnum::LESSON:
$examTypeText = $this->configuration->getLanguage('lesson', 'export');
break;
case ExamEnum::MODULE:
$examTypeText = $this->configuration->getLanguage('module', 'export');
break;
case ExamEnum::COURSE:
$examTypeText = $this->configuration->getLanguage('course', 'export');
break;
}
return $examTypeText;
}
public function notifyStudentsExam(Exam $exam)
{
$today = date('Y-m-d');
$dateLastNotify = $exam->getDateLastNotify('Y-m-d');
if($today != $dateLastNotify){
$course = $exam->getCourse();
$lessonModule = $exam->getLessonModule();
$lesson = $exam->getLesson();
if(
$course->getStatus() == CourseEnum::PUBLISHED &&
$exam->getStatus() == ExamEnum::PUBLISHED
){
$taskQueueService = $this->generalService->getService('TaskQueueService');
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$users = $this->getUsersNotStartExam($exam);
foreach ($users as $key => $user) {
if($user->getAllowNotifyNewExam() == ExamEnum::YES){
$taskQueueService->insert(TaskQueueEnum::EMAIL_NEW_EXAM, $user, $exam->getId());
}
}
$exam->setDateLastNotify(date('Y-m-d H:i:s'));
$this->em->flush();
}
}
}
public function getUsersNotStartExam(Exam $exam)
{
$userRepository = $this->em->getRepository(User::class);
$query = $userRepository->createQueryBuilder('u');
$query->innerJoin('EADPlataforma:Enrollment', 'e', 'WITH', 'e.user = u.id');
$query->andWhere('u.deleted = :deleted');
$query->andWhere('e.course = :course');
$query->andWhere('e.status = :enrollmentStatus');
$query->andWhere('e.deleted = :enrollmentDeleted');
$query->andWhere('e.datePeriod >= :now');
$subQuery = $userRepository->createQueryBuilder('examUser');
$subQuery->select('IDENTITY(eu.user)');
$subQuery->from('EADPlataforma:ExamUser', 'eu');
$subQuery->andWhere('eu.exam = :exam');
$subQuery->andWhere('eu.course = :course');
$subQuery = $subQuery->getDQL();
$query->andWhere($query->expr()->notIn('u.id', $subQuery));
$subQuery = $userRepository->createQueryBuilder('certificate');
$subQuery->select('IDENTITY(cc.user)');
$subQuery->from('EADPlataforma:CourseCertificate', 'cc');
$subQuery->andWhere('cc.course = :course');
$subQuery = $subQuery->getDQL();
$query->andWhere($query->expr()->notIn('u.id', $subQuery));
$query->setParameter('deleted', UserEnum::ITEM_NO_DELETED);
$query->setParameter('course', $exam->getCourse()->getId());
$query->setParameter('enrollmentStatus', EnrollmentEnum::STATUS_ACTIVE);
$query->setParameter('enrollmentDeleted', EnrollmentEnum::ITEM_NO_DELETED);
$query->setParameter('now', date('Y-m-d H:i:s'));
$query->setParameter('exam', $exam->getId());
return $query->getQuery()->execute();
}
public function isExamTeacher(Exam $exam, User $user)
{
$lesson = $exam->getLesson();
$lessonModule = $exam->getLessonModule();
$course = $exam->getCourse();
return $this->allowUserExam($course, $lessonModule, $lesson, $user);
}
public function allowUserExam(
?Course $course = null,
?LessonModule $lessonModule = null,
?Lesson $lesson = null,
User $user
): bool
{
if(!$course){
return false;
}
$uId = $user->getId();
if(($course->getUser()->getId() == $uId)){
return true;
}
if($lessonModule){
$lessonModuleTeahcerId = $lessonModule->getUser()->getId();
return ($lessonModuleTeahcerId == $uId);
}else if($lesson){
$lessonTeahcerId = $lesson->getUser()->getId();
$lessonModuleTeahcerId = $lesson->getLessonModule()->getUser()->getId();
return ($lessonTeahcerId == $uId || $lessonModuleTeahcerId == $uId);
}
return false;
}
public function getExamToIndexNew(
?bool $isStudent = true,
int $type, Course $course,
?LessonModule $lessonModule = null,
?Lesson $lesson = null
)
{
$filter = [
"course" => $course->getId(),
"type" => $type,
"deleted" => ExamEnum::ITEM_NO_DELETED
];
if($isStudent){
$filter["status"] = ExamEnum::PUBLISHED;
}
if(!empty($lessonModule)){
$filter["lessonModule"] = $lessonModule->getId();
}
if(!empty($lesson)){
$filter["lesson"] = $lesson->getId();
}
$e = $this->findOneBy($filter);
$examUser = null;
if($e && !empty($e->getQuestionNumber())){
$examUserData = null;
if($this->getUser()){
$examUserRepository = $this->em->getRepository(ExamUser::class);
$examUser = $examUserRepository->findOneBy([
"user" => $this->getUser()->getId(),
"exam" => $e->getId(),
"inactive" => ExamUserEnum::NO,
"deleted" => ExamUserEnum::ITEM_NO_DELETED
]);
}
$infoAccess = $this->checkExamIsAccessible($e, $examUser);
$data = [
"id" => $e->getId(),
"title" => $e->getTitle(),
"type" => $e->getType(),
"status" => (
$examUser ?
$examUser->getStatus() :
ExamUserEnum::NOT_STARTED
),
"examStatus" => $e->getStatus(),
"examWeight" => $e->getExamWeight(),
"examTime" => $e->getExamTime(),
"questionNumber" => $e->getQuestionNumber(),
"isAccessible" => $infoAccess->isAccessible,
"acessMessage" => $infoAccess->message,
"dateReleaseAccess" => $infoAccess->dateRelease,
"dateCloseAccess" => $infoAccess->dateClose,
];
return $data;
}
return;
}
public function getExamToIndex(
int $type, Course $course,
?LessonModule $lessonModule = null,
?Lesson $lesson = null
)
{
$filter = [
"course" => $course->getId(),
"type" => $type,
"status" => ExamEnum::PUBLISHED,
"deleted" => ExamEnum::ITEM_NO_DELETED
];
if(!empty($lessonModule)){
$filter["lessonModule"] = $lessonModule->getId();
}
if(!empty($lesson)){
$filter["lesson"] = $lesson->getId();
}
$e = $this->findOneBy($filter);
$examUser = null;
if($e && !empty($e->getQuestionNumber())){
$examUserData = null;
if($this->getUser()){
$examUserRepository = $this->em->getRepository(ExamUser::class);
$examUser = $examUserRepository->findOneBy([
"user" => $this->getUser()->getId(),
"exam" => $e->getId(),
"inactive" => ExamUserEnum::NO,
"deleted" => ExamUserEnum::ITEM_NO_DELETED
]);
if($examUser){
$dateTimeUtil = $this->generalService->getUtil('DateTimeUtil');
$todaySeconds = strtotime(date('Y-m-d H:i:s'));
$dateStartSeconds = strtotime($examUser->getDateStart());
$timeSeconds = $dateTimeUtil->timeToSec($e->getExamTime());
$dateExamEndSecods = $dateStartSeconds + $timeSeconds;
$timeRestToEndSeconds = $timeSeconds;
if($examUser->getStatus() == ExamUserEnum::IN_PROGRESS){
$timeRestToEndSeconds = $dateExamEndSecods - $todaySeconds;
if($timeRestToEndSeconds < 0){
$timeRestToEndSeconds = 0;
}
}
$progress = 100;
$qNumber = $e->getQuestionNumber();
if(
$examUser->getStatus() == ExamUserEnum::IN_PROGRESS ||
$examUser->getStatus() == ExamUserEnum::TIMEOUT
){
$euAnswerRepository = $this->em->getRepository(ExamUserAnswer::class);
$qNumber = $euAnswerRepository->countAsnweredByExamUser(
$examUser
);
$progress = round($qNumber * 100 / $e->getQuestionNumber());
}
$examUserData = (object)[
"id" => $examUser->getId(),
"status" => $examUser->getStatus(),
"grade" => $examUser->getGrade(),
"timeRestToEnd" => $dateTimeUtil->secToTime($timeRestToEndSeconds),
"timeRestToEndSeconds" => $timeRestToEndSeconds,
"dateStart" => $examUser->getDateStart('Y-m-d H:i:s'),
"questionAnswerNumber" => $qNumber,
"progress" => $progress
];
}
}
$infoAccess = $this->checkExamIsAccessible($e, $examUser);
$data = [
"id" => $e->getId(),
"courseId" => $e->getCourse()->getId(),
"lessonModuleId" => (
$e->getLessonModule() ? $e->getLessonModule()->getId() : null
),
"lessonId" => ($e->getLesson() ? $e->getLesson()->getId() : null),
"title" => $e->getTitle(),
"type" => $e->getType(),
"examWeight" => $e->getExamWeight(),
"examTime" => $e->getExamTime(),
"questionNumber" => $e->getQuestionNumber(),
"isAccessible" => $infoAccess->isAccessible,
"dateReleaseAccess" => $infoAccess->dateRelease,
"dateCloseAccess" => $infoAccess->dateClose,
"examUser" => $examUserData,
];
return $data;
}
return;
}
public function getExamByQuestions(Question $question)
{
$questionId = $question->getId();
$query = $this->createQueryBuilder('e');
$query->innerJoin(
'EADPlataforma:Question',
'q',
'WITH',
'e MEMBER OF q.exam AND q.id = :questionId'
);
$query->setParameter('questionId', $questionId);
$query->andWhere('e.deleted = :deleted');
$query->setParameter('deleted', ExamEnum::ITEM_NO_DELETED);
return $query->getQuery()->execute();
}
public function counterExamAndQuiz(Question $question){
$quantityQuiz = 0;
$examIds = $question->toReturn()["exam"];
$quantityExam = count($question->toReturn()["exam"]);
$exams = $this->findBy([
"id" => $examIds
]);
foreach ($exams as $exam) {
if($exam->getType() == ExamEnum::QUIZ) {
$quantityQuiz++;
$quantityExam--;
}
}
$counters = [
"quantityQuiz" => $quantityQuiz,
"quantityExam" => $quantityExam
];
return $counters;
}
public function checkExamIsAccessible(Exam $exam, ?ExamUser $examUser = null){
$info = (object)[
"isAccessible" => false,
"dateRelease" => null,
"dateClose" => null,
"message" => null,
];
$typeRelease = $exam->getTypeRelease();
if($typeRelease == ExamEnum::FIXED_DATE){
$dateStart = $exam->getDateStart();
$info->dateRelease = $dateStart;
}
if(!$this->getUser()){
$info->message = $this->configuration->getLanguage('error_auth', 'lesson_view_error');
return $info;
}
$lessonRepository = $this->em->getRepository(Lesson::class);
$logRepository = $this->em->getRepository(LessonLogOrigin::class);
$lessonLogRepository = $this->emEadmin->getRepository(LessonLog::class);
$courseRepository = $this->em->getRepository(Course::class);
$examUserRepository = $this->em->getRepository(ExamUser::class);
if(empty($examUser)){
$examUser = $examUserRepository->findOneBy([
"user" => $this->getUser()->getId(),
"exam" => $exam->getId(),
"inactive" => ExamUserEnum::NO,
"deleted" => ExamUserEnum::ITEM_NO_DELETED
]);
}
if(!empty($examUser)){
$info->isAccessible = true;
return $info;
}
$today = date('Y-m-d');
$todayDateTime = date('Y-m-d H:i:s');
$course = $exam->getCourse();
$isStudent = $courseRepository->isStudent($course);
if(!$isStudent){
$info->isAccessible = true;
return $info;
}
$lesson = null;
$hasLessonControl = $this->configuration->checkModuleIsAbleOnPlan(
'lessonControlFunction'
);
$lessonNumber = $lessonRepository->getLessonPublicNumber($course);
$lessonNumberModule = $lessonNumber;
if(empty($lessonNumber)){
$info->isAccessible = true;
return $info;
}
if(!$hasLessonControl && $exam->getType() != ExamEnum::QUIZ){
$lessonsNumberComplete = $logRepository->count([
"complete" => LessonEnum::YES,
"course" => $course->getId(),
"user" => $this->getUser()->getId()
]);
if($lessonsNumberComplete >= $lessonNumber){
$info->isAccessible = true;
return $info;
}
}else if($exam->getType() == ExamEnum::COURSE){
$lesson = $lessonRepository->getLastLesson($course);
$lessonNumberRequired = $lessonRepository->getLessonPublicNumber(
$course,
null,
true
);
$lessonsNumberComplete = $logRepository->countLessonLogOriginCompleteRequired(
$this->getUser(),
$course
);
if($lessonsNumberComplete < $lessonNumberRequired){
$info->message = $this->configuration->getLanguage(
'error_required_incomplete',
'lesson_view_error'
);
return $info;
}
}else if($exam->getType() == ExamEnum::MODULE){
$lesson = $lessonRepository->getLastLesson($course, $exam->getLessonModule());
$lessonNumberRequired = $lessonRepository->getLessonPublicNumber(
$course,
$exam->getLessonModule(),
true
);
$lessonNumberModule = $lessonRepository->getLessonPublicNumber(
$course,
$exam->getLessonModule(),
false,
true
);
$lessonsNumberComplete = $logRepository->countLessonLogOriginCompleteRequired(
$this->getUser(),
$course,
$exam->getLessonModule()
);
if($lessonsNumberComplete < $lessonNumberRequired){
$info->message = $this->configuration->getLanguage(
'error_required_incomplete',
'lesson_view_error'
);
return $info;
}
}else if($exam->getType() == ExamEnum::LESSON){
$lesson = $exam->getLesson();
}else if($exam->getType() == ExamEnum::QUIZ){
$lesson = $exam->getLesson();
}
if($lesson && $hasLessonControl){
$currentLesson = $lessonRepository->getCurrentLesson($lesson, $this->getUser());
if(empty($currentLesson)){
if(empty($examUser)){
$currentExam = $examUserRepository->getCurrentExamOnCourseToUserByLesson(
$lesson,
$this->getUser()
);
if($currentExam && $currentExam->getId() != $exam->getId()){
if($exam->getType() != ExamEnum::QUIZ){
$info->message = $this->configuration->getLanguage(
'error_required_incomplete',
'lesson_view_error'
);
return $info;
}
}
}
$logId = "{$course->getId()}#{$this->getUser()->getId()}#{$lesson->getId()}";
$log = $lessonLogRepository->find($logId);
if($lesson->getControlRequirement() == LessonEnum::YES){
if(!$log || ($log && empty($log->getDateConclusion()))){
$info->message = $this->configuration->getLanguage(
'error_required_incomplete',
'lesson_view_error'
);
return $info;
}
}
$dateReference = ($log ? $log->getDateConclusion() : null);
if($typeRelease == ExamEnum::FIXED_DATE){
$dateStart = $exam->getDateStart();
if($dateStart > $todayDateTime){
$info->dateRelease = $dateStart;
$info->message = "{$this->configuration->getLanguage(
'error_date_release',
'lesson_view_error'
)} {$exam->getDateStart('d/m/Y H:i:s')}";
return $info;
}
}else if($typeRelease == ExamEnum::FLEXIBLE_PERIOD){
if(empty($dateReference)){
$info->message = $this->configuration->getLanguage(
'error_required_incomplete',
'lesson_view_error'
);
return $info;
}
$textDate = "{$dateReference} + {$exam->getPeriodStart()} days";
$dateStart = date('Y-m-d', strtotime($textDate));
if($dateStart > $today){
$info->dateRelease = $dateStart;
$dateStartText = date('d/m/Y H:i:s', strtotime($textDate));
$info->message = "{$this->configuration->getLanguage(
'error_date_release',
'lesson_view_error'
)} {$dateStartText}";
return $info;
}
}
$periodEnd = $exam->getPeriodEnd();
if(!empty($periodEnd)){
if($typeRelease == ExamEnum::FIXED_DATE){
$dateReference = $exam->getDateStart();
}
if(empty($dateReference) && $typeRelease != ExamEnum::RELEASED){
$info->message = $this->configuration->getLanguage(
'error_required_incomplete',
'lesson_view_error'
);
return $info;
}
$textDate = "{$dateReference} + {$periodEnd} days";
$dateEnd = date('Y-m-d', strtotime($textDate));
if($dateEnd < $today){
if(empty($examUser)){
$dateEndText = date('d/m/Y H:i:s', strtotime($textDate));
$info->dateClose = $dateEnd;
$info->message = "{$this->configuration->getLanguage(
'error_date_end',
'lesson_view_error'
)} {$dateEndText}";
return $info;
}
}
}
$info->isAccessible = true;
return $info;
}
}else if(empty($lessonNumber) || empty($lessonNumberModule) || !$hasLessonControl){
if($typeRelease == ExamEnum::FIXED_DATE){
$dateStart = $exam->getDateStart();
if($dateStart > $todayDateTime){
$info->dateRelease = $dateStart;
$info->message = "{$this->configuration->getLanguage(
'error_date_release',
'lesson_view_error'
)} {$exam->getDateStart('d/m/Y H:i:s')}";
return $info;
}
$info->isAccessible = true;
return $info;
}
//else if($typeRelease == ExamEnum::FLEXIBLE_PERIOD){
//}
$periodEnd = $exam->getPeriodEnd();
if(!empty($periodEnd)){
$dateReference = null;
if($typeRelease == ExamEnum::FIXED_DATE){
$dateReference = $exam->getDateStart();
}
if(empty($dateReference) && $typeRelease != ExamEnum::RELEASED){
$info->message = $this->configuration->getLanguage(
'error_required_incomplete',
'lesson_view_error'
);
return $info;
}
/*$textDate = "{$dateReference} + {$periodEnd} days";
$dateEnd = date('Y-m-d', strtotime($textDate));
if($dateEnd < $today){
if(empty($examUser)){
$info->dateClose = $dateEnd;
return $info;
}
}*/
}
$info->isAccessible = true;
return $info;
}
return $info;
}
public function getExamNumberByProduct(Product $product)
{
$query = $this->createQueryBuilder('e');
$query->select("COUNT(e.id) AS total");
$query->innerJoin('EADPlataforma:Product', 'p', 'WITH', 'e.course MEMBER OF p.course');
$query->andWhere('e.deleted = :deleted');
$query->andWhere('e.status = :examStatus');
$query->andWhere('p.id = :productId');
$query->setParameter('deleted', ExamEnum::ITEM_NO_DELETED);
$query->setParameter('examStatus', ExamEnum::PUBLISHED);
$query->setParameter('productId', $product->getId());
$result = (object)$query->getQuery()->getOneOrNullResult();
return $result->total;
}
public function getNextExamIdByLesson(Lesson $lesson, bool $addTitle = false)
{
$lessonRepository = $this->em->getRepository(Lesson::class);
$course = $lesson->getCourse();
$lessonModule = $lesson->getLessonModule();
$addExams = false;
$addExamsLimited = false;
if(
$this->configuration->isModuleActive("exam_module") &&
$this->configuration->checkModuleIsAbleOnPlan("examFunction")
){
$addExams = true;
$addExamsLimited = $this->configuration->checkModuleIsAbleOnPlan(
"unlimitedExamFunction"
);
}
$exam = $this->findOneBy([
"deleted" => ExamEnum::ITEM_NO_DELETED,
"course" => $course->getId(),
"lessonModule" => $lessonModule->getId(),
"lesson" => $lesson->getId(),
"status" => ExamEnum::PUBLISHED,
"type" => ExamEnum::QUIZ
]);
if(!$exam){
if(!$addExams || !$addExamsLimited){
return;
}
$exam = $this->findOneBy([
"deleted" => ExamEnum::ITEM_NO_DELETED,
"course" => $course->getId(),
"lessonModule" => $lessonModule->getId(),
"lesson" => $lesson->getId(),
"status" => ExamEnum::PUBLISHED,
"type" => ExamEnum::LESSON
]);
}
if(!$exam && $lessonRepository->isLastModuleLesson($lesson)){
if(!$addExams || !$addExamsLimited){
return;
}
$exam = $this->findOneBy([
"deleted" => ExamEnum::ITEM_NO_DELETED,
"course" => $course->getId(),
"lessonModule" => $lessonModule->getId(),
"status" => ExamEnum::PUBLISHED,
"type" => ExamEnum::MODULE
]);
}
if(!$exam && $lessonRepository->isLastCourseLesson($lesson)){
if(!$addExams){
return;
}
$exam = $this->findOneBy([
"deleted" => ExamEnum::ITEM_NO_DELETED,
"course" => $course->getId(),
"status" => ExamEnum::PUBLISHED,
"type" => ExamEnum::COURSE
]);
}
if($exam){
if($addTitle){
return (object)[
"id" => $exam->getId(),
"title" => $exam->getTitle(),
"courseId" => $exam->getCourse()->getId(),
"moduleId" => (
$exam->getLessonModule() ?
$exam->getLessonModule()->getId() :
null
),
"moduleTitle" => (
$exam->getLessonModule() ?
$exam->getLessonModule()->getTitle() :
null
),
"lessonId" => (
$exam->getLesson() ?
$exam->getLesson()->getId() :
null
),
"type" => $exam->getType(),
];
}
return $exam->getId();
}
return;
}
public function getCurrentNotStartedExamUser(Lesson $lessonBase, User $user){
$query = $this->createQueryBuilder('e');
$query->leftJoin(
'EADPlataforma:LessonModule',
'lm',
'WITH',
'lm.id = e.lessonModule'
);
$query->leftJoin(
'EADPlataforma:Lesson',
'l',
'WITH',
'l.id = e.lesson'
);
$query->andWhere('e.course = :course');
$query->andWhere('e.requirement = :yes');
$query->andWhere('e.status = :statusExam');
$query->andWhere('(
(
e.type = :typeLesson
AND l.deleted = :lDeleted
AND l.status = :lStatus
AND lm.status = :lmStatus
) OR
(
e.type = :typeModule
AND lm.deleted = :lmDeleted
AND lm.status = :lmStatus
)
)');
$query->andWhere('(
( l.order <= :orderL AND lm.order = :orderLM ) OR
( lm.order < :orderLM )
)');
$query->andWhere('e.deleted = :deleted');
$course = $lessonBase->getCourse();
$lessonModule = $lessonBase->getLessonModule();
$subQuery = $this->createQueryBuilder('examUser');
$subQuery->select('IDENTITY(eu.exam)');
$subQuery->from('EADPlataforma:ExamUser', 'eu');
$subQuery->andWhere('eu.course = :course');
$subQuery->andWhere('eu.deleted = :deletedExamUser');
$subQuery->andWhere('eu.status = :statusAproved OR eu.releaseAccess = :yes');
$subQuery->andWhere('eu.user = :user');
$subQuery = $subQuery->getDQL();
$query->andWhere($query->expr()->notIn('e.id', $subQuery));
$secondsDay = 24 * 60 * 60;
$query->andWhere("
(e.periodEnd IS NULL OR e.periodEnd = :no) OR
(
( e.typeRelease = :typeStartReleased )
OR
(
e.typeRelease = :typeStartFixedDate
AND TIME_TO_SEC(e.dateStart) <= TIME_TO_SEC(:now)
AND (
TIME_TO_SEC(e.dateStart) + (e.periodEnd * {$secondsDay})
) >= TIME_TO_SEC(:now)
)
OR
(
e.typeRelease = :typeStartFlexPeriod
)
)
");
/* !!!
$caseDate = "CASE
WHEN e.type = :typeLesson
AND (
SELECT
lg.id
FROM EADPlataforma:LessonLogOrigin AS lg
WHERE lg.user = :user
AND lg.lesson = e.lesson
AND lg.complete = :completed
LIMIT 1
) > 0
THEN
(
SELECT
lg.dateConclusion
FROM EADPlataforma:LessonLogOrigin AS lg
WHERE lg.user = :user
AND lg.lesson = e.lesson
AND lg.complete = :completed
LIMIT 1
)
WHEN e.type = :typeModule
AND (
SELECT
lg.id
FROM EADPlataforma:LessonLogOrigin AS lg
WHERE lg.user = :user
AND lg.complete = :completed
AND lg.lesson = (
SELECT
le.aula_id
FROM EADPlataforma:Lesson AS le
WHERE le.lessonModule = e.lessonModule
AND le.status = :statusL
AND le.deleted = 0
ORDER BY le.order DESC LIMIT 1
)
LIMIT 1
) > 0
THEN
(
SELECT
lg.dateConclusion
FROM EADPlataforma:LessonLogOrigin AS lg
WHERE lg.user = :user
AND lg.complete = :completed
AND lg.lesson = (
SELECT
le.aula_id
FROM EADPlataforma:Lesson AS le
WHERE le.lessonModule = e.lessonModule
AND le.status = :statusL
AND le.deleted = 0
ORDER BY le.order DESC LIMIT 1
)
LIMIT 1
)
ELSE
'0000-00-00 00:00:00'
END";
$caseLogId = "CASE
WHEN e.type = :typeLesson
THEN
(
SELECT
lg.id
FROM EADPlataforma:LessonLogOrigin AS lg
WHERE lg.user = :user
AND lg.lesson = e.lesson
AND lg.complete = :completed
LIMIT 1
)
WHEN e.type = :typeModule
THEN
(
SELECT
lg.id
FROM EADPlataforma:LessonLogOrigin AS lg
WHERE lg.user = :user
AND lg.complete = :completed
AND lg.lesson = (
SELECT
le.aula_id
FROM EADPlataforma:Lesson AS le
WHERE le.lessonModule = e.lessonModule
AND le.status = :statusL
AND le.deleted = 0
ORDER BY le.order DESC LIMIT 1
)
LIMIT 1
)
ELSE 0
END";
AND (1) > 0
AND (
TIME_TO_SEC({$caseDate}) + (e.periodStart * {$secondsDay})
) <= TIME_TO_SEC(:now)
AND (
TIME_TO_SEC({$caseDate}) + (e.periodEnd * {$secondsDay})
) >= TIME_TO_SEC(:now)
*/
$query->setParameter('lmDeleted', LessonModuleEnum::ITEM_NO_DELETED);
$query->setParameter('lmStatus', LessonModuleEnum::PUBLISHED);
$query->setParameter('lDeleted', LessonEnum::ITEM_NO_DELETED);
$query->setParameter('lStatus', LessonEnum::PUBLISHED);
$query->setParameter('now', date('Y-m-d H:i:s'));
$query->setParameter('typeStartReleased', ExamEnum::RELEASED);
$query->setParameter('typeStartFixedDate', ExamEnum::FIXED_DATE);
$query->setParameter('typeStartFlexPeriod', ExamEnum::FLEXIBLE_PERIOD);
$query->setParameter('course', $course->getId());
$query->setParameter('user', $user->getId());
$query->setParameter('yes', ExamEnum::YES);
$query->setParameter('no', ExamEnum::NO);
$query->setParameter('statusAproved', ExamUserEnum::APPROVED);
$query->setParameter('statusExam', ExamEnum::PUBLISHED);
$query->setParameter('typeModule', ExamEnum::MODULE);
$query->setParameter('typeLesson', ExamEnum::LESSON);
$query->setParameter('orderL', $lessonBase->getOrder());
$query->setParameter('orderLM', $lessonModule->getOrder());
$query->setParameter('deleted', ExamEnum::ITEM_NO_DELETED);
$query->setParameter('deletedExamUser', ExamUserEnum::ITEM_NO_DELETED);
$query->setMaxResults(1);
return $query->getQuery()->getOneOrNullResult();
}
public function getLastExamIdByLesson(Lesson $lessonBase, bool $complete = false)
{
$addExams = false;
$addExamsLimited = false;
if(
$this->configuration->isModuleActive("exam_module") &&
$this->configuration->checkModuleIsAbleOnPlan("examFunction")
){
$addExams = true;
$addExamsLimited = $this->configuration->checkModuleIsAbleOnPlan(
"unlimitedExamFunction"
);
}
$lessonRepository = $this->em->getRepository(Lesson::class);
$lessonIdBefore = $lessonRepository->getLessonIdBeforeThat($lessonBase);
$lesson = $lessonRepository->findOneBy([
"id" => $lessonIdBefore,
"deleted" => ExamEnum::ITEM_NO_DELETED,
]);
if(!$lesson){
return;
}
$course = $lesson->getCourse();
$lessonModule = $lesson->getLessonModule();
$exam = null;
if(!$exam && $lessonRepository->isLastModuleLesson($lesson)){
if(!$addExams || !$addExamsLimited){
return;
}
$exam = $this->findOneBy([
"deleted" => ExamEnum::ITEM_NO_DELETED,
"course" => $course->getId(),
"lessonModule" => $lessonModule->getId(),
"status" => ExamEnum::PUBLISHED,
"type" => ExamEnum::MODULE
]);
}
if(!$exam){
if(!$addExams || !$addExamsLimited){
return;
}
$exam = $this->findOneBy([
"deleted" => ExamEnum::ITEM_NO_DELETED,
"course" => $course->getId(),
"lessonModule" => $lessonModule->getId(),
"lesson" => $lesson->getId(),
"status" => ExamEnum::PUBLISHED,
"type" => ExamEnum::LESSON
]);
}
if(!$exam){
$exam = $this->findOneBy([
"deleted" => ExamEnum::ITEM_NO_DELETED,
"course" => $course->getId(),
"lessonModule" => $lessonModule->getId(),
"lesson" => $lesson->getId(),
"status" => ExamEnum::PUBLISHED,
"type" => ExamEnum::QUIZ
]);
}
if($exam){
if($complete){
return (object)[
"id" => $exam->getId(),
"title" => $exam->getTitle(),
"courseId" => $exam->getCourse()->getId(),
"moduleId" => (
$exam->getLessonModule() ?
$exam->getLessonModule()->getId() :
null
),
"moduleTitle" => (
$exam->getLessonModule() ?
$exam->getLessonModule()->getTitle() :
null
),
"lessonId" => (
$exam->getLesson() ?
$exam->getLesson()->getId() :
null
),
"type" => $exam->getType(),
];
}
return $exam->getId();
}
return;
}
public function sumCourseWeight($courseId)
{
$query = $this->createQueryBuilder('e');
$query->select('SUM(e.examWeight) AS examWeight');
$query->andWhere('e.status = :yes');
$query->andWhere('e.requirement = :yes');
$query->andWhere('e.course = :course');
$query->andWhere('e.deleted = :deleted');
$query->setParameter('course', $courseId);
$query->setParameter('yes', ExamEnum::YES);
$query->setParameter('deleted', ExamEnum::ITEM_NO_DELETED);
$result = (object)$query->getQuery()->getOneOrNullResult();
return $result->examWeight;
}
public function countCourseWeight($courseId)
{
$query = $this->createQueryBuilder('e');
$query->select('COUNT(e.id) AS total');
$query->andWhere('e.status = 1');
$query->andWhere('e.examWeight > 0');
$query->andWhere('e.requirement = 1');
$query->andWhere('e.course = :course');
$query->setParameter('course', $courseId);
$result = (object)$query->getQuery()->getOneOrNullResult();
return $result->total;
}
public function countValidExamByCourse(int $courseId)
{
$query = $this->createQueryBuilder('e');
$query->innerJoin('EADPlataforma:Course', 'c', 'WITH', 'c.id = e.course');
$query->leftJoin('EADPlataforma:LessonModule', 'lm', 'WITH', 'lm.id = e.lessonModule');
$query->leftJoin('EADPlataforma:Lesson', 'l', 'WITH', 'l.id = e.lesson');
$query->select('COUNT(e.id) AS total');
$query->andWhere('e.deleted = :examDeleted');
$query->andWhere('e.status = :examStatus');
$query->andWhere('e.examWeight > 0');
//$query->andWhere('e.requirement = :yes');
$query->andWhere('e.course = :course');
$query->andWhere('
(
(e.type = :typeCourse AND c.deleted = :courseDeleted)
OR (
e.type = :typeLessonModule
AND lm.status = :lessonModuleStatus
AND lm.deleted = :lessonModuleDeleted
)
OR (
e.type = :typeLesson
AND l.status = :lessonStatus
AND l.deleted = :lessonDeleted
)
)
');
$query->setParameter('typeCourse', ExamEnum::COURSE);
$query->setParameter('typeLessonModule', ExamEnum::MODULE);
$query->setParameter('typeLesson', ExamEnum::LESSON);
$query->setParameter('examDeleted', ExamEnum::ITEM_NO_DELETED);
$query->setParameter('courseDeleted', CourseEnum::ITEM_NO_DELETED);
$query->setParameter('lessonModuleDeleted', LessonModuleEnum::ITEM_NO_DELETED);
$query->setParameter('lessonDeleted', LessonEnum::ITEM_NO_DELETED);
$query->setParameter('lessonModuleStatus', LessonModuleEnum::PUBLISHED);
$query->setParameter('lessonStatus', LessonEnum::PUBLISHED);
$query->setParameter('examStatus', ExamEnum::PUBLISHED);
//$query->setParameter('yes', ExamEnum::YES);
$query->setParameter('course', $courseId);
$result = (object)$query->getQuery()->getOneOrNullResult();
return $result->total;
}
public function copyExamQuestion(Exam $oldExam, Exam $newExam)
{
$questionRepository = $this->em->getRepository(Question::class);
$questions = $oldExam->getQuestion();
if($questions){
foreach ($questions as $key => $question) {
if($question->isLive()){
if($oldExam->getCourse()->getId() != $newExam->getCourse()->getId()){
$newQuestion = $questionRepository->copyQuestionsByCourse(
$question,
$newExam->getCourse()
);
if($newQuestion){
$newExam->addQuestion($newQuestion);
}
}else{
$newExam->addQuestion($question);
}
}
}
}
return $newExam;
}
public function copyExamByCourse(Course $oldCourse, Course $newCourse)
{
$oldExam = $this->findOneBy([
"course" => $oldCourse->getId(),
"type" => ExamEnum::COURSE,
"deleted" => ExamEnum::ITEM_NO_DELETED
]);
if($oldExam){
$newExam = new Exam();
$newExam->setTitle($oldExam->getTitle());
$newExam->setType($oldExam->getType());
$newExam->setStatus($oldExam->getStatus());
$newExam->setRequirement($oldExam->getRequirement());
$newExam->setQuestionOrder($oldExam->getQuestionOrder());
$newExam->setQuestionNumber($oldExam->getQuestionNumber());
$newExam->setExamAverage($oldExam->getExamAverage());
$newExam->setExamWeight($oldExam->getExamWeight());
$newExam->setTypeRelease($oldExam->getTypeRelease());
$newExam->setPeriodStart($oldExam->getPeriodStart());
$newExam->setPeriodEnd($oldExam->getPeriodEnd());
$newExam->setDateStart($oldExam->getDateStart());
$newExam->setExamTime($oldExam->getExamTime());
$newExam->setAttempts($oldExam->getAttempts());
$newExam->setAttemptsNumber($oldExam->getAttemptsNumber());
$newExam->setAttemptsAutoRelease($oldExam->getAttemptsAutoRelease());
$newExam->setAttemptsTypeRelease($oldExam->getAttemptsTypeRelease());
$newExam->setAttemptsTime($oldExam->getAttemptsTime());
$newExam->setAttemptsPeriod($oldExam->getAttemptsPeriod());
$newExam->setShowTemplate($oldExam->getShowTemplate());
$newExam->setNote($oldExam->getNote());
$newExam->setDateLastNotify($oldExam->getDateLastNotify());
$newExam->setCourse($newCourse);
$newExam = $this->copyExamQuestion($oldExam, $newExam);
$this->em->persist($newExam);
$this->em->flush();
}
}
public function copyExamByModule(LessonModule $oldModule, LessonModule $newModule)
{
$oldExam = $this->findOneBy([
"course" => $oldModule->getCourse()->getId(),
"lessonModule"=> $oldModule->getId(),
"type" => ExamEnum::MODULE,
"deleted" => ExamEnum::ITEM_NO_DELETED
]);
if($oldExam){
$newExam = new Exam();
$newExam->setTitle($oldExam->getTitle());
$newExam->setType($oldExam->getType());
$newExam->setStatus($oldExam->getStatus());
$newExam->setRequirement($oldExam->getRequirement());
$newExam->setQuestionOrder($oldExam->getQuestionOrder());
$newExam->setQuestionNumber($oldExam->getQuestionNumber());
$newExam->setExamAverage($oldExam->getExamAverage());
$newExam->setExamWeight($oldExam->getExamWeight());
$newExam->setTypeRelease($oldExam->getTypeRelease());
$newExam->setPeriodStart($oldExam->getPeriodStart());
$newExam->setPeriodEnd($oldExam->getPeriodEnd());
$newExam->setDateStart($oldExam->getDateStart());
$newExam->setExamTime($oldExam->getExamTime());
$newExam->setAttempts($oldExam->getAttempts());
$newExam->setAttemptsNumber($oldExam->getAttemptsNumber());
$newExam->setAttemptsAutoRelease($oldExam->getAttemptsAutoRelease());
$newExam->setAttemptsTypeRelease($oldExam->getAttemptsTypeRelease());
$newExam->setAttemptsTime($oldExam->getAttemptsTime());
$newExam->setAttemptsPeriod($oldExam->getAttemptsPeriod());
$newExam->setShowTemplate($oldExam->getShowTemplate());
$newExam->setNote($oldExam->getNote());
$newExam->setDateLastNotify($oldExam->getDateLastNotify());
$newExam->setCourse($newModule->getCourse());
$newExam->setLessonModule($newModule);
$newExam = $this->copyExamQuestion($oldExam, $newExam);
$this->em->persist($newExam);
$this->em->flush();
}
}
public function copyExamByLesson(Lesson $oldLesson, Lesson $newLesson)
{
$oldExam = $this->findOneBy([
"course" => $oldLesson->getCourse()->getId(),
"lessonModule" => $oldLesson->getLessonModule()->getId(),
"lesson"=> $oldLesson->getId(),
"type" => ExamEnum::LESSON,
"deleted" => ExamEnum::ITEM_NO_DELETED
]);
if($oldExam){
$newExam = new Exam();
$newExam->setTitle($oldExam->getTitle());
$newExam->setType($oldExam->getType());
$newExam->setStatus($oldExam->getStatus());
$newExam->setRequirement($oldExam->getRequirement());
$newExam->setQuestionOrder($oldExam->getQuestionOrder());
$newExam->setQuestionNumber($oldExam->getQuestionNumber());
$newExam->setExamAverage($oldExam->getExamAverage());
$newExam->setExamWeight($oldExam->getExamWeight());
$newExam->setTypeRelease($oldExam->getTypeRelease());
$newExam->setPeriodStart($oldExam->getPeriodStart());
$newExam->setPeriodEnd($oldExam->getPeriodEnd());
$newExam->setDateStart($oldExam->getDateStart());
$newExam->setExamTime($oldExam->getExamTime());
$newExam->setAttempts($oldExam->getAttempts());
$newExam->setAttemptsNumber($oldExam->getAttemptsNumber());
$newExam->setAttemptsAutoRelease($oldExam->getAttemptsAutoRelease());
$newExam->setAttemptsTypeRelease($oldExam->getAttemptsTypeRelease());
$newExam->setAttemptsTime($oldExam->getAttemptsTime());
$newExam->setAttemptsPeriod($oldExam->getAttemptsPeriod());
$newExam->setShowTemplate($oldExam->getShowTemplate());
$newExam->setNote($oldExam->getNote());
$newExam->setDateLastNotify($oldExam->getDateLastNotify());
$newExam->setCourse($newLesson->getCourse());
$newExam->setLessonModule($newLesson->getLessonModule());
$newExam->setLesson($newLesson);
$newExam = $this->copyExamQuestion($oldExam, $newExam);
$this->em->persist($newExam);
$this->em->flush();
}
}
public function copyQuizByLesson(Lesson $oldLesson, Lesson $newLesson)
{
$oldExam = $this->findOneBy([
"course" => $oldLesson->getCourse()->getId(),
"lessonModule" => $oldLesson->getLessonModule()->getId(),
"lesson"=> $oldLesson->getId(),
"type" => ExamEnum::QUIZ,
"deleted" => ExamEnum::ITEM_NO_DELETED
]);
if($oldExam){
$newExam = new Exam();
$newExam->setTitle($oldExam->getTitle());
$newExam->setType($oldExam->getType());
$newExam->setStatus($oldExam->getStatus());
$newExam->setRequirement($oldExam->getRequirement());
$newExam->setQuestionOrder($oldExam->getQuestionOrder());
$newExam->setQuestionNumber($oldExam->getQuestionNumber());
$newExam->setExamAverage($oldExam->getExamAverage());
$newExam->setExamWeight($oldExam->getExamWeight());
$newExam->setTypeRelease($oldExam->getTypeRelease());
$newExam->setPeriodStart($oldExam->getPeriodStart());
$newExam->setPeriodEnd($oldExam->getPeriodEnd());
$newExam->setDateStart($oldExam->getDateStart());
$newExam->setExamTime($oldExam->getExamTime());
$newExam->setAttempts($oldExam->getAttempts());
$newExam->setAttemptsNumber($oldExam->getAttemptsNumber());
$newExam->setAttemptsAutoRelease($oldExam->getAttemptsAutoRelease());
$newExam->setAttemptsTypeRelease($oldExam->getAttemptsTypeRelease());
$newExam->setAttemptsTime($oldExam->getAttemptsTime());
$newExam->setAttemptsPeriod($oldExam->getAttemptsPeriod());
$newExam->setShowTemplate($oldExam->getShowTemplate());
$newExam->setNote($oldExam->getNote());
$newExam->setDateLastNotify($oldExam->getDateLastNotify());
$newExam->setCourse($newLesson->getCourse());
$newExam->setLessonModule($newLesson->getLessonModule());
$newExam->setLesson($newLesson);
$newExam = $this->copyExamQuestion($oldExam, $newExam);
$this->em->persist($newExam);
$this->em->flush();
}
}
public function editManyStatus(Exam $exam, $permission, $status)
{
$userPermissionUtil = $this->generalService->getUtil('UserPermissionUtil');
$allTypes = $this->configuration->checkModuleIsAbleOnPlan('unlimitedExamFunction');
if(!$allTypes){
if($exam->getType() != ExamEnum::COURSE && $exam->getType() != ExamEnum::QUIZ){
return false;
}
}
if($exam->getType() == ExamEnum::MODULE){
$permission = $userPermissionUtil->getPermission(
"course",
"lesson_module",
"exam_configuration",
"edit"
);
}else if($exam->getType() == ExamEnum::LESSON){
$permission = $userPermissionUtil->getPermission(
"course",
"lesson",
"exam_configuration",
"edit"
);
}else if($exam->getType() == ExamEnum::QUIZ){
$permission = $userPermissionUtil->getPermission(
"course",
"lesson",
"quiz_configuration",
"edit"
);
}
$isExamTeacher = $this->isExamTeacher($exam, $this->getUser());
if($isExamTeacher && $userPermissionUtil->isMiddle($permission) ||
$userPermissionUtil->isHigh($permission)){
$exam->setStatus($status);
}
}
public function deleteExam(Exam $exam): void
{
$exam->removeAllQuestion();
$exam->setTitle(null);
$exam->setStatus(ExamEnum::DRAFT);
$exam->setRequirement(ExamEnum::NO);
$exam->setQuestionOrder(ExamEnum::SEQUENTIAL);
$exam->setQuestionNumber(0);
$exam->setExamAverage(ExamEnum::DEFAULT_AVERAGE);
$exam->setExamWeight(0);
$exam->setTypeRelease(ExamEnum::RELEASED);
$exam->setControlTime(ExamEnum::NO);
$exam->setPeriodStart(0);
$exam->setPeriodEnd(0);
$exam->setDateStart(null);
$exam->setExamTime(null);
$exam->setAttempts(ExamEnum::NO);
$exam->setAttemptsNumber(1);
$exam->setAttemptsAutoRelease(ExamEnum::YES);
$exam->setAttemptsTypeRelease(ExamEnum::DAYS);
$exam->setAttemptsTime(null);
$exam->setAttemptsPeriod(null);
$exam->setShowTemplate(ExamEnum::NO);
$exam->setNote(null);
$exam->setDateLastNotify(null);
$this->em->flush();
}
public function delete(Exam $exam, $typeItem, $permission, $isTrash)
{
$userPermissionUtil = $this->generalService->getUtil('UserPermissionUtil');
$allTypes = $this->configuration->checkModuleIsAbleOnPlan('unlimitedExamFunction');
if(!$allTypes){
if($exam->getType() != ExamEnum::COURSE && $exam->getType() != ExamEnum::QUIZ){
return false;
}
}
if($exam->getType() == ExamEnum::MODULE){
$permission = $userPermissionUtil->getPermission(
"course",
"lesson_module",
"exam_configuration",
"delete"
);
}else if($exam->getType() == ExamEnum::LESSON){
$permission = $userPermissionUtil->getPermission(
"course",
"lesson",
"exam_configuration",
"delete"
);
}else if($exam->getType() == ExamEnum::QUIZ){
$permission = $userPermissionUtil->getPermission(
"course",
"lesson",
"quiz_configuration",
"delete"
);
}
$isExamTeacher = $this->isExamTeacher($exam, $this->getUser());
if($isExamTeacher || $userPermissionUtil->isHigh($permission)){
$questions = $exam->getQuestion();
if(!empty($questions)){
foreach ($questions as $key => $question) {
$exam->removeQuestion($question);
}
}
$exam->setTitle(null);
$exam->setStatus(ExamEnum::DRAFT);
$exam->setRequirement(ExamEnum::NO);
$exam->setQuestionOrder(ExamEnum::SEQUENTIAL);
$exam->setQuestionNumber(0);
$exam->setExamAverage(ExamEnum::DEFAULT_AVERAGE);
$exam->setExamWeight(0);
$exam->setTypeRelease(ExamEnum::RELEASED);
$exam->setControlTime(ExamEnum::NO);
$exam->setPeriodStart(0);
$exam->setPeriodEnd(0);
$exam->setDateStart(null);
$exam->setExamTime(null);
$exam->setAttempts(ExamEnum::NO);
$exam->setAttemptsNumber(1);
$exam->setAttemptsAutoRelease(ExamEnum::YES);
$exam->setAttemptsTypeRelease(ExamEnum::DAYS);
$exam->setAttemptsTime(null);
$exam->setAttemptsPeriod(null);
$exam->setShowTemplate(ExamEnum::NO);
$exam->setNote(null);
$exam->setDateLastNotify(null);
$this->em->flush();
}
}
public function restoreByCourse($courseId, $deleted, $typeDelete, $userDelete, $dateNow){
$sql = "UPDATE EADPlataforma:Exam AS e
SET e.deleted = :deleted, e.userDelete = :userDelete,
e.dateDelete = :dateNow
WHERE e.deleted = (1 - :deleted)
AND e.typeDelete = :typeDelete
AND e.course = :courseId";
$query = $this->em->createQuery($sql);
$query->setParameter('courseId', $courseId);
$query->setParameter('deleted', $deleted);
$query->setParameter('typeDelete', $typeDelete);
$query->setParameter('userDelete', $userDelete);
$query->setParameter('dateNow', $dateNow);
$query->execute();
return true;
}
public function deleteByCourse($courseId, $deleted, $typeDelete, $userDelete, $dateNow){
$sql = "UPDATE EADPlataforma:Exam AS e
SET e.deleted = :deleted, e.typeDelete = :typeDelete,
e.userDelete = :userDelete, e.dateDelete = :dateNow
WHERE e.deleted = (:deleted - 1)
AND e.course = :courseId";
$query = $this->em->createQuery($sql);
$query->setParameter('courseId', $courseId);
$query->setParameter('deleted', $deleted);
$query->setParameter('typeDelete', $typeDelete);
$query->setParameter('userDelete', $userDelete);
$query->setParameter('dateNow', $dateNow);
$query->execute();
return true;
}
public function restoreByLessonModule($lessonModuleId, $deleted, $typeDelete, $userDelete, $dateNow){
$sql = "UPDATE EADPlataforma:Exam AS e
SET e.deleted = :deleted, e.userDelete = :userDelete,
e.dateDelete = :dateNow
WHERE e.deleted = (1 - :deleted)
AND e.typeDelete = :typeDelete
AND e.lessonModule = :lessonModuleId";
$query = $this->em->createQuery($sql);
$query->setParameter('lessonModuleId', $lessonModuleId);
$query->setParameter('deleted', $deleted);
$query->setParameter('typeDelete', $typeDelete);
$query->setParameter('userDelete', $userDelete);
$query->setParameter('dateNow', $dateNow);
$query->execute();
return true;
}
public function deleteByLessonModule($lessonModuleId, $deleted, $typeDelete, $userDelete, $dateNow){
$sql = "UPDATE EADPlataforma:Exam AS e
SET e.deleted = :deleted, e.typeDelete = :typeDelete,
e.userDelete = :userDelete, e.dateDelete = :dateNow
WHERE e.deleted = (:deleted - 1)
AND e.lessonModule = :lessonModuleId";
$query = $this->em->createQuery($sql);
$query->setParameter('lessonModuleId', $lessonModuleId);
$query->setParameter('deleted', $deleted);
$query->setParameter('typeDelete', $typeDelete);
$query->setParameter('userDelete', $userDelete);
$query->setParameter('dateNow', $dateNow);
$query->execute();
return true;
}
public function restoreByLesson($lessonId, $deleted, $typeDelete, $userDelete, $dateNow){
$sql = "UPDATE EADPlataforma:Exam AS e
SET e.deleted = :deleted, e.userDelete = :userDelete,
e.dateDelete = :dateNow
WHERE e.deleted = (1 - :deleted)
AND e.typeDelete = :typeDelete
AND e.lesson = :lessonId";
$query = $this->em->createQuery($sql);
$query->setParameter('lessonId', $lessonId);
$query->setParameter('deleted', $deleted);
$query->setParameter('typeDelete', $typeDelete);
$query->setParameter('userDelete', $userDelete);
$query->setParameter('dateNow', $dateNow);
$query->execute();
return true;
}
public function deleteByLesson($lessonId, $deleted, $typeDelete, $userDelete, $dateNow){
$sql = "UPDATE EADPlataforma:Exam AS e
SET e.deleted = :deleted, e.typeDelete = :typeDelete,
e.userDelete = :userDelete, e.dateDelete = :dateNow
WHERE e.deleted = (:deleted - 1)
AND e.lesson = :lessonId";
$query = $this->em->createQuery($sql);
$query->setParameter('lessonId', $lessonId);
$query->setParameter('deleted', $deleted);
$query->setParameter('typeDelete', $typeDelete);
$query->setParameter('userDelete', $userDelete);
$query->setParameter('dateNow', $dateNow);
$query->execute();
return true;
}
public function deleteTrashCron()
{
$sql = "UPDATE EADPlataforma:Exam AS e
SET e.deleted = 2 WHERE e.dateDelete <= :date AND e.deleted = 1 ";
$query = $this->em->createQuery($sql);
$query->setParameter('date', date('Y-m-d H:i:s', strtotime('-90 days')));
$query->execute();
}
}