Index: question/category_class.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/category_class.php,v retrieving revision 1.32.2.3 diff -u -r1.32.2.3 category_class.php --- question/category_class.php 2 Apr 2008 06:10:05 -0000 1.32.2.3 +++ question/category_class.php 9 May 2008 13:47:59 -0000 @@ -352,8 +352,9 @@ } function move_questions($oldcat, $newcat){ - if (!set_field('question', 'category', $newcat, 'category', $oldcat)) { - error("Error while moving questions from category '$oldcat' to '$newcat'", $this->pageurl->out()); + $questionids = get_records_select_menu('question', "category = $oldcat AND parent = 0", '', 'id,1'); + if (!question_move_questions_to_category(implode(',', array_keys($questionids)), $newcat)) { + print_error('errormovingquestions', 'question', $returnurl, $ids); } } Index: question/contextmoveq.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/contextmoveq.php,v retrieving revision 1.4.2.2 diff -u -r1.4.2.2 contextmoveq.php --- question/contextmoveq.php 20 Dec 2007 11:26:50 -0000 1.4.2.2 +++ question/contextmoveq.php 9 May 2008 13:47:59 -0000 @@ -159,9 +159,10 @@ } } - //now move questions - if (!execute_sql("UPDATE {$CFG->prefix}question SET category = {$tocat->id} WHERE id IN ({$ids})", false)){ - error("Could not move the questions {$ids} to category ".$tocat->name, $returnurl); + +/// Now move questions. + if (!question_move_questions_to_category(implode(',', $ids), $tocat->id)) { + print_error('errormovingquestions', 'question', $returnurl, $ids); } redirect($returnurl); } Index: question/editlib.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/editlib.php,v retrieving revision 1.76.2.7 diff -u -r1.76.2.7 editlib.php --- question/editlib.php 23 Apr 2008 13:45:40 -0000 1.76.2.7 +++ question/editlib.php 9 May 2008 13:47:59 -0000 @@ -447,11 +447,8 @@ } $returnurl = $pageurl->out(false, array('category'=>"$tocategoryid,$contextid")); if (!$checkforfiles){ - foreach ($questionids as $questionid){ - //move question - if (!set_field('question', 'category', $tocategory->id, 'id', $questionid)) { - error('Could not update category field'); - } + if (!question_move_questions_to_category(implode(',', $questionids), $tocategory->id)) { + print_error('errormovingquestions', 'question', $returnurl, $questionids); } redirect($returnurl); } else { Index: lib/questionlib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/questionlib.php,v retrieving revision 1.119.2.5 diff -u -r1.119.2.5 questionlib.php --- lib/questionlib.php 28 Feb 2008 12:52:59 -0000 1.119.2.5 +++ lib/questionlib.php 9 May 2008 13:47:59 -0000 @@ -559,6 +559,30 @@ } return true; } + +/** + * This function should be considered private to the question bank, it is called from + * question/editlib.php question/contextmoveq.php and a few similar places to to the work of + * acutally moving questions and associated data. However, callers of this function also have to + * do other work, which is why you should not call this method directly from outside the questionbank. + * + * @param string $questionids a comma-separated list of question ids. + * @param integer $newcategory the id of the category to move to. + */ +function question_move_questions_to_category($questionids, $newcategory) { + $result = true; + + // Move the questions themselves. + $result = $result && set_field_select('question', 'category', $newcategory, "id IN ($questionids)"); + + // Move any subquestions belonging to them. + $result = $result && set_field_select('question', 'category', $newcategory, "parent IN ($questionids)"); + + // TODO Deal with datasets. + + return $result; +} + /** * @param array $row tab objects * @param question_edit_contexts $contexts object representing contexts available from this context Index: lang/en_utf8/question.php =================================================================== RCS file: /cvsroot/moodle/moodle/lang/en_utf8/question.php,v retrieving revision 1.7 diff -u -r1.7 question.php --- lang/en_utf8/question.php 21 Aug 2007 06:55:49 -0000 1.7 +++ lang/en_utf8/question.php 9 May 2008 13:47:58 -0000 @@ -32,6 +32,7 @@ $string['erroraccessingcontext'] = 'Cannot access context'; $string['errorfilecannotbecopied'] = 'Error cannot copy file $a.'; $string['errorfilecannotbemoved'] = 'Error cannot move file $a.'; +$string['errormovingquestions'] = 'Error while moving questions with ids $a.'; $string['errorfileschanged'] = 'Error files linked to from questions have changed since form was displayed.'; $string['exportcategory'] = 'Export category'; $string['filesareasite']= 'the site files area';