MDL-51629 mod_survey: New WS mod_survey_submit_answers
[moodle.git] / mod / survey / save.php
index 4b3580e..0fff2c7 100644 (file)
         exit;
     }
 
-
-// Sort through the data and arrange it
-// This is necessary because some of the questions
-// may have two answers, eg Question 1 -> 1 and P1
-
-    $answers = array();
-
-    foreach ($formdata as $key => $val) {
-        if ($key <> "userid" && $key <> "id") {
-            if ( substr($key,0,1) == "q") {
-                $key = clean_param(substr($key,1), PARAM_ALPHANUM);   // keep everything but the 'q', number or Pnumber
-            }
-            if ( substr($key,0,1) == "P") {
-                $realkey = (int) substr($key,1);
-                $answers[$realkey][1] = $val;
-            } else {
-                $answers[$key][0] = $val;
-            }
-        }
-    }
-
-
-// Now store the data.
-
-    $timenow = time();
-    foreach ($answers as $key => $val) {
-        if ($key != 'sesskey') {
-            $newdata = new stdClass();
-            $newdata->time = $timenow;
-            $newdata->userid = $USER->id;
-            $newdata->survey = $survey->id;
-            $newdata->question = $key;
-            if (!empty($val[0])) {
-                $newdata->answer1 = $val[0];
-            } else {
-                $newdata->answer1 = "";
-            }
-            if (!empty($val[1])) {
-                $newdata->answer2 = $val[1];
-            } else {
-                $newdata->answer2 = "";
-            }
-
-            $DB->insert_record("survey_answers", $newdata);
-        }
-    }
+    survey_save_answers($survey, $formdata, $course, $context);
 
     $params = array(
         'context' => $context,