Skip to content

Commit

Permalink
SAK-45987 Fix cancel behaviour with rubric evaluations (sakaiproject#…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianfish authored Dec 23, 2021
1 parent 8774834 commit 23bef61
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

<form wicket:id="sakaiRubricGradingForm">
<sakai-rubric-grading wicket:id="sakai-rubric-grading"></sakai-rubric-grading>

<div>
<input type="submit" wicket:id="submit" wicket:message="value:button.saverubricgrading" />
<input type="submit" wicket:id="cancel" wicket:message="value:button.cancel" />
</div>
</form>
<script src="/gradebookng-tool/scripts/gradebook-saverubric.js"></script>
<script src="/gradebookng-tool/scripts/gradebook-savecancelrubric.js"></script>

</wicket:panel>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
RubricGradePanel.this.window.close(target);
}
};
cancel.setOutputMarkupId(true).setMarkupId("cancelrubric");
cancel.setDefaultFormProcessing(false);
form.add(cancel);

Expand Down
5 changes: 5 additions & 0 deletions gradebookng/tool/src/webapp/scripts/gradebook-cancelrubric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
document.getElementById("cancelrubric").addEventListener("click", e => {

const rubricGrading = document.getElementsByTagName("sakai-rubric-grading").item(0);
rubricGrading && rubricGrading.cancel();
});
11 changes: 11 additions & 0 deletions gradebookng/tool/src/webapp/scripts/gradebook-savecancelrubric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
document.getElementById("saverubric").addEventListener("click", e => {

const rubricGrading = document.getElementsByTagName("sakai-rubric-grading").item(0);
rubricGrading && rubricGrading.release();
});

document.getElementById("cancelrubric").addEventListener("click", e => {

const rubricGrading = document.getElementsByTagName("sakai-rubric-grading").item(0);
rubricGrading && rubricGrading.cancel();
});
5 changes: 0 additions & 5 deletions gradebookng/tool/src/webapp/scripts/gradebook-saverubric.js

This file was deleted.

21 changes: 18 additions & 3 deletions samigo/samigo-app/src/webapp/js/samigo-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,26 @@ function returnToHostUrl(url) {
}
}

