Esempio n. 1
0
require_login($course, false, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/glossary:approve', $context);
if ($newstate != $entry->approved && confirm_sesskey()) {
    $newentry = new stdClass();
    $newentry->id = $entry->id;
    $newentry->approved = $newstate;
    $newentry->timemodified = time();
    // wee need this date here to speed up recent activity, TODO: use timestamp in approved field instead in 2.0
    $DB->update_record("glossary_entries", $newentry);
    // Trigger event about entry approval/disapproval.
    $params = array('context' => $context, 'objectid' => $entry->id);
    if ($newstate) {
        $event = \mod_glossary\event\entry_approved::create($params);
    } else {
        $event = \mod_glossary\event\entry_disapproved::create($params);
    }
    $entry->approved = $newstate ? 1 : 0;
    $entry->timemodified = $newentry->timemodified;
    $event->add_record_snapshot('glossary_entries', $entry);
    $event->trigger();
    // Update completion state
    $completion = new completion_info($course);
    if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries) {
        $completion->update_state($cm, COMPLETION_COMPLETE, $entry->userid);
    }
    // Reset caches.
    if ($entry->usedynalink) {
        \mod_glossary\local\concept_cache::reset_glossary($glossary);
    }
}
Esempio n. 2
0
 public function test_entry_approved()
 {
     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();
     $student = $this->getDataGenerator()->create_user();
     $rolestudent = $DB->get_record('role', array('shortname' => 'student'));
     $this->getDataGenerator()->enrol_user($student->id, $course->id, $rolestudent->id);
     $teacher = $this->getDataGenerator()->create_user();
     $roleteacher = $DB->get_record('role', array('shortname' => 'teacher'));
     $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id);
     $this->setUser($teacher);
     $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course, 'defaultapproval' => 0));
     $context = context_module::instance($glossary->cmid);
     $this->setUser($student);
     $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
     $entry = $glossarygenerator->create_content($glossary);
     $this->assertEquals(0, $entry->approved);
     // Approve entry, trigger and validate event.
     $this->setUser($teacher);
     $newentry = new stdClass();
     $newentry->id = $entry->id;
     $newentry->approved = true;
     $newentry->timemodified = time();
     $DB->update_record("glossary_entries", $newentry);
     $params = array('context' => $context, 'objectid' => $entry->id);
     $event = \mod_glossary\event\entry_approved::create($params);
     $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\\entry_approved', $event);
     $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
     $this->assertEquals($glossary->cmid, $event->contextinstanceid);
     $expected = array($course->id, "glossary", "approve entry", "showentry.php?id={$glossary->cmid}&eid={$entry->id}", $entry->id, $glossary->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $this->assertEventContextNotUsed($event);
     // Disapprove entry, trigger and validate event.
     $this->setUser($teacher);
     $newentry = new stdClass();
     $newentry->id = $entry->id;
     $newentry->approved = false;
     $newentry->timemodified = time();
     $DB->update_record("glossary_entries", $newentry);
     $params = array('context' => $context, 'objectid' => $entry->id);
     $event = \mod_glossary\event\entry_disapproved::create($params);
     $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\\entry_disapproved', $event);
     $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
     $this->assertEquals($glossary->cmid, $event->contextinstanceid);
     $expected = array($course->id, "glossary", "disapprove entry", "showentry.php?id={$glossary->cmid}&eid={$entry->id}", $entry->id, $glossary->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $this->assertEventContextNotUsed($event);
 }