Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reset functionality #6

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 85 additions & 10 deletions js/summary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
H5P.Summary = (function ($, Question) {
H5P.Summary = (function ($, Question, JoubelUI) {

function Summary(options, contentId, contentData) {
if (!(this instanceof H5P.Summary)) {
Expand All @@ -19,7 +19,7 @@ H5P.Summary = (function ($, Question) {
return element.summary !== undefined;
});
}

if (contentData && contentData.previousState !== undefined &&
contentData.previousState.progress !== undefined &&
contentData.previousState.answers) {
Expand All @@ -44,7 +44,7 @@ H5P.Summary = (function ($, Question) {
}
}
var that = this;
this.options = H5P.jQuery.extend({}, {
this.options = H5P.jQuery.extend(true, {}, {
response: {
scorePerfect:
{
Expand Down Expand Up @@ -72,23 +72,32 @@ H5P.Summary = (function ($, Question) {
intro: "Choose the correct statement.",
solvedLabel: "Solved:",
scoreLabel: "Wrong answers:",
behavior: {
enableRetry: false
},
postUserStatistics: (H5P.postUserStatistics === true)
}, options);

this.summaries = that.options.summaries;

// Required questiontype contract function
this.showSolutions = function() {
// intentionally left blank, no solution view exists
this.getAnswerGiven = function () {
return this.progress > 0;
};

// Required questiontype contract function
this.getScore = function() {
return this.getMaxScore() - this.countErrors();
};

// Required questiontype contract function
this.getMaxScore = function() {
return this.summaries.length;
};

this.getScore = function() {
return this.getMaxScore() - this.countErrors();
// Required questiontype contract function
this.showSolutions = function() {
// intentionally left blank, no solution view exists
};

this.getTitle = function() {
Expand All @@ -113,6 +122,26 @@ H5P.Summary = (function ($, Question) {
Summary.prototype.registerDomElements = function () {
// Register task content area
this.setContent(this.createQuestion());

// Register retry button to h5p question
var self = this;

// Parameters are undefined when used wihtin another H5P content type
if (self.summaries === undefined){
return;
}

// Attach the retry buton if it is enabled in the semantics
if (this.options.behavior.enableRetry) {

// Add JoubelUI retry button using the h5p-question module
this.addButton('try-again', 'retry', function () {

// Remove button after is has been clicked
self.hideButton('try-again');
self.resetTask();
}, self.progress === self.summaries.length);
}
};

// Function for attaching the multichoice to a DOM element.
Expand Down Expand Up @@ -189,6 +218,12 @@ H5P.Summary = (function ($, Question) {
* Used when alt was selected with keyboard.
*/
var selectedAlt = function ($el, setFocus) {

// Don't do anything if the option has already been clicked
if ($el.hasClass("summary-failed")){
return;
}

that.triggerXAPI('interacted');
var node_id = Number($el.attr('data-bit'));
var panel_id = Number($el.parent().data('panel'));
Expand Down Expand Up @@ -217,7 +252,6 @@ H5P.Summary = (function ($, Question) {
setTimeout(function () {
$answer.css({backgroundColor: ''});
}, 1);
//$answer.animate({backgroundColor: '#eee'}, 'slow');

var panel = parseInt($el.parent().attr('data-panel'));
var $curr_panel = $('.h5p-panel:eq(' + panel + ')', that.$myDom);
Expand Down Expand Up @@ -273,7 +307,9 @@ H5P.Summary = (function ($, Question) {
// Hide intermediate evaluation
$evaluation_content.html(that.options.resultLabel);

// Show results and retry button
that.do_final_evaluation($summary_container, $options, $summary_list, that.score);
that.showButton('try-again');
}
that.trigger('resize');
});
Expand All @@ -283,7 +319,6 @@ H5P.Summary = (function ($, Question) {
}
else {
// Remove event handler (prevent repeated clicks) and mouseover effect
$el.off('click');
$el.addClass('summary-failed');
$el.removeClass('summary-claim-unclicked');

Expand Down Expand Up @@ -436,7 +471,47 @@ H5P.Summary = (function ($, Question) {
* Used for contracts.
*/
Summary.prototype.resetTask = function () {
// Summary is not yet able to Reset itself

// Reset starting attributes
this.offset = 0;
this.score = 0;
this.progress = 0;
this.error_counts = [];
this.answers = [];

// Reset the progress bar
this.$myDom.find(".summary-evaluation").find(".summary-progress").html(this.options.solvedLabel + ' 0/' + this.summaries.length);

// Reset the summary container that holds the answers
this.$myDom.find(".summary-container").removeClass("has-results");
this.$myDom.find(".summary-container ul").empty();
this.$myDom.find(".summary-score").css("display", "none");

// Reset the data for each of the divs
this.$myDom.find('li[role="button"]').each(function() {
$(this).removeClass("summary-failed").addClass("summary-claim-unclicked");
$(this).attr("tabindex", 0);
$(this).on('click');
});

for (var i = 0; i < this.options.summaries.length; i++) {
$('.h5p-panel:eq(' + (i) + ')', this.$myDom).removeClass("panel-disabled");
}

// Reset the html statement
this.$myDom.find(".summary-evaluation-content").html("Choose the correct statement.");

//Set the tip for the first question
var first_tip = this.summaries[0].tip;
if (first_tip){
this.$myDom.find(".summary-evaluation-content").append(H5P.JoubelUI.createTip(first_tip));
}

// Remove the feedback bar
this.setFeedback();

// Show the first question and its answers
$('.h5p-panel:eq(' + (0) + ')', this.$myDom).fadeIn(1000);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Test your users with fill in the summary tasks.",
"majorVersion": 1,
"minorVersion": 5,
"patchVersion": 0,
"patchVersion": 1,
"runnable": 1,
"machineName": "H5P.Summary",
"license": "MIT",
Expand Down Expand Up @@ -46,4 +46,4 @@
"minorVersion": 0
}
]
}
}
16 changes: 16 additions & 0 deletions semantics.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,21 @@
"default": "You got @score of @total statements (@percent %) correct on your first try.",
"description": "Available variables: @score, @total, @percent. Example: You got @score of @total statements (@percent %) correct.",
"common": true
},
{
"name": "behaviour",
"type": "group",
"label": "Behavioural settings.",
"description": "These options will let you control how the task behaves.",
"optional": true,
"fields": [
{
"label": "Enable \"Retry\"",
"name": "enableRetry",
"type": "boolean",
"default": false,
"optional": true
}
]
}
]