]> git.mjollnir.org Git - moodle.git/commitdiff
portfolio MDL-21079 be more selective about including libraries.
authorPenny Leach <penny@liip.ch>
Sat, 12 Dec 2009 11:27:07 +0000 (11:27 +0000)
committerPenny Leach <penny@liip.ch>
Sat, 12 Dec 2009 11:27:07 +0000 (11:27 +0000)
this involves moving all formslib includes to where they're actually needed.
it also moves the portfolio stuff from assignment,data,chat,forum and glossary
into a locallib.php which involved creating it for many of those modules.

25 files changed:
admin/portfolio.php
lib/navigationlib.php
lib/portfolio/exporter.php
lib/portfoliolib.php
mod/assignment/lib.php
mod/assignment/locallib.php [new file with mode: 0644]
mod/chat/lib.php
mod/chat/locallib.php [new file with mode: 0644]
mod/chat/report.php
mod/data/export.php
mod/data/export_form.php
mod/data/lib.php
mod/data/locallib.php [new file with mode: 0644]
mod/forum/discuss.php
mod/forum/lib.php
mod/forum/locallib.php [new file with mode: 0644]
mod/glossary/export.php
mod/glossary/lib.php
mod/glossary/locallib.php [new file with mode: 0644]
portfolio/add.php
portfolio/download/file.php
portfolio/file.php
portfolio/mahara/lib.php
portfolio/mahara/preconfig.php
user/portfolio.php

index 23cc2a7d19f8396d0896421e611c4ccbe70edb38..ea47d5038bdb8a28173583ad1ddf2ee9863479f7 100644 (file)
@@ -2,6 +2,7 @@
 
 require_once(dirname(dirname(__FILE__)) . '/config.php');
 require_once($CFG->libdir . '/portfoliolib.php');
+require_once($CFG->libdir . '/portfolio/forms.php');
 require_once($CFG->libdir . '/adminlib.php');
 
 $edit    = optional_param('edit', 0, PARAM_INT);
index 5f2c29a769bb67ceb451782f517d52778c55fbb6..880fa2e9128c83b2cc384d348ba62f811881d071 100644 (file)
@@ -3152,12 +3152,15 @@ class settings_navigation extends navigation_node {
         }
 
         // Portfolio
