From ec22ccd53a36cbdc20644fd82ba82d6d57aa45dd Mon Sep 17 00:00:00 2001 From: Nicolas Connault Date: Fri, 30 Oct 2009 07:24:55 +0000 Subject: [PATCH] MDL-20523 Script for upgrading sites that use the old bloglevel settings. --- admin/bloglevelupgrade.php | 150 +++++++++++++++++++++++++++++++++++++ admin/cron.php | 15 +++- lang/en_utf8/forum.php | 4 +- 3 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 admin/bloglevelupgrade.php diff --git a/admin/bloglevelupgrade.php b/admin/bloglevelupgrade.php new file mode 100644 index 0000000000..ba3b52cb6c --- /dev/null +++ b/admin/bloglevelupgrade.php @@ -0,0 +1,150 @@ +dirroot.'/course/lib.php'); +require_once($CFG->dirroot.'/blog/lib.php'); +require_once($CFG->dirroot.'/mod/forum/lib.php'); +require_once($CFG->libdir.'/adminlib.php'); + +admin_externalpage_setup('bloglevelupgrade'); +$PAGE->set_generaltype('maintenance'); + +$go = optional_param('go', 0, PARAM_BOOL); + +admin_externalpage_print_header(); +echo $OUTPUT->heading(get_string('bloglevelupgrade', 'admin')); + +$strbloglevelupgrade = get_string('bloglevelupgradeinfo', 'admin'); + +if (!$go or !data_submitted() or !confirm_sesskey()) { /// Print a form + $optionsyes = array('go'=>1, 'sesskey'=>sesskey()); + echo $OUTPUT->confirm($strbloglevelupgrade, new moodle_url('bloglevelupgrade.php', $optionsyes), 'index.php'); + echo $OUTPUT->footer(); + die; +} + +echo $OUTPUT->box_start(); + +/// Turn off time limits, sometimes upgrades can be slow. + +@set_time_limit(0); +@ob_implicit_flush(true); +while(@ob_end_flush()); + +$i = 0; + +// If $CFG->bloglevel is set to BLOG_GROUP_LEVEL or BLOG_COURSE_LEVEL, create a new "blog" forum in each course +// whose enrolled students have written blog entries, copy these entries in that forum and switch off blogs at site level + +if ($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) { + $pbar = new progress_bar('bloglevelupgrade', 500, true); + + $bloggers = $DB->get_records_sql("SELECT userid FROM {post} WHERE module = 'blog' GROUP BY userid"); + require_once($CFG->dirroot.'/mod/forum/lib.php'); + + $a = new object(); + $a->userscount = 0; + $a->blogcount = 0; + + foreach ($bloggers as $blogger) { + $courses = get_my_courses($blogger->userid); + $blogentries = $DB->get_records('post', array('module' => 'blog', 'userid' => $blogger->userid)); + + foreach ($courses as $course) { + $forum = forum_get_course_forum($course->id, 'blog'); + $cm = get_coursemodule_from_instance('forum', $forum->id); + + if ($CFG->bloglevel == BLOG_GROUP_LEVEL && $course->groupmode != NOGROUPS) { + // Unless the course is set to separate groups forced, force the forum to Separate groups + if (!($course->groupmode == SEPARATEGROUPS && $course->groupmodeforce)) { + $cm->groupmode = SEPARATEGROUPS; + $DB->update_record('course_modules', $cm); + } + + $groups = groups_get_user_groups($course->id, $blogger->userid); + foreach ($groups[0] as $groupid) { // [0] is for all groupings combined + $a->blogcount += bloglevelupgrade_entries($blogentries, $forum, $cm, $groupid); + } + } else { + $a->blogcount += bloglevelupgrade_entries($blogentries, $forum, $cm); + } + } + + $a->userscount = $i . '/' . count($bloggers); + $pbar->update($i, count($bloggers), get_string('bloglevelupgradeprogress', 'admin', $a)); + $i++; + } +} + +function bloglevelupgrade_entries($blogentries, $forum, $cm, $groupid=-1) { + $count = 0; + + $forumcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $sitecontext = get_context_instance(CONTEXT_SYSTEM); + + foreach ($blogentries as $blogentry) { + $discussion = new stdClass(); + $discussion->course = $forum->course; + $discussion->forum = $forum->id; + $discussion->name = $blogentry->subject; + $discussion->intro = $blogentry->summary; + $discussion->assessed = $forum->assessed; + $discussion->messageformat = $forum->introformat; + $discussion->messagetrust = 0; + $discussion->attachments = 0; + $discussion->mailnow = false; + $discussion->timemodified = $blogentry->created; + $discussion->itemid = null; + $discussion->groupid = $groupid; + $message = ''; + + $discussionid = forum_add_discussion($discussion, null, $message); + + // Copy file attachment records + $fs = get_file_storage(); + $files = $fs->get_area_files($sitecontext->id, 'blog_attachment', $blogentry->id); + + if (!empty($files)) { + foreach ($files as $storedfile) { + $newfile = new object(); + $newfile->filearea = 'forum_attachment'; + $newfile->itemid = $discussion->firstpost; + $newfile->contextid = $forumcontext->id; + $fs->create_file_from_storedfile($newfile, $storedfile->get_id()); + } + } + + $files = $fs->get_area_files($sitecontext->id, 'blog_post', $blogentry->id); + + if (!empty($files)) { + foreach ($files as $storedfile) { + $newfile = new object(); + $newfile->filearea = 'forum_post'; + $newfile->itemid = $discussion->firstpost; + $newfile->contextid = $forumcontext->id; + $fs->create_file_from_storedfile($newfile, $storedfile->get_id()); + } + } + $count++; + } + return $count; +} +// END OF LOOP + +// set conversion flag - switches to new plugin automatically +set_config('bloglevel_upgrade_complete', 1); +// Finally switch bloglevel to 0 (disabled) +set_config('bloglevel', 0); + +echo $OUTPUT->box_end(); + +/// Rebuild course cache which might be incorrect now +echo $OUTPUT->notification('Rebuilding course cache...', 'notifysuccess'); +rebuild_course_cache(); +echo $OUTPUT->notification('...finished', 'notifysuccess'); + +echo $OUTPUT->continue_button('index.php'); + +echo $OUTPUT->footer(); +die; diff --git a/admin/cron.php b/admin/cron.php index cd6982811a..fd14064d02 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -548,14 +548,23 @@ // Run external blog cron if needed if ($CFG->useexternalblogs) { require_once($CFG->dirroot . '/blog/lib.php'); - $sql = "SELECT * FROM {blog_external} WHERE timefetched < ? - ? OR timefetched = 0"; - $external_blogs = $DB->get_records_sql($sql, array(mktime(), $CFG->externalblogcrontime)); + mtrace("Fetching external blog entries...", ''); + $sql = "timefetched < ? - ? OR timefetched = 0"; + $externalblogs = $DB->get_records_select('blog_external', $sql, array(mktime(), $CFG->externalblogcrontime)); foreach ($external_blogs as $eb) { - blog_fetch_external_entries($eb); + blog_sync_external_entries($eb); } } + // Run blog associations cleanup + if ($CFG->useblogassociations) { + require_once($CFG->dirroot . '/blog/lib.php'); + // delete entries whose contextids no longer exists + mtrace("Deleting blog associations linked to non-existent contexts...", ''); + $DB->delete_records_select('blog_association', 'contextid NOT IN (SELECT id FROM {context})'); + } + // cleanup file trash $fs = get_file_storage(); $fs->cron(); diff --git a/lang/en_utf8/forum.php b/lang/en_utf8/forum.php index 6076401316..93e7acc9fc 100644 --- a/lang/en_utf8/forum.php +++ b/lang/en_utf8/forum.php @@ -30,6 +30,7 @@ $string['attachmentnopost'] = 'You cannot export attachments without a post id'; $string['blockafter'] = 'Post threshold for blocking'; $string['blockperiod'] = 'Time period for blocking'; $string['blockperioddisabled'] = 'Don\'t block'; +$string['blogforum'] = 'Standard forum displayed in a blog-like format'; $string['bynameondate'] = 'by $a->name - $a->date'; $string['cannotadd'] = 'Could not add the discussion for this forum'; $string['cannotaddteacherforumto'] = 'Could not add converted teacher forum instance to section 0 in the course'; @@ -164,6 +165,7 @@ $string['forum:viewsubscribers'] = 'View subscribers'; $string['generalforum'] = 'Standard forum for general use'; $string['generalforums'] = 'General forums'; $string['inforum'] = 'in $a'; +$string['introblog'] = 'The posts in this forum were copied here automatically from blogs of users in this course because those blog entries are no longer available'; $string['intronews'] = 'General news and announcements'; $string['introsocial'] = 'An open forum for chatting about anything you want to'; $string['introteacher'] = 'A forum for teacher-only notes and discussion'; @@ -359,4 +361,4 @@ $string['youratedthis'] = 'You rated this'; $string['yournewquestion'] = 'Your new question'; $string['yournewtopic'] = 'Your new discussion topic'; $string['yourreply'] = 'Your reply'; -?> \ No newline at end of file +?> -- 2.39.5