/**
  * Test the tag deleted event.
  */
 public function test_tag_deleted()
 {
     global $DB;
     $this->setAdminUser();
     // Create a course.
     $course = $this->getDataGenerator()->create_course();
     // Create tag we are going to delete.
     $tag = $this->getDataGenerator()->create_tag();
     // Trigger and capture the event for deleting a tag.
     $sink = $this->redirectEvents();
     tag_delete($tag->id);
     $events = $sink->get_events();
     $event = reset($events);
     // Check that the tag was deleted and the event data is valid.
     $this->assertEquals(0, $DB->count_records('tag'));
     $this->assertInstanceOf('\\core\\event\\tag_deleted', $event);
     $this->assertEquals(context_system::instance(), $event->get_context());
     // Create two tags we are going to delete to ensure passing multiple tags work.
     $tag = $this->getDataGenerator()->create_tag();
     $tag2 = $this->getDataGenerator()->create_tag();
     // Trigger and capture the events for deleting multiple tags.
     $sink = $this->redirectEvents();
     tag_delete(array($tag->id, $tag2->id));
     $events = $sink->get_events();
     // Check that the tags were deleted and the events data is valid.
     $this->assertEquals(0, $DB->count_records('tag'));
     foreach ($events as $event) {
         $this->assertInstanceOf('\\core\\event\\tag_deleted', $event);
         $this->assertEquals(context_system::instance(), $event->get_context());
     }
     // Create another tag to delete.
     $tag = $this->getDataGenerator()->create_tag();
     // Add a tag instance to a course.
     tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
     // Trigger and capture the event for deleting a personal tag for a user for a course.
     $sink = $this->redirectEvents();
     coursetag_delete_keyword($tag->id, 2, $course->id);
     $events = $sink->get_events();
     $event = $events[1];
     // Check that the tag was deleted and the event data is valid.
     $this->assertEquals(0, $DB->count_records('tag'));
     $this->assertInstanceOf('\\core\\event\\tag_deleted', $event);
     $this->assertEquals(context_system::instance(), $event->get_context());
     // Create a new tag we are going to delete.
     $tag = $this->getDataGenerator()->create_tag();
     // Add the tag instance to the course again as it was deleted.
     tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
     // Trigger and capture the event for deleting all tags in a course.
     $sink = $this->redirectEvents();
     coursetag_delete_course_tags($course->id);
     $events = $sink->get_events();
     $event = $events[1];
     // Check that the tag was deleted and the event data is valid.
     $this->assertEquals(0, $DB->count_records('tag'));
     $this->assertInstanceOf('\\core\\event\\tag_deleted', $event);
     $this->assertEquals(context_system::instance(), $event->get_context());
     // Create two tags we are going to delete to ensure passing multiple tags work.
     $tag = $this->getDataGenerator()->create_tag();
     $tag2 = $this->getDataGenerator()->create_tag();
     // Add multiple tag instances now and check that it still works.
     tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
     tag_assign('course', $course->id, $tag2->id, 0, 2, 'course', context_course::instance($course->id)->id);
     // Trigger and capture the event for deleting all tags in a course.
     $sink = $this->redirectEvents();
     coursetag_delete_course_tags($course->id);
     $events = $sink->get_events();
     $events = array($events[2], $events[3]);
     // Check that the tags were deleted and the events data is valid.
     $this->assertEquals(0, $DB->count_records('tag'));
     foreach ($events as $event) {
         $this->assertInstanceOf('\\core\\event\\tag_deleted', $event);
         $this->assertEquals(context_system::instance(), $event->get_context());
     }
 }
Example #2
0
// Language strings
$tagslang = 'block_tags';

// Store data
if ($data = data_submitted()) {
    if (confirm_sesskey() and $courseid > 0 and $USER->id > 0 and $canedit) {
        // store personal tag
        if (trim(strip_tags($keyword))) {
            $myurl = 'tag/search.php';
            $keywords = explode(',', $keyword);
            coursetag_store_keywords($keywords, $courseid, $USER->id, 'default', $myurl);
        }
        // delete personal tag
        if ($deltag > 0) {
            coursetag_delete_keyword($deltag, $USER->id, $courseid);
        }
    }
}

// The title and breadcrumb
$title = get_string('edittitle', $tagslang);
$coursefullname = format_string($course->fullname);
$courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
$PAGE->navbar->add($title);
$PAGE->set_title($title);
$PAGE->set_heading($course->fullname);
$PAGE->set_cacheable(false);
echo $OUTPUT->header();

    // Print personal tags for all courses