You are here

function question_delete_activity in Quiz 6.6

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

All question categories and their questions are deleted for this activity.

Parameters

object $cm the course module object representing the activity:

boolean $feedback to specify if the process must output a summary of its work:

Return value

boolean

File

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

Code

function question_delete_activity($cm, $feedback = true) {

  //To store feedback to be showed at the end of the process
  $feedbackdata = array();

  //Cache some strings
  $strcatdeleted = get_string('unusedcategorydeleted', 'quiz');
  $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
  if ($categoriesmods = get_records('question_categories', 'contextid', $modcontext->id, 'parent', 'id, parent, name')) {

    //Sort categories following their tree (parent-child) relationships

    //this will make the feedback more readable
    $categoriesmods = sort_categories_by_tree($categoriesmods);
    foreach ($categoriesmods as $category) {

      //Delete it completely (questions and category itself)

      //deleting questions
      if ($questions = get_records("question", "category", $category->id)) {
        foreach ($questions as $question) {
          delete_question($question->id);
        }
        delete_records("question", "category", $category->id);
      }

      //delete the category
      delete_records('question_categories', 'id', $category->id);

      //Fill feedback
      $feedbackdata[] = array(
        $category->name,
        $strcatdeleted,
      );
    }

    //Inform about changes performed if feedback is enabled
    if ($feedback) {
      $table = new stdClass();
      $table->head = array(
        get_string('category', 'quiz'),
        get_string('action'),
      );
      $table->data = $feedbackdata;
      print_table($table);
    }
  }
  return true;
}