protected function saveQuizData(SwatForm $form) { if (!$this->response instanceof InquisitionResponse) { $class_name = SwatDBClassMap::get('CMEQuizResponse'); $this->response = new $class_name(); $this->response->account = $this->app->session->account->id; $this->response->inquisition = $this->quiz->id; $this->response->createdate = new SwatDate(); $this->response->createdate->toUTC(); } else { // delete old response values $this->response->values->delete(); } // set complete date $this->response->complete_date = new SwatDate(); $this->response->complete_date->toUTC(); // save response $this->response->setDatabase($this->app->db); $this->response->save(); // set new response values $wrapper = SwatDBClassMap::get('InquisitionResponseValueWrapper'); $response_values = new $wrapper(); $total_questions_with_answers = 0; $correct_answers = 0; foreach ($this->quiz->question_bindings as $question_binding) { $view = $this->question_views[$question_binding->id]; $response_value = $view->getResponseValue(); $response_value->response = $this->response->id; $response_values[] = $response_value; $question = $question_binding->question; $correct_option = $question->getInternalValue('correct_option'); $response_option = $response_value->getInternalValue('question_option'); if ($correct_option !== null) { $total_questions_with_answers++; if ($correct_option === $response_option) { $correct_answers++; } } } $this->response->values = $response_values; // save grade if ($total_questions_with_answers > 0) { $this->response->grade = $correct_answers / $total_questions_with_answers; $this->response->save(); } // save response values $this->response->values->save(); $this->saveEarnedCredit(); $this->sendCompletionEmail(); $this->addCompletionMessage(); // clear CME hours cache for this account $key = 'cme-hours-' . $this->app->session->account->id; $this->app->deleteCacheValue($key, 'cme-hours'); }