|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00026 define('NO_OUTPUT_BUFFERING', true); 00027 00028 require('../../../config.php'); 00029 require_once($CFG->dirroot.'/course/lib.php'); 00030 require_once($CFG->dirroot.'/blog/lib.php'); 00031 require_once($CFG->dirroot.'/mod/forum/lib.php'); 00032 require_once($CFG->libdir.'/adminlib.php'); 00033 00034 admin_externalpage_setup('toolbloglevelupgrade'); 00035 $PAGE->set_pagelayout('maintenance'); 00036 00037 $go = optional_param('go', 0, PARAM_BOOL); 00038 00039 echo $OUTPUT->header(); 00040 echo $OUTPUT->heading(get_string('pluginname', 'tool_bloglevelupgrade')); 00041 00042 $strbloglevelupgrade = get_string('bloglevelupgradeinfo', 'tool_bloglevelupgrade'); 00043 00044 if (!$go or !data_submitted() or !confirm_sesskey()) { 00045 $optionsyes = array('go'=>1, 'sesskey'=>sesskey()); 00046 echo $OUTPUT->confirm($strbloglevelupgrade, new moodle_url('/admin/tool/bloglevelupgrade/index.php', $optionsyes), new moodle_url('/admin/index.php')); 00047 echo $OUTPUT->footer(); 00048 die; 00049 } 00050 00051 echo $OUTPUT->box_start(); 00052 00054 00055 set_time_limit(0); 00056 00057 $i = 0; 00058 00059 // If $CFG->bloglevel is set to BLOG_GROUP_LEVEL or BLOG_COURSE_LEVEL, create a new "blog" forum in each course 00060 // whose enrolled students have written blog entries, copy these entries in that forum and switch off blogs at site level 00061 00062 if ($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) { 00063 $pbar = new progress_bar('toolbloglevelupgrade', 500, true); 00064 00065 $bloggers = $DB->get_records_sql("SELECT userid FROM {post} WHERE module = 'blog' GROUP BY userid"); 00066 require_once($CFG->dirroot.'/mod/forum/lib.php'); 00067 00068 $a = new stdClass(); 00069 $a->userscount = 0; 00070 $a->blogcount = 0; 00071 00072 foreach ($bloggers as $blogger) { 00073 $courses = enrol_get_users_courses($blogger->userid, true, 'groupmode,groupmodeforce'); 00074 $blogentries = $DB->get_records('post', array('module' => 'blog', 'userid' => $blogger->userid)); 00075 00076 foreach ($courses as $course) { 00077 $forum = forum_get_course_forum($course->id, 'blog'); 00078 $cm = get_coursemodule_from_instance('forum', $forum->id); 00079 00080 if ($CFG->bloglevel == BLOG_GROUP_LEVEL && $course->groupmode != NOGROUPS) { 00081 // Unless the course is set to separate groups forced, force the forum to Separate groups 00082 if (!($course->groupmode == SEPARATEGROUPS && $course->groupmodeforce)) { 00083 $cm->groupmode = SEPARATEGROUPS; 00084 $DB->update_record('course_modules', $cm); 00085 } 00086 00087 $groups = groups_get_user_groups($course->id, $blogger->userid); 00088 foreach ($groups[0] as $groupid) { // [0] is for all groupings combined 00089 $a->blogcount += bloglevelupgrade_entries($blogentries, $forum, $cm, $groupid); 00090 } 00091 } else { 00092 $a->blogcount += bloglevelupgrade_entries($blogentries, $forum, $cm); 00093 } 00094 } 00095 00096 $a->userscount = $i . '/' . count($bloggers); 00097 $pbar->update($i, count($bloggers), get_string('bloglevelupgradeprogress', 'tool_bloglevelupgrade', $a)); 00098 $i++; 00099 } 00100 } 00101 00102 function bloglevelupgrade_entries($blogentries, $forum, $cm, $groupid=-1) { 00103 $count = 0; 00104 00105 $forumcontext = get_context_instance(CONTEXT_MODULE, $cm->id); 00106 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00107 00108 foreach ($blogentries as $blogentry) { 00109 $discussion = new stdClass(); 00110 $discussion->course = $forum->course; 00111 $discussion->forum = $forum->id; 00112 $discussion->name = $blogentry->subject; 00113 $discussion->assessed = $forum->assessed; 00114 $discussion->message = $blogentry->summary; 00115 $discussion->messageformat = $blogentry->summaryformat; 00116 $discussion->messagetrust = 0; 00117 $discussion->attachments = 0; 00118 $discussion->mailnow = false; 00119 $discussion->timemodified = $blogentry->created; 00120 $discussion->itemid = null; 00121 $discussion->groupid = $groupid; 00122 $message = ''; 00123 00124 $discussionid = forum_add_discussion($discussion, null, $message); 00125 00126 // Copy file attachment records 00127 $fs = get_file_storage(); 00128 $files = $fs->get_area_files($sitecontext->id, 'blog', 'attachment', $blogentry->id); 00129 00130 if (!empty($files)) { 00131 foreach ($files as $storedfile) { 00132 $newfile = new stdClass(); 00133 $newfile->component = 'mod_forum'; 00134 $newfile->filearea = 'attachment'; 00135 $newfile->itemid = $discussion->firstpost; 00136 $newfile->contextid = $forumcontext->id; 00137 $fs->create_file_from_storedfile($newfile, $storedfile->get_id()); 00138 } 00139 } 00140 00141 $files = $fs->get_area_files($sitecontext->id, 'blog', 'post', $blogentry->id); 00142 00143 if (!empty($files)) { 00144 foreach ($files as $storedfile) { 00145 $newfile = new stdClass(); 00146 $newfile->component = 'mod_forum'; 00147 $newfile->filearea = 'post'; 00148 $newfile->itemid = $discussion->firstpost; 00149 $newfile->contextid = $forumcontext->id; 00150 $fs->create_file_from_storedfile($newfile, $storedfile->get_id()); 00151 } 00152 } 00153 $count++; 00154 } 00155 return $count; 00156 } 00157 // END OF LOOP 00158 00159 // set conversion flag - switches to new plugin automatically 00160 unset_config('tool_bloglevelupgrade_pending'); 00161 // Finally switch bloglevel to 0 (disabled) 00162 set_config('bloglevel', 0); 00163 00164 echo $OUTPUT->box_end(); 00165 00167 echo $OUTPUT->notification('Rebuilding course cache...', 'notifysuccess'); 00168 rebuild_course_cache(); 00169 echo $OUTPUT->notification('...finished', 'notifysuccess'); 00170 00171 echo $OUTPUT->continue_button(new moodle_url('/admin/index.php')); 00172 00173 echo $OUTPUT->footer(); 00174 die;