You are here

function CourseController::renderComplete in Course 8.2

Same name and namespace in other branches
  1. 8.3 src/Controller/CourseController.php \Drupal\course\Controller\CourseController::renderComplete()
  2. 3.x src/Controller/CourseController.php \Drupal\course\Controller\CourseController::renderComplete()

Render a landing page for course completion.

Parameters

Course $course:

Return value

array Render array for the completion landing page.

1 string reference to 'CourseController::renderComplete'
course.routing.yml in ./course.routing.yml
course.routing.yml

File

src/Controller/CourseController.php, line 31

Class

CourseController

Namespace

Drupal\course\Controller

Code

function renderComplete(Course $course) {
  $account = \Drupal::currentUser();

  // User's course record.
  $course_enrollment = $course
    ->getEnrollment($account);

  // Render array.
  $page = array();

  // Links.
  $links = array();
  $url = $course
    ->getUrl();
  $url
    ->setOption('title', t('Return to the course to view course details and material.'));
  $links['course'] = \Drupal\Core\Link::fromTextAndUrl(t('Return to course'), $url);
  if ($course_enrollment
    ->isComplete()) {

    // Allow modules to add links to the course completion landing page, such as
    // post-course actions.
    Drupal::moduleHandler()
      ->alter('course_outline_completion_links', $links, $course, $account);
  }
  else {
    Drupal::moduleHandler()
      ->alter('course_outline_incomplete_links', $links, $course, $account);
  }
  $page['#title'] = $course_enrollment
    ->isComplete() ? t('Course complete') : t('Remaining requirements');
  $objects = $course
    ->getObjects();
  $items = array();
  foreach ($objects as $courseObject) {
    if ($courseObject
      ->access('see')) {

      // Find required course objects the user has not yet completed.
      $req = $courseObject
        ->getFulfillment($account);
      $status_css = $req
        ->isComplete() ? 'complete' : 'incomplete';
      $status_img = $req
        ->isComplete() ? 'core/misc/icons/73b355/check.svg' : ($req
        ->getCourseObject()
        ->isRequired() ? 'core/misc/icons/e32700/error.svg' : 'core/misc/icons/e29700/warning.svg');
      $status_class = 'course-complete-item-' . $status_img;
      $status_optional = ' (' . (!$req
        ->getCourseObject()
        ->isRequired() ? t('optional') : t('required')) . ')';
      if ($courseObject
        ->access('take')) {
        $link = \Drupal\Core\Link::fromTextAndUrl($req
          ->getCourseObject()
          ->getTitle(), $courseObject
          ->getUrl())
          ->toString();
      }
      else {
        $link = $req
          ->getCourseObject()
          ->getTitle();
      }
      $items[] = array(
        'data' => array(
          array(
            'data' => [
              '#theme' => 'image',
              '#uri' => $status_img,
              '#alt' => $status_css,
            ],
            'width' => 20,
            'class' => array(
              'course-complete-item-status',
            ),
          ),
          array(
            'data' => [
              '#markup' => $link . $status_optional . '<br/>' . $req
                ->getCourseObject()
                ->getStatus(),
            ],
            'class' => array(
              'course-complete-item-title',
            ),
          ),
        ),
        'class' => array(
          $status_class,
        ),
      );
    }
  }
  if ($course_enrollment
    ->isComplete()) {
    $message = t('You have completed the course. Use the links below to review the course content.');
  }
  else {
    $message = t('This course is not complete. Use the links below to access the remaining course content.');
  }
  $page['course_header'] = array(
    '#type' => 'item',
    '#title' => t('Thank you for participating in this course.'),
    '#description' => $message,
    '#description_display' => TRUE,
    '#weight' => 1,
  );
  $page['course_completion_requirements'] = array(
    '#theme' => 'table',
    '#header' => NULL,
    '#rows' => $items,
    '#weight' => 3,
    '#attributes' => array(
      'class' => array(
        'course-complete-items',
      ),
    ),
  );
  foreach ($links as $key => $link) {
    $element = array(
      '#title' => $link
        ->toString(),
      '#description' => $link
        ->getUrl()
        ->getOption('title'),
      '#type' => 'item',
      '#description_display' => TRUE,
    );
    $page['course_links'][$key] = $element;
  }
  $page['course_links']['#weight'] = 2;
  $page['#cache']['max-age'] = 0;
  return $page;
}