{% extends 'shorts/exams.html' %}
{% load crispy_forms_tags %}
{% load help_tags %}

{% block content %}
    <a href="{% url 'shorts:exam_question_detail' exam.id question_details.current|add:'-1' %}" title="View the Question">View</a>
    <a href="{% url 'shorts:question_update' question.id %}" title="Edit the Question">Edit</a>
    {% if request.user.is_superuser %}
        <a href="{% url 'admin:shorts_question_change' question.id %}" title="Edit the Question using the admin interface">Admin
            Edit</a> <a href="{% url 'admin:shorts_useranswer_change' answer.id %}"
                    title="Edit the user answer using the admin interface">Admin
            Edit (user answer)</a>
    {% endif %}
    <h2>Marking question <a href="{% url 'shorts:mark' exam.id question_details.current|add:'-1' %}"
                            title="View question answers">{{question_details.current}}</a> of {{question_details.total}}</h2>

    {% if discrepancy_form %}
        <div class="alert alert-info sticky-alert" role="alert">
            <details open>
                <summary><h3>This answer has discrepant scores</h3></summary>
                You can set the overall score here.<br/>

                <form method="POST" class="post-form">{% csrf_token %}
                    <input type="hidden" name="form_id" value="discrepancy_form">
                    {{ discrepancy_form.as_p }}
                    <button type="submit" name="save" class="" title="Save score">Save Score</button>
                </form>

                <h4>Marker scores and reason</h4>
                <ul>
                    {% for mark_object in answer.mark.all %}
                        <li>{{mark_object.marker}}: {{mark_object.score}}<br/>
                            {{mark_object.mark_reason}}
                        </li>
                    {% endfor %}
                </ul>
            </details>
        </div>
    {% endif %}

    {% help "Help" %}
        <p>Marking guidance for the 2B exam can be viewed on the RCR site at <a href="https://www.rcr.ac.uk/exams-training/rcr-exams/clinical-radiology-exams/frcr-part-2b-radiology-cr2b/frcr-part-2b-radiology-cr2b-scoring-system/" target="_blank">here</a></p>

        <p>For convienience the info is also copied below:</p>
        <div class="alert alert-secondary" role="alert">
            <h3>FRCR Part 2B Short Cases Marking Guidance</h3>
        <p>
            Candidates report on <b>25 cases</b> and have the opportunity to attain <b>five marks per case</b>, so a maximum of <b>125 marks</b> across the exam. <b>No half marks</b> will be allocated. Candidate responses are independently double marked and the final score awarded will be the average of the two examiner marks. The marking framework permits a difference of up to one mark between the two examiners. Any score difference of two or above will be flagged for review and discussion.
        </p>

        <p>
            <a href="#">Click here for short case example questions.</a>
        </p>

        <p>
            All questions include question-specific marking guidance to indicate key findings, diagnosis and recommended onward management. Examiners will use the following mark scheme descriptors for examiners alongside the marking guidance to score candidates’ responses:
        </p>

        <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th>Score</th>
                    <th>Marking descriptors</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><b>5</b></td>
                    <td>
                        <ul>
                            <li>Detects all major findings and most minor observations.</li>
                            <li>Makes correct diagnosis or differential diagnoses using clinical information provided.</li>
                            <li>Makes safe further management plans with clear recommendations (including MDT or specialist referral if appropriate).</li>
                            <li>Demonstrates a clear understanding of likely further management required by different MDTs.</li>
                            <li>Report is clear, logical and concise.</li>
                            <li>May demonstrate knowledge beyond the core curriculum.</li>
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td><b>4</b></td>
                    <td>
                        <ul>
                            <li>Detects most major findings (including those marked essential) and most minor observations.</li>
                            <li>Makes correct diagnosis or differential diagnoses using clinical information provided.</li>
                            <li>Makes safe further management plans with clear recommendations (including MDT or specialist referral if appropriate).</li>
                            <li>Report is clear, logical and concise.</li>
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td><b>3</b></td>
                    <td>
                        <ul>
                            <li>Detects most major findings (including those marked essential) and some minor observations.</li>
                            <li>May or may not reach the correct diagnosis but demonstrates sensible clinical reasoning.</li>
                            <li>Safe differential diagnoses offered.</li>
                            <li>May not know full management plan but demonstrates enough knowledge not to compromise patient safety.</li>
                            <li>Report is clear although it may not be ideally structured.</li>
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td><b>2</b></td>
                    <td>
                        <ul>
                            <li>Major/critical observations missed.</li>
                            <li>Undue significance placed on irrelevant or minor findings.</li>
                            <li>Limited understanding of pathology.</li>
                            <li>Fails to make correct diagnosis or provide reasonable differential diagnoses.</li>
                            <li>Failure to refer to appropriate MDT/clinical team or inappropriate over-investigation e.g. MRI and orthopaedic referral for benign bone lesions.</li>
                            <li>Unstructured, scattergun report.</li>
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td><b>1</b></td>
                    <td>
                        <ul>
                            <li>Major/critical observations missed.</li>
                            <li>Undue significance placed on minor or incidental findings.</li>
                            <li>Demonstrates no understanding of pathology.</li>
                            <li>Inappropriate patient management (including over-investigation).</li>
                            <li>Incoherent, rambling report.</li>
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td><b>0</b></td>
                    <td>
                        <ul>
                            <li>No answer provided or answer is entirely incorrect or inappropriate/unsafe patient management.</li>
                        </ul>
                    </td>
                </tr>
            </tbody>
        </table>

        <p>
            An Angoff standard setting process is used to set the pass mark for each sitting based on the content used.
        </p>
        </div>

        <p>Ideally each question will have individual marking guidance that will be shown below.</p>
    {% endhelp %}

    {% if not next_unmarked_id and not unmarked %}
        <div class="alert alert-info sticky-alert" role="alert"><h3>Success! Marking question complete.</h3>

            {% if exam.double_mark %}
                <a href="{% url 'shorts:mark_answer_override' exam_id=exam.pk question_number=question_details.current|add:'-1' answer_id=answer.id %}">Set final score</a><br/>
            {% endif %}

            Return to <a class="marking-link" href="{% url 'shorts:mark' exam.id question_details.current|add:'-1' %}">question
                overview</a>, <a class="marking-link" href="{% url 'shorts:mark_overview' pk=exam.pk %}">marking
                    overview</a><br />

            {% if previous_answer_id %}
                <a href="{% url 'shorts:mark_answer' exam_id=exam.pk question_number=question_details.current|add:'-1' answer_id=previous_answer_id %}" class="btn btn-sm btn-secondary">Previous candidate</a>
            {% else %}
                <a class="btn btn-sm btn-secondary disabled-link" tabindex="-1" aria-disabled="true" style="pointer-events: none; color: #aaa; text-decoration: none;">Previous candidate</a>
            {% endif %}
            {% if next_answer_id %}
                <a href="{% url 'shorts:mark_answer' exam_id=exam.pk question_number=question_details.current|add:'-1' answer_id=next_answer_id %}" class="btn btn-sm btn-secondary">Next candidate</a>
            {% else %}
                <a class="btn btn-sm btn-secondary disabled-link" tabindex="-1" aria-disabled="true" style="pointer-events: none; color: #aaa; text-decoration: none;">Next candidate</a>
            {% endif %}
        </div>
    {% endif %}

    <div class="alert alert-primary mb-3" role="alert" style="font-size: 1.1em;">
        <strong>Marking Candidate:</strong>
        <span class="ml-2">{{ answer.get_candidate_masked }}</span>
        <span class="ml-3 text-muted">({{ unmarked|length }} answer{{ unmarked|length|pluralize }} left to mark)</span>
    </div>

    <!-- Enable Bootstrap dark mode using 'bg-dark', 'text-light', and dark card classes -->
    <div class="card mb-4 bg-dark text-light border-secondary">
        <details>
            <summary class="card-header bg-secondary text-light" title="Click to view/hide the question details">
                <strong>Question Details</strong>
            </summary>
            <div class="card-body">
                <p class="pre-whitespace mb-2"><b>History:</b> {{ question.history }}</p>
                <p class="pre-whitespace mb-2"><b>Region:</b> {{ question.get_regions }}</p>
                <p class="pre-whitespace mb-2"><b>Examination:</b> {{ question.get_examinations }}</p>
                <p class="pre-whitespace mb-2"><b>Abnormality:</b> {{ question.get_abnormalities }}</p>
                <div class="pre-whitespace multi-image-block mb-2"><b>Images:</b>
                    <div class="row">
                            <div class="col-md-4 mb-2 w-auto">
                        {% for image in question.images.all %}
                                <div class="card h-100 w-auto bg-dark text-light border-secondary">
                                    <div class="card-body p-2">
                                        <span class="image-block" style="">
                                            <strong>Image {{ forloop.counter }}</strong>
                                            {% if image.description %} ({{image.description}}){% endif %}
                                            {% if image.feedback_image %}
                                                <span class="badge bg-info text-dark">Feedback image</span>
                                            {% endif %}
                                            <div class="dicom-image shorts-img {% if image.feedback_image %}feedback-img{% endif %} mt-2"
                                                 data-url="{{ remote_url }}{{ image.image.url}}"></div>
                                        </span>
                                    </div>
                                </div>
                        {% endfor %}
                            </div>
                    </div>
                </div>
                <p class="pre-whitespace mb-0"><b>Feedback:</b> {{ question.feedback }}</p>
            </div>
        </details>
    </div>

    <div class="row mb-4">
        <div class="col-md-6">
            <div class="card h-100 bg-dark text-light border-secondary">
                <details open>
                    <summary class="card-header bg-secondary text-light" title="Click to view/hide the mark scheme">
                        <strong>Marking Guidance</strong>
                    </summary>
                    <div class="card-body">
                        {{ question.marking_guidance | safe }}
                    </div>
                </details>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card h-100 bg-dark text-light border-secondary">
                <details open>
                    <summary class="card-header bg-secondary text-light" title="Click to view/hide user answers">
                        <strong>User Answer</strong>
                    </summary>
                    <div class="card-body">
                        <div class="shorts-answer">
                            <pre class="mb-0 text-light bg-dark border-0">{{ answer.answer }}</pre>
                        </div>
                    </div>
                </details>
            </div>
        </div>
    </div>
    <div class="marking">
        <form method="POST" class="post-form">{% csrf_token %}
            <input type="hidden" name="form_id" value="mark_form">
            {{ form|crispy }}
            <button type="submit" name="save" class="save btn btn-default" title="Save answer">Save</button>
            {% if next_unmarked_id %}
                <button type="submit" name="next" class="save btn btn-default"
                        title="Save answer and move to next question">Next answer</button>
                <!-- <button type="submit" name="skip" class="save btn btn-default">Skip</button> -->
            {% else %}
                {% if not unmarked %}
                    Success! Marking question complete. <a href="{% url 'shorts:mark_overview' pk=exam.pk %}">Return to marking
                        overview</a>
                {% endif %}
            {% endif %}
        </form>
    </div>
    {% if previous_answer_id %}
        <a href="{% url 'shorts:mark_answer' exam_id=exam.pk question_number=question_details.current|add:'-1' answer_id=previous_answer_id %}">Previous
            candidate</a>
    {% endif %}
    {% if next_answer_id %}
        <a href="{% url 'shorts:mark_answer' exam_id=exam.pk question_number=question_details.current|add:'-1' answer_id=next_answer_id %}">Next candidate</a>
    {% endif %}
{% endblock %}
