use A17\Twill\Http\Controllers\Admin\ModuleController;
use App\Models\Poll;
+use Illuminate\Support\Arr;
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,
]);
}
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
'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
}
+ /**
+ * @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,
//
//
//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
/* 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: {
var _this = this;
axios.get('/otherContent/pollAnswers/' + value.id).then(function (d) {
- return _this.answers = d.data;
+ return _this.poll_data = d.data;
});
}
},
? _c(
"div",
[
- _c("div", { staticClass: "form-group" }, [
+ _c("div", { staticClass: "form-group mb-4" }, [
_c("label", { attrs: { for: "polls" } }),
_vm._v(" "),
_c(
_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()
)
: _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
//
//
//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
+//
/* 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: {
var _this = this;
axios.get('/otherContent/pollAnswers/' + value.id).then(function (d) {
- return _this.answers = d.data;
+ return _this.poll_data = d.data;
});
}
},
? _c(
"div",
[
- _c("div", { staticClass: "form-group" }, [
+ _c("div", { staticClass: "form-group mb-4" }, [
_c("label", { attrs: { for: "polls" } }),
_vm._v(" "),
_c(
_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()
)
: _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
<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>
</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)
}
},
@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>
@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>