Repository: learningequality/kolibri

Issue:
Showing 120% score in exam report
### Observed behavior
After submitting exam, when coach user watching progress of each user in exam report. Coach user see 120% score in exam report. We have attached screenshot and database file,so you can easily re-generate this issue.

### Expected behavior
Score must be between 0-100%.

### Steps to reproduce
1. Copy attached database file in .kolibri folder.
2. login with username "pm" and password "sc".
3. Click on Coach.
4. Click on Class 4A.
5. Click on Exams.
6. See report of the Unit 2B-Final exam.
7. See learner Junaid Shaikh.

### Context
 * Kolibri version : Kolibri 0.4.9
 * Operating system : Ubuntu 14.04
 * Browser : Chrome 

### Screenshots
![1](https://user-images.githubusercontent.com/12776071/34758551-fc3ec802-f5fc-11e7-8055-f09bf143dc7d.png)

### Database
[db.sqlite3.zip](https://github.com/learningequality/kolibri/files/1617728/db.sqlite3.zip)





Discussion:
@swapnil106111 Would you mind posting a screenshot of Junaid's individual exam report (by clicking the link on Junaid's name).

Thanks!
This looks like this is where the percentage is calculated:

https://github.com/learningequality/kolibri/blob/c1c36677438cfc177d8ade3883dce67694a9b928/kolibri/plugins/coach/assets/src/views/exam-report-page/index.vue#L54

```
{{ $tr('scorePercentage', { num: examTaker.score / exam.question_count }) }}
```

This should work as expected if `examTaker.score` is less than or equal to `exam.question_count`.

I assume that `exam.question_count` is pretty straightforward, so it might be an issue with how the exam taker score is aggregated from the logs.
> it might be an issue with how the exam taker score is aggregated from the logs

Or possibly how the logs are initially recorded. I believe that's been a source of bugs in the past.

@jonboiser  attached Junaid Shaikh's exam report shows 73% score after clicking on Junaid Shaikh name.
![screen shot 2018-01-11 at 12 16 15 pm](https://user-images.githubusercontent.com/12776071/34812014-d5b6aa92-f6c9-11e7-88d7-244d6f108e23.png)

![screen shot 2018-01-11 at 12 18 45 pm](https://user-images.githubusercontent.com/12776071/34812016-d61a53d0-f6c9-11e7-9c7e-29744e93960b.png)

@swapnil106111 Thanks for the screenshot. So, has Junaid's score changed recently? Or is this showing another potential issue where the scores are not consistent (120% on the whole-class Exam report, but 73% on Junaid's individual report)?

Knowing that this is a 15-question Exam is helpful too, since it implies that Junaid's "score" on the whole-class report was 18, since 18/15 = 1.2. In other words, it is possible for the exam taker score to exceed the length of the exam.
Okay, I think it does have something to do with how the Exam attempts are logged (and then aggregated). Here is the code:

https://github.com/learningequality/kolibri/blob/5c981bd7cc4aff05dad02eacb6fb400ff0d59d12/kolibri/logger/serializers.py#L25-L26

This student has 23 attempts logged for this Exam, 18 of which are "correct" (hence the 120%). But this also counts multiple correct attempts for the same questions. In other words, two correct attempts for the same question is seen, in the code, as the same as two correct attempts for two different questions.

The underlying assumption being broken in here is that a question can have at most 1 correct attempt, which is not the case based on the screenshots above (Question 1 has two green checkmarks).
An examattemptlog can have multiple attempts in it though, the subsequent attempts *should* all be encoded on the same attempt log, and the question view's attempt history is only based on one attempt log (that doesn't mean that there might not still be superfluous examattemptlogs that are breaking this assumption, but the screenshot above is not evidence of this).

Would need to double check in the database itself what this query evaluates to.
@jonboiser It is showing 120% score before clicking on Junaid's name. But after clicking on Junaid's name, it is showing actual result (73% score) of Junaid Shaikh.

Resolving patch:
diff --git a/kolibri/logger/serializers.py b/kolibri/logger/serializers.py
--- a/kolibri/logger/serializers.py
+++ b/kolibri/logger/serializers.py
@@ -20,10 +20,10 @@ class ExamLogSerializer(KolibriModelSerializer):
     score = serializers.SerializerMethodField()
 
     def get_progress(self, obj):
-        return obj.attemptlogs.count()
+        return obj.exam.question_count
 
     def get_score(self, obj):
-        return obj.attemptlogs.aggregate(Sum('correct')).get('correct__sum')
+        return obj.attemptlogs.values_list('item').order_by('completion_timestamp').distinct().aggregate(Sum('correct')).get('correct__sum')
 
     class Meta:
         model = ExamLog


Test patch: