View Issue Details

This bug affects 1 person(s).
 14
IDProjectCategoryView StatusLast Update
16313Bug reportsQuestion editorpublic2020-10-19 10:13
Reporterlarjona Assigned Togabrieljenik  
PrioritylowSeveritypartial_block 
Status closedResolutionfixed 
Product Version4.2.4 
Summary16313: Issues saving/modifying answers: "Answer codes must be unique by question"
Description

Hello, this is an instance running Version 4.2.4+200520, but it had surveys created from former versions.

it seems each time I want to add answer options or modify answer options, I need to change the answer options code to something different than the codes that were present. If I try to save without changing the answer codes, I obtain this error:

Question has been stored, but an error happened: Answer option couldn't be saved. Error: Array ( [code] => Array ( [0] => Answer codes must be unique by question ) ) (/var/www/surveys/application/controllers/admin/questionedit.php:299)

#0 /var/www/surveys/framework/base/CErrorHandler.php(131): CErrorHandler->handleException()
#1 /var/www/surveys/framework/base/CApplication.php(750): CErrorHandler->handle()
#2 unknown(0): LSYii_Application->handleException()

Steps To Reproduce

I have created a new survey, added a new group and a new question "favourite colour", selected Single Choice question, list (Radio) type L, and went to Answer options to create some test answer options. There is already a line there with code AO01, value 0 so I just type black in the Answer option box and click the button "Add answer option" to get AO02, value 0 and type white in the Answer option, add another answer, I get AO03, value 0, and type red as answer option,
Then I click "Save" and they are saved.

If I go to the database:
select * from lime_answers where qid=556;
aid | qid | code | sortorder | assessment_value | scale_id
------+-----+------+-----------+------------------+----------
1030 | 556 | AO01 | 1 | 0 | 0
1031 | 556 | AO02 | 2 | 0 | 0
1032 | 556 | AO03 | 3 | 0 | 0
(3 rows)

Then I go to the text boxes and I change the texts for example to upper caps (BLACK, WHITE, RED) and try to save and obtain the following error:
Question has been stored, but an error happened: Answer option couldn't be saved. Error: Array ( [code] => Array ( [0] => Answer codes must be unique by question ) ) (/var/www/surveys/application/controllers/admin/questionedit.php:299)

#0 /var/www/surveys/framework/base/CErrorHandler.php(131): CErrorHandler->handleException()
#1 /var/www/surveys/framework/base/CApplication.php(750): CErrorHandler->handle()
#2 unknown(0): LSYii_Application->handleException()

And if I go now to the database:
select * from lime_answers where qid=556;
aid | qid | code | sortorder | assessment_value | scale_id
------+-----+------+-----------+------------------+----------
1030 | 556 | AO01 | 1 | 0 | 0
(1 fila)

Strange.

Now I modify the question codes to PAO01, PAO02 and PAO03, and click save, and they are successfully saved, but look at the "aid" codes:

select * from lime_answers where qid=556;
aid | qid | code | sortorder | assessment_value | scale_id
------+-----+-------+-----------+------------------+----------
1030 | 556 | PAO01 | 1 | 0 | 0
1033 | 556 | PAO02 | 2 | 0 | 0
1034 | 556 | PAO03 | 3 | 0 | 0
(3 rows)

Now if I change the assessment values to 10, 20, 30 and click save, I obtain again the error, and the database looks like this:

select * from lime_answers where qid=556;
aid | qid | code | sortorder | assessment_value | scale_id
------+-----+-------+-----------+------------------+----------
1030 | 556 | PAO01 | 1 | 0 | 0
(1 row)

If I delete all the answer options (from the web interface) and create a new answer option XAO01, I can save:

select * from lime_answers where qid=556;
aid | qid | code | sortorder | assessment_value | scale_id
------+-----+-------+-----------+------------------+----------
1035 | 556 | XAO01 | 1 | 0 | 0
(1 row)

but if I add any other option, or modify this option but keeping the XAO01 code, clicking save produces again the error.

TagsNo tags attached.
Bug heat14
Complete LimeSurvey version number (& build)Version 4.2.4+200520
I will donate to the project if issue is resolvedNo
BrowserFirefox 68.8.0esr (64-bit)
Database type & versionpostgresql-9.6 (latest Debian 9 package)
Server OS (if known)Debian 9 stretch
Webserver software & version (if known)nginx 1.10.3 (latest Debian 9 package)
PHP Versionphp 7.0.33 (latest Debian 9 package), php7.0-fpm

