Skip to content

Commit

Permalink
MDL-44316 core_tag: changed the API to accept a contextid and component
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Mar 22, 2014
1 parent 8dfc4d9 commit cc033d4
Show file tree
Hide file tree
Showing 24 changed files with 427 additions and 67 deletions.
17 changes: 13 additions & 4 deletions backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,8 @@ public function process_tag($data) {
// Add the one being restored
$tags[] = $data->rawname;
// Send all the tags back to the course
tag_set('course', $this->get_courseid(), $tags);
tag_set('course', $this->get_courseid(), $tags, 'core',
context_course::instance($this->get_courseid())->id);
}
}

Expand Down Expand Up @@ -3279,6 +3280,9 @@ protected function apply_activity_instance($newitemid) {
*/
class restore_create_categories_and_questions extends restore_structure_step {

/** @var array $cachecategory store the categories */
protected $cachedcategory = array();

protected function define_structure() {

$category = new restore_path_element('question_category', '/question_categories/question_category');
Expand Down Expand Up @@ -3384,7 +3388,7 @@ protected function process_question($data) {
// step will be in charge of restoring all the question files
}

protected function process_question_hint($data) {
protected function process_question_hint($data) {
global $DB;

$data = (object)$data;
Expand Down Expand Up @@ -3448,7 +3452,7 @@ protected function process_tag($data) {
$newquestion = $this->get_new_parentid('question');

if (!empty($CFG->usetags)) { // if enabled in server
// TODO: This is highly inneficient. Each time we add one tag
// TODO: This is highly inefficient. Each time we add one tag
// we fetch all the existing because tag_set() deletes them
// so everything must be reinserted on each call
$tags = array();
Expand All @@ -3459,8 +3463,13 @@ protected function process_tag($data) {
}
// Add the one being restored
$tags[] = $data->rawname;
// Get the category, so we can then later get the context.
$categoryid = $this->get_new_parentid('question_category');
if (empty($this->cachedcategory) || $this->cachedcategory->id != $categoryid) {
$this->cachedcategory = $DB->get_record('question_categories', array('id' => $categoryid));
}
// Send all the tags back to the question
tag_set('question', $newquestion, $tags);
tag_set('question', $newquestion, $tags, 'core_question', $this->cachedcategory->contextid);
}
}

Expand Down
2 changes: 1 addition & 1 deletion backup/util/dbops/restore_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ public static function create_included_users($basepath, $restoreid, $userid,
$usertag = (object)$usertag;
$tags[] = $usertag->rawname;
}
tag_set('user', $newuserid, $tags);
tag_set('user', $newuserid, $tags, 'core', $newuserctxid);
}

// Process preferences
Expand Down
6 changes: 4 additions & 2 deletions blog/external_blog_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@

$newexternal->id = $DB->insert_record('blog_external', $newexternal);
blog_sync_external_entries($newexternal);
tag_set('blog_external', $newexternal->id, explode(',', $data->autotags));
tag_set('blog_external', $newexternal->id, explode(',', $data->autotags), 'core',
context_user::instance($newexternal->userid)->id);

break;

Expand All @@ -102,7 +103,8 @@
$external->timemodified = time();

$DB->update_record('blog_external', $external);
tag_set('blog_external', $external->id, explode(',', $data->autotags));
tag_set('blog_external', $external->id, explode(',', $data->autotags), 'core',
context_user::instance($newexternal->userid)->id);

} else {
print_error('wrongexternalid', 'blog');
Expand Down
2 changes: 1 addition & 1 deletion blog/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ function blog_sync_external_entries($externalblog) {

// Set tags
if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) {
tag_set('post', $id, $tags);
tag_set('post', $id, $tags, 'core', context_user::instance($externalblog->userid)->id);
}
} else {
$newentry->id = $postid;
Expand Down
8 changes: 4 additions & 4 deletions blog/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public function add() {
$this->add_associations();
}

tag_set('post', $this->id, $this->tags);
tag_set('post', $this->id, $this->tags, 'core', context_user::instance($this->userid)->id);

// Trigger an event for the new entry.
$event = \core\event\blog_entry_created::create(array(
Expand Down Expand Up @@ -303,7 +303,7 @@ public function edit($params=array(), $form=null, $summaryoptions=array(), $atta

// Update record.
$DB->update_record('post', $entry);
tag_set('post', $entry->id, $entry->tags);
tag_set('post', $entry->id, $entry->tags, 'core', context_user::instance($this->userid)->id);

$event = \core\event\blog_entry_updated::create(array(
'objectid' => $entry->id,
Expand All @@ -327,7 +327,7 @@ public function delete() {
// Get record to pass onto the event.
$record = $DB->get_record('post', array('id' => $this->id));
$DB->delete_records('post', array('id' => $this->id));
tag_set('post', $this->id, array());
tag_set('post', $this->id, array(), 'core', context_user::instance($this->userid)->id);

$event = \core\event\blog_entry_deleted::create(array(
'objectid' => $this->id,
Expand Down Expand Up @@ -434,7 +434,7 @@ public function add_tags_info() {
}
}

tag_set('post', $this->id, $tags);
tag_set('post', $this->id, $tags, 'core', context_user::instance($this->userid)->id);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,9 @@ function course_delete_module($cmid) {
$DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id,
'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));

// Delete the tag instances.
$DB->delete_records('tag_instance', array('component' => 'mod_' . $modulename, 'contextid' => $modcontext->id));

// Delete the context.
context_helper::delete_instance(CONTEXT_MODULE, $cm->id);

Expand Down
23 changes: 18 additions & 5 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/course/tests/fixtures/course_capability_assignment.php');
require_once($CFG->dirroot . '/enrol/imsenterprise/tests/imsenterprise_test.php');
require_once($CFG->dirroot . '/tag/lib.php');

class core_course_courselib_testcase extends advanced_testcase {

Expand Down Expand Up @@ -1366,28 +1367,40 @@ public function test_course_delete_module() {
// Generate an assignment with due date (will generate a course event).
$assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));

$cm = get_coursemodule_from_instance('assign', $assign->id);
// Get the module context.
$modcontext = context_module::instance($assign->cmid);

// Verify context exists.
$this->assertInstanceOf('context_module', context_module::instance($cm->id, IGNORE_MISSING));
$this->assertInstanceOf('context_module', $modcontext);

// Add some tags to this assignment.
tag_set('assign', $assign->id, array('Tag 1', 'Tag 2', 'Tag 3'), 'mod_assign', $modcontext->id);

// Confirm the tag instances were added.
$this->assertEquals(3, $DB->count_records('tag_instance', array('component' => 'mod_assign', 'contextid' =>
$modcontext->id)));

// Verify event assignment event has been generated.
$eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign'));
$this->assertEquals(1, $eventcount);

// Run delete..
course_delete_module($cm->id);
course_delete_module($assign->cmid);

// Verify the context has been removed.
$this->assertFalse(context_module::instance($cm->id, IGNORE_MISSING));
$this->assertFalse(context_module::instance($assign->cmid, IGNORE_MISSING));

// Verify the course_module record has been deleted.
$cmcount = $DB->count_records('course_modules', array('id' => $cm->id));
$cmcount = $DB->count_records('course_modules', array('id' => $assign->cmid));
$this->assertEmpty($cmcount);

// Verify event assignment events have been removed.
$eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign'));
$this->assertEmpty($eventcount);

// Verify the tag instances were deleted.
$this->assertEquals(0, $DB->count_records('tag_instance', array('component' => 'mod_assign', 'contextid' =>
$modcontext->id)));
}

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 23 additions & 4 deletions lib/questionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit cc033d4

Please sign in to comment.