You are here

public function UserController::buildTrainingDetails in Opigno statistics 8

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

Builds render array for a user training details.

Parameters

\Drupal\user\UserInterface $user: User.

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

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::buildTrainingDetails()
UserController::ajaxLoadTrainingDetails in src/Controller/UserController.php
Loads a user training details with the AJAX.
UserController::training in src/Controller/UserController.php
Builds render array for a user training statistics page.

File

src/Controller/UserController.php, line 750

Class

UserController
Class UserController.

Namespace

Drupal\opigno_statistics\Controller

Code

public function buildTrainingDetails(UserInterface $user, GroupInterface $group) {
  $gid = $group
    ->id();
  $uid = $user
    ->id();

  // Get user training expiration flag.
  $expired = LPStatus::isCertificateExpired($group, $uid);

  // Get training latest certification timestamp.
  $latest_cert_date = LPStatus::getTrainingStartDate($group, $uid);

  // Load group steps.
  // Get training guided navigation option.
  $freeNavigation = !OpignoGroupManagerController::getGuidedNavigation($group);
  if ($freeNavigation) {

    // Get all steps for LP.
    $steps = opigno_learning_path_get_all_steps($gid, $uid, NULL, $latest_cert_date);
  }
  else {

    // Get guided steps.
    $steps = opigno_learning_path_get_steps($gid, $uid, NULL, $latest_cert_date);
  }
  $steps_count = count($steps);
  if ($expired) {
    $passed_modules_count = 0;
    $training_data = [
      'progress' => 0,
      'score' => 0,
      'time' => 0,
    ];
    $modules = [];
  }
  else {
    $query = $this->database
      ->select('opigno_learning_path_achievements', 'a')
      ->fields('a', [
      'score',
      'progress',
      'time',
      'completed',
    ])
      ->condition('a.gid', $gid)
      ->condition('a.uid', $uid);
    $training_data = $query
      ->execute()
      ->fetchAssoc();
    $query = $this->database
      ->select('opigno_learning_path_step_achievements', 'sa')
      ->fields('sa', [
      'entity_id',
      'typology',
      'name',
      'score',
      'status',
      'time',
      'completed',
    ])
      ->condition('sa.gid', $gid)
      ->condition('sa.uid', $uid)
      ->condition('sa.parent_id', 0);
    $modules = $query
      ->execute()
      ->fetchAll();
    $passed_modules = array_filter($modules, function ($module) {
      return $module->status === 'passed';
    });
    $passed_modules_count = count($passed_modules);
  }
  $content = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'training-details-content',
      ],
    ],
  ];
  $content[] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'module-passed',
      ],
    ],
    'module_passed' => $this
      ->buildValueWithIndicator($this
      ->t('Module Passed'), $passed_modules_count / $steps_count, $this
      ->t('@passed/@total', [
      '@passed' => $passed_modules_count,
      '@total' => $steps_count,
    ])),
  ];
  $content[] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'completion',
      ],
    ],
    'completion' => $this
      ->buildValueWithIndicator($this
      ->t('Completion'), $training_data['progress'] / 100),
  ];
  $content[] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'score',
      ],
    ],
    'completion' => $this
      ->buildValueWithIndicator($this
      ->t('Score'), $training_data['score'] / 100),
  ];
  $time = isset($training_data['time']) && $training_data['time'] > 0 ? $this->date_formatter
    ->formatInterval($training_data['time']) : '-';
  if (isset($training_data['completed'])) {
    $datetime = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $training_data['completed']);
    $timestamp = $datetime
      ->getTimestamp();
    $completed_on = $this->date_formatter
      ->format($timestamp, 'custom', 'F d Y');
  }
  else {
    $completed_on = '-';
  }
  $content[] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'right-block',
      ],
    ],
    [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'time',
        ],
      ],
      [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'value-wrapper',
          ],
        ],
        [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'class' => [
              'label',
            ],
          ],
          '#value' => $this
            ->t('Time spent'),
        ],
        [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'class' => [
              'value',
            ],
          ],
          '#value' => $time,
        ],
      ],
    ],
    [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'completed',
        ],
      ],
      [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'value-wrapper',
          ],
        ],
        [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'class' => [
              'label',
            ],
          ],
          '#value' => $this
            ->t('Completed on'),
        ],
        [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'class' => [
              'value',
            ],
          ],
          '#value' => $completed_on,
        ],
      ],
    ],
  ];
  $rows = array_map(function ($step) use ($modules, $uid, $gid) {
    $module = NULL;
    foreach ($modules as $mod) {
      if ($mod->entity_id == $step['id'] && $mod->typology == $step["typology"]) {
        $module = $mod;
        break;
      }
    }
    $id = isset($module) ? $module->entity_id : $step['id'];
    $name = isset($module) ? $module->name : $step['name'];
    $score = $step['current attempt score'];
    $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 'course':
        $details = Link::createFromRoute('', 'opigno_statistics.user.course_details', [
          'user' => $uid,
          'training' => $gid,
          'course' => $id,
        ])
          ->toRenderable();
        $details['#attributes']['class'][] = 'details';
        $details['#attributes']['class'][] = 'course-details-open';
        $details['#attributes']['data-user'] = $uid;
        $details['#attributes']['data-training'] = $gid;
        $details['#attributes']['data-id'] = $id;
        $details = [
          'data' => $details,
        ];
        break;
      case 'module':
        $module_id = $id;
        $details = Link::createFromRoute('', 'opigno_statistics.user.training_module_details', [
          'user' => $uid,
          'training' => $gid,
          'module' => $module_id,
        ])
          ->toRenderable();
        $details['#attributes']['class'][] = 'details';
        $details['#attributes']['class'][] = 'training-module-details-open';
        $details['#attributes']['data-user'] = $uid;
        $details['#attributes']['data-training'] = $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_{$uid}_{$gid}_{$module_id}",
                ],
              ],
            ],
          ],
        ];
        break;
      default:
        $details = '';
        break;
    }
    $is_course = $typology === 'course';
    return [
      'class' => $is_course ? 'course' : 'module',
      'data-training' => $gid,
      'data-id' => $id,
      'data-user' => $uid,
      'data' => [
        $name,
        $score,
        $status,
        $details,
      ],
    ];
  }, $steps);
  $content[] = [
    '#type' => 'html_tag',
    '#tag' => 'hr',
  ];
  $current_route = \Drupal::routeMatch()
    ->getRouteName();
  $content[] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'training-modules',
      ],
    ],
    [
      '#type' => 'table',
      '#attributes' => [
        'class' => [
          !empty($current_route) && $current_route == 'opigno_statistics.user.training_details' ? 'trainings-list' : '',
          'statistics-table',
          'training-modules-list',
          'mb-0',
        ],
      ],
      '#header' => [
        $this
          ->t('Course / Module'),
        $this
          ->t('Results'),
        $this
          ->t('State'),
        '',
      ],
      '#rows' => $rows,
    ],
  ];
  return $content;
}