FileUploadActivityAnswer.php in Opigno module 8
File
ActivityTypes/opigno_file_upload/src/Plugin/ActivityAnswer/FileUploadActivityAnswer.php
View source
<?php
namespace Drupal\opigno_file_upload\Plugin\ActivityAnswer;
use Drupal\opigno_module\ActivityAnswerPluginBase;
use Drupal\opigno_module\Entity\OpignoActivityInterface;
use Drupal\opigno_module\Entity\OpignoAnswerInterface;
class FileUploadActivityAnswer extends ActivityAnswerPluginBase {
public function evaluatedOnSave(OpignoActivityInterface $activity) {
$method = $activity
->get('opigno_evaluation_method')->value;
if ($method == 0) {
return TRUE;
}
else {
return FALSE;
}
}
public function getScore(OpignoAnswerInterface $answer) {
$score = 10;
$activity = $answer
->getActivity();
$method = $activity
->get('opigno_evaluation_method')->value;
if ($method == 0) {
$db_connection = \Drupal::service('database');
$score_query = $db_connection
->select('opigno_module_relationship', 'omr')
->fields('omr', [
'max_score',
])
->condition('omr.parent_id', $answer
->getModule()
->id())
->condition('omr.parent_vid', $answer
->getModule()
->getRevisionId())
->condition('omr.child_id', $activity
->id())
->condition('omr.child_vid', $activity
->getRevisionId());
$score_result = $score_query
->execute()
->fetchField();
if ($score_result) {
$score = $score_result;
}
}
return $score;
}
public function getAnswerResultItemHeaders(OpignoAnswerInterface $answer) {
$headings = [
$this
->t('Your answer'),
];
if (!$answer
->isEvaluated()) {
$headings[] = $this
->t('Result');
}
return $headings;
}
public function getAnswerResultItemData(OpignoAnswerInterface $answer) {
$data = [];
$uploaded_file = $answer
->get('opigno_file')->entity;
if ($uploaded_file !== NULL) {
$file_link = [
'#theme' => 'file_link',
'#file' => $uploaded_file,
];
$data['item'][] = \Drupal::service('renderer')
->render($file_link);
}
if (!$answer
->isEvaluated()) {
$data['item'][] = $this
->t('This answer has not yet been scored.');
}
return $data;
}
}