Skip to content

Commit

Permalink
fixup prevent unload of page for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dionysius committed Mar 11, 2019
1 parent d784b7a commit 0d9517f
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 37 deletions.
2 changes: 1 addition & 1 deletion amd/build/studentquiz.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 6 additions & 25 deletions amd/src/studentquiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ define(['jquery'], function($) {
initialise: function() {
// Ajax request POST on CLICK for add comment.
$('.studentquiz_behaviour .add_comment').off('click').on('click', function() {
disablePreventUnload();
var $comments = $(this).closest('.comments');
var $field = $comments.find('.add_comment_field');
var questionid = $field.attr('name').substr(1);
Expand All @@ -40,16 +39,14 @@ define(['jquery'], function($) {
{save: 'comment', cmid: cmid, questionid: questionid, sesskey: M.cfg.sesskey, text: $field.val()},
function() {
$field.val('');
$field.trigger("keyup");
getCommentList(questionid, $commentlist, cmid);
}
).always(function() {
ensurePreventUnload();
});
);
});

// Ajax request POST on CLICK for add rating.
$('.studentquiz_behaviour .rate .rating .rateable').off('click').on('click', function() {
disablePreventUnload();
var rate = $(this).attr('data-rate');
var $that = $(this);
var $cmidfield = $(this).closest('form').find('.cmid_field');
Expand All @@ -69,9 +66,7 @@ define(['jquery'], function($) {

$('.studentquiz_behaviour > .rate > .rate_error').addClass('hide');
}
).always(function() {
ensurePreventUnload();
});
);
});

// On CLICK check if student submitted result and has rated if not abort and show error for rating.
Expand Down Expand Up @@ -115,20 +110,17 @@ define(['jquery'], function($) {
* Binding action buttons after refresh comment list.
*/
function bindButtons() {
disablePreventUnload();
$('.studentquiz_behaviour .remove_action').off('click').on('click', function() {
var $cmidfield = $(this).closest('form').find('.cmid_field');
var cmid = $cmidfield.attr('value');
var questionid = $(this).attr('data-question_id');
var $commentlist = $(this).closest('.comments').children('.comment_list');
$.post($('#baseurlmoodle').val() + '/mod/studentquiz/remove.php',
$.post(M.cfg.wwwroot + '/mod/studentquiz/remove.php',
{id: $(this).attr('data-id'), cmid: cmid, sesskey: M.cfg.sesskey},
function() {
getCommentList(questionid, $commentlist, cmid);
}
).always(function() {
ensurePreventUnload();
});
);
});
}

Expand All @@ -139,7 +131,7 @@ define(['jquery'], function($) {
* @param {int} cmid course module id
*/
function getCommentList(questionid, $commentlist, cmid) {
var commentlisturl = $('#baseurlmoodle').val() + '/mod/studentquiz/comment_list.php?questionid=';
var commentlisturl = M.cfg.wwwroot + '/mod/studentquiz/comment_list.php?questionid=';
commentlisturl += questionid + '&cmid=' + cmid + '&sesskey=' + M.cfg.sesskey;
$.get(commentlisturl,
function(data) {
Expand All @@ -152,17 +144,6 @@ define(['jquery'], function($) {
/**
* Kindly ask to prevent leaving page when there's a unsaved comment
*
* It seems to be pretty browser specific how the beforeunload event is processed. Observations when event was set:
* Chrome: Allows POSTing data (via) but prevents navigating since they're also
* form submissions, and also prevents closing of the window and navigating using other links
* Firefox: Whatever you try to do, POSTing or navigating, always prevents it, even when returning nothing or void.
* Unknown what the expected behaviour by spec should be. All proposed solutions were all not working...
*
* That's why we need to carefully enable and disable the beforeunload event. Rule of thumb is, enable whenever
* the comment box is not empty, but disable whenever a quiz interaction button is pressed (add comment,
* quiz navigation)
* Whenever the action is done, it should be enabled again, if the comment textarea is still not empty.
*
* Note: Only in preview is the commenting visible without answering the question. If someone has filled the
* textarea and afterwards answers the question, he'll get the dialogue, which is fine.
*/
Expand Down
9 changes: 0 additions & 9 deletions attempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,6 @@
$transaction = $DB->start_delegated_transaction();
$questionusage->finish_question($slot);
$transaction->allow_commit();

$comment = optional_param('q'.$question->id, "", PARAM_TEXT);
if ($comment != "") {
$data = new \stdClass();
$data->userid = $USER->id;
$data->questionid = $question->id;
$data->comment = $comment;
mod_studentquiz_save_comment($data, $course, $cm);
}
}

// There should be no question data if he has already answered them, as the fields are disabled.
Expand Down
2 changes: 1 addition & 1 deletion comment_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@

$comments = mod_studentquiz_get_comments_with_creators($questionid);

echo mod_studentquiz_comment_renderer($comments, $userid, $anonymize, $ismoderator);
echo mod_studentquiz_comment_renderer($comments, $userid, $context->id, $anonymize, $ismoderator);
2 changes: 1 addition & 1 deletion lang/en/studentquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
$string['before_submission_end_date'] = 'This StudentQuiz closes for question submission on {$a}.';
$string['before_submission_start_date'] = 'Open for question submission from {$a}.';
$string['comment_column_name'] = 'Comments';
$string['comment_error_hint'] = 'You might want to save the comment first?';
$string['comment_error_hint'] = 'Do you want to save this comment first?';
$string['comment_help'] = 'Write a comment';
$string['comment_help_help'] = 'Write a comment to the question';
$string['comment_veryshort'] = 'C';
Expand Down

0 comments on commit 0d9517f

Please sign in to comment.