]> _ Git - psq.git/commitdiff
poll answer viewing
authorLouis Jeckel <louis.jeckel@outlook.cm>
Sat, 15 Aug 2020 23:13:46 +0000 (01:13 +0200)
committerLouis Jeckel <louis.jeckel@outlook.cm>
Sat, 15 Aug 2020 23:13:46 +0000 (01:13 +0200)
app/Http/Controllers/Admin/PollController.php
app/Models/Poll.php
public/admin/js/admin.js
public/js/app.js
resources/js/components/PollAnswers/PollAnswers.vue
resources/views/site/blocks/poll_check.blade.php

index dfe6567e808494c9ddcd5483359cdc8898a0cd0c..cf27b1ebbfd90f58466f914a3ec0a91f233e00ee 100644 (file)
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin;
 
 use A17\Twill\Http\Controllers\Admin\ModuleController;
 use App\Models\Poll;
+use Illuminate\Support\Arr;
 
 class PollController extends ModuleController
 {
@@ -24,9 +25,41 @@ class PollController extends ModuleController
 
     public function getPollAnswers(Poll $poll)
     {
+        $questions = $poll->getQuestions();
+        $answers = $poll->entries()->get();
+
+        $answerCount = $answers->count();
+
+
+        $data = $questions->map(function($question) use($answers, $answerCount){
+
+
+            $ans = $answers->flatMap(function($ans) use($question){
+                    return Arr::wrap($ans->answers[$question['slug']]);
+            });
+            $ansCountBy = $ans->countBy()->toArray();
+
+
+            $ansCountBy = array_map(function($value, $count) use($answerCount) {
+                return [
+                    'value' => $value,
+                    'count' => $count,
+                    'percentage' => number_format($count*100/$answerCount, 2)
+                ];
+            }, array_keys($ansCountBy), $ansCountBy);
+
+
+
+
+            return array_merge($question, [
+                'answers' => $ansCountBy
+            ]);
+        });
+
+
         return response()->json([
-            'poll' => $poll->getQuestions(),
-            'answsers' => $poll->entries()->get()
+            'questions' => $data,
+            'entries_count' => $answerCount,
         ]);
     }
 
index 2a7019ce095e4502e643a5d565a30198e5c660e3..e1a91fbb2abc9555e5831bed7357591e84831176 100644 (file)
@@ -8,6 +8,7 @@ use A17\Twill\Models\Behaviors\HasMedias;
 use A17\Twill\Models\Block;
 use A17\Twill\Models\Model;
 use App\PollEntry;
+use Illuminate\Support\Collection;
 use Illuminate\Support\Str;
 
 class Poll extends Model
@@ -24,6 +25,11 @@ class Poll extends Model
         'title',
     ];
 
