LearningPathValidator.php in Opigno Learning path 3.x
File
src/LearningPathValidator.php
View source
<?php
namespace Drupal\opigno_learning_path;
use Drupal\Core\Url;
use Drupal\group\Entity\Group;
use Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent;
use Drupal\opigno_learning_path\Entity\LPManagedContent;
use Drupal\opigno_module\Entity\OpignoModule;
use Symfony\Component\HttpFoundation\RedirectResponse;
class LearningPathValidator {
public static function userHasPassed($uid, Group $learning_path) {
$contents = LPManagedContent::loadByLearningPathId($learning_path
->id());
foreach ($contents as $content) {
$min_score = $content
->getSuccessScoreMin() / 100;
$content_type = $content
->getLearningPathContentType();
$user_score = $content_type
->getUserScore($uid, $content
->getEntityId());
if ($user_score < $min_score) {
return FALSE;
}
}
return TRUE;
}
protected static function moduleValidate(OpignoModule $module, &$redirect_step) {
$activities = $module
->getModuleActivities();
$is_valid = !empty($activities);
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler
->moduleExists('opigno_skills_system') && $module
->getSkillsActive() && $module
->getModuleSkillsGlobal()) {
$is_valid = TRUE;
}
if (!$is_valid && ($redirect_step === NULL || $redirect_step >= 4)) {
if ($module
->getSkillsActive()) {
$is_valid = TRUE;
}
else {
$redirect_step = 4;
$messenger = \Drupal::messenger();
$messenger
->addError(t('Please, add at least one activity to @module module!', [
'@module' => $module
->label(),
]));
}
}
return $is_valid;
}
protected static function courseValidate($course_id, &$redirect_step) {
$contents = OpignoGroupManagedContent::loadByGroupId($course_id);
$is_valid = !empty($contents);
if (!$is_valid && ($redirect_step === NULL || $redirect_step >= 3)) {
$redirect_step = 3;
$current_route = \Drupal::service('current_route_match');
$current_step = (int) $current_route
->getParameter('current');
if ($current_step === $redirect_step) {
$messenger = \Drupal::messenger();
$messenger
->addError(t('Please make sure that every course contains at least one module.'));
}
}
else {
foreach ($contents as $course_content) {
$module_id = $course_content
->getEntityId();
$module = OpignoModule::load($module_id);
if (!static::moduleValidate($module, $redirect_step)) {
$is_valid = FALSE;
}
}
}
return $is_valid;
}
public static function stepsValidate(Group $group) {
$messenger = \Drupal::messenger();
$current_route = \Drupal::service('current_route_match');
$current_route_name = $current_route
->getRouteName();
$current_route_step = (int) $current_route
->getParameter('current');
$group_type = opigno_learning_path_get_group_type();
if ($group_type !== 'learning_path') {
return TRUE;
}
$current_step = (int) opigno_learning_path_get_current_step();
if ($current_step === 1 || $current_step === 2) {
return TRUE;
}
$redirect_step = NULL;
$contents = OpignoGroupManagedContent::loadByGroupId($group
->id());
if (empty($contents)) {
$redirect_step = 2;
$messenger
->addError(t('Please, add some course or module!'));
}
else {
$group_courses = $group
->getContent('subgroup:opigno_course');
if (empty($group_courses) && $current_route_name === 'opigno_learning_path.learning_path_courses') {
$redirect_step = 4;
}
$has_mandatory = self::hasMandatoryItem($contents);
if (!$has_mandatory) {
$redirect_step = 2;
$messenger
->addError(t('At least one entity must be mandatory!'));
}
elseif ($current_route_name != 'opigno_learning_path.learning_path_modules' && $current_route_step != 2 && $current_route_step != 3) {
foreach ($contents as $content) {
$type_id = $content
->getGroupContentTypeId();
switch ($type_id) {
case 'ContentTypeModule':
$module_id = $content
->getEntityId();
$module = OpignoModule::load($module_id);
static::moduleValidate($module, $redirect_step);
break;
case 'ContentTypeCourse':
$course_id = $content
->getEntityId();
static::courseValidate($course_id, $redirect_step);
break;
}
}
}
}
if (isset($redirect_step)) {
$redirect_route_name = array_search($redirect_step, opigno_learning_path_get_routes_steps());
if ($redirect_route_name === $current_route_name) {
return FALSE;
}
$response = new RedirectResponse(Url::fromRoute($redirect_route_name, [
'group' => $group
->id(),
])
->toString());
return $response
->send();
}
return TRUE;
}
protected static function hasMandatoryItem($contents) {
foreach ($contents as $content) {
if ($content
->isMandatory()) {
return TRUE;
}
}
return FALSE;
}
}