You are here

function opigno_learning_path_get_step_list_top in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 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 634
Contains opigno_learning_path.module.

Code

function opigno_learning_path_get_step_list_top() {
  $group_type = opigno_learning_path_get_group_type();
  $current_step = opigno_learning_path_get_current_step();
  $enough_activities = TRUE;
  if ($group_type == 'learning_path') {

    // 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;
                }
              }
            }
          }
        }
      }
    }
  }

  // Add steps explanation.
  $html = '';
  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 content-box'>{$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 content-box'>{$message}</div>";
      }
      $messenger
        ->deleteByType('error');
    }
    $explanation = $explanations[$current_step];
    $html .= "<div class='lp_step_explanation content-box'>{$explanation}</div>";
  }
  return new FormattableMarkup($html, []);
}