View source
<?php
namespace Drupal\opigno_learning_path\Traits;
use Drupal\group\Entity\GroupInterface;
use Drupal\opigno_group_manager\Controller\OpignoGroupManagerController;
use Drupal\opigno_group_manager\OpignoGroupContext;
use Drupal\opigno_learning_path\Entity\LPStatus;
use Drupal\opigno_learning_path\LearningPathContent;
use Drupal\opigno_module\Entity\OpignoActivity;
use Drupal\opigno_module\Entity\OpignoModule;
trait LearningPathAchievementTrait {
protected $entityTypeManager;
protected $routeMatch;
public function getStepsByGroup(GroupInterface $group) : array {
$static_steps =& drupal_static(__FUNCTION__, []);
if (empty($group)) {
return [];
}
if (isset($static_steps[$group
->id()])) {
return $static_steps[$group
->id()];
}
$freeNavigation = !OpignoGroupManagerController::getGuidedNavigation($group);
if ($freeNavigation) {
$steps = LearningPathContent::getAllStepsOnlyModules($group
->id(), $this
->currentUser()
->id(), TRUE);
}
else {
$steps = LearningPathContent::getAllStepsOnlyModules($group
->id(), $this
->currentUser()
->id());
}
$user = $this
->currentUser();
$steps = array_filter($steps, function ($step) use ($user) {
if ($step['typology'] === 'Meeting') {
if (!$user
->hasPermission('view meeting entities')) {
return FALSE;
}
$meeting = $this
->entityTypeManager()
->getStorage('opigno_moxtra_meeting')
->load($step['id']);
if (!$meeting
->isMember($user
->id())) {
return FALSE;
}
}
elseif ($step['typology'] === 'ILT') {
$ilt = $this
->entityTypeManager()
->getStorage('opigno_ilt')
->load($step['id']);
if (!$ilt
->isMember($user
->id())) {
return FALSE;
}
}
return TRUE;
});
$static_steps[$group
->id()] = $steps;
return $steps;
}
protected function getCurrentGroupId() {
if ($group_id = OpignoGroupContext::getCurrentGroupId()) {
return $group_id;
}
}
protected function getCurrentGroupContentId() {
if ($cid = OpignoGroupContext::getCurrentGroupContentId()) {
return $cid;
}
}
protected function getCurrentActivityId() {
if ($opigno_activity = $this
->routeMatch()
->getParameter('opigno_activity') ?? FALSE) {
return $opigno_activity
->id();
}
}
protected function getTargetAttempt(array $attempts, OpignoModule $module) {
if ($module
->getKeepResultsOption() == 'newest') {
$attempt = end($attempts);
}
else {
$attempt = opigno_learning_path_best_attempt($attempts);
}
return $attempt;
}
public function getActivities(GroupInterface $training, GroupInterface $course = NULL, OpignoModule $module) : array {
$user = $this
->currentUser();
$moduleHandler = \Drupal::moduleHandler();
$latest_cert_date = LPStatus::getTrainingStartDate($training, $user
->id());
$parent = isset($course) ? $course : $training;
$step = opigno_learning_path_get_module_step($parent
->id(), $user
->id(), $module, $latest_cert_date);
$module = OpignoModule::load($step['id']);
$attempts = $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();
}
$activities = array_map(function ($activity) {
return OpignoActivity::load($activity->id);
}, $activities);
return [
$activities,
$attempts,
];
}
public function getActivityStatus($activities, $attempts, $module) : array {
if (!empty($attempts)) {
$attempt = $this
->getTargetAttempt($attempts, $module);
}
else {
$attempt = NULL;
}
$user = $this
->currentUser();
$array_map = [];
foreach ($activities as $key => $activity) {
$answer = isset($attempt) ? $activity
->getUserAnswer($module, $attempt, $user) : NULL;
if ($answer && $activity
->hasField('opigno_evaluation_method') && $activity
->get('opigno_evaluation_method')->value && !$answer
->isEvaluated()) {
$state_class = 'pending';
}
else {
$state_class = isset($answer) ? $answer
->isEvaluated() ? 'passed' : 'failed' : 'pending';
}
$array_map[$key] = $state_class;
}
return $array_map;
}
protected function entityTypeManager() {
if (!$this->entityTypeManager) {
$this->entityTypeManager = \Drupal::entityTypeManager();
}
return $this->entityTypeManager;
}
protected function routeMatch() {
if (!$this->routeMatch) {
$this->routeMatch = \Drupal::routeMatch();
}
return $this->routeMatch;
}
public function currentUser() {
return \Drupal::currentUser();
}
}