function initRubricDialog(gradingId, saveText, cancelText, titleText) {
function initRubricDialog(gradingId, doneText, cancelText, titleText) {

var modalId = "modal" + gradingId;
var previousScore = $('.adjustedScore' + gradingId).val();
$("#" + modalId).dialog({
modal: true,
buttons: [
{
text: saveText,
click: function () { $(this).dialog("close"); }
text: doneText,
click: function () {
$(this).dialog("close");
}
},
{
text: cancelText,
click: function () {

const rubric = this.querySelector(`sakai-rubric-grading`);
rubric.cancel();

$(this).dialog("close");
$('.adjustedScore' + gradingId).val(previousScore);
}
Expand Down Expand Up @@ -162,6 +167,16 @@ $(function () {
saveButton = document.getElementById("editTotalResults:save");
saveButton && saveButton.addEventListener("click", save);

const cancel = e => {
[...document.getElementsByTagName("sakai-rubric-grading")].forEach(srb => srb.cancel());
};

let cancelButton = document.getElementById("editStudentResults:cancel");
cancelButton && cancelButton.addEventListener("click", cancel);

cancelButton = document.getElementById("editTotalResults:cancel");
cancelButton && cancelButton.addEventListener("click", cancel);

if ( $("#selectIndexForm\\:selectTable").length ) {
$("#selectIndexForm\\:selectTable").tablesorter({
sortList: [[2,0]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ function toPoint(id)
<f:actionListener
type="org.sakaiproject.tool.assessment.ui.listener.evaluation.TotalScoreListener" />
</h:commandButton>
<h:commandButton value="#{commonMessages.cancel_action}" action="totalScores" immediate="true">
<h:commandButton id="cancel" value="#{commonMessages.cancel_action}" action="totalScores" immediate="true">
<f:actionListener type="org.sakaiproject.tool.assessment.ui.listener.evaluation.ResetTotalScoreListener" />
<f:actionListener type="org.sakaiproject.tool.assessment.ui.listener.evaluation.TotalScoreListener" />
</h:commandButton>
Expand Down
4 changes: 2 additions & 2 deletions samigo/samigo-app/src/webapp/jsf/evaluation/questionScore.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ $Id$
function initRubricDialogWrapper(gradingId) {
initRubricDialog(gradingId
, <h:outputText value="'#{evaluationMessages.save_cont}'"/>
, <h:outputText value="'#{evaluationMessages.done}'"/>
, <h:outputText value="'#{evaluationMessages.cancel}'"/>
, <h:outputText value="'#{evaluationMessages.saverubricgrading}'"/>);
}
Expand Down Expand Up @@ -1224,7 +1224,7 @@ $Id$
<f:actionListener
type="org.sakaiproject.tool.assessment.ui.listener.evaluation.QuestionScoreListener" />
</h:commandButton>
<h:commandButton value="#{commonMessages.cancel_action}" action="totalScores" immediate="true"/>
<h:commandButton id="cancel" value="#{commonMessages.cancel_action}" action="totalScores" immediate="true"/>
</p>
</h:form>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ export class SakaiGrader extends gradableDataMixin(SakaiElement) {
<fa-icon size="1.5em" i-class="fas video" path-prefix="/webcomponents/assets" style="vertical-align: middle;"></fa-icon>
</div>
<button @click=${this.doneWithFeedbackDialog}>${this.assignmentsI18n["gen.don"]}</button>
<button @click=${this.cancelRubric}>${this.assignmentsI18n["gen.cancel"]}</button>
</div>
<div class="feedback-attachments-block grader-label">
${this.submission.feedbackAttachments ? html`
Expand Down Expand Up @@ -514,6 +515,14 @@ export class SakaiGrader extends gradableDataMixin(SakaiElement) {
this.querySelector("#grader-rubric-link").focus();
}

cancelRubric() {

const rubricGrading = document.getElementsByTagName("sakai-rubric-grading").item(0);
rubricGrading && rubricGrading.cancel();

this.doneWithRubricDialog();
}

replaceWithEditor(id, width = 160, height = 60) {

const editor = CKEDITOR.replace(id, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {SakaiElement} from "../sakai-element.js";
import { getUserLocale } from "../sakai-portal-utils.js";

class RubricsElement extends SakaiElement {

constructor() {

super();

this.locale = (window.top?.portal?.locale || window.top?.sakai?.locale?.userLocale || "en-US").replace("_", "-");
this.locale = getUserLocale();
}

isUtilsAvailable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { RubricsElement } from "./rubrics-element.js";
import { html } from "../assets/lit-element/lit-element.js";
import "./sakai-rubric-grading-comment.js";
import { SakaiRubricsLanguage, tr } from "./sakai-rubrics-language.js";
import { getUserId } from "../sakai-portal-utils.js";

export class SakaiRubricGrading extends RubricsElement {

constructor() {

super();

this.existingEvaluation = false;

this.rubric = { title: "" };
this.criteria = [];
this.totalPoints = 0;
Expand Down Expand Up @@ -68,6 +67,18 @@ export class SakaiRubricGrading extends RubricsElement {

get evaluatedItemId() { return this._evaluatedItemId; }

set evaluation(value) {

this._evaluation = value;
if (value.id && value.status !== "DRAFT") {
// Store the evaluation in case the grading gets cancelled.
sessionStorage.setItem(this.getStorageKey(), JSON.stringify(value));
}
this.requestUpdate();
}

get evaluation() { return this._evaluation; }

set toolId(value) {

this._toolId = value;
Expand All @@ -80,6 +91,10 @@ export class SakaiRubricGrading extends RubricsElement {
return this.i18nLoaded;
}

getStorageKey() {
return `rubric-evaluation-${this.evaluation.id}-${getUserId()}`;
}

render() {

return html`
Expand Down Expand Up @@ -285,7 +300,7 @@ export class SakaiRubricGrading extends RubricsElement {
});

const evaluation = {
evaluatorId: window.top.portal.user.id,
evaluatorId: getUserId(),
evaluatedItemId: this.evaluatedItemId,
evaluatedItemOwnerId: this.evaluatedItemOwnerId,
evaluatedItemOwnerType: this.group ? "GROUP" : "USER",
Expand All @@ -299,9 +314,14 @@ export class SakaiRubricGrading extends RubricsElement {
evaluation.metadata = this.evaluation.metadata;
}

this.saveEvaluation(evaluation, status);
}

saveEvaluation(evaluation) {

let url = "/rubrics-service/rest/evaluations";
if (this.evaluation && this.evaluation.id) url += `/${this.evaluation.id}`;
fetch(url, {
return fetch(url, {
body: JSON.stringify(evaluation),
credentials: "same-origin",
headers: {
Expand All @@ -310,7 +330,39 @@ export class SakaiRubricGrading extends RubricsElement {
"Content-Type": "application/json"
},
method: this.evaluation && this.evaluation.id ? "PATCH" : "POST"
}).then(r => r.json()).then(r => this.evaluation = r);
})
.then(r => {

if (r.ok) {
return r.json();
}
throw new Error("Server error while saving rubric evaluation");
})
.then(data => this.evaluation = data)
.catch(error => console.error(error));
}

deleteEvaluation() {

if (!this?.evaluation?.id) return;

const url = `/rubrics-service/rest/evaluations/${this.evaluation.id}`;
fetch(url, {
credentials: "same-origin",
headers: { "Authorization": this.token, },
method: "DELETE"
})
.then(r => {

if (r.ok) {
this.criteria.forEach(c => c.ratings.forEach(rat => rat.selected = false));
this.evaluation = { criterionOutcomes: [] };
this.requestUpdate();
} else {
throw new Error("Server error while deleting evaluation");
}
})
.catch(error => console.error(error));
}

getOverriddenClass(ovrdvl, selected) {
Expand Down Expand Up @@ -385,6 +437,30 @@ export class SakaiRubricGrading extends RubricsElement {
}
}

cancel() {

if (this.evaluation.status !== "DRAFT") return;

// Get the evaluation from session storage. This should be the last non draft evaluation that
// the server originally sent before the user started setting ratings. Save it baack to the
// server.
const evaluation = JSON.parse(sessionStorage.getItem(this.getStorageKey()));
if (evaluation) {
sessionStorage.removeItem(this.getStorageKey());

// Save cached evaluation and reset the criteria ready for rendering
this.saveEvaluation(evaluation).then(() => {

// Unset any ratings
this.criteria.forEach(c => c.ratings.forEach(r => r.selected = false));
// And set the original ones
this.decorateCriteria();
});
} else {
this.deleteEvaluation();
}
}

getAssociation() {

if (!this.toolId || !this.entityId || !this.token || !this.evaluatedItemId) {
Expand Down Expand Up @@ -419,7 +495,6 @@ export class SakaiRubricGrading extends RubricsElement {
}).done(data => {

this.evaluation = data._embedded.evaluations[0] || { criterionOutcomes: [] };
this.existingEvaluation = true;

this.rubric = rubric;

Expand Down Expand Up @@ -448,6 +523,5 @@ export class SakaiRubricGrading extends RubricsElement {
}
}

if (!customElements.get("sakai-rubric-grading")) {
customElements.define("sakai-rubric-grading", SakaiRubricGrading);
}
const tagName = "sakai-rubric-grading";
!customElements.get(tagName) && customElements.define(tagName, SakaiRubricGrading);
3 changes: 3 additions & 0 deletions webcomponents/tool/src/main/frontend/js/sakai-portal-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const getUserId = () => window.top?.portal?.user?.id || "";
export const getUserLocale = () => (window.top?.portal?.locale
|| window.top?.sakai?.locale?.userLocale || "en-US").replace("_", "-");
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { getUserId, getUserLocale } from "../js/sakai-portal-utils.js";

Deno.test("getUserId", () => {

const userId = "xyz";

window.top = {
portal: {
user: { id: userId }
},
};

assertEquals(getUserId(), userId);

window.top = {};

assertEquals(getUserId(), "");
});

0 comments on commit 23bef61

Please sign in to comment.