Completed
Push — master ( 067f55...e1cb48 )
by Julito
09:39
created

getQuestionOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 24
rs 9.7
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CourseBundle\Entity\CSurveyQuestionOption;
5
6
require_once __DIR__.'/../../main/inc/global.inc.php';
7
8
api_protect_course_script(true);
9
api_protect_teacher_script();
10
11
$surveyId = isset($_GET['survey']) ? (int) $_GET['survey'] : 0;
12
$surveyData = SurveyManager::get_survey($surveyId);
13
$courseId = api_get_course_int_id();
14
15
if (empty($surveyData)) {
16
    api_not_allowed(true);
17
}
18
19
$plugin = SurveyExportTxtPlugin::create();
20
$allowExportIncomplete = 'true' === $plugin->get('export_incomplete');
21
22
if ($plugin->get('enabled') !== 'true') {
23
    api_not_allowed(true);
24
}
25
26
$questionsData = SurveyManager::get_questions($surveyId, $courseId);
27
28
// Sort questions by their "sort" field
29
usort(
30
    $questionsData,
31
    function ($qL, $qR) {
32
        if ($qL['sort'] == $qR['sort']) {
33
            return 0;
34
        }
35
36
        return $qL['sort'] < $qR['sort'] ? -1 : 1;
37
    }
38
);
39
40
$content = [];
41
42
$parts = [];
43
$indexPart = 0;
44
45
$numberOfQuestions = 0;
46
47
// Separate questions in introduction and main blocks
48
foreach ($questionsData as $questionData) {
49
    if (!in_array($questionData['type'], ['yesno', 'pagebreak', 'multiplechoice'])) {
50
        continue;
51
    }
52
53
    if ('pagebreak' === $questionData['type']) {
54
        $indexPart++;
55
56
        continue;
57
    }
58
59
    $numberOfQuestions++;
60
61
    if (0 === $indexPart) {
62
        $parts[0][] = $questionData;
63
64
        continue;
65
    }
66
67
    $parts[$indexPart][] = $questionData;
68
}
69
70
if (count($parts) < 2) {
71
    api_not_allowed(
72
        true,
73
        Display::return_message(get_lang('NoData'), 'warning')
74
    );
75
}
76
77
// Process introduction questions to show
78
foreach ($parts[0] as $key => $introQuestion) {
79
    $content[] = chr($key + 97).'. '.strip_tags($introQuestion['question']);
80
81
    foreach ($introQuestion['answers'] as $answer) {
82
        $content[] = '>'.strip_tags($answer);
83
    }
84
}
85
86
$content[] = str_repeat('*', 40);
87
88
// Get surveys by users
89
$surveyAnswers = Database::getManager()
90
    ->createQuery(
91
        'SELECT sa.user, MIN(sa.iid) AS id FROM ChamiloCourseBundle:CSurveyAnswer sa
92
        WHERE sa.cId = :course AND sa.surveyId = :survey
93
        GROUP BY sa.user ORDER BY id ASC'
94
    )
95
    ->setParameters(['course' => $courseId, 'survey' => $surveyId])
96
    ->getResult();
97
98
// Process answers
99
$i = 1;
100
101
foreach ($surveyAnswers as $answer) {
102
    $userAnswersCount = 0;
103
    $surveyLine = '';
104
105
    // Show answers for introduction questions
106
    foreach ($parts[0] as $introQuestion) {
107
        $options = getQuestionOptions(
108
            $answer['user'],
109
            $courseId,
110
            $introQuestion['survey_id'],
111
            $introQuestion['question_id']
112
        );
113
        $userAnswersCount += count($options);
114
115
        /** @var CSurveyQuestionOption $option */
116
        foreach ($options as $option) {
117
            $surveyLine .= $option->getSort();
118
        }
119
    }
120
121
    $surveyLine .= '","';
122
123
    foreach ($parts as $z => $part) {
124
        if (0 === $z) {
125
            continue;
126
        }
127
128
        // Show answers for main questions
129
        foreach ($part as $mainQuestion) {
130
            $options = getQuestionOptions(
131
                $answer['user'],
132
                $courseId,
133
                $mainQuestion['survey_id'],
134
                $mainQuestion['question_id']
135
            );
136
            $userAnswersCount += count($options);
137
138
            /** @var CSurveyQuestionOption $option */
139
            foreach ($options as $option) {
140
                $surveyLine .= $option->getSort();
141
            }
142
        }
143
    }
144
145
    $surveyLine .= '"';
146
147
    if (!$allowExportIncomplete && $userAnswersCount < $numberOfQuestions) {
148
        continue;
149
    }
150
151
    $content[] = '"'.$i.'","'.$surveyLine;
152
    $i++;
153
}
154
155
// Add EOL to lines
156
$fileContent = array_map(
157
    function ($line) {
158
        return html_entity_decode($line).PHP_EOL.PHP_EOL;
159
    },
160
    $content
161
);
162
163
// Generate file
164
$fileName = api_get_path(SYS_ARCHIVE_PATH).md5($surveyId.time()).'.txt';
165
166
file_put_contents($fileName, $fileContent);
167
168
DocumentManager::file_send_for_download($fileName, true);
169
170
/**
171
 * @param string $user
172
 * @param int    $courseId
173
 * @param int    $surveyId
174
 * @param int    $questionId
175
 *
176
 * @return array
177
 */
178
function getQuestionOptions($user, $courseId, $surveyId, $questionId)
179
{
180
    $options = Database::getManager()
181
        ->createQuery(
182
            'SELECT sqo FROM ChamiloCourseBundle:CSurveyQuestionOption sqo
183
            INNER JOIN ChamiloCourseBundle:CSurveyAnswer sa
184
                WITH
185
                    sqo.cId = sa.cId
186
                    AND sqo.questionId = sa.questionId
187
                    AND sqo.surveyId = sa.surveyId
188
                    AND sqo.iid = sa.optionId
189
            WHERE sa.user = :user AND sa.cId = :course AND sa.surveyId = :survey AND sa.questionId = :question'
190
        )
191
        ->setParameters(
192
            [
193
                'user' => $user,
194
                'course' => $courseId,
195
                'survey' => $surveyId,
196
                'question' => $questionId,
197
            ]
198
        )
199
        ->getResult();
200
201
    return $options;
202
}
203