+    public static $pollTypes = [
+        'poll_text' => 'Question ouverte (texte)',
+        'poll_check_check' => 'Question fermée (choix multiples)',
+        'poll_check_radio' => 'Question fermée (choix unique)',
+    ];
 
     /**
      * @return \Illuminate\Database\Eloquent\Relations\HasMany
@@ -34,16 +40,23 @@ class Poll extends Model
     }
 
 
+    /**
+     * @return Collection
+     */
     public function getQuestions()
     {
         $questions = $this->blocks()->whereNull('parent_id')->get();
 
         return $questions->map(function(Block $q) {
             $q->options = $q->children;
+            $subtype = $q->input('type');
+            $subtype = $subtype === null ?
+                '' :
+                "_$subtype";
 
             return [
-                'type' => $q->type,
-                'sub_type' => $q->input('type'),
+                'type' => $type = $q->type.$subtype,
+                'type_str' => self::$pollTypes[$type] ?? 'Type inconnu',
                 'question' => $question = $q->input('question'),
                 'slug' => Str::slug($question),
                 'position' => $q->position,
index ae0d05ccf218e9fbec2d6fcc92fbce21c04395d9..77bc4a7d4d5657013377cb4fd5872e5cc6304fd2 100644 (file)
@@ -2575,14 +2575,51 @@ __webpack_require__.r(__webpack_exports__);
 //
 //
 //
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
 /* harmony default export */ __webpack_exports__["default"] = ({
   name: "PollAnswers",
   data: function data() {
     return {
       polls: [],
       selected_poll: null,
-      answers: null,
-      loaded: false
+      poll_data: null,
+      loaded: false,
+      selected_question: null
     };
   },
   watch: {
@@ -2590,7 +2627,7 @@ __webpack_require__.r(__webpack_exports__);
       var _this = this;
 
       axios.get('/otherContent/pollAnswers/' + value.id).then(function (d) {
-        return _this.answers = d.data;
+        return _this.poll_data = d.data;
       });
     }
   },
@@ -50541,7 +50578,7 @@ var render = function() {
       ? _c(
           "div",
           [
-            _c("div", { staticClass: "form-group" }, [
+            _c("div", { staticClass: "form-group mb-4" }, [
               _c("label", { attrs: { for: "polls" } }),
               _vm._v(" "),
               _c(
@@ -50584,26 +50621,117 @@ var render = function() {
             _vm._v(" "),
             !!_vm.selected_poll
               ? [
-                  _vm.answers === null
+                  _vm.poll_data === null
                     ? _c("div", { staticClass: "alert alert-info" }, [
                         _vm._v(
-                          "\n                Chargement en cours...\n            "
+                          "\n                    Chargement en cours...\n                "
                         )
                       ])
-                    : !!_vm.answers && _vm.answers.length === 0
+                    : !!_vm.poll_data && _vm.poll_data.entries_count === 0
                     ? _c("div", { staticClass: "alert alert-warning" }, [
                         _vm._v(
-                          "\n                Aucune réponse pour le moment !\n            "
+                          "\n                    Aucune réponse pour le moment !\n                "
                         )
                       ])
-                    : _c("div", [
-                        _c("pre", [
+                    : _c("div", { staticClass: "box p-3" }, [
+                        _c("p", { staticClass: "lead" }, [
                           _vm._v(
-                            "                    " +
-                              _vm._s(_vm.answers) +
-                              "\n                "
+                            _vm._s(_vm.poll_data.entries_count) +
+                              " réponses enregistrées"
                           )
-                        ])
+                        ]),
+                        _vm._v(" "),
+                        _c("div", { staticClass: "form-group mb-2" }, [
+                          _c("label", { attrs: { for: "question" } }, [
+                            _vm._v(
+                              "Sélectionnez une question pour voir les résultats"
+                            )
+                          ]),
+                          _vm._v(" "),
+                          _c(
+                            "select",
+                            {
+                              directives: [
+                                {
+                                  name: "model",
+                                  rawName: "v-model",
+                                  value: _vm.selected_question,
+                                  expression: "selected_question"
+                                }
+                              ],
+                              staticClass: "form-control",
+                              attrs: { name: "question", id: "question" },
+                              on: {
+                                change: function($event) {
+                                  var $$selectedVal = Array.prototype.filter
+                                    .call($event.target.options, function(o) {
+                                      return o.selected
+                                    })
+                                    .map(function(o) {
+                                      var val =
+                                        "_value" in o ? o._value : o.value
+                                      return val
+                                    })
+                                  _vm.selected_question = $event.target.multiple
+                                    ? $$selectedVal
+                                    : $$selectedVal[0]
+                                }
+                              }
+                            },
+                            _vm._l(_vm.poll_data.questions, function(question) {
+                              return _c(
+                                "option",
+                                { domProps: { value: question } },
+                                [_vm._v(_vm._s(question.question))]
+                              )
+                            }),
+                            0
+                          )
+                        ]),
+                        _vm._v(" "),
+                        _vm.selected_question === null
+                          ? _c("div", { staticClass: "alert alert-info" }, [
+                              _vm._v(
+                                "\n                        Sélectionnez une question pour voir les réponses\n                    "
+                              )
+                            ])
+                          : _c("div", [
+                              _c(
+                                "table",
+                                {
+                                  staticClass:
+                                    "table table-bordered table-hover"
+                                },
+                                [
+                                  _vm._m(0),
+                                  _vm._v(" "),
+                                  _c(
+                                    "tbody",
+                                    _vm._l(
+                                      _vm.selected_question.answers,
+                                      function(answer) {
+                                        return _c("tr", [
+                                          _c("td", [
+                                            _vm._v(_vm._s(answer.value))
+                                          ]),
+                                          _vm._v(" "),
+                                          _c("td", [
+                                            _vm._v(_vm._s(answer.count))
+                                          ]),
+                                          _vm._v(" "),
+                                          _c("td", [
+                                            _vm._v(
+                                              _vm._s(answer.percentage) + "%"
+                                            )
+                                          ])
+                                        ])
+                                      }
+                                    ),
+                                    0
+                                  )
+                                ]
+                              )
+                            ])
                       ])
                 ]
               : _vm._e()
@@ -50612,14 +50740,29 @@ var render = function() {
         )
       : _vm.loaded
       ? _c("div", { staticClass: "alert alert-warning" }, [
-          _vm._v("\n        Aucun sondage trouvé\n    ")
+          _vm._v("\n            Aucun sondage trouvé\n        ")
         ])
       : _c("div", { staticClass: "alert alert-info" }, [
-          _vm._v("\n        Chargement en cours...\n    ")
+          _vm._v("\n            Chargement en cours...\n        ")
         ])
   ])
 }
-var staticRenderFns = []
+var staticRenderFns = [
+  function() {
+    var _vm = this
+    var _h = _vm.$createElement
+    var _c = _vm._self._c || _h
+    return _c("thead", [
+      _c("tr", [
+        _c("th", [_vm._v("Réponse")]),
+        _vm._v(" "),
+        _c("th", [_vm._v("Occurences")]),
+        _vm._v(" "),
+        _c("th", [_vm._v("Pourcentage")])
+      ])
+    ])
+  }
+]
 render._withStripped = true
 
 
index 6b9b84507909511138e85f2be041ec267aca4636..8c9f06b25e35f7f5db025f7210673a7efe3a41ab 100644 (file)
@@ -7770,14 +7770,51 @@ __webpack_require__.r(__webpack_exports__);
 //
 //
 //
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
 /* harmony default export */ __webpack_exports__["default"] = ({
   name: "PollAnswers",
   data: function data() {
     return {
       polls: [],
       selected_poll: null,
-      answers: null,
-      loaded: false
+      poll_data: null,
+      loaded: false,
+      selected_question: null
     };
   },
   watch: {
@@ -7785,7 +7822,7 @@ __webpack_require__.r(__webpack_exports__);
       var _this = this;
 
       axios.get('/otherContent/pollAnswers/' + value.id).then(function (d) {
-        return _this.answers = d.data;
+        return _this.poll_data = d.data;
       });
     }
   },
@@ -80021,7 +80058,7 @@ var render = function() {
       ? _c(
           "div",
           [
-            _c("div", { staticClass: "form-group" }, [
+            _c("div", { staticClass: "form-group mb-4" }, [
               _c("label", { attrs: { for: "polls" } }),
               _vm._v(" "),
               _c(
@@ -80064,26 +80101,117 @@ var render = function() {
             _vm._v(" "),
             !!_vm.selected_poll
               ? [
-                  _vm.answers === null
+                  _vm.poll_data === null
                     ? _c("div", { staticClass: "alert alert-info" }, [
                         _vm._v(
-                          "\n                Chargement en cours...\n            "
+                          "\n                    Chargement en cours...\n                "
                         )
                       ])
-                    : !!_vm.answers && _vm.answers.length === 0
+                    : !!_vm.poll_data && _vm.poll_data.entries_count === 0
                     ? _c("div", { staticClass: "alert alert-warning" }, [
                         _vm._v(
-                          "\n                Aucune réponse pour le moment !\n            "
+                          "\n                    Aucune réponse pour le moment !\n                "
                         )
                       ])
-                    : _c("div", [
-                        _c("pre", [
+                    : _c("div", { staticClass: "box p-3" }, [
+                        _c("p", { staticClass: "lead" }, [
                           _vm._v(
-                            "                    " +
-                              _vm._s(_vm.answers) +
-                              "\n                "
+                            _vm._s(_vm.poll_data.entries_count) +
+                              " réponses enregistrées"
                           )
-                        ])
+                        ]),
+                        _vm._v(" "),
+                        _c("div", { staticClass: "form-group mb-2" }, [
+                          _c("label", { attrs: { for: "question" } }, [
+                            _vm._v(
+                              "Sélectionnez une question pour voir les résultats"
+                            )
+                          ]),
+                          _vm._v(" "),
+                          _c(
+                            "select",
+                            {
+                              directives: [
+                                {
+                                  name: "model",
+                                  rawName: "v-model",
+                                  value: _vm.selected_question,
+                                  expression: "selected_question"
+                                }
+                              ],
+                              staticClass: "form-control",
+                              attrs: { name: "question", id: "question" },
+                              on: {
+                                change: function($event) {
+                                  var $$selectedVal = Array.prototype.filter
+                                    .call($event.target.options, function(o) {
+                                      return o.selected
+                                    })
+                                    .map(function(o) {
+                                      var val =
+                                        "_value" in o ? o._value : o.value
+                                      return val
+                                    })
+                                  _vm.selected_question = $event.target.multiple
+                                    ? $$selectedVal
+                                    : $$selectedVal[0]
+                                }
+                              }
+                            },
+                            _vm._l(_vm.poll_data.questions, function(question) {
+                              return _c(
+                                "option",
+                                { domProps: { value: question } },
+                                [_vm._v(_vm._s(question.question))]
+                              )
+                            }),
+                            0
+                          )
+                        ]),
+                        _vm._v(" "),
+                        _vm.selected_question === null
+                          ? _c("div", { staticClass: "alert alert-info" }, [
+                              _vm._v(
+                                "\n                        Sélectionnez une question pour voir les réponses\n                    "
+                              )
+                            ])
+                          : _c("div", [
+                              _c(
+                                "table",
+                                {
+                                  staticClass:
+                                    "table table-bordered table-hover"
+                                },
+                                [
+                                  _vm._m(0),
+                                  _vm._v(" "),
+                                  _c(
+                                    "tbody",
+                                    _vm._l(
+                                      _vm.selected_question.answers,
+                                      function(answer) {
+                                        return _c("tr", [
+                                          _c("td", [
+                                            _vm._v(_vm._s(answer.value))
+                                          ]),
+                                          _vm._v(" "),
+                                          _c("td", [
+                                            _vm._v(_vm._s(answer.count))
+                                          ]),
+                                          _vm._v(" "),
+                                          _c("td", [
+                                            _vm._v(
+                                              _vm._s(answer.percentage) + "%"
+                                            )
+                                          ])
+                                        ])
+                                      }
+                                    ),
+                                    0
+                                  )
+                                ]
+                              )
+                            ])
                       ])
                 ]
               : _vm._e()
@@ -80092,14 +80220,29 @@ var render = function() {
         )
       : _vm.loaded
       ? _c("div", { staticClass: "alert alert-warning" }, [
-          _vm._v("\n        Aucun sondage trouvé\n    ")
+          _vm._v("\n            Aucun sondage trouvé\n        ")
         ])
       : _c("div", { staticClass: "alert alert-info" }, [
-          _vm._v("\n        Chargement en cours...\n    ")
+          _vm._v("\n            Chargement en cours...\n        ")
         ])
   ])
 }
-var staticRenderFns = []
+var staticRenderFns = [
+  function() {
+    var _vm = this
+    var _h = _vm.$createElement
+    var _c = _vm._self._c || _h
+    return _c("thead", [
+      _c("tr", [
+        _c("th", [_vm._v("Réponse")]),
+        _vm._v(" "),
+        _c("th", [_vm._v("Occurences")]),
+        _vm._v(" "),
+        _c("th", [_vm._v("Pourcentage")])
+      ])
+    ])
+  }
+]
 render._withStripped = true
 
 
index fba46975f15619125c48129989e1d081b1affad5..966de0040deb273e9f71527e82c56e558cc4e10b 100644 (file)
@@ -1,7 +1,7 @@
 <template>
     <div>
         <div v-if="polls.length > 0">
-            <div class="form-group">
+            <div class="form-group mb-4">
                 <label for="polls"></label>
                 <select name="polls" id="polls" class="form-control" v-model="selected_poll">
                     <option v-for="poll in polls" :value="poll">{{poll.title}}</option>
@@ -9,18 +9,54 @@
             </div>
 
             <template v-if="!! selected_poll">
-                <div v-if="answers === null" class="alert alert-info">
+                <div v-if="poll_data === null" class="alert alert-info">
                     Chargement en cours...
                 </div>
 
-                <div v-else-if="!!answers && answers.length === 0" class="alert alert-warning">
+                <div v-else-if="!! poll_data && poll_data.entries_count === 0" class="alert alert-warning">
                     Aucune réponse pour le moment !
                 </div>
 
-                <div v-else>
-                    <pre>
-                        {{answers}}
-                    </pre>
+                <div class="box p-3" v-else>
+<!--                    <h2>{{ selected_poll.title }}</h2>-->
+                    <p class="lead">{{poll_data.entries_count}} réponses enregistrées</p>
+
+                    <div class="form-group mb-2">
+                        <label for="question">Sélectionnez une question pour voir les résultats</label>
+
+                        <select name="question" class="form-control" id="question" v-model="selected_question">
+                            <option v-for="question in poll_data.questions" :value="question">{{question.question}}</option>
+                        </select>
+                    </div>
+
+                    <div v-if="selected_question === null" class="alert alert-info">
+                        Sélectionnez une question pour voir les réponses
+                    </div>
+
+                    <div v-else>
+                        <table class="table table-bordered table-hover">
+                            <thead>
+                                <tr>
+                                    <th>Réponse</th>
+                                    <th>Occurences</th>
+                                    <th>Pourcentage</th>
+                                </tr>
+                            </thead>
+                            <tbody>
+                                <tr v-for="answer in selected_question.answers">
+                                    <td>{{answer.value}}</td>
+                                    <td>{{answer.count}}</td>
+                                    <td>{{answer.percentage}}%</td>
+                                </tr>
+                            </tbody>
+                        </table>
+                    </div>
+
+
+
+
+
+
                 </div>
 
             </template>
             return {
                 polls: [],
                 selected_poll: null,
-                answers: null,
+                poll_data: null,
                 loaded: false,
+                selected_question: null,
 
             }
         },
         watch: {
             selected_poll: function(value) {
-                axios.get('/otherContent/pollAnswers/' + value.id).then(d => this.answers = d.data)
+                axios.get('/otherContent/pollAnswers/' + value.id).then(d => this.poll_data = d.data)
             }
         },
 
index 72502058083fd4d40c02eeab285058fe98bb7f35..c56d20789fcda38af2aef4d9ad82c9c2d2be7136 100644 (file)
@@ -13,7 +13,7 @@ $slug = Str::slug($question = $block->input('question'))
     @endphp
         @if($block->input('type') === 'radio')
             <div class="form-check">
-                <input class="form-check-input" type="radio" name="{{$slug}}" id="answer-{{$i}}-{{$slug}}" value="{{Str::slug($option->input('option'))}}">
+                <input class="form-check-input" type="radio" name="{{$slug}}" id="answer-{{$i}}-{{$slug}}" value="{{$option->input('option')}}">
                 <label class="form-check-label" for="answer-{{$i}}-{{$slug}}">
                     {{$option->input('option')}}
                 </label>
@@ -21,7 +21,7 @@ $slug = Str::slug($question = $block->input('question'))
 
         @else
             <div class="form-check">
-                <input class="form-check-input" type="checkbox" value="{{Str::slug($option->input('option'))}}"  name="{{$slug}}[]" id="answer-{{$i}}-{{$slug}}">
+                <input class="form-check-input" type="checkbox" value="{{$option->input('option')}}"  name="{{$slug}}[]" id="answer-{{$i}}-{{$slug}}">
                 <label class="form-check-label" for="answer-{{$i}}-{{$slug}}">
                     {{$option->input('option')}}
                 </label>