-        if (empty($userindexpage) && $currentuser && !empty($CFG->enableportfolios) && has_capability('moodle/portfolio:export', $systemcontext) && portfolio_instances(true, false)) {
-            $portfoliokey = $usersetting->add(get_string('portfolios', 'portfolio'), null, self::TYPE_SETTING);
-            $url = new moodle_url($CFG->wwwroot .'/user/portfolio.php');
-            $usersetting->get($portfoliokey)->add(get_string('configure', 'portfolio'), $url, self::TYPE_SETTING);
-            $url = new moodle_url($CFG->wwwroot .'/user/portfoliologs.php');
-            $usersetting->get($portfoliokey)->add(get_string('logs', 'portfolio'), $url, self::TYPE_SETTING);
+        if (empty($userindexpage) && $currentuser && !empty($CFG->enableportfolios) && has_capability('moodle/portfolio:export', $systemcontext)) {
+            require_once($CFG->libdir . '/portfoliolib.php');
+            if (portfolio_instances(true, false)) {
+                $portfoliokey = $usersetting->add(get_string('portfolios', 'portfolio'), null, self::TYPE_SETTING);
+                $url = new moodle_url($CFG->wwwroot .'/user/portfolio.php');
+                $usersetting->get($portfoliokey)->add(get_string('configure', 'portfolio'), $url, self::TYPE_SETTING);
+                $url = new moodle_url($CFG->wwwroot .'/user/portfoliologs.php');
+                $usersetting->get($portfoliokey)->add(get_string('logs', 'portfolio'), $url, self::TYPE_SETTING);
+            }
         }
 
         // Repository
index 4934324e113ab50df4a93a840d11e1c08a8b5fa9..99c6b7c230bec9bb68170cee1911eeaf5a88b759 100644 (file)
@@ -281,7 +281,7 @@ class portfolio_exporter {
     * @return boolean whether or not to process the next stage. this is important as the control function is called recursively.
     */
     public function process_stage_config() {
-        global $OUTPUT;
+        global $OUTPUT, $CFG;
         $pluginobj = $callerobj = null;
         if ($this->instance->has_export_config()) {
             $pluginobj = $this->instance;
@@ -306,6 +306,7 @@ class portfolio_exporter {
                 'formats' => $formats,
                 'expectedtime' => $expectedtime,
             );
+            require_once($CFG->libdir . '/portfolio/forms.php');
             $mform = new portfolio_export_form('', $customdata);
             if ($mform->is_cancelled()){
                 $this->cancel_request();
@@ -659,6 +660,9 @@ class portfolio_exporter {
     public static function rewaken_object($id) {
         global $DB, $CFG;
         require_once($CFG->libdir . '/filelib.php');
+        require_once($CFG->libdir . '/portfolio/exporter.php');
+        require_once($CFG->libdir . '/portfolio/caller.php');
+        require_once($CFG->libdir . '/portfolio/plugin.php');
         if (!$data = $DB->get_record('portfolio_tempdata', array('id' => $id))) {
             // maybe it's been finished already by a pull plugin
             // so look in the logs
@@ -713,7 +717,7 @@ class portfolio_exporter {
             throw new portfolio_exception('notyours', 'portfolio');
         }
         if (!$readonly && $this->get('instance') && !$this->get('instance')->allows_multiple_exports()
-            && ($already = portfolio_exporter::existing_exports($this->get('user')->id, $this->get('instance')->get('plugin')))
+            && ($already = portfolio_existing_exports($this->get('user')->id, $this->get('instance')->get('plugin')))
             && array_shift(array_keys($already)) != $this->get('id')
         ) {
             $a = (object)array(
@@ -848,40 +852,6 @@ class portfolio_exporter {
         exit;
     }
 
-    /**
-     * return a list of current exports for the given user
-     * this will not go through and call rewaken_object, because it's heavy
-     * it's really just used to figure out what exports are currently happening.
-     * this is useful for plugins that don't support multiple exports per session
-     *
-     * @param int $userid  the user to check for
-     * @param string $type (optional) the portfolio plugin to filter by
-     *
-     * @return array
-     */
-    public static function existing_exports($userid, $type=null) {
-        global $DB;
-        $sql = 'SELECT t.*,t.instance,i.plugin,i.name FROM {portfolio_tempdata} t JOIN {portfolio_instance} i ON t.instance = i.id WHERE t.userid = ? ';
-        $values = array($userid);
-        if ($type) {
-            $sql .= ' AND i.plugin = ?';
-            $values[] = $type;
-        }
-        return $DB->get_records_sql($sql, $values);
-    }
-
-    /**
-     * Return an array of existing exports by type for a given user.
-     * This is much more lightweight than {@see existing_exports} because it only returns the types, rather than the whole serialised data
-     * so can be used for checking availability of multiple plugins at the same time.
-     */
-    public static function existing_exports_by_plugin($userid) {
-        global $DB;
-        $sql = 'SELECT t.id,i.plugin FROM {portfolio_tempdata} t JOIN {portfolio_instance} i ON t.instance = i.id WHERE t.userid = ? ';
-        $values = array($userid);
-        return $DB->get_records_sql_menu($sql, $values);
-    }
-
     public static function print_cleaned_export($log, $instance=null) {
         global $CFG, $OUTPUT, $PAGE;
         if (empty($instance) || !$instance instanceof portfolio_plugin) {
index 352bc72e144d154dc81b707f97ec0d5d8a8ae890..44f961678c6f96954b1d3a38bf2d5f59b67afce7 100644 (file)
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
-/** require all the sublibraries first. */
-require_once($CFG->libdir . '/portfolio/constants.php');   // all the constants for time, export format etc.
+// require some of the sublibraries first.
+// this is not an exhaustive list, the others are pulled in as they're needed
+// so we don't have to always include everything unnecessarily for performance
+
+// very lightweight list of constants. always needed and no further dependencies
+require_once($CFG->libdir . '/portfolio/constants.php');
+// a couple of exception deinitions. always needed and no further dependencies
 require_once($CFG->libdir . '/portfolio/exceptions.php');  // exception classes used by portfolio code
-require_once($CFG->libdir . '/portfolio/formats.php');     // the export format hierarchy
-require_once($CFG->libdir . '/portfolio/forms.php');       // the form classes that subclass moodleform
-require_once($CFG->libdir . '/portfolio/exporter.php');    // the exporter class
-require_once($CFG->libdir . '/portfolio/plugin.php');      // the base classes for plugins
-require_once($CFG->libdir . '/portfolio/caller.php');      // the base classes for calling code
+// The base class for the caller classes. We always need this because we're either drawing a button,
+// in which case the button needs to know the calling class definition, which requires the base class,
+// or we're exporting, in which case we need the caller class anyway.
+require_once($CFG->libdir . '/portfolio/caller.php');
+
+// the other dependencies are included on demand:
+// libdir/portfolio/formats.php  - the classes for the export formats
+// libdir/portfolio/forms.php    - all portfolio form classes (requires formslib)
+// libdir/portfolio/plugin.php   - the base class for the export plugins
+// libdir/portfolio/exporter.php - the exporter class
+
 
 /**
  * use this to add a portfolio button or icon or form to a page
@@ -127,6 +138,7 @@ class portfolio_add_button {
             throw new portfolio_button_exception('nocallbackfile', 'portfolio', $file);
         }
         $this->callbackfile = $file;
+        require_once($CFG->libdir . '/portfolio/caller.php'); // require the base class first
         require_once($CFG->dirroot . $file);
         if (!class_exists($class)) {
             throw new portfolio_button_exception('nocallbackclass', 'portfolio', $class);
@@ -263,7 +275,7 @@ class portfolio_add_button {
                 debugging(get_string('instancemisconfigured', 'portfolio', get_string($error[$instance->get('id')], 'portfolio_' . $instance->get('plugin'))));
                 return;
             }
-            if (!$instance->allows_multiple_exports() && $already = portfolio_exporter::existing_exports($USER->id, $instance->get('plugin'))) {
+            if (!$instance->allows_multiple_exports() && $already = portfolio_existing_exports($USER->id, $instance->get('plugin'))) {
                 debugging(get_string('singleinstancenomultiallowed', 'portfolio'));
                 return;
             }
@@ -390,7 +402,7 @@ function portfolio_instance_select($instances, $callerformats, $callbackclass, $
 
     $count = 0;
     $selectoutput = "\n" . '<select name="' . $selectname . '">' . "\n";
-    $existingexports = portfolio_exporter::existing_exports_by_plugin($USER->id);
+    $existingexports = portfolio_existing_exports_by_plugin($USER->id);
     foreach ($instances as $instance) {
         $formats = portfolio_supported_formats_intersect($callerformats, $instance->supported_formats());
         if (count($formats) == 0) {
@@ -512,6 +524,7 @@ function portfolio_supported_formats() {
 * @return string the format constant (see PORTFOLIO_FORMAT_XXX constants)
 */
 function portfolio_format_from_file(stored_file $file) {
+    global $CFG;
     static $alreadymatched;
     if (empty($alreadymatched)) {
         $alreadymatched = array();
@@ -524,6 +537,7 @@ function portfolio_format_from_file(stored_file $file) {
         return $alreadymatched[$mimetype];
     }
     $allformats = portfolio_supported_formats();
+    require_once($CFG->libdir . '/portfolio/formats.php');
     foreach ($allformats as $format => $classname) {
         $supportedmimetypes = call_user_func(array($classname, 'mimetypes'));
         if (!is_array($supportedmimetypes)) {
@@ -550,6 +564,7 @@ function portfolio_format_from_file(stored_file $file) {
 * @param array $pluginformats formats the portfolio plugin supports
 */
 function portfolio_supported_formats_intersect($callerformats, $pluginformats) {
+    global $CFG;
     $allformats = portfolio_supported_formats();
     $intersection = array();
     foreach ($callerformats as $cf) {
@@ -559,6 +574,7 @@ function portfolio_supported_formats_intersect($callerformats, $pluginformats) {
             }
             continue;
         }
+        require_once($CFG->libdir . '/portfolio/formats.php');
         $cfobj = new $allformats[$cf]();
         foreach ($pluginformats as $p => $pf) {
             if (!array_key_exists($pf, $allformats)) {
@@ -614,6 +630,7 @@ function portfolio_format_is_abstract($format) {
 * @return array merged formats with dups removed
 */
 function portfolio_most_specific_formats($specificformats, $generalformats) {
+    global $CFG;
     $allformats = portfolio_supported_formats();
     if (empty($specificformats)) {
         return $generalformats;
@@ -623,10 +640,11 @@ function portfolio_most_specific_formats($specificformats, $generalformats) {
     foreach ($specificformats as $f) {
         // look for something less specific and remove it, ie outside of the inheritance tree of the current formats.
         if (!array_key_exists($f, $allformats)) {
-            if (!portfolio_format_is_abstract($pf)) {
+            if (!portfolio_format_is_abstract($f)) {
                 throw new portfolio_button_exception('invalidformat', 'portfolio', $f);
             }
         }
+        require_once($CFG->libdir . '/portfolio/formats.php');
         $fobj = new $allformats[$f];
         foreach ($generalformats as $key => $cf) {
             $cfclass = $allformats[$cf];
@@ -658,6 +676,8 @@ function portfolio_most_specific_formats($specificformats, $generalformats) {
 * @return portfolio_format object
 */
 function portfolio_format_object($name) {
+    global $CFG;
+    require_once($CFG->libdir . '/portfolio/formats.php');
     $formats = portfolio_supported_formats();
     return new $formats[$name];
 }
@@ -682,6 +702,7 @@ function portfolio_instance($instanceid, $record=null) {
             throw new portfolio_exception('invalidinstance', 'portfolio');
         }
     }
+    require_once($CFG->libdir . '/portfolio/plugin.php');
     require_once($CFG->dirroot . '/portfolio/'. $instance->plugin . '/lib.php');
     $classname = 'portfolio_plugin_' . $instance->plugin;
     return new $classname($instanceid, $instance);
@@ -717,6 +738,7 @@ function portfolio_static_function($plugin, $function) {
         array_shift($args);
     }
 
+    require_once($CFG->libdir . '/portfolio/plugin.php');
     require_once($CFG->dirroot . '/portfolio/' . $plugin .  '/lib.php');
     return call_user_func_array(array('portfolio_plugin_' . $plugin, $function), $args);
 }
@@ -873,6 +895,8 @@ function portfolio_fake_add_url($instanceid, $classname, $classfile, $callbackar
 */
 function portfolio_handle_event($eventdata) {
     global $CFG;
+
+    require_once($CFG->libdir . '/portfolio/exporter.php');
     $exporter = portfolio_exporter::rewaken_object($eventdata);
     $exporter->process_stage_package();
     $exporter->process_stage_send();
@@ -889,7 +913,7 @@ function portfolio_handle_event($eventdata) {
 */
 function portfolio_cron() {
     global $DB;
-
+    require_once($CFG->libdir . '/portfolio/exporter.php');
     if ($expired = $DB->get_records_select('portfolio_tempdata', 'expirytime < ?', array(time()), '', 'id')) {
         foreach ($expired as $d) {
             try {
@@ -1089,3 +1113,37 @@ function portfolio_export_type_to_id($type, $userid) {
     $sql = 'SELECT t.id FROM {portfolio_tempdata} t JOIN {portfolio_instance} i ON t.instance = i.id WHERE t.userid = ? AND i.plugin = ?';
     return $DB->get_field_sql($sql, array($userid, $type));
 }
+
+/**
+ * return a list of current exports for the given user
+ * this will not go through and call rewaken_object, because it's heavy
+ * it's really just used to figure out what exports are currently happening.
+ * this is useful for plugins that don't support multiple exports per session
+ *
+ * @param int $userid  the user to check for
+ * @param string $type (optional) the portfolio plugin to filter by
+ *
+ * @return array
+ */
+function portfolio_existing_exports($userid, $type=null) {
+    global $DB;
+    $sql = 'SELECT t.*,t.instance,i.plugin,i.name FROM {portfolio_tempdata} t JOIN {portfolio_instance} i ON t.instance = i.id WHERE t.userid = ? ';
+    $values = array($userid);
+    if ($type) {
+        $sql .= ' AND i.plugin = ?';
+        $values[] = $type;
+    }
+    return $DB->get_records_sql($sql, $values);
+}
+
+/**
+ * Return an array of existing exports by type for a given user.
+ * This is much more lightweight than {@see existing_exports} because it only returns the types, rather than the whole serialised data
+ * so can be used for checking availability of multiple plugins at the same time.
+ */
+function portfolio_existing_exports_by_plugin($userid) {
+    global $DB;
+    $sql = 'SELECT t.id,i.plugin FROM {portfolio_tempdata} t JOIN {portfolio_instance} i ON t.instance = i.id WHERE t.userid = ? ';
+    $values = array($userid);
+    return $DB->get_records_sql_menu($sql, $values);
+}
index d7547b4df4857b793ace9b7ece7150afd64ed26a..f8baef1925b41e2f896970a8d116511e7b84bbe7 100644 (file)
@@ -29,8 +29,6 @@
 require_once($CFG->libdir.'/eventslib.php');
 /** Include formslib.php */
 require_once($CFG->libdir.'/formslib.php');
-/** Include portfoliolib.php */
-require_once($CFG->libdir.'/portfoliolib.php');
 /** Include calendar/lib.php */
 require_once($CFG->dirroot.'/calendar/lib.php');
 
@@ -1805,6 +1803,8 @@ class assignment_base {
         $found = false;
 
         if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) {
+            require_once($CFG->libdir.'/portfoliolib.php');
+            require_once($CFG->dirroot . '/mod/assignment/locallib.php');
             $button = new portfolio_add_button();
             foreach ($files as $file) {
                 $filename = $file->get_filename();
@@ -1813,7 +1813,7 @@ class assignment_base {
                 $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
                 $output .= '<a href="'.$path.'" ><img src="'.$OUTPUT->old_icon_url(file_mimetype_icon($mimetype)).'" class="icon" alt="'.$mimetype.'" />'.s($filename).'</a>';
                 if ($this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) {
-                    $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()));
+                    $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()), '/mod/assignment/locallib.php');
                     $button->set_format_by_file($file);
                     $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                 }
@@ -3344,125 +3344,6 @@ function assignment_get_extra_capabilities() {
     return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames');
 }
 
-/**
- * @package   mod-assignment
- * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class assignment_portfolio_caller extends portfolio_module_caller_base {
-
-    /**
-    * the assignment subclass
-    */
-    private $assignment;
-
-    /**
-    * the file to include when waking up to load the assignment subclass def
-    */
-    private $assignmentfile;
-
-    /**
-    * callback arg for a single file export
-    */
-    protected $fileid;
-
-    public static function expected_callbackargs() {
-        return array(
-            'id'     => true,
-            'fileid' => false,
-        );
-    }
-
-    public function load_data() {
-        global $DB, $CFG;
-
-        if (! $this->cm = get_coursemodule_from_id('assignment', $this->id)) {
-            throw new portfolio_caller_exception('invalidcoursemodule');
-        }
-
-        if (! $assignment = $DB->get_record("assignment", array("id"=>$this->cm->instance))) {
-            throw new portfolio_caller_exception('invalidid', 'assignment');
-        }
-
-        $this->assignmentfile = '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php';
-        require_once($CFG->dirroot . $this->assignmentfile);
-        $assignmentclass = "assignment_$assignment->assignmenttype";
-
-        $this->assignment = new $assignmentclass($this->cm->id, $assignment, $this->cm);
-
-        if (!$this->assignment->portfolio_exportable()) {
-            throw new portfolio_caller_exception('notexportable', 'portfolio', $this->get_return_url());
-        }
-
-        $this->set_file_and_format_data($this->fileid, $this->assignment->context->id, 'assignment_submission', $this->user->id, 'timemodified', false);
-    }
-
-    public function prepare_package() {
-        global $CFG;
-        if (is_callable(array($this->assignment, 'portfolio_prepare_package'))) {
-            return $this->assignment->portfolio_prepare_package($this->exporter, $this->user->id);
-        }
-        if ($this->exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
-            $leapwriter = $this->exporter->get('format')->leap2a_writer();
-            $files = array();
-            if ($this->singlefile) {
-                $files[] = $this->singlefile;
-            } elseif ($this->multifiles) {
-                $files = $this->multifiles;
-            } else {
-                throw new portfolio_caller_exception('invalidpreparepackagefile', 'portfolio', $this->get_return_url());
-            }
-            $baseid = 'assignment' . $this->assignment->assignment->assignmenttype . $this->assignment->assignment->id . 'submission';
-            foreach ($files as $file) {
-                $id = $baseid . $file->get_id();
-                $entry = new portfolio_format_leap2a_entry($id, $file->get_filename(), 'resource',  $file);
-                $entry->add_category('offline', 'resource_type');
-                $leapwriter->add_entry($entry);
-                $this->exporter->copy_existing_file($file);
-            }
-            return $this->exporter->write_new_file($leapwriter->to_xml(), $this->exporter->get('format')->manifest_name(), true);
-        }
-        return $this->prepare_package_file();
-    }
-
-    public function get_sha1() {
-        global $CFG;
-        if (is_callable(array($this->assignment, 'portfolio_get_sha1'))) {
-            return $this->assignment->portfolio_get_sha1($this->user->id);
-        }
-        return $this->get_sha1_file();
-    }
-
-    public function expected_time() {
-        if (is_callable(array($this->assignment, 'portfolio_get_expected_time'))) {
-            return $this->assignment->portfolio_get_expected_time();
-        }
-        return $this->expected_time_file();
-    }
-
-    public function check_permissions() {
-        $context = get_context_instance(CONTEXT_MODULE, $this->assignment->cm->id);
-        return has_capability('mod/assignment:exportownsubmission', $context);
-    }
-
-    public function __wakeup() {
-        global $CFG;
-        if (empty($CFG)) {
-            return true; // too early yet
-        }
-        require_once($CFG->dirroot . $this->assignmentfile);
-        $this->assignment = unserialize(serialize($this->assignment));
-    }
-
-    public static function display_name() {
-        return get_string('modulename', 'assignment');
-    }
-
-    public static function base_supported_formats() {
-        return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_LEAP2A);
-    }
-}
-
 /**
  * @param string $feature FEATURE_xx constant for requested feature
  * @return mixed True if module supports feature, null if doesn't know
diff --git a/mod/assignment/locallib.php b/mod/assignment/locallib.php
new file mode 100644 (file)
index 0000000..ec4f013
--- /dev/null
@@ -0,0 +1,140 @@
+<?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 all the functions that aren't needed by core moodle
+// but start becoming required once we're actually inside the assignment module.
+
+require_once($CFG->libdir . '/portfolio/caller.php');
+
+/**
+ * @package   mod-assignment
+ * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class assignment_portfolio_caller extends portfolio_module_caller_base {
+
+    /**
+    * the assignment subclass
+    */
+    private $assignment;
+
+    /**
+    * the file to include when waking up to load the assignment subclass def
+    */
+    private $assignmentfile;
+
+    /**
+    * callback arg for a single file export
+    */
+    protected $fileid;
+
+    public static function expected_callbackargs() {
+        return array(
+            'id'     => true,
+            'fileid' => false,
+        );
+    }
+
+    public function load_data() {
+        global $DB, $CFG;
+
+        if (! $this->cm = get_coursemodule_from_id('assignment', $this->id)) {
+            throw new portfolio_caller_exception('invalidcoursemodule');
+        }
+
+        if (! $assignment = $DB->get_record("assignment", array("id"=>$this->cm->instance))) {
+            throw new portfolio_caller_exception('invalidid', 'assignment');
+        }
+
+        $this->assignmentfile = '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php';
+        require_once($CFG->dirroot . $this->assignmentfile);
+        $assignmentclass = "assignment_$assignment->assignmenttype";
+
+        $this->assignment = new $assignmentclass($this->cm->id, $assignment, $this->cm);
+
+        if (!$this->assignment->portfolio_exportable()) {
+            throw new portfolio_caller_exception('notexportable', 'portfolio', $this->get_return_url());
+        }
+
+        $this->set_file_and_format_data($this->fileid, $this->assignment->context->id, 'assignment_submission', $this->user->id, 'timemodified', false);
+    }
+
+    public function prepare_package() {
+        global $CFG;
+        if (is_callable(array($this->assignment, 'portfolio_prepare_package'))) {
+            return $this->assignment->portfolio_prepare_package($this->exporter, $this->user->id);
+        }
+        if ($this->exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
+            $leapwriter = $this->exporter->get('format')->leap2a_writer();
+            $files = array();
+            if ($this->singlefile) {
+                $files[] = $this->singlefile;
+            } elseif ($this->multifiles) {
+                $files = $this->multifiles;
+            } else {
+                throw new portfolio_caller_exception('invalidpreparepackagefile', 'portfolio', $this->get_return_url());
+            }
+            $baseid = 'assignment' . $this->assignment->assignment->assignmenttype . $this->assignment->assignment->id . 'submission';
+            foreach ($files as $file) {
+                $id = $baseid . $file->get_id();
+                $entry = new portfolio_format_leap2a_entry($id, $file->get_filename(), 'resource',  $file);
+                $entry->add_category('offline', 'resource_type');
+                $leapwriter->add_entry($entry);
+                $this->exporter->copy_existing_file($file);
+            }
+            return $this->exporter->write_new_file($leapwriter->to_xml(), $this->exporter->get('format')->manifest_name(), true);
+        }
+        return $this->prepare_package_file();
+    }
+
+    public function get_sha1() {
+        global $CFG;
+        if (is_callable(array($this->assignment, 'portfolio_get_sha1'))) {
+            return $this->assignment->portfolio_get_sha1($this->user->id);
+        }
+        return $this->get_sha1_file();
+    }
+
+    public function expected_time() {
+        if (is_callable(array($this->assignment, 'portfolio_get_expected_time'))) {
+            return $this->assignment->portfolio_get_expected_time();
+        }
+        return $this->expected_time_file();
+    }
+
+    public function check_permissions() {
+        $context = get_context_instance(CONTEXT_MODULE, $this->assignment->cm->id);
+        return has_capability('mod/assignment:exportownsubmission', $context);
+    }
+
+    public function __wakeup() {
+        global $CFG;
+        if (empty($CFG)) {
+            return true; // too early yet
+        }
+        require_once($CFG->dirroot . $this->assignmentfile);
+        $this->assignment = unserialize(serialize($this->assignment));
+    }
+
+    public static function display_name() {
+        return get_string('modulename', 'assignment');
+    }
+
+    public static function base_supported_formats() {
+        return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_LEAP2A);
+    }
+}
index 120860804e6086a4013ce232d32fbb80c5e301df..1b2b4ad8c2854ce38907822455fbe7cdf2e4bd28 100644 (file)
@@ -23,8 +23,6 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
-/** Include portfoliolib.php */
-require_once($CFG->libdir.'/portfoliolib.php');
 require_once($CFG->dirroot.'/calendar/lib.php');
 
 // The HTML head for the message window to start with (<!-- nix --> is used to get some browsers starting with output
@@ -1219,138 +1217,6 @@ function chat_get_extra_capabilities() {
     return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames');
 }
 
-/**
- * @package   mod-chat
- * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class chat_portfolio_caller extends portfolio_module_caller_base {
-    /** @var object */
-    private $chat;
-    /** @var int Timestamp */
-    protected $start;
-    /** @var int Timestamp */
-    protected $end;
-    /**
-     * @return array
-     */
-    public static function expected_callbackargs() {
-        return array(
-            'id'    => true,
-            'start' => false,
-            'end'   => false,
-        );
-    }
-    /**
-     * @global object
-     */
-    public function load_data() {
-        global $DB;
-
-        if (!$this->cm = get_coursemodule_from_id('chat', $this->id)) {
-            throw new portfolio_caller_exception('invalidid', 'chat');
-        }
-        $this->chat = $DB->get_record('chat', array('id' => $this->cm->instance));
-        $select = 'chatid = ?';
-        $params = array($this->chat->id);
-        if ($this->start && $this->end) {
-            $select .= ' AND timestamp >= ? AND timestamp <= ?';
-            $params[] = $this->start;
-            $params[] = $this->end;
-        }
-        $this->messages = $DB->get_records_select(
-                'chat_messages',
-                $select,
-                $params,
-                'timestamp ASC'
-            );
-        $select .= ' AND userid = ?';
-        $params[] = $this->user->id;
-        $this->participated = $DB->record_exists_select(
-            'chat_messages',
-            $select,
-            $params
-        );
-    }
-    /**
-     * @return array
-     */
-    public static function base_supported_formats() {
-        return array(PORTFOLIO_FORMAT_PLAINHTML);
-    }
-    /**
-     *
-     */
-    public function expected_time() {
-        return portfolio_expected_time_db(count($this->messages));
-    }
-    /**
-     * @return string
-     */
-    public function get_sha1() {
-        $str = '';
-        ksort($this->messages);
-        foreach ($this->messages as $m) {
-            $str .= implode('', (array)$m);
-        }
-        return sha1($str);
-    }
-
-    /**
-     * @return bool
-     */
-    public function check_permissions() {
-        $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
-        return has_capability('mod/chat:exportsession', $context)
-            || ($this->participated
-                && has_capability('mod/chat:exportparticipatedsession', $context));
-    }
-
-    /**
-     * @todo Document this function
-     */
-    public function prepare_package() {
-        $content = '';
-        $lasttime = 0;
-        $sessiongap = 5 * 60;    // 5 minutes silence means a new session
-        foreach ($this->messages as $message) {  // We are walking FORWARDS through messages
-            $m = clone $message; // grrrrrr - this causes the sha1 to change as chat_format_message changes what it's passed.
-            $formatmessage = chat_format_message($m, null, $this->user);
-            if (!isset($formatmessage->html)) {
-                continue;
-            }
-            if (empty($lasttime) || (($message->timestamp - $lasttime) > $sessiongap)) {
-                $content .= '<hr />';
-                $content .= userdate($message->timestamp);
-            }
-            $content .= $formatmessage->html;
-            $lasttime = $message->timestamp;
-        }
-        $content = preg_replace('/\<img[^>]*\>/', '', $content);
-
-        $this->exporter->write_new_file($content, clean_filename($this->cm->name . '-session.html'), false);
-    }
-
-    /**
-     * @return string
-     */
-    public static function display_name() {
-        return get_string('modulename', 'chat');
-    }
-
-    /**
-     * @global object
-     * @return string
-     */
-    public function get_return_url() {
-        global $CFG;
-
-        return $CFG->wwwroot . '/mod/chat/report.php?id='
-            . $this->cm->id . ((isset($this->start))
-                ? '&start=' . $this->start . '&end=' . $this->end
-                : '');
-    }
-}
 
 /**
  * @param string $feature FEATURE_xx constant for requested feature
diff --git a/mod/chat/locallib.php b/mod/chat/locallib.php
new file mode 100644 (file)
index 0000000..45de13b
--- /dev/null
@@ -0,0 +1,155 @@
+<?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/>.
+
+/**
+ * Library of functions for chat outside of the core api
+ */
+
+require_once($CFG->libdir . '/portfolio/caller.php');
+
+/**
+ * @package   mod-chat
+ * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class chat_portfolio_caller extends portfolio_module_caller_base {
+    /** @var object */
+    private $chat;
+    /** @var int Timestamp */
+    protected $start;
+    /** @var int Timestamp */
+    protected $end;
+    /**
+     * @return array
+     */
+    public static function expected_callbackargs() {
+        return array(
+            'id'    => true,
+            'start' => false,
+            'end'   => false,
+        );
+    }
+    /**
+     * @global object
+     */
+    public function load_data() {
+        global $DB;
+
+        if (!$this->cm = get_coursemodule_from_id('chat', $this->id)) {
+            throw new portfolio_caller_exception('invalidid', 'chat');
+        }
+        $this->chat = $DB->get_record('chat', array('id' => $this->cm->instance));
+        $select = 'chatid = ?';
+        $params = array($this->chat->id);
+        if ($this->start && $this->end) {
+            $select .= ' AND timestamp >= ? AND timestamp <= ?';
+            $params[] = $this->start;
+            $params[] = $this->end;
+        }
+        $this->messages = $DB->get_records_select(
+                'chat_messages',
+                $select,
+                $params,
+                'timestamp ASC'
+            );
+        $select .= ' AND userid = ?';
+        $params[] = $this->user->id;
+        $this->participated = $DB->record_exists_select(
+            'chat_messages',
+            $select,
+            $params
+        );
+    }
+    /**
+     * @return array
+     */
+    public static function base_supported_formats() {
+        return array(PORTFOLIO_FORMAT_PLAINHTML);
+    }
+    /**
+     *
+     */
+    public function expected_time() {
+        return portfolio_expected_time_db(count($this->messages));
+    }
+    /**
+     * @return string
+     */
+    public function get_sha1() {
+        $str = '';
+        ksort($this->messages);
+        foreach ($this->messages as $m) {
+            $str .= implode('', (array)$m);
+        }
+        return sha1($str);
+    }
+
+    /**
+     * @return bool
+     */
+    public function check_permissions() {
+        $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
+        return has_capability('mod/chat:exportsession', $context)
+            || ($this->participated
+                && has_capability('mod/chat:exportparticipatedsession', $context));
+    }
+
+    /**
+     * @todo Document this function
+     */
+    public function prepare_package() {
+        $content = '';
+        $lasttime = 0;
+        $sessiongap = 5 * 60;    // 5 minutes silence means a new session
+        foreach ($this->messages as $message) {  // We are walking FORWARDS through messages
+            $m = clone $message; // grrrrrr - this causes the sha1 to change as chat_format_message changes what it's passed.
+            $formatmessage = chat_format_message($m, $this->course->id, $this->user);
+            if (!isset($formatmessage->html)) {
+                continue;
+            }
+            if (empty($lasttime) || (($message->timestamp - $lasttime) > $sessiongap)) {
+                $content .= '<hr />';
+                $content .= userdate($message->timestamp);
+            }
+            $content .= $formatmessage->html;
+            $lasttime = $message->timestamp;
+        }
+        $content = preg_replace('/\<img[^>]*\>/', '', $content);
+
+        $this->exporter->write_new_file($content, clean_filename($this->cm->name . '-session.html'), false);
+    }
+
+    /**
+     * @return string
+     */
+    public static function display_name() {
+        return get_string('modulename', 'chat');
+    }
+
+    /**
+     * @global object
+     * @return string
+     */
+    public function get_return_url() {
+        global $CFG;
+
+        return $CFG->wwwroot . '/mod/chat/report.php?id='
+            . $this->cm->id . ((isset($this->start))
+                ? '&start=' . $this->start . '&end=' . $this->end
+                : '');
+    }
+}
index 6d4f3abb528b9424e7bf50cf22e86b072b073f8d..5cd9aa825acc09c3fda0c074d9b5ac8af3c0f401 100644 (file)
                         'end'   => $end,
                     );
                     $button = new portfolio_add_button();
-                    $button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/lib.php');
+                    $button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/locallib.php');
                     $button->render();
                 }
             echo $OUTPUT->box_end();
                         'end'   => $sessionend,
                     );
                     $button = new portfolio_add_button();
-                    $button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/lib.php');
+                    $button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/locallib.php');
                     $button->render(PORTFOLIO_ADD_TEXT_LINK);
                 }
                 if (has_capability('mod/chat:deletelog', $context)) {
     if (has_capability('mod/chat:exportsession', $context)) {
         require_once($CFG->libdir . '/portfoliolib.php');
         $button = new portfolio_add_button();
-        $button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id), '/mod/chat/lib.php');
+        $button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id), '/mod/chat/locallib.php');
         $button->render(null, get_string('addalltoportfolio', 'portfolio'));
     }
 
index 244b09f42c0e0507688470bd32f8cb3f27e0afcf..bf2b588db16f7ed4e6185632e50cede7b5c520a7 100644 (file)
@@ -101,7 +101,8 @@ if (array_key_exists('portfolio', $formdata) && !empty($formdata['portfolio']))
     // fake  portfolio callback stuff and redirect
     $formdata['id'] = $cm->id;
     $formdata['exporttype'] = 'csv'; // force for now
-    $url = portfolio_fake_add_url($formdata['portfolio'], 'data_portfolio_caller', '/mod/data/lib.php', $formdata, array());
+    require_once($CFG->libdir . '/portfoliolib.php');
+    $url = portfolio_fake_add_url($formdata['portfolio'], 'data_portfolio_caller', '/mod/data/locallib.php', $formdata, array(PORTFOLIO_FORMAT_SPREADSHEET));
     redirect($url);
 }
 
index 46f60c8dd332bc71e40f38ad7af4ff90dffe26a9..22799c19c12023c790014c62b0422c950d4690b7 100644 (file)
@@ -59,11 +59,12 @@ class mod_data_export_form extends moodleform {
         }
         $this->add_checkbox_controller(1, null, null, 1);
         require_once($CFG->libdir . '/portfoliolib.php');
+        require_once($CFG->dirroot . '/mod/data/locallib.php');
         if ($CFG->enableportfolios && has_capability('mod/data:exportallentries', get_context_instance(CONTEXT_MODULE, $this->_cm->id))) {
             if ($portfoliooptions = portfolio_instance_select(
                 portfolio_instances(),
                 call_user_func(array('data_portfolio_caller', 'base_supported_formats')),
-                'data_portfolio_caller', null, '', true, true)) {
+                'data_portfolio_caller', '/mod/data/locallib.php', '', true, true)) {
                 $mform->addElement('header', 'notice', get_string('portfolionotfile', 'data') . ':');
                 $portfoliooptions[0] = get_string('none');
                 ksort($portfoliooptions);
index 3ce3ac4775e046f7f64069b80b5f986a79f4e1a6..ebfba40b4b4773022e050fabd38e7ec3961b6b46 100755 (executable)
@@ -21,9 +21,6 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
-/** Require portfoliolib.php */
-require_once($CFG->libdir . '/portfoliolib.php');
-
 // Some constants
 define ('DATA_MAX_ENTRIES', 50);
 define ('DATA_PERPAGE_SINGLE', 1);
@@ -1253,8 +1250,9 @@ function data_print_template($template, $records, $data, $search='', $page=0, $r
         if (($template == 'singletemplate' || $template == 'listtemplate')
             && ((has_capability('mod/data:exportentry', $context)
                 || (data_isowner($record->id) && has_capability('mod/data:exportownentry', $context))))) {
+            require_once($CFG->libdir . '/portfoliolib.php');
             $button = new portfolio_add_button();
-            $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id, 'recordid' => $record->id));
+            $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id, 'recordid' => $record->id), '/mod/data/locallib.php');
             list($formats, $files) = data_portfolio_caller::formats($fields, $record);
             $button->set_formats($formats);
             $replacement[] = $button->to_html(PORTFOLIO_ADD_ICON_LINK);
@@ -2835,297 +2833,6 @@ function data_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedow
     return false;
 }
 
-/**
- * @package   mod-data
- * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-class data_portfolio_caller extends portfolio_module_caller_base {
-
-    /** @var int */
-    protected $recordid;
-    /** @var string */
-    protected $exporttype;
-    /** @var string */
-    protected $delimiter_name;
-
-    /** @var object */
-    private $data;
-    /**#@+ @var array */
-    private $selectedfields;
-    private $fields;
-    private $fieldtypes;
-    private $exportdata;
-    /**#@-*/
-    /**#@+ @var object */
-    private $singlerecord;
-    private $singlefield;
-    /**#@-*/
-    /**
-     * @return array
-     */
-    public static function expected_callbackargs() {
-        return array(
-            'id'             => true,
-            'recordid'       => false,
-            'delimiter_name' => false,
-            'exporttype'     => false,
-        );
-    }
-    /**
-     * @param array $callbackargs
-     */
-    public function __construct($callbackargs) {
-        parent::__construct($callbackargs);
-        if (empty($this->exporttype)) {
-            $this->exporttype = 'csv';
-        }
-        $this->selectedfields = array();
-        foreach ($callbackargs as $key => $value) {
-            if (strpos($key, 'field_') === 0) {
-                $this->selectedfields[] = substr($key, 6);
-            }
-        }
-    }
-
-    /**
-     * @global object
-     */
-    public function load_data() {
-        global $DB;
-        if (!$this->cm = get_coursemodule_from_id('data', $this->id)) {
-            throw new portfolio_caller_exception('invalidid', 'data');
-        }
-        $this->data = $DB->get_record('data', array('id' => $this->cm->instance));
-        $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$this->cm->instance), 'id');
-        // populate objets for this databases fields
-        $this->fields = array();
-        foreach ($fieldrecords as $fieldrecord) {
-            $tmp = data_get_field($fieldrecord, $this->data);
-            $this->fields[] = $tmp;
-            $this->fieldtypes[]  = $tmp->type;
-        }
-
-        if ($this->recordid) {
-            //user has selected to export one single entry rather than the whole thing
-            // which is completely different
-            $this->singlerecord = $DB->get_record('data_records', array('id' => $this->recordid));
-            $this->singlerecord->content = $DB->get_records('data_content', array('recordid' => $this->singlerecord->id));
-            $this->exporttype = 'single';
-
-            list($formats, $files) = self::formats($this->fields, $this->singlerecord);
-            if (count($files) == 1 && count($this->fields) == 1) {
-                $this->singlefile = $files[0];
-                $this->exporttype = 'singlefile';
-            } else if (count($files) > 0) {
-                $this->multifiles = $files;
-            }
-        } else {
-            // all records as csv or whatever
-            $this->exportdata = data_get_exportdata($this->cm->instance, $this->fields, $this->selectedfields);
-        }
-    }
-
-    /**
-     * @todo penny  later when we suport exporting to more than just csv, we may
-     * need to ask the user here if we have not already passed it
-     *
-     * @return bool
-     */
-    public function has_export_config() {
-        return false;
-    }
-
-    /**
-     * @uses PORTFOLIO_TIME_LOW
-     * @return mixed
-     */
-    public function expected_time() {
-        if ($this->exporttype == 'single') {
-            return PORTFOLIO_TIME_LOW;
-        }
-        return portfolio_expected_time_db(count($this->exportdata));
-    }
-
-    /**
-     * @return string
-     */
-    public function get_sha1() {
-        if ($this->exporttype == 'singlefile') {
-            return $this->singlefile->get_contenthash();
-        }
-        $loopdata = $this->exportdata;
-        if ($this->exporttype == 'single') {
-            $loopdata = $this->singlerecord;
-        }
-        $str = '';
-        foreach ($loopdata as $data) {
-            if (is_array($data) || is_object($data)) {
-                $testkey = array_pop(array_keys($data));
-                if (is_array($data[$testkey]) || is_object($data[$testkey])) {
-                    foreach ($data as $d) {
-                        $str .= implode(',', (array)$d);
-                    }
-                } else {
-                    $str .= implode(',', (array)$data);
-                }
-            } else {
-                $str .= $data;
-            }
-        }
-        return sha1($str . ',' . $this->exporttype);
-    }
-    /**
-     * @global object
-     */
-    public function prepare_package() {
-        global $DB;
-        $count = count($this->exportdata);
-        $content = '';
-        $filename = '';
-        switch ($this->exporttype) {
-            case 'singlefile':
-                return $this->get('exporter')->copy_existing_file($this->singlefile);
-            case 'single':
-                $content = $this->exportsingle();
-                $filename = clean_filename($this->cm->name . '-entry.html');
-                break;
-            case 'csv':
-                $content = data_export_csv($this->exportdata, $this->delimiter_name, $this->cm->name, $count, true);
-                $filename = clean_filename($this->cm->name . '.csv');
-                break;
-            case 'xls':
-                throw new portfolio_caller_exception('notimplemented', 'portfolio', '', 'xls');
-                $content = data_export_xls($this->exportdata, $this->cm->name, $count, true);
-                break;
-            case 'ods':
-                throw new portfolio_caller_exception('notimplemented', 'portfolio', '', 'ods');
-                $content = data_export_ods($this->exportdata, $this->cm->name, $count, true);
-                break;
-            default:
-                throw new portfolio_caller_exception('notimplemented', 'portfolio', '', $this->exporttype);
-            break;
-        }
-        return $this->exporter->write_new_file(
-            $content,
-            $filename,
-            ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH) // if we have associate files, this is a 'manifest'
-        );
-    }
-
-    /**
-     * @return bool
-     */
-    public function check_permissions() {
-        return has_capability('mod/data:exportallentries', get_context_instance(CONTEXT_MODULE, $this->cm->id));
-    }
-
-    /**
-     *  @return string
-     */
-    public static function display_name() {
-        return get_string('modulename', 'data');
-    }
-
-    /**
-     * @global object
-     * @return bool|void
-     */
-    public function __wakeup() {
-        global $CFG;
-        if (empty($CFG)) {
-            return true; // too early yet
-        }
-        foreach ($this->fieldtypes as $key => $field) {
-            require_once($CFG->dirroot . '/mod/data/field/' . $field .'/field.class.php');
-            $this->fields[$key] = unserialize(serialize($this->fields[$key]));
-        }
-    }
-
-    /**
-     * @global object
-     * @return string
-     */
-    private function exportsingle() {
-        global $DB;
-    // Replacing tags
-        $patterns = array();
-        $replacement = array();
-        $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
-
-    // Then we generate strings to replace for normal tags
-        $format = $this->get('exporter')->get('format');
-        foreach ($this->fields as $field) {
-            $patterns[]='[['.$field->field->name.']]';
-            if (is_callable(array($field, 'get_file'))) {
-                // TODO this used to be:
-                // if ($field instanceof data_field_file) {
-                // - see  MDL-16493
-                if (!$file = $field->get_file($this->singlerecord->id)) {
-                    $replacement[] = '';
-                    continue; // probably left empty
-                }
-                $replacement[] = $format->file_output($file);
-                $this->get('exporter')->copy_existing_file($file);
-            } else {
-                $replacement[] = $field->display_browse_field($this->singlerecord->id, 'singletemplate');
-            }
-        }
-
-    // Replacing special tags (##Edit##, ##Delete##, ##More##)
-        $patterns[]='##edit##';
-        $patterns[]='##delete##';
-        $patterns[]='##export##';
-        $patterns[]='##more##';
-        $patterns[]='##moreurl##';
-        $patterns[]='##user##';
-        $patterns[]='##approve##';
-        $patterns[]='##comments##';
-        $patterns[] = '##timeadded##';
-        $patterns[] = '##timemodified##';
-        $replacement[] = '';
-        $replacement[] = '';
-        $replacement[] = '';
-        $replacement[] = '';
-        $replacement[] = '';
-        $replacement[] = '';
-        $replacement[] = '';
-        $replacement[] = '';
-        $replacement[] = userdate($this->singlerecord->timecreated);
-        $replacement[] = userdate($this->singlerecord->timemodified);
-
-        // actual replacement of the tags
-        return str_ireplace($patterns, $replacement, $this->data->singletemplate);
-    }
-
-    /**
-     * @param array $fields
-     * @param object $record
-     * @uses PORTFOLIO_FORMAT_PLAINHTML
-     * @uses PORTFOLIO_FORMAT_RICHHTML
-     * @return array
-     */
-    public static function formats($fields, $record) {
-        $formats = array(PORTFOLIO_FORMAT_PLAINHTML);
-        $includedfiles = array();
-        foreach ($fields as $singlefield) {
-            if (is_callable(array($singlefield, 'get_file'))) {
-                $includedfiles[] = $singlefield->get_file($record->id);
-            }
-        }
-        if (count($includedfiles) == 1 && count($fields) == 1) {
-            $formats= array(portfolio_format_from_file($includedfiles[0]));
-        } else if (count($includedfiles) > 0) {
-            $formats = array(PORTFOLIO_FORMAT_RICHHTML);
-        }
-        return array($formats, $includedfiles);
-    }
-
-    public static function base_supported_formats() {
-        return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML);
-    }
-}
 
 function data_extend_navigation($navigation, $course, $module, $cm) {
     global $CFG, $OUTPUT, $USER, $DB;
diff --git a/mod/data/locallib.php b/mod/data/locallib.php
new file mode 100644 (file)
index 0000000..39f82a8
--- /dev/null
@@ -0,0 +1,314 @@
+<?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/>.
+
+/**
+ * @package   mod-data
+ * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * @package   mod-data
+ * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+class data_portfolio_caller extends portfolio_module_caller_base {
+
+    /** @var int */
+    protected $recordid;
+    /** @var string */
+    protected $exporttype;
+    /** @var string */
+    protected $delimiter_name;
+
+    /** @var object */
+    private $data;
+    /**#@+ @var array */
+    private $selectedfields;
+    private $fields;
+    private $fieldtypes;
+    private $exportdata;
+    /**#@-*/
+    /**#@+ @var object */
+    private $singlerecord;
+    private $singlefield;
+    /**#@-*/
+    /**
+     * @return array
+     */
+    public static function expected_callbackargs() {
+        return array(
+            'id'             => true,
+            'recordid'       => false,
+            'delimiter_name' => false,
+            'exporttype'     => false,
+        );
+    }
+    /**
+     * @param array $callbackargs
+     */
+    public function __construct($callbackargs) {
+        parent::__construct($callbackargs);
+        if (empty($this->exporttype)) {
+            $this->exporttype = 'csv';
+        }
+        $this->selectedfields = array();
+        foreach ($callbackargs as $key => $value) {
+            if (strpos($key, 'field_') === 0) {
+                $this->selectedfields[] = substr($key, 6);
+            }
+        }
+    }
+
+    /**
+     * @global object
+     */
+    public function load_data() {
+        global $DB;
+        if (!$this->cm = get_coursemodule_from_id('data', $this->id)) {
+            throw new portfolio_caller_exception('invalidid', 'data');
+        }
+        $this->data = $DB->get_record('data', array('id' => $this->cm->instance));
+        $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$this->cm->instance), 'id');
+        // populate objets for this databases fields
+        $this->fields = array();
+        foreach ($fieldrecords as $fieldrecord) {
+            $tmp = data_get_field($fieldrecord, $this->data);
+            $this->fields[] = $tmp;
+            $this->fieldtypes[]  = $tmp->type;
+        }
+
+        if ($this->recordid) {
+            //user has selected to export one single entry rather than the whole thing
+            // which is completely different
+            $this->singlerecord = $DB->get_record('data_records', array('id' => $this->recordid));
+            $this->singlerecord->content = $DB->get_records('data_content', array('recordid' => $this->singlerecord->id));
+            $this->exporttype = 'single';
+
+            list($formats, $files) = self::formats($this->fields, $this->singlerecord);
+            if (count($files) == 1 && count($this->fields) == 1) {
+                $this->singlefile = $files[0];
+                $this->exporttype = 'singlefile';
+            } else if (count($files) > 0) {
+                $this->multifiles = $files;
+            }
+        } else {
+            // all records as csv or whatever
+            $this->exportdata = data_get_exportdata($this->cm->instance, $this->fields, $this->selectedfields);
+        }
+    }
+
+    /**
+     * @todo penny  later when we suport exporting to more than just csv, we may
+     * need to ask the user here if we have not already passed it
+     *
+     * @return bool
+     */
+    public function has_export_config() {
+        return false;
+    }
+
+    /**
+     * @uses PORTFOLIO_TIME_LOW
+     * @return mixed
+     */
+    public function expected_time() {
+        if ($this->exporttype == 'single') {
+            return PORTFOLIO_TIME_LOW;
+        }
+        return portfolio_expected_time_db(count($this->exportdata));
+    }
+
+    /**
+     * @return string
+     */
+    public function get_sha1() {
+        if ($this->exporttype == 'singlefile') {
+            return $this->singlefile->get_contenthash();
+        }
+        $loopdata = $this->exportdata;
+        if ($this->exporttype == 'single') {
+            $loopdata = $this->singlerecord;
+        }
+        $str = '';
+        foreach ($loopdata as $data) {
+            if (is_array($data) || is_object($data)) {
+                $testkey = array_pop(array_keys($data));
+                if (is_array($data[$testkey]) || is_object($data[$testkey])) {
+                    foreach ($data as $d) {
+                        $str .= implode(',', (array)$d);
+                    }
+                } else {
+                    $str .= implode(',', (array)$data);
+                }
+            } else {
+                $str .= $data;
+            }
+        }
+        return sha1($str . ',' . $this->exporttype);
+    }
+    /**
+     * @global object
+     */
+    public function prepare_package() {
+        global $DB;
+        $count = count($this->exportdata);
+        $content = '';
+        $filename = '';
+        switch ($this->exporttype) {
+            case 'singlefile':
+                return $this->get('exporter')->copy_existing_file($this->singlefile);
+            case 'single':
+                $content = $this->exportsingle();
+                $filename = clean_filename($this->cm->name . '-entry.html');
+                break;
+            case 'csv':
+                $content = data_export_csv($this->exportdata, $this->delimiter_name, $this->cm->name, $count, true);
+                $filename = clean_filename($this->cm->name . '.csv');
+                break;
+            case 'xls':
+                throw new portfolio_caller_exception('notimplemented', 'portfolio', '', 'xls');
+                $content = data_export_xls($this->exportdata, $this->cm->name, $count, true);
+                break;
+            case 'ods':
+                throw new portfolio_caller_exception('notimplemented', 'portfolio', '', 'ods');
+                $content = data_export_ods($this->exportdata, $this->cm->name, $count, true);
+                break;
+            default:
+                throw new portfolio_caller_exception('notimplemented', 'portfolio', '', $this->exporttype);
+            break;
+        }
+        return $this->exporter->write_new_file(
+            $content,
+            $filename,
+            ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH) // if we have associate files, this is a 'manifest'
+        );
+    }
+
+    /**
+     * @return bool
+     */
+    public function check_permissions() {
+        return has_capability('mod/data:exportallentries', get_context_instance(CONTEXT_MODULE, $this->cm->id));
+    }
+
+    /**
+     *  @return string
+     */
+    public static function display_name() {
+        return get_string('modulename', 'data');
+    }
+
+    /**
+     * @global object
+     * @return bool|void
+     */
+    public function __wakeup() {
+        global $CFG;
+        if (empty($CFG)) {
+            return true; // too early yet
+        }
+        foreach ($this->fieldtypes as $key => $field) {
+            require_once($CFG->dirroot . '/mod/data/field/' . $field .'/field.class.php');
+            $this->fields[$key] = unserialize(serialize($this->fields[$key]));
+        }
+    }
+
+    /**
+     * @global object
+     * @return string
+     */
+    private function exportsingle() {
+        global $DB;
+    // Replacing tags
+        $patterns = array();
+        $replacement = array();
+        $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
+
+    // Then we generate strings to replace for normal tags
+        $format = $this->get('exporter')->get('format');
+        foreach ($this->fields as $field) {
+            $patterns[]='[['.$field->field->name.']]';
+            if (is_callable(array($field, 'get_file'))) {
+                // TODO this used to be:
+                // if ($field instanceof data_field_file) {
+                // - see  MDL-16493
+                if (!$file = $field->get_file($this->singlerecord->id)) {
+                    $replacement[] = '';
+                    continue; // probably left empty
+                }
+                $replacement[] = $format->file_output($file);
+                $this->get('exporter')->copy_existing_file($file);
+            } else {
+                $replacement[] = $field->display_browse_field($this->singlerecord->id, 'singletemplate');
+            }
+        }
+
+    // Replacing special tags (##Edit##, ##Delete##, ##More##)
+        $patterns[]='##edit##';
+        $patterns[]='##delete##';
+        $patterns[]='##export##';
+        $patterns[]='##more##';
+        $patterns[]='##moreurl##';
+        $patterns[]='##user##';
+        $patterns[]='##approve##';
+        $patterns[]='##comments##';
+        $patterns[] = '##timeadded##';
+        $patterns[] = '##timemodified##';
+        $replacement[] = '';
+        $replacement[] = '';
+        $replacement[] = '';
+        $replacement[] = '';
+        $replacement[] = '';
+        $replacement[] = '';
+        $replacement[] = '';
+        $replacement[] = '';
+        $replacement[] = userdate($this->singlerecord->timecreated);
+        $replacement[] = userdate($this->singlerecord->timemodified);
+
+        // actual replacement of the tags
+        return str_ireplace($patterns, $replacement, $this->data->singletemplate);
+    }
+
+    /**
+     * @param array $fields
+     * @param object $record
+     * @uses PORTFOLIO_FORMAT_PLAINHTML
+     * @uses PORTFOLIO_FORMAT_RICHHTML
+     * @return array
+     */
+    public static function formats($fields, $record) {
+        $formats = array(PORTFOLIO_FORMAT_PLAINHTML);
+        $includedfiles = array();
+        foreach ($fields as $singlefield) {
+            if (is_callable(array($singlefield, 'get_file'))) {
+                $includedfiles[] = $singlefield->get_file($record->id);
+            }
+        }
+        if (count($includedfiles) == 1 && count($fields) == 1) {
+            $formats= array(portfolio_format_from_file($includedfiles[0]));
+        } else if (count($includedfiles) > 0) {
+            $formats = array(PORTFOLIO_FORMAT_RICHHTML);
+        }
+        return array($formats, $includedfiles);
+    }
+
+    public static function base_supported_formats() {
+        return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML);
+    }
+}
index 4bb2956d799816db31f0fd592bef8df4b5c59750..2671dff8b61992bdad50ffdcc8725c7e8f56e5ad 100644 (file)
     echo "</td><td>";
 
     if (has_capability('mod/forum:exportdiscussion', $modcontext)) {
+        require_once($CFG->libdir.'/portfoliolib.php');
         $button = new portfolio_add_button();
-        $button->set_callback_options('forum_portfolio_caller', array('discussionid' => $discussion->id), '/mod/forum/lib.php');
+        $button->set_callback_options('forum_portfolio_caller', array('discussionid' => $discussion->id), '/mod/forum/locallib.php');
         $button->render();
         echo '</td><td>';
     }
index d2af1a2d9ce2a8a41f35ad9fc5316f8e92144a6c..29ed8ab0a15d193993e4f5f4803996b7390f2731 100644 (file)
@@ -24,7 +24,6 @@
 /** Include required files */
 require_once($CFG->libdir.'/filelib.php');
 require_once($CFG->libdir.'/eventslib.php');
-require_once($CFG->libdir.'/portfoliolib.php');
 require_once($CFG->libdir . '/completionlib.php');
 require_once($CFG->dirroot.'/user/selector/lib.php');
 
@@ -3504,8 +3503,9 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa
         $p = array(
             'postid' => $post->id,
         );
+        require_once($CFG->libdir.'/portfoliolib.php');
         $button = new portfolio_add_button();
-        $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id));
+        $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id), '/mod/forum/locallib.php');
         if (empty($attachments)) {
             $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
         } else {
@@ -4346,6 +4346,7 @@ function forum_print_attachments($post, $cm, $type) {
 
     $canexport = (has_capability('mod/forum:exportpost', $context) || ($post->userid == $USER->id && has_capability('mod/forum:exportownpost', $context)));
 
+    require_once($CFG->libdir.'/portfoliolib.php');
     if ($files = $fs->get_area_files($context->id, 'forum_attachment', $post->id, "timemodified", false)) {
         $button = new portfolio_add_button();
         foreach ($files as $file) {
@@ -4358,7 +4359,7 @@ function forum_print_attachments($post, $cm, $type) {
                 $output .= "<a href=\"$path\">$iconimage</a> ";
                 $output .= "<a href=\"$path\">".s($filename)."</a>";
                 if ($canexport) {
-                    $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()));
+                    $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()), '/mod/forum/locallib.php');
                     $button->set_format_by_file($file);
                     $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                 }
@@ -7964,363 +7965,6 @@ function forum_get_extra_capabilities() {
     return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames', 'moodle/site:trustcontent');
 }
 
-/**
- * @package mod-forum
- * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class forum_portfolio_caller extends portfolio_module_caller_base {
-
-    protected $postid;
-    protected $discussionid;
-    protected $attachment;
-
-    private $post;
-    private $forum;
-    private $discussion;
-    private $posts;
-    private $keyedfiles; // just using multifiles isn't enough if we're exporting a full thread
-
-    /**
-     * @return array
-     */
-    public static function expected_callbackargs() {
-        return array(
-            'postid'       => false,
-            'discussionid' => false,
-            'attachment'   => false,
-        );
-    }
-    /**
-     * @param array $callbackargs
-     */
-    function __construct($callbackargs) {
-        parent::__construct($callbackargs);
-        if (!$this->postid && !$this->discussionid) {
-            throw new portfolio_caller_exception('mustprovidediscussionorpost', 'forum');
-        }
-    }
-    /**
-     * @global object
-     */
-    public function load_data() {
-        global $DB;
-
-        if ($this->postid) {
-            if (!$this->post = $DB->get_record('forum_posts', array('id' => $this->postid))) {
-                throw new portfolio_caller_exception('invalidpostid', 'forum');
-            }
-        }
-
-        $dparams = array();
-        if ($this->discussionid) {
-            $dbparams = array('id' => $this->discussionid);
-        } else if ($this->post) {
-            $dbparams = array('id' => $this->post->discussion);
-        } else {
-            throw new portfolio_caller_exception('mustprovidediscussionorpost', 'forum');
-        }
-
-        if (!$this->discussion = $DB->get_record('forum_discussions', $dbparams)) {
-            throw new portfolio_caller_exception('invaliddiscussionid', 'forum');
-        }
-
-        if (!$this->forum = $DB->get_record('forum', array('id' => $this->discussion->forum))) {
-            throw new portfolio_caller_exception('invalidforumid', 'forum');
-        }
-
-        if (!$this->cm = get_coursemodule_from_instance('forum', $this->forum->id)) {
-            throw new portfolio_caller_exception('invalidcoursemodule');
-        }
-
-        $modcontext = get_context_instance(CONTEXT_MODULE, $this->cm->id);
-        if ($this->post) {
-            $this->set_file_and_format_data($this->attachment, $modcontext->id, 'forum_attachment', $this->post->id, 'timemodified', false);
-            if (!empty($this->multifiles)) {
-                $this->keyedfiles[$this->post->id] = $this->multifiles;
-            } else if (!empty($this->singlefile)) {
-                $this->keyedfiles[$this->post->id] = array($this->singlefile);
-            }
-        } else { // whole thread
-            $fs = get_file_storage();
-            $this->posts = forum_get_all_discussion_posts($this->discussion->id, 'p.created ASC');
-            $this->multifiles = array();
-            foreach ($this->posts as $post) {
-                if (!$this->keyedfiles[$post->id] = $fs->get_area_files($modcontext->id, 'forum_attachment', $post->id, "timemodified", false)) {
-                    continue;
-                }
-                $this->multifiles = array_merge($this->multifiles, array_values($this->keyedfiles[$post->id]));
-            }
-        }
-        if (empty($this->multifiles) && !empty($this->singlefile)) {
-            $this->multifiles = array($this->singlefile); // copy_files workaround
-        }
-        // depending on whether there are files or not, we might have to change richhtml/plainhtml
-        if (!empty($this->multifiles)) {
-            $this->add_format(PORTFOLIO_FORMAT_RICHHTML);
-        } else {
-            $this->add_format(PORTFOLIO_FORMAT_PLAINHTML);
-        }
-    }
-
-    /**
-     * @global object
-     * @return string
-     */
-    function get_return_url() {
-        global $CFG;
-        return $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $this->discussion->id;
-    }
-    /**
-     * @global object
-     * @return array
-     */
-    function get_navigation() {
-        global $CFG;
-
-        $navlinks = array();
-        $navlinks[] = array(
-            'name' => format_string($this->discussion->name),
-            'link' => $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $this->discussion->id,
-            'type' => 'title'
-        );
-        return array($navlinks, $this->cm);
-    }
-    /**
-     * either a whole discussion
-     * a single post, with or without attachment
-     * or just an attachment with no post
-     *
-     * @global object
-     * @global object
-     * @uses PORTFOLIO_FORMAT_RICH
-     * @return mixed
-     */
-    function prepare_package() {
-        global $CFG;
-
-        // set up the leap2a writer if we need it
-        $writingleap = false;
-        if ($this->exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
-            $leapwriter = $this->exporter->get('format')->leap2a_writer();
-            $writingleap = true;
-        }
-        if ($this->attachment) { // simplest case first - single file attachment
-            $this->copy_files(array($this->singlefile), $this->attachment);
-            if ($writingleap) { // if we're writing leap, make the manifest to go along with the file
-                $entry = new portfolio_format_leap2a_entry($id, $this->attachment->get_filename(), 'resource',  $this->attachment);
-                $entry->add_category('offline', 'resource_type');
-                $leapwriter->add_entry($entry);
-                return $this->exporter->write_new_file($leapwriter->to_xml(), $this->exporter->get('format')->manifest_name(), true);
-            }
-
-        } else if (empty($this->post)) {  // exporting whole discussion
-            $content = ''; // if we're just writing HTML, start a string to add each post to
-            $ids = array(); // if we're writing leap2a, keep track of all entryids so we can add a selection element
-            foreach ($this->posts as $post) {
-                $posthtml =  $this->prepare_post($post);
-                if ($writingleap) {
-                    $ids[] = $this->prepare_post_leap2a($leapwriter, $post, $posthtml);
-                } else {
-                    $content .= $posthtml . '<br /><br />';
-                }
-            }
-            $this->copy_files($this->multifiles);
-            $name = 'discussion.html';
-            $manifest = ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH);
-            if ($writingleap) {
-                // add on an extra 'selection' entry
-                $selection = new portfolio_format_leap2a_entry('forumdiscussion' . $this->discussionid, get_string('discussion', 'forum'), 'selection');
-                $leapwriter->add_entry($selection);
-                $leapwriter->make_selection($selection, $ids, 'Grouping');
-                $content = $leapwriter->to_xml();
-                $name = $this->get('exporter')->get('format')->manifest_name();
-            }
-            $this->get('exporter')->write_new_file($content, $name, $manifest);
-
-        } else { // exporting a single post
-            $posthtml = $this->prepare_post($this->post);
-
-            $content = $posthtml;
-            $name = 'post.html';
-            $manifest = ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH);
-
-            if ($writingleap) {
-                $this->prepare_post_leap2a($leapwriter, $this->post, $posthtml);
-                $content = $leapwriter->to_xml();
-                $name = $this->exporter->get('format')->manifest_name();
-            }
-            $this->copy_files($this->multifiles);
-            $this->get('exporter')->write_new_file($content, $name, $manifest);
-        }
-    }
-
-    /**
-     * helper function to add a leap2a entry element
-     * that corresponds to a single forum post,
-     * including any attachments
-     *
-     * the entry/ies are added directly to the leapwriter, which is passed by ref
-     *
-     * @param portfolio_format_leap2a_writer $leapwriter writer object to add entries to
-     * @param object $post                               the stdclass object representing the database record
-     * @param string $posthtml                           the content of the post (prepared by {@link prepare_post}
-     *
-     * @return int id of new entry
-     */
-    private function prepare_post_leap2a(portfolio_format_leap2a_writer $leapwriter, $post, $posthtml) {
-        $entry = new portfolio_format_leap2a_entry('forumpost' . $post->id,  $post->subject, 'resource', $posthtml);
-        $entry->published = $post->created;
-        $entry->updated = $post->modified;
-        $entry->author = $post->author;
-        if (is_array($this->keyedfiles) && array_key_exists($post->id, $this->keyedfiles) && is_array($this->keyedfiles[$post->id])) {
-            foreach ($this->keyedfiles[$post->id] as $file) {
-                // copying the file into the package area is handled elsewhere
-                $entry->add_attachment($file);
-            }
-        }
-        $entry->add_category('web', 'resource_type');
-        $leapwriter->add_entry($entry);
-        return $entry->id;
-    }
-
-    /**
-     * @param array $files
-     * @param mixed $justone false of id of single file to copy
-     * @return bool|void
-     */
-    private function copy_files($files, $justone=false) {
-        if (empty($files)) {
-            return;
-        }
-        foreach ($files as $f) {
-            if ($justone && $f->get_id() != $justone) {
-                continue;
-            }
-            $this->get('exporter')->copy_existing_file($f);
-            if ($justone && $f->get_id() == $justone) {
-                return true; // all we need to do
-            }
-        }
-    }
-    /**
-     * this is a very cut down version of what is in forum_make_mail_post
-     *
-     * @global object
-     * @param int $post
-     * @return string
-     */
-    private function prepare_post($post, $fileoutputextras=null) {
-        global $DB;
-        static $users;
-        if (empty($users)) {
-            $users = array($this->user->id => $this->user);
-        }
-        if (!array_key_exists($post->userid, $users)) {
-            $users[$post->userid] = $DB->get_record('user', array('id' => $post->userid));
-        }
-        // add the user object on to the post so we can pass it to the leap writer if necessary
-        $post->author = $users[$post->userid];
-        $viewfullnames = true;
-        // format the post body
-        $options = new object();
-        $options->para = true;
-        $formattedtext = format_text($post->message, $post->messageformat, $options, $this->get('course')->id);
-
-        $output = '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
-
-        $output .= '<tr class="header"><td>';// can't print picture.
-        $output .= '</td>';
-
-        if ($post->parent) {
-            $output .= '<td class="topic">';
-        } else {
-            $output .= '<td class="topic starter">';
-        }
-        $output .= '<div class="subject">'.format_string($post->subject).'</div>';
-
-        $fullname = fullname($users[$post->userid], $viewfullnames);
-        $by = new object();
-        $by->name = $fullname;
-        $by->date = userdate($post->modified, '', $this->user->timezone);
-        $output .= '<div class="author">'.get_string('bynameondate', 'forum', $by).'</div>';
-
-        $output .= '</td></tr>';
-
-        $output .= '<tr><td class="left side" valign="top">';
-
-        $output .= '</td><td class="content">';
-
-        $output .= $formattedtext;
-
-        if (is_array($this->keyedfiles) && array_key_exists($post->id, $this->keyedfiles) && is_array($this->keyedfiles[$post->id]) && count($this->keyedfiles[$post->id]) > 0) {
-            $output .= '<div class="attachments">';
-            $output .= '<br /><b>' .  get_string('attachments', 'forum') . '</b>:<br /><br />';
-            $format = $this->get('exporter')->get('format');
-            foreach ($this->keyedfiles[$post->id] as $file) {
-                $output .= $format->file_output($file)  . '<br/ >';
-            }
-            $output .= "</div>";
-        }
-
-        $output .= '</td></tr></table>'."\n\n";
-
-        return $output;
-    }
-    /**
-     * @return string
-     */
-    function get_sha1() {
-        $filesha = '';
-        try {
-            $filesha = $this->get_sha1_file();
-        } catch (portfolio_caller_exception $e) { } // no files
-
-        if ($this->post) {
-            return sha1($filesha . ',' . $this->post->subject . ',' . $this->post->message);
-        } else {
-            $sha1s = array($filesha);
-            foreach ($this->posts as $post) {
-                $sha1s[] = sha1($post->subject . ',' . $post->message);
-            }
-            return sha1(implode(',', $sha1s));
-        }
-    }
-
-    function expected_time() {
-        $filetime = $this->expected_time_file();
-        if ($this->posts) {
-            $posttime = portfolio_expected_time_db(count($this->posts));
-            if ($filetime < $posttime) {
-                return $posttime;
-            }
-        }
-        return $filetime;
-    }
-    /**
-     * @uses CONTEXT_MODULE
-     * @return bool
-     */
-    function check_permissions() {
-        $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
-        if ($this->post) {
-            return (has_capability('mod/forum:exportpost', $context)
-                || ($this->post->userid == $this->user->id
-                    && has_capability('mod/forum:exportownpost', $context)));
-        }
-        return has_capability('mod/forum:exportdiscussion', $context);
-    }
-    /**
-     * @return string
-     */
-    public static function display_name() {
-        return get_string('modulename', 'forum');
-    }
-
-    public static function base_supported_formats() {
-        return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_LEAP2A);
-    }
-}
 
 /**
  * This function is used to extend the global navigation by add forum nodes if there
@@ -8698,4 +8342,4 @@ class forum_existing_subscriber_selector extends forum_subscriber_selector_base
         return array(get_string("existingsubscribers", 'forum') => $subscribers);
     }
 
-}
\ No newline at end of file
+}
diff --git a/mod/forum/locallib.php b/mod/forum/locallib.php
new file mode 100644 (file)
index 0000000..9cef8d2
--- /dev/null
@@ -0,0 +1,380 @@
+<?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/>.
+
+/**
+ * Library of functions for forum outside of the core api
+ */
+
+require_once($CFG->libdir . '/portfolio/caller.php');
+
+/**
+ * @package   mod-forum
+ * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class forum_portfolio_caller extends portfolio_module_caller_base {
+
+    protected $postid;
+    protected $discussionid;
+    protected $attachment;
+
+    private $post;
+    private $forum;
+    private $discussion;
+    private $posts;
+    private $keyedfiles; // just using multifiles isn't enough if we're exporting a full thread
+
+    /**
+     * @return array
+     */
+    public static function expected_callbackargs() {
+        return array(
+            'postid'       => false,
+            'discussionid' => false,
+            'attachment'   => false,
+        );
+    }
+    /**
+     * @param array $callbackargs
+     */
+    function __construct($callbackargs) {
+        parent::__construct($callbackargs);
+        if (!$this->postid && !$this->discussionid) {
+            throw new portfolio_caller_exception('mustprovidediscussionorpost', 'forum');
+        }
+    }
+    /**
+     * @global object
+     */
+    public function load_data() {
+        global $DB;
+
+        if ($this->postid) {
+            if (!$this->post = $DB->get_record('forum_posts', array('id' => $this->postid))) {
+                throw new portfolio_caller_exception('invalidpostid', 'forum');
+            }
+        }
+
+        $dparams = array();
+        if ($this->discussionid) {
+            $dbparams = array('id' => $this->discussionid);
+        } else if ($this->post) {
+            $dbparams = array('id' => $this->post->discussion);
+        } else {
+            throw new portfolio_caller_exception('mustprovidediscussionorpost', 'forum');
+        }
+
+        if (!$this->discussion = $DB->get_record('forum_discussions', $dbparams)) {
+            throw new portfolio_caller_exception('invaliddiscussionid', 'forum');
+        }
+
+        if (!$this->forum = $DB->get_record('forum', array('id' => $this->discussion->forum))) {
+            throw new portfolio_caller_exception('invalidforumid', 'forum');
+        }
+
+        if (!$this->cm = get_coursemodule_from_instance('forum', $this->forum->id)) {
+            throw new portfolio_caller_exception('invalidcoursemodule');
+        }
+
+        $modcontext = get_context_instance(CONTEXT_MODULE, $this->cm->id);
+        if ($this->post) {
+            $this->set_file_and_format_data($this->attachment, $modcontext->id, 'forum_attachment', $this->post->id, 'timemodified', false);
+            if (!empty($this->multifiles)) {
+                $this->keyedfiles[$this->post->id] = $this->multifiles;
+            } else if (!empty($this->singlefile)) {
+                $this->keyedfiles[$this->post->id] = array($this->singlefile);
+            }
+        } else { // whole thread
+            $fs = get_file_storage();
+            $this->posts = forum_get_all_discussion_posts($this->discussion->id, 'p.created ASC');
+            $this->multifiles = array();
+            foreach ($this->posts as $post) {
+                if (!$this->keyedfiles[$post->id] = $fs->get_area_files($modcontext->id, 'forum_attachment', $post->id, "timemodified", false)) {
+                    continue;
+                }
+                $this->multifiles = array_merge($this->multifiles, array_values($this->keyedfiles[$post->id]));
+            }
+        }
+        if (empty($this->multifiles) && !empty($this->singlefile)) {
+            $this->multifiles = array($this->singlefile); // copy_files workaround
+        }
+        // depending on whether there are files or not, we might have to change richhtml/plainhtml
+        if (!empty($this->multifiles)) {
+            $this->add_format(PORTFOLIO_FORMAT_RICHHTML);
+        } else {
+            $this->add_format(PORTFOLIO_FORMAT_PLAINHTML);
+        }
+    }
+
+    /**
+     * @global object
+     * @return string
+     */
+    function get_return_url() {
+        global $CFG;
+        return $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $this->discussion->id;
+    }
+    /**
+     * @global object
+     * @return array
+     */
+    function get_navigation() {
+        global $CFG;
+
+        $navlinks = array();
+        $navlinks[] = array(
+            'name' => format_string($this->discussion->name),
+            'link' => $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $this->discussion->id,
+            'type' => 'title'
+        );
+        return array($navlinks, $this->cm);
+    }
+    /**
+     * either a whole discussion
+     * a single post, with or without attachment
+     * or just an attachment with no post
+     *
+     * @global object
+     * @global object
+     * @uses PORTFOLIO_FORMAT_RICH
+     * @return mixed
+     */
+    function prepare_package() {
+        global $CFG;
+
+        // set up the leap2a writer if we need it
+        $writingleap = false;
+        if ($this->exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
+            $leapwriter = $this->exporter->get('format')->leap2a_writer();
+            $writingleap = true;
+        }
+        if ($this->attachment) { // simplest case first - single file attachment
+            $this->copy_files(array($this->singlefile), $this->attachment);
+            if ($writingleap) { // if we're writing leap, make the manifest to go along with the file
+                $entry = new portfolio_format_leap2a_entry($id, $this->attachment->get_filename(), 'resource',  $this->attachment);
+                $entry->add_category('offline', 'resource_type');
+                $leapwriter->add_entry($entry);
+                return $this->exporter->write_new_file($leapwriter->to_xml(), $this->exporter->get('format')->manifest_name(), true);
+            }
+
+        } else if (empty($this->post)) {  // exporting whole discussion
+            $content = ''; // if we're just writing HTML, start a string to add each post to
+            $ids = array(); // if we're writing leap2a, keep track of all entryids so we can add a selection element
+            foreach ($this->posts as $post) {
+                $posthtml =  $this->prepare_post($post);
+                if ($writingleap) {
+                    $ids[] = $this->prepare_post_leap2a($leapwriter, $post, $posthtml);
+                } else {
+                    $content .= $posthtml . '<br /><br />';
+                }
+            }
+            $this->copy_files($this->multifiles);
+            $name = 'discussion.html';
+            $manifest = ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH);
+            if ($writingleap) {
+                // add on an extra 'selection' entry
+                $selection = new portfolio_format_leap2a_entry('forumdiscussion' . $this->discussionid, get_string('discussion', 'forum'), 'selection');
+                $leapwriter->add_entry($selection);
+                $leapwriter->make_selection($selection, $ids, 'Grouping');
+                $content = $leapwriter->to_xml();
+                $name = $this->get('exporter')->get('format')->manifest_name();
+            }
+            $this->get('exporter')->write_new_file($content, $name, $manifest);
+
+        } else { // exporting a single post
+            $posthtml = $this->prepare_post($this->post);
+
+            $content = $posthtml;
+            $name = 'post.html';
+            $manifest = ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH);
+
+            if ($writingleap) {
+                $this->prepare_post_leap2a($leapwriter, $this->post, $posthtml);
+                $content = $leapwriter->to_xml();
+                $name = $this->exporter->get('format')->manifest_name();
+            }
+            $this->copy_files($this->multifiles);
+            $this->get('exporter')->write_new_file($content, $name, $manifest);
+        }
+    }
+
+    /**
+     * helper function to add a leap2a entry element
+     * that corresponds to a single forum post,
+     * including any attachments
+     *
+     * the entry/ies are added directly to the leapwriter, which is passed by ref
+     *
+     * @param portfolio_format_leap2a_writer $leapwriter writer object to add entries to
+     * @param object $post                               the stdclass object representing the database record
+     * @param string $posthtml                           the content of the post (prepared by {@link prepare_post}
+     *
+     * @return int id of new entry
+     */
+    private function prepare_post_leap2a(portfolio_format_leap2a_writer $leapwriter, $post, $posthtml) {
+        $entry = new portfolio_format_leap2a_entry('forumpost' . $post->id,  $post->subject, 'resource', $posthtml);
+        $entry->published = $post->created;
+        $entry->updated = $post->modified;
+        $entry->author = $post->author;
+        if (is_array($this->keyedfiles) && array_key_exists($post->id, $this->keyedfiles) && is_array($this->keyedfiles[$post->id])) {
+            foreach ($this->keyedfiles[$post->id] as $file) {
+                // copying the file into the package area is handled elsewhere
+                $entry->add_attachment($file);
+            }
+        }
+        $entry->add_category('web', 'resource_type');
+        $leapwriter->add_entry($entry);
+        return $entry->id;
+    }
+
+    /**
+     * @param array $files
+     * @param mixed $justone false of id of single file to copy
+     * @return bool|void
+     */
+    private function copy_files($files, $justone=false) {
+        if (empty($files)) {
+            return;
+        }
+        foreach ($files as $f) {
+            if ($justone && $f->get_id() != $justone) {
+                continue;
+            }
+            $this->get('exporter')->copy_existing_file($f);
+            if ($justone && $f->get_id() == $justone) {
+                return true; // all we need to do
+            }
+        }
+    }
+    /**
+     * this is a very cut down version of what is in forum_make_mail_post
+     *
+     * @global object
+     * @param int $post
+     * @return string
+     */
+    private function prepare_post($post, $fileoutputextras=null) {
+        global $DB;
+        static $users;
+        if (empty($users)) {
+            $users = array($this->user->id => $this->user);
+        }
+        if (!array_key_exists($post->userid, $users)) {
+            $users[$post->userid] = $DB->get_record('user', array('id' => $post->userid));
+        }
+        // add the user object on to the post so we can pass it to the leap writer if necessary
+        $post->author = $users[$post->userid];
+        $viewfullnames = true;
+        // format the post body
+        $options = new object();
+        $options->para = true;
+        $formattedtext = format_text($post->message, $post->messageformat, $options, $this->get('course')->id);
+
+        $output = '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
+
+        $output .= '<tr class="header"><td>';// can't print picture.
+        $output .= '</td>';
+
+        if ($post->parent) {
+            $output .= '<td class="topic">';
+        } else {
+            $output .= '<td class="topic starter">';
+        }
+        $output .= '<div class="subject">'.format_string($post->subject).'</div>';
+
+        $fullname = fullname($users[$post->userid], $viewfullnames);
+        $by = new object();
+        $by->name = $fullname;
+        $by->date = userdate($post->modified, '', $this->user->timezone);
+        $output .= '<div class="author">'.get_string('bynameondate', 'forum', $by).'</div>';
+
+        $output .= '</td></tr>';
+
+        $output .= '<tr><td class="left side" valign="top">';
+
+        $output .= '</td><td class="content">';
+
+        $output .= $formattedtext;
+
+        if (is_array($this->keyedfiles) && array_key_exists($post->id, $this->keyedfiles) && is_array($this->keyedfiles[$post->id]) && count($this->keyedfiles[$post->id]) > 0) {
+            $output .= '<div class="attachments">';
+            $output .= '<br /><b>' .  get_string('attachments', 'forum') . '</b>:<br /><br />';
+            $format = $this->get('exporter')->get('format');
+            foreach ($this->keyedfiles[$post->id] as $file) {
+                $output .= $format->file_output($file)  . '<br/ >';
+            }
+            $output .= "</div>";
+        }
+
+        $output .= '</td></tr></table>'."\n\n";
+
+        return $output;
+    }
+    /**
+     * @return string
+     */
+    function get_sha1() {
+        $filesha = '';
+        try {
+            $filesha = $this->get_sha1_file();
+        } catch (portfolio_caller_exception $e) { } // no files
+
+        if ($this->post) {
+            return sha1($filesha . ',' . $this->post->subject . ',' . $this->post->message);
+        } else {
+            $sha1s = array($filesha);
+            foreach ($this->posts as $post) {
+                $sha1s[] = sha1($post->subject . ',' . $post->message);
+            }
+            return sha1(implode(',', $sha1s));
+        }
+    }
+
+    function expected_time() {
+        $filetime = $this->expected_time_file();
+        if ($this->posts) {
+            $posttime = portfolio_expected_time_db(count($this->posts));
+            if ($filetime < $posttime) {
+                return $posttime;
+            }
+        }
+        return $filetime;
+    }
+    /**
+     * @uses CONTEXT_MODULE
+     * @return bool
+     */
+    function check_permissions() {
+        $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
+        if ($this->post) {
+            return (has_capability('mod/forum:exportpost', $context)
+                || ($this->post->userid == $this->user->id
+                    && has_capability('mod/forum:exportownpost', $context)));
+        }
+        return has_capability('mod/forum:exportdiscussion', $context);
+    }
+    /**
+     * @return string
+     */
+    public static function display_name() {
+        return get_string('modulename', 'forum');
+    }
+
+    public static function base_supported_formats() {
+        return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_LEAP2A);
+    }
+}
index 752347f963fac98b536c8f146cc9312f653dfbf8..bb1f46840e5859e5b62701449f9886e32703e6c7 100644 (file)
@@ -73,7 +73,7 @@ echo $OUTPUT->box_start('glossarydisplay generalbox');
     if ($DB->count_records('glossary_entries', array('glossaryid' => $glossary->id))) {
         require_once($CFG->libdir . '/portfoliolib.php');
         $button = new portfolio_add_button();
-        $button->set_callback_options('glossary_full_portfolio_caller', array('id' => $cm->id), '/mod/glossary/lib.php');
+        $button->set_callback_options('glossary_full_portfolio_caller', array('id' => $cm->id), '/mod/glossary/locallib.php');
         $button->render();
     }
     echo $OUTPUT->box_end();
