You are here

public function UserController::buildCourseDetails in Opigno statistics 8

Same name and namespace in other branches
  1. 3.x src/Controller/UserController.php \Drupal\opigno_statistics\Controller\UserController::buildCourseDetails()

Builds render array for a user course details.

Parameters

\Drupal\user\UserInterface $user: User.

\Drupal\group\Entity\GroupInterface $training: Training.

\Drupal\group\Entity\GroupInterface $course: Course.

Return value

array Render array.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

2 calls to UserController::buildCourseDetails()
UserController::ajaxLoadCourseDetails in src/Controller/UserController.php
Loads a user course details with the AJAX.
UserController::course in src/Controller/UserController.php
Builds render array for a user course statistics page.

File

src/Controller/UserController.php, line 622

Class

UserController
Class UserController.

Namespace

Drupal\opigno_statistics\Controller

Code

public function buildCourseDetails(UserInterface $user, GroupInterface $training, GroupInterface $course) {

  // Load group steps.
  $steps = opigno_learning_path_get_steps($course
    ->id(), $user
    ->id());
  $query = $this->database
    ->select('opigno_learning_path_step_achievements', 'sa');
  $query
    ->leftJoin('opigno_learning_path_step_achievements', 'sa2', 'sa2.parent_id = sa.id');
  $query = $query
    ->fields('sa2', [
    'entity_id',
    'typology',
    'name',
    'score',
    'status',
  ])
    ->condition('sa.uid', $user
    ->id())
    ->condition('sa.gid', $training
    ->id())
    ->condition('sa.entity_id', $course
    ->id())
    ->condition('sa.parent_id', 0);
  $modules = $query
    ->execute()
    ->fetchAll();
  $rows = array_map(function ($step) use ($modules, $training, $course, $user) {
    $module = NULL;
    foreach ($modules as $mod) {
      if ($mod->entity_id === $step['id']) {
        $module = $mod;
        break;
      }
    }
    $id = isset($module) ? $module->entity_id : $step['id'];
    $name = isset($module) ? $module->name : $step['name'];
    $score = isset($module) ? $module->score : 0;
    $status = isset($module) ? $module->status : 'pending';
    $typology = strtolower(isset($module) ? $module->typology : $step['typology']);
    $score = isset($score) ? $score : 0;
    $score = [
      'data' => $this
        ->buildScore($score),
    ];
    $status = isset($status) ? $status : 'pending';
    $status = [
      'data' => $this
        ->buildStatus($status),
    ];
    switch ($typology) {
      case 'module':
        $training_gid = $training
          ->id();
        $course_gid = $course
          ->id();
        $module_id = $id;
        $user_id = $user
          ->id();
        $details = Link::createFromRoute('', 'opigno_statistics.user.course_module_details', [
          'user' => $user
            ->id(),
          'training' => $training
            ->id(),
          'course' => $course
            ->id(),
          'module' => $id,
        ])
          ->toRenderable();
        $details['#attributes']['class'][] = 'details';
        $details['#attributes']['class'][] = 'course-module-details-open';
        $details['#attributes']['data-user'] = $user_id;
        $details['#attributes']['data-training'] = $training_gid;
        $details['#attributes']['data-course'] = $course_gid;
        $details['#attributes']['data-id'] = $module_id;
        $details = [
          'data' => [
            $details,
            [
              '#type' => 'container',
              '#attributes' => [
                'class' => [
                  'module-panel-wrapper',
                ],
              ],
              [
                '#type' => 'html_tag',
                '#tag' => 'span',
                '#attributes' => [
                  'id' => "module_panel_{$user_id}_{$training_gid}_{$course_gid}_{$module_id}",
                ],
              ],
            ],
          ],
        ];
        break;
      default:
        $details = '';
        break;
    }
    return [
      $name,
      $score,
      $status,
      $details,
    ];
  }, $steps);
  return [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'course-modules',
      ],
    ],
    [
      '#type' => 'table',
      '#attributes' => [
        'class' => [
          'statistics-table',
          'course-modules-list',
        ],
      ],
      '#header' => [],
      '#rows' => $rows,
    ],
  ];
}