media_question: Check for values before using them

Don't blindly use the indexes directly in the code, instead gracefully
fallback for non mandatory parameters and throw a Exception over the
mandatory one. Same for course_id set by ::updateCourse().
skala
Diego Escalante Urrelo 11 years ago
parent fd041dd927
commit 13f7e1765e
  1. 25
      main/exercice/media_question.class.php

@ -28,16 +28,33 @@ class MediaQuestion extends Question
public function saveMedia($params)
{
$table_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
$course_id = '';
$questionName = '';
if (!empty($this->course['real_id'])) {
$course_id = $this->course['real_id'];
} else {
throw new Exception('Missing $this->course info in MediaQuestion::saveMedia()');
}
if (isset($params['questionName'])) {
$questionName = $params['questionName'];
} else {
throw new Exception('Missing questionName in $params in MediaQuestion::saveMedia()');
}
$questionDescription = isset($params['questionDescription']) ? $params['questionDescription'] : '';
$new_params = array(
'c_id' => $this->course['real_id'],
'question' => $params['questionName'],
'description' => $params['questionDescription'],
'c_id' => $course_id,
'question' => $questionName,
'description' => $questionDescription,
'parent_id' => 0,
'type' => MEDIA_QUESTION
);
if (isset($this->id) && !empty($this->id)) {
Database::update($table_question, $new_params, array('iid = ? and c_id = ?' => array($this->id, $this->course['real_id'])));
Database::update($table_question, $new_params, array('iid = ? and c_id = ?' => array($this->id, $course_id)));
} else {
return Database::insert($table_question, $new_params);
}

Loading…
Cancel
Save