MDL-9469 mod_forum: create basic forum export structure

Part of MDL-66075
This commit is contained in:
Simey Lameze 2019-07-01 13:50:49 +08:00
parent 357d74fda9
commit f7b7f4e679
3 changed files with 190 additions and 0 deletions

View File

@ -86,6 +86,20 @@ class discussion extends db_table_vault {
}, $results);
}
/**
* Get all discussions in the specified forum.
*
* @param forum_entity $forum
* @return array
*/
public function get_all_discussions_in_forum(forum_entity $forum): ?array {
$records = $this->get_db()->get_records(self::TABLE, [
'forum' => $forum->get_id(),
]);
return $this->transform_db_records_to_entities($records);
}
/**
* Get the first discussion in the specified forum.
*

114
mod/forum/export.php Normal file
View File

@ -0,0 +1,114 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Page to export forum discussions.
*
* @package mod_forum
* @copyright 2019 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_OUTPUT_BUFFERING', true);
require_once(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/dataformatlib.php');
require_once($CFG->dirroot . '/mod/forum/export_form.php');
$forumid = optional_param('id', 0, PARAM_INT);
$doexport = optional_param('export', false, PARAM_BOOL);
$vaultfactory = mod_forum\local\container::get_vault_factory();
$managerfactory = mod_forum\local\container::get_manager_factory();
$forumvault = $vaultfactory->get_forum_vault();
$forum = $forumvault->get_from_id($forumid);
if (empty($forum)) {
throw new \moodle_exception('Unable to find forum with id ' . $forumid);
}
$capabilitymanager = $managerfactory->get_capability_manager($forum);
$course = $forum->get_course_record();
$coursemodule = $forum->get_course_module_record();
$cm = \cm_info::create($coursemodule);
require_course_login($course, true, $cm);
$url = new moodle_url('/mod/forum/export.php');
$pagetitle = get_string('export', 'mod_forum');
$context = $forum->get_context();
if ($doexport == false) {
$PAGE->set_context($context);
$PAGE->set_url($url);
$PAGE->set_title($pagetitle);
$PAGE->set_pagelayout('admin');
$PAGE->set_heading($pagetitle);
}
$form = new export($url->out(false), [
'forum' => $forum
]);
if ($form->is_cancelled()) {
redirect($url);
} else if ($data = $form->get_data()) {
require_sesskey();
$dataformat = $data->format;
$discussionvault = $vaultfactory->get_discussion_vault();
$discussions = $discussionvault->get_all_discussions_in_forum($forum);
$discussionids = array_map(function ($discussion) {
return $discussion->get_id();
}, $discussions);
$postvault = $vaultfactory->get_post_vault();
$posts = $postvault->get_from_discussion_ids(
$USER,
$discussionids,
$capabilitymanager->can_view_any_private_reply($USER)
);
$builderfactory = mod_forum\local\container::get_builder_factory();
$exportedpostsbuilder = $builderfactory->get_exported_posts_builder();
$exportedposts = $exportedpostsbuilder->build(
$USER,
[$forum],
$discussions,
$posts
);
$fields = ['id', 'subject', 'message'];
$exportdata = new ArrayObject($exportedposts);
$iterator = $exportdata->getIterator();
require_once($CFG->libdir . '/dataformatlib.php');
$filename = clean_filename('discussion');
download_as_dataformat($filename, $dataformat, $fields, $iterator);
die();
}
if ($doexport == false) {
echo $OUTPUT->header();
echo $OUTPUT->heading($pagetitle);
$form->display();
echo $OUTPUT->footer();
}

62
mod/forum/export_form.php Normal file
View File

@ -0,0 +1,62 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the form definition for discussion export.
*
* @package mod_forum
* @copyright 2019 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->dirroot.'/mod/forum/lib.php');
require_once($CFG->libdir.'/formslib.php');
/**
* Export discussion form.
*
* @package mod_forum
* @copyright 2019 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL Juv3 or later
*/
class export extends moodleform {
/**
* Define the form - called by parent constructor
*/
public function definition() {
$mform = $this->_form;
$forum = $this->_customdata['forum'];
$mform->addElement('hidden', 'export');
$mform->setType('export', PARAM_BOOL);
$mform->setDefault('export', true);
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->setDefault('id', $forum->get_id());
// Export formats.
$formats = core_plugin_manager::instance()->get_plugins_of_type('dataformat');
$options = [];
foreach ($formats as $format) {
$options[$format->name] = $format->displayname;
}
$mform->addElement('select', 'format', 'Format', $options);
$this->add_action_buttons(true, get_string('export', 'mod_forum'));
}
}