From acc81acdeab95cbfc47adad457bd5aea80bf7b82 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 18 May 2020 18:19:07 +0200 Subject: [PATCH] Can block the creation of new responses on a survey --- lib/helpers/survey_helper.dart | 7 +++++++ lib/models/survey.dart | 9 ++++++++- lib/ui/widgets/survey_widget.dart | 31 ++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/helpers/survey_helper.dart b/lib/helpers/survey_helper.dart index c225edc..55aecc3 100644 --- a/lib/helpers/survey_helper.dart +++ b/lib/helpers/survey_helper.dart @@ -34,6 +34,12 @@ class SurveyHelper { .isOK; } + /// Prevent new choices from being created on a survey + static Future blockNewChoicesCreation(int postID) async => + await APIRequest.withLogin("surveys/block_new_choices_creation") + .addInt("postID", postID) + .execWithThrow(); + /// Turn an API entry into a [Survey] object static Survey apiToSurvey(Map map) { // Parse survey responses @@ -50,6 +56,7 @@ class SurveyHelper { question: map["question"], userChoice: map["user_choice"], choices: choices, + allowNewChoicesCreation: map["allowNewChoices"], ); } } diff --git a/lib/models/survey.dart b/lib/models/survey.dart index 3bb7b32..9aa657d 100644 --- a/lib/models/survey.dart +++ b/lib/models/survey.dart @@ -1,4 +1,5 @@ import 'package:comunic/models/survey_choice.dart'; +import 'package:comunic/utils/account_utils.dart' as account; import 'package:meta/meta.dart'; /// Survey information @@ -13,6 +14,7 @@ class Survey { final String question; int userChoice; final Set choices; + bool allowNewChoicesCreation; Survey({ @required this.id, @@ -22,6 +24,7 @@ class Survey { @required this.question, @required this.userChoice, @required this.choices, + @required this.allowNewChoicesCreation, }) : assert(id != null), assert(userID != null), assert(postID != null), @@ -29,13 +32,17 @@ class Survey { assert(question != null), assert(userChoice != null), assert(choices != null), - assert(choices.length > 0); + assert(choices.length > 0), + assert(allowNewChoicesCreation != null); bool get hasResponded => this.userChoice != null && this.userChoice > 0; bool get hasResponses => this.choices.where((f) => f.responses > 0).length > 0; + bool get canBlockNewChoicesCreation => + allowNewChoicesCreation && account.userID() == this.userID; + SurveyChoice get userResponse { if (!hasResponded) return null; diff --git a/lib/ui/widgets/survey_widget.dart b/lib/ui/widgets/survey_widget.dart index 71bafa0..544a4f0 100644 --- a/lib/ui/widgets/survey_widget.dart +++ b/lib/ui/widgets/survey_widget.dart @@ -1,6 +1,7 @@ import 'package:comunic/helpers/survey_helper.dart'; import 'package:comunic/models/survey.dart'; import 'package:comunic/models/survey_choice.dart'; +import 'package:comunic/ui/widgets/safe_state.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; @@ -21,7 +22,7 @@ class SurveyWidget extends StatefulWidget { _SurveyWidgetState createState() => _SurveyWidgetState(); } -class _SurveyWidgetState extends State { +class _SurveyWidgetState extends SafeState { final SurveyHelper _helper = SurveyHelper(); Survey get survey => widget.survey; @@ -42,6 +43,12 @@ class _SurveyWidgetState extends State { style: TextStyle(fontWeight: FontWeight.bold), ), _buildUserResponse(), + + // Offer to block the creation of new responses, if possible + survey.allowNewChoicesCreation + ? _buildBlockNewResponsesLink() + : Container(), + survey.hasResponses ? _buildChart() : _buildNoResponseNotice(), ], ); @@ -109,6 +116,11 @@ class _SurveyWidgetState extends State { ); } + Widget _buildBlockNewResponsesLink() => InkWell( + onTap: _blockNewChoices, + child: Text(tr("Block the creation of new responses")), + ); + Widget _buildChart() { return PieChart( dataMap: _buildDataMap(), @@ -133,4 +145,21 @@ class _SurveyWidgetState extends State { survey.setUserResponse(choice); }); } + + /// Block the creation of new choices on this survey + void _blockNewChoices() async { + try { + if (!await showConfirmDialog( + context: context, + message: tr("Do you really want to block new responses ?"))) return; + + await SurveyHelper.blockNewChoicesCreation(survey.postID); + + setState(() => survey.allowNewChoicesCreation = false); + } catch (e, s) { + print("Could not block the creation of new choices! $e\n$s"); + showSimpleSnack( + context, tr("Could not block the creation of new choices!")); + } + } }