Merge branch 'MDL-44316_master' of git://github.com/markn86/moodle

This commit is contained in:
Marina Glancy 2014-03-25 14:03:06 +08:00
commit 81e50a3661
28 changed files with 582 additions and 69 deletions

View file

@ -231,6 +231,9 @@ function uninstall_plugin($type, $name) {
$fs = get_file_storage();
$fs->delete_component_files($component);
// Delete all tag instances for this component.
$DB->delete_records('tag_instance', array('component' => $component));
// Finally purge all caches.
purge_all_caches();

View file

@ -2057,15 +2057,19 @@
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="tagid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="component" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="Defines the Moodle component which the tag was added to"/>
<FIELD NAME="itemtype" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The context id of the item that was tagged"/>
<FIELD NAME="tiuserid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="ordering" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Maintains the order of the tag instances of an item"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="timemodified"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="tagid" TYPE="foreign" FIELDS="tagid" REFTABLE="tag" REFFIELDS="id"/>
<KEY NAME="contextid" TYPE="foreign" FIELDS="contextid" REFTABLE="context" REFFIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="itemtype-itemid-tagid-tiuserid" UNIQUE="true" FIELDS="itemtype, itemid, tagid, tiuserid"/>

View file

@ -3183,5 +3183,102 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2014031400.04);
}
if ($oldversion < 2014032000.01) {
// Add new fields to the 'tag_instance' table.
$table = new xmldb_table('tag_instance');
$field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, false, null, null, 'tagid');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', null, false, null, '0', 'itemid');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'ordering');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$sql = "UPDATE {tag_instance}
SET timecreated = timemodified";
$DB->execute($sql);
// Update all the course tags.
$sql = "UPDATE {tag_instance}
SET component = 'core',
contextid = (SELECT ctx.id
FROM {context} ctx
WHERE ctx.contextlevel = :contextlevel
AND ctx.instanceid = {tag_instance}.itemid)
WHERE itemtype = 'course'";
$DB->execute($sql, array('contextlevel' => CONTEXT_COURSE));
// Update all the user tags.
$sql = "UPDATE {tag_instance}
SET component = 'core',
contextid = (SELECT ctx.id
FROM {context} ctx
WHERE ctx.contextlevel = :contextlevel
AND ctx.instanceid = {tag_instance}.itemid)
WHERE itemtype = 'user'";
$DB->execute($sql, array('contextlevel' => CONTEXT_USER));
// Update all the blog post tags.
$sql = "UPDATE {tag_instance}
SET component = 'core',
contextid = (SELECT ctx.id
FROM {context} ctx
JOIN {post} p
ON p.userid = ctx.instanceid
WHERE ctx.contextlevel = :contextlevel
AND p.id = {tag_instance}.itemid)
WHERE itemtype = 'post'";
$DB->execute($sql, array('contextlevel' => CONTEXT_USER));
// Update all the wiki page tags.
$sql = "UPDATE {tag_instance}
SET component = 'mod_wiki',
contextid = (SELECT ctx.id
FROM {context} ctx
JOIN {course_modules} cm
ON cm.id = ctx.instanceid
JOIN {modules} m
ON m.id = cm.module
JOIN {wiki} w
ON w.id = cm.instance
JOIN {wiki_subwikis} sw
ON sw.wikiid = w.id
JOIN {wiki_pages} wp
ON wp.subwikiid = sw.id
WHERE m.name = 'wiki'
AND ctx.contextlevel = :contextlevel
AND wp.id = {tag_instance}.itemid)
WHERE itemtype = 'wiki_pages'";
$DB->execute($sql, array('contextlevel' => CONTEXT_MODULE));
// Update all the question tags.
$sql = "UPDATE {tag_instance}
SET component = 'core_question',
contextid = (SELECT qc.contextid
FROM {question} q
JOIN {question_categories} qc
ON q.category = qc.id
WHERE q.id = {tag_instance}.itemid)
WHERE itemtype = 'question'";
$DB->execute($sql);
// Update all the tag tags.
$sql = "UPDATE {tag_instance}
SET component = 'core',
contextid = :systemcontext
WHERE itemtype = 'tag'";
$DB->execute($sql, array('systemcontext' => context_system::instance()->id));
// Main savepoint reached.
upgrade_main_savepoint(true, 2014032000.01);
}
return true;
}

View file

