You are here

function opigno_learning_path_get_step_list_top in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x opigno_learning_path.module \opigno_learning_path_get_step_list_top()

Returns step list for page top area.

1 call to opigno_learning_path_get_step_list_top()
opigno_learning_path_preprocess_region in ./opigno_learning_path.module
Implements hook_preprocess_region().

File

./opigno_learning_path.module, line 655
Contains opigno_learning_path.module.

Code

function opigno_learning_path_get_step_list_top() {
  $steps = [];
  $group_type = opigno_learning_path_get_group_type();
  $current_step = opigno_learning_path_get_current_step();
  $passed_steps = opigno_learning_path_validate_group_creating_steps();
  $enough_activities = TRUE;
  if ($group_type == 'learning_path') {
    $steps = [
      1 => t('Create training'),
      2 => t('Structure the training'),
      3 => t('Add modules to courses'),
      4 => t('Add activities to modules'),
      5 => t('Enroll learners'),
    ];

    // Check if training has 'automatic skills module'.
    $group = \Drupal::routeMatch()
      ->getParameter('group');
    $moduleHandler = \Drupal::service('module_handler');
    if ($moduleHandler
      ->moduleExists('opigno_skills_system') && $current_step == 5) {
      $group_steps = OpignoGroupManagedContent::loadByGroupId($group
        ->id());
      foreach ($group_steps as $group_step) {
        $id = $group_step
          ->getEntityId();
        $type_id = $group_step
          ->getGroupContentTypeId();
        if ($type_id === 'ContentTypeModule') {
          $module = OpignoModule::load($id);
          if ($module
            ->getSkillsActive() && $module
            ->getModuleSkillsGlobal()) {
            $target_skill = $module
              ->getTargetSkill();
            $term_storage = \Drupal::entityTypeManager()
              ->getStorage('taxonomy_term');
            $skills_tree = array_reverse($term_storage
              ->loadTree('skills', $target_skill));
            $skills_ids = [];

            // Get all ids of skills from that tree.
            foreach ($skills_tree as $skill) {
              $skills_ids[] = $skill->tid;
            }

            // Get all suitable activities for that skills tree from Opigno system.
            $activities = $module
              ->getSuitableActivities($skills_ids);
            foreach ($skills_tree as $key => $skill) {
              $tid = $skill->tid;
              $term_storage = \Drupal::entityTypeManager()
                ->getStorage('taxonomy_term');
              $skill_entity = $term_storage
                ->load($tid);
              $min_count_answers = $skill_entity
                ->get('field_minimum_count_of_answers')
                ->getValue()[0]['value'];
              $skill_levels = $skill_entity
                ->get('field_level_names')
                ->getValue();
              $count_of_levels = count($skill_levels);
              $skill_availability = [];
              while ($count_of_levels > 0) {
                $skill_availability[$count_of_levels] = 0;
                $count_of_levels--;
              }
              ksort($skill_availability);

              // Get count of suitable activities for each level of skills.
              foreach ($activities as $activity) {
                if ($activity->skills_list == $skill->tid) {
                  $skill_availability[$activity->skill_level]++;
                }
              }
              foreach ($skill_availability as $level => $count_of_activities) {
                if ($count_of_activities < $min_count_answers) {
                  $enough_activities = FALSE;
                  $target_skill_entity = $term_storage
                    ->load($target_skill);
                  $not_enough_skill_name = $skill_entity
                    ->label();
                  $not_enough_skill_level = $skill_levels[$level - 1]['value'];
                  break;
                }
              }
            }
          }
        }
      }
    }
  }
  elseif ($group_type == 'opigno_course') {
    $steps = [
      1 => t('Create course'),
      2 => t('Add modules to courses'),
      3 => t('Add activities to modules'),
    ];
  }
  elseif ($group_type == 'opigno_module') {
    $steps = [
      1 => t('Create module'),
      2 => t('Add activities to module'),
      3 => t('Add bulk activities to module'),
    ];
  }
  elseif ($group_type == 'opigno_class') {
    $steps = [
      1 => t('Create class'),
      2 => t('Enroll learners'),
    ];
  }
  $html = '<ul class="d-none d-md-flex justify-content-center text-center list-unstyled step-list-top mb-5">';
  $icon = '';
  foreach ($steps as $key => $step) {
    $classes = [
      'mx-4',
      'mb-4',
      'mb-md-0',
    ];
    if ($current_step >= $key) {
      $classes[] = 'active';
    }
    if ($passed_steps) {

      // @TODO check if step is validated.
      if (in_array($key, $passed_steps) && ($key != 4 || isset($enough_activities) && $enough_activities)) {
        $classes[] = 'done';
        $icon = 'ok-fill';
      }
      else {
        $classes[] = 'error';
        $icon = 'close';
      }
    }
    $html .= '<li class="' . implode(' ', $classes) . '">';
    $html .= '<div class="number mx-auto">' . $key . '</div>';
    $html .= '<div class="title">' . $step . '</div>';
    $html .= '<i class="icon-' . $icon . '"></i>';
    $html .= '</li>';
  }
  $html .= '</ul>';

  // Add steps explanation.
  if ($group_type == 'learning_path') {
    $explanations = [
      1 => t('<p>A training is a program composed of diverse courses, modules and activities which guide students throughout their learning process.</p><p>Start here by describing the basic information of your learning program and click the button «Next» . You will be taken to step 2, to the Learning Path Manager to create content.</p><p>You can learn more in our <a href="https://opigno.atlassian.net/wiki/spaces/OUM20/overview" target="_blank">online user manual</a></p>'),
      2 => t('<p>Welcome to the Learning Path Manager. It makes possible to build up the steps composing your training, that can be courses (groups of modules), modules, live meetings or instructor-led trainings. At the following steps you will be able to manage the modules inside the courses, and the activities inside the modules.</p><p>Start by adding or creating your first content.</p>'),
      3 => t('In case you added some courses to your training, you can manage here the modules inside these courses . Note that you can also directly add modules in the training at step 2.'),
      4 => t('You can manage here the contents of the modules inside your training, add activities to them, manage the existing activities.'),
      5 => t('You can invite here users or classes (group of users) to join your training. Then click on "Publish" button to have your training ready!'),
    ];
    if (isset($enough_activities) && !$enough_activities) {
      $warning = t('There are not enough activities in the Opigno System to complete "@target_skill" skills tree!<br />
        Missed skill "@skill_name" with level "@skill_level".', [
        '@target_skill' => $target_skill_entity
          ->getName(),
        '@skill_name' => $not_enough_skill_name,
        '@skill_level' => $not_enough_skill_level,
      ]);
      $html .= "<div class='lp_step_warning lp_step_explanation'>{$warning}</div>";
    }
    $messenger = \Drupal::messenger();
    $messages = $messenger
      ->messagesByType('error');
    if (!empty($messages)) {
      foreach ($messages as $message) {
        $html .= "<div class='lp_step_warning lp_step_explanation'>{$message}</div>";
      }
      $messenger
        ->deleteByType('error');
    }
    $explanation = $explanations[$current_step];
    $html .= "<div class='lp_step_explanation'>{$explanation}</div>";
  }
  return new FormattableMarkup($html, []);
}