@@ -494,6 +494,78 @@ public function newQuestion(int $id, ?string $type = null, string $text = '', ?i
494
494
return new DataResponse ($ response );
495
495
}
496
496
497
+ /**
498
+ * @CORS
499
+ * @NoAdminRequired
500
+ *
501
+ * Writes the given key-value pairs into Database.
502
+ * Key 'order' should only be changed by reorderQuestions() and is not allowed here.
503
+ *
504
+ * @param int $id the form id
505
+ * @param int $questionId id of question to update
506
+ * @param array $keyValuePairs Array of key=>value pairs to update.
507
+ * @return DataResponse
508
+ * @throws OCSBadRequestException
509
+ * @throws OCSForbiddenException
510
+ */
511
+ public function updateQuestion (int $ id , int $ questionId , array $ keyValuePairs ): DataResponse {
512
+ $ this ->logger ->debug ('Updating question: formId: {id}, questionId: {id}, values: {keyValuePairs} ' , [
513
+ 'id ' => $ id ,
514
+ 'questionId ' => $ questionId ,
515
+ 'keyValuePairs ' => $ keyValuePairs
516
+ ]);
517
+
518
+ try {
519
+ $ question = $ this ->questionMapper ->findById ($ questionId );
520
+ } catch (IMapperException $ e ) {
521
+ $ this ->logger ->debug ('Could not find question ' );
522
+ throw new OCSBadRequestException ('Could not find question ' );
523
+ }
524
+
525
+ if ($ question ->getFormId () !== $ id ) {
526
+ throw new OCSBadRequestException ('Question doesn \'t belong to given Form ' );
527
+ }
528
+
529
+ $ form = $ this ->getFormIfAllowed ($ id );
530
+ if ($ this ->formsService ->isFormArchived ($ form )) {
531
+ $ this ->logger ->debug ('This form is archived and can not be modified ' );
532
+ throw new OCSForbiddenException ();
533
+ }
534
+
535
+ // Don't allow empty array
536
+ if (sizeof ($ keyValuePairs ) === 0 ) {
537
+ $ this ->logger ->info ('Empty keyValuePairs, will not update. ' );
538
+ throw new OCSForbiddenException ();
539
+ }
540
+
541
+ //Don't allow to change id or formId
542
+ if (key_exists ('id ' , $ keyValuePairs ) || key_exists ('formId ' , $ keyValuePairs )) {
543
+ $ this ->logger ->debug ('Not allowed to update id or formId ' );
544
+ throw new OCSForbiddenException ();
545
+ }
546
+
547
+ // Don't allow to reorder here
548
+ if (key_exists ('order ' , $ keyValuePairs )) {
549
+ $ this ->logger ->debug ('Key \'order \' is not allowed on updateQuestion. Please use reorderQuestions() to change order. ' );
550
+ throw new OCSForbiddenException ('Please use reorderQuestions() to change order ' );
551
+ }
552
+
553
+ if (key_exists ('extraSettings ' , $ keyValuePairs ) && !$ this ->formsService ->areExtraSettingsValid ($ keyValuePairs ['extraSettings ' ], $ question ->getType ())) {
554
+ throw new OCSBadRequestException ('Invalid extraSettings, will not update. ' );
555
+ }
556
+
557
+ // Create QuestionEntity with given Params & Id.
558
+ $ question = Question::fromParams ($ keyValuePairs );
559
+ $ question ->setId ($ questionId );
560
+
561
+ // Update changed Columns in Db.
562
+ $ this ->questionMapper ->update ($ question );
563
+
564
+ $ this ->formsService ->setLastUpdatedTimestamp ($ id );
565
+
566
+ return new DataResponse ($ question ->getId ());
567
+ }
568
+
497
569
/*
498
570
/* Legacy API v2 methods (TODO: remove with Forms v5)
499
571
*/
@@ -959,7 +1031,7 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
959
1031
* @throws OCSBadRequestException
960
1032
* @throws OCSForbiddenException
961
1033
*/
962
- public function updateQuestion (int $ id , array $ keyValuePairs ): DataResponse {
1034
+ public function updateQuestionLegacy (int $ id , array $ keyValuePairs ): DataResponse {
963
1035
$ this ->logger ->debug ('Updating question: questionId: {id}, values: {keyValuePairs} ' , [
964
1036
'id ' => $ id ,
965
1037
'keyValuePairs ' => $ keyValuePairs
0 commit comments