Lots of improvemnets

This commit is contained in:
Ross
2024-01-15 22:46:41 +00:00
parent aa41423b22
commit 5c2578a3ce
22 changed files with 527 additions and 216 deletions
+7 -1
View File
@@ -150,6 +150,7 @@ class AnatomyQuestionForm(ModelForm):
"region",
"modality",
"structure",
"feedback",
"examination",
"body_part",
"description",
@@ -158,9 +159,14 @@ class AnatomyQuestionForm(ModelForm):
widgets = {
"structure": autocomplete.ModelSelect2(url="atlas:structure-autocomplete"),
"answer_help": TinyMCE(attrs={"cols": 80, "rows": 10}),
"answer_help": TinyMCE(attrs={"cols": 80, "rows": 5}),
"feedback": TinyMCE(attrs={"cols": 80, "rows": 5}),
}
class AnatomyQuestionAnswerForm(ModelForm):
class Meta:
model = AnatomyQuestion
fields = []
AnswerFormSet = inlineformset_factory(
AnatomyQuestion,
+12
View File
@@ -451,6 +451,18 @@ class Exam(ExamBase):
return exam_json
def get_question_cid_user_answer(self, question_index, cid):
question = self.get_question_by_index(question_index)
answer = UserAnswer.objects.filter(question=question, cid=cid, exam=self)
return answer
def get_question_user_user_answer(self, question_index, user):
print(user)
question = self.get_question_by_index(question_index)
answer = UserAnswer.objects.filter(question=question, user=user.pk, exam=self)
return answer
@reversion.register
class UserAnswer(UserAnswerBase):
@@ -26,17 +26,10 @@
document.addEventListener('drop', function (e) { e.preventDefault(); }, false);
function add_input_form() {
var form_idx = $('#id_answers-TOTAL_FORMS').val();
$('#form_set').append($('#empty_form').html().replace(/__prefix__/g, form_idx));
$('#id_answers-TOTAL_FORMS').val(parseInt(form_idx) + 1);
}
$(document).ready(function () {
$("#add_examination").appendTo($("label[for='id_examination']"));
$("#add_body_part").appendTo($("label[for='id_body_part']"));
$("#add_structure").appendTo($("label[for='id_structure']"));
dropContainer = document.getElementById("drop-container");
@@ -221,15 +214,18 @@
{{ form.media }}
{% endblock %}
{% block content %}
<h2>Submit Question</h2>
{% if object %}
{% include 'anatomy/question_link_header.html' %}
{% endif %}
<h2>{{object|yesno:"Update,Create"}} Question</h2>
<form action="" method="post" enctype="multipart/form-data" id="anatomyquestion-form">
{% csrf_token %}
<a href="/anatomy/examination/create" id="add_examination" class="add-popup"
onclick="return showAddPopup(this);"><img src="{% static '/img/icon-addlink.svg' %}"></a>
<a href="/anatomy/body_part/create" id="add_body_part" class="add-popup"
onclick="return showAddPopup(this);"><img src="{% static '/img/icon-addlink.svg' %}"></a>
<a href="/anatomy/structure/create" id="add_structure" class="add-popup" onclick="return showAddPopup(this);"><img
src="{% static '/img/icon-addlink.svg' %}"></a>
<table>
{{ form.as_table }}
@@ -237,24 +233,6 @@
<div id="drop-container" class="drop-target">Drop image here
<div id="drop-filenames"></div>
</div>
<h3>Answers:</h3>
<input type="button" value="Add More Answers" id="add_more">
<div id="form_set">
{% for form in answer_formset %}
<ul class="no-error answer-formset">
{{form.non_field_errors}}
{{form.errors}}
{{ form.as_ul }}
</ul>
{% endfor %}
</div>
{{ answer_formset.management_form }}
<input type="submit" class="submit-button" value="Submit" name="submit">
<input type="submit" class="submit-button" value="Submit and Clone" name="submit-clone">
</form>
<div id="empty_form" style="display:none">
<ul class='no_error answer-formset'>
{{ answer_formset.empty_form.as_ul }}
</ul>
</div>
{% endblock %}
@@ -0,0 +1,56 @@
{% extends "anatomy/base.html" %}
{% load static %}
{% block js %}
<!--<script type="text/javascript" src="/admin/jsi18n/"></script>-->
<script type="text/javascript">
function add_input_form() {
var form_idx = $('#id_answers-TOTAL_FORMS').val();
$('#form_set').append($('#empty_form').html().replace(/__prefix__/g, form_idx));
$('#id_answers-TOTAL_FORMS').val(parseInt(form_idx) + 1);
}
$(document).ready(function () {
$('#add_more').click(() => {
add_input_form()
});
});
</script>
{{ form.media }}
{% endblock %}
{% block content %}
{% if object %}
{% include 'anatomy/question_link_header.html' %}
{% endif %}
<h2>Question Answers</h2>
This page can be used to add and modify answers associated with a question. Answers in here will be used for automarking submitted answers so it is best not to remove. Updating an answer here will not automatically update a submitted answers (scores cache would need to be refreshed)
<form action="" method="post" enctype="multipart/form-data" id="anatomyquestion-form">
{% csrf_token %}
<h3>Answers:</h3>
<div id="form_set">
{% for form in answer_formset %}
<ul class="no-error answer-formset">
{{form.non_field_errors}}
{{form.errors}}
{{ form.as_ul }}
</ul>
{% endfor %}
</div>
{{ answer_formset.management_form }}
<input type="button" value="Add More Answers" id="add_more">
<p>
<input type="submit" class="submit-button" value="Submit" name="submit">
</p>
</form>
<div id="empty_form" style="display:none">
<ul class='no_error answer-formset'>
{{ answer_formset.empty_form.as_ul }}
</ul>
</div>
{% endblock %}
+15 -10
View File
@@ -1,19 +1,24 @@
{% extends 'generic/exam_scores_base.html' %}
{% block table_answers %}
{% for question in questions %}
<tr>
<td><a href="{% url 'anatomy:mark' exam_pk=exam.pk sk=forloop.counter0 %}">Question {{forloop.counter}}</a>
</td>
{% for question in questions %}
<tr>
<td><a href="{% url 'anatomy:mark' exam_pk=exam.pk sk=forloop.counter0 %}">Question {{forloop.counter}}</a>
</td>
{% comment %} {% for cid in cids %}
<td class="anatomy-ans user-answer-score-{{score_by_question|get_item:question|get_item:cid}}" title="answer score: {{score_by_question|get_item:question|get_item:cid}}">{{ans_by_question|get_item:question|get_item:cid}}</td>
{% endfor %} {% endcomment %}
{% for cid in cids %}
{% with by_question|get_item:question|get_item:cid as ans_score %}
<td class="anatomy-ans user-answer-score-{{ans_score.1}}" title="answer score: {{ans_score.1}}">{{ans_score.0}}</td>
{% endwith %}
{% endfor %}
</tr>
{% for cid in cids %}
{% with by_question|get_item:question|get_item:cid as ans_score %}
<td class="anatomy-ans user-answer-score-{{ans_score.1}}"
title="answer score: {{ans_score.1}}"
data-examquestionindex={{forloop.parentloop.counter0}}
data-cid={{cid}}
>{{ans_score.0}}<a class="show-on-hover" href="{% url 'anatomy:exam_question_user_answer' exam.pk forloop.parentloop.counter0 cid|slice:':1' cid|slice:'2:' %}"><i class="bi bi-pen"></i>
</a></td>
{% endwith %}
{% endfor %}
</tr>
{% endfor %}
{% endblock table_answers %}
+87 -112
View File
@@ -1,45 +1,20 @@
{% extends 'anatomy/base.html' %}
{% block content %}
{% load static %}
{% load static %}
<!-- testing -->
<!--<div id="dicom-image" data-url="http://localhost:8000/static/abdoct.jpg"
data-annotations='{{question.image_annotations}}' data-edit_annotation=true></div>-->
<div class="floating-header">
<a href="{% url 'anatomy:anatomy_question_update' question.id %}" title="Edit the Question">Edit</a>
<a href="{% url 'anatomy:question_clone' question.id %}" title="Clone the Question">Clone</a>
<a href="{% url 'anatomy:question_delete' pk=question.pk %}" title="Delete the Question">Delete</a>
<a href="#"
onclick="return window.create_popup_window('{% url 'feedback_create' question_type='anatomy' pk=question.pk %}')">
Add Note</a>
{% if request.user.is_superuser %}
<a href="{% url 'admin:anatomy_anatomyquestion_change' question.id %}"
title="Edit the Question using the admin interface">Admin Edit</a>
<a href="{% url 'anatomy:question_user_answers' question.id %}" title="View user answers associated with this question">User answers</a>
{% endif %}
{% if exam %}
<div>
{% if previous > -1 %}
<a href="{% url 'anatomy:exam_question_detail' exam.id previous %}">Previous question</a>
{% endif %}
Viewing question as part of exam: <a href="{% url 'anatomy:exam_overview' exam.id %}">{{exam.name}}</a> [{{pos}}/{{exam_length}}]
{% if next %}
<a href="{% url 'anatomy:exam_question_detail' exam.id next %}">Next question</a>
{% endif %}
</div>
{% endif %}
</div>
<button id="save-annotations">Save Annotations</button>
<div id="dicom-image" class="dicom-image" data-url="https://www.penracourses.org.uk{{ question.image.url}}"
data-annotations='{{question.image_annotations}}' data-edit_annotation=true></div>
<div class="question">
<div class="date">
Created: {{ question.created_date|date:"d/m/Y" }}
</div>
<h1>{{ question.get_primary_answer }}</h1>
<h2>{{question.question_type}}</h2>
{% include 'anatomy/question_link_header.html' %}
<button id="save-annotations">Save Annotations</button>
<div id="dicom-image" class="dicom-image" data-url="https://www.penracourses.org.uk{{ question.image.url}}"
data-annotations='{{question.image_annotations}}' data-edit_annotation=true></div>
<div class="question">
<div class="date">
Created: {{ question.created_date|date:"d/m/Y" }}
</div>
<h1>{{ question.get_primary_answer }}</h1>
<h2>{{question.question_type}}</h2>
<details>
<summary>
Answers:
@@ -50,7 +25,7 @@
<tr>
<td>
<span {% if answer.proposed %}class="proposed-answer" data-aid="{{answer.pk}}" data-question-type="anatomy"
{% endif %}>
{% endif %}>
{{ answer }}
</span>
<td>
@@ -59,67 +34,67 @@
{% endfor %}
</table>
</details>
<div>
Answer help: {{ question.answer_help|safe }}
</div>
<div>
Answer suggest incorrect: {{ question.answer_suggest_incorrect }}
</div>
<div>
Description: {{ question.description }}
</div>
<div>
Exams: {% for exam in question.exams.all %}
<a href="{% url 'anatomy:exam_overview' pk=exam.pk %}">{{ exam.name }}</a>
{% endfor %}
</div>
<div>
Modality: {{ question.modality }}
</div>
<div>
Region: {{ question.region }}
</div>
<div>
Structure: {{ question.structure }}
</div>
<div>
Body Part: {{ question.body_part }}
</div>
<div>
Author: {% for author in question.author.all %}
{{ author }},
{% endfor %}
</div>
<div>
Open access: {{ question.open_access }}
</div>
{% include 'question_notes.html' %}
<div>
<details>
<summary>
<span id="annotation-json-toggle">Annotation JSON (+/-):</span> <span id="annotation-json-content">
</summary>
<div>
Answer help: {{ question.answer_help|safe }}
</div>
<div>
Answer suggest incorrect: {{ question.answer_suggest_incorrect }}
</div>
<div>
Description: {{ question.description }}
</div>
<div>
Exams: {% for exam in question.exams.all %}
<a href="{% url 'anatomy:exam_overview' pk=exam.pk %}">{{ exam.name }}</a>
{% endfor %}
</div>
<div>
Modality: {{ question.modality }}
</div>
<div>
Region: {{ question.region }}
</div>
<div>
Structure: {{ question.structure }}
</div>
<div>
Body Part: {{ question.body_part }}
</div>
<div>
Author: {% for author in question.author.all %}
{{ author }},
{% endfor %}
</div>
<div>
Open access: {{ question.open_access }}
</div>
{% include 'question_notes.html' %}
<div>
<details>
<summary>
<span id="annotation-json-toggle">Annotation JSON (+/-):</span> <span id="annotation-json-content">
</summary>
{% if question.image_annotations %}
{% if question.image_annotations %}
<pre>{{ question.image_annotations }}</pre>
{% else %}
No saved annotations.
{% endif %}
</span>
</details>
{% else %}
No saved annotations.
{% endif %}
</span>
</details>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
<script type="text/javascript">
$(document).ready(function () {
//$("#annotation-json-toggle").click(() => {
// $("#annotation-json-content").toggle()
//});
// send request to change the is_private state on customSwitches toggle
$("#save-annotations").click(function () {
json = getJsonToolStateNoId();
$.ajax({
$("#save-annotations").click(function () {
json = getJsonToolStateNoId();
$.ajax({
url: "{% url 'anatomy:question_save_annotation' pk=question.pk %}",
data: {
csrfmiddlewaretoken: "{{ csrf_token }}",
@@ -134,38 +109,38 @@
})
// $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly
.done(function (data) {
console.log(data);
.done(function (data) {
console.log(data);
// show some message according to the response.
// For eg. A message box showing that the status has been changed
if (data.status == "success") {
toastr.info('Annotations saved')
} else {
toastr.warning('Error saving annotations')
}
})
.always(function () {
console.log('[Done]');
})
})
});
if (data.status == "success") {
toastr.info('Annotations saved')
} else {
toastr.warning('Error saving annotations')
}
})
.always(function () {
console.log('[Done]');
})
})
});
function getJsonToolStateNoId() {
const el = document.getElementById("dicom-image");
function getJsonToolStateNoId() {
const el = document.getElementById("dicom-image");
const c = cornerstone.getEnabledElement(el);
const c = cornerstone.getEnabledElement(el);
const toolStateManager = cornerstoneTools.globalImageIdSpecificToolStateManager;
const toolStateManager = cornerstoneTools.globalImageIdSpecificToolStateManager;
const image_id = c.image.imageId;
const image_id = c.image.imageId;
const state_with_id = toolStateManager.saveToolState()
const state_with_id = toolStateManager.saveToolState()
const json_tool_state = JSON.stringify(state_with_id[image_id]);
const json_tool_state = JSON.stringify(state_with_id[image_id]);
return json_tool_state;
return json_tool_state;
}
</script>
}
</script>
{% endblock %}
@@ -0,0 +1,28 @@
<div class="floating-header">
<a href="{% url 'anatomy:anatomy_question_update' question.id %}" title="Edit the Question">Edit</a>
<a href="{% url 'anatomy:question_clone' question.id %}" title="Clone the Question">Clone</a>
<a href="{% url 'anatomy:question_delete' pk=question.pk %}" title="Delete the Question">Delete</a>
<a href="{% url 'anatomy:question_answer_update' pk=question.pk %}" title="Update the question answers">Edit Answers</a>
<a href="#"
onclick="return window.create_popup_window('{% url 'feedback_create' question_type='anatomy' pk=question.pk %}')">
Add Note</a>
{% if request.user.is_superuser %}
<a href="{% url 'admin:anatomy_anatomyquestion_change' question.id %}"
title="Edit the Question using the admin interface">Admin Edit</a>
<a href="{% url 'anatomy:question_user_answers' question.id %}" title="View user answers associated with this question">User answers</a>
{% endif %}
{% if exam %}
<div>
{% if previous > -1 %}
<a href="{% url 'anatomy:exam_question_detail' exam.id previous %}">Previous question</a>
{% endif %}
Viewing question as part of exam: <a href="{% url 'anatomy:exam_overview' exam.id %}">{{exam.name}}</a> [{{pos}}/{{exam_length}}]
{% if next %}
<a href="{% url 'anatomy:exam_question_detail' exam.id next %}">Next question</a>
{% endif %}
</div>
{% endif %}
</div>
@@ -1,6 +1,7 @@
{% extends 'anatomy/base.html' %}
{% block content %}
{% include 'anatomy/question_link_header.html' %}
<h2>{{question}}</h2>
{% if answer_compare %}
+5
View File
@@ -28,6 +28,11 @@ urlpatterns = [
views.AnatomyQuestionUpdate.as_view(),
name="anatomy_question_update",
),
path(
"question/<int:pk>/update_answers",
views.AnatomyQuestionAnswerUpdate.as_view(),
name="question_answer_update",
),
path(
"question/<int:pk>/save_annotation",
views.question_save_annotation,
+65 -23
View File
@@ -28,6 +28,7 @@ from dal import autocomplete
from .forms import (
AnatomyAnswerForm,
AnatomyQuestionAnswerForm,
AnswerFormSet,
AnswerUpdateFormSet,
BodyPartForm,
@@ -247,7 +248,7 @@ def mark(request, exam_pk, sk, unmarked_exam_answers_only=True, review=False):
elif not unmarked_exam_answers_only:
mark_url = "anatomy:mark_all"
questions = exam.exam_questions.all()
questions = exam.exam_questions.all().prefetch_related("answers")
n = sk
@@ -358,6 +359,7 @@ def mark(request, exam_pk, sk, unmarked_exam_answers_only=True, review=False):
for suggest_incorrect in question.answer_suggest_incorrect:
if suggest_incorrect in ans:
answer_suggest_incorrect.append(ans)
break
return render(
request,
@@ -554,6 +556,42 @@ class AnatomyQuestionCreate(AnatomyQuestionCreateBase):
# return super().form_valid(form)
class AnatomyQuestionAnswerUpdate(RevisionMixin, AuthorOrCheckerRequiredMixin, UpdateView):
model = AnatomyQuestion
form_class = AnatomyQuestionAnswerForm
template_name = "anatomy/anatomyquestionanswer_form.html"
def get_context_data(self, **kwargs):
context = super(AnatomyQuestionAnswerUpdate, self).get_context_data(**kwargs)
if self.request.POST:
context["answer_formset"] = AnswerUpdateFormSet(
self.request.POST, self.request.FILES, instance=self.object
)
context["answer_formset"].full_clean()
else:
context["answer_formset"] = AnswerUpdateFormSet(instance=self.object)
context["question"] = context["object"]
return context
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.save()
#form.instance.author.add(self.request.user.id)
context = self.get_context_data(form=form)
formset = context["answer_formset"]
# logger.debug(formset.is_valid())
if formset.is_valid():
response = super().form_valid(form)
formset.instance = self.object
formset.save()
return response
else:
return super().form_invalid(form)
# class AnatomyQuestionUpdate(LoginRequiredMixin, AuthorOrCheckerRequiredMixin,
class AnatomyQuestionUpdate(RevisionMixin, AuthorOrCheckerRequiredMixin, UpdateView):
model = AnatomyQuestion
@@ -566,13 +604,14 @@ class AnatomyQuestionUpdate(RevisionMixin, AuthorOrCheckerRequiredMixin, UpdateV
def get_context_data(self, **kwargs):
context = super(AnatomyQuestionUpdate, self).get_context_data(**kwargs)
if self.request.POST:
context["answer_formset"] = AnswerUpdateFormSet(
self.request.POST, self.request.FILES, instance=self.object
)
context["answer_formset"].full_clean()
else:
context["answer_formset"] = AnswerUpdateFormSet(instance=self.object)
#if self.request.POST:
# context["answer_formset"] = AnswerUpdateFormSet(
# self.request.POST, self.request.FILES, instance=self.object
# )
# context["answer_formset"].full_clean()
#else:
# context["answer_formset"] = AnswerUpdateFormSet(instance=self.object)
context["question"] = context["object"]
return context
def form_valid(self, form):
@@ -589,23 +628,24 @@ class AnatomyQuestionUpdate(RevisionMixin, AuthorOrCheckerRequiredMixin, UpdateV
form.instance.author.add(self.request.user.id)
context = self.get_context_data(form=form)
formset = context["answer_formset"]
# logger.debug(formset.is_valid())
if formset.is_valid():
response = super().form_valid(form)
formset.instance = self.object
formset.save()
#formset = context["answer_formset"]
return super().form_valid(form)
## logger.debug(formset.is_valid())
#if formset.is_valid():
# response = super().form_valid(form)
# formset.instance = self.object
# formset.save()
# restore exam orders
#for exam in self.object.exams.all():
# if exam in exam_orders and self.object in exam_orders[exam]:
# print(exam_orders[exam])
# exam.exam_questions.set(exam_orders[exam])
# exam.save()
# # restore exam orders
# #for exam in self.object.exams.all():
# # if exam in exam_orders and self.object in exam_orders[exam]:
# # print(exam_orders[exam])
# # exam.exam_questions.set(exam_orders[exam])
# # exam.save()
return response
else:
return super().form_invalid(form)
# return response
#else:
# return super().form_invalid(form)
class QuestionClone(AnatomyQuestionCreateBase):
@@ -625,6 +665,8 @@ class QuestionClone(AnatomyQuestionCreateBase):
# initial_data["exams"] = list(exams)
# TODO: clone answers?
return initial_data