index e7483ffa0926cad44aee924bdd1764125b1db0ea..7c182b2934deedb29703df8ec982c0c1fa06288f 100644 (file)
@@ -24,8 +24,6 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
-/** Include Portfolio lib */
-require_once($CFG->libdir.'/portfoliolib.php');
 
 define("GLOSSARY_SHOW_ALL_CATEGORIES", 0);
 define("GLOSSARY_SHOW_NOT_CATEGORISED", -1);
@@ -983,8 +981,9 @@ function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$h
     if (has_capability('mod/glossary:exportentry', $context)
         || ($entry->userid == $USER->id
             && has_capability('mod/glossary:exportownentry', $context))) {
+        require_once($CFG->libdir . '/portfoliolib.php');
         $button = new portfolio_add_button();
-        $button->set_callback_options('glossary_entry_portfolio_caller',  array('id' => $cm->id, 'entryid' => $entry->id));
+        $button->set_callback_options('glossary_entry_portfolio_caller',  array('id' => $cm->id, 'entryid' => $entry->id), '/mod/glossary/locallib.php');
 
         $filecontext = $context;
         if ($entry->sourceglossaryid == $cm->instance) {
@@ -995,6 +994,8 @@ function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$h
         $fs = get_file_storage();
         if ($files = $fs->get_area_files($filecontext->id, 'glossary_attachment', $entry->id, "timemodified", false)) {
             $button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
+        } else {
+            $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
         }
 
         $return .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
@@ -2673,197 +2674,6 @@ function glossary_supports($feature) {
         default: return null;
     }
 }
-/**
- * @package   mod-glossary
- * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class glossary_full_portfolio_caller extends portfolio_module_caller_base {
-
-    private $glossary;
-    private $exportdata;
-    /**
-     * @return array
-     */
-    public static function expected_callbackargs() {
-        return array(
-            'id' => true,
-        );
-    }
-    /**
-     * @return global
-     */
-    public function load_data() {
-        global $DB;
-        if (!$this->cm = get_coursemodule_from_id('glossary', $this->id)) {
-            throw new portfolio_caller_exception('invalidid', 'glossary');
-        }
-        if (!$this->glossary = $DB->get_record('glossary', array('id' => $this->cm->instance))) {
-            throw new portfolio_caller_exception('invalidid', 'glossary');
-        }
-        $entries = $DB->get_records('glossary_entries', array('glossaryid' => $this->glossary->id));
-        list($where, $params) = $DB->get_in_or_equal(array_keys($entries));
-
-        $aliases = $DB->get_records_select('glossary_alias', 'entryid ' . $where, $params);
-        $categoryentries = $DB->get_records_sql('SELECT ec.entryid, c.name FROM {glossary_entries_categories} ec
-            JOIN {glossary_categories} c
-            ON c.id = ec.categoryid
-            WHERE ec.entryid ' . $where, $params);
-
-        $this->exportdata = array('entries' => $entries, 'aliases' => $aliases, 'categoryentries' => $categoryentries);
-    }
-
-    /**
-     * @return string
-     */
-    public function expected_time() {
-        return portfolio_expected_time_db(count($this->exportdata['entries']));
-    }
-    /**
-     * @return string
-     */
-    public function get_sha1() {
-        return sha1(serialize($this->exportdata));
-    }
-    /**
-     * @return object
-     */
-    public function prepare_package() {
-        $entries = $this->exportdata['entries'];
-        $aliases = array();
-        $categories = array();
-        if (is_array($this->exportdata['aliases'])) {
-            foreach ($this->exportdata['aliases'] as $alias) {
-                if (!array_key_exists($alias->entryid, $aliases)) {
-                    $aliases[$alias->entryid] = array();
-                }
-                $aliases[$alias->entryid][] = $alias->alias;
-            }
-        }
-        if (is_array($this->exportdata['categoryentries'])) {
-            foreach ($this->exportdata['categoryentries'] as $cat) {
-                if (!array_key_exists($cat->entryid, $categories)) {
-                    $categories[$cat->entryid] = array();
-                }
-                $categories[$cat->entryid][] = $cat->name;
-            }
-        }
-        // TODO detect format here
-        $csv = glossary_generate_export_csv($entries, $aliases, $categories);
-        return $this->exporter->write_new_file($csv, clean_filename($this->cm->name) . '.csv', false);
-        // TODO when csv, what do we do with attachments?!
-    }
-    /**
-     * @return bool
-     */
-    public function check_permissions() {
-        return has_capability('mod/glossary:export', get_context_instance(CONTEXT_MODULE, $this->cm->id));
-    }
-    /**
-     * @return string
-     */
-    public static function display_name() {
-        return get_string('modulename', 'glossary');
-    }
-
-    public static function base_supported_formats() {
-        return array(PORTFOLIO_FORMAT_FILE);
-    }
-}
-
-/**
- * @package   mod-glossary
- * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class glossary_entry_portfolio_caller extends portfolio_module_caller_base { // TODO files support
-
-    private $glossary;
-    private $entry;
-    protected $entryid;
-    /*
-     * @return array
-     */
-    public static function expected_callbackargs() {
-        return array(
-            'entryid' => true,
-            'id'      => true,
-        );
-    }
-    /**
-     * @global object
-     */
-    public function load_data() {
-        global $DB;
-        if (!$this->cm = get_coursemodule_from_id('glossary', $this->id)) {
-            throw new portfolio_caller_exception('invalidid', 'glossary');
-        }
-        if (!$this->glossary = $DB->get_record('glossary', array('id' => $this->cm->instance))) {
-            throw new portfolio_caller_exception('invalidid', 'glossary');
-        }
-        if ($this->entryid) {
-            if (!$this->entry = $DB->get_record('glossary_entries', array('id' => $this->entryid))) {
-                throw new portfolio_caller_exception('noentry', 'glossary');
-            }
-            // in case we don't have USER this will make the entry be printed
-            $this->entry->approved = true;
-        }
-        $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
-        if ($this->entry->sourceglossaryid == $this->cm->instance) {
-            if ($maincm = get_coursemodule_from_instance('glossary', $this->entry->glossaryid)) {
-                $context = get_context_instance(CONTEXT_MODULE, $maincm->id);
-            }
-        }
-        $fs = get_file_storage();
-        $this->multifiles = $fs->get_area_files($context->id, 'glossary_attachment', $this->entry->id, "timemodified", false);
-    }
-    /**
-     * @return string
-     */
-    public function expected_time() {
-        return PORTFOLIO_TIME_LOW;
-    }
-    /**
-     * @return bool
-     */
-    public function check_permissions() {
-        $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
-        return has_capability('mod/glossary:exportentry', $context)
-            || ($this->entry->userid == $this->user->id && has_capability('mod/glossary:exportownentry', $context));
-    }
-    /**
-     * @return string
-     */
-    public static function display_name() {
-        return get_string('modulename', 'glossary');
-    }
-    /**
-     * @return object
-     */
-    public function prepare_package() {
-        define('PORTFOLIO_INTERNAL', true);
-        ob_start();
-        $entry = clone $this->entry;
-        glossary_print_entry($this->get('course'), $this->cm, $this->glossary, $entry, null, null, false);
-        $content = ob_get_clean();
-        if ($this->multifiles) {
-            foreach ($this->multifiles as $file) {
-                $this->exporter->copy_existing_file($file);
-            }
-        }
-        return $this->exporter->write_new_file($content, clean_filename($this->entry->concept) . '.html', !empty($files));
-    }
-    /**
-     * @return string
-     */
-    public function get_sha1() {
-        return sha1(serialize($this->entry) . $this->get_sha1_file());
-    }
-
-    public static function base_supported_formats() {
-        return array(PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML);
-    }
-}
 
 function glossary_extend_navigation($navigation, $course, $module, $cm) {
     global $CFG;
diff --git a/mod/glossary/locallib.php b/mod/glossary/locallib.php
new file mode 100644 (file)
index 0000000..cca817c
--- /dev/null
@@ -0,0 +1,216 @@
+<?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/>.
+
+require_once($CFG->libdir . '/portfolio/caller.php');
+
+/**
+ * Library of functions and constants for module glossary
+ * outside of what is required for the core moodle api
+ *
+ * @package   mod-glossary
+ * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class glossary_full_portfolio_caller extends portfolio_module_caller_base {
+
+    private $glossary;
+    private $exportdata;
+    /**
+     * @return array
+     */
+    public static function expected_callbackargs() {
+        return array(
+            'id' => true,
+        );
+    }
+    /**
+     * @return global
+     */
+    public function load_data() {
+        global $DB;
+        if (!$this->cm = get_coursemodule_from_id('glossary', $this->id)) {
+            throw new portfolio_caller_exception('invalidid', 'glossary');
+        }
+        if (!$this->glossary = $DB->get_record('glossary', array('id' => $this->cm->instance))) {
+            throw new portfolio_caller_exception('invalidid', 'glossary');
+        }
+        $entries = $DB->get_records('glossary_entries', array('glossaryid' => $this->glossary->id));
+        list($where, $params) = $DB->get_in_or_equal(array_keys($entries));
+
+        $aliases = $DB->get_records_select('glossary_alias', 'entryid ' . $where, $params);
+        $categoryentries = $DB->get_records_sql('SELECT ec.entryid, c.name FROM {glossary_entries_categories} ec
+            JOIN {glossary_categories} c
+            ON c.id = ec.categoryid
+            WHERE ec.entryid ' . $where, $params);
+
+        $this->exportdata = array('entries' => $entries, 'aliases' => $aliases, 'categoryentries' => $categoryentries);
+    }
+
+    /**
+     * @return string
+     */
+    public function expected_time() {
+        return portfolio_expected_time_db(count($this->exportdata['entries']));
+    }
+    /**
+     * @return string
+     */
+    public function get_sha1() {
+        return sha1(serialize($this->exportdata));
+    }
+    /**
+     * @return object
+     */
+    public function prepare_package() {
+        $entries = $this->exportdata['entries'];
+        $aliases = array();
+        $categories = array();
+        if (is_array($this->exportdata['aliases'])) {
+            foreach ($this->exportdata['aliases'] as $alias) {
+                if (!array_key_exists($alias->entryid, $aliases)) {
+                    $aliases[$alias->entryid] = array();
+                }
+                $aliases[$alias->entryid][] = $alias->alias;
+            }
+        }
+        if (is_array($this->exportdata['categoryentries'])) {
+            foreach ($this->exportdata['categoryentries'] as $cat) {
+                if (!array_key_exists($cat->entryid, $categories)) {
+                    $categories[$cat->entryid] = array();
+                }
+                $categories[$cat->entryid][] = $cat->name;
+            }
+        }
+        // TODO detect format here
+        $csv = glossary_generate_export_csv($entries, $aliases, $categories);
+        return $this->exporter->write_new_file($csv, clean_filename($this->cm->name) . '.csv', false);
+        // TODO when csv, what do we do with attachments?!
+    }
+    /**
+     * @return bool
+     */
+    public function check_permissions() {
+        return has_capability('mod/glossary:export', get_context_instance(CONTEXT_MODULE, $this->cm->id));
+    }
+    /**
+     * @return string
+     */
+    public static function display_name() {
+        return get_string('modulename', 'glossary');
+    }
+
+    public static function base_supported_formats() {
+        return array(PORTFOLIO_FORMAT_FILE);
+    }
+}
+
+/**
+ * @package   mod-glossary
+ * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class glossary_entry_portfolio_caller extends portfolio_module_caller_base { // TODO files support
+
+    private $glossary;
+    private $entry;
+    protected $entryid;
+    /*
+     * @return array
+     */
+    public static function expected_callbackargs() {
+        return array(
+            'entryid' => true,
+            'id'      => true,
+        );
+    }
+    /**
+     * @global object
+     */
+    public function load_data() {
+        global $DB;
+        if (!$this->cm = get_coursemodule_from_id('glossary', $this->id)) {
+            throw new portfolio_caller_exception('invalidid', 'glossary');
+        }
+        if (!$this->glossary = $DB->get_record('glossary', array('id' => $this->cm->instance))) {
+            throw new portfolio_caller_exception('invalidid', 'glossary');
+        }
+        if ($this->entryid) {
+            if (!$this->entry = $DB->get_record('glossary_entries', array('id' => $this->entryid))) {
+                throw new portfolio_caller_exception('noentry', 'glossary');
+            }
+            // in case we don't have USER this will make the entry be printed
+            $this->entry->approved = true;
+        }
+        $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
+        if ($this->entry->sourceglossaryid == $this->cm->instance) {
+            if ($maincm = get_coursemodule_from_instance('glossary', $this->entry->glossaryid)) {
+                $context = get_context_instance(CONTEXT_MODULE, $maincm->id);
+            }
+        }
+        $fs = get_file_storage();
+        $this->multifiles = $fs->get_area_files($context->id, 'glossary_attachment', $this->entry->id, "timemodified", false);
+    }
+    /**
+     * @return string
+     */
+    public function expected_time() {
+        return PORTFOLIO_TIME_LOW;
+    }
+    /**
+     * @return bool
+     */
+    public function check_permissions() {
+        $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
+        return has_capability('mod/glossary:exportentry', $context)
+            || ($this->entry->userid == $this->user->id && has_capability('mod/glossary:exportownentry', $context));
+    }
+    /**
+     * @return string
+     */
+    public static function display_name() {
+        return get_string('modulename', 'glossary');
+    }
+    /**
+     * @return object
+     */
+    public function prepare_package() {
+        define('PORTFOLIO_INTERNAL', true);
+        ob_start();
+        $entry = clone $this->entry;
+        glossary_print_entry($this->get('course'), $this->cm, $this->glossary, $entry, null, null, false);
+        $content = ob_get_clean();
+        if ($this->multifiles) {
+            foreach ($this->multifiles as $file) {
+                $this->exporter->copy_existing_file($file);
+            }
+        }
+        return $this->exporter->write_new_file($content, clean_filename($this->entry->concept) . '.html', !empty($files));
+    }
+    /**
+     * @return string
+     */
+    public function get_sha1() {
+        if ($this->multifiles) {
+            return sha1(serialize($this->entry) . $this->get_sha1_file());
+        }
+        return sha1(serialize($this->entry));
+    }
+
+    public static function base_supported_formats() {
+        return array(PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML);
+    }
+}
index 4954db67ef84067ccee0574508e7fe71e4d3bffb..0b988838f95d75fa4a23891149f5b238fd7c5b1a 100644 (file)
@@ -31,10 +31,10 @@ if (empty($CFG->enableportfolios)) {
     print_error('disabled', 'portfolio');
 }
 
