示例#1
0
 /**
  * Test the tag_set_delete function.
  * This function was deprecated in 3.1
  */
 public function test_tag_set_delete()
 {
     global $DB;
     // Create a course to tag.
     $course = $this->getDataGenerator()->create_course();
     // Create the tag and tag instance we are going to delete.
     tag_set_add('course', $course->id, 'A random tag', 'core', context_course::instance($course->id)->id);
     $this->assertDebuggingCalled();
     // Call the tag_set_delete function.
     tag_set_delete('course', $course->id, 'a random tag', 'core', context_course::instance($course->id)->id);
     $this->assertDebuggingCalled();
     // Now check that there are no tags or tag instances.
     $this->assertEquals(0, $DB->count_records('tag'));
     $this->assertEquals(0, $DB->count_records('tag_instance'));
     // Add tag again, add and remove related tag.
     tag_set_add('course', $course->id, 'A random tag', 'core', context_course::instance($course->id)->id);
     $this->assertDebuggingCalled();
     $taginstance = $DB->get_record('tag_instance', array('itemtype' => 'course', 'itemid' => $course->id), '*', MUST_EXIST);
     $tagid = $taginstance->tagid;
     tag_set_add('tag', $tagid, 'Some related tag');
     $this->assertDebuggingCalled();
     tag_set_delete('tag', $tagid, 'Some related tag');
     $this->assertDebuggingCalled();
     $relatedtags = tag_get_related_tags($tagid);
     $this->assertDebuggingCalled();
     $this->assertCount(0, $relatedtags);
 }
示例#2
0
文件: lib.php 项目: r007/PMoodle
/**
 * Set the tags assigned to a record.  This overwrites the current tags.
 * 
 * This function is meant to be fed the string coming up from the user 
 * interface, which contains all tags assigned to a record.
 *
 * @param string $record_type the type of record to tag ('post' for blogs, 
 *     'user' for users, 'tag' for tags, etc.
 * @param int $record_id the id of the record to tag
 * @param array $tags the array of tags to set on the record. If 
 *     given an empty array, all tags will be removed.
 * @return void 
 */
function tag_set($record_type, $record_id, $tags)
{
    static $in_recursion_semaphore = false;
    // this is to prevent loops when tagging a tag
    if ($record_type == 'tag' && !$in_recursion_semaphore) {
        $current_tagged_tag_name = tag_get_name($record_id);
    }
    $tags_ids = tag_get_id($tags, TAG_RETURN_ARRAY);
    // force an array, even if we only have one tag.
    $cleaned_tags = tag_normalize($tags);
    //echo 'tags-in-tag_set'; var_dump($tags); var_dump($tags_ids); var_dump($cleaned_tags);
    $current_ids = tag_get_tags_ids($record_type, $record_id);
    //var_dump($current_ids);
    // for data coherence reasons, it's better to remove deleted tags
    // before adding new data: ordering could be duplicated.
    foreach ($current_ids as $current_id) {
        if (!in_array($current_id, $tags_ids)) {
            tag_delete_instance($record_type, $record_id, $current_id);
            if ($record_type == 'tag' && !$in_recursion_semaphore) {
                // if we are removing a tag-on-a-tag (manually related tag),
                // we need to remove the opposite relationship as well.
                tag_delete_instance('tag', $current_id, $record_id);
            }
        }
    }
    foreach ($tags as $ordering => $tag) {
        $tag = trim($tag);
        if (!$tag) {
            continue;
        }
        $clean_tag = $cleaned_tags[$tag];
        $tag_current_id = $tags_ids[$clean_tag];
        if (is_null($tag_current_id)) {
            // create new tags
            //echo "call to add tag $tag\n";
            $new_tag = tag_add($tag);
            $tag_current_id = $new_tag[$clean_tag];
        }
        tag_assign($record_type, $record_id, $tag_current_id, $ordering);
        // if we are tagging a tag (adding a manually-assigned related tag), we
        // need to create the opposite relationship as well.
        if ($record_type == 'tag' && !$in_recursion_semaphore) {
            $in_recursion_semaphore = true;
            tag_set_add('tag', $tag_current_id, $current_tagged_tag_name);
            $in_recursion_semaphore = false;
        }
    }
}
 protected function process_wiki_tag($data)
 {
     global $CFG, $DB;
     $data = (object) $data;
     $oldid = $data->id;
     if (empty($CFG->usetags)) {
         // tags disabled in server, nothing to process
         return;
     }
     $tag = $data->rawname;
     $itemid = $this->get_new_parentid('wiki_page');
     tag_set_add('wiki_pages', $itemid, $tag);
 }
