Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ForumNG export button #2116

  • Loading branch information...
commit 62f9795c4a8161f03e2b18d90202c36b61e8e6f0 1 parent e20b338
Bryan Waddington authored sammarshallou committed
View
83 feature/export/export.php
@@ -0,0 +1,83 @@
+<?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/>.
+
+/**
+ * Export to portfolio script. This uses the post selector infrastructure to
+ * handle the situation when posts are being selected.
+ * @package forumngfeature
+ * @subpackage export
+ * @copyright 2011 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+require_once('../forumngfeature_post_selector.php');
+
+class export_post_selector extends forumngfeature_post_selector {
+ public function get_button_name() {
+ return get_string('export', 'forumngfeature_export');
+ }
+
+ public function apply($discussion, $all, $selected, $formdata) {
+ global $COURSE, $USER, $CFG;
+
+ if (!$CFG->enableportfolios) {
+ return '';
+ }
+
+ /*
+ * It is necessary to encode the array of selected discussion posts as text in order to
+ * pass the data in the button callback options because it gets filtered as PARAM_ALPHA
+ */
+ if (is_array($selected)) {
+ $seltext = implode('x', $selected);
+ $letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
+ $posts = '';
+ $selarray = str_split($seltext);
+ foreach ($selarray as $char) {
+ $posts .= ($char == 'x')? $char:$letters[$char];
+ }
+ } else {
+ $posts = '';
+ }
+
+ // get the id of the forum
+ $forumobj = $discussion->get_forum();
+ $forumngid = $forumobj->get_id();
+
+ require_once($CFG->libdir . '/portfoliolib.php');
+
+ // check if the forum is shared and pass the cloneid in the callback options
+ $linkparams = $discussion->get_link_params_array();
+ if (array_key_exists('clone', $linkparams)) {
+ $cloneid = $linkparams['clone'];
+ } else {
+ $cloneid = 0;
+ }
+
+ $button = new portfolio_add_button();
+ $button->set_callback_options('forumng_all_portfolio_caller',
+ array('forumngid' => $forumngid,
+ 'cloneid' => $cloneid,
+ 'posts' => $posts,
+ 'discussionid' => $discussion->get_id()),
+ '/mod/forumng/feature/export/portfoliolib.php');
+ $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
+
+ // redirect to the export page
+ redirect($button->to_html(PORTFOLIO_ADD_FAKE_URL));
+ }
+}
+
+forumngfeature_post_selector::go(new export_post_selector());
View
54 feature/export/forumngfeature_export.php
@@ -0,0 +1,54 @@
+<?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/>.
+
+/**
+ * Discussion feature: forward posts by email.
+ * @package forumngfeature
+ * @subpackage forward
+ * @copyright 2011 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class forumngfeature_export extends forumngfeature_discussion {
+ public function get_order() {
+ return 1000;
+ }
+
+ public function display($discussion) {
+ return parent::get_button($discussion,
+ get_string('export', 'forumngfeature_export'),
+ 'feature/export/export.php',
+ false, array(), '', false, true);
+ }
+
+ // Always display the Export button
+ public function should_display($discussion) {
+ global $CFG;
+
+ //check are portfolios enabled
+ if (!$CFG->enableportfolios) {
+ return false;
+ }
+
+ //check at there's at least one enabled and visible portfolio plugin
+ require_once($CFG->libdir . '/portfoliolib.php');
+ $instances = portfolio_instances();
+ if (empty($instances)) {
+ return false;
+ }
+
+ return true;
+ }
+}
View
25 feature/export/lang/en/forumngfeature_export.php
@@ -0,0 +1,25 @@
+<?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/>.
+
+/**
+ * Lang strings.
+ * @package forumngfeature
+ * @subpackage forward
+ * @copyright 2011 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+$string['export'] = 'Export';
+$string['pluginname'] = 'Export posts';
View
259 feature/export/portfoliolib.php
@@ -0,0 +1,259 @@
+<?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/>.
+
+/**
+ * Local library file for forumng. These are non-standard functions that are used
+ * only by the forumng export feature.
+ *
+ * @package mod
+ * @subpackage forumng
+ * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
+ **/
+
+// Make sure this isn't being directly accessed.
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->dirroot . '/mod/forumng/mod_forumng.php');
+require_once($CFG->dirroot . '/mod/forumng/mod_forumng_cron.php');
+require_once($CFG->libdir . '/portfolio/caller.php');
+
+abstract class forumng_portfolio_caller_base extends portfolio_module_caller_base {
+ protected $modcontext;
+ protected $files = array();
+
+ // Attachments: array of arrays of stored_file, keyed on versionid
+ protected $attachments = array();
+
+ protected function load_base_data($forumngid) {
+ global $DB, $COURSE;
+
+ $this->forumng = $DB->get_record(
+ 'forumng', array('id' => $forumngid), '*', MUST_EXIST);
+
+ if (!empty($COURSE->id) && $COURSE->id == $this->forumng->course) {
+ $course = $COURSE;
+ } else {
+ $course = $DB->get_record(
+ 'course', array('id' => $this->forumng->course), '*', MUST_EXIST);
+ }
+
+ $modinfo = get_fast_modinfo($course);
+ $instances = $modinfo->get_instances_of('forumng');
+ if (!array_key_exists($this->forumng->id, $instances)) {
+ throw new portfolio_caller_exception('error_export', 'forumng');
+ }
+ $this->cm = $instances[$this->forumng->id];
+ $this->modcontext = get_context_instance(CONTEXT_MODULE, $this->cm->id);
+ }
+
+ /**
+ * Adds all the files from the given pageversions.
+ * @param array $pageversions
+ */
+ protected function add_files() {
+
+ $fs = get_file_storage();
+
+ /*
+ * decoding the array from letters to numbers, see export.php
+ */
+ if ($this->posts !== '') {
+ $selected = $this->decode_string_to_array();
+ } else {
+ $discussion = mod_forumng_discussion::get_from_id($this->discussionid, $this->cloneid);
+ $rootpost = $discussion->get_root_post();
+ $allposts = array();
+ $rootpost->build_linear_children($allposts);
+ $selected = array();
+ foreach ($allposts as $post) {
+ $selected[] = $post->get_id();
+ }
+ }
+
+ foreach ($selected as $post) {
+ $attach = $fs->get_area_files($this->modcontext->id, 'mod_forumng', 'attachment',
+ $post, "sortorder, itemid, filepath, filename", false);
+ $this->attachments[$post] = $attach;
+ $embed = $fs->get_area_files($this->modcontext->id, 'mod_forumng', 'message',
+ $post, "sortorder, itemid, filepath, filename", false);
+ $this->files = array_merge($this->files, $attach, $embed);
+ }
+
+ $this->set_file_and_format_data($this->files);
+
+ if (empty($this->multifiles) && !empty($this->singlefile)) {
+ $this->multifiles = array($this->singlefile); // copy_files workaround
+ }
+ // If there are files, change to rich/plain
+ if (!empty($this->multifiles)) {
+ $this->add_format(PORTFOLIO_FORMAT_RICHHTML);
+ } else {
+ $this->add_format(PORTFOLIO_FORMAT_PLAINHTML);
+ }
+ }
+
+ /**
+ * @param array $files Array of file items to copy
+ * @return void
+ */
+ protected function copy_files($files) {
+ if (empty($files)) {
+ return;
+ }
+ foreach ($files as $f) {
+ $this->get('exporter')->copy_existing_file($f);
+ }
+ }
+
+ public function get_navigation() {
+ global $CFG;
+
+ $navlinks = array();
+ $navlinks[] = array(
+ 'name' => format_string($this->forumng->name),
+ 'link' => $CFG->wwwroot . '/mod/forumng/view.php?id=' . $this->cm->id,
+ 'type' => 'title'
+ );
+ return array($navlinks, $this->cm);
+ }
+
+ public function expected_time() {
+ return $this->expected_time_file();
+ }
+
+ public function check_permissions() {
+ $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
+ return (has_capability('mod/forumng:view', $context));
+ }
+
+ public static function display_name() {
+ return get_string('modulename', 'forumng');
+ }
+
+ public static function base_supported_formats() {
+ return array(PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML);
+ }
+
+ /**
+ * @param string $name Name to be used in filename
+ * @return string Safe version of name (replaces unknown characters with _)
+ */
+ protected function make_filename_safe($name) {
+ return preg_replace('~[^A-Za-z0-9 _!,.-]~u', '_', $name);
+ }
+
+ protected function decode_string_to_array() {
+ $numbers = array('a' => '0', 'b' => '1', 'c' => '2', 'd' => '3', 'e' => '4',
+ 'f' => '5', 'g' => '6', 'h' => '7', 'i' => '8', 'j' => '9');
+ $selarray = '';
+ $array = str_split($this->posts);
+ foreach ($array as $char) {
+ $selarray .= ($char == 'x')? $char:$numbers[$char];
+ }
+
+ return explode('x', $selarray);
+ }
+}
+
+/**
+ * Portfolio class for exporting the contents of an entire discussion.
+ */
+class forumng_all_portfolio_caller extends forumng_portfolio_caller_base {
+ protected $forumngid;
+ protected $cloneid;
+ protected $posts;
+ protected $discussionid;
+
+ public static function expected_callbackargs() {
+ return array(
+ 'forumngid' => true,
+ 'cloneid' => true,
+ 'posts' => true,
+ 'discussionid' => true);
+ }
+
+ public function load_data() {
+ global $DB, $COURSE;
+
+ // Load base data
+ $this->load_base_data($this->forumngid);
+
+ // Get all files used in the discussion or selected posts.
+ $this->add_files();
+ }
+
+ public function get_return_url() {
+ return new moodle_url('/mod/forumng/discuss.php',
+ array('d' => $this->discussionid, 'clone' => $this->cloneid));
+ }
+
+ public function prepare_package() {
+ global $CFG;
+
+ /*
+ * decoding the array from letters to numbers, see export.php
+ */
+ if ($this->posts !== '') {
+ $selected = $this->decode_string_to_array();
+ } else {
+ $selected = false;
+ }
+
+ // build the html for the export
+ $alltext = '';
+ $allhtml = "<body id='forumng-email'>\n";
+
+ $allhtml .= '<hr size="1" noshade="noshade" />';
+ $poststext = '';
+ $postshtml = '';
+ // we need a discussion object
+ $discussion = mod_forumng_discussion::get_from_id($this->discussionid, $this->cloneid);
+ $discussion->build_selected_posts_email($selected, $poststext, $postshtml);
+ $alltext .= $poststext;
+ $allhtml .= $postshtml . '</body>';
+
+ // Remove embedded img and attachment paths.
+ $plugin = $this->get('exporter')->get('instance')->get('plugin');
+ foreach ($this->files as $file) {
+ $filename = $file->get_filename();
+ $pattern = '/\"http:\/\/.*?'.$filename.'.*?\"/';
+ if ($plugin == 'rtf') {
+ $replace = '"site_files/'.$filename.'"';
+ } else {
+ $replace = '"'.$filename.'"';
+ }
+ $allhtml = preg_replace($pattern, $replace, $allhtml);
+ }
+
+ $content = $allhtml;
+ $name = $this->make_filename_safe('forum_discussion') . '.html';
+ $manifest = ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH);
+
+ $this->copy_files($this->multifiles);
+ $this->get('exporter')->write_new_file($content, $name, $manifest);
+ }
+
+ public function get_sha1() {
+ $filesha = '';
+ if (!empty($this->multifiles)) {
+ $filesha = $this->get_sha1_file();
+ }
+ $bigstring = $filesha;
+
+ return sha1($bigstring);
+ }
+}
View
25 feature/export/version.php
@@ -0,0 +1,25 @@
+<?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/>.
+
+/**
+ * Version.
+ * @package forumngfeature
+ * @subpackage forward
+ * @copyright 2011 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+$plugin->version = 2011020301;
View
130 internaldoc/testcase.export.txt
@@ -0,0 +1,130 @@
+This file contains a sequence of test cases for the ForumNG Export feature.
+
+Initial setup
+=============
+
+See testcase.txt for a description of the format of these tests and the initial
+site and ForumNG setup. Only one forum is required. Two discussion will need to
+be setup, as follows:
+
+D1 - a discussion containing two or more posts and they contain only text.
+D2 - a discussion containing two posts. One post has an embedded image and the
+other post has an attachement.
+
+
+ETD Discussion - Text posts only
+================================
+
+ETD01
+ Open discussion D1 and scroll to the end of the list of posts. You will see
+ an Export button.
+
+ETD02
+ Press the Export button. A popup appears asking what you want to export, choose
+ Discussion. A page is shown asking you to select your export type.
+
+ETD03
+ Choose 'File download' in the drop-down box and click 'Next'. If this is the
+ first export from this discussion you will be taken straight to the save file
+ dialogue box, otherwise you will see the page that lists previous exports and
+ asks you to confirm you want to export, choose Continue to be taken to the save
+ file dialogue box.
+
+ETD04
+ Confirm that the file you have downloaded is a HTML file and when opened in
+ a browser you will see all the posts in the discussion.
+
+** PASSED 2012-01-25
+
+
+ETS Selected - Text posts only
+==============================
+
+ETS01
+ Open discussion D1 and scroll to the end of the list of posts. You will see
+ an Export button.
+
+ETS02
+ Press the Export button. A popup appears asking what you want to export, choose
+ Selected posts. Select one of the two posts in D1 and then click on
+ Confirm selection at the bottom of the list of posts. A page is shown asking
+ you to select your export type.
+
+ETS03
+ Choose 'File download' in the drop-down box and click 'Next'. If this is the
+ first export from this discussion you will be taken straight to the save file
+ dialogue box, otherwise you will see the page that lists previous exports and
+ asks you to confirm you want to export, choose Continue to be taken to the save
+ file dialogue box.
+
+ETS04
+ Confirm that the file you have downloaded is a HTML file and when opened in
+ a browser you will see the posts you selected.
+
+** PASSED 2012-01-25
+
+
+EAD Discussion - Embedded image
+===============================
+
+EAD01
+ Open discussion D2 and scroll to the end of the list of posts. You will see
+ an Export button.
+
+EAD02
+ Press the Export button. A popup appears asking what you want to export, choose
+ Discussion. A page is shown asking you to select your export type.
+
+EAD03
+ Choose 'File download' in the drop-down box and click 'Next'. If this is the
+ first export from this discussion you will be taken straight to the save file
+ dialogue box, otherwise you will see the page that lists previous exports and
+ asks you to confirm you want to export, choose Continue to be taken to the save
+ file dialogue box.
+
+EAD04
+ Confirm that the file you have downloaded is a zip file and that it contains a
+ HTML file along with the embedded images and attachements that are in the posts
+ of this discussion.
+
+EAD05
+ Extract the contents of the downloaded zip file to a temporary location and open
+ the HTML file in your browser. You should see all the posts in the discussion
+ and the embedded image should be visible. If you click the link to the attached
+ file it will open up.
+
+** PASSED 2012-01-25
+
+
+EAS Selected - Embedded image
+=============================
+
+EAS01
+ Open discussion D2 and scroll to the end of the list of posts. You will see
+ an Export button.
+
+EAS02
+ Press the Export button. A popup appears asking what you want to export, choose
+ Selected posts. Select one of the two posts in D2 and then click on
+ Confirm selection at the bottom of the list of posts. A page is shown asking
+ you to select your export type.
+
+EAS03
+ Choose 'File download' in the drop-down box and click 'Next'. If this is the
+ first export from this discussion you will be taken straight to the save file
+ dialogue box, otherwise you will see the page that lists previous exports and
+ asks you to confirm you want to export, choose Continue to be taken to the save
+ file dialogue box.
+
+EAS04
+ Confirm that the file you have downloaded is a zip file and that it contains a
+ HTML file along with the embedded images or attachements that are in the posts
+ you selected.
+
+EAD05
+ Extract the contents of the downloaded zip file to a temporary location and open
+ the HTML file in your browser. You should see all the posts you selected and the
+ embedded image should be visible. If you click the link to the attached file it
+ will open up.
+
+** PASSED 2012-01-25
Please sign in to comment.
Something went wrong with that request. Please try again.