/** * Given an object containing all the necessary data,(defined by the * form in mod_form.php) this function will update an existing instance * with new data. * * @param object $oublog the data from the mod form * @return boolean true on success, false on failure. */ function oublog_update_instance($oublog) { global $DB; $oublog->id = $oublog->instance; if (!$DB->get_record('oublog', array('id' => $oublog->id))) { return false; } if (empty($oublog->ratingtime) || empty($oublog->assessed)) { $oublog->assesstimestart = 0; $oublog->assesstimefinish = 0; } if (!$DB->update_record('oublog', $oublog)) { return false; } $blogtags = oublog_clarify_tags($oublog->tags); // For each tag in the blog check if it already exists in oublog_tags table, // if it does not a tag record is created. foreach ($blogtags as $tag) { if (!$DB->get_record('oublog_tags', array('tag' => $tag))) { $DB->insert_record('oublog_tags', (object) array('tag' => $tag)); } } oublog_grade_item_update($oublog); return true; }
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. }
/** * Obtains tags for a $post object whether or not it currently has them * defined in some way. (If they're not defined, uses a database query.) * * @param object $post Post object, must contain ->id at least * @param bool $includespaces If true, replaces the _ with space again * @return array Array of tags (may be empty) */ function oublog_get_post_tags($post, $includespaces = false) { global $CFG, $DB; // Work out tags from existing data if possible (to save adding a query) if (isset($post->tags)) { $taglist = oublog_clarify_tags($post->tags); } else { // Tags aren't in post so use database query $rs = $DB->get_recordset_sql("\nSELECT\n t.tag\nFROM\n {oublog_taginstances} ti\n INNER JOIN {oublog_tags} t ON ti.tagid = t.id\nWHERE\n ti.postid=?", array($post->id)); $taglist = array(); foreach ($rs as $rec) { $taglist[] = $rec->tag; } $rs->close(); } if ($includespaces) { foreach ($taglist as $ix => $tag) { // Make the spaces in tags back into spaces so they're searchable (sigh) $taglist[$ix] = str_replace('_', ' ', $tag); } } return $taglist; }
/** * Obtains tags for a $post object whether or not it currently has them * defined in some way. (If they're not defined, uses a database query.) * * @param object $post Post object, must contain ->id at least * @param bool $includespaces If true, replaces the _ with space again * @return array Array of tags (may be empty) */ function oublog_get_post_tags($post, $includespaces = false) { global $CFG; // Work out tags from existing data if possible (to save adding a query) if (isset($post->tags)) { $taglist = oublog_clarify_tags($post->tags); } else { // Tags aren't in post so use database query $rs = get_recordset_sql("\nSELECT\n t.tag\nFROM\n {$CFG->prefix}oublog_taginstances ti\n INNER JOIN {$CFG->prefix}oublog_tags t ON ti.tagid = t.id\nWHERE\n ti.postid={$post->id}"); $taglist = array(); while ($rec = rs_fetch_next_record($rs)) { $taglist[] = $rec->tag; } } if ($includespaces) { foreach ($taglist as $ix => $tag) { // Make the spaces in tags back into spaces so they're searchable (sigh) $taglist[$ix] = str_replace('_', ' ', $tag); } } return $taglist; }