-
Notifications
You must be signed in to change notification settings - Fork 988
/
CopyQuestion.php
265 lines (241 loc) · 10.1 KB
/
CopyQuestion.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace LimeSurvey\Models\Services;
use LimeSurvey\Datavalueobjects\CopyQuestionValues;
/**
* Class CopyQuestion
*
* This class is responsible for the copy question process.
*
* @package LimeSurvey\Models\Services
*/
class CopyQuestion
{
/**
* @var CopyQuestionValues values needed to copy a question (e.g. questioncode, questionGroupId ...)
*/
private $copyQuestionValues;
/**
* @var \Question the new question
*/
private $newQuestion;
/**
* CopyQuestion constructor.
*
* @param CopyQuestionValues $copyQuestionValues
*/
public function __construct($copyQuestionValues)
{
$this->copyQuestionValues = $copyQuestionValues;
$this->newQuestion = null;
}
/**
* Copies the question and all necessary values/parameters
* (languages, subquestions, answeroptions, defaultanswers, settings)
*
* @param array $copyOptions has the following boolean elements
* ['copySubquestions']
* ['copyAnswerOptions']
* ['copyDefaultAnswers']
* ['copySettings'] --> generalSettings and advancedSettings
*
* @return boolean True if new copied question could be saved, false otherwise
*/
public function copyQuestion($copyOptions)
{
$copySuccessful = $this->createNewCopiedQuestion(
$this->copyQuestionValues->getQuestionCode(),
$this->copyQuestionValues->getQuestionGroupId(),
$this->copyQuestionValues->getQuestiontoCopy()
);
if ($copySuccessful) {
//copy question languages
$this->copyQuestionLanguages($this->copyQuestionValues->getQuestiontoCopy(), $this->copyQuestionValues->getQuestionL10nData());
//copy subquestions
if ($copyOptions['copySubquestions']) {
$this->copyQuestionsSubQuestions($this->copyQuestionValues->getQuestiontoCopy()->qid);
}
//copy answer options
if ($copyOptions['copyAnswerOptions']) {
$this->copyQuestionsAnswerOptions($this->copyQuestionValues->getQuestiontoCopy()->qid);
}
//copy default answers
if ($copyOptions['copyDefaultAnswers']) {
$this->copyQuestionsDefaultAnswers($this->copyQuestionValues->getQuestiontoCopy()->qid);
}
////copy question settings (generalsettings and advanced settings)
if ($copyOptions['copySettings']) {
$this->copyQuestionsSettings($this->copyQuestionValues->getQuestiontoCopy()->qid);
}
}
return $copySuccessful;
}
/**
* Creates a new question copying the values from questionToCopy
*
* @param string $questionCode
* @param int $groupId
* @param \Question $questionToCopy the question that should be copied
*
* @return bool true if question could be saved, false otherwise
*/
public function createNewCopiedQuestion($questionCode, $groupId, $questionToCopy)
{
$this->newQuestion = new \Question();
$this->newQuestion->attributes = $questionToCopy->attributes;
$this->newQuestion->title = $questionCode;
$this->newQuestion->gid = $groupId;
$this->newQuestion->question_order = $this->copyQuestionValues->getQuestionPositionInGroup();
$this->newQuestion->qid = null;
return $this->newQuestion->save();
}
/**
* Copies the languages of a question.
*
* @param \Question $oQuestion old question from where to copy the languages (see table questions_l10ns)
* @param array<string,\LimeSurvey\Datavalueobjects\CopyQuestionTextValues> $newQuestionL10nData the text values to override
*
* @before $this->newQuestion must exist and should not be null
*
* @return bool true if all languages could be copied,
* false if no language was copied or save failed for one language
*/
private function copyQuestionLanguages($oQuestion, $newQuestionL10nData = [])
{
$allLanguagesAreCopied = false;
if ($oQuestion !== null) {
$allLanguagesAreCopied = true;
foreach ($oQuestion->questionl10ns as $questionl10n) {
$copyLanguage = new \QuestionL10n();
$copyLanguage->attributes = $questionl10n->attributes;
$copyLanguage->id = null; //new id needed
$copyLanguage->qid = $this->newQuestion->qid;
if (isset($newQuestionL10nData[$questionl10n->language])) {
$copyLanguage->question = $newQuestionL10nData[$questionl10n->language]->getQuestionText();
$copyLanguage->help = $newQuestionL10nData[$questionl10n->language]->getHelp();
}
$allLanguagesAreCopied = $allLanguagesAreCopied && $copyLanguage->save();
}
}
return $allLanguagesAreCopied;
}
/**
* Copy subquestions of a question
*
* @param int $parentId id of question to be copied
*
* * @before $this->newQuestion must exist and should not be null
*
* @return bool true if all subquestions could be copied&saved, false if a subquestion could not be saved
*/
private function copyQuestionsSubQuestions($parentId)
{
//copy subquestions
$areSubquestionsCopied = true;
$subquestions = \Question::model()->findAllByAttributes(['parent_qid' => $parentId]);
foreach ($subquestions as $subquestion) {
$copiedSubquestion = new \Question();
$copiedSubquestion->attributes = $subquestion->attributes;
$copiedSubquestion->parent_qid = $this->newQuestion->qid;
$copiedSubquestion->qid = null; //new question id needed ...
$areSubquestionsCopied = $areSubquestionsCopied && $copiedSubquestion->save();
foreach ($subquestion->questionl10ns as $subquestLanguage) {
$newSubquestLanguage = new \QuestionL10n();
$newSubquestLanguage->attributes = $subquestLanguage->attributes;
$newSubquestLanguage->qid = $copiedSubquestion->qid;
$newSubquestLanguage->id = null;
$newSubquestLanguage->save();
}
}
return $areSubquestionsCopied;
}
/**
* Copies the answer options of a question
*
* * @before $this->newQuestion must exist and should not be null
*
* @param int $questionIdToCopy
*/
private function copyQuestionsAnswerOptions($questionIdToCopy)
{
$answerOptions = \Answer::model()->findAllByAttributes(['qid' => $questionIdToCopy]);
foreach ($answerOptions as $answerOption) {
$copiedAnswerOption = new \Answer();
$copiedAnswerOption->attributes = $answerOption->attributes;
$copiedAnswerOption->aid = null;
$copiedAnswerOption->qid = $this->newQuestion->qid;
if ($copiedAnswerOption->save()) {
//copy the languages
foreach ($answerOption->answerl10ns as $answerLanguage) {
$copiedAnswerOptionLanguage = new \AnswerL10n();
$copiedAnswerOptionLanguage->attributes = $answerLanguage->attributes;
$copiedAnswerOptionLanguage->id = null;
$copiedAnswerOptionLanguage->aid = $copiedAnswerOption->aid;
$copiedAnswerOptionLanguage->save();
}
}
}
}
/**
* Copies the default answers of the question
*
* * @before $this->newQuestion must exist and should not be null
*
* @param int $questionIdToCopy
*/
private function copyQuestionsDefaultAnswers($questionIdToCopy)
{
$defaultAnswers = \DefaultValue::model()->findAllByAttributes(['qid' => $questionIdToCopy]);
foreach ($defaultAnswers as $defaultAnswer) {
$copiedDefaultAnswer = new \DefaultValue();
$copiedDefaultAnswer->attributes = $defaultAnswer->attributes;
$copiedDefaultAnswer->qid = $this->newQuestion->qid;
$copiedDefaultAnswer->dvid = null;
if ($copiedDefaultAnswer->save()) {
//copy languages if needed
$defaultValLanguages = \DefaultValueL10n::model()
->findAllByAttributes(['dvid' => $defaultAnswer->dvid]);
foreach ($defaultValLanguages as $defaultAnswerL10n) {
$copieDefaultAnswerLanguage = new \DefaultValueL10n();
$copieDefaultAnswerLanguage->attributes = $defaultAnswerL10n->attributes;
$copieDefaultAnswerLanguage->dvid = $copiedDefaultAnswer->dvid;
$copieDefaultAnswerLanguage->id = null;
$copieDefaultAnswerLanguage->save();
}
}
}
}
/**
* Copies the question settings (general_settings (on the left in questioneditor) and advanced settings (bottom)
*
* @param $questionIdToCopy
*
* * @before $this->newQuestion must exist and should not be null
*
* @return boolean True if settings are copied, false otherwise
*/
private function copyQuestionsSettings($questionIdToCopy)
{
$settingsFromQuestionToCopy = \QuestionAttribute::model()->findAllByAttributes(['qid' => $questionIdToCopy]);
$areSettingsCopied = false;
if ($this->newQuestion !== null) {
$areSettingsCopied = true;
foreach ($settingsFromQuestionToCopy as $settingToCopy) {
$newSetting = new \QuestionAttribute();
$newSetting->attributes = $settingToCopy->attributes;
$newSetting->qaid = null; //create new id
$newSetting->qid = $this->newQuestion->qid;
$areSettingsCopied = $areSettingsCopied && $newSetting->save();
}
}
return $areSettingsCopied;
}
/**
* Returns the new created question or null if question was not copied.
*
* @return \Question|null
*/
public function getNewCopiedQuestion()
{
return $this->newQuestion;
}
}