Loading social_course.module +28 −25 Original line number Diff line number Diff line Loading @@ -1640,7 +1640,7 @@ function social_course_preprocess_node(array &$variables): void { } else { foreach ($entities as $entity) { if ($entity->getStatus() === CourseEnrollmentInterface::IN_PROGRESS) { if (!$entity->hasFinishedAttempt() && $entity->getStatus() === CourseEnrollmentInterface::IN_PROGRESS) { $variables['section_status'] = 'in-progress'; $variables['section_current'] = $entity->get('mid')->target_id; break; Loading Loading @@ -1842,6 +1842,9 @@ function social_course_entity_view(array &$build, EntityInterface $entity, Entit $course_wrapper = \Drupal::service('social_course.course_wrapper'); $account = \Drupal::currentUser(); $storage = \Drupal::entityTypeManager() ->getStorage('course_enrollment'); /** @var \Drupal\node\NodeInterface $entity */ if ($entity->bundle() == 'course_section') { $course_wrapper->setCourseFromSection($entity); Loading @@ -1850,22 +1853,22 @@ function social_course_entity_view(array &$build, EntityInterface $entity, Entit if ($course_wrapper->getCourse() && !$course_wrapper->courseIsSequential() && $material) { // Join user to course. $storage = \Drupal::entityTypeManager() ->getStorage('course_enrollment'); $entities = $storage->loadByProperties([ 'gid' => $course_wrapper->getCourse()->id(), 'sid' => $entity->id(), 'mid' => $material->id(), 'uid' => $account->id(), ]); $exists = $storage->getQuery() ->condition('gid', $course_wrapper->getCourse()->id()) ->condition('sid', $entity->id()) ->condition('mid', $material->id()) ->condition('uid', $account->id()) ->accessCheck() ->execute(); if (!$entities) { $storage->create([ if (!$exists) { $enrollment = $storage->create([ 'gid' => $course_wrapper->getCourse()->id(), 'sid' => $entity->id(), 'mid' => $material->id(), 'status' => CourseEnrollmentInterface::IN_PROGRESS, ])->save(); ]); $enrollment->save(); } } } Loading @@ -1876,22 +1879,22 @@ function social_course_entity_view(array &$build, EntityInterface $entity, Entit if ($course_wrapper->getCourse() && !$course_wrapper->courseIsSequential()) { // Join user to course. $storage = \Drupal::entityTypeManager() ->getStorage('course_enrollment'); $entities = $storage->loadByProperties([ 'gid' => $course_wrapper->getCourse()->id(), 'sid' => $section->id(), 'mid' => $entity->id(), 'uid' => $account->id(), ]); $exists = $storage->getQuery() ->condition('gid', $course_wrapper->getCourse()->id()) ->condition('sid', $section->id()) ->condition('mid', $entity->id()) ->condition('uid', $account->id()) ->accessCheck() ->execute(); if (!$entities) { $storage->create([ if (!$exists) { $enrollment = $storage->create([ 'gid' => $course_wrapper->getCourse()->id(), 'sid' => $section->id(), 'mid' => $entity->id(), 'status' => CourseEnrollmentInterface::IN_PROGRESS, ])->save(); ]); $enrollment->save(); } } } Loading src/Controller/CoursesController.php +39 −30 Original line number Diff line number Diff line Loading @@ -94,27 +94,28 @@ class CoursesController extends ControllerBase { 'node' => $material->id(), ]); /** @var \Drupal\Core\Entity\EntityStorageInterface $course_enrollment_storage */ $course_enrollment_storage = $this->entityTypeManager()->getStorage('course_enrollment'); $entities = $course_enrollment_storage->loadByProperties([ 'uid' => $this->currentUser()->id(), 'sid' => $node->id(), ]); $storage = $this->entityTypeManager()->getStorage('course_enrollment'); $exists = $storage->getQuery() ->condition('uid', $this->currentUser()->id()) ->condition('sid', $node->id()) ->accessCheck() ->execute(); // If user has already started or finished section, we just redirect them // to the material instead of creating a new enrollment. if ($entities) { if ($exists) { return $material_redirect; } // Join user to course. $storage = $this->entityTypeManager()->getStorage('course_enrollment'); $storage->create([ $enrollment = $storage->create([ 'gid' => $group->id(), 'sid' => $node->id(), 'mid' => $material->id(), 'status' => CourseEnrollmentInterface::IN_PROGRESS, ])->save(); ]); $enrollment->save(); $this->messenger()->addMessage($this->t('You have successfully enrolled')); Loading Loading @@ -146,30 +147,37 @@ class CoursesController extends ControllerBase { $account = $this->currentUser(); foreach ($field->getValue() as $key => $value) { $course_enrollment = $storage->loadByProperties([ $exists = $storage->getQuery() ->condition('gid', $group->id()) ->condition('sid', $node->id()) ->condition('mid', $value['target_id']) ->condition('uid', $account->id()) ->accessCheck() ->execute(); if (!$exists) { $enrollment = $storage->create([ 'gid' => $group->id(), 'sid' => $node->id(), 'mid' => $value['target_id'], 'uid' => $account->id(), 'status' => CourseEnrollmentInterface::IN_PROGRESS, ]); $enrollment->save(); if (!$course_enrollment) { $storage->create([ 'gid' => $group->id(), 'sid' => $node->id(), 'mid' => $value['target_id'], 'status' => CourseEnrollmentInterface::IN_PROGRESS, ])->save(); $next_material = $field->get($key)->entity; break; } elseif (!$current_material) { $course_enrollment = current($course_enrollment); /** @var \Drupal\social_course\Entity\CourseEnrollment[] $course_enrollments */ $course_enrollments = $storage->loadMultiple($exists); // For some reasons user can have more than one enrollment. So, here // we need to update status for all if the current attempt is finished. foreach ($course_enrollments as $enrollment) { // Set the correct status for all previous materials. if ($course_enrollment->getStatus() !== CourseEnrollmentInterface::FINISHED) { $course_enrollment->setStatus(CourseEnrollmentInterface::FINISHED); $course_enrollment->save(); if ($enrollment->getStatus() !== CourseEnrollmentInterface::FINISHED) { $enrollment->setStatus(CourseEnrollmentInterface::FINISHED); $enrollment->save(); if ($field->get($key + 1)) { $current_material = $field->get($key)->entity; Loading @@ -178,6 +186,7 @@ class CoursesController extends ControllerBase { } } } } // Redirect after finishing course. if (!$group->get('field_course_redirect_url')->isEmpty()) { Loading src/CourseWrapper.php +3 −2 Original line number Diff line number Diff line Loading @@ -154,7 +154,8 @@ class CourseWrapper implements CourseWrapperInterface { } foreach ($entities as $entity) { if ($entity->getStatus() === CourseEnrollmentInterface::IN_PROGRESS) { /** @var \Drupal\social_course\Entity\CourseEnrollmentInterface $entity */ if (!$entity->hasFinishedAttempt()) { return CourseEnrollmentInterface::IN_PROGRESS; } } Loading @@ -179,7 +180,7 @@ class CourseWrapper implements CourseWrapperInterface { foreach ($entities as $entity) { /** @var \Drupal\social_course\Entity\CourseEnrollmentInterface $entity */ if ($entity->getStatus() === CourseEnrollmentInterface::IN_PROGRESS) { if (!$entity->hasFinishedAttempt()) { return CourseEnrollmentInterface::IN_PROGRESS; } } Loading src/Entity/CourseEnrollment.php +17 −0 Original line number Diff line number Diff line Loading @@ -177,4 +177,21 @@ class CourseEnrollment extends ContentEntityBase implements CourseEnrollmentInte return $this; } /** * {@inheritdoc} */ public function hasFinishedAttempt(): bool { $attempts = $this->entityTypeManager()->getStorage('course_enrollment') ->getQuery() ->condition('gid', $this->getCourseId()) ->condition('sid', $this->getSectionId()) ->condition('mid', $this->getMaterialId()) ->condition('uid', $this->getOwnerId()) ->condition('status', self::FINISHED) ->accessCheck(FALSE) ->execute(); return (bool) $attempts; } } src/Entity/CourseEnrollmentInterface.php +7 −0 Original line number Diff line number Diff line Loading @@ -85,4 +85,11 @@ interface CourseEnrollmentInterface extends ContentEntityInterface, EntityOwnerI */ public function setStatus($status); /** * Check if user has finished attempt among same user enrollments. * * @return bool * Displays if user was able to finish attempt. */ public function hasFinishedAttempt(): bool; } Loading
social_course.module +28 −25 Original line number Diff line number Diff line Loading @@ -1640,7 +1640,7 @@ function social_course_preprocess_node(array &$variables): void { } else { foreach ($entities as $entity) { if ($entity->getStatus() === CourseEnrollmentInterface::IN_PROGRESS) { if (!$entity->hasFinishedAttempt() && $entity->getStatus() === CourseEnrollmentInterface::IN_PROGRESS) { $variables['section_status'] = 'in-progress'; $variables['section_current'] = $entity->get('mid')->target_id; break; Loading Loading @@ -1842,6 +1842,9 @@ function social_course_entity_view(array &$build, EntityInterface $entity, Entit $course_wrapper = \Drupal::service('social_course.course_wrapper'); $account = \Drupal::currentUser(); $storage = \Drupal::entityTypeManager() ->getStorage('course_enrollment'); /** @var \Drupal\node\NodeInterface $entity */ if ($entity->bundle() == 'course_section') { $course_wrapper->setCourseFromSection($entity); Loading @@ -1850,22 +1853,22 @@ function social_course_entity_view(array &$build, EntityInterface $entity, Entit if ($course_wrapper->getCourse() && !$course_wrapper->courseIsSequential() && $material) { // Join user to course. $storage = \Drupal::entityTypeManager() ->getStorage('course_enrollment'); $entities = $storage->loadByProperties([ 'gid' => $course_wrapper->getCourse()->id(), 'sid' => $entity->id(), 'mid' => $material->id(), 'uid' => $account->id(), ]); $exists = $storage->getQuery() ->condition('gid', $course_wrapper->getCourse()->id()) ->condition('sid', $entity->id()) ->condition('mid', $material->id()) ->condition('uid', $account->id()) ->accessCheck() ->execute(); if (!$entities) { $storage->create([ if (!$exists) { $enrollment = $storage->create([ 'gid' => $course_wrapper->getCourse()->id(), 'sid' => $entity->id(), 'mid' => $material->id(), 'status' => CourseEnrollmentInterface::IN_PROGRESS, ])->save(); ]); $enrollment->save(); } } } Loading @@ -1876,22 +1879,22 @@ function social_course_entity_view(array &$build, EntityInterface $entity, Entit if ($course_wrapper->getCourse() && !$course_wrapper->courseIsSequential()) { // Join user to course. $storage = \Drupal::entityTypeManager() ->getStorage('course_enrollment'); $entities = $storage->loadByProperties([ 'gid' => $course_wrapper->getCourse()->id(), 'sid' => $section->id(), 'mid' => $entity->id(), 'uid' => $account->id(), ]); $exists = $storage->getQuery() ->condition('gid', $course_wrapper->getCourse()->id()) ->condition('sid', $section->id()) ->condition('mid', $entity->id()) ->condition('uid', $account->id()) ->accessCheck() ->execute(); if (!$entities) { $storage->create([ if (!$exists) { $enrollment = $storage->create([ 'gid' => $course_wrapper->getCourse()->id(), 'sid' => $section->id(), 'mid' => $entity->id(), 'status' => CourseEnrollmentInterface::IN_PROGRESS, ])->save(); ]); $enrollment->save(); } } } Loading
src/Controller/CoursesController.php +39 −30 Original line number Diff line number Diff line Loading @@ -94,27 +94,28 @@ class CoursesController extends ControllerBase { 'node' => $material->id(), ]); /** @var \Drupal\Core\Entity\EntityStorageInterface $course_enrollment_storage */ $course_enrollment_storage = $this->entityTypeManager()->getStorage('course_enrollment'); $entities = $course_enrollment_storage->loadByProperties([ 'uid' => $this->currentUser()->id(), 'sid' => $node->id(), ]); $storage = $this->entityTypeManager()->getStorage('course_enrollment'); $exists = $storage->getQuery() ->condition('uid', $this->currentUser()->id()) ->condition('sid', $node->id()) ->accessCheck() ->execute(); // If user has already started or finished section, we just redirect them // to the material instead of creating a new enrollment. if ($entities) { if ($exists) { return $material_redirect; } // Join user to course. $storage = $this->entityTypeManager()->getStorage('course_enrollment'); $storage->create([ $enrollment = $storage->create([ 'gid' => $group->id(), 'sid' => $node->id(), 'mid' => $material->id(), 'status' => CourseEnrollmentInterface::IN_PROGRESS, ])->save(); ]); $enrollment->save(); $this->messenger()->addMessage($this->t('You have successfully enrolled')); Loading Loading @@ -146,30 +147,37 @@ class CoursesController extends ControllerBase { $account = $this->currentUser(); foreach ($field->getValue() as $key => $value) { $course_enrollment = $storage->loadByProperties([ $exists = $storage->getQuery() ->condition('gid', $group->id()) ->condition('sid', $node->id()) ->condition('mid', $value['target_id']) ->condition('uid', $account->id()) ->accessCheck() ->execute(); if (!$exists) { $enrollment = $storage->create([ 'gid' => $group->id(), 'sid' => $node->id(), 'mid' => $value['target_id'], 'uid' => $account->id(), 'status' => CourseEnrollmentInterface::IN_PROGRESS, ]); $enrollment->save(); if (!$course_enrollment) { $storage->create([ 'gid' => $group->id(), 'sid' => $node->id(), 'mid' => $value['target_id'], 'status' => CourseEnrollmentInterface::IN_PROGRESS, ])->save(); $next_material = $field->get($key)->entity; break; } elseif (!$current_material) { $course_enrollment = current($course_enrollment); /** @var \Drupal\social_course\Entity\CourseEnrollment[] $course_enrollments */ $course_enrollments = $storage->loadMultiple($exists); // For some reasons user can have more than one enrollment. So, here // we need to update status for all if the current attempt is finished. foreach ($course_enrollments as $enrollment) { // Set the correct status for all previous materials. if ($course_enrollment->getStatus() !== CourseEnrollmentInterface::FINISHED) { $course_enrollment->setStatus(CourseEnrollmentInterface::FINISHED); $course_enrollment->save(); if ($enrollment->getStatus() !== CourseEnrollmentInterface::FINISHED) { $enrollment->setStatus(CourseEnrollmentInterface::FINISHED); $enrollment->save(); if ($field->get($key + 1)) { $current_material = $field->get($key)->entity; Loading @@ -178,6 +186,7 @@ class CoursesController extends ControllerBase { } } } } // Redirect after finishing course. if (!$group->get('field_course_redirect_url')->isEmpty()) { Loading
src/CourseWrapper.php +3 −2 Original line number Diff line number Diff line Loading @@ -154,7 +154,8 @@ class CourseWrapper implements CourseWrapperInterface { } foreach ($entities as $entity) { if ($entity->getStatus() === CourseEnrollmentInterface::IN_PROGRESS) { /** @var \Drupal\social_course\Entity\CourseEnrollmentInterface $entity */ if (!$entity->hasFinishedAttempt()) { return CourseEnrollmentInterface::IN_PROGRESS; } } Loading @@ -179,7 +180,7 @@ class CourseWrapper implements CourseWrapperInterface { foreach ($entities as $entity) { /** @var \Drupal\social_course\Entity\CourseEnrollmentInterface $entity */ if ($entity->getStatus() === CourseEnrollmentInterface::IN_PROGRESS) { if (!$entity->hasFinishedAttempt()) { return CourseEnrollmentInterface::IN_PROGRESS; } } Loading
src/Entity/CourseEnrollment.php +17 −0 Original line number Diff line number Diff line Loading @@ -177,4 +177,21 @@ class CourseEnrollment extends ContentEntityBase implements CourseEnrollmentInte return $this; } /** * {@inheritdoc} */ public function hasFinishedAttempt(): bool { $attempts = $this->entityTypeManager()->getStorage('course_enrollment') ->getQuery() ->condition('gid', $this->getCourseId()) ->condition('sid', $this->getSectionId()) ->condition('mid', $this->getMaterialId()) ->condition('uid', $this->getOwnerId()) ->condition('status', self::FINISHED) ->accessCheck(FALSE) ->execute(); return (bool) $attempts; } }
src/Entity/CourseEnrollmentInterface.php +7 −0 Original line number Diff line number Diff line Loading @@ -85,4 +85,11 @@ interface CourseEnrollmentInterface extends ContentEntityInterface, EntityOwnerI */ public function setStatus($status); /** * Check if user has finished attempt among same user enrollments. * * @return bool * Displays if user was able to finish attempt. */ public function hasFinishedAttempt(): bool; }