Skip to content

Commit

Permalink
Allow the data groups survey questions to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
syoung-smallwisdom committed Jan 26, 2016
1 parent 3653ca2 commit 00d3ca9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
1 change: 0 additions & 1 deletion APCAppCore/APCAppCore/UI/Model/APCDataGroupsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ extern NSString * const APCDataGroupsStepIdentifier;
- (void)setSurveyAnswerWithStepResult:(ORKStepResult *)result;
- (void)setSurveyAnswerWithIdentifier:(NSString*)identifier selectedIndices:(NSArray*)selectedIndices;


@end

NS_ASSUME_NONNULL_END
16 changes: 13 additions & 3 deletions APCAppCore/APCAppCore/UI/Model/APCDataGroupsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
NSString * const APCDataGroupsMappingSurveyDetailKey = @"detail";
NSString * const APCDataGroupsMappingSurveyQuestionIdentifierKey = @"identifier";
NSString * const APCDataGroupsMappingSurveyQuestionTypeKey = @"type";
NSString * const APCDataGroupsMappingSurveyQuestionPromptKey = @"prompt";
NSString * const APCDataGroupsMappingSurveyQuestionProfileCaptionKey = @"profileCaption";
NSString * const APCDataGroupsMappingSurveyQuestionPromptKey = @"text";
NSString * const APCDataGroupsMappingSurveyQuestionProfileCaptionKey = @"shortText";
NSString * const APCDataGroupsMappingSurveyQuestionOptionalKey = @"optional";
NSString * const APCDataGroupsMappingSurveyQuestionValueMapKey = @"valueMap";
NSString * const APCDataGroupsMappingSurveyQuestionTypeBoolean = @"boolean";
NSString * const APCDataGroupsMappingSurveyQuestionValueMapValueKey = @"value";
Expand Down Expand Up @@ -139,9 +140,18 @@ - (ORKFormStep *)surveyStep {
// Add the questions from the mapping
NSMutableArray *formItems = [NSMutableArray new];
for (NSDictionary *question in questions) {

// Get the default choices and add the skip choice if this question is optional
NSArray *textChoices = [self choicesForQuestion:question];
if ([question[APCDataGroupsMappingSurveyQuestionOptionalKey] boolValue]) {
ORKTextChoice *skipChoice = [ORKTextChoice choiceWithText:NSLocalizedStringWithDefaultValue(@"APC_SKIP_CHOICE", @"APCAppCore", APCBundle(), @"Prefer not to answer", @"Choice text for skipping a question") value:@(NSNotFound)];
textChoices = [textChoices arrayByAddingObject:skipChoice];
}

ORKAnswerFormat *format = [ORKTextChoiceAnswerFormat
choiceAnswerFormatWithStyle:ORKChoiceAnswerStyleSingleChoice
textChoices:[self choicesForQuestion:question]];
textChoices:textChoices];

NSString *text = !useQuestionPrompt ? question[APCDataGroupsMappingSurveyQuestionPromptKey] : nil;
ORKFormItem *item = [[ORKFormItem alloc] initWithIdentifier:question[APCDataGroupsMappingSurveyQuestionIdentifierKey]
text:text
Expand Down
60 changes: 55 additions & 5 deletions APCAppCore/APCAppCoreTests/APCDataGroupsManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ - (void)testSetSurveyAnswer_NoChange
XCTAssertFalse(manager.hasChanges);
}

- (void)testSurveySteps
- (void)testSurveySteps_NotOptional
{
APCDataGroupsManager * manager = [self createDataGroupsManagerWithDataGroups:nil];

Expand All @@ -167,7 +167,7 @@ - (void)testSurveySteps
XCTAssertNotNil(step);
XCTAssertFalse(step.optional);
XCTAssertEqualObjects(step.title, @"Are you in Control?");
XCTAssertEqualObjects(step.text, @"Engineers and scientists like classifications. To help us better classify you, please answer these required questions.");
XCTAssertEqualObjects(step.text, @"Engineers and scientists like classifications. To help us better classify you, please answer this question.");

XCTAssertEqual(step.formItems.count, 1);
ORKFormItem *item = step.formItems.firstObject;
Expand All @@ -184,6 +184,33 @@ - (void)testSurveySteps
XCTAssertEqualObjects(answerFormat.textChoices, expectedChoices);
}

- (void)testSurveySteps_Optional
{
APCDataGroupsManager * manager = [self createDataGroupsManagerWithDataGroups:nil required:NO questionOptional:YES];

ORKFormStep * step = [manager surveyStep];

XCTAssertNotNil(step);
XCTAssertFalse(step.optional);
XCTAssertEqualObjects(step.title, @"Are you in Control?");
XCTAssertEqualObjects(step.text, @"Engineers and scientists like classifications. To help us better classify you, please answer this question.");

XCTAssertEqual(step.formItems.count, 1);
ORKFormItem *item = step.formItems.firstObject;
XCTAssertEqualObjects(item.identifier, @"control_question");
XCTAssertEqualObjects(item.text, @"Have you ever been diagnosed with XYZ?");

ORKTextChoiceAnswerFormat *answerFormat = (ORKTextChoiceAnswerFormat *)item.answerFormat;
XCTAssertTrue([answerFormat isKindOfClass:[ORKTextChoiceAnswerFormat class]]);
XCTAssertEqual(answerFormat.style, ORKChoiceAnswerStyleSingleChoice);

NSArray *expectedChoices = @[[ORKTextChoice choiceWithText:@"Yes" value:@YES],
[ORKTextChoice choiceWithText:@"No" value:@NO],
[ORKTextChoice choiceWithText:@"Prefer not to answer" value:@(NSNotFound)]];
XCTAssertEqual(answerFormat.textChoices.count, expectedChoices.count);
XCTAssertEqualObjects(answerFormat.textChoices, expectedChoices);
}

- (void)testSetSurveyAnswerWithStepResult_StudyA
{
APCDataGroupsManager * manager = [self createDataGroupsManagerWithDataGroups:nil];
Expand Down Expand Up @@ -220,9 +247,31 @@ - (void)testSetSurveyAnswerWithStepResult_Control
XCTAssertEqualObjects(actualGroup, @"control");
}

- (void)testSetSurveyAnswerWithStepResult_Skipped
{
APCDataGroupsManager * manager = [self createDataGroupsManagerWithDataGroups:nil required:NO questionOptional:YES];

// Change the survey answer
ORKChoiceQuestionResult *result = [[ORKChoiceQuestionResult alloc] initWithIdentifier:@"control_question"];
result.choiceAnswers = @[@(NSNotFound)];
ORKStepResult *stepResult = [[ORKStepResult alloc] initWithStepIdentifier:APCDataGroupsStepIdentifier results:@[result]];

[manager setSurveyAnswerWithStepResult:stepResult];

// For a skipped question, the data groups are *not* set to anything
XCTAssertEqual(manager.dataGroups.count, 0);
XCTAssertFalse(manager.hasChanges);

}

#pragma mark - heper methods

- (APCDataGroupsManager*)createDataGroupsManagerWithDataGroups:(NSArray*)dataGroups {
return [self createDataGroupsManagerWithDataGroups:dataGroups required:YES questionOptional:NO];
}

- (APCDataGroupsManager*)createDataGroupsManagerWithDataGroups:(NSArray*)dataGroups required:(BOOL)required questionOptional:(BOOL)questionOptional {


NSDictionary *mapping = @{
@"items": @[@{ @"group_name" : @"control",
Expand All @@ -235,15 +284,16 @@ - (APCDataGroupsManager*)createDataGroupsManagerWithDataGroups:(NSArray*)dataGro
@"is_control_group" : @(false),
}
],
@"required": @(true),
@"required": @(required),
@"title": @"Are you in Control?",
@"detail": @"Engineers and scientists like classifications. To help us better classify you, please answer these required questions.",
@"detail": @"Engineers and scientists like classifications. To help us better classify you, please answer this question.",
@"questions":
@[
@{
@"identifier": @"control_question",
@"prompt": @"Have you ever been diagnosed with XYZ?",
@"text": @"Have you ever been diagnosed with XYZ?",
@"type": @"boolean",
@"optional": @(questionOptional),
@"valueMap": @[@{ @"value" : @YES,
@"groups" : @[@"studyA"]},
@{ @"value" : @NO,
Expand Down

0 comments on commit 00d3ca9

Please sign in to comment.