MDL-26643 added missing message property to discussion object used to upgrade blog
[moodle.git] / admin / bloglevelupgrade.php
CommitLineData
20207b82 1<?php
ec22ccd5
NC
2 /// Create "blog" forums in each course and copy blog entries from these courses' participants in these forums
3
cbad562e
PS
4define('NO_OUTPUT_BUFFERING', true);
5
ec22ccd5
NC
6require_once('../config.php');
7require_once($CFG->dirroot.'/course/lib.php');
8require_once($CFG->dirroot.'/blog/lib.php');
9require_once($CFG->dirroot.'/mod/forum/lib.php');
10require_once($CFG->libdir.'/adminlib.php');
11
12admin_externalpage_setup('bloglevelupgrade');
78946b9b 13$PAGE->set_pagelayout('maintenance');
ec22ccd5
NC
14
15$go = optional_param('go', 0, PARAM_BOOL);
16
61ef8f9f 17echo $OUTPUT->header();
ec22ccd5
NC
18echo $OUTPUT->heading(get_string('bloglevelupgrade', 'admin'));
19
20$strbloglevelupgrade = get_string('bloglevelupgradeinfo', 'admin');
21
22if (!$go or !data_submitted() or !confirm_sesskey()) { /// Print a form
23 $optionsyes = array('go'=>1, 'sesskey'=>sesskey());
24 echo $OUTPUT->confirm($strbloglevelupgrade, new moodle_url('bloglevelupgrade.php', $optionsyes), 'index.php');
25 echo $OUTPUT->footer();
26 die;
27}
28
29echo $OUTPUT->box_start();
30
31/// Turn off time limits, sometimes upgrades can be slow.
32
cbad562e 33set_time_limit(0);
ec22ccd5
NC
34
35$i = 0;
36
37// If $CFG->bloglevel is set to BLOG_GROUP_LEVEL or BLOG_COURSE_LEVEL, create a new "blog" forum in each course
38// whose enrolled students have written blog entries, copy these entries in that forum and switch off blogs at site level
39
40if ($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) {
41 $pbar = new progress_bar('bloglevelupgrade', 500, true);
42
43 $bloggers = $DB->get_records_sql("SELECT userid FROM {post} WHERE module = 'blog' GROUP BY userid");
44 require_once($CFG->dirroot.'/mod/forum/lib.php');
45
a226a972 46 $a = new stdClass();
ec22ccd5
NC
47 $a->userscount = 0;
48 $a->blogcount = 0;
49
50 foreach ($bloggers as $blogger) {
df997f84 51 $courses = enrol_get_users_courses($blogger->userid, true, 'groupmode,groupmodeforce');
ec22ccd5
NC
52 $blogentries = $DB->get_records('post', array('module' => 'blog', 'userid' => $blogger->userid));
53
54 foreach ($courses as $course) {
55 $forum = forum_get_course_forum($course->id, 'blog');
56 $cm = get_coursemodule_from_instance('forum', $forum->id);
57
58 if ($CFG->bloglevel == BLOG_GROUP_LEVEL && $course->groupmode != NOGROUPS) {
59 // Unless the course is set to separate groups forced, force the forum to Separate groups
60 if (!($course->groupmode == SEPARATEGROUPS && $course->groupmodeforce)) {
61 $cm->groupmode = SEPARATEGROUPS;
62 $DB->update_record('course_modules', $cm);
63 }
64
65 $groups = groups_get_user_groups($course->id, $blogger->userid);
66 foreach ($groups[0] as $groupid) { // [0] is for all groupings combined
67 $a->blogcount += bloglevelupgrade_entries($blogentries, $forum, $cm, $groupid);
68 }
69 } else {
70 $a->blogcount += bloglevelupgrade_entries($blogentries, $forum, $cm);
71 }
72 }
73
74 $a->userscount = $i . '/' . count($bloggers);
75 $pbar->update($i, count($bloggers), get_string('bloglevelupgradeprogress', 'admin', $a));
76 $i++;
77 }
78}
79
80function bloglevelupgrade_entries($blogentries, $forum, $cm, $groupid=-1) {
81 $count = 0;
82
83 $forumcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
84 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
85
86 foreach ($blogentries as $blogentry) {
87 $discussion = new stdClass();
88 $discussion->course = $forum->course;
89 $discussion->forum = $forum->id;
90 $discussion->name = $blogentry->subject;
ec22ccd5 91 $discussion->assessed = $forum->assessed;
2a5302b6 92 $discussion->message = $blogentry->summary;
ec22ccd5
NC
93 $discussion->messageformat = $forum->introformat;
94 $discussion->messagetrust = 0;
95 $discussion->attachments = 0;
96 $discussion->mailnow = false;
97 $discussion->timemodified = $blogentry->created;
98 $discussion->itemid = null;
99 $discussion->groupid = $groupid;
100 $message = '';
101
102 $discussionid = forum_add_discussion($discussion, null, $message);
103
104 // Copy file attachment records
105 $fs = get_file_storage();
64f93798 106 $files = $fs->get_area_files($sitecontext->id, 'blog', 'attachment', $blogentry->id);
ec22ccd5
NC
107
108 if (!empty($files)) {
109 foreach ($files as $storedfile) {
a226a972 110 $newfile = new stdClass();
64f93798
PS
111 $newfile->component = 'mod_forum';
112 $newfile->filearea = 'attachment';
ec22ccd5
NC
113 $newfile->itemid = $discussion->firstpost;
114 $newfile->contextid = $forumcontext->id;
115 $fs->create_file_from_storedfile($newfile, $storedfile->get_id());
116 }
117 }
118
64f93798 119 $files = $fs->get_area_files($sitecontext->id, 'blog', 'post', $blogentry->id);
ec22ccd5
NC
120
121 if (!empty($files)) {
122 foreach ($files as $storedfile) {
a226a972 123 $newfile = new stdClass();
64f93798
PS
124 $newfile->component = 'mod_forum';
125 $newfile->filearea = 'post';
ec22ccd5
NC
126 $newfile->itemid = $discussion->firstpost;
127 $newfile->contextid = $forumcontext->id;
128 $fs->create_file_from_storedfile($newfile, $storedfile->get_id());
129 }
130 }
131 $count++;
132 }
133 return $count;
134}
135// END OF LOOP
136
137// set conversion flag - switches to new plugin automatically
138set_config('bloglevel_upgrade_complete', 1);
139// Finally switch bloglevel to 0 (disabled)
140set_config('bloglevel', 0);
141
142echo $OUTPUT->box_end();
143
144/// Rebuild course cache which might be incorrect now
145echo $OUTPUT->notification('Rebuilding course cache...', 'notifysuccess');
146rebuild_course_cache();
147echo $OUTPUT->notification('...finished', 'notifysuccess');
148
149echo $OUTPUT->continue_button('index.php');
150
151echo $OUTPUT->footer();
152die;