@ -4214,7 +4214,7 @@ function delete_user(stdClass $user) {
// TODO: remove from cohorts using standard API here.
// Remove user tags.
tag_set('user', $user->id, array());
tag_set('user', $user->id, array(), 'core', $usercontext->id);
// Unconditionally unenrol from all courses.
enrol_user_delete($user);

View file

@ -331,6 +331,9 @@ function question_delete_question($questionid) {
question_bank::get_qtype($question->qtype, false)->delete_question(
$questionid, $question->contextid);
// Delete all tag instances.
$DB->delete_records('tag_instance', array('component' => 'core_question', 'itemid' => $question->id));
// Now recursively delete all child questions
if ($children = $DB->get_records('question',
array('parent' => $questionid), '', 'id, qtype')) {
@ -435,7 +438,7 @@ function question_delete_course_category($category, $newcategory, $feedback=true
// Check to see if there were any questions that were kept because
// they are still in use somehow, even though quizzes in courses
// in this category will already have been deteted. This could
// in this category will already have been deleted. This could
// happen, for example, if questions are added to a course,
// and then that course is moved to another category (MDL-14802).
$questionids = $DB->get_records_menu('question',
@ -474,12 +477,17 @@ function question_delete_course_category($category, $newcategory, $feedback=true
}
} else {
// Move question categories ot the new context.
// Move question categories to the new context.
if (!$newcontext = context_coursecat::instance($newcategory->id)) {
return false;
}
$DB->set_field('question_categories', 'contextid', $newcontext->id,
array('contextid'=>$context->id));
// Update the contextid for any tag instances for questions in the old context.
$DB->set_field('tag_instance', 'contextid', $newcontext->id, array('component' => 'core_question',
'contextid' => $context->id));
$DB->set_field('question_categories', 'contextid', $newcontext->id, array('contextid' => $context->id));
if ($feedback) {
$a = new stdClass();
$a->oldplace = $context->get_context_name();
@ -611,6 +619,10 @@ function question_move_questions_to_category($questionids, $newcategoryid) {
$DB->set_field_select('question', 'category', $newcategoryid,
"parent $questionidcondition", $params);
// Update the contextid for any tag instances that may exist for these questions.
$DB->set_field_select('tag_instance', 'contextid', $newcontextid,
"component = 'core_question' AND itemid $questionidcondition", $params);
// TODO Deal with datasets.
// Purge these questions from the cache.
@ -641,6 +653,13 @@ function question_move_category_to_context($categoryid, $oldcontextid, $newconte
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) {

View file

@ -42,6 +42,7 @@ class testing_data_generator {
protected $groupcount = 0;
protected $groupingcount = 0;
protected $rolecount = 0;
protected $tagcount = 0;
/** @var array list of plugin generators */
protected $generators = array();
@ -744,6 +745,57 @@ EOD;
return $newroleid;
}
/**
* Create a tag.
*
* @param array|stdClass $record
* @return stdClass the tag record
*/
public function create_tag($record = null) {
global $DB, $USER;
$this->tagcount++;
$i = $this->tagcount;
$record = (array) $record;
if (!isset($record['userid'])) {
$record['userid'] = $USER->id;
}
if (!isset($record['name'])) {
$record['name'] = 'Tag name ' . $i;
}
if (!isset($record['rawname'])) {
$record['rawname'] = 'Raw tag name ' . $i;
}
if (!isset($record['tagtype'])) {
$record['tagtype'] = 'default';
}
if (!isset($record['description'])) {
$record['description'] = 'Tag description';
}
if (!isset($record['descriptionformat'])) {
$record['descriptionformat'] = FORMAT_MOODLE;
}
if (!isset($record['flag'])) {
$record['flag'] = 0;
}
if (!isset($record['timemodified'])) {
$record['timemodified'] = time();
}
$id = $DB->insert_record('tag', $record);
return $DB->get_record('tag', array('id' => $id), '*', MUST_EXIST);
}
/**
* Helper method which combines $defaults with the values specified in $record.
* If $record is an object, it is converted to an array.

View file

@ -26,8 +26,13 @@
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/tag/lib.php');
// Get the necessary files to perform backup and restore.
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
/**
* Unit tests for (some of) ../questionlib.php.
@ -35,7 +40,16 @@ require_once($CFG->libdir . '/questionlib.php');
* @copyright 2006 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_questionlib_testcase extends basic_testcase {
class core_questionlib_testcase extends advanced_testcase {
/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp() {
$this->resetAfterTest();
}
public function test_question_reorder_qtypes() {
$this->assertEquals(
@ -70,4 +84,118 @@ class core_questionlib_testcase extends basic_testcase {
$this->assertEquals(-0.1428571, match_grade_options($gradeoptions, -0.15, 'nearest'));
}
/**
* 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.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$questioncat1 = $questiongenerator->create_question_category(array('contextid' =>
context_coursecat::instance($coursecat1->id)->id));
$questioncat2 = $questiongenerator->create_question_category(array('contextid' =>
context_coursecat::instance($coursecat2->id)->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.
tag_set('question', $question1->id, array('tag 1', 'tag 2'), 'core_question', $questioncat1->contextid);
tag_set('question', $question2->id, array('tag 3', 'tag 4'), 'core_question', $questioncat1->contextid);
tag_set('question', $question3->id, array('tag 5', 'tag 6'), 'core_question', $questioncat2->contextid);
tag_set('question', $question4->id, array('tag 7', 'tag 8'), 'core_question', $questioncat2->contextid);
// 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.
$questioncat = $questiongenerator->create_question_category(array('contextid' =>
context_course::instance($course->id)->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.
tag_set('question', $question1->id, array('tag 1', 'tag 2'), 'core_question', $questioncat->contextid);
tag_set('question', $question2->id, array('tag 1', 'tag 2'), 'core_question', $questioncat->contextid);
// 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();
$filepath = $CFG->dataroot . '/temp/backup/test-restore-course';
$file->extract_to_pathname($fp, $filepath);
$bc->destroy();
unset($bc);
// 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)));
}
}

View file

@ -44,6 +44,8 @@ JavaSript:
* New "Time spent waiting for the database" performance metric displayed along with the
other MDL_PERF vars; the change affects both the error logs and the vars displayed in
the page footer.
* Changes in the tag API. The component and contextid are now saved when assigning tags to an item. Please see
tag/upgrade.txt for more information.
=== 2.6 ===