Add script to delete orphan questions from the platform BT#15534

pull/3173/head
Julio Montoya 7 years ago
parent f52b084e35
commit 3e052551a8
  1. 24
      main/exercise/question.class.php
  2. 26
      tests/scripts/delete_orphan_questions.php

@ -1340,10 +1340,20 @@ abstract class Question
* @author Olivier Brouckaert * @author Olivier Brouckaert
* *
* @param int $deleteFromEx - exercise ID if the question is only removed from one exercise * @param int $deleteFromEx - exercise ID if the question is only removed from one exercise
*
* @return bool
*/ */
public function delete($deleteFromEx = 0) 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_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_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 //update the question_order of each question to avoid inconsistencies
$sql = "SELECT exercice_id, question_order $sql = "SELECT exercice_id, question_order
FROM $TBL_EXERCISE_QUESTION 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); $res = Database::query($sql);
if (Database::num_rows($res) > 0) { if (Database::num_rows($res) > 0) {
@ -1366,7 +1376,7 @@ abstract class Question
$sql = "UPDATE $TBL_EXERCISE_QUESTION $sql = "UPDATE $TBL_EXERCISE_QUESTION
SET question_order = question_order-1 SET question_order = question_order-1
WHERE WHERE
c_id = $course_id AND c_id = $courseId AND
exercice_id = ".intval($row['exercice_id'])." AND exercice_id = ".intval($row['exercice_id'])." AND
question_order > ".$row['question_order']; question_order > ".$row['question_order'];
Database::query($sql); Database::query($sql);
@ -1375,21 +1385,21 @@ abstract class Question
} }
$sql = "DELETE FROM $TBL_EXERCISE_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); Database::query($sql);
$sql = "DELETE FROM $TBL_QUESTIONS $sql = "DELETE FROM $TBL_QUESTIONS
WHERE c_id = $course_id AND id = ".$id; WHERE c_id = $courseId AND id = ".$id;
Database::query($sql); Database::query($sql);
$sql = "DELETE FROM $TBL_REPONSES $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); Database::query($sql);
// remove the category of this question in the question_rel_category table // remove the category of this question in the question_rel_category table
$sql = "DELETE FROM $TBL_QUIZ_QUESTION_REL_CATEGORY $sql = "DELETE FROM $TBL_QUIZ_QUESTION_REL_CATEGORY
WHERE WHERE
c_id = $course_id AND c_id = $courseId AND
question_id = ".$id; question_id = ".$id;
Database::query($sql); Database::query($sql);

@ -0,0 +1,26 @@
<?php
/* For licensing terms, see /license.txt */
exit;
require_once __DIR__ . '/../../main/inc/global.inc.php';
$sql = 'SELECT iid, c_id, question
FROM c_quiz_question
WHERE iid not in (SELECT question_id from c_quiz_rel_question)
ORDER BY iid';
$result = Database::query($sql);
$data = Database::store_result($result);
$counter = 1;
$totalQuestions = count($data);
echo 'Questions to delete: '.$totalQuestions.PHP_EOL;
foreach ($data as $row) {
$courseInfo = api_get_course_info_by_id($row['c_id']);
$question = Question::read($row['iid'], $courseInfo);
if (empty($question->exerciseList)) {
$question->delete(1);
}
echo 'Deleting question '.$counter.'/'.$totalQuestions.' - #'.$row['iid'].PHP_EOL;
$counter++;
}
Loading…
Cancel
Save