import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/survey.dart'; import 'package:comunic/models/survey_choice.dart'; import 'package:meta/meta.dart'; /// Survey helper /// /// @author Pierre HUBERT class SurveyHelper { /// Cancel the response of a user to a survey Future cancelResponse(Survey survey) async { return (await APIRequest( uri: "surveys/cancel_response", needLogin: true, args: {"postID": survey.postID.toString()}).exec()) .isOK; } /// Send the response of a user to a survey Future respondToSurvey( {@required Survey survey, @required SurveyChoice choice}) async { assert(survey != null); assert(choice != null); return (await APIRequest( uri: "surveys/send_response", needLogin: true, args: { "postID": survey.postID.toString(), "choiceID": choice.id.toString(), }, ).exec()) .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 Set choices = Set(); map["choices"].forEach((k, e) => choices.add(SurveyChoice( id: e["choiceID"], name: e["name"], responses: e["responses"]))); return Survey( id: map["ID"], userID: map["userID"], postID: map["postID"], creationTime: map["creation_time"], question: map["question"], userChoice: map["user_choice"], choices: choices, allowNewChoicesCreation: map["allowNewChoices"], ); } }