Browse Source

Improve validations, refactor name for clarity.

Syfaro 10 months ago
parent
commit
3669eb6869
2 changed files with 21 additions and 16 deletions
  1. 19
    14
      index.php
  2. 2
    2
      install.sql

+ 19
- 14
index.php View File

@@ -52,7 +52,7 @@ class Question extends Model
52 52
             case 'numeric':
53 53
                 return NumericResponse::where('question_id', $this->id);
54 54
             case 'single':
55
-                return SingleChoiceResponse::where('question_id', $this->id);
55
+                return ChoiceResponse::where('question_id', $this->id);
56 56
         }
57 57
 
58 58
         return null;
@@ -66,7 +66,7 @@ class Question extends Model
66 66
             case 'numeric':
67 67
                 return NumericResponse::where('id', $response_id);
68 68
             case 'single':
69
-                return SingleChoiceResponse::where('id', $response_id);
69
+                return ChoiceResponse::where('id', $response_id);
70 70
         }
71 71
 
72 72
         return null;
@@ -76,14 +76,14 @@ class Question extends Model
76 76
     {
77 77
         if ($this->type == 'freeform' || $this->type == 'numeric') return null;
78 78
 
79
-        return $this->has_many('ResponseChoice');
79
+        return $this->has_many('Choice');
80 80
     }
81 81
 
82 82
     public function choice($choice_id)
83 83
     {
84 84
         if ($this->type == 'freeform' || $this->type == 'numeric') return null;
85 85
 
86
-        return $this->has_one('ResponseChoice');
86
+        return $this->has_one('Choice');
87 87
     }
88 88
 }
89 89
 
@@ -108,7 +108,7 @@ class QuestionConfig extends Model
108 108
  * @property int $question_id
109 109
  * @property string $value
110 110
  */
111
-class ResponseChoice extends Model
111
+class Choice extends Model
112 112
 {
113 113
     public function question()
114 114
     {
@@ -144,11 +144,11 @@ class FreeFormResponse extends Response
144 144
  * @property int $question_id
145 145
  * @property int $choice_id
146 146
  */
147
-class SingleChoiceResponse extends Response
147
+class ChoiceResponse extends Response
148 148
 {
149 149
     public function choice()
150 150
     {
151
-        return ResponseChoice::where('id', $this->choice_id);
151
+        return Choice::where('id', $this->choice_id);
152 152
     }
153 153
 }
154 154
 
@@ -284,7 +284,7 @@ $klein->with('/admin', function () use ($klein) {
284 284
 
285 285
             $klein->with('/choice', function () use ($klein) {
286 286
                 $klein->respond('POST', '/add', function ($request, $response, $service, $app) {
287
-                    $choice = ResponseChoice::create();
287
+                    $choice = Choice::create();
288 288
 
289 289
                     $choice->question_id = $request->id;
290 290
                     $choice->value = $request->value;
@@ -296,7 +296,7 @@ $klein->with('/admin', function () use ($klein) {
296 296
 
297 297
                 $klein->with('/[i:cid]', function () use ($klein) {
298 298
                     $klein->respond('GET', '', function ($request, $response, $service, $app) {
299
-                        $choice = ResponseChoice::find_one($request->cid);
299
+                        $choice = Choice::find_one($request->cid);
300 300
 
301 301
                         return $app->twig->render('admin/question/choice/view.html', [
302 302
                             'choice' => $choice,
@@ -304,7 +304,12 @@ $klein->with('/admin', function () use ($klein) {
304 304
                     });
305 305
 
306 306
                     $klein->respond('POST', '/delete', function ($request, $response, $service, $app) {
307
-                        $choice = ResponseChoice::find_one($request->cid);
307
+                        $choice = Choice::find_one($request->cid);
308
+
309
+                        foreach (ChoiceResponse::where('choice_id', $choice->id)->find_many() as $resp) {
310
+                            $resp->delete();
311
+                        }
312
+
308 313
                         $choice->delete();
309 314
 
310 315
                         return $response->redirect('/admin/question/' . $request->id);
@@ -357,7 +362,7 @@ $klein->with('/admin', function () use ($klein) {
357 362
                             $numeric->save();
358 363
                             break;
359 364
                         case 'single':
360
-                            $single = SingleChoiceResponse::create();
365
+                            $single = ChoiceResponse::create();
361 366
                             $single->question_id = $question->id;
362 367
                             $single->choice_id = $request->value;
363 368
                             $single->save();
@@ -432,7 +437,7 @@ $klein->with('/api/v1', function () use ($klein) {
432 437
                         $responses = [];
433 438
                         foreach ($items as $item) {
434 439
                             $data = $item->as_array();
435
-                            $data['choice'] = ResponseChoice::find_one($item->choice_id)->value;
440
+                            $data['choice'] = Choice::find_one($item->choice_id)->value;
436 441
                             $responses[] = $data;
437 442
                         }
438 443
                         return $response->json([
@@ -499,14 +504,14 @@ $klein->with('/api/v1', function () use ($klein) {
499 504
                                 'error' => 'missing input',
500 505
                             ]);
501 506
                         }
502
-                        $response_choice = ResponseChoice::where('question_id', $question->id)->where('id', $json->value)->find_one();
507
+                        $response_choice = Choice::where('question_id', $question->id)->where('id', $json->value)->find_one();
503 508
                         if (!$response_choice) {
504 509
                             return $response->json([
505 510
                                 'ok' => false,
506 511
                                 'error' => 'invalid choice',
507 512
                             ]);
508 513
                         }
509
-                        $choice = SingleChoiceResponse::create();
514
+                        $choice = ChoiceResponse::create();
510 515
                         $choice->question_id = $question->id;
511 516
                         $choice->choice_id = $response_choice->id;
512 517
                         $choice->save();

+ 2
- 2
install.sql View File

@@ -23,7 +23,7 @@ CREATE TABLE question_config (
23 23
   UNIQUE (question_id, key)
24 24
 );
25 25
 
26
-CREATE TABLE response_choice (
26
+CREATE TABLE choice (
27 27
   id INTEGER PRIMARY KEY,
28 28
   question_id INTEGER NOT NULL,
29 29
   value TEXT NOT NULL,
@@ -38,7 +38,7 @@ CREATE TABLE free_form_response (
38 38
   FOREIGN KEY (question_id) REFERENCES question(id)
39 39
 );
40 40
 
41
-CREATE TABLE single_choice_response (
41
+CREATE TABLE choice_response (
42 42
   id INTEGER PRIMARY KEY,
43 43
   question_id INTEGER NOT NULL,
44 44
   choice_id INTEGER NOT NULL,

Loading…
Cancel
Save