You are here

public function ModuleResultForm::buildForm in Opigno module 3.x

Same name and namespace in other branches
  1. 8 src/Form/ModuleResultForm.php \Drupal\opigno_module\Form\ModuleResultForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/ModuleResultForm.php, line 39

Class

ModuleResultForm
Class ModuleResultForm.

Namespace

Drupal\opigno_module\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, OpignoModule $opigno_module = NULL, UserModuleStatus $user_module_status = NULL) {

  // Get attempt answers.
  $form['answers'] = [
    '#type' => 'fieldset',
    '#tree' => TRUE,
  ];
  $answers = $user_module_status
    ->getAnswers();
  foreach ($answers as $answer_id => $answer) {
    $answer_activity = $answer
      ->getActivity();
    $answer_type = $answer
      ->getType();
    if (!$answer_activity
      ->hasField('opigno_evaluation_method')) {
      continue;
    }
    elseif (!$answer_activity
      ->get('opigno_evaluation_method')
      ->getValue()[0]['value']) {
      continue;
    }
    $form['answers'][$answer_id] = [
      '#type' => 'fieldset',
      '#title' => Link::createFromRoute($this
        ->t('Activity: %activity', [
        '%activity' => $answer_activity
          ->getName(),
      ]), 'entity.opigno_activity.canonical', [
        'opigno_activity' => $answer_activity
          ->id(),
      ])
        ->toString(),
    ];
    $question_markup = '';
    $answer_markup = '';
    switch ($answer_type) {
      case 'opigno_file_upload':
        $answer_markup = $answer->opigno_file
          ->view('full');
        $question_markup = $answer_activity->opigno_body
          ->view('full');
        break;
      case 'opigno_long_answer':
        $answer_markup = $answer->opigno_body
          ->view('full');
        $question_markup = $answer_activity->opigno_body
          ->view('full');
        break;
      case 'opigno_slide':
        $question_markup = \Drupal::entityTypeManager()
          ->getViewBuilder('opigno_answer')
          ->view($answer);
        break;
      case 'opigno_h5p':

        // Get xApiData.

        /* @var $db_connection \Drupal\Core\Database\Connection */
        $db_connection = \Drupal::service('database');
        $query = $db_connection
          ->select('opigno_h5p_user_answer_results', 'ohr')
          ->fields('ohr')
          ->condition('ohr.answer_id', $answer
          ->id());
        $result = $query
          ->execute()
          ->fetchAll();
        $question_markup = [];
        if ($result) {
          foreach ($result as $xapi_data) {
            $H5PReport = H5PReport::getInstance();
            $reportHtml = $H5PReport
              ->generateReport($xapi_data);
            $question_markup[] = [
              '#markup' => $reportHtml,
            ];
          }
        }
        break;
      case 'opigno_scorm':
        break;
      case 'opigno_tincan':
        break;
    }
    if (!empty($question_markup)) {
      $form['answers'][$answer_id]['question_markup'] = $question_markup;
      $form['answers'][$answer_id]['question_markup']['#weight'] = -10;
    }
    if (!empty($answer_markup)) {
      $form['answers'][$answer_id]['answer_markup'] = $answer_markup;
      $form['answers'][$answer_id]['answer_markup']['#weight'] = -9;
    }
    $max_score = $this
      ->getAnswerMaxScore($answer);
    $form['answers'][$answer_id]['score'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Score'),
      '#default_value' => $answer
        ->getScore(),
      '#required' => TRUE,
      '#field_suffix' => "<span>/{$max_score}</span>",
      '#attributes' => [
        'class' => [
          'max-score',
        ],
      ],
    ];
  }
  $form['#attached']['library'][] = 'opigno_module/module_results_form';
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save score'),
  ];
  return $form;
}