You are here

CourseObjectPoll.php in Course 3.x

File

modules/course_poll/src/Plugin/course/CourseObject/CourseObjectPoll.php
View source
<?php

namespace Drupal\course_poll\Plugin\course\CourseObject;

use Drupal\poll\Entity\Poll;
use Drupal;
use Drupal\Core\Url;
use Drupal\course\Entity\CourseObject;
use Drupal\poll\Entity\PollChoice;
use function course_get_course_object;
use function module_load_include;

/**
 * @CourseObject(
 *   id = "poll",
 *   label = "Poll",
 *   handlers = {
 *     "fulfillment" = "\Drupal\course_poll\Plugin\course\CourseObject\CourseObjectPollFulfillment"
 *   }
 * )
 */
class CourseObjectPoll extends CourseObject {

  /**
   * Course context handler callback.
   */
  public static function context() {
    $route_match = Drupal::routeMatch();
    if (in_array($route_match
      ->getRouteName(), [
      'entity.poll.canonical',
    ])) {
      $poll = $route_match
        ->getParameter('poll');
      if ($courseObject = course_get_course_object('poll', $poll
        ->id())) {
        return array(
          'object_type' => 'poll',
          'instance' => $poll
            ->id(),
        );
      }
    }
  }
  function getTakeType() {
    return 'redirect';
  }
  public function optionsDefinition() {
    $options = parent::optionsDefinition();
    $options['use_node_title'] = 1;
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function createInstance() {
    $poll = Poll::create();
    $poll_choice_ids = [];
    $poll_choice1 = PollChoice::create([
      'choice' => 'Yes',
    ]);
    $poll_choice1
      ->save();
    $poll_choice2 = PollChoice::create([
      'choice' => 'No',
    ]);
    $poll_choice2
      ->save();
    $poll_choice_ids[] = $poll_choice1
      ->id();
    $poll_choice_ids[] = $poll_choice2
      ->id();
    $poll
      ->set('choice', $poll_choice_ids)
      ->save();
    $this
      ->setInstanceId($poll
      ->id());
  }
  public function getReports() {
    $reports = parent::getReports();
    $reports += array(
      'results' => array(
        'title' => 'Results',
      ),
      'all' => array(
        'title' => 'List votes',
      ),
    );
    return $reports;
  }
  public function getReport($key) {
    module_load_include('inc', 'poll', 'poll.pages');
    switch ($key) {
      case 'results':
        return array(
          'title' => $this
            ->getNode()->title,
          'content' => poll_view_results($this
            ->getNode(), NULL, NULL, NULL),
        );
      case 'all':
        $out = poll_votes($this
          ->getNode());
        return array(
          'title' => 'All votes',
          'content' => Drupal::service('renderer')
            ->render($out),
        );
    }
    return parent::getReport($key);
  }
  function getNodeTypes() {
    return array(
      'poll',
    );
  }
  function getCloneAbility() {
    return TRUE;
  }
  public function getEditUrl() {
    return Url::fromRoute('entity.poll.edit_form', [
      'poll' => $this
        ->getInstanceId(),
    ]);
  }
  public function getViewUrl() {
    return Url::fromRoute('entity.poll.canonical', [
      'poll' => $this
        ->getInstanceId(),
    ]);
  }
  public function getTakeUrl() {
    return $this
      ->getViewUrl();
  }

}

Classes

Namesort descending Description
CourseObjectPoll Plugin annotation @CourseObject( id = "poll", label = "Poll", handlers = { "fulfillment" = "\Drupal\course_poll\Plugin\course\CourseObject\CourseObjectPollFulfillment" } )