Пример #1
0
 public function test_twf_trigger_content_uploaded_event()
 {
     $this->resetAfterTest();
     $user = $this->getDataGenerator()->create_user();
     $course = $this->getDataGenerator()->create_course();
     $twf = $this->getDataGenerator()->create_module('twf', array('course' => $course->id));
     $context = context_module::instance($twf->cmid);
     $this->setUser($user->id);
     $fakepost = (object) array('id' => 123, 'message' => 'Yay!', 'discussion' => 100);
     $cm = get_coursemodule_from_instance('twf', $twf->id);
     $fs = get_file_storage();
     $dummy = (object) array('contextid' => $context->id, 'component' => 'mod_twf', 'filearea' => 'attachment', 'itemid' => $fakepost->id, 'filepath' => '/', 'filename' => 'myassignmnent.pdf');
     $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
     $data = new stdClass();
     $sink = $this->redirectEvents();
     twf_trigger_content_uploaded_event($fakepost, $cm, 'some triggered from value');
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     $this->assertInstanceOf('\\mod_twf\\event\\assessable_uploaded', $event);
     $this->assertEquals($context->id, $event->contextid);
     $this->assertEquals($fakepost->id, $event->objectid);
     $this->assertEquals($fakepost->message, $event->other['content']);
     $this->assertEquals($fakepost->discussion, $event->other['discussionid']);
     $this->assertCount(1, $event->other['pathnamehashes']);
     $this->assertEquals($fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
     $expected = new stdClass();
     $expected->modulename = 'twf';
     $expected->name = 'some triggered from value';
     $expected->cmid = $twf->cmid;
     $expected->itemid = $fakepost->id;
     $expected->courseid = $course->id;
     $expected->userid = $user->id;
     $expected->content = $fakepost->message;
     $expected->pathnamehashes = array($fi->get_pathnamehash());
     $this->assertEventLegacyData($expected, $event);
     $this->assertEventContextNotUsed($event);
 }
Пример #2
0
/**
 * Given an object containing all the necessary data,
 * create a new discussion and return the id
 *
 * @param object $post
 * @param mixed $mform
 * @param string $unused
 * @param int $userid
 * @return object
 */
function twf_add_discussion($discussion, $mform = null, $unused = null, $userid = null)
{
    global $USER, $CFG, $DB;
    $timenow = time();
    if (is_null($userid)) {
        $userid = $USER->id;
    }
    // The first post is stored as a real post, and linked
    // to from the discuss entry.
    $twf = $DB->get_record('twf', array('id' => $discussion->twf));
    $cm = get_coursemodule_from_instance('twf', $twf->id);
    $post = new stdClass();
    $post->discussion = 0;
    $post->parent = 0;
    $post->userid = $userid;
    $post->created = $timenow;
    $post->modified = $timenow;
    $post->mailed = FORUM_MAILED_PENDING;
    $post->subject = $discussion->name;
    $post->message = $discussion->message;
    $post->messageformat = $discussion->messageformat;
    $post->messagetrust = $discussion->messagetrust;
    $post->attachments = isset($discussion->attachments) ? $discussion->attachments : null;
    $post->twf = $twf->id;
    // speedup
    $post->course = $twf->course;
    // speedup
    $post->mailnow = $discussion->mailnow;
    $post->id = $DB->insert_record("twf_posts", $post);
    // TODO: Fix the calling code so that there always is a $cm when this function is called
    if (!empty($cm->id) && !empty($discussion->itemid)) {
        // In "single simple discussions" this may not exist yet
        $context = context_module::instance($cm->id);
        $text = file_save_draft_area_files($discussion->itemid, $context->id, 'mod_twf', 'post', $post->id, mod_twf_post_form::editor_options($context, null), $post->message);
        $DB->set_field('twf_posts', 'message', $text, array('id' => $post->id));
    }
    // Now do the main entry for the discussion, linking to this first post
    $discussion->firstpost = $post->id;
    $discussion->timemodified = $timenow;
    $discussion->usermodified = $post->userid;
    $discussion->userid = $userid;
    $discussion->assessed = 0;
    $post->discussion = $DB->insert_record("twf_discussions", $discussion);
    // Finally, set the pointer on the post.
    $DB->set_field("twf_posts", "discussion", $post->discussion, array("id" => $post->id));
    if (!empty($cm->id)) {
        twf_add_attachment($post, $twf, $cm, $mform, $unused);
    }
    if (twf_tp_can_track_twfs($twf) && twf_tp_is_tracked($twf)) {
        twf_tp_mark_post_read($post->userid, $post, $post->twf);
    }
    // Let Moodle know that assessable content is uploaded (eg for plagiarism detection)
    if (!empty($cm->id)) {
        twf_trigger_content_uploaded_event($post, $cm, 'twf_add_discussion');
    }
    return $post->discussion;
}