From bdad6c4a9931d70dba72e75e93d0d9dcc8bbe684 Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Wed, 15 Feb 2017 09:48:42 +0000 Subject: [PATCH] MDL-57697 mod_survey: clean up data passing to js Get rid of the ugly huge array passed to JS as it went over our debug limit. (It's a cleaner solution and we get rid of one globals hack.. I just was avoiding doing to many changes in the original patch, because this whole interface is not very usable and needs rethinking) --- mod/survey/amd/build/validation.min.js | 2 +- mod/survey/amd/src/validation.js | 13 +++---------- mod/survey/lib.php | 13 ++++--------- mod/survey/view.php | 8 +------- 4 files changed, 9 insertions(+), 27 deletions(-) diff --git a/mod/survey/amd/build/validation.min.js b/mod/survey/amd/build/validation.min.js index 004399b74d8..56bf8462a2e 100644 --- a/mod/survey/amd/build/validation.min.js +++ b/mod/survey/amd/build/validation.min.js @@ -1 +1 @@ -define(["jquery","core/str","core/modal_factory","core/notification"],function(a,b,c,d){return{ensureRadiosChosen:function(e,f){var g=b.get_strings([{key:"error",component:"moodle"},{key:"questionsnotanswered",component:"survey"}]).then(function(a){return c.create({type:c.types.CANCEL,title:a[0],body:a[1]})})["catch"](d.exception),h=a("#"+e);h.submit(function(a){var b=!1;return f.forEach(function(a){var c=h.find('input:radio[name="'+a.name+'"]:checked');c.val()==a["default"]&&(b=!0)}),!b||(a.preventDefault(),g.then(function(a){a.show()}),!1)})}}}); \ No newline at end of file +define(["jquery","core/str","core/modal_factory","core/notification"],function(a,b,c,d){return{ensureRadiosChosen:function(e){var f=b.get_strings([{key:"error",component:"moodle"},{key:"questionsnotanswered",component:"survey"}]).then(function(a){return c.create({type:c.types.CANCEL,title:a[0],body:a[1]})})["catch"](d.exception),g=a("#"+e);g.submit(function(a){return 0===g.find('input:radio[data-survey-default="true"]:checked').length||(a.preventDefault(),f.then(function(a){a.show()}),!1)})}}}); \ No newline at end of file diff --git a/mod/survey/amd/src/validation.js b/mod/survey/amd/src/validation.js index f04de066643..41f78dc0fb7 100644 --- a/mod/survey/amd/src/validation.js +++ b/mod/survey/amd/src/validation.js @@ -31,7 +31,7 @@ define(['jquery', 'core/str', 'core/modal_factory', 'core/notification'], functi * @param {String} formid HTML id of form * @param {Array} questions */ - ensureRadiosChosen: function(formid, questions) { + ensureRadiosChosen: function(formid) { // Prepare modal for display in case of problems. var modalPromise = Str.get_strings([ {key: 'error', component: 'moodle'}, @@ -46,15 +46,8 @@ define(['jquery', 'core/str', 'core/modal_factory', 'core/notification'], functi var form = $('#' + formid); form.submit(function(e) { - var error = false; - questions.forEach(function(question) { - var checkedResponse = form.find('input:radio[name="' + question.name + '"]:checked'); - if (checkedResponse.val() == question.default) { - error = true; - } - }); - - if (error) { + // Look for unanswered questions.. + if (form.find('input:radio[data-survey-default="true"]:checked').length !== 0) { e.preventDefault(); // Display the modal error. modalPromise.then(function(modal) { diff --git a/mod/survey/lib.php b/mod/survey/lib.php index 38a6e7e4378..73f67a68d1e 100644 --- a/mod/survey/lib.php +++ b/mod/survey/lib.php @@ -497,7 +497,7 @@ function survey_shorten_name ($name, $numwords) { * @param object $question */ function survey_print_multi($question) { - global $USER, $DB, $qnum, $checklist, $DB, $OUTPUT; //TODO: this is sloppy globals abuse + global $USER, $DB, $qnum, $DB, $OUTPUT; //TODO: this is sloppy globals abuse $stripreferthat = get_string("ipreferthat", "survey"); $strifoundthat = get_string("ifoundthat", "survey"); @@ -555,15 +555,13 @@ function survey_print_multi($question) { echo $q->text ."\n"; $default = get_accesshide($strdefault); - echo ""; + echo ""; for ($i=1;$i<=$numoptions;$i++) { $hiddentext = get_accesshide($options[$i-1]); $id = "q$P" . $q->id . "_$i"; echo ""; } - $checklist["q$P$q->id"] = 0; - } else { echo ""; echo "$qnum   "; @@ -572,7 +570,7 @@ function survey_print_multi($question) { echo "$q->text\n"; $default = get_accesshide($strdefault); - echo ''; + echo ''; for ($i=1;$i<=$numoptions;$i++) { @@ -589,16 +587,13 @@ function survey_print_multi($question) { echo "$q->text\n"; $default = get_accesshide($strdefault); - echo ''; + echo ''; for ($i=1;$i<=$numoptions;$i++) { $hiddentext = get_accesshide($options[$i-1]); $id = "q" . $q->id . "_$i"; echo ""; } - - $checklist["qP$q->id"] = 0; - $checklist["q$q->id"] = 0; } echo "\n"; } diff --git a/mod/survey/view.php b/mod/survey/view.php index 139410692dc..884feb6c8e1 100644 --- a/mod/survey/view.php +++ b/mod/survey/view.php @@ -151,9 +151,7 @@ echo '
'. get_string('allquestionrequireanswer', 'survey'). '
'; $questions = survey_get_questions($survey); global $qnum; // TODO: ugly globals hack for survey_print_*(). -global $checklist; // TODO: ugly globals hack for survey_print_*(). $qnum = 0; -$checklist = array(); foreach ($questions as $question) { if ($question->type >= 0) { @@ -175,11 +173,7 @@ if (!is_enrolled($context)) { exit; } -$questions = array(); -foreach ($checklist as $question => $default) { - $questions[] = array('name' => $question, 'default' => $default); -} -$PAGE->requires->js_call_amd('mod_survey/validation', 'ensureRadiosChosen', array('surveyform', $questions)); +$PAGE->requires->js_call_amd('mod_survey/validation', 'ensureRadiosChosen', array('surveyform')); echo '
'; echo '';