MDL-16423 - big refactor of the way callers interact with the portfolio code
authormjollnir_ <mjollnir_>
Thu, 11 Sep 2008 13:42:58 +0000 (13:42 +0000)
committermjollnir_ <mjollnir_>
Thu, 11 Sep 2008 13:42:58 +0000 (13:42 +0000)
added two new contract methods to the caller class, load_data and expected_callbackargs (static)
this means that the base class is the only place that needs a constructor
and that no data loading happens in the constructor
this in turn means we can check callback argument validity much more lightly

also completely remoted portfolio_add_button function and replaced with a class
as the argument list was getting out of control.  it's now much more readable.

15 files changed:
lang/en_utf8/portfolio.php
lib/portfolio/caller.php
lib/portfoliolib.php
mod/assignment/lib.php
mod/assignment/type/online/assignment.class.php
mod/assignment/type/upload/assignment.class.php
mod/chat/lib.php
mod/chat/report.php
mod/data/lib.php
mod/forum/discuss.php
mod/forum/lib.php
mod/glossary/export.php
mod/glossary/lib.php
mod/resource/lib.php
portfolio/add.php

index f3040f4..2a7b135 100644 (file)
@@ -59,6 +59,7 @@ $string['invalidconfigproperty'] = 'Could not find that config property ($a->pro
 $string['invalidbuttonproperty'] = 'Could not find that property ($a) of portfolio_button';
 $string['manageportfolios'] = 'Manage portfolios';
 $string['manageyourportfolios'] = 'Manage your portfolios';
+$string['missingcallbackarg'] = 'Missing callback argument $a->arg for class $a->class';
 $string['moderatefilesizethreshold'] = 'Moderate transfer filesize';
 $string['moderatefilesizethresholddesc'] = 'Filesizes over this threshold will be considered to take a moderate amount of time to transfer';
 $string['moderatedbsizethreshold'] = 'Moderate transfer dbsize';
index 1ed9329..4923c8f 100644 (file)
@@ -64,6 +64,21 @@ abstract class portfolio_caller_base {
     */
     protected $supportedformats;
 
+
+    public function __construct($callbackargs) {
+        $expected = call_user_func(array(get_class($this), 'expected_callbackargs'));
+        foreach ($expected as $key => $required) {
+            if (!array_key_exists($key, $callbackargs)) {
+                if ($required) {
+                    $a = (object)array('key' => $key, 'class' => get_class($this));
+                    throw new portfolio_caller_exception('missingcallbackarg', 'portfolio', null, $a);
+                }
+                continue;
+            }
+            $this->{$key} = $callbackargs[$key];
+        }
+    }
+
     /**
     * if this caller wants any additional config items
     * they should be defined here.
@@ -281,6 +296,11 @@ abstract class portfolio_caller_base {
     public function heading_summary() {
         return get_string('exportingcontentfrom', 'portfolio', $this->display_name());
     }
+
+    public abstract function load_data();
+
+    public static abstract function expected_callbackargs();
+
 }
 
 /**
@@ -301,6 +321,12 @@ abstract class portfolio_module_caller_base extends portfolio_caller_base {
     */
     protected $cm;
 
+    /**
+    *
+    * int cmid
+    */
+    protected $id;
+
     /**
     * stdclass course object
     */
index 3a6addb..cf3e8c1 100644 (file)
@@ -124,6 +124,12 @@ class portfolio_add_button {
         if (!class_exists($class)) {
             throw new portfolio_button_exception('nocallbackclass', 'portfolio', $class);
         }
+
+        // this will throw exceptions
+        // but should not actually do anything other than verify callbackargs
+        $test = new $class($argarray);
+        unset($test);
+
         $this->callbackclass = $class;
         $this->callbackargs = $argarray;
     }
@@ -309,17 +315,6 @@ class portfolio_add_button {
     }
 }
 
