Example #1
0
 public function test_category_created()
 {
     global $DB;
     // There is no proper API to call to trigger this event, so what we are
     // doing here is simply making sure that the events returns the right information.
     $this->setAdminUser();
     $course = $this->getDataGenerator()->create_course();
     $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
     $context = context_module::instance($glossary->cmid);
     // Create category and trigger event.
     $category = new stdClass();
     $category->name = 'New category';
     $category->usedynalink = 0;
     $category->id = $DB->insert_record('glossary_categories', $category);
     $event = \mod_glossary\event\category_created::create(array('context' => $context, 'objectid' => $category->id));
     $sink = $this->redirectEvents();
     $event->trigger();
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_glossary\\event\\category_created', $event);
     $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
     $this->assertEquals($glossary->cmid, $event->contextinstanceid);
     //add_to_log($course->id, "glossary", "add category", "editcategories.php?id=$cm->id", $cat->id,$cm->id);
     $expected = array($course->id, "glossary", "add category", "editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $this->assertEventContextNotUsed($event);
     // Update category and trigger event.
     $category->name = 'Updated category';
     $DB->update_record('glossary_categories', $category);
     $event = \mod_glossary\event\category_updated::create(array('context' => $context, 'objectid' => $category->id));
     $sink = $this->redirectEvents();
     $event->trigger();
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_glossary\\event\\category_updated', $event);
     $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
     $this->assertEquals($glossary->cmid, $event->contextinstanceid);
     //add_to_log($course->id, "glossary", "edit category", "editcategories.php?id=$cm->id", $hook,$cm->id);
     $expected = array($course->id, "glossary", "edit category", "editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $this->assertEventContextNotUsed($event);
     // Delete category and trigger event.
     $category = $DB->get_record('glossary_categories', array('id' => $category->id));
     $DB->delete_records('glossary_categories', array('id' => $category->id));
     $event = \mod_glossary\event\category_deleted::create(array('context' => $context, 'objectid' => $category->id));
     $event->add_record_snapshot('glossary_categories', $category);
     $sink = $this->redirectEvents();
     $event->trigger();
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_glossary\\event\\category_deleted', $event);
     $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
     $this->assertEquals($glossary->cmid, $event->contextinstanceid);
     //add_to_log($course->id, "glossary", "delete category", "editcategories.php?id=$cm->id", $hook,$cm->id);
     $expected = array($course->id, "glossary", "delete category", "editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $this->assertEventContextNotUsed($event);
 }
    $rightalignment = 'left';
    $leftalignment = 'right';
} else {
    $rightalignment = 'right';
    $leftalignment = 'left';
}
if ($hook > 0) {
    if ($action == "edit") {
        if ($confirm) {
            $action = "";
            $cat = new stdClass();
            $cat->id = $hook;
            $cat->name = $name;
            $cat->usedynalink = $usedynalink;
            $DB->update_record("glossary_categories", $cat);
            $event = \mod_glossary\event\category_updated::create(array('context' => $context, 'objectid' => $hook));
            $cat->glossaryid = $glossary->id;
            $event->add_record_snapshot('glossary_categories', $cat);
            $event->add_record_snapshot('glossary', $glossary);
            $event->trigger();
            // Reset caches.
            \mod_glossary\local\concept_cache::reset_glossary($glossary);
        } else {
            echo $OUTPUT->header();
            echo $OUTPUT->heading(format_string($glossary->name), 2);
            echo $OUTPUT->heading(format_string(get_string("edit") . " " . get_string("category", "glossary")), 3);
            $name = $category->name;
            $usedynalink = $category->usedynalink;
            require "editcategories.html";
            echo $OUTPUT->footer();
            die;