/** * Test the tag updated event. */ public function test_tag_updated() { $this->setAdminUser(); // Save the system context. $systemcontext = context_system::instance(); // Create a tag we are going to update. $tag = $this->getDataGenerator()->create_tag(); // Store the name before we change it. $oldname = $tag->name; // Trigger and capture the event when renaming a tag. $sink = $this->redirectEvents(); tag_rename($tag->id, 'newname'); $this->assertDebuggingCalled(); // Update the tag's name since we have renamed it. $tag->name = 'newname'; $events = $sink->get_events(); $event = reset($events); // Check that the event data is valid. $this->assertInstanceOf('\\core\\event\\tag_updated', $event); $this->assertEquals($systemcontext, $event->get_context()); $expected = array(SITEID, 'tag', 'update', 'index.php?id=' . $tag->id, $oldname . '->' . $tag->name); $this->assertEventLegacyLogData($expected, $event); // Trigger and capture the event when setting the type of a tag. $sink = $this->redirectEvents(); tag_type_set($tag->id, 'official'); $this->assertDebuggingCalled(); $events = $sink->get_events(); $event = reset($events); // Check that the event data is valid. $this->assertInstanceOf('\\core\\event\\tag_updated', $event); $this->assertEquals($systemcontext, $event->get_context()); $expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name); $this->assertEventLegacyLogData($expected, $event); // Trigger and capture the event for setting the description of a tag. $sink = $this->redirectEvents(); tag_description_set($tag->id, 'description', FORMAT_MOODLE); $this->assertDebuggingCalled(); $events = $sink->get_events(); $event = reset($events); // Check that the event data is valid. $this->assertInstanceOf('\\core\\event\\tag_updated', $event); $this->assertEquals($systemcontext, $event->get_context()); $expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name); $this->assertEventLegacyLogData($expected, $event); }
/** * Stores a tag for a course for a user * * @uses $CFG * @param array $tags simple array of keywords to be stored * @param integer $courseid * @param integer $userid * @param string $tagtype official or default only * @param string $myurl optional for logging creation of course tags */ function coursetag_store_keywords($tags, $courseid, $userid=0, $tagtype='official', $myurl='') { global $CFG; if (is_array($tags) and !empty($tags)) { foreach($tags as $tag) { $tag = trim($tag); if (strlen($tag) > 0) { //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags //add tag if does not exist if (!$tagid = tag_get_id($tag)) { $tag_id_array = tag_add(array($tag), $tagtype); $tagid = $tag_id_array[moodle_strtolower($tag)]; } //ordering $ordering = 0; if ($current_ids = tag_get_tags_ids('course', $courseid)) { end($current_ids); $ordering = key($current_ids) + 1; } //set type tag_type_set($tagid, $tagtype); //tag_instance entry tag_assign('course', $courseid, $tagid, $ordering, $userid); //logging - note only for user added tags if ($tagtype == 'default' and $myurl != '') { $url = $myurl.'?query='.urlencode($tag); add_to_log($courseid, 'coursetags', 'add', $url, 'Course tagged'); } } } } }
$editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $CFG->maxbytes, 'trusttext' => false, 'context' => $systemcontext, 'subdirs' => file_area_contains_subdirs($systemcontext, 'tag', 'description', $tag->id)); $tag = file_prepare_standard_editor($tag, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id); $tagform = new tag_edit_form(null, compact('editoroptions')); if ($tag->tagtype == 'official') { $tag->tagtype = '1'; } else { $tag->tagtype = '0'; } $tagform->set_data($tag); // If new data has been sent, update the tag record if ($tagnew = $tagform->get_data()) { if (has_capability('moodle/tag:manage', $systemcontext)) { if ($tag->tagtype != 'default' && (!isset($tagnew->tagtype) || $tagnew->tagtype != '1')) { tag_type_set($tag->id, 'default'); } elseif ($tag->tagtype != 'official' && $tagnew->tagtype == '1') { tag_type_set($tag->id, 'official'); } } if (!has_capability('moodle/tag:manage', $systemcontext)) { unset($tagnew->name); unset($tagnew->rawname); } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name $norm = tag_normalize($tagnew->rawname, TAG_CASE_LOWER); $tagnew->name = array_shift($norm); if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error $errorstring = s($tagnew->rawname) . ': ' . get_string('namesalreadybeeingused', 'tag'); }
//notice to inform what tags had their names effectively updated if ($tags_names_changed) { $notice = implode($tags_names_changed, ', '); $notice .= ' -- ' . get_string('updated', 'tag'); } break; case 'addofficialtag': if (!data_submitted() or !confirm_sesskey()) { break; } $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG)); $notice = ''; foreach ($new_otags as $new_otag) { if ($new_otag_id = tag_get_id($new_otag)) { // tag exists, change the type tag_type_set($new_otag_id, 'official'); } else { require_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM)); tag_add($new_otag, 'official'); } $notice .= get_string('addedotag', 'tag', $new_otag) . ' '; } break; } if ($err_notice) { echo $OUTPUT->notification($err_notice, 'red'); } if ($notice) { echo $OUTPUT->notification($notice, 'green'); } // small form to add an official tag
/** * Stores a tag for a course for a user * * @package core_tag * @category tag * @param array $tags simple array of keywords to be stored * @param int $courseid the id of the course we wish to store a tag for * @param int $userid the id of the user we wish to store a tag for * @param string $tagtype official or default only * @param string $myurl (optional) for logging creation of course tags */ function coursetag_store_keywords($tags, $courseid, $userid = 0, $tagtype = 'official', $myurl = '') { global $CFG; if (is_array($tags) and !empty($tags)) { foreach ($tags as $tag) { $tag = trim($tag); if (strlen($tag) > 0) { //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags //add tag if does not exist if (!($tagid = tag_get_id($tag))) { $tag_id_array = tag_add(array($tag), $tagtype); $tagid = $tag_id_array[core_text::strtolower($tag)]; } //ordering $ordering = 0; if ($current_ids = tag_get_tags_ids('course', $courseid)) { end($current_ids); $ordering = key($current_ids) + 1; } //set type tag_type_set($tagid, $tagtype); //tag_instance entry tag_assign('course', $courseid, $tagid, $ordering, $userid, 'core', context_course::instance($courseid)->id); } } } }
/** * Stores a tag for a course for a user * * @deprecated since 3.0 * @package core_tag * @category tag * @param array $tags simple array of keywords to be stored * @param int $courseid the id of the course we wish to store a tag for * @param int $userid the id of the user we wish to store a tag for * @param string $tagtype official or default only * @param string $myurl (optional) for logging creation of course tags */ function coursetag_store_keywords($tags, $courseid, $userid = 0, $tagtype = 'official', $myurl = '') { debugging('Function coursetag_store_keywords() is deprecated. Userid is no longer used for tagging courses.', DEBUG_DEVELOPER); global $CFG; require_once $CFG->dirroot . '/tag/lib.php'; if (is_array($tags) and !empty($tags)) { foreach ($tags as $tag) { $tag = trim($tag); if (strlen($tag) > 0) { //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags //add tag if does not exist if (!($tagid = tag_get_id($tag))) { $tag_id_array = tag_add(array($tag), $tagtype); $tagid = $tag_id_array[core_text::strtolower($tag)]; } //ordering $ordering = 0; if ($current_ids = tag_get_tags_ids('course', $courseid)) { end($current_ids); $ordering = key($current_ids) + 1; } //set type tag_type_set($tagid, $tagtype); //tag_instance entry tag_assign('course', $courseid, $tagid, $ordering, $userid, 'core', context_course::instance($courseid)->id); } } } }
/** * Test for function tag_cleanup() that is part of tag cron */ public function test_cleanup() { global $DB; $user = $this->getDataGenerator()->create_user(); // Setting tags will create non-official tags 'cat', 'dog' and 'fish'. tag_set('user', $user->id, array('cat', 'dog', 'fish'), 'core', context_user::instance($user->id)->id); $this->assertTrue($DB->record_exists('tag', array('name' => 'cat'))); $this->assertTrue($DB->record_exists('tag', array('name' => 'dog'))); $this->assertTrue($DB->record_exists('tag', array('name' => 'fish'))); // Make tag 'dog' official. $dogtag = tag_get('name', 'dog'); $fishtag = tag_get('name', 'fish'); tag_type_set($dogtag->id, 'official'); // Manually remove the instances pointing on tags 'dog' and 'fish'. $DB->execute('DELETE FROM {tag_instance} WHERE tagid in (?,?)', array($dogtag->id, $fishtag->id)); // Call tag_cleanup(). tag_cleanup(); // Tag 'cat' is still present because it's used. Tag 'dog' is present because it's official. // Tag 'fish' was removed because it is not official and it is no longer used by anybody. $this->assertTrue($DB->record_exists('tag', array('name' => 'cat'))); $this->assertTrue($DB->record_exists('tag', array('name' => 'dog'))); $this->assertFalse($DB->record_exists('tag', array('name' => 'fish'))); // Delete user without using API function. $DB->update_record('user', array('id' => $user->id, 'deleted' => 1)); // Call tag_cleanup(). tag_cleanup(); // Tag 'cat' was now deleted too. $this->assertFalse($DB->record_exists('tag', array('name' => 'cat'))); // Assign tag to non-existing record. Make sure tag was created in the DB. tag_set('course', 1231231, array('bird'), 'core', context_system::instance()->id); $this->assertTrue($DB->record_exists('tag', array('name' => 'bird'))); // Call tag_cleanup(). tag_cleanup(); // Tag 'bird' was now deleted because the related record does not exist in the DB. $this->assertFalse($DB->record_exists('tag', array('name' => 'bird'))); // Now we have a tag instance pointing on 'sometag' tag. $user = $this->getDataGenerator()->create_user(); tag_set('user', $user->id, array('sometag'), 'core', context_user::instance($user->id)->id); $sometag = tag_get('name', 'sometag'); $this->assertTrue($DB->record_exists('tag_instance', array('tagid' => $sometag->id))); // Some hacker removes the tag without using API. $DB->delete_records('tag', array('id' => $sometag->id)); // Call tag_cleanup(). tag_cleanup(); // The tag instances were also removed. $this->assertFalse($DB->record_exists('tag_instance', array('tagid' => $sometag->id))); }
} } redirect($PAGE->url); break; case 'addofficialtag': require_sesskey(); $otagsadd = optional_param('otagsadd', '', PARAM_RAW); $newtags = preg_split('/\\s*,\\s*/', trim($otagsadd), -1, PREG_SPLIT_NO_EMPTY); $newtags = array_filter(tag_normalize($newtags, TAG_CASE_ORIGINAL)); if (!$newtags) { redirect($PAGE->url); } foreach ($newtags as $newotag) { if ($newotagid = tag_get_id($newotag)) { // Tag exists, change the type. tag_type_set($newotagid, 'official'); } else { tag_add($newotag, 'official'); } } redirect(new moodle_url($PAGE->url, array('notice' => 'added'))); break; } echo $OUTPUT->header(); if ($notice && get_string_manager()->string_exists($notice, 'tag')) { echo $OUTPUT->notification(get_string($notice, 'tag'), 'notifysuccess'); } // Small form to add an official tag. print '<form class="tag-addtags-form" method="post" action="' . $CFG->wwwroot . '/tag/manage.php">'; print '<input type="hidden" name="action" value="addofficialtag" />'; print '<input type="hidden" name="perpage" value="' . $perpage . '" />';