From 3e052551a83b9039db2ad58918772c0264292ca8 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Thu, 25 Apr 2019 15:02:54 +0200 Subject: [PATCH] Add script to delete orphan questions from the platform BT#15534 --- main/exercise/question.class.php | 24 +++++++++++++++------ tests/scripts/delete_orphan_questions.php | 26 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 tests/scripts/delete_orphan_questions.php diff --git a/main/exercise/question.class.php b/main/exercise/question.class.php index 775ed51d81..16638f9b4a 100755 --- a/main/exercise/question.class.php +++ b/main/exercise/question.class.php @@ -1340,10 +1340,20 @@ abstract class Question * @author Olivier Brouckaert * * @param int $deleteFromEx - exercise ID if the question is only removed from one exercise + * + * @return bool */ public function delete($deleteFromEx = 0) { - $course_id = api_get_course_int_id(); + if (empty($this->course)) { + return false; + } + + $courseId = $this->course['real_id']; + + if (empty($courseId)) { + return false; + } $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION); @@ -1357,7 +1367,7 @@ abstract class Question //update the question_order of each question to avoid inconsistencies $sql = "SELECT exercice_id, question_order FROM $TBL_EXERCISE_QUESTION - WHERE c_id = $course_id AND question_id = ".intval($id); + WHERE c_id = $courseId AND question_id = ".$id; $res = Database::query($sql); if (Database::num_rows($res) > 0) { @@ -1366,7 +1376,7 @@ abstract class Question $sql = "UPDATE $TBL_EXERCISE_QUESTION SET question_order = question_order-1 WHERE - c_id = $course_id AND + c_id = $courseId AND exercice_id = ".intval($row['exercice_id'])." AND question_order > ".$row['question_order']; Database::query($sql); @@ -1375,21 +1385,21 @@ abstract class Question } $sql = "DELETE FROM $TBL_EXERCISE_QUESTION - WHERE c_id = $course_id AND question_id = ".$id; + WHERE c_id = $courseId AND question_id = ".$id; Database::query($sql); $sql = "DELETE FROM $TBL_QUESTIONS - WHERE c_id = $course_id AND id = ".$id; + WHERE c_id = $courseId AND id = ".$id; Database::query($sql); $sql = "DELETE FROM $TBL_REPONSES - WHERE c_id = $course_id AND question_id = ".$id; + WHERE c_id = $courseId AND question_id = ".$id; Database::query($sql); // remove the category of this question in the question_rel_category table $sql = "DELETE FROM $TBL_QUIZ_QUESTION_REL_CATEGORY WHERE - c_id = $course_id AND + c_id = $courseId AND question_id = ".$id; Database::query($sql); diff --git a/tests/scripts/delete_orphan_questions.php b/tests/scripts/delete_orphan_questions.php new file mode 100644 index 0000000000..27291bbe52 --- /dev/null +++ b/tests/scripts/delete_orphan_questions.php @@ -0,0 +1,26 @@ +exerciseList)) { + $question->delete(1); + } + echo 'Deleting question '.$counter.'/'.$totalQuestions.' - #'.$row['iid'].PHP_EOL; + $counter++; +} \ No newline at end of file