-// this will pull in all the other required libraries
 require_once($CFG->libdir . '/portfoliolib.php');
-// so plugins don't have to.
-require_once($CFG->libdir . '/formslib.php');
+require_once($CFG->libdir . '/portfolio/exporter.php');
+require_once($CFG->libdir . '/portfolio/caller.php');
+require_once($CFG->libdir . '/portfolio/plugin.php');
 
 $dataid        = optional_param('id', 0, PARAM_INT);                          // id of partially completed export. corresponds to a record in portfolio_tempdata
 $type          = optional_param('type', null, PARAM_SAFEDIR);                 // if we're returning from an external system (postcontrol) for a single-export only plugin
@@ -223,6 +223,8 @@ if (!$exporter->get('instance')) {
         $instance = array_shift(array_keys($options));
         redirect($CFG->wwwroot . '/portfolio/add.php?id= ' . $exporter->get('id') . '&instance=' . $instance . '&sesskey=' . sesskey());
     }
+    // be very selective about not including this unless we really need to
+    require_once($CFG->libdir . '/portfolio/forms.php');
     $mform = new portfolio_instance_select('', array('id' => $exporter->get('id'), 'caller' => $exporter->get('caller'), 'options' => $options));
     if ($mform->is_cancelled()) {
         $exporter->cancel_request();
index 6c1a4b8690395f4d005891f80995774d0f7fb034..9671f887f53fbb344375f93c21bc27b6e12f24b1 100644 (file)
@@ -11,6 +11,8 @@ if (empty($CFG->enableportfolios)) {
 }
 
 require_once($CFG->libdir.'/portfoliolib.php');
+require_once($CFG->libdir.'/portfolio/exporter.php');
+
 $PAGE->requires->yui_lib('dom');
 $id = required_param('id', PARAM_INT);
 
index 282e7fdc285b7f44c423ca21318e4786130676e1..b0efae0147f339d992c724b6a9e64d183f11cea8 100644 (file)
@@ -34,6 +34,7 @@ if (empty($CFG->enableportfolios)) {
 }
 
 require_once($CFG->libdir . '/portfoliolib.php');
+require_once($CFG->libdir . '/portfolio/exporter.php');
 require_once($CFG->libdir . '/file/stored_file.php');
 require_once($CFG->libdir . '/filelib.php');
 
index 8889b1d30800db303bf500bea31a9a9b5f469611..f42b5e89c1e2b4f1370129e080c24d1b9ba37e00 100644 (file)
@@ -5,7 +5,9 @@ define('PORTFOLIO_MAHARA_ERR_NOHOSTS', 'err_nomnethosts');
 define('PORTFOLIO_MAHARA_ERR_INVALIDHOST', 'err_invalidhost');
 define('PORTFOLIO_MAHARA_ERR_NOMNETAUTH', 'err_nomnetauth');
 
-require_once($CFG->dirroot . '/lib/portfoliolib.php');
+require_once($CFG->libdir . '/portfoliolib.php');
+require_once($CFG->libdir . '/portfolio/plugin.php');
+require_once($CFG->libdir . '/portfolio/exporter.php');
 require_once($CFG->dirroot . '/mnet/lib.php');
 
 define('PORTFOLIO_MAHARA_QUEUE', PORTFOLIO_TIME_HIGH);
@@ -363,8 +365,10 @@ class portfolio_plugin_mahara extends portfolio_plugin_pull_base {
      * which might be higher up the format hierarchy tree (eg 'file')
      */
     private function resolve_format() {
+        global $CFG;
         $thisformat = $this->get_export_config('format');
         $allformats = portfolio_supported_formats();
+        require_once($CFG->libdir . '/portfolio/formats.php');
         $thisobj = new $allformats[$thisformat];
         foreach ($this->supported_formats() as $f) {
             $class = $allformats[$f];
index 3eb8e4d5a6759ad0b65dd6b818895ce5aefa9b74..a3c9a202d3194dac21045d5131b032ef722255e2 100644 (file)
@@ -6,6 +6,8 @@ if (empty($CFG->enableportfolios)) {
 }
 
 require_once($CFG->libdir . '/portfoliolib.php');
+require_once($CFG->libdir . '/portfolio/plugin.php');
+require_once($CFG->libdir . '/portfolio/exporter.php');
 require_once($CFG->dirroot . '/mnet/lib.php');
 
 require_login();
index b9cff92f515d531df02cfda8fe75f6338d2f6968..fc8eecb532752b9a7ebd65936ed1f33cb74d169a 100644 (file)
@@ -30,6 +30,7 @@ if (empty($CFG->enableportfolios)) {
 }
 
 require_once($CFG->libdir . '/portfoliolib.php');
+require_once($CFG->libdir . '/portfolio/forms.php');
 
 $config = optional_param('config', 0, PARAM_INT);
 $hide = optional_param('hide', 0, PARAM_INT);