2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Page to export forum discussions.
21 * @copyright 2019 Simey Lameze <simey@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 define('NO_OUTPUT_BUFFERING', true);
26 require_once(__DIR__
. '/../../config.php');
27 require_once($CFG->libdir
. '/adminlib.php');
28 require_once($CFG->dirroot
. '/calendar/externallib.php');
30 $forumid = required_param('id', PARAM_INT
);
31 $userids = optional_param_array('userids', [], PARAM_INT
);
32 $discussionids = optional_param_array('discids', [], PARAM_INT
);
33 $from = optional_param_array('from', [], PARAM_INT
);
34 $to = optional_param_array('to', [], PARAM_INT
);
35 $fromtimestamp = optional_param('timestampfrom', '', PARAM_INT
);
36 $totimestamp = optional_param('timestampto', '', PARAM_INT
);
38 if (!empty($from['enabled'])) {
39 unset($from['enabled']);
40 $from = core_calendar_external
::get_timestamps([$from])['timestamps'][0]['timestamp'];
42 $from = $fromtimestamp;
45 if (!empty($to['enabled'])) {
46 unset($to['enabled']);
47 $to = core_calendar_external
::get_timestamps([$to])['timestamps'][0]['timestamp'];
52 $vaultfactory = mod_forum\local\container
::get_vault_factory();
53 $managerfactory = mod_forum\local\container
::get_manager_factory();
54 $legacydatamapperfactory = mod_forum\local\container
::get_legacy_data_mapper_factory();
56 $forumvault = $vaultfactory->get_forum_vault();
58 $forum = $forumvault->get_from_id($forumid);
60 throw new moodle_exception('Unable to find forum with id ' . $forumid);
63 $capabilitymanager = $managerfactory->get_capability_manager($forum);
64 if (!$capabilitymanager->can_export_forum($USER)) {
65 throw new moodle_exception('cannotexportforum', 'forum');
68 $course = $forum->get_course_record();
69 $coursemodule = $forum->get_course_module_record();
70 $cm = cm_info
::create($coursemodule);
72 require_course_login($course, true, $cm);
74 $url = new moodle_url('/mod/forum/export.php');
75 $pagetitle = get_string('export', 'mod_forum');
76 $context = $forum->get_context();
78 $form = new mod_forum\form\
export_form($url->out(false), [
82 if ($form->is_cancelled()) {
83 redirect(new moodle_url('/mod/forum/view.php', ['id' => $cm->id
]));
84 } else if ($data = $form->get_data()) {
85 $dataformat = $data->format
;
87 // This may take a very long time and extra memory.
88 \core_php_time_limit
::raise();
89 raise_memory_limit(MEMORY_HUGE
);
91 $discussionvault = $vaultfactory->get_discussion_vault();
92 if ($data->discussionids
) {
93 $discussionids = $data->discussionids
;
94 } else if (empty($discussionids)) {
95 $discussions = $discussionvault->get_all_discussions_in_forum($forum);
96 $discussionids = array_map(function ($discussion) {
97 return $discussion->get_id();
101 $filters = ['discussionids' => $discussionids];
102 if ($data->useridsselected
) {
103 $filters['userids'] = $data->useridsselected
;
106 $filters['from'] = $data->from
;
109 $filters['to'] = $data->to
;
112 // Retrieve posts based on the selected filters, note if forum has no discussions then there is nothing to export.
113 if (!empty($filters['discussionids'])) {
114 $postvault = $vaultfactory->get_post_vault();
115 $posts = $postvault->get_from_filters($USER, $filters, $capabilitymanager->can_view_any_private_reply($USER));
120 $striphtml = !empty($data->striphtml
);
121 $humandates = !empty($data->humandates
);
123 $fields = ['id', 'discussion', 'parent', 'userid', 'userfullname', 'created', 'modified', 'mailed', 'subject', 'message',
124 'messageformat', 'messagetrust', 'attachment', 'totalscore', 'mailnow', 'deleted', 'privatereplyto',
125 'privatereplytofullname', 'wordcount', 'charcount'];
127 $canviewfullname = has_capability('moodle/site:viewfullnames', $context);
129 $datamapper = $legacydatamapperfactory->get_post_data_mapper();
130 $exportdata = new ArrayObject($datamapper->to_legacy_objects($posts));
131 $iterator = $exportdata->getIterator();
133 $filename = clean_filename('discussion');
134 \core\dataformat
::download_data(
139 function($exportdata) use ($fields, $striphtml, $humandates, $canviewfullname, $context) {
140 $data = new stdClass();
142 foreach ($fields as $field) {
143 // Set data field's value from the export data's equivalent field by default.
144 $data->$field = $exportdata->$field ??
null;
146 if ($field == 'userfullname') {
147 $user = \core_user
::get_user($data->userid
);
148 $data->userfullname
= fullname($user, $canviewfullname);
151 if ($field == 'privatereplytofullname' && !empty($data->privatereplyto
)) {
152 $user = \core_user
::get_user($data->privatereplyto
);
153 $data->privatereplytofullname
= fullname($user, $canviewfullname);
156 if ($field == 'message') {
157 $data->message
= file_rewrite_pluginfile_urls($data->message
, 'pluginfile.php', $context->id
, 'mod_forum',
161 // Convert any boolean fields to their integer equivalent for output.
162 if (is_bool($data->$field)) {
163 $data->$field = (int) $data->$field;
168 $data->message
= html_to_text(format_text($data->message
, $data->messageformat
), 0, false);
169 $data->messageformat
= FORMAT_PLAIN
;
172 $data->created
= userdate($data->created
);
173 $data->modified
= userdate($data->modified
);
180 $PAGE->set_context($context);
181 $PAGE->set_url($url);
182 $PAGE->set_title($pagetitle);
183 $PAGE->set_pagelayout('admin');
184 $PAGE->add_body_class('limitedwidth');
185 $PAGE->set_heading($pagetitle);
186 $PAGE->activityheader
->disable();
188 echo $OUTPUT->header();
189 if (!$PAGE->has_secondary_navigation()) {
190 echo $OUTPUT->heading($pagetitle);
193 // It is possible that the following fields have been provided in the URL.
194 $form->set_data(['useridsselected' => $userids, 'discussionids' => $discussionids, 'from' => $from, 'to' => $to]);
198 echo $OUTPUT->footer();