-
-function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null, $format=PORTFOLIO_ADD_FULL_FORM, $addstr=null, $return=false, $callersupports=null) {
-    $button = new portfolio_add_button();
-    $button->set_callback_options($callbackclass, $callbackargs, $callbackfile);
-    $button->set_formats($callersupports);
-    if ($return) {
-        return $button->to_html($format, $addstr);
-    }
-    $button->render();
-}
-
 /**
 * returns a drop menu with a list of available instances.
 *
index 38ea4f8..84864d6 100644 (file)
@@ -1705,10 +1705,7 @@ class assignment_base {
         $found = false;
 
         if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) {
-            $p = array(
-                'assignmentid' => $this->cm->id,
-                'userid'       => $USER->id,
-            );
+            $button = new portfolio_add_button();
             foreach ($files as $file) {
                 $filename = $file->get_filename();
                 $found = true;
@@ -1717,15 +1714,16 @@ class assignment_base {
                 $path = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
                 $output .= '<a href="'.$path.'" ><img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.s($filename).'</a>';
                 if ($this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) {
-                    $p['file'] = $file->get_id();
-                    $formats = array(portfolio_format_from_file($file));
-                    $output .= portfolio_add_button('assignment_portfolio_caller', $p, null, PORTFOLIO_ADD_ICON_LINK, null, true, $formats);
+                    $button->set_formats(portfolio_format_from_file($file));
+                    $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'file' => $file->get_id()));
+                    $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                 }
                 $output .= '<br />';
             }
-            if ($this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) {
-                unset($p['file']);// for all files
-                $output .= '<br />' . portfolio_add_button('assignment_portfolio_caller', $p, null, PORTFOLIO_ADD_FULL_FORM, null, true);
+            if (count($files) > 1  && $this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) {
+                $button->set_formats(PORTFOLIO_PORMAT_FILE);
+                $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id));
+                $output .= '<br />'  . $button->to_html();
             }
         }
 
@@ -3147,13 +3145,20 @@ class assignment_portfolio_caller extends portfolio_module_caller_base {
 
     private $assignment;
     private $assignmentfile;
-    private $file;
     private $files;
+    protected $fileid;
 
-    public function __construct($callbackargs) {
+    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', $callbackargs['assignmentid'])) {
+        if (! $this->cm = get_coursemodule_from_id('assignment', $this->id)) {
             throw new portfolio_caller_exception('invalidcoursemodule');
         }
 
@@ -3161,27 +3166,27 @@ class assignment_portfolio_caller extends portfolio_module_caller_base {
             throw new portfolio_caller_exception('invalidid', 'assignment');
         }
 
-        if (!array_key_exists('userid', $callbackargs)) {
-            throw new portfolio_caller_exception('invaliduserid', 'assignment');
-        }
-        if (!$this->user = $DB->get_record('user', array('id' => $callbackargs['userid']))) {
-            throw new portfolio_caller_exception('invaliduserid', 'assignment');
-        }
         $this->assignmentfile = $CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php';
         require_once($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());
         }
+
         $fs = get_file_storage();
-        if (array_key_exists('file', $callbackargs)) {
-            $this->file = $fs->get_file_by_id($callbackargs['file']);
-            $this->files = array($this->file);
-            $this->supportedformats = array(portfolio_format_from_file($this->file));
+        if ($this->fileid) {
+            $f = $fs->get_file_by_id($this->fileid);
+            $this->files = array($f);
         } else {
             $this->files = $fs->get_area_files($this->assignment->context->id, 'assignment_submission', $this->user->id, '', false);
         }
+        if (is_array($this->files) && count($this->files) == 1) {
+            $f = array_values($this->files);
+            $this->supportedformats = array(portfolio_format_from_file($f[0]));
+        }
         if (empty($this->supportedformats) && is_callable(array($this->assignment, 'portfolio_supported_formats'))) {
             $this->supportedformats = $this->assignment->portfolio_supported_formats();
         }
@@ -3203,11 +3208,6 @@ class assignment_portfolio_caller extends portfolio_module_caller_base {
         if (is_callable(array($this->assignment, 'portfolio_get_sha1'))) {
             return $this->assignment->portfolio_get_sha1($this->user->id);
         }
-
-        // default ...
-        if ($this->file) {
-            return $this->file->get_contenthash();
-        }
         $sha1s = array();
         foreach ($this->files as $file) {
             $sha1s[] = $file->get_contenthash();
index 3817422..eb6b4ee 100644 (file)
@@ -103,11 +103,9 @@ class assignment_online extends assignment_base {
                 print_box_start('generalbox boxwidthwide boxaligncenter', 'online');
                 if ($submission && has_capability('mod/assignment:exportownsubmission', $this->context)) {
                     echo format_text($submission->data1, $submission->data2);
-                    $p = array(
-                        'userid' => $USER->id,
-                        'assignmentid' => $this->cm->id,
-                    );
-                    portfolio_add_button('assignment_portfolio_caller', $p, '/mod/assignment/lib.php');
+                    $button = new portfolio_add_button();
+                    $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/lib.php');
+                    $button->render();
                 } else if (!has_capability('mod/assignment:submit', $context)) { //fix for #4604
                     echo '<div style="text-align:center">'. get_string('guestnosubmit', 'assignment').'</div>';
                 } else if ($this->isopen()){    //fix for #4206
index 7b2904c..c554e30 100644 (file)
@@ -342,10 +342,7 @@ class assignment_upload extends assignment_base {
         $browser = get_file_browser();
 
         if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) {
-            $p = array(
-                'assignmentid' => $this->cm->id,
-                'userid'       => $USER->id,
-            );
+            $button = new portfolio_add_button();
             foreach ($files as $file) {
                 $filename = $file->get_filename();
                 $mimetype = $file->get_mimetype();
@@ -361,15 +358,16 @@ class assignment_upload extends assignment_base {
                 }
     
                 if (has_capability('mod/assignment:exportownsubmission', $this->context)) {
-                    $p['file'] = $file->get_id();
-                    $formats = array(portfolio_format_from_file($file));
-                    $output .= portfolio_add_button('assignment_portfolio_caller', $p, '/mod/assignment/lib.php', PORTFOLIO_ADD_ICON_LINK, null, true, $formats);
+                    $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'file' => file->get_id()), '/mod/assignment/lib.php');
+                    $button->set_formats(portfolio_format_from_file($file));
+                    $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                 }
                 $output .= '<br />';
             }
-            if (has_capability('mod/assignment:exportownsubmission', $this->context)) {
-                unset($p['file']);// for all files
-                $output .= '<br />' . portfolio_add_button('assignment_portfolio_caller', $p, '/mod/assignment/lib.php', null, null, true);
+            if (count($files) > 1 && has_capability('mod/assignment:exportownsubmission', $this->context)) {
+                $button->set_formats(PORTFOLIO_FORMAT_FILE);
+                $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/lib.php');
+                $output .= $button->to_html();
             }
         }
 
index 80ca576..b497600 100644 (file)
@@ -823,24 +823,30 @@ require_once($CFG->libdir . '/portfoliolib.php');
 class chat_portfolio_caller extends portfolio_module_caller_base {
 
     private $chat;
-    private $start;
-    private $end;
+    protected $start;
+    protected $end;
+
+    public static function expected_callbackargs() {
+        return array(
+            'id'    => true,
+            'start' => false,
+            'end'   => false,
+        );
+    }
+
+    public function load_data() {
+        global $DB;
 
-    public function __construct($callbackargs) {
-        global $DB, $USER;
-        if (!$this->cm = get_coursemodule_from_id('chat', $callbackargs['id'])) {
+        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 (array_key_exists('start', $callbackargs) && array_key_exists('end', $callbackargs)
-            && !empty($callbackargs['start']) && !empty($callbackargs['end'])) {
+        if ($this->start && $this->end) {
             $select .= ' AND timestamp >= ? AND timestamp <= ?';
-            $params[] = $callbackargs['start'];
-            $params[] = $callbackargs['end'];
-            $this->start =  $callbackargs['start'];
-            $this->end =  $callbackargs['end'];
+            $params[] = $this->start;
+            $params[] = $this->end;
         }
         $this->messages = $DB->get_records_select(
                 'chat_messages',
@@ -849,13 +855,16 @@ class chat_portfolio_caller extends portfolio_module_caller_base {
                 'timestamp ASC'
             );
         $select .= ' AND userid = ?';
-        $params[] = $USER->id;
+        $params[] = $this->user->id;
         $this->participated = $DB->record_exists_select(
             'chat_messages',
             $select,
             $params
         );
-        $this->supportedformats = array(PORTFOLIO_FORMAT_HTML);
+    }
+
+    public static function supported_formats($caller) {
+        return array(PORTFOLIO_FORMAT_HTML);
     }
 
     public function expected_time() {
index ad1f619..19e72c6 100644 (file)
                     || (array_key_exists($USER->id, $sessionusers)
                         && has_capability('mod/chat:exportparticipatedsession', $context))) {
                     require_once($CFG->libdir . '/portfoliolib.php');
-                    $p  = array(
+                    $buttonoptions  = array(
                         'id'    => $cm->id,
                         'start' => $start,
                         'end'   => $end,
                     );
-                    echo '<br />' . portfolio_add_button('chat_portfolio_caller', $p, '/mod/chat/lib.php', PORTFOLIO_ADD_FULL_FORM, null, true);
+                    $button = new portfolio_add_button();
+                    $button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/lib.php');
+                    $button->render();
                 }
             print_simple_box_end();
         }
                     || (array_key_exists($USER->id, $sessionusers)
                         && has_capability('mod/chat:exportparticipatedsession', $context))) {
                     require_once($CFG->libdir . '/portfoliolib.php');
-                    $p  = array(
+                    $buttonoptions  = array(
                         'id'    => $cm->id,
                         'start' => $sessionstart,
                         'end'   => $sessionend,
                     );
-                    echo '<br />' . portfolio_add_button('chat_portfolio_caller', $p, '/mod/chat/lib.php', PORTFOLIO_ADD_TEXT_LINK, null, true);
+                    $button = new portfolio_add_button();
+                    $button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/lib.php');
+                    $button->render(PORTFOLIO_ADD_TEXT_LINK);
                 }
                 if (has_capability('mod/chat:deletelog', $context)) {
                     echo "<br /><a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend&amp;deletesession=1\">$strdeletesession</a>";
 
     if (has_capability('mod/chat:exportsession', $context)) {
         require_once($CFG->libdir . '/portfoliolib.php');
-        $p  = array(
-            'id'    => $cm->id,
-        );
-        echo '<br />' . portfolio_add_button('chat_portfolio_caller', $p, '/mod/chat/lib.php', PORTFOLIO_ADD_FULL_FORM, get_string('addalltoportfolio', 'portfolio'), true);
+        $button = new portfolio_add_button();
+        $button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id), '/mod/chat/lib.php');
+        $button->render(null, get_string('addalltoportfolio', 'portfolio'));
     }
 
 
index 1858fc2..5dd7e7b 100755 (executable)
@@ -1020,7 +1020,10 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
             && ((has_capability('mod/data:exportentry', $context)
                 || (data_isowner($record->id) && has_capability('mod/data:exportownentry', $context))))) {
             require_once($CFG->libdir . '/portfoliolib.php');
-            $replacement[] = portfolio_add_button('data_portfolio_caller', array('id' => $cm->id, 'record' => $record->id), null, PORTFOLIO_ADD_ICON_LINK, null, true);
+            $button = new portfolio_add_button();
+            $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id, 'record' => $record->id));
+            $button->set_formats(PORTFOLIO_FORMAT_HTML);
+            $replacement[] = $button->to_html(PORTFOLIO_ADD_ICON_LINK);
         } else {
             $replacement[] = '';
         }
@@ -2436,18 +2439,42 @@ function data_get_exportdata($dataid, $fields, $selectedfields) {
 require_once($CFG->libdir . '/portfoliolib.php');
 class data_portfolio_caller extends portfolio_module_caller_base {
 
+    protected $recordid;
+    protected $exporttype;
+    protected $delimiter_name;
+
     private $data;
     private $selectedfields;
-    private $exporttype;
     private $fields;
     private $fieldtypes;
-    private $delimiter;
     private $exportdata;
     private $singlerecord;
 
+    public static function expected_callbackargs() {
+        return array(
+            'id'             => true,
+            'recordid'       => false,
+            'delimiter_name' => false,
+            'exporttype'     => false,
+        );
+    }
+
     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);
+            }
+        }
+    }
+
+    public function load_data() {
         global $DB;
-        if (!$this->cm = get_coursemodule_from_id('data', $callbackargs['id'])) {
+        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));
@@ -2460,23 +2487,15 @@ class data_portfolio_caller extends portfolio_module_caller_base {
             $this->fieldtypes[]  = $tmp->type;
         }
 
-        if (array_key_exists('record', $callbackargs) && !empty($callbackargs['record'])) {
+        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' => $callbackargs['record']));
+            $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';
             $this->supportedformats = array(PORTFOLIO_FORMAT_HTML);
         } else {
             // all records as csv or whatever
-            $this->selectedfields = array();
-            foreach ($callbackargs as $key => $value) {
-                if (strpos($key, 'field_') === 0) {
-                    $this->selectedfields[] = substr($key, 6);
-                }
-            }
-            $this->delimiter = array_key_exists('delimiter_name', $callbackargs) ? $callbackargs['delimiter_name'] : null;
-            $this->exporttype = array_key_exists('exporttype', $callbackargs) ? $callbackargs['exporttype'] : 'csv'; //@todo penny later support more
             $this->exportdata = data_get_exportdata($this->cm->instance, $this->fields, $this->selectedfields);
         }
     }
@@ -2528,7 +2547,7 @@ class data_portfolio_caller extends portfolio_module_caller_base {
                 $filename = clean_filename($this->cm->name . '-entry.html');
                 break;
             case 'csv':
-                $content = data_export_csv($this->exportdata, $this->delimiter, $this->cm->name, $count, true);
+                $content = data_export_csv($this->exportdata, $this->delimiter_name, $this->cm->name, $count, true);
                 $filename = clean_filename($this->cm->name . '.csv');
                 break;
             case 'xls':
index 552c5ca..db1c141 100644 (file)
 
 /// Print the controls across the top
 
-    if (has_capability('mod/forum:exportdiscussion', $modcontext)) {
-
-        $p = array(
-            'discussionid' => $discussion->id,
-        );
-        $portfolio = portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', null, null, true);
-    }
     echo '<table width="100%" class="discussioncontrols"><tr><td>';
 
     // groups selector not needed here
     forum_print_mode_form($discussion->id, $displaymode);
     echo "</td><td>";
 
-    if (isset($portfolio)) {
-        echo $portfolio . '</td><td>';
+    if (has_capability('mod/forum:exportdiscussion', $modcontext)) {
+        $button = new portfolio_add_button();
+        $button->set_callback_options('forum_portfolio_caller', array('discussionid' => $discussion->id), '/mod/forum/lib.php');
+        $button->render();
+        echo '</td><td>';
     }
 
     if ($forum->type != 'single'
index c7afd49..dbe3afe 100644 (file)
@@ -3175,7 +3175,12 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa
         $p = array(
             'postid' => $post->id,
         );
-        $commands[] = portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', PORTFOLIO_ADD_TEXT_LINK, null, true);
+        $button = new portfolio_add_button();
+        $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id));
+        if (empty($attachments)) {
+            $button->set_formats(PORTFOLIO_FORMAT_HTML);
+        }
+        $commands[] = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
     }
 
     echo '<div class="commands">';
@@ -3898,6 +3903,8 @@ function forum_print_attachments($post, $cm, $type) {
     $canexport = (has_capability('mod/forum:exportpost', $context) || ($post->userid == $USER->id && has_capability('mod/forum:exportownpost')));
 
     if ($files = $fs->get_area_files($context->id, 'forum_attachment', $post->id, "timemodified", false)) {
+        require_once($CFG->libdir . '/portfoliolib.php');
+        $button = new portfolio_add_button();
         foreach ($files as $file) {
             $filename = $file->get_filename();
             $mimetype = $file->get_mimetype();
@@ -3909,18 +3916,9 @@ function forum_print_attachments($post, $cm, $type) {
                 $output .= "<a href=\"$path\">$iconimage</a> ";
                 $output .= "<a href=\"$path\">".s($filename)."</a>";
                 if ($canexport) {
-                    require_once($CFG->libdir . '/portfoliolib.php');
-                    $p = array(
-                        'postid' => $post->id,
-                        'attachment' => $file->get_id(),
-                    );
-                    $output .= portfolio_add_button(
-                        'forum_portfolio_caller',
-                        $p, '/mod/forum/lib.php',
-                        PORTFOLIO_ADD_ICON_LINK,
-                        null, true,
-                        array(portfolio_format_from_file($file))
-                    );
+                    $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()));
+                    $button->set_formats(portfolio_format_from_file($file));
+                    $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                 }
                 $output .= "<br />";
 
@@ -3932,35 +3930,17 @@ function forum_print_attachments($post, $cm, $type) {
                     // Image attachments don't get printed as links
                     $imagereturn .= "<br /><img src=\"$path\" alt=\"\" />";
                     if ($canexport) {
-                        require_once($CFG->libdir . '/portfoliolib.php');
-                        $p = array(
-                            'postid' => $post->id,
-                            'attachment' => $file->get_id(),
-                        );
-                        $imagereturn .= portfolio_add_button(
-                            'forum_portfolio_caller',
-                            $p, '/mod/forum/lib.php',
-                            PORTFOLIO_ADD_ICON_LINK,
-                            null, true,
-                            array(portfolio_format_from_file($file))
-                        );
+                        $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()));
+                        $button->set_formats(portfolio_format_from_file($file));
+                        $imagereturn .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                     }
                 } else {
                     $output .= "<a href=\"$path\">$iconimage</a> ";
                     $output .= filter_text("<a href=\"$path\">".s($filename)."</a>");
                     if ($canexport) {
-                        require_once($CFG->libdir . '/portfoliolib.php');
-                        $p = array(
-                            'postid' => $post->id,
-                            'attachment' => $file->get_id(),
-                        );
-                        $output .= portfolio_add_button(
-                            'forum_portfolio_caller',
-                            $p, '/mod/forum/lib.php',
-                            PORTFOLIO_ADD_ICON_LINK,
-                            null, true,
-                            array(portfolio_format_from_file($file))
-                        );
+                        $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()));
+                        $button->set_formats(portfolio_format_from_file($file));
+                        $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
                     }
                     $output .= '<br />';
                 }
@@ -7225,44 +7205,64 @@ function forum_get_extra_capabilities() {
 require_once($CFG->libdir . '/portfoliolib.php');
 class forum_portfolio_caller extends portfolio_module_caller_base {
 
+    protected $postid;
+    protected $discussionid;
+    protected $attachment;
+
     private $post;
     private $forum;
     private $discussion;
-    private $attachment;
     private $postfiles;
     private $allfiles;
     private $posts;
 
+    public static function expected_callbackargs() {
+        return array(
+            'postid'       => false,
+            'discussionid' => false,
+            'attachment'   => false,
+        );
+    }
+
     function __construct($callbackargs) {
+        parent::__construct($callbackargs);
+        if (!$this->postid && !$this->discussionid) {
+            throw new portfolio_caller_exception('mustprovidediscussionorpost', 'forum');
+        }
+    }
+
+    public function load_data() {
         global $DB;
 
-        if (array_key_exists('postid', $callbackargs)) {
-            if (!$this->post = $DB->get_record('forum_posts', array('id' => $callbackargs['postid']))) {
+        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 (array_key_exists('discussionid', $callbackargs)) {
-            $dbparams = array('id' => $callbackargs['discussionid']);
+        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');
         }
+
         $fs = get_file_storage();
-        if ($this->attachment = (array_key_exists('attachment', $callbackargs) ? $callbackargs['attachment'] : false)) {
-            if (!$this->post) {
-                throw new portfolio_caller_exception('attachmentsnopost', 'forum');
-            }
+        if ($this->attachment) {
             if (!$f = $fs->get_file_by_id($this->attachment)) {
                 throw new portfolio_caller_exception('noattachments', 'forum');
             }
index 9a26d06..c64c768 100644 (file)
     // don't need cap check here, we share with the general export.
     if ($DB->count_records('glossary_entries', array('glossaryid' => $glossary->id))) {
         require_once($CFG->libdir . '/portfoliolib.php');
-        $p = array(
-            'id' => $cm->id,
-        );
-        portfolio_add_button('glossary_csv_portfolio_caller', $p, '/mod/glossary/lib.php');
+        $button = new portfolio_add_button();
+        $button->set_callback_options('glossary_csv_portfolio_caller', array('id' => $cm->id), '/mod/glossary/lib.php');
+        $button->render();
     }
     print_box_end();
     print_footer($course);
index f15ab95..c19abb0 100644 (file)
@@ -903,11 +903,9 @@ function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$h
         || ($entry->userid == $USER->id
             && has_capability('mod/glossary:exportownentry', $context))) {
         require_once($CFG->libdir . '/portfoliolib.php');
-        $p = array(
-            'id' => $cm->id,
-            'entryid' => $entry->id,
-        );
-        $return .= portfolio_add_button('glossary_entry_portfolio_caller', $p, null, PORTFOLIO_ADD_ICON_LINK, null, true);
+        $button = new portfolio_add_button();
+        $button->set_callback_options('glossary_entry_portfolio_caller',  array('id' => $cm->id, 'entryid' => $entry->id));
+        $return .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
     }
     $return .= "&nbsp;&nbsp;"; // just to make up a little the output in Mozilla ;)
 
@@ -2402,9 +2400,15 @@ class glossary_csv_portfolio_caller extends portfolio_module_caller_base {
     private $glossary;
     private $exportdata;
 
-    public function __construct($callbackargs) {
+    public static function expected_callbackargs() {
+        return array(
+            'id' => true,
+        );
+    }
+
+    public function load_data() {
         global $DB;
-        if (!$this->cm = get_coursemodule_from_id('glossary', $callbackargs['id'])) {
+        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))) {
@@ -2467,18 +2471,29 @@ class glossary_entry_portfolio_caller extends portfolio_module_caller_base {
 
     private $glossary;
     private $entry;
+    protected $entryid;
 
-    public function __construct($callbackargs) {
+    public static function expected_callbackargs() {
+        return array(
+            'entryid' => true,
+            'id'      => true,
+        );
+    }
+
+    public function load_data() {
         global $DB;
-        if (!$this->cm = get_coursemodule_from_id('glossary', $callbackargs['id'])) {
+        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 (!array_key_exists('entryid', $callbackargs)
-            || !$this->entry = $DB->get_record('glossary_entries', array('id' => $callbackargs['entryid']))) {
-            throw new portfolio_caller_exception('noentry', '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;
         }
         $this->supportedformats = array(PORTFOLIO_FORMAT_HTML);
     }
@@ -2498,11 +2513,10 @@ class glossary_entry_portfolio_caller extends portfolio_module_caller_base {
     }
 
     public function prepare_package() {
-        // in case we don't have USER this will make the entry be printed
-        $this->entry->approved = true;
         define('PORTFOLIO_INTERNAL', true);
         ob_start();
-        glossary_print_entry($this->get('course'), $this->cm, $this->glossary, $this->entry, null, null, false);
+        $entry = clone $this->entry;
+        glossary_print_entry($this->get('course'), $this->cm, $this->glossary, $entry, null, null, false);
         $content = ob_get_clean();
         return $this->exporter->write_new_file($content, clean_filename($this->entry->concept) . '.html');
     }
index 71833bd..31e09fc 100644 (file)
@@ -711,10 +711,15 @@ class resource_portfolio_caller extends portfolio_module_caller_base {
     private $resource;
     private $resourcefile;
 
-    public function __construct($callbackargs) {
-        global $CFG;
-        global $DB;
-        if (!array_key_exists('id', $callbackargs) || !$this->cm = get_coursemodule_from_instance('resource', $callbackargs['id'])) {
+    public static function expected_callbackargs() {
+        return array(
+            'id' => true,
+        );
+    }
+
+    public function load_data() {
+        global $CFG, $DB;
+        if (!$this->cm = get_coursemodule_from_instance('resource', $this->id)) {
             throw new portfolio_caller_exception('invalidid');
         }
         $this->cm->type = $DB->get_field('resource', 'type', array('id' => $this->cm->instance));
@@ -722,9 +727,13 @@ class resource_portfolio_caller extends portfolio_module_caller_base {
         $this->resourcefile = $CFG->dirroot.'/mod/resource/type/'.$this->cm->type.'/resource.class.php';
         require_once($this->resourcefile);
         $this->resource= new $resourceclass($this->cm->id);
+        $this->supportedformats = array(self::type_to_format($this->cm->type));
+    }
+
+    public static function type_to_format($type) {
         // this is kind of yuk... but there's just not good enough OO here
         $format = PORTFOLIO_FORMAT_FILE;
-        switch ($this->cm->type) {
+        switch ($type) {
             case 'html':
                 $format = PORTFOLIO_FORMAT_HTML;
             case 'text':
@@ -732,7 +741,7 @@ class resource_portfolio_caller extends portfolio_module_caller_base {
             case 'file':
                 // $format = portfolio_format_from_file($file); // change after we switch upload type resources over to new files api.
         }
-        $this->supportedformats = array($format);
+        return $format;
     }
 
     public function __wakeup() {
@@ -766,11 +775,17 @@ class resource_portfolio_caller extends portfolio_module_caller_base {
             debugging(get_string('portfolionotimplemented', 'resource'));
             return false;
         }
-        $callersupports = array();
+        $callersupports = array(self::type_to_format($resource->cm->type));
         if ($resource->cm->type == 'file') {
             // $callersupports = array(portfolio_format_from_file($file);
         }
-        return portfolio_add_button('resource_portfolio_caller', array('id' => $resource->cm->instance),  '/mod/resource/lib.php', $format, null, $return, $callersupports);
+        $button = new portfolio_add_button();
+        $button->set_callback_options('resource_portfolio_caller', array('id' => $resource->cm->instance),  '/mod/resource/lib.php');
+        $button->set_formats($callersupports);
+        if ($return) {
+            return $button->to_html($format);
+        }
+        $button->render($format);
     }
 
     public function get_sha1() {
index 86e77b2..32116e8 100644 (file)
@@ -83,6 +83,7 @@ if ($dataid) {
     require_once($CFG->dirroot . $callbackfile);
     $caller = new $callbackclass($callbackargs);
     $caller->set('user', $USER);
+    $caller->load_data();
     if (!$caller->check_permissions()) {
         throw new portfolio_caller_exception('nopermissions', 'portfolio', $caller->get_return_url());
     }