Issues (2134)

public/main/exercise/UploadAnswer.php (1 issue)

Labels
Severity
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\AttemptFile;
5
use Chamilo\CoreBundle\Entity\ResourceNode;
6
use Chamilo\CoreBundle\Entity\TrackEAttempt;
7
use Chamilo\CoreBundle\Framework\Container;
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
The property question_table_class does not exist on UploadAnswer. Did you mean question?
Loading history...
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 ResourceNode(s) to the question attempt as AttemptFile.
67
     */
68
    public static function saveAssetInQuestionAttempt(int $attemptId, array $postedNodeIds = []): 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
        $nodeIds = array_values(array_filter(array_map('intval', $postedNodeIds)));
82
83
        if (empty($nodeIds)) {
84
            $sessionVal = ChamiloSession::read($sessionKey);
85
            $nodeIds = is_array($sessionVal) ? $sessionVal : (empty($sessionVal) ? [] : [(int) $sessionVal]);
86
        }
87
88
        if (empty($nodeIds)) {
89
            return;
90
        }
91
92
        ChamiloSession::erase($sessionKey);
93
94
        $resourceNodeRepo = Container::getResourceNodeRepository();
95
96
        foreach ($nodeIds as $id) {
97
            if (!$id) {
98
                continue;
99
            }
100
101
            /** @var ResourceNode|null $node */
102
            $node = $resourceNodeRepo->find($id);
103
            if (null === $node) {
104
                continue;
105
            }
106
107
            $attemptFile = new AttemptFile();
108
            $attemptFile->setResourceNode($node);
109
            $attempt->addAttemptFile($attemptFile);
110
111
            $em->persist($attemptFile);
112
        }
113
114
        $em->flush();
115
    }
116
}
117