You are here

CourseObjectWebform.php in Course 3.x

File

modules/course_webform/src/Plugin/course/CourseObject/CourseObjectWebform.php
View source
<?php

namespace Drupal\course_webform\Plugin\course\CourseObject;

use Drupal;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\course\Entity\CourseObject;
use Drupal\webform\Entity\Webform;
use function course_get_course_object;

/**
 * @CourseObject(
 *   id = "webform",
 *   label = "Webform",
 *   handlers = {
 *     "fulfillment" = "\Drupal\course_webform\Plugin\course\CourseObject\CourseObjectWebformFulfillment"
 *   }
 * )
 */
class CourseObjectWebform extends CourseObject {

  /**
   * {@inheritdoc}
   */
  public function createInstance() {
    $webform = Webform::create([
      'id' => 'course_object_' . uniqid(),
      'title' => $this
        ->getTitle(),
      'uid' => \Drupal::currentUser()
        ->id(),
    ]);
    $webform
      ->save();
    $this
      ->setInstanceId($webform
      ->id());
  }

  /**
   * {@inheritdoc}
   */
  public function getWarnings() {
    $warnings = parent::getWarnings();
    $webform = Webform::load($this
      ->getInstanceId());
    if ($this
      ->getInstanceId() && !$webform
      ->getElementsDecoded()) {
      $link = Link::createFromRoute(t('add questions'), 'entity.webform.edit_form', [
        'webform' => $webform
          ->id(),
      ])
        ->toString();
      $warnings[] = t('The Webform has no questions. Please @link.', array(
        '@link' => $link,
      ));
    }
    return $warnings;
  }

  /**
   * {@inheritdoc}
   */
  public function getReports() {
    $reports = parent::getReports();
    $results_url = Url::fromRoute('entity.webform.results_submissions', [
      'webform' => $this
        ->getInstanceId(),
    ]);
    $results_link = Link::fromTextAndUrl('Submissions', $results_url);
    $analysis_url = Url::fromRoute('entity.webform.results_analysis', [
      'webform' => $this
        ->getInstanceId(),
    ]);
    $analysis_link = Link::fromTextAndUrl('Analysis', $analysis_url);
    $download_url = Url::fromRoute('entity.webform.results_export', [
      'webform' => $this
        ->getInstanceId(),
    ]);
    $download_link = Link::fromTextAndUrl('Download', $download_url);
    $reports += array(
      'submissions' => array(
        'title' => t('Submissions'),
        'link' => $results_link,
        'modal' => TRUE,
      ),
      'analysis' => array(
        'title' => t('Analysis'),
        'link' => $analysis_link,
      ),
      'download' => array(
        'title' => t('Download'),
        'link' => $download_link,
        'modal' => TRUE,
      ),
    );
    return $reports;
  }

  /**
   * {@inheritdoc}
   */
  public function getReport($key) {
    switch ($key) {
      case 'submissions':
        return array(
          'title' => t('Webform results'),
          'modal' => 'node/1',
          'content' => webform_results_submissions($this
            ->getNode(), FALSE, 50),
        );
      case 'analysis':
        return array(
          'title' => t('Webform results'),
          'modal' => 'node/1',
          'content' => webform_results_analysis($this
            ->getNode()),
        );
      case 'download':
        $out = drupal_get_form('webform_results_download_form', $this
          ->getNode());
        return array(
          'title' => t('Webform results'),
          'url ' => 'node/1',
          'content' => \Drupal::service('renderer')
            ->render($out),
        );
    }
    return parent::getReport($key);
  }

  /**
   * {@inheritdoc}
   */
  function getCloneAbility() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  function getOptionsSummary() {
    $summary = parent::getOptionsSummary();
    if ($this
      ->getInstanceId()) {
      $link = Link::createFromRoute(t('Edit questions'), 'entity.webform.edit_form', [
        'webform' => $this
          ->getInstanceId(),
      ])
        ->toString();
      $summary['questions'] = $link;
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  function optionsForm(&$form, FormStateInterface $form_state) {
    parent::optionsForm($form, $form_state);
    $form['instance'] = array(
      '#type' => 'entity_autocomplete',
      '#target_type' => 'webform',
      '#default_value' => $this
        ->getOption('instance') ? Webform::load($this
        ->getOption('instance')) : NULL,
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  function getViewUrl() {
    $url = Url::fromRoute('entity.webform.canonical', [
      'webform' => $this
        ->getInstanceId(),
    ]);
    return $url;
  }

  /**
   * {@inheritdoc}
   */
  function getTakeUrl() {
    $url = Url::fromRoute('entity.webform.canonical', [
      'webform' => $this
        ->getInstanceId(),
    ]);
    return $url;
  }

  /**
   * {@inheritdoc}
   */
  function getEditUrl() {
    $url = Url::fromRoute('entity.webform.edit_form', [
      'webform' => $this
        ->getInstanceId(),
    ]);
    return $url;
  }

  /**
   * {@inheritdoc}
   */
  public function getTakeType() {
    return 'redirect';
  }

  /**
   * {@inheritdoc}
   */
  public static function context() {
    $route_match = Drupal::routeMatch();
    if (in_array($route_match
      ->getRouteName(), [
      'entity.webform.canonical',
      'entity.webform.confirmation',
    ])) {
      $webform = $route_match
        ->getParameter('webform');
      if ($courseObject = course_get_course_object('webform', $webform
        ->id())) {
        return array(
          'object_type' => 'webform',
          'instance' => $webform
            ->id(),
        );
      }
    }
  }

}

Classes

Namesort descending Description
CourseObjectWebform Plugin annotation @CourseObject( id = "webform", label = "Webform", handlers = { "fulfillment" = "\Drupal\course_webform\Plugin\course\CourseObject\CourseObjectWebformFulfillment" } )