delete_deleted_exercises_with_questions.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. exit;
  4. require_once __DIR__ . '/../../main/inc/global.inc.php';
  5. $sql = 'SELECT iid, c_id, title
  6. FROM c_quiz
  7. WHERE active = -1
  8. ORDER BY iid';
  9. $result = Database::query($sql);
  10. $data = Database::store_result($result);
  11. $counter = 1;
  12. $total = count($data);
  13. echo 'Exercises to delete: '.$total.PHP_EOL;
  14. foreach ($data as $row) {
  15. $id = $row['iid'];
  16. $courseId = $row['c_id'];
  17. //$exercise = new Exercise($courseId);
  18. //$exercise->read($id);
  19. $courseInfo = api_get_course_info_by_id($row['c_id']);
  20. $sql = "SELECT question_id, c_id
  21. FROM c_quiz_rel_question
  22. WHERE exercice_id = $id
  23. ORDER BY iid";
  24. $result = Database::query($sql);
  25. $questions = Database::store_result($result);
  26. $totalQuestions = count($questions);
  27. echo PHP_EOL.'-------';
  28. echo PHP_EOL.'Deleting exercise "'.$row['title'].'" - #'.$row['iid'].PHP_EOL;
  29. echo '-------'.PHP_EOL.PHP_EOL;
  30. if (!empty($questions)) {
  31. $counter = 1;
  32. foreach ($questions as $questionData) {
  33. $questionId = $questionData['question_id'];
  34. // Check if question is used in another exercise:
  35. $sql = "SELECT count(iid)
  36. FROM c_quiz_rel_question
  37. WHERE exercice_id != $id AND question_id = $questionId";
  38. $result = Database::query($sql);
  39. $dataQuestion = Database::fetch_array($result);
  40. $count = $dataQuestion['count'];
  41. if (empty($count)) {
  42. $question = Question::read($questionId, $courseInfo);
  43. $question->delete();
  44. echo 'Deleting question '.$counter.'/'.$totalQuestions.' - #'.$questionId.PHP_EOL;
  45. } else {
  46. echo 'Cannot delete question, it\'s been used by another exercise'.$counter.'/'.$totalQuestions.' - #'.$questionId.PHP_EOL;
  47. }
  48. $counter++;
  49. }
  50. }
  51. $sql = "DELETE FROM c_quiz WHERE iid = $id";
  52. $result = Database::query($sql);
  53. $sql = "DELETE FROM c_item_property WHERE ref = $id AND c_id = $courseId and tool = 'quiz' ";
  54. $result = Database::query($sql);
  55. }