diff --git a/rapids/models.py b/rapids/models.py index 02755dbf..18beb338 100644 --- a/rapids/models.py +++ b/rapids/models.py @@ -485,10 +485,11 @@ class Rapid(QuestionBase): images = [] annotations_list = [] feedback_images = [] + i: RapidImage for i in self.images.all(): # TODO: add anotations - if i.feedback_image == True: + if i.feedback_image: if feedback: annotations_list.append(i.image_annotations) if based: diff --git a/shorts/models.py b/shorts/models.py index ccf68f14..ce61c720 100644 --- a/shorts/models.py +++ b/shorts/models.py @@ -34,6 +34,17 @@ from django.utils.html import format_html from helpers.images import combine_dicom_images_side_by_side from django.core.files import File import tempfile +from typing import List, TypedDict, Required, NotRequired + + +class QuestionData(TypedDict): + images: Required[list[str]] + annotations: Required[List[str]] + type: Required[str] + history: NotRequired[str] + answers: NotRequired[List[str]] + feedback: NotRequired[str] + feedback_images: NotRequired[List[str]] def image_directory_path(instance, filename): @@ -214,6 +225,66 @@ class Question(QuestionBase): """Returns a string of the examinations associated with the question.""" examinations = self.examination.all().values_list("examination", flat=True) return ", ".join(examinations) + + def get_sample_answers(self): + return self.sample_answers.all().order_by("-score") + + def get_question_json( + self, + based: bool = True, + feedback: bool = False, + answers: bool = True, + history: bool = False, + annotations: bool = False, + ) -> QuestionData: + """Returns a json representation of the question""" + + # Loop through rapidimage associations + images = [] + annotations_list = [] + feedback_images = [] + i: QuestionImage + for i in self.images.all(): + # TODO: add anotations + + if i.feedback_image: + if feedback: + #annotations_list.append(i.image_annotations) + if based: + feedback_images.append(image_as_base64(i.image)) + else: + feedback_images.append( + "{}/{}".format(settings.REMOTE_URL, i.image.url) + ) + # feedback_images.append("{}/{}".format(settings.REMOTE_URL, i.image.url)) + else: + #annotations_list.append(i.image_annotations) + if based: + images.append(image_as_base64(i.image)) + else: + images.append("{}/{}".format(settings.REMOTE_URL, i.image.url)) + + json = { + "images": images, + # "feedback_image": [], + "type": "shorts", + } + + #if annotations: + # json["annotations"] = annotations_list + + if history: + json["history"] = self.history + + if answers: + sample_answers = list(self.get_sample_answers().values_list("answer", "score")) + json["answers"] = sample_answers + + if feedback: + json["feedback_images"] = feedback_images + json["feedback"] = self.feedback + + return json class QuestionImage(models.Model): diff --git a/shorts/templates/shorts/exam_scores_user.html b/shorts/templates/shorts/exam_scores_user.html index e1760c5f..feafc2f6 100644 --- a/shorts/templates/shorts/exam_scores_user.html +++ b/shorts/templates/shorts/exam_scores_user.html @@ -10,7 +10,9 @@
- Answers: + Sample Answers: + +
@@ -59,30 +61,39 @@ }) // $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly .done(function (data) { - console.log(data); + console.log(data); - let images = data.images + let images = data.images - if ("feedback_images" in data) { - images.push(...data.feedback_images) - } + if ("feedback_images" in data) { + images.push(...data.feedback_images) + } - let event = new CustomEvent('loadDicomViewerUrls', { - "detail": { "images": images, "annotations": data.annotations } - }); + let event = new CustomEvent('loadDicomViewerUrls', { + "detail": { "images": images, "annotations": data.annotations } + }); - window.dispatchEvent(event); - $(".question-display-block .answers").empty().append(data.answers.toString()); - n = parseInt(question_number) + 1 - $(".question-display-block .question-number").empty().append(n); - $(".question-display-block .feedback").empty().append(`Feedback: ${data.feedback}
`); + window.dispatchEvent(event); + console.log(data) + if (data.answers && data.answers.length > 0) { + // Format: [[answer, score], ...] + let formattedAnswers = data.answers.map( + ans => `
${ans[0]} Score: ${ans[1]}
` + ).join(''); + $(".question-display-block .answers").empty().append(`${formattedAnswers}`); + } else { + $(".question-display-block .answers").empty().append("No sample answers available"); + } + n = parseInt(question_number) + 1 + $(".question-display-block .question-number").empty().append(n); + $(".question-display-block .feedback").empty().append(`Feedback: ${data.feedback}
`); // show some message according to the response. // For eg. A message box showing that the status has been changed - $(".inner-display-block").show(); - }) + $(".inner-display-block").show(); + }) .always(function () { - console.log('[Done]'); - }) + console.log('[Done]'); + }) }) $(".view-question-link").first().click(); @@ -93,11 +104,10 @@ {% block css %} - + {% endblock css %} - \ No newline at end of file