Skip to content

Commit 5ecb8e3

Browse files
committed
Merge pull request #13 from BlockScore/fix/query_parm_qs
Correct question set results scoring per #8
2 parents 0ee55d7 + 86ce6a9 commit 5ecb8e3

File tree

3 files changed

+101
-14
lines changed

3 files changed

+101
-14
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.1
1+
4.0.2

lib/ApiResource.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ protected function _score($answers)
181181
$url = static::instanceUrl() . '/score';
182182

183183
// Weird request requires us to build the cURL request manually
184-
$params = '';
184+
$params = array();
185185
foreach ($answers as $key => $value) {
186-
$params = $params . 'answers[][{$key}]={$value}&';
186+
$params[] = "answers[][question_id]={$value['question_id']}";
187+
$params[] = "answers[][answer_id]={$value['answer_id']}";
187188
}
188-
rtrim($params, '&');
189189

190-
$response = static::_makeRequest('post', $url, $params);
190+
$response = static::_makeRequest('post', $url, implode('&', $params));
191191
return Util\Util::convertToBlockScoreObject($response);
192192
}
193193
}

tests/QuestionSetTest.php

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55
class QuestionSetTest extends TestCase
66
{
7+
private static $qs_answers = array(
8+
'Which one of the following addresses is associated with you?' => '309 Colver Rd',
9+
'Which one of the following area codes is associated with you?' => '812',
10+
'Which one of the following counties is associated with you?' => 'Jasper',
11+
'Which one of the following zip codes is associated with you?' => '49230',
12+
'What state was your SSN issued in?' => 'None Of The Above',
13+
'Which one of the following adult individuals is most closely associated with you?' => 'None Of The Above',
14+
);
15+
716
public function testUrl()
817
{
918
$this->assertSame(QuestionSet::classUrl(), '/question_sets');
@@ -16,7 +25,7 @@ public function testClassType()
1625
$this->assertTrue($qs instanceof QuestionSet);
1726
$this->assertTrue($qs instanceof Object);
1827
}
19-
28+
2029
public function testCreateQuestionSet()
2130
{
2231
$person = self::createTestPerson();
@@ -25,7 +34,7 @@ public function testCreateQuestionSet()
2534
$this->assertSame('None Of The Above',$i->answers[4]->answer);
2635
}
2736
}
28-
37+
2938
public function testAllQuestionSet()
3039
{
3140
$person = self::createTestPerson();
@@ -35,7 +44,7 @@ public function testAllQuestionSet()
3544
$all_qs = $person->question_sets->all();
3645
$this->assertSame($qs->id, $all_qs[0]->id);
3746
}
38-
47+
3948
public function testRetrieveQuestionSet()
4049
{
4150
$person = self::createTestPerson();
@@ -45,21 +54,99 @@ public function testRetrieveQuestionSet()
4554
$this->assertSame($retrieved_qs->$key, $value);
4655
}
4756
}
48-
57+
4958
public function testScoringQuestionSet()
5059
{
5160
$person = self::createTestPerson();
5261
$qs = $person->question_sets->create();
62+
63+
// Compute the correct answer to the first question
64+
$first_question = $qs->questions[0];
65+
$first_question_answer = self::$qs_answers[$first_question->question];
66+
$first_question_answer_id = 0;
67+
68+
foreach ($first_question->answers as $answer) {
69+
if ($answer->answer == $first_question_answer) {
70+
$first_question_answer_id = $answer->id;
71+
}
72+
}
73+
5374
$answers = array(
54-
array('question_id' => 1, 'answer_id' => 2),
55-
array('question_id' => 2, 'answer_id' => 2),
56-
array('question_id' => 3, 'answer_id' => 2),
57-
array('question_id' => 4, 'answer_id' => 2),
58-
array('question_id' => 5, 'answer_id' => 2),
75+
array('question_id' => 1, 'answer_id' => $first_question_answer_id),
76+
array('question_id' => 2, 'answer_id' => 6),
77+
array('question_id' => 3, 'answer_id' => 6),
78+
array('question_id' => 4, 'answer_id' => 6),
79+
array('question_id' => 5, 'answer_id' => 6),
5980
);
81+
6082
$score = $qs->score($answers);
83+
84+
// Test whether questions returned are the same
6185
foreach ($qs as $key => $value) {
6286
$this->assertSame($score->questions->$key, $value);
6387
}
88+
89+
// Test whether we scored a 20%!
90+
$this->assertSame($score->score, 20.0);
91+
}
92+
93+
public function testScoringQuestionSet2()
94+
{
95+
$person = self::createTestPerson();
96+
$qs = $person->question_sets->create();
97+
98+
// Compute the correct answer to all the questions!
99+
$answer_ids = array();
100+
101+
foreach ($qs->questions as $question) {
102+
$question_answer = self::$qs_answers[$question->question];
103+
foreach ($question->answers as $answer) {
104+
if ($answer->answer == $question_answer) {
105+
array_push($answer_ids, $answer->id);
106+
}
107+
}
108+
}
109+
110+
$answers = array(
111+
array('question_id' => 1, 'answer_id' => $answer_ids[0]),
112+
array('question_id' => 2, 'answer_id' => $answer_ids[1]),
113+
array('question_id' => 3, 'answer_id' => $answer_ids[2]),
114+
array('question_id' => 4, 'answer_id' => $answer_ids[3]),
115+
array('question_id' => 5, 'answer_id' => $answer_ids[4]),
116+
);
117+
118+
$score = $qs->score($answers);
119+
120+
// Test whether questions returned are the same
121+
foreach ($qs as $key => $value) {
122+
$this->assertSame($score->questions->$key, $value);
123+
}
124+
125+
// Test whether we scored a 100%!
126+
$this->assertSame($score->score, 100.0);
127+
}
128+
129+
public function testScoringQuestionSet3()
130+
{
131+
$person = self::createTestPerson();
132+
$qs = $person->question_sets->create();
133+
134+
$answers = array(
135+
array('question_id' => 1, 'answer_id' => 6),
136+
array('question_id' => 2, 'answer_id' => 6),
137+
array('question_id' => 3, 'answer_id' => 6),
138+
array('question_id' => 4, 'answer_id' => 6),
139+
array('question_id' => 5, 'answer_id' => 6),
140+
);
141+
142+
$score = $qs->score($answers);
143+
144+
// Test whether questions returned are the same
145+
foreach ($qs as $key => $value) {
146+
$this->assertSame($score->questions->$key, $value);
147+
}
148+
149+
// Test whether we scored a 0%!
150+
$this->assertSame($score->score, 0.0);
64151
}
65152
}

0 commit comments

Comments
 (0)