Passed
Branch master (80b093)
by Angel Fernando Quiroz
07:55
created

OralExpression::initFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 8
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\Asset;
6
use Chamilo\CoreBundle\Entity\AttemptFile;
7
use Chamilo\CoreBundle\Entity\TrackEAttempt;
8
use Chamilo\CoreBundle\Framework\Container;
9
use Symfony\Component\Uid\Uuid;
10
11
/**
12
 * Class OralExpression
13
 * This class allows to instantiate an object of type FREE_ANSWER,
14
 * extending the class question.
15
 *
16
 * @author Eric Marguin
17
 */
18
class OralExpression extends Question
19
{
20
    public $typePicture = 'audio_question.png';
21
    public $explanationLangVar = 'Oral expression';
22
    public $available_extensions = ['wav', 'ogg'];
23
24
    public function __construct()
25
    {
26
        parent::__construct();
27
        $this->type = ORAL_EXPRESSION;
28
        $this->isContent = $this->getIsContent();
29
    }
30
31
    public function createAnswersForm($form)
32
    {
33
        $form->addText(
34
            'weighting',
35
            get_lang('Score'),
36
            ['class' => 'span1']
37
        );
38
        global $text;
39
        // setting the save button here and not in the question class.php
40
        $form->addButtonSave($text, 'submitQuestion');
41
        if (!empty($this->id)) {
42
            $form->setDefaults(['weighting' => float_format($this->weighting, 1)]);
43
        } else {
44
            if (1 == $this->isContent) {
45
                $form->setDefaults(['weighting' => '10']);
46
            }
47
        }
48
    }
49
50
    public function processAnswersCreation($form, $exercise)
51
    {
52
        $this->weighting = $form->getSubmitValue('weighting');
53
        $this->save($exercise);
54
    }
55
56
    public function return_header(Exercise $exercise, $counter = null, $score = [])
57
    {
58
        $score['revised'] = $this->isQuestionWaitingReview($score);
59
        $header = parent::return_header($exercise, $counter, $score);
60
        $header .= '<table class="'.$this->question_table_class.'">
61
            <tr>
62
                <th>'.get_lang('Answer').'</th>
63
            </tr>';
64
65
        return $header;
66
    }
67
68
    /**
69
     * Return the HTML code to show the RecordRTC/Wami recorder.
70
     */
71
    public function returnRecorder(int $trackExerciseId): string
72
    {
73
        $recordAudioView = new Template(
74
            '',
75
            false,
76
            false,
77
            false,
78
            false,
79
            false,
80
            false
81
        );
82
83
        $recordAudioView->assign('type', Asset::EXERCISE_ATTEMPT);
84
        $recordAudioView->assign('t_exercise_id', $trackExerciseId);
85
        $recordAudioView->assign('question_id', $this->id);
86
87
        $template = $recordAudioView->get_template('exercise/oral_expression.html.twig');
88
89
        return $recordAudioView->fetch($template);
90
    }
91
92
    public static function saveAssetInQuestionAttempt($attemptId)
93
    {
94
        $em = Container::getEntityManager();
95
96
        $attempt = $em->find(TrackEAttempt::class, $attemptId);
97
98
        $variable = 'oral_expression_asset_'.$attempt->getQuestionId();
99
100
        $assetId = ChamiloSession::read($variable);
101
        $asset = Container::getAssetRepository()->find(Uuid::fromRfc4122($assetId));
102
103
        if (null === $asset) {
104
            return;
105
        }
106
107
        ChamiloSession::erase($variable);
108
109
        $attemptFile = (new AttemptFile())
110
            ->setAsset($asset)
111
        ;
112
113
        $attempt->addAttemptFile($attemptFile);
114
115
        $em->persist($attemptFile);
116
        $em->flush();
117
    }
118
}
119