Users monitoring this issue

davidrgl

Activities

cdorin

cdorin

2020-05-20 14:19

reporter   ~57958

Hello and thank for your report. Could you please share with us the .lss file? So that everyone works on the same example? Thanks for report!

larjona

larjona

2020-05-20 15:11

reporter   ~57965

Last edited: 2020-07-29 22:44

Hi! thanks for the quick answer. I have attached an export of the survey as zip.
I think it's not related to particularities of the survey, since this was a new one, created as test.
I can provide a dump of the related rows of the database, if you tell me which sql commands to run (I have other surveys there with sensitive content so I cannot provide a whole database dump).

Kind regards,

surveys_archive.zip (3,821 bytes)
jcieszlak

jcieszlak

2020-06-05 20:53

reporter   ~58205

Last edited: 2020-07-29 22:44

I had this same issue. my fix is below. The qid, oldQid, scale_id, oldScaleid are of different types. string / integer mix so !== fails even though the values are the string equivalents are the same. Hopefully cdorin can provide a more permanent fix.
/application/models/Answer.php
public function checkUniqueness($attribute, $params)
{
if($this->code !== $this->oldCode || $this->qid !== strval($this->oldQid) || $this->scale_id !== strval($this->oldScaleId))
{
$model = self::model()->find('code = ? AND qid = ? AND scale_id = ?', array($this->code, $this->qid, $this->scale_id));
if($model != null)
$this->addError('code',"Answer codes must be unique by question");
}
}

xmasterninni

xmasterninni

2020-07-06 15:46

reporter   ~58724

Last edited: 2020-07-29 22:44

Hi, we're having the same issue on our installation, running Version 4.3.2+200629 and postgresql.
As far as i can tell, the problem only occurs on single-choice questions of type "List".

Steps to reproduce: Create Survey -> Add Group -> Add Question -> choose List(Dropdown)
-> add Answers -> Saved successfully! -> change or add answers on the same question without changing the answer codes
(using the auto-generated codes) -> error!

Developer Console shows me this:
error: {
code: 500
errorCode: 0
file: "/var/www/limesurvey/application/controllers/QuestionEditorController.php"
line: 339
message: "Question has been stored, but an error happened:
\nAnswer option couldn't be saved. Error: Array\n(\n
[code] => Array\n(\n [0] => Answer codes must be unique by question \n\n\n)\n"
}

The full Console Error Trace is attached.

Kind regards and let me know if you need anything else!

js-console-06072020.log (2,504 bytes)   
QUESTIONEDIT
{…}
​
data: {…}
​​
error: {…}
​​​
code: 500
​​​
errorCode: 0
​​​
file: "/var/www/limesurvey/application/controllers/QuestionEditorController.php"
​​​
line: 339
​​​
message: "Question has been stored, but an error happened: \nAnswer option couldn't be saved. Error: Array\n(\n    [code] => Array\n        (\n            [0] => Answer codes must be unique by question\n        )\n\n)\n"
​​​
trace: "#0 [internal function]: QuestionEditorController->actionSaveQuestionData('615754')\n#1 /var/www/limesurvey/framework/web/actions/CAction.php(109): ReflectionMethod->invokeArgs(Object(QuestionEditorController), Array)\n#2 /var/www/limesurvey/framework/web/actions/CInlineAction.php(47): CAction->runWithParamsInternal(Object(QuestionEditorController), Object(ReflectionMethod), Array)\n#3 /var/www/limesurvey/framework/web/CController.php(308): CInlineAction->runWithParams(Array)\n#4 /var/www/limesurvey/framework/web/CController.php(286): CController->runAction(Object(CInlineAction))\n#5 /var/www/limesurvey/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)\n#6 /var/www/limesurvey/application/controllers/LSBaseController.php(140): CController->run('saveQuestionDat...')\n#7 /var/www/limesurvey/framework/web/CWebApplication.php(282): LSBaseController->run('saveQuestionDat...')\n#8 /var/www/limesurvey/framework/web/CWebApplication.php(141): CWebApplication->runController('questionEditor/...')\n#9 /var/www/limesurvey/framework/base/CApplication.php(185): CWebApplication->processRequest()\n#10 /var/www/limesurvey/index.php(182): CApplication->run()\n#11 {main}"
​​​
traces: Array(11) [ {…}, {…}, {…}, … ]
​​​
type: "LSJsonException"
​​​
<prototype>: Object { … }
​​
message: "Question has been stored, but an error happened: \nAnswer option couldn't be saved. Error: Array\n(\n    [code] => Array\n        (\n            [0] => Answer codes must be unique by question\n        )\n\n)\n"
​​
redirectTo: "/index.php?r=questionEditor/view&surveyid=615754&gid=29&qid=153"
​​
success: false
​​
<prototype>: Object { … }
​
error: "Internal Server Error"
​
success: false
​
transferStatus: "error"
​
xhr: Object { readyState: 4, getResponseHeader: getResponseHeader(e), getAllResponseHeaders: getAllResponseHeaders(), … }
​
<prototype>: Object { … }
ConsoleShim.js:131:26
    value ConsoleShim.js:131
    submitCurrentState App.vue:476
