protected function UserController::buildModuleDetails in Opigno statistics 8
Same name and namespace in other branches
- 3.x src/Controller/UserController.php \Drupal\opigno_statistics\Controller\UserController::buildModuleDetails()
Build render array for a user module details.
Parameters
\Drupal\user\UserInterface $user: User.
\Drupal\group\Entity\GroupInterface $training: Training.
null|\Drupal\group\Entity\GroupInterface $course: Course.
\Drupal\opigno_module\Entity\OpignoModule $module: Module.
Return value
array Render array.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
4 calls to UserController::buildModuleDetails()
- UserController::ajaxLoadCourseModuleDetails in src/
Controller/ UserController.php - Loads module panel with a AJAX.
- UserController::ajaxLoadTrainingModuleDetails in src/
Controller/ UserController.php - Loads module panel with a AJAX.
- UserController::courseModule in src/
Controller/ UserController.php - Builds render array for a user course statistics page.
- UserController::trainingModule in src/
Controller/ UserController.php - Builds render array for a user course statistics page.
File
- src/
Controller/ UserController.php, line 355
Class
- UserController
- Class UserController.
Namespace
Drupal\opigno_statistics\ControllerCode
protected function buildModuleDetails(UserInterface $user, GroupInterface $training, $course, OpignoModule $module) {
// Get user training expiration flag.
$expired = LPStatus::isCertificateExpired($training, $user
->id());
// Get training latest certification timestamp.
$latest_cert_date = LPStatus::getTrainingStartDate($training, $user
->id());
$moduleHandler = \Drupal::service('module_handler');
$parent = isset($course) ? $course : $training;
$step = opigno_learning_path_get_module_step($parent
->id(), $user
->id(), $module, $latest_cert_date);
if ($expired) {
$completed_on = '';
}
else {
$completed_on = $step['completed on'];
$completed_on = $completed_on > 0 ? $this->date_formatter
->format($completed_on, 'custom', 'F d, Y') : '';
}
/** @var \Drupal\opigno_module\Entity\OpignoModule $module */
$module = OpignoModule::load($step['id']);
/** @var \Drupal\opigno_module\Entity\UserModuleStatus[] $attempts */
$attempts = !$expired ? $module
->getModuleAttempts($user, NULL, $latest_cert_date) : [];
if ($moduleHandler
->moduleExists('opigno_skills_system') && $module
->getSkillsActive() && $module
->getModuleSkillsGlobal() && !empty($attempts)) {
$activities_from_module = $module
->getModuleActivities();
$activity_ids = array_keys($activities_from_module);
$attempt = $this
->getTargetAttempt($attempts, $module);
$db_connection = \Drupal::service('database');
$query = $db_connection
->select('opigno_answer_field_data', 'o_a_f_d');
$query
->leftJoin('opigno_module_relationship', 'o_m_r', 'o_a_f_d.activity = o_m_r.child_id');
$query
->addExpression('MAX(o_a_f_d.activity)', 'id');
$query
->condition('o_a_f_d.user_id', $user
->id())
->condition('o_a_f_d.module', $module
->id());
if (!$module
->getModuleSkillsGlobal()) {
$query
->condition('o_a_f_d.activity', $activity_ids, 'IN');
}
$query
->condition('o_a_f_d.user_module_status', $attempt
->id())
->groupBy('o_a_f_d.activity');
$activities = $query
->execute()
->fetchAllAssoc('id');
}
else {
$activities = $module
->getModuleActivities();
}
/** @var \Drupal\opigno_module\Entity\OpignoActivity[] $activities */
$activities = array_map(function ($activity) {
/** @var \Drupal\opigno_module\Entity\OpignoActivity $activity */
return OpignoActivity::load($activity->id);
}, $activities);
if (!empty($attempts)) {
// If "newest" score - get the last attempt,
// else - get the best attempt.
$attempt = $this
->getTargetAttempt($attempts, $module);
$max_score = $attempt
->calculateMaxScore();
$score_percent = $attempt
->getAttemptScore();
$score = round($score_percent * $max_score / 100);
}
else {
$attempt = NULL;
$max_score = !empty($activities) ? array_sum(array_map(function ($activity) use ($module) {
return (int) $this
->getActivityMaxScore($module, $activity);
}, $activities)) : 0;
$score_percent = 0;
$score = 0;
}
$activities = array_map(function ($activity) use ($user, $module, $attempt) {
/** @var \Drupal\opigno_module\Entity\OpignoActivity $activity */
/** @var \Drupal\opigno_module\Entity\OpignoAnswer $answer */
$answer = isset($attempt) ? $activity
->getUserAnswer($module, $attempt, $user) : NULL;
$score = isset($answer) ? $answer
->getScore() : 0;
$max_score = (int) $this
->getActivityMaxScore($module, $activity);
if ($max_score == 0 && $activity
->get('auto_skills')
->getValue()[0]['value'] == 1) {
$max_score = 10;
}
if ($answer && $activity
->hasField('opigno_evaluation_method') && $activity
->get('opigno_evaluation_method')->value && !$answer
->isEvaluated()) {
$state_class = 'step_state_pending';
}
else {
$state_class = isset($answer) ? 'step_state_passed' : ($max_score == 0 ? 'step_state_pending' : 'step_state_failed');
}
return [
[
'data' => $activity
->getName(),
],
[
'data' => [
'#markup' => $score . '/' . $max_score,
],
],
[
'data' => [
'#type' => 'html_tag',
'#tag' => 'span',
'#attributes' => [
'class' => [
$state_class,
],
],
'#value' => '',
],
],
];
}, $activities);
$activities = [
'#type' => 'table',
'#attributes' => [
'class' => [
'module_panel_activities_overview',
],
],
'#rows' => $activities,
];
$training_id = $training
->id();
$module_id = $module
->id();
$user_id = $user
->id();
if (isset($course)) {
$course_id = $course
->id();
$id = "module_panel_{$user_id}_{$training_id}_{$course_id}_{$module_id}";
}
else {
$id = "module_panel_{$user_id}_{$training_id}_{$module_id}";
}
if (isset($attempt)) {
$details = Link::createFromRoute($this
->t('Details'), 'opigno_module.module_result', [
'opigno_module' => $module
->id(),
'user_module_status' => $attempt
->id(),
], [
'query' => [
'skip-links' => TRUE,
],
])
->toRenderable();
$details['#attributes']['target'] = '_blank';
}
else {
$details = [];
}
return [
'#type' => 'container',
'#attributes' => [
'id' => $id,
'class' => [
'module_panel',
],
],
[
'#type' => 'container',
'#attributes' => [
'class' => [
'module_panel_header',
],
],
[
'#markup' => '<a href="#" class="module_panel_close">×</a>',
],
[
'#type' => 'html_tag',
'#tag' => 'h3',
'#attributes' => [
'class' => [
'module_panel_title',
],
],
'#value' => $step['name'] . ' ' . (!empty($completed_on) ? t('completed') : ''),
],
],
[
'#type' => 'html_tag',
'#tag' => 'hr',
'#value' => '',
],
[
'#type' => 'container',
'#attributes' => [
'class' => [
'module_panel_content',
],
],
!empty($completed_on) ? [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('@name completed on @date', [
'@name' => $step['name'],
'@date' => $completed_on,
]),
] : [],
!($max_score == 0) ? [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('User got @score of @max_score possible points.', [
'@score' => $score,
'@max_score' => $max_score,
]),
] : [],
!($max_score == 0) ? [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('Total score @percent%', [
'@percent' => $score_percent,
]),
] : [],
[
'#type' => 'html_tag',
'#tag' => 'h3',
'#attributes' => [
'class' => [
'module_panel_overview_title',
],
],
'#value' => t('Activities Overview'),
],
$activities,
$details,
],
];
}