public function response_insert($sid, $section, $rid, $userid, $resume = false)
 {
     global $DB, $USER;
     $record = new stdClass();
     $record->submitted = time();
     if (empty($rid)) {
         // Create a uniqe id for this response.
         $record->survey_id = $sid;
         $record->username = $userid;
         $rid = $DB->insert_record('questionnaire_response', $record);
     } else {
         $record->id = $rid;
         $DB->update_record('questionnaire_response', $record);
     }
     if ($resume) {
         // Log this saved response.
         // Needed for the event logging.
         $context = context_module::instance($this->cm->id);
         $anonymous = $this->respondenttype == 'anonymous';
         $params = array('context' => $context, 'courseid' => $this->course->id, 'relateduserid' => $userid, 'anonymous' => $anonymous, 'other' => array('questionnaireid' => $this->id));
         $event = \mod_questionnaire\event\attempt_saved::create($params);
         $event->trigger();
     }
     if (!empty($this->questionsbysec[$section])) {
         foreach ($this->questionsbysec[$section] as $question) {
             // NOTE *** $val really should be a value obtained from the caller or somewhere else.
             // Note that "optional_param" accepting arrays is deprecated for optional_param_array.
             if ($question->response_table == 'resp_multiple') {
                 $val = optional_param_array('q' . $question->id, '', PARAM_RAW);
             } else {
                 $val = optional_param('q' . $question->id, '', PARAM_RAW);
             }
             $question->insert_response($rid, $val);
         }
     }
     return $rid;
 }
 private function response_insert($sid, $section, $rid, $userid, $resume = false)
 {
     global $DB, $USER;
     $record = new object();
     $record->submitted = time();
     if (empty($rid)) {
         // Create a uniqe id for this response.
         $record->survey_id = $sid;
         $record->username = $userid;
         $rid = $DB->insert_record('questionnaire_response', $record);
     } else {
         $record->id = $rid;
         $DB->update_record('questionnaire_response', $record);
     }
     if ($resume) {
         // Log this saved response.
         // Needed for the event logging.
         $context = context_module::instance($this->cm->id);
         $anonymous = $this->respondenttype == 'anonymous';
         $params = array('context' => $context, 'courseid' => $this->course->id, 'relateduserid' => $userid, 'anonymous' => $anonymous, 'other' => array('questionnaireid' => $this->id));
         $event = \mod_questionnaire\event\attempt_saved::create($params);
         $event->trigger();
     }
     if (!empty($this->questionsbysec[$section])) {
         foreach ($this->questionsbysec[$section] as $question) {
             $question->insert_response($rid);
         }
     }
     return $rid;
 }