js-console-06072020.log (2,504 bytes)   
xmasterninni

xmasterninni

2020-07-17 21:16

reporter   ~58997

Last edited: 2020-07-29 22:44

Just a quick update to my previous post:
A new Limesurvey Installation and choosing MySQL over postgresql fixes the issue on our side.

jcieszlak

jcieszlak

2020-08-19 18:11

reporter   ~59528

This bug still exists in version 4.3.11 when using postgres. adding strval() still fixes the issue on my side. I am not sure why the types are different here when comparing oldQid and oldScaleId. Can a fix be put into the master branch so I do not have to apply this fix during each update?
/application/models/Answer.php
public function checkUniqueness($attribute, $params)
{
if($this->code !== $this->oldCode || $this->qid !== strval($this->oldQid) || $this->scale_id !== strval($this->oldScaleId))

gabrieljenik

gabrieljenik

2020-08-20 01:11

manager   ~59539

PR: https://github.com/LimeSurvey/LimeSurvey/pull/1558

Replaced comparison operators as they could end up having different types

gabrieljenik

gabrieljenik

2020-10-16 07:13

manager   ~60222

Fix committed to master branch: http://bugs.limesurvey.org/plugin.php?page=Source/view&amp;id=30604

lime_release_bot

lime_release_bot

2020-10-19 10:13

administrator   ~60257

Fixed in Release 4.3.22+201019

Related Changesets

LimeSurvey: master 56b5ac45

2020-10-16 07:13:08

gabrieljenik


Committer: GitHub Details Diff
Fixed issue 16313: Issues saving/modifying answers: "Answer codes must be unique by question" (#1558)

Replaced comparison operators as they could end up having different types
Affected Issues
16313
mod - application/models/Answer.php Diff File

Issue History

Date Modified Username Field Change
2020-05-20 12:57 larjona New Issue
2020-05-20 14:19 cdorin Note Added: 57958
2020-05-20 14:20 cdorin Status new => feedback
2020-05-20 15:11 larjona Note Added: 57965
2020-05-20 15:11 larjona File Added: surveys_archive.zip
2020-05-20 15:11 larjona Status feedback => new
2020-05-20 17:01 ollehar Priority none => normal
2020-05-20 20:53 davidrgl Issue Monitored: davidrgl
2020-06-05 20:53 jcieszlak Note Added: 58205
2020-07-06 15:46 xmasterninni Note Added: 58724
2020-07-06 15:46 xmasterninni File Added: js-console-06072020.log
2020-07-17 21:16 xmasterninni Note Added: 58997
2020-07-29 22:43 cdorin Priority normal => low
2020-07-29 22:43 cdorin Status new => confirmed
2020-07-29 22:44 cdorin Zoho Sprints => |Yes|
2020-07-29 22:44 swendrich Zoho Sprints ID => 14469000000197073
2020-08-19 18:11 jcieszlak Note Added: 59528
2020-08-20 01:11 gabrieljenik Note Added: 59539
2020-10-16 07:13 gabrieljenik Changeset attached => LimeSurvey master 56b5ac45
2020-10-16 07:13 gabrieljenik Note Added: 60222
2020-10-16 07:13 gabrieljenik Assigned To => gabrieljenik
2020-10-16 07:13 gabrieljenik Resolution open => fixed
2020-10-19 10:13 lime_release_bot Zoho Sprints Yes => |Yes|
2020-10-19 10:13 lime_release_bot Note Added: 60257
2020-10-19 10:13 lime_release_bot Status confirmed => closed
2021-08-03 09:20 guest Bug heat 12 => 14