1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
class MultipleAnswerDropdown extends Question |
6
|
|
|
{ |
7
|
|
|
public $typePicture = 'mcma_dropdown.png'; |
8
|
|
|
public $explanationLangVar = 'Multiple Answer Dropdown'; |
9
|
|
|
|
10
|
|
|
public $question_table_class = 'table table-striped table-hover'; |
11
|
|
|
|
12
|
|
|
public function __construct() |
13
|
|
|
{ |
14
|
|
|
parent::__construct(); |
15
|
|
|
$this->type = MULTIPLE_ANSWER_DROPDOWN; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function createForm(&$form, $exercise) |
19
|
|
|
{ |
20
|
|
|
global $text; |
21
|
|
|
|
22
|
|
|
parent::createForm($form, $exercise); |
23
|
|
|
|
24
|
|
|
$objExe = ChamiloSession::read('objExercise'); |
25
|
|
|
$course = api_get_course_info(); |
26
|
|
|
$courseId = (int)($course['real_id'] ?? 0); |
27
|
|
|
|
28
|
|
|
$form->addTextarea( |
29
|
|
|
'list_text', |
30
|
|
|
[get_lang('AnswerList'), get_lang('Enter list of answers one answer by line')], |
31
|
|
|
['rows' => 8] |
32
|
|
|
); |
33
|
|
|
$form->addFile( |
34
|
|
|
'list_file', |
35
|
|
|
['', get_lang('Or select Csv file with list of answers')], |
36
|
|
|
['accept' => 'text/csv'] |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
$buttonGroup = []; |
40
|
|
|
|
41
|
|
|
if ($objExe->edit_exercise_in_lp || |
42
|
|
|
(empty($this->exerciseList) && empty($objExe->iid)) |
43
|
|
|
) { |
44
|
|
|
$buttonGroup[] = $form->addButton( |
45
|
|
|
'submitQuestion', |
46
|
|
|
$text, |
47
|
|
|
'check', |
48
|
|
|
'primary', |
49
|
|
|
'default', |
50
|
|
|
null, |
51
|
|
|
['id' => 'submit-question'], |
52
|
|
|
true |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$form->addGroup($buttonGroup); |
57
|
|
|
|
58
|
|
|
if (!empty($this->iid)) { |
59
|
|
|
// Load existing options bound to this question (pass course + exercise) |
60
|
|
|
$objAnswer = new Answer((int) $this->iid, $courseId, $exercise, false); |
61
|
|
|
$optionData = array_column($objAnswer->getAnswers(), 'answer'); |
62
|
|
|
|
63
|
|
|
$form->setDefaults(['list_text' => implode(PHP_EOL, $optionData)]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function createAnswersForm($form) |
68
|
|
|
{ |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function processCreation($form, $exercise) |
72
|
|
|
{ |
73
|
|
|
$listFile = $form->getSubmitValue('list_file'); |
74
|
|
|
$listText = $form->getSubmitValue('list_text'); |
75
|
|
|
|
76
|
|
|
parent::processCreation($form, $exercise); |
77
|
|
|
|
78
|
|
|
$questionId = 0; |
79
|
|
|
if (!empty($this->iid)) { |
80
|
|
|
$questionId = (int) $this->iid; |
81
|
|
|
} elseif (property_exists($this, 'iId') && !empty($this->iId)) { |
82
|
|
|
$questionId = (int) $this->iId; |
83
|
|
|
} elseif (!empty($this->id)) { |
84
|
|
|
// Fallback in case your build exposes "id" |
85
|
|
|
$questionId = (int) $this->id; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Safety net: if we still don't have a question id, abort with a clear message |
89
|
|
|
if ($questionId <= 0) { |
90
|
|
|
throw new \RuntimeException('Question id was not generated; cannot save dropdown options.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Build the lines array (either CSV first column, or textarea lines) |
94
|
|
|
$lines = []; |
95
|
|
|
if (is_array($listFile) && isset($listFile['error']) && (int) $listFile['error'] === UPLOAD_ERR_OK) { |
96
|
|
|
$lines = Import::csvColumnToArray($listFile['tmp_name']); // reads first column only |
97
|
|
|
} elseif (!empty($listText)) { |
98
|
|
|
$lines = explode("\n", $listText); |
99
|
|
|
} |
100
|
|
|
// Normalize: trim, drop empty |
101
|
|
|
$lines = array_values(array_filter(array_map('trim', $lines), static fn ($v) => $v !== '')); |
102
|
|
|
|
103
|
|
|
// Create Answer bound to this question + course + exercise |
104
|
|
|
$course = api_get_course_info(); |
105
|
|
|
$courseId = (int) ($course['real_id'] ?? 0); |
106
|
|
|
$objAnswer = new Answer($questionId, $courseId, $exercise, false); |
107
|
|
|
|
108
|
|
|
// Fill answers (dropdown typically has no "correct" by default) |
109
|
|
|
$pos = 1; |
110
|
|
|
foreach ($lines as $line) { |
111
|
|
|
$objAnswer->createAnswer($line, 0, '', 0, $pos); |
112
|
|
|
$pos++; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// 6) Persist answers |
116
|
|
|
$objAnswer->save(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param FormValidator $form |
121
|
|
|
* @param Exercise $exercise |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function processAnswersCreation($form, $exercise) |
126
|
|
|
{ |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function return_header(Exercise $exercise, $counter = null, $score = []) |
130
|
|
|
{ |
131
|
|
|
$header = parent::return_header($exercise, $counter, $score); |
132
|
|
|
|
133
|
|
|
$tableClass = (property_exists($this, 'question_table_class') && !empty($this->question_table_class)) |
134
|
|
|
? $this->question_table_class |
135
|
|
|
: 'table table-striped table-hover'; |
136
|
|
|
|
137
|
|
|
$header .= '<table class="'.$tableClass.'"><thead><tr>'; |
138
|
|
|
|
139
|
|
|
$header .= '<th class="text-center">'.get_lang('Choice').'</th>'; |
140
|
|
|
|
141
|
|
|
if ($exercise->showExpectedChoiceColumn()) { |
142
|
|
|
$header .= '<th class="text-center">'.get_lang('ExpectedChoice').'</th>'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$header .= '<th>'.get_lang('Answer').'</th>'; |
146
|
|
|
|
147
|
|
|
if ($exercise->showExpectedChoice()) { |
148
|
|
|
$header .= '<th class="text-center">'.get_lang('Status').'</th>'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$header .= '</tr></thead>'; |
152
|
|
|
|
153
|
|
|
return $header; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|