From e6a85226da6107198d78707db6264575bca3640f Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 24 Oct 2012 15:15:31 +0200 Subject: [PATCH] Fixing exercise wrong question copy see #5668 --- .../classes/CourseBuilder.class.php | 53 +++++++++++++------ .../classes/CourseRestorer.class.php | 32 ++++++++--- main/exercice/question.class.php | 1 + 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/main/coursecopy/classes/CourseBuilder.class.php b/main/coursecopy/classes/CourseBuilder.class.php index a5e43f7226..e1f1981f64 100644 --- a/main/coursecopy/classes/CourseBuilder.class.php +++ b/main/coursecopy/classes/CourseBuilder.class.php @@ -438,13 +438,27 @@ class CourseBuilder { $db_result = Database::query($sql); while ($obj = Database::fetch_object($db_result)) { if (strlen($obj->sound) > 0) { - $sql = "SELECT id FROM ".$table_doc." WHERE c_id = $course_id AND path = '/audio/".$obj->sound."'"; + $sql = "SELECT id FROM $table_doc WHERE c_id = $course_id AND path = '/audio/".$obj->sound."'"; $res = Database::query($sql); $doc = Database::fetch_object($res); $obj->sound = $doc->id; } - $quiz = new Quiz($obj->id, $obj->title, $obj->description, $obj->random, $obj->type, $obj->active, $obj->sound, $obj->max_attempt, - $obj->results_disabled, $obj->access_condition, $obj->start_time, $obj->end_time, $obj->feedback_type, $obj->random_answers, $obj->expired_time, $obj->session_id); + $quiz = new Quiz( $obj->id, + $obj->title, + $obj->description, + $obj->random, + $obj->type, + $obj->active, + $obj->sound, + $obj->max_attempt, + $obj->results_disabled, + $obj->access_condition, + $obj->start_time, + $obj->end_time, + $obj->feedback_type, + $obj->random_answers, + $obj->expired_time, + $obj->session_id); $sql = 'SELECT * FROM '.$table_rel.' WHERE c_id = '.$course_id.' AND exercice_id = '.$obj->id; $db_result2 = Database::query($sql); while ($obj2 = Database::fetch_object($db_result2)) { @@ -473,9 +487,9 @@ class CourseBuilder { $table_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER); // Building normal tests. - $sql = "SELECT * FROM $table_que WHERE c_id = $course_id "; - + $sql = "SELECT * FROM $table_que WHERE c_id = $course_id "; $db_result = Database::query($sql); + while ($obj = Database::fetch_object($db_result)) { $question = new QuizQuestion($obj->id, $obj->question, $obj->description, $obj->ponderation, $obj->type, $obj->position, $obj->picture, $obj->level, $obj->extra); $sql = 'SELECT * FROM '.$table_ans.' WHERE c_id = '.$course_id.' AND question_id = '.$obj->id; @@ -489,12 +503,14 @@ class CourseBuilder { // Building a fictional test for collecting orphan questions. $build_orphan_questions = !empty($_POST['recycle_option']); // When a course is emptied this option should be activated (true). - $sql = "SELECT questions.* FROM $table_que as questions + $sql = "SELECT questions.* + FROM $table_que as questions LEFT JOIN $table_rel as quizz_questions ON (questions.id = quizz_questions.question_id AND questions.c_id = $course_id AND quizz_questions.c_id = $course_id) LEFT JOIN $table_qui as exercices ON (exercice_id = exercices.id AND exercices.c_id = $course_id AND questions.c_id = $course_id) - WHERE quizz_questions.exercice_id IS NULL OR exercices.active = -1"; // active = -1 means "deleted" test. + WHERE questions.c_id = $course_id AND + (quizz_questions.exercice_id IS NULL OR exercices.active = -1) "; // active = -1 means "deleted" test. $db_result = Database::query($sql); if (Database::num_rows($db_result) > 0) { @@ -528,14 +544,17 @@ class CourseBuilder { $course_id = api_get_course_int_id(); - $sql = 'SELECT * FROM '.$table_que.' as questions LEFT JOIN '.$table_rel.' as quizz_questions - ON questions.id=quizz_questions.question_id LEFT JOIN '.$table_qui.' as exercices + $sql = 'SELECT * + FROM '.$table_que.' as questions + LEFT JOIN '.$table_rel.' as quizz_questions + ON questions.id=quizz_questions.question_id + LEFT JOIN '.$table_qui.' as exercices ON exercice_id=exercices.id - WHERE questions.c_id = '.$course_id.' AND + WHERE questions.c_id = '.$course_id.' AND quizz_questions.c_id = '.$course_id.' AND exercices.c_id = '.$course_id.' AND - quizz_questions.exercice_id IS NULL OR - exercices.active = -1'; + (quizz_questions.exercice_id IS NULL OR + exercices.active = -1)'; $db_result = Database::query($sql); if (Database::num_rows($db_result) > 0) { $orphan_questions = new Quiz(-1, get_lang('OrphanQuestions', ''), '', 0, 0, 1, '', 0); // Tjis is the fictional test for collecting orphan questions. @@ -925,8 +944,12 @@ class CourseBuilder { //$sql = 'SELECT * FROM '.$table_thematic_plan.' WHERE c_id = '.$course_id.' AND thematic_id = '.$row['id']; $sql = "SELECT tp.* - FROM $table_thematic_plan tp INNER JOIN $table_thematic t ON (t.id=tp.thematic_id) - WHERE t.c_id = $course_id AND tp.c_id = $course_id AND thematic_id = {$row['id']} AND tp.id IN (".implode(', ', $thematic_plan_id_list).") "; + FROM $table_thematic_plan tp + INNER JOIN $table_thematic t ON (t.id=tp.thematic_id) + WHERE t.c_id = $course_id AND + tp.c_id = $course_id AND + thematic_id = {$row['id']} AND + tp.id IN (".implode(', ', $thematic_plan_id_list).") "; $result = Database::query($sql); @@ -959,4 +982,4 @@ class CourseBuilder { $this->course->add_resource($obj); } } -} +} \ No newline at end of file diff --git a/main/coursecopy/classes/CourseRestorer.class.php b/main/coursecopy/classes/CourseRestorer.class.php index 5e9e6c19f0..e2e1287a95 100644 --- a/main/coursecopy/classes/CourseRestorer.class.php +++ b/main/coursecopy/classes/CourseRestorer.class.php @@ -1153,6 +1153,7 @@ class CourseRestorer $this->course->resources[RESOURCE_QUIZ][$id]->destination_id = $new_id; $order = 0; + foreach ($quiz->question_ids as $index => $question_id) { $qid = $this->restore_quiz_question($question_id); $question_order = $quiz->question_orders[$index] ? $quiz->question_orders[$index] : ++$order; @@ -1168,8 +1169,8 @@ class CourseRestorer */ function restore_quiz_question($id) { $resources = $this->course->resources; - $question = $resources[RESOURCE_QUIZQUESTION][$id]; - + $question = $resources[RESOURCE_QUIZQUESTION][$id]; + $new_id = 0; if (is_object($question)) { @@ -1203,7 +1204,16 @@ class CourseRestorer $t[$answer['position']] = $answer; } foreach ($t as $index => $answer) { - $sql = "INSERT INTO ".$table_ans." SET c_id = ".$this->destination_course_id." , id= '".$index."',question_id = '".$new_id."', answer = '".self::DBUTF8escapestring($answer['answer'])."', correct = '".$answer['correct']."', comment = '".self::DBUTF8escapestring($answer['comment'])."', ponderation='".$answer['ponderation']."', position = '".$answer['position']."', hotspot_coordinates = '".$answer['hotspot_coordinates']."', hotspot_type = '".$answer['hotspot_type']."'"; + $sql = "INSERT INTO ".$table_ans." SET + c_id = ".$this->destination_course_id." , + id = '".$index."', question_id = '".$new_id."', + answer = '".self::DBUTF8escapestring($answer['answer'])."', + correct = '".$answer['correct']."', + comment = '".self::DBUTF8escapestring($answer['comment'])."', + ponderation = '".$answer['ponderation']."', + position = '".$answer['position']."', + hotspot_coordinates = '".$answer['hotspot_coordinates']."', + hotspot_type = '".$answer['hotspot_type']."'"; Database::query($sql); } } else { @@ -1213,7 +1223,17 @@ class CourseRestorer $answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['answer'], $this->course->code, $this->course->destination_path); $answer['comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['comment'], $this->course->code, $this->course->destination_path); - $sql = "INSERT INTO ".$table_ans." SET c_id = ".$this->destination_course_id." , id= '". ($index +1)."',question_id = '".$new_id."', answer = '".self::DBUTF8escapestring($answer['answer'])."', correct = '".$answer['correct']."', comment = '".self::DBUTF8escapestring($answer['comment'])."', ponderation='".$answer['ponderation']."', position = '".$answer['position']."', hotspot_coordinates = '".$answer['hotspot_coordinates']."', hotspot_type = '".$answer['hotspot_type']."'"; + $sql = "INSERT INTO ".$table_ans." SET + c_id = ".$this->destination_course_id." , + id = '". ($index + 1)."', + question_id = '".$new_id."', + answer = '".self::DBUTF8escapestring($answer['answer'])."', + correct = '".$answer['correct']."', + comment = '".self::DBUTF8escapestring($answer['comment'])."', + ponderation = '".$answer['ponderation']."', + position = '".$answer['position']."', + hotspot_coordinates = '".$answer['hotspot_coordinates']."', + hotspot_type = '".$answer['hotspot_type']."'"; Database::query($sql); } } @@ -1234,6 +1254,7 @@ class CourseRestorer } $new_answers = Database::select('id, correct', $table_ans, array('WHERE' => array('question_id = ? AND c_id = ? '=>array($new_id, $this->destination_course_id)))); foreach ($new_answers as $answer_item) { + $params = array(); $params['correct'] = $old_option_ids[$answer_item['correct']]; $question_option_id = Database::update($table_ans, $params, array('id = ? AND c_id = ? '=> array($answer_item['id'], $this->destination_course_id))); } @@ -1959,8 +1980,7 @@ class CourseRestorer } function DBUTF8_array($array) { - if (UTF8_CONVERT) { - + if (UTF8_CONVERT) { foreach ($array as &$item) { $item = utf8_encode($item); } diff --git a/main/exercice/question.class.php b/main/exercice/question.class.php index bc82dcd9fc..1190358173 100644 --- a/main/exercice/question.class.php +++ b/main/exercice/question.class.php @@ -1171,6 +1171,7 @@ abstract class Question if(is_array($fck_config)){ $editor_config = array_merge($editor_config, $fck_config); } + if(!api_is_allowed_to_edit(null,true)) $editor_config['UserStatus'] = 'student'; $form->addElement('advanced_settings','