1 | <?php |
||
2 | /* For licensing terms, see /license.txt */ |
||
3 | |||
4 | use Chamilo\CoreBundle\Entity\AttemptFile; |
||
5 | use Chamilo\CoreBundle\Entity\TrackEAttempt; |
||
6 | use Chamilo\CoreBundle\Framework\Container; |
||
7 | use Symfony\Component\Uid\Uuid; |
||
8 | |||
9 | /** |
||
10 | * Question with file upload, where the file is the answer. |
||
11 | * Acts as an open question: requires teacher's review for a score. |
||
12 | */ |
||
13 | class UploadAnswer extends Question |
||
14 | { |
||
15 | public $typePicture = 'file_upload_question.png'; |
||
16 | public $explanationLangVar = 'Upload Answer'; |
||
17 | |||
18 | public function __construct() |
||
19 | { |
||
20 | parent::__construct(); |
||
21 | $this->type = UPLOAD_ANSWER; |
||
22 | $this->isContent = $this->getIsContent(); |
||
23 | } |
||
24 | |||
25 | /** {@inheritdoc} */ |
||
26 | public function createAnswersForm($form) |
||
27 | { |
||
28 | $form->addElement('text', 'weighting', get_lang('Score')); |
||
29 | global $text; |
||
30 | // Set the save button here (not in question.class.php) |
||
31 | $form->addButtonSave($text, 'submitQuestion'); |
||
32 | |||
33 | if (!empty($this->iid)) { |
||
34 | $form->setDefaults(['weighting' => float_format($this->weighting, 1)]); |
||
35 | } elseif (1 == $this->isContent) { |
||
36 | $form->setDefaults(['weighting' => '10']); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | /** {@inheritdoc} */ |
||
41 | public function processAnswersCreation($form, $exercise) |
||
42 | { |
||
43 | $this->weighting = $form->getSubmitValue('weighting'); |
||
44 | $this->save($exercise); |
||
45 | } |
||
46 | |||
47 | /** {@inheritdoc} */ |
||
48 | public function return_header(Exercise $exercise, $counter = null, $score = []) |
||
49 | { |
||
50 | $score['revised'] = $this->isQuestionWaitingReview($score); |
||
51 | $header = parent::return_header($exercise, $counter, $score); |
||
52 | |||
53 | $tableClass = property_exists($this, 'questionTableClass') |
||
54 | ? $this->questionTableClass |
||
55 | : ($this->question_table_class ?? 'data_table'); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
56 | |||
57 | $header .= '<table class="'.$tableClass.'"> |
||
58 | <tr> |
||
59 | <th>'.get_lang('Answer').'</th> |
||
60 | </tr>'; |
||
61 | |||
62 | return $header; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Attach uploaded Asset(s) to the question attempt as AttemptFile. |
||
67 | */ |
||
68 | public static function saveAssetInQuestionAttempt(int $attemptId, array $postedAssetIds = []): void |
||
69 | { |
||
70 | $em = Container::getEntityManager(); |
||
71 | |||
72 | /** @var TrackEAttempt|null $attempt */ |
||
73 | $attempt = $em->find(TrackEAttempt::class, $attemptId); |
||
74 | if (null === $attempt) { |
||
75 | return; |
||
76 | } |
||
77 | |||
78 | $questionId = (int) $attempt->getQuestionId(); |
||
79 | $sessionKey = 'upload_answer_assets_'.$questionId; |
||
80 | |||
81 | $assetIds = array_values(array_filter(array_map('strval', $postedAssetIds))); |
||
82 | if (empty($assetIds)) { |
||
83 | $sessionVal = ChamiloSession::read($sessionKey); |
||
84 | $assetIds = is_array($sessionVal) ? $sessionVal : (empty($sessionVal) ? [] : [$sessionVal]); |
||
85 | } |
||
86 | if (empty($assetIds)) { |
||
87 | return; |
||
88 | } |
||
89 | |||
90 | ChamiloSession::erase($sessionKey); |
||
91 | $repo = Container::getAssetRepository(); |
||
92 | |||
93 | foreach ($assetIds as $id) { |
||
94 | try { |
||
95 | $asset = $repo->find(Uuid::fromRfc4122($id)); |
||
96 | } catch (\Throwable $e) { |
||
97 | continue; |
||
98 | } |
||
99 | if (!$asset) { |
||
100 | continue; |
||
101 | } |
||
102 | |||
103 | $attemptFile = (new AttemptFile())->setAsset($asset); |
||
104 | $attempt->addAttemptFile($attemptFile); |
||
105 | $em->persist($attemptFile); |
||
106 | } |
||
107 | |||
108 | $em->flush(); |
||
109 | } |
||
110 | } |
||
111 |