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;
class CourseObjectWebform extends CourseObject {
public function createInstance() {
$webform = Webform::create([
'id' => 'course_object_' . uniqid(),
'title' => $this
->getTitle(),
'uid' => \Drupal::currentUser()
->id(),
]);
$webform
->save();
$this
->setInstanceId($webform
->id());
}
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;
}
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;
}
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);
}
function getCloneAbility() {
return TRUE;
}
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;
}
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;
}
function getViewUrl() {
$url = Url::fromRoute('entity.webform.canonical', [
'webform' => $this
->getInstanceId(),
]);
return $url;
}
function getTakeUrl() {
$url = Url::fromRoute('entity.webform.canonical', [
'webform' => $this
->getInstanceId(),
]);
return $url;
}
function getEditUrl() {
$url = Url::fromRoute('entity.webform.edit_form', [
'webform' => $this
->getInstanceId(),
]);
return $url;
}
public function getTakeType() {
return 'redirect';
}
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(),
);
}
}
}
}