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);
 }
            echo $OUTPUT->box_end();
        }
    }
} elseif ($action == "add") {
    if ($confirm) {
        $dupcategory = $DB->get_records_sql("SELECT * FROM {glossary_categories} WHERE " . $DB->sql_like('name', '?', false) . " AND glossaryid=?", array($name, $glossary->id));
        if ($dupcategory) {
            redirect("editcategories.php?id={$cm->id}&action=add&name={$name}", get_string("duplicatecategory", "glossary"), 2);
        } else {
            $action = "";
            $cat = new stdClass();
            $cat->name = $name;
            $cat->usedynalink = $usedynalink;
            $cat->glossaryid = $glossary->id;
            $cat->id = $DB->insert_record("glossary_categories", $cat);
            $event = \mod_glossary\event\category_created::create(array('context' => $context, 'objectid' => $cat->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 "<h3 class=\"main\">" . get_string("add") . " " . get_string("category", "glossary") . "</h3>";
        $name = "";
        require "editcategories.html";
    }
}
if ($action) {