public function test_oublog_get_last_modified()
 {
     global $USER, $DB;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $course = $this->get_new_course();
     $oublog = $this->get_new_oublog($course->id);
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     $post = $this->get_post_stub($oublog->id);
     $postid = oublog_add_post($post, $cm, $oublog, $course);
     $timeposted = $DB->get_field('oublog_posts', 'timeposted', array('id' => $postid));
     $lastmodified = oublog_get_last_modified($cm, $course, $USER->id);
     $this->assertTrue(is_numeric($lastmodified));
     $this->assertEquals($timeposted, $lastmodified);
     // TODO: More comprehensive checking with separate group/individual blogs.
 }
Example #2
0
 public function create_content($instance, $record = array())
 {
     global $USER, $DB, $CFG;
     require_once $CFG->dirroot . '/mod/oublog/locallib.php';
     // Send $record['post'] (object) or $record['comment'] (object).
     // Returns id of post/comment created.
     $cm = get_coursemodule_from_instance('oublog', $instance->id);
     $context = context_module::instance($cm->id);
     $course = get_course($instance->course);
     // Default add a default post if nothing sent.
     if (!isset($record['post']) && !isset($record['comment'])) {
         $record['post'] = new stdClass();
     }
     if (isset($record['post'])) {
         if (empty($record['post']->userid)) {
             $record['post']->userid = $USER->id;
         }
         if (empty($record['post']->oublogid)) {
             $record['post']->oublogid = $instance->id;
         }
         if (empty($record['post']->message)) {
             $record['post']->message = array('text' => 'Test post');
         } else {
             if (is_string($record['post']->message)) {
                 // Support message being string to insert in db not form style.
                 $record['post']->message = array('text' => $record['post']->message);
             }
         }
         if (empty($record['post']->message['itemid'])) {
             // Draft files won't work anyway as no editor - so set to 1.
             $record['post']->message['itemid'] = 1;
         }
         if (empty($record['post']->allowcomments)) {
             $record['post']->allowcomments = OUBLOG_COMMENTS_ALLOW;
         }
         if (empty($record['post']->title)) {
             $record['post']->title = '';
         }
         // Force attachments to be empty as will not work.
         $record['post']->attachments = null;
         if ($USER->id === 0) {
             mtrace('oublog_add_post() will error as you must be a valid user to add a post');
         }
         return oublog_add_post($record['post'], $cm, $instance, $course);
     } else {
         if (isset($record['comment'])) {
             if (empty($record['comment']->postid)) {
                 throw new coding_exception('Must pass postid when creating comment');
             }
             if (empty($record['comment']->userid)) {
                 $record['comment']->userid = $USER->id;
             }
             if (empty($record['comment']->messagecomment)) {
                 if (empty($record['comment']->message)) {
                     $record['comment']->messagecomment = array('text' => 'Test comment');
                 } else {
                     // Support message being string to insert in db not form style.
                     $record['comment']->messagecomment = array('text' => $record['comment']->message);
                 }
             } else {
                 if (is_string($record['post']->messagecomment)) {
                     // Support message being string to insert in db not form style.
                     $record['comment']->messagecomment = array('text' => $record['comment']->messagecomment);
                 }
             }
             return oublog_add_comment($course, $cm, $instance, $record['comment']);
         }
     }
 }
        // update the post
        $post->id = $post->post;
        $post->oublogid = $oublog->id;
        $post->userid = $oubloginstance->userid;
        oublog_edit_post($post, $cm);
        add_to_log($course->id, "oublog", "edit post", $viewurl, $oublog->id, $cm->id);
        redirect($viewurl);
    } else {
        if (class_exists('ouflags')) {
            $DASHBOARD_COUNTER = DASHBOARD_BLOG_POST;
        }
        // insert the post
        unset($post->id);
        $post->oublogid = $oublog->id;
        $post->userid = $USER->id;
        //consider groups only when it is not an individual blog
        if ($oublog->individual) {
            $post->groupid = 0;
        } else {
            if (!$currentgroup && $groupmode) {
                error("Can't add post with no group");
            }
            $post->groupid = $currentgroup;
        }
        if (!oublog_add_post($post, $cm, $oublog, $course)) {
            error('Could not add post');
        }
        add_to_log($course->id, "oublog", "add post", $viewurl, $oublog->id, $cm->id);
        redirect($viewurl);
    }
}
 public function test_oublog_tags()
 {
     global $USER, $DB, $SITE;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $course = $this->get_new_course();
     $stud1 = $this->get_new_user('student', $course->id);
     $group1 = $this->get_new_group($course->id);
     $group2 = $this->get_new_group($course->id);
     $this->get_new_group_member($group1->id, $stud1->id);
     // Whole course blog.
     $oublog = $this->get_new_oublog($course->id);
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     $post = $this->get_post_stub($oublog->id);
     $post->tags = array('1', 'new', 'new', 'new2', 'a space');
     $postid = oublog_add_post($post, $cm, $oublog, $course);
     $this->assertEquals(21, strlen(oublog_get_tags_csv($postid)));
     $tags = oublog_get_tags($oublog, 0, $cm, null, -1);
     $this->assertCount(4, $tags);
     foreach ($tags as $tag) {
         $this->assertEquals(1, $tag->count);
         $this->assertContains($tag->tag, $post->tags);
     }
     // Individual blog.
     $oublog = $this->get_new_oublog($course->id, array('individual' => OUBLOG_VISIBLE_INDIVIDUAL_BLOGS));
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     $post = $this->get_post_stub($oublog->id);
     $post->tags = array('1', 'new', 'new', 'new2', 'a space');
     $postid = oublog_add_post($post, $cm, $oublog, $course);
     $tags = oublog_get_tags($oublog, 0, $cm, null, $USER->id);
     $this->assertCount(4, $tags);
     $tags = oublog_get_tags($oublog, 0, $cm, null, $stud1->id);
     $this->assertEmpty($tags);
     // Group blog.
     $oublog = $this->get_new_oublog($course->id, array('groupmode' => VISIBLEGROUPS));
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     $post = $this->get_post_stub($oublog->id);
     $post->groupid = $group1->id;
     $post->tags = array('1', 'new', 'new', 'new2', 'a space');
     $postid = oublog_add_post($post, $cm, $oublog, $course);
     $tags = oublog_get_tags($oublog, $group1->id, $cm);
     $this->assertCount(4, $tags);
     $tags = oublog_get_tags($oublog, $group2->id, $cm);
     $this->assertEmpty($tags);
     // Personal blog.
     if (!($oublog = $DB->get_record('oublog', array('global' => 1)))) {
         $oublog = $this->get_new_oublog($SITE->id, array('global' => 1, 'maxvisibility' => OUBLOG_VISIBILITY_PUBLIC));
     }
     $cm = get_coursemodule_from_instance('oublog', $oublog->id);
     list($oublog, $bloginstance) = oublog_get_personal_blog($stud1->id);
     $post1stub = $this->get_post_stub($oublog->id);
     $post1stub->userid = $stud1->id;
     $post1stub->visibility = OUBLOG_VISIBILITY_COURSEUSER;
     // Private.
     $post1stub->tags = array('private');
     oublog_add_post($post1stub, $cm, $oublog, $SITE);
     $post2stub = $this->get_post_stub($oublog->id);
     $post2stub->userid = $stud1->id;
     $post2stub->visibility = OUBLOG_VISIBILITY_LOGGEDINUSER;
     // User must be logged in.
     $post2stub->tags = array('loggedin');
     oublog_add_post($post2stub, $cm, $oublog, $SITE);
     $post3stub = $this->get_post_stub($oublog->id);
     $post3stub->userid = $stud1->id;
     $post3stub->visibility = OUBLOG_VISIBILITY_PUBLIC;
     // Any user.
     $post3stub->tags = array('public');
     oublog_add_post($post3stub, $cm, $oublog, $SITE);
     $tags = oublog_get_tags($oublog, 0, $cm, $bloginstance->id);
     $this->assertCount(2, $tags);
     $this->setUser($stud1);
     $tags = oublog_get_tags($oublog, 0, $cm, $bloginstance->id);
     $this->assertCount(3, $tags);
     $this->setGuestUser();
     $tags = oublog_get_tags($oublog, 0, $cm, $bloginstance->id);
     $this->assertCount(1, $tags);
     $this->setUser($stud1);
     $post4stub = $this->get_post_stub($oublog->id);
     $post4stub->userid = $stud1->id;
     $post4stub->visibility = OUBLOG_VISIBILITY_PUBLIC;
     // Any user.
     // Add unordered alphabetic tags.
     $post4stub->tags = array('toad', 'newt', 'private', 'crock', 'loggedin', 'frog', 'public', 'dino');
     $postid = oublog_add_post($post4stub, $cm, $oublog, $course);
     $this->assertEquals(56, strlen(oublog_get_tags_csv($postid)));
     $post5stub = $this->get_post_stub($oublog->id);
     $post5stub->userid = $stud1->id;
     $post5stub->visibility = OUBLOG_VISIBILITY_PUBLIC;
     // Any user.
     // Add unordered alphabetic tags.
     $post5stub->tags = array('toad', 'private', 'crock', 'loggedin', 'frog', 'public', 'dino');
     $postid = oublog_add_post($post5stub, $cm, $oublog, $course);
     $this->assertEquals(50, strlen(oublog_get_tags_csv($postid)));
     // Recover tags with default ordering ie Alphabetic.
     $tags = oublog_get_tags($oublog, 0, $cm, null, -1);
     $this->assertCount(8, $tags);
     foreach ($tags as $tag) {
         $this->assertContains($tag->tag, $post4stub->tags);
     }
     $this->assertEquals('toad', $tag->tag);
     // Last in default, Alphabetical order.
     // Recover tags in Use order.
     $tags = oublog_get_tags($oublog, 0, $cm, null, -1, 'use');
     $this->assertCount(8, $tags);
     $lasttag = end($tags);
     $this->assertEquals('newt', $lasttag->tag);
     // Last when Use order specified.
     // Recover tags in Alphabetical order.
     $tags = oublog_get_tags($oublog, 0, $cm, null, -1, 'alpha');
     $this->assertCount(8, $tags);
     $lasttag = end($tags);
     $this->assertEquals('toad', $lasttag->tag);
     // Last when Alphabetic order specified.
     // Testing the create update of a blog instance with predefined tags.
     // set and also testing oublog_get_tag_list().
     // Whole course blog created with predefined tag set.
     $oublog = $this->get_new_oublog($course->id, array('tags' => 'blogtag01'));
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     // Check that the predefined tag is inserted to the oublog_tags table.
     $blogtags = oublog_clarify_tags($oublog->tags);
     foreach ($blogtags as $tag) {
         $predefinedtags[] = $DB->get_record('oublog_tags', array('tag' => $tag));
     }
     // Confirm finding one predefined tag in the oublog_tags table.
     $this->assertCount(1, $predefinedtags);
     // Change the predefined tags on the blog.
     $oublog->tags = 'blogtag01, blogtag02';
     $oublog->instance = $oublog->id;
     oublog_update_instance($oublog);
     // Check that the changed tags are inserted to the oublog_tags table.
     $blogtags = oublog_clarify_tags($oublog->tags);
     foreach ($blogtags as $tag) {
         $changedtags[] = $DB->get_record('oublog_tags', array('tag' => $tag));
     }
     // Confirm finding predefined tags in the oublog_tags table.
     $this->assertCount(2, $changedtags);
     // Create post with 1 pre-defined and 1 user tag.
     $post = $this->get_post_stub($oublog->id);
     $post->tags = array('antelope', 'blogtag01');
     $postid = oublog_add_post($post, $cm, $oublog, $course);
     $this->assertEquals(19, strlen(oublog_get_tags_csv($postid)));
     // Recover post tags in default order.
     $tags = oublog_get_tags($oublog, 0, $cm, null, -1);
     foreach ($tags as $tag) {
         $this->assertEquals(1, $tag->count);
         $this->assertContains($tag->tag, $post->tags);
     }
     $this->assertNotEquals('antelope', $tag->tag);
     $this->assertEquals('blogtag01', $tag->tag);
     // Last in Alphabetical order.
     // Testing 'Set' tag only restrictions.
     // No restriction, recover full list of blog 'Set' and post tags.
     $oublog->restricttags = 0;
     $taglist = oublog_get_tag_list($oublog, 0, $cm, null, -1);
     $this->assertCount(3, $taglist);
     foreach ($taglist as $tag) {
         $fulltaglist[] = $tag->tag;
         if (isset($tag->label)) {
             // It should be an 'Official' ie. 'Set' predefined blog tag.
             $this->assertContains($tag->tag, $oublog->tags);
             $this->assertNotEmpty($tag->label);
             $this->assertGreaterThanOrEqual(0, $tag->count);
         }
         if (!isset($tag->label)) {
             // It should be the user post tag.
             $this->assertContains($tag->tag, $post->tags);
             $this->assertEquals('antelope', $tag->tag);
             $this->assertEquals(1, $tag->count);
         }
     }
     $this->assertContains('antelope', $fulltaglist);
     $this->assertContains('blogtag01', $fulltaglist);
     $this->assertContains('blogtag02', end($fulltaglist));
     // Last in full list.
     // Restriction applied, get restricted list of blog 'Set' tags.
     $oublog->restricttags = 1;
     $taglist = oublog_get_tag_list($oublog, 0, $cm, null, -1);
     $this->assertCount(2, $taglist);
     foreach ($taglist as $tag) {
         $restrictedtaglist[] = $tag->tag;
         if (isset($tag->label)) {
             // It should be an 'Official' ie. 'Set' predefined blog tag.
             $this->assertContains($tag->tag, $oublog->tags);
             $this->assertNotEmpty($tag->label);
             $this->assertGreaterThanOrEqual(0, $tag->count);
         }
         if (!isset($tag->label)) {
             $this->assertEmpty($tag->id);
             $this->assertEmpty($tag->weight);
             $this->assertEquals(0, $tag->count);
         }
     }
     $this->assertNotContains('antelope', $restrictedtaglist);
     $this->assertContains('blogtag01', $restrictedtaglist);
     $this->assertContains('blogtag02', end($restrictedtaglist));
     // Last in restricted list.
 }