You are here

function delete_question in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/lib/questionlib.php \delete_question()

Deletes question and all associated data from the database

It will not delete a question if it is used by an activity module

Parameters

object $question The question being deleted:

3 calls to delete_question()
question_delete_activity in includes/moodle/lib/questionlib.php
All question categories and their questions are deleted for this activity.
question_delete_course in includes/moodle/lib/questionlib.php
All question categories and their questions are deleted for this course.
question_delete_course_category in includes/moodle/lib/questionlib.php
Category is about to be deleted, 1/ All question categories and their questions are deleted for this course category. 2/ All questions are moved to new category

File

includes/moodle/lib/questionlib.php, line 451

Code

function delete_question($questionid) {
  global $QTYPES;
  if (!($question = get_record('question', 'id', $questionid))) {

    // In some situations, for example if this was a child of a
    // Cloze question that was previously deleted, the question may already
    // have gone. In this case, just do nothing.
    return;
  }

  // Do not delete a question if it is used by an activity module
  if (count(question_list_instances($questionid))) {
    return;
  }

  // delete questiontype-specific data
  question_require_capability_on($question, 'edit');
  if ($question) {
    if (isset($QTYPES[$question->qtype])) {
      $QTYPES[$question->qtype]
        ->delete_question($questionid);
    }
  }
  else {
    echo "Question with id {$questionid} does not exist.<br />";
  }
  if ($states = get_records('question_states', 'question', $questionid)) {
    $stateslist = implode(',', array_keys($states));

    // delete questiontype-specific data
    foreach ($QTYPES as $qtype) {
      $qtype
        ->delete_states($stateslist);
    }
  }

  // delete entries from all other question tables
  // It is important that this is done only after calling the questiontype functions
  delete_records("question_answers", "question", $questionid);
  delete_records("question_states", "question", $questionid);
  delete_records("question_sessions", "questionid", $questionid);

  // Now recursively delete all child questions
  if ($children = get_records('question', 'parent', $questionid)) {
    foreach ($children as $child) {
      if ($child->id != $questionid) {
        delete_question($child->id);
      }
    }
  }

  // Finally delete the question record itself
  delete_records('question', 'id', $questionid);
  return;
}