/** * This function tests that the functions responsible for moving questions to * different contexts also updates the tag instances associated with the questions. */ public function test_altering_tag_instance_context() { global $CFG, $DB; // Set to admin user. $this->setAdminUser(); // Create two course categories - we are going to delete one of these later and will expect // all the questions belonging to the course in the deleted category to be moved. $coursecat1 = $this->getDataGenerator()->create_category(); $coursecat2 = $this->getDataGenerator()->create_category(); // Create a couple of categories and questions. $context1 = context_coursecat::instance($coursecat1->id); $context2 = context_coursecat::instance($coursecat2->id); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $questioncat1 = $questiongenerator->create_question_category(array('contextid' => $context1->id)); $questioncat2 = $questiongenerator->create_question_category(array('contextid' => $context2->id)); $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id)); $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id)); $question3 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id)); $question4 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id)); // Now lets tag these questions. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $context1, array('tag 1', 'tag 2')); core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $context1, array('tag 3', 'tag 4')); core_tag_tag::set_item_tags('core_question', 'question', $question3->id, $context2, array('tag 5', 'tag 6')); core_tag_tag::set_item_tags('core_question', 'question', $question4->id, $context2, array('tag 7', 'tag 8')); // Test moving the questions to another category. question_move_questions_to_category(array($question1->id, $question2->id), $questioncat2->id); // Test that all tag_instances belong to one context. $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question', 'contextid' => $questioncat2->contextid))); // Test moving them back. question_move_questions_to_category(array($question1->id, $question2->id), $questioncat1->id); // Test that all tag_instances are now reset to how they were initially. $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question', 'contextid' => $questioncat1->contextid))); $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question', 'contextid' => $questioncat2->contextid))); // Now test moving a whole question category to another context. question_move_category_to_context($questioncat1->id, $questioncat1->contextid, $questioncat2->contextid); // Test that all tag_instances belong to one context. $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question', 'contextid' => $questioncat2->contextid))); // Now test moving them back. question_move_category_to_context($questioncat1->id, $questioncat2->contextid, context_coursecat::instance($coursecat1->id)->id); // Test that all tag_instances are now reset to how they were initially. $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question', 'contextid' => $questioncat1->contextid))); $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question', 'contextid' => $questioncat2->contextid))); // Now we want to test deleting the course category and moving the questions to another category. question_delete_course_category($coursecat1, $coursecat2, false); // Test that all tag_instances belong to one context. $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question', 'contextid' => $questioncat2->contextid))); // Create a course. $course = $this->getDataGenerator()->create_course(); // Create some question categories and questions in this course. $coursecontext = context_course::instance($course->id); $questioncat = $questiongenerator->create_question_category(array('contextid' => $coursecontext->id)); $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id)); $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id)); // Add some tags to these questions. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, array('tag 1', 'tag 2')); core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, array('tag 1', 'tag 2')); // Create a course that we are going to restore the other course to. $course2 = $this->getDataGenerator()->create_course(); // Create backup file and save it to the backup location. $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2); $bc->execute_plan(); $results = $bc->get_results(); $file = $results['backup_destination']; $fp = get_file_packer('application/vnd.moodle.backup'); $filepath = $CFG->dataroot . '/temp/backup/test-restore-course'; $file->extract_to_pathname($fp, $filepath); $bc->destroy(); // Now restore the course. $rc = new restore_controller('test-restore-course', $course2->id, backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE); $rc->execute_precheck(); $rc->execute_plan(); // Get the created question category. $restoredcategory = $DB->get_record('question_categories', array('contextid' => context_course::instance($course2->id)->id), '*', MUST_EXIST); // Check that there are two questions in the restored to course's context. $this->assertEquals(2, $DB->count_records('question', array('category' => $restoredcategory->id))); $rc->destroy(); }
/** * This function helps move a question cateogry to a new context by moving all * the files belonging to all the questions to the new context. * Also moves subcategories. * @param integer $categoryid the id of the category being moved. * @param integer $oldcontextid the old context id. * @param integer $newcontextid the new context id. */ function question_move_category_to_context($categoryid, $oldcontextid, $newcontextid) { global $DB; $questionids = $DB->get_records_menu('question', array('category' => $categoryid), '', 'id,qtype'); foreach ($questionids as $questionid => $qtype) { question_bank::get_qtype($qtype)->move_files($questionid, $oldcontextid, $newcontextid); // Purge this question from the cache. question_bank::notify_question_edited($questionid); } $subcatids = $DB->get_records_menu('question_categories', array('parent' => $categoryid), '', 'id,1'); foreach ($subcatids as $subcatid => $notused) { $DB->set_field('question_categories', 'contextid', $newcontextid, array('id' => $subcatid)); question_move_category_to_context($subcatid, $oldcontextid, $newcontextid); } }
/** * This function helps move a question cateogry to a new context by moving all * the files belonging to all the questions to the new context. * Also moves subcategories. * @param integer $categoryid the id of the category being moved. * @param integer $oldcontextid the old context id. * @param integer $newcontextid the new context id. */ function question_move_category_to_context($categoryid, $oldcontextid, $newcontextid) { global $DB, $QTYPES; $questionids = $DB->get_records_menu('question', array('category' => $categoryid), '', 'id,qtype'); foreach ($questionids as $questionid => $qtype) { $QTYPES[$qtype]->move_files($questionid, $oldcontextid, $newcontextid); } $subcatids = $DB->get_records_menu('question_categories', array('parent' => $categoryid), '', 'id,1'); foreach ($subcatids as $subcatid => $notused) { $DB->set_field('question_categories', 'contextid', $newcontextid, array('id' => $subcatid)); question_move_category_to_context($subcatid, $oldcontextid, $newcontextid); } }
/** * This function helps move a question cateogry to a new context by moving all * the files belonging to all the questions to the new context. * Also moves subcategories. * @param integer $categoryid the id of the category being moved. * @param integer $oldcontextid the old context id. * @param integer $newcontextid the new context id. */ function question_move_category_to_context($categoryid, $oldcontextid, $newcontextid) { global $DB; $questionids = $DB->get_records_menu('question', array('category' => $categoryid), '', 'id,qtype'); foreach ($questionids as $questionid => $qtype) { question_bank::get_qtype($qtype)->move_files($questionid, $oldcontextid, $newcontextid); // Purge this question from the cache. question_bank::notify_question_edited($questionid); } if ($questionids) { // Update the contextid for any tag instances that may exist for these questions. list($questionids, $params) = $DB->get_in_or_equal(array_keys($questionids)); $DB->set_field_select('tag_instance', 'contextid', $newcontextid, "component = 'core_question' AND itemid {$questionids}", $params); } $subcatids = $DB->get_records_menu('question_categories', array('parent' => $categoryid), '', 'id,1'); foreach ($subcatids as $subcatid => $notused) { $DB->set_field('question_categories', 'contextid', $newcontextid, array('id' => $subcatid)); question_move_category_to_context($subcatid, $oldcontextid, $newcontextid); } }
/** * Updates an existing category with given params */ public function update_category($updateid, $newparent, $newname, $newinfo) { global $CFG, $DB; if (empty($newname)) { print_error('categorynamecantbeblank', 'question'); } // Get the record we are updating. $oldcat = $DB->get_record('question_categories', array('id' => $updateid)); $lastcategoryinthiscontext = question_is_only_toplevel_category_in_context($updateid); if (!empty($newparent) && !$lastcategoryinthiscontext) { list($parentid, $tocontextid) = explode(',', $newparent); } else { $parentid = $oldcat->parent; $tocontextid = $oldcat->contextid; } // Check permissions. $fromcontext = context::instance_by_id($oldcat->contextid); require_capability('moodle/question:managecategory', $fromcontext); // If moving to another context, check permissions some more. if ($oldcat->contextid != $tocontextid) { $tocontext = context::instance_by_id($tocontextid); require_capability('moodle/question:managecategory', $tocontext); } // Update the category record. $cat = new stdClass(); $cat->id = $updateid; $cat->name = $newname; $cat->info = $newinfo; $cat->parent = $parentid; $cat->contextid = $tocontextid; $DB->update_record('question_categories', $cat); // If the category name has changed, rename any random questions in that category. if ($oldcat->name != $cat->name) { $where = "qtype = 'random' AND category = ? AND " . $DB->sql_compare_text('questiontext') . " = ?"; $randomqtype = question_bank::get_qtype('random'); $randomqname = $randomqtype->question_name($cat, false); $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '0')); $randomqname = $randomqtype->question_name($cat, true); $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '1')); } if ($oldcat->contextid != $tocontextid) { // Moving to a new context. Must move files belonging to questions. question_move_category_to_context($cat->id, $oldcat->contextid, $tocontextid); } // Cat param depends on the context id, so update it. $this->pageurl->param('cat', $updateid . ',' . $tocontextid); redirect($this->pageurl); }