2019-06-28 09:32:36 +00:00
|
|
|
import 'package:comunic/models/survey_choice.dart';
|
2020-05-18 16:19:07 +00:00
|
|
|
import 'package:comunic/utils/account_utils.dart' as account;
|
2019-06-28 09:32:36 +00:00
|
|
|
|
|
|
|
/// Survey information
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
class Survey {
|
|
|
|
final int id;
|
|
|
|
final int userID;
|
|
|
|
final int postID;
|
|
|
|
final int creationTime;
|
|
|
|
final String question;
|
|
|
|
int userChoice;
|
|
|
|
final Set<SurveyChoice> choices;
|
2020-05-18 16:19:07 +00:00
|
|
|
bool allowNewChoicesCreation;
|
2019-06-28 09:32:36 +00:00
|
|
|
|
|
|
|
Survey({
|
2022-03-10 18:39:57 +00:00
|
|
|
required this.id,
|
|
|
|
required this.userID,
|
|
|
|
required this.postID,
|
|
|
|
required this.creationTime,
|
|
|
|
required this.question,
|
|
|
|
required this.userChoice,
|
|
|
|
required this.choices,
|
|
|
|
required this.allowNewChoicesCreation,
|
2022-03-11 15:27:01 +00:00
|
|
|
}) : assert(choices.length > 0);
|
|
|
|
|
|
|
|
bool get hasResponded => this.userChoice > 0;
|
2019-06-28 09:32:36 +00:00
|
|
|
|
2020-04-25 15:28:13 +00:00
|
|
|
bool get hasResponses =>
|
|
|
|
this.choices.where((f) => f.responses > 0).length > 0;
|
|
|
|
|
2020-05-18 16:19:07 +00:00
|
|
|
bool get canBlockNewChoicesCreation =>
|
|
|
|
allowNewChoicesCreation && account.userID() == this.userID;
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
SurveyChoice? get userResponse {
|
2019-06-28 09:32:36 +00:00
|
|
|
if (!hasResponded) return null;
|
|
|
|
|
|
|
|
return choices.firstWhere((e) => e.id == userChoice);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cancelUserResponse() {
|
2022-03-10 18:39:57 +00:00
|
|
|
if (hasResponded) userResponse!.responses--;
|
2019-06-28 09:32:36 +00:00
|
|
|
userChoice = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setUserResponse(SurveyChoice choice) {
|
|
|
|
cancelUserResponse();
|
|
|
|
userChoice = choice.id;
|
|
|
|
choice.responses++;
|
|
|
|
}
|
|
|
|
}
|