見出し画像

survey.js - 問題の種類

ちょっとお休み回

今、現状の構造だが、「質問」自体は survey_elements というテーブルに格納されている(名前がわかり辛いけどライブラリー側に合わせている)

        Schema::create('survey_elements', function (Blueprint $table) {
            $table->id();
            $table->foreignId('survey_page_id')->constrained();
            $table->string('title');
            $table->boolean('is_required')->default(0);
            $table->enum('type', ['text', 'radiogroup', 'checkbox']);
            $table->timestamps();
        });

つまりこのtypeが質問の形式になるんだけど、今は3種類しか定義していない。もうちょっと増やしてみよう。

chatgpt作成の表

の前に今あるものを見てみる。

text型

あんま語り所が無い。主に名前の記入とかで使われるであろう(記名式の場合)

checkboxとradiogroup

これもあんま語り所が無いか?択一だったり多肢選択だったりするものである。

rating式

ここからはDBの拡張が必要であるが、とりあえずjsonはこんな感じである

                {
                    "type": "rating",
                    "name": "satisfactionRating",
                    "title": "サービスに対する満足度を評価してください",
                    "rateMin": 1,
                    "rateMax": 5,
                    "rateStep": 1,
                    "minRateDescription": "非常に不満",
                    "maxRateDescription": "非常に満足"
                }
            

この場合

        Schema::create('survey_elements', function (Blueprint $table) {
            $table->id();
            $table->foreignId('survey_page_id')->constrained();
            $table->string('title');
            $table->boolean('is_required')->default(0);
            $table->enum('type', ['text', 'radiogroup', 'checkbox', 'rating']);
            $table->integer('rate_min')->nullable();
            $table->integer('rate_max')->nullable();
            $table->integer('rate_step')->nullable();
            $table->string('min_rate_description')->nullable();
            $table->string('max_rate_description')->nullable();
            $table->timestamps();
        });

こんな感じになるかな?宿命としてこの手のやり方をするとカラムがどんどん生える、なので今コードを書いてたものに関しては最小限にしていたけど場合によってはこのような構成も必要かもしれない(あるいはjson、しかしこここでのjsonは…)

そうすっとfillableも増えるし

    protected $fillable = [
        'survey_page_id',
        'type',
        'title',
        'is_required',
        'rate_min',
        'rate_max',
        'rate_step',
        'min_rate_description',
        'max_rate_description',
    ];

作成するとこも増える

                $data                 = [
                    'survey_page_id'       => $surveyPage->id,
                    'type'                 => $element['type'],
                    'title'                => $element['title'],
                    'is_required'          => $element['isRequired'] ?? false,
                    'rate_min'             => $element['rateMin'] ?? null,
                    'rate_max'             => $element['rateMax'] ?? null,
                    'rate_step'            => $element['rateStep'] ?? null,
                    'max_rate_description' => $element['maxRateDescription'] ?? null,
                    'min_rate_description' => $element['minRateDescription'] ?? null,
                ];
                return [
                    'type'               => $element->type,
                    'name'               => (string)$element->id,
                    'title'              => $element->title,
                    'isRequired'         => $element->is_required,
                    'choices'            => $choices,
                    'rateMin'            => $element->rate_min,
                    'rateMax'            => $element->rate_max,
                    'rateStep'           => $element->rate_step,
                    'minRateDescription' => $element->min_rate_description,
                    'maxRateDescription' => $element->max_rate_description,
                ];

このように拡張が非常に増えるのでややこしいが完成すると

こんな具合である。ところで

1:とても不満:やや不満3:普通4:とても満足5:非常に満足

みたいな表記をお願いします!とかいう話もまあまああるわけ。これはまた濃くて以下のようにDBを拡張する。なお、descriptionも拡張した

        Schema::create('survey_elements', function (Blueprint $table) {
            $table->id();
            $table->foreignId('survey_page_id')->constrained();
            $table->string('title');
            $table->boolean('is_required')->default(0);
            $table->enum('type', ['text', 'radiogroup', 'checkbox', 'rating']);
            $table->integer('rate_min')->nullable();
            $table->integer('rate_max')->nullable();
            $table->integer('rate_step')->nullable();
            $table->string('min_rate_description')->nullable();
            $table->string('max_rate_description')->nullable();
            $table->json('rate_values')->nullable(); // これと
            $table->string('description')->nullable(); // これ
            $table->timestamps();
        });

jsonを使っているのでモデルから更新する

    protected $fillable = [
        'survey_page_id',
        'type',
        'title',
        'is_required',
        'rate_min',
        'rate_max',
        'rate_step',
        'min_rate_description',
        'max_rate_description',
        'rate_values',
        'description',
    ];
    protected $casts = [
        'rate_values' => 'array',
    ];
                $data                 = [
                    'survey_page_id'       => $surveyPage->id,
                    'type'                 => $element['type'],
                    'title'                => $element['title'],
                    'is_required'          => $element['isRequired'] ?? false,
                    'rate_min'             => $element['rateMin'] ?? null,
                    'rate_max'             => $element['rateMax'] ?? null,
                    'rate_step'            => $element['rateStep'] ?? null,
                    'max_rate_description' => $element['maxRateDescription'] ?? null,
                    'min_rate_description' => $element['minRateDescription'] ?? null,
                    'rate_values'          => $element['rateValues'] ?? [],
                    'description'          => $element['description'] ?? null,
                ];


なる。LocaleStringが微妙www だし、普通にdescriptionを与えるだけにしとく方があたしゃ好みっすかね。

その他

は、あんま使わないかもしれんから次回やるかもしれないし、やらないかもしれない。質問の種類を増やすのはともかく、既存タイプを使ってもうちょっと面白い使い方もできる、かもしれない。








この記事が気に入ったらサポートをしてみませんか?