CourseObjectQuizFulfillment.php in Course 3.x
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;
class CourseObjectQuizFulfillment extends CourseObjectFulfillment {
function optionsDefinition() {
return array(
'quiz_result_ids' => array(),
);
}
function delete() {
$result_ids = (array) $this
->getOption('quiz_result_ids');
$controller = \Drupal::entityTypeManager()
->getStorage('quiz_result');
$entities = $controller
->loadMultiple($result_ids);
$controller
->delete($entities);
parent::delete();
}
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();
}
}
}