MDL-57697 mod_survey: clean up data passing to js
authorDan Poltawski <dan@moodle.com>
Wed, 15 Feb 2017 09:48:42 +0000 (09:48 +0000)
committerDan Poltawski <dan@moodle.com>
Wed, 15 Feb 2017 09:51:16 +0000 (09:51 +0000)
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
mod/survey/amd/src/validation.js
mod/survey/lib.php
mod/survey/view.php

index 004399b..56bf846 100644 (file)
Binary files a/mod/survey/amd/build/validation.min.js and b/mod/survey/amd/build/validation.min.js differ
index f04de06..41f78dc 100644 (file)
@@ -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) {
index 38a6e7e..73f67a6 100644 (file)
@@ -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 ."</th>\n";
 
             $default = get_accesshide($strdefault);
-            echo "<td class=\"whitecell\"><label for=\"q$P$q->id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_D\" value=\"0\" checked=\"checked\" />$default</label></td>";
+            echo "<td class=\"whitecell\"><label for=\"q$P$q->id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_D\" value=\"0\" checked=\"checked\" data-survey-default=\"true\" />$default</label></td>";
 
             for ($i=1;$i<=$numoptions;$i++) {
                 $hiddentext = get_accesshide($options[$i-1]);
                 $id = "q$P" . $q->id . "_$i";
                 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
             }
-            $checklist["q$P$q->id"] = 0;
-
         } else {
             echo "<th scope=\"row\" class=\"optioncell\">";
             echo "<b class=\"qnumtopcell\">$qnum</b> &nbsp; ";
@@ -572,7 +570,7 @@ function survey_print_multi($question) {
             echo "<span class=\"option\">$q->text</span></th>\n";
 
             $default = get_accesshide($strdefault);
-            echo '<td class="whitecell"><label for="qP'.$q->id.'"><input type="radio" name="qP'.$q->id.'" id="qP'.$q->id.'" value="0" checked="checked" />'.$default.'</label></td>';
+            echo '<td class="whitecell"><label for="qP'.$q->id.'"><input type="radio" name="qP'.$q->id.'" id="qP'.$q->id.'" value="0" checked="checked" data-survey-default="true" />'.$default.'</label></td>';
 
 
             for ($i=1;$i<=$numoptions;$i++) {
@@ -589,16 +587,13 @@ function survey_print_multi($question) {
             echo "<span class=\"option\">$q->text</span></th>\n";
 
             $default = get_accesshide($strdefault);
-            echo '<td class="whitecell"><label for="q'. $q->id .'"><input type="radio" name="q'.$q->id. '" id="q'. $q->id .'" value="0" checked="checked" />'.$default.'</label></td>';
+            echo '<td class="whitecell"><label for="q'. $q->id .'"><input type="radio" name="q'.$q->id. '" id="q'. $q->id .'" value="0" checked="checked" data-survey-default="true" />'.$default.'</label></td>';
 
             for ($i=1;$i<=$numoptions;$i++) {
                 $hiddentext = get_accesshide($options[$i-1]);
                 $id = "q" . $q->id . "_$i";
                 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>";
             }
-
-            $checklist["qP$q->id"] = 0;
-            $checklist["q$q->id"] = 0;
         }
         echo "</tr>\n";
     }
index 1394106..884feb6 100644 (file)
@@ -151,9 +151,7 @@ echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
 $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 '<br />';
 echo '<input type="submit" class="btn btn-primary" value="'.get_string("clicktocontinue", "survey").'" />';