Some improvements to delete_question()

This commit is contained in:
gustav_delius 2006-03-20 23:04:22 +00:00
parent 75cd257be8
commit 90c3f31066
13 changed files with 77 additions and 48 deletions

View File

@ -173,39 +173,61 @@ class cmoptions {
}
/// 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;
}
/**

View File

@ -11,7 +11,6 @@
*/
require_once($CFG->libdir.'/pagelib.php');
require_once($CFG->dirroot.'/mod/quiz/constants.php');
/// CONSTANTS ///////////////////////////////////////////////////////////////////
@ -377,7 +376,7 @@ function quiz_delete_course($course, $feedback=true) {
//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);
}

View File

@ -203,11 +203,16 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
* @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;
}

View File

@ -74,8 +74,8 @@ class question_essay_qtype extends default_questiontype {
* @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;
}

View File

@ -108,9 +108,9 @@ class question_match_qtype extends default_questiontype {
* @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;
}

View File

@ -171,8 +171,8 @@ class quiz_embedded_cloze_qtype extends default_questiontype {
* @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;
}

View File

@ -144,8 +144,8 @@ class question_multichoice_qtype extends default_questiontype {
* @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;
}

View File

@ -228,9 +228,9 @@ class question_numerical_qtype extends question_shortanswer_qtype {
* @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;
}

View File

@ -183,7 +183,7 @@ class default_questiontype {
* @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

View File

@ -61,8 +61,8 @@ class question_randomsamatch_qtype extends question_match_qtype {
* @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;
}

View File

@ -129,9 +129,13 @@ class question_rqp_qtype extends default_questiontype {
* @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;
}

View File

@ -112,9 +112,8 @@ class question_shortanswer_qtype extends default_questiontype {
* @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;
}

View File

@ -115,8 +115,8 @@ class question_truefalse_qtype extends default_questiontype {
* @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;
}