}
-
/// FUNCTIONS //////////////////////////////////////////////////////
+/**
+ * Returns an array with all the course modules that use this question
+ *
+ * @param object $questionid
+ */
+function question_whereused($questionid) {
+ $instances = array();
+ $modules = get_records('modules');
+ foreach ($modules as $module) {
+ $fn = $module->name.'_question_whereused';
+ if (function_exists($fn)) {
+ $instances[] = $fn($questionid);
+ }
+ }
+ return $instances;
+}
/**
* Deletes question and all associated data from the database
*
- * TODO: remove quiz dependence
- *
+ * It will not delete a question if it is used by an activity module
* @param object $question The question being deleted
*/
-function delete_question($question) {
+function delete_question($questionid) {
global $QTYPES;
+
+ // Do not delete a question if it is used by an activity module
+ if (count(question_whereused($questionid))) {
+ return;
+ }
+
+ // delete questiontype-specific data
if (isset($QTYPES[$question->qtype])) {
- $QTYPES[$question->qtype]->delete_question($question);
- } else {echo 'qtype: '.$question->qtype.'<br />';}
- delete_records("question_answers", "question", $question->id);
- delete_records("question_states", "question", $question->id);
- delete_records("question_sessions", "questionid", $question->id);
- if ($newversions = get_records('quiz_question_versions', 'oldquestion', $question->id)) {
- foreach ($newversions as $newversion) {
- $newquestion = get_record('question', 'id', $newversion->newquestion);
- delete_question($newquestion);
- }
- delete_records("quiz_question_versions", "oldquestion", $question->id);
+ $QTYPES[$question->qtype]->delete_question($questionid);
}
- delete_records("quiz_question_versions", "newquestion", $question->id);
- if ($children = get_records('question', 'parent', $question->id)) {
+
+ // 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) {
- delete_question($child);
+ delete_question($child->id);
}
}
- return true;
+
+ // Finally delete the question record itself
+ delete_records('question', 'id', $questionid);
+
+ return;
}
/**
*/
require_once($CFG->libdir.'/pagelib.php');
-require_once($CFG->dirroot.'/mod/quiz/constants.php');
/// CONSTANTS ///////////////////////////////////////////////////////////////////
//deleting questions
if ($questions = get_records("question", "category", $category->id)) {
foreach ($questions as $question) {
- delete_question($question);
+ delete_question($question->id);
}
delete_records("question", "category", $category->id);
}
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
- function delete_question($question) {
- delete_records("question_calculated", "question", $question->id);
- delete_records("question_numerical_units", "question", $question->id);
- delete_records("question_datasets", "question", $question->id);
- //TODO: delete entries from the question_dataset_items and question_dataset_definitions tables
+ function delete_question($questionid) {
+ delete_records("question_calculated", "question", $questionid);
+ delete_records("question_numerical_units", "question", $questionid);
+ if ($datasets = get_records('question_datasets', 'question', $questionid)) {
+ foreach ($datasets as $dataset) {
+ delete_records('question_dataset_definitions', 'id', $datasets->datasetdefinition);
+ delete_records('question_dataset_items', 'definition', $datasets->datasetdefinition);
+ }
+ }
+ delete_records("question_datasets", "question", $questionid);
return true;
}
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
- function delete_question($question) {
- delete_records("question_essay", "question", $question->id);
+ function delete_question($questionid) {
+ delete_records("question_essay", "question", $questionid);
return true;
}
* @return boolean Success/Failure
* @param integer $question->id
*/
- function delete_question($question) {
- delete_records("question_match", "question", $question->id);
- delete_records("question_match_sub", "question", $question->id);
+ function delete_question($questionid) {
+ delete_records("question_match", "question", $questionid);
+ delete_records("question_match_sub", "question", $questionid);
return true;
}
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
- function delete_question($question) {
- delete_records("question_multianswer", "question", $question->id);
+ function delete_question($questionid) {
+ delete_records("question_multianswer", "question", $questionid);
return true;
}
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
- function delete_question($question) {
- delete_records("question_multichoice", "question", $question->id);
+ function delete_question($questionid) {
+ delete_records("question_multichoice", "question", $questionid);
return true;
}
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
- function delete_question($question) {
- delete_records("question_numerical", "question", $question->id);
- delete_records("question_numerical_units", "question", $question->id);
+ function delete_question($questionid) {
+ delete_records("question_numerical", "question", $questionid);
+ delete_records("question_numerical_units", "question", $questionid);
return true;
}
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
- function delete_question($question) {
+ function delete_question($questionid) {
/// The default question type does not have any tables of its own
// therefore there is nothing to delete
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
- function delete_question($question) {
- delete_records("question_randomsamatch", "question", $question->id);
+ function delete_question($questionid) {
+ delete_records("question_randomsamatch", "question", $questionid);
return true;
}
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
- function delete_question($question) {
+ function delete_question($questionid) {
delete_records("question_rqp", "question", $questionid);
- //TODO: delete also the states from question_rqp_states
+ if ($states = get_records('question_states', 'question', $questionid)) {
+ foreach ($states as $state) {
+ delete_records('question_rqp_states', 'stateid', $state->id);
+ }
+ }
return true;
}
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
- function delete_question($question) {
- delete_records("question_shortanswer", "question", $question->id);
- //TODO: delete also the states from question_rqp_states
+ function delete_question($questionid) {
+ delete_records("question_shortanswer", "question", $questionid);
return true;
}
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
- function delete_question($question) {
- delete_records("question_truefalse", "question", $question->id);
+ function delete_question($questionid) {
+ delete_records("question_truefalse", "question", $questionid);
return true;
}