You are here

public function UserController::ajaxLoadCourseDetails in Opigno statistics 8

Same name and namespace in other branches
  1. 3.x src/Controller/UserController.php \Drupal\opigno_statistics\Controller\UserController::ajaxLoadCourseDetails()

Loads a user course details with the AJAX.

Parameters

\Drupal\user\UserInterface $user: User.

\Drupal\group\Entity\GroupInterface $training: Training.

\Drupal\group\Entity\GroupInterface $course: Course.

Return value

\Drupal\Core\Ajax\AjaxResponse AJAX response.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'UserController::ajaxLoadCourseDetails'
opigno_statistics.routing.yml in ./opigno_statistics.routing.yml
opigno_statistics.routing.yml

File

src/Controller/UserController.php, line 1136

Class

UserController
Class UserController.

Namespace

Drupal\opigno_statistics\Controller

Code

public function ajaxLoadCourseDetails(UserInterface $user, GroupInterface $training, GroupInterface $course) {
  $training_gid = $training
    ->id();
  $course_gid = $course
    ->id();
  $user_id = $user
    ->id();
  $selector = ".training-modules-list tr.course[data-user=\"{$user_id}\"][data-training=\"{$training_gid}\"][data-id=\"{$course_gid}\"]";
  $content = [
    [
      '#type' => 'html_tag',
      '#tag' => 'tr',
      '#attributes' => [
        'data-training' => $training_gid,
        'data-id' => $course_gid,
        'class' => [
          'course-active',
        ],
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'td',
        '#attributes' => [
          'colspan' => 3,
        ],
        '#value' => $course
          ->label(),
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'td',
        'close' => [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'data-training' => $training_gid,
            'data-id' => $course_gid,
            'class' => [
              'course-close',
            ],
          ],
        ],
      ],
    ],
    [
      '#type' => 'html_tag',
      '#tag' => 'tr',
      '#attributes' => [
        'data-training' => $training_gid,
        'data-id' => $course_gid,
        'class' => [
          'course-details',
        ],
      ],
      [
        '#type' => 'html_tag',
        '#tag' => 'td',
        '#attributes' => [
          'colspan' => 4,
        ],
        'details' => $this
          ->buildCourseDetails($user, $training, $course),
      ],
    ],
  ];
  $content['#attached']['library'][] = 'opigno_statistics/user';
  $response = new AjaxResponse();
  $response
    ->addCommand(new AfterCommand($selector, $content));
  return $response;
}