77
77
/**
78
78
* @psalm-import-type FormsPartialForm from ResponseDefinitions
79
79
* @psalm-import-type FormsForm from ResponseDefinitions
80
+ * @psalm-import-type FormsQuestion from ResponseDefinitions
81
+ * @psalm-import-type FormsOption from ResponseDefinitions
80
82
*/
81
83
class ApiController extends OCSController {
82
84
private ?IUser $ currentUser ;
@@ -367,10 +369,10 @@ public function deleteForm(int $formId): DataResponse {
367
369
/**
368
370
* Read all questions (including options)
369
371
*
370
- * @param int $formId FormId
371
- * @return DataResponse
372
- * @throws OCSBadRequestException
373
- * @throws OCSForbiddenException
372
+ * @param int $formId the form id
373
+ * @return DataResponse<array<FormQuestion>, Http::STATUS_OK, array<>>
374
+ * @throws OCSForbiddenException User has no permissions to get this form
375
+ * @throws OCSNotFoundException Could not find form
374
376
*/
375
377
#[CORS ()]
376
378
#[NoAdminRequired()]
@@ -380,7 +382,7 @@ public function getQuestions(int $formId): DataResponse {
380
382
$ form = $ this ->formMapper ->findById ($ formId );
381
383
} catch (IMapperException $ e ) {
382
384
$ this ->logger ->debug ('Could not find form ' );
383
- throw new OCSBadRequestException ();
385
+ throw new OCSNotFoundException ();
384
386
}
385
387
386
388
if (!$ this ->formsService ->hasUserAccess ($ form )) {
@@ -398,9 +400,10 @@ public function getQuestions(int $formId): DataResponse {
398
400
*
399
401
* @param int $formId FormId
400
402
* @param int $questionId QuestionId
401
- * @return DataResponse
402
- * @throws OCSBadRequestException
403
- * @throws OCSForbiddenException
403
+ * @return DataResponse<FormQuestion, Http::STATUS_OK, array<>>
404
+ * @throws OCSBadRequestException Question doesn\'t belong to given Form
405
+ * @throws OCSForbiddenException User has no permissions to get this form
406
+ * @throws OCSNotFoundException Could not find form
404
407
*/
405
408
#[CORS ()]
406
409
#[NoAdminRequired()]
@@ -410,7 +413,7 @@ public function getQuestion(int $formId, int $questionId): DataResponse {
410
413
$ form = $ this ->formMapper ->findById ($ formId );
411
414
} catch (IMapperException $ e ) {
412
415
$ this ->logger ->debug ('Could not find form ' );
413
- throw new OCSBadRequestException ();
416
+ throw new OCSNotFoundException ();
414
417
}
415
418
416
419
if (!$ this ->formsService ->hasUserAccess ($ form )) {
@@ -421,7 +424,7 @@ public function getQuestion(int $formId, int $questionId): DataResponse {
421
424
$ question = $ this ->formsService ->getQuestion ($ questionId );
422
425
423
426
if ($ question ['formId ' ] !== $ formId ) {
424
- throw new OCSBadRequestException ('Question doesn \'t belong to given Form ' );
427
+ throw new OCSBadRequestException ('Question doesn \'t belong to given form ' );
425
428
}
426
429
427
430
return new DataResponse ($ question );
@@ -433,10 +436,14 @@ public function getQuestion(int $formId, int $questionId): DataResponse {
433
436
* @param int $formId the form id
434
437
* @param string $type the new question type
435
438
* @param string $text the new question title
436
- * @param int $fromId (optional) id of the question that should be cloned
437
- * @return DataResponse
438
- * @throws OCSBadRequestException
439
- * @throws OCSForbiddenException
439
+ * @param ?int $fromId (optional) id of the question that should be cloned
440
+ * @return DataResponse<FormQuestion, Http::STATUS_CREATED, array<>>
441
+ * @throws OCSBadRequestException Invalid type
442
+ * @throws OCSBadRequestException Datetime question type no longer supported
443
+ * @throws OCSForbiddenException User has no permissions to get this form
444
+ * @throws OCSForbiddenException This form is archived and can not be modified
445
+ * @throws OCSNotFoundException Could not find form
446
+ * @throws OCSNotFoundException Could not find question
440
447
*/
441
448
#[CORS ()]
442
449
#[NoAdminRequired()]
@@ -530,19 +537,26 @@ public function newQuestion(int $formId, ?string $type = null, string $text = ''
530
537
531
538
$ this ->formMapper ->update ($ form );
532
539
533
- return new DataResponse ($ response );
540
+ return new DataResponse ($ response, Http:: STATUS_CREATED );
534
541
}
535
542
536
543
/**
537
- * Writes the given key-value pairs into Database.
538
- * Key ' order' should only be changed by reorderQuestions() and is not allowed here.
544
+ * Writes the given key-value pairs into Database
545
+ * Key ` order` should only be changed by reorderQuestions() and is not allowed here
539
546
*
540
547
* @param int $formId the form id
541
548
* @param int $questionId id of question to update
542
- * @param array $keyValuePairs Array of key=>value pairs to update.
543
- * @return DataResponse
544
- * @throws OCSBadRequestException
545
- * @throws OCSForbiddenException
549
+ * @param array<string, mixed> $keyValuePairs Array of key=>value pairs to update.
550
+ * @return DataResponse<int id, Http::STATUS_OK, array<>>
551
+ * @throws OCSBadRequestException Question doesn\'t belong to given Form
552
+ * @throws OCSBadRequestException Invalid extraSettings, will not update.
553
+ * @throws OCSForbiddenException Empty keyValuePairs, will not update
554
+ * @throws OCSForbiddenException Not allowed to update `id` or `formId`
555
+ * @throws OCSForbiddenException Please use reorderQuestions() to change order
556
+ * @throws OCSForbiddenException This form is archived and can not be modified
557
+ * @throws OCSForbiddenException User has no permissions to get this form
558
+ * @throws OCSNotFoundException Could not find form
559
+ * @throws OCSNotFoundException Could not find question
546
560
*/
547
561
#[CORS ()]
548
562
#[NoAdminRequired()]
@@ -558,7 +572,7 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair
558
572
$ question = $ this ->questionMapper ->findById ($ questionId );
559
573
} catch (IMapperException $ e ) {
560
574
$ this ->logger ->debug ('Could not find question ' );
561
- throw new OCSBadRequestException ('Could not find question ' );
575
+ throw new OCSNotFoundException ('Could not find question ' );
562
576
}
563
577
564
578
if ($ question ->getFormId () !== $ formId ) {
@@ -568,19 +582,19 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair
568
582
$ form = $ this ->getFormIfAllowed ($ formId );
569
583
if ($ this ->formsService ->isFormArchived ($ form )) {
570
584
$ this ->logger ->debug ('This form is archived and can not be modified ' );
571
- throw new OCSForbiddenException ();
585
+ throw new OCSForbiddenException (' This form is archived and can not be modified ' );
572
586
}
573
587
574
588
// Don't allow empty array
575
589
if (sizeof ($ keyValuePairs ) === 0 ) {
576
590
$ this ->logger ->info ('Empty keyValuePairs, will not update. ' );
577
- throw new OCSForbiddenException ( );
591
+ throw new OCSBacRequestException ( ' This form is archived and can not be modified ' );
578
592
}
579
593
580
594
//Don't allow to change id or formId
581
595
if (key_exists ('id ' , $ keyValuePairs ) || key_exists ('formId ' , $ keyValuePairs )) {
582
596
$ this ->logger ->debug ('Not allowed to update \'id \' or \'formId \'' );
583
- throw new OCSForbiddenException ();
597
+ throw new OCSForbiddenException (' Not allowed to update \' id \' or \' formId \'' );
584
598
}
585
599
586
600
// Don't allow to reorder here
@@ -609,9 +623,12 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair
609
623
*
610
624
* @param int $formId the form id
611
625
* @param int $questionId the question id
612
- * @return DataResponse
613
- * @throws OCSBadRequestException
614
- * @throws OCSForbiddenException
626
+ * @return DataResponse<int id, Http::STATUS_OK, array<>>
627
+ * @throws OCSBadRequestException Question doesn\'t belong to given Form
628
+ * @throws OCSForbiddenException This form is archived and can not be modified
629
+ * @throws OCSForbiddenException User has no permissions to get this form
630
+ * @throws OCSNotFoundException Could not find form
631
+ * @throws OCSNotFoundException Could not find question
615
632
*/
616
633
#[CORS ()]
617
634
#[NoAdminRequired()]
@@ -625,7 +642,7 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
625
642
$ question = $ this ->questionMapper ->findById ($ questionId );
626
643
} catch (IMapperException $ e ) {
627
644
$ this ->logger ->debug ('Could not find question ' );
628
- throw new OCSBadRequestException ('Could not find question ' );
645
+ throw new OCSNotFoundException ('Could not find question ' );
629
646
}
630
647
631
648
if ($ question ->getFormId () !== $ formId ) {
@@ -635,7 +652,7 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
635
652
$ form = $ this ->getFormIfAllowed ($ formId );
636
653
if ($ this ->formsService ->isFormArchived ($ form )) {
637
654
$ this ->logger ->debug ('This form is archived and can not be modified ' );
638
- throw new OCSForbiddenException ();
655
+ throw new OCSForbiddenException (' This form is archived and can not be modified ' );
639
656
}
640
657
641
658
// Store Order of deleted Question
@@ -661,13 +678,19 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
661
678
}
662
679
663
680
/**
664
- * Updates the Order of all Questions of a Form.
681
+ * Updates the Order of all Questions of a Form
665
682
*
666
683
* @param int $formId Id of the form to reorder
667
- * @param array<int, int> $newOrder Array of Question-Ids in new order.
668
- * @return DataResponse
669
- * @throws OCSBadRequestException
670
- * @throws OCSForbiddenException
684
+ * @param array<string, int> $newOrder Array of Question-Ids in new order.
685
+ * @return DataResponse<array<int, int>, Http::STATUS_OK, array<>>
686
+ * @throws OCSBadRequestException The given array contains duplicates
687
+ * @throws OCSBadRequestException The length of the given array does not match the number of stored questions
688
+ * @throws OCSBadRequestException Question doesn\'t belong to given Form
689
+ * @throws OCSBadRequestException One question has already been marked as deleted
690
+ * @throws OCSForbiddenException This form is archived and can not be modified
691
+ * @throws OCSForbiddenException User has no permissions to get this form
692
+ * @throws OCSNotFoundException Could not find form
693
+ * @throws OCSNotFoundException Could not find question
671
694
*/
672
695
#[CORS ()]
673
696
#[NoAdminRequired()]
@@ -681,7 +704,7 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
681
704
$ form = $ this ->getFormIfAllowed ($ formId );
682
705
if ($ this ->formsService ->isFormArchived ($ form )) {
683
706
$ this ->logger ->debug ('This form is archived and can not be modified ' );
684
- throw new OCSForbiddenException ();
707
+ throw new OCSForbiddenException (' This form is archived and can not be modified ' );
685
708
}
686
709
687
710
// Check if array contains duplicates
@@ -705,27 +728,27 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
705
728
try {
706
729
$ questions [$ arrayKey ] = $ this ->questionMapper ->findById ($ questionId );
707
730
} catch (IMapperException $ e ) {
708
- $ this ->logger ->debug ('Could not find question. Id: {questionId} ' , [
731
+ $ this ->logger ->debug ('Could not find question {questionId} ' , [
709
732
'questionId ' => $ questionId
710
733
]);
711
- throw new OCSBadRequestException ( );
734
+ throw new OCSNotFoundException ( ' Could not find question ' );
712
735
}
713
736
714
737
// Abort if a question is not part of the Form.
715
738
if ($ questions [$ arrayKey ]->getFormId () !== $ formId ) {
716
- $ this ->logger ->debug ('This Question is not part of the given Form: questionId : {questionId} ' , [
739
+ $ this ->logger ->debug ('This Question is not part of the given form : {questionId} ' , [
717
740
'questionId ' => $ questionId
718
741
]);
719
- throw new OCSBadRequestException ();
742
+ throw new OCSBadRequestException (' Question doesn \' t belong to given Form ' );
720
743
}
721
744
722
745
// Abort if a question is already marked as deleted (order==0)
723
746
$ oldOrder = $ questions [$ arrayKey ]->getOrder ();
724
747
if ($ oldOrder === 0 ) {
725
- $ this ->logger ->debug ('This Question has already been marked as deleted: Id: {questionId} ' , [
748
+ $ this ->logger ->debug ('This question has already been marked as deleted: Id: {questionId} ' , [
726
749
'questionId ' => $ questions [$ arrayKey ]->getId ()
727
750
]);
728
- throw new OCSBadRequestException ();
751
+ throw new OCSBadRequestException (' One question has already been marked as deleted ' );
729
752
}
730
753
731
754
// Only set order, if it changed.
0 commit comments