CourseObjectQuizFulfillment.php in Course 8.3
File
modules/course_quiz/src/Plugin/course/CourseObject/CourseObjectQuizFulfillment.php
View source
<?php
namespace Drupal\course_quiz\Plugin\course\CourseObject;
use Drupal\course\Entity\CourseObjectFulfillment;
use Drupal\quiz\Entity\QuizResult;
use function entity_delete_multiple;
class CourseObjectQuizFulfillment extends CourseObjectFulfillment {
function optionsDefinition() {
return array(
'quiz_result_ids' => array(),
);
}
function delete() {
parent::delete();
$result_ids = (array) $this
->getOption('quiz_result_ids');
entity_delete_multiple('quiz_result', $result_ids);
}
function grade(QuizResult $quiz_result) {
$result_ids = (array) $this
->getOption('quiz_result_ids');
$result_ids[] = $quiz_result
->id();
$this
->setOption('instance', $quiz_result
->id());
$this
->setOption('quiz_result_ids', $result_ids);
if ($quiz_result && $quiz_result
->get('score')
->getString() >= $this
->getCourseObject()
->getOption('passing_grade')) {
$this
->setGrade($quiz_result
->get('score')
->getString())
->setComplete()
->save();
}
else {
$this
->setGrade($quiz_result
->get('score')
->getString())
->save();
}
}
}