mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 12:59:21 +00:00
Can block the creation of new responses on a survey
This commit is contained in:
parent
b4465cc70c
commit
acc81acdea
@ -34,6 +34,12 @@ class SurveyHelper {
|
||||
.isOK;
|
||||
}
|
||||
|
||||
/// Prevent new choices from being created on a survey
|
||||
static Future<void> 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<String, dynamic> map) {
|
||||
// Parse survey responses
|
||||
@ -50,6 +56,7 @@ class SurveyHelper {
|
||||
question: map["question"],
|
||||
userChoice: map["user_choice"],
|
||||
choices: choices,
|
||||
allowNewChoicesCreation: map["allowNewChoices"],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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<SurveyChoice> 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;
|
||||
|
||||
|
@ -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<SurveyWidget> {
|
||||
class _SurveyWidgetState extends SafeState<SurveyWidget> {
|
||||
final SurveyHelper _helper = SurveyHelper();
|
||||
|
||||
Survey get survey => widget.survey;
|
||||
@ -42,6 +43,12 @@ class _SurveyWidgetState extends State<SurveyWidget> {
|
||||
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<SurveyWidget> {
|
||||
);
|
||||
}
|
||||
|
||||
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<SurveyWidget> {
|
||||
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!"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user