View source
<?php
namespace Drupal\course_quiz\Plugin\course\CourseObject;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal;
use Drupal\course\Entity\CourseObject;
use Drupal\quiz\Entity\Quiz;
use stdClass;
use function module_load_include;
use function quiz_stats_get_adv_stats;
use function views_embed_view;
class CourseObjectQuiz extends CourseObject {
public static function context() {
$route_match = Drupal::routeMatch();
if (in_array($route_match
->getRouteName(), [
'entity.quiz.canonical',
'quiz.take',
'quiz.question.feedback',
'quiz.question.take',
'entity.quiz_result.canonical',
])) {
$quiz = $route_match
->getParameter('quiz');
if (is_numeric($quiz)) {
$quiz = Quiz::load($quiz);
}
if ($courseObject = course_get_course_object('quiz', $quiz
->id())) {
return array(
'object_type' => 'quiz',
'instance' => $quiz
->id(),
);
}
}
}
function createInstance() {
$quiz = Quiz::create([
'title' => $this
->getTitle(),
'type' => 'quiz',
]);
$quiz
->save();
$this
->setInstanceId($quiz
->id());
}
public function getTakeType() {
return 'redirect';
}
function getTakeUrl() {
if ($this
->getOption('quiz_goto') == 'take') {
return Url::fromRoute('entity.quiz.take', [
'quiz' => $this
->getInstanceId(),
]);
}
else {
return Url::fromRoute('entity.quiz.canonical', [
'quiz' => $this
->getInstanceId(),
]);
}
}
function getViewUrl() {
return Url::fromRoute('entity.quiz.canonical', [
'quiz' => $this
->getInstanceId(),
]);
}
function getEditUrl() {
return Url::fromRoute('entity.quiz.edit_form', [
'quiz' => $this
->getInstanceId(),
]);
}
public function optionsDefinition() {
$options = parent::optionsDefinition();
$options['quiz_goto'] = 'view';
$options['passing_grade'] = 75;
return $options;
}
public function optionsForm(&$form, FormStateInterface $form_state) {
parent::optionsForm($form, $form_state);
$defaults = $this
->getOptions();
$form['instance'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'quiz',
'#default_value' => $this
->getOption('instance') ? Quiz::load($this
->getOption('instance')) : NULL,
);
$form['quiz_goto'] = array(
'#type' => 'select',
'#title' => t('Quiz entry point'),
'#options' => array(
'view' => t('View Quiz'),
'take' => t('Take Quiz'),
),
'#default_value' => $defaults['quiz_goto'],
'#description' => t('Selecting "Take Quiz" will launch the user directly into taking the quiz, without viewing the quiz body.'),
);
$form['grading']['passing_grade'] = array(
'#title' => t('Passing grade'),
'#type' => 'textfield',
'#size' => 4,
'#default_value' => $defaults['passing_grade'],
'#description' => t('The user will not be able to proceed past this object unless this grade is met.'),
);
}
public function getWarnings() {
$warnings = parent::getWarnings();
if ($this
->getInstanceId()) {
$quiz = Quiz::load($this
->getInstanceId());
if (!$quiz
->getNumberOfQuestions()) {
$link = Link::createFromRoute('add questions', "quiz.questions", [
'quiz' => $this
->getInstanceId(),
]);
$warnings[] = t('This Quiz does not have any questions. Please @link.', array(
'@link' => $link
->toString(),
));
}
}
return $warnings;
}
public function getReports() {
$reports = parent::getReports();
$reports['results'] = array(
'title' => t('Results'),
);
if (Drupal::moduleHandler()
->moduleExists('quiz_stats')) {
$reports['statistics'] = array(
'title' => t('Statistics'),
);
}
return $reports;
}
public function getReport($key) {
module_load_include('inc', 'quiz', 'quiz.admin');
switch ($key) {
case 'results':
$out = views_embed_view('quiz_results', 'default', $this
->getInstanceId());
return array(
'title' => t('Quiz results'),
'content' => $out,
);
case 'statistics':
module_load_include('inc', 'quiz_stats', 'quiz_stats.admin');
return array(
'title' => t('Quiz statistics'),
'content' => quiz_stats_get_adv_stats($this
->getNode()->vid),
);
}
return parent::getReport($key);
}
function getNodeTypes() {
return array(
'quiz',
);
}
function isGraded() {
return TRUE;
}
function getCloneAbility() {
return t('%object can only be partially cloned. It will be created with the same settings, but without the questions.', array(
'%object' => $this
->getTitle(),
));
}
function getOptionsSummary() {
$summary = parent::getOptionsSummary();
if ($this
->getInstanceId()) {
$link = Link::createFromRoute('Edit questions', "quiz.questions", [
'quiz' => $this
->getInstanceId(),
]);
$summary['questions'] = $link
->toString();
}
return $summary;
}
function getStatus() {
$account = Drupal::currentUser();
$grade = $this
->isGraded() ? t('Your grade: %grade_result%<br/>Pass grade: %passing_grade%', array(
'%grade_result' => $this
->getFulfillment($account)
->getOption('grade_result'),
'%passing_grade' => $this
->getOption('passing_grade'),
)) : '';
return $grade;
}
public static function getNodeInstances($node) {
$quizzes = array();
$sql = 'SELECT n.nid, r.parent_vid AS vid, n.title FROM {quiz_node_relationship} r
JOIN {node} n ON n.nid = r.parent_nid
WHERE r.child_vid = :child_vid
ORDER BY r.parent_vid DESC';
$res = Drupal::database()
->query($sql, array(
':child_vid' => $node->vid,
));
while ($row = $res
->fetch()) {
$quizzes[] = $row->nid;
}
return $quizzes;
}
}