示例#4
0
文件: user.php 项目: vuchannguyen/web
if (empty($CFG->usetags)) {
    print_error('tagdisabled');
}
if (isguestuser()) {
    print_error('noguest');
}
if (!confirm_sesskey()) {
    print_error('sesskey');
}
switch ($action) {
    case 'addinterest':
        if (empty($tag) && $id) {
            // for backward-compatibility (people saving bookmarks, mostly..)
            $tag = tag_get_name($id);
        }
        tag_set_add('user', $USER->id, $tag);
        redirect($CFG->wwwroot . '/tag/index.php?tag=' . rawurlencode($tag));
        break;
    case 'removeinterest':
        if (empty($tag) && $id) {
            // for backward-compatibility (people saving bookmarks, mostly..)
            $tag = tag_get_name($id);
        }
        tag_set_delete('user', $USER->id, $tag);
        redirect($CFG->wwwroot . '/tag/index.php?tag=' . rawurlencode($tag));
        break;
    case 'flaginappropriate':
        tag_set_flag(tag_get_id($tag));
        redirect($CFG->wwwroot . '/tag/index.php?tag=' . rawurlencode($tag), get_string('responsiblewillbenotified', 'tag'));
        break;
    default:
 /**
  * Test the tag_set_delete function.
  */
 public function test_tag_set_delete()
 {
     global $DB;
     // Create a course to tag.
     $course = $this->getDataGenerator()->create_course();
     // Create the tag and tag instance we are going to delete.
     tag_set_add('course', $course->id, 'A random tag', 'core', context_course::instance($course->id)->id);
     // Call the tag_set_delete function.
     tag_set_delete('course', $course->id, 'a random tag', 'core', context_course::instance($course->id)->id);
     // Now check that there are no tags or tag instances.
     $this->assertEquals(0, $DB->count_records('tag'));
     $this->assertEquals(0, $DB->count_records('tag_instance'));
     // Recreate the tag and tag instance.
     tag_set_add('course', $course->id, 'A random tag', 'core', context_course::instance($course->id)->id);
     // Now call the tag_set_delete function without specifying the component or
     // contextid and ensure the function debugging is called.
     tag_set_delete('course', $course->id, 'A random tag');
     $this->assertDebuggingCalled();
 }
 protected function process_wiki_tag($data)
 {
     global $CFG, $DB;
     $data = (object) $data;
     $oldid = $data->id;
     if (empty($CFG->usetags)) {
         // tags disabled in server, nothing to process
         return;
     }
     $tag = $data->rawname;
     $itemid = $this->get_new_parentid('wiki_page');
     $wikiid = $this->get_new_parentid('wiki');
     $cm = get_coursemodule_from_instance('wiki', $wikiid);
     tag_set_add('wiki_pages', $itemid, $tag, 'mod_wiki', context_module::instance($cm->id)->id);
 }
 protected function process_forumng_forumtaginstance($data)
 {
     global $CFG;
     $data = (object) $data;
     $oldid = $data->id;
     if (empty($CFG->usetags)) {
         // Tags disabled in server, nothing to process.
         return;
     }
     $tag = $data->rawname;
     $forumid = $this->get_new_parentid('forumng');
     $cm = get_coursemodule_from_instance('forumng', $forumid);
     tag_set_add('forumng', $forumid, $tag, 'mod_forumng', context_module::instance($cm->id)->id);
 }
示例#8
0
    print_error('tagdisabled');
}
if (isguestuser()) {
    print_error('noguest');
}
if (!confirm_sesskey()) {
    print_error('sesskey');
}
$usercontext = context_user::instance($USER->id);
switch ($action) {
    case 'addinterest':
        if (empty($tag) && $id) {
            // for backward-compatibility (people saving bookmarks, mostly..)
            $tag = tag_get_name($id);
        }
        tag_set_add('user', $USER->id, $tag, 'core', $usercontext->id);
        redirect($CFG->wwwroot . '/tag/index.php?tag=' . rawurlencode($tag));
        break;
    case 'removeinterest':
        if (empty($tag) && $id) {
            // for backward-compatibility (people saving bookmarks, mostly..)
            $tag = tag_get_name($id);
        }
        tag_set_delete('user', $USER->id, $tag, 'core', $usercontext->id);
        redirect($CFG->wwwroot . '/tag/index.php?tag=' . rawurlencode($tag));
        break;
    case 'flaginappropriate':
        $tagid = tag_get_id($tag);
        tag_set_flag($tagid);
        redirect($CFG->wwwroot . '/tag/index.php?tag=' . rawurlencode($tag), get_string('responsiblewillbenotified', 'tag'));
        break;
示例#9
0
             $existingmethod = get_record('classification_value', 'value', $method2);
             if (empty($existingmethod)) {
                 //create new classification if needed.
                 $newmeth = new stdclass();
                 $newmeth->type = get_field('classification_type', 'id', 'name', 'Subject');
                 $newmeth->value = $method2;
                 insert_record('classification_value', $newmeth);
                 $existingmethod = get_record('classification_value', 'value', $method2);
             }
             if (!empty($existingmethod)) {
                 //this is a valid method.
                 //now check to see if classification already set for this LP
                 if (!record_exists('course_classification', 'course', $courseid, 'value', $existingmethod->id)) {
                     //need to insert a classification and a tag.
                     insert_record('course_classification', (object) array('course' => $courseid, 'value' => $existingmethod->id));
                     tag_set_add('courseclassification', $courseid, strtolower($method));
                 }
             }
         }
     }
 }
 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
 //now do stuff with contributors
 if (!empty($lp['contributors'])) {
     $useridarray = array();
     $contribroleid = get_field('role', 'id', 'shortname', ROLE_LPCONTRIBUTOR);
     foreach ($lp['contributors'] as $conid) {
         $user = get_record('user', 'username', $conid);
         if (!empty($user)) {
             role_assign($contribroleid, $user->id, 0, $coursecontext->id);
             $useridarray[] = $user->id;