From 0d06b6fda771f5369509361ec46dce67a1b9c398 Mon Sep 17 00:00:00 2001 From: mjollnir_ Date: Thu, 11 Sep 2008 13:42:58 +0000 Subject: [PATCH] MDL-16423 - big refactor of the way callers interact with the portfolio code 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. --- lang/en_utf8/portfolio.php | 1 + lib/portfolio/caller.php | 26 ++++++ lib/portfoliolib.php | 17 ++-- mod/assignment/lib.php | 56 +++++------ .../type/online/assignment.class.php | 8 +- .../type/upload/assignment.class.php | 18 ++-- mod/chat/lib.php | 35 ++++--- mod/chat/report.php | 19 ++-- mod/data/lib.php | 49 +++++++--- mod/forum/discuss.php | 14 +-- mod/forum/lib.php | 92 +++++++++---------- mod/glossary/export.php | 7 +- mod/glossary/lib.php | 44 ++++++--- mod/resource/lib.php | 31 +++++-- portfolio/add.php | 1 + 15 files changed, 246 insertions(+), 172 deletions(-) diff --git a/lang/en_utf8/portfolio.php b/lang/en_utf8/portfolio.php index f3040f4e7f..2a7b1356e6 100644 --- a/lang/en_utf8/portfolio.php +++ b/lang/en_utf8/portfolio.php @@ -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'; diff --git a/lib/portfolio/caller.php b/lib/portfolio/caller.php index 1ed9329795..4923c8f6bf 100644 --- a/lib/portfolio/caller.php +++ b/lib/portfolio/caller.php @@ -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 */ diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php index 3a6addb2f8..cf3e8c1459 100644 --- a/lib/portfoliolib.php +++ b/lib/portfoliolib.php @@ -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. * diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 38ea4f8b27..84864d648a 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -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 .= ''.$icon.''.s($filename).''; 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 .= '
'; } - if ($this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) { - unset($p['file']);// for all files - $output .= '
' . 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 .= '
' . $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(); diff --git a/mod/assignment/type/online/assignment.class.php b/mod/assignment/type/online/assignment.class.php index 381742239e..eb6b4eed9b 100644 --- a/mod/assignment/type/online/assignment.class.php +++ b/mod/assignment/type/online/assignment.class.php @@ -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 '
'. get_string('guestnosubmit', 'assignment').'
'; } else if ($this->isopen()){ //fix for #4206 diff --git a/mod/assignment/type/upload/assignment.class.php b/mod/assignment/type/upload/assignment.class.php index 7b2904cf42..c554e30aff 100644 --- a/mod/assignment/type/upload/assignment.class.php +++ b/mod/assignment/type/upload/assignment.class.php @@ -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 .= '
'; } - if (has_capability('mod/assignment:exportownsubmission', $this->context)) { - unset($p['file']);// for all files - $output .= '
' . 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(); } } diff --git a/mod/chat/lib.php b/mod/chat/lib.php index 80ca5768fb..b4976007c6 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -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() { diff --git a/mod/chat/report.php b/mod/chat/report.php index ad1f61989c..19e72c6bdc 100644 --- a/mod/chat/report.php +++ b/mod/chat/report.php @@ -81,12 +81,14 @@ || (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 '
' . 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(); } @@ -199,12 +201,14 @@ || (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 '
' . 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 "
id&start=$sessionstart&end=$sessionend&deletesession=1\">$strdeletesession"; @@ -222,10 +226,9 @@ if (has_capability('mod/chat:exportsession', $context)) { require_once($CFG->libdir . '/portfoliolib.php'); - $p = array( - 'id' => $cm->id, - ); - echo '
' . 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')); } diff --git a/mod/data/lib.php b/mod/data/lib.php index 1858fc248b..5dd7e7b838 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -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': diff --git a/mod/forum/discuss.php b/mod/forum/discuss.php index 552c5cae25..db1c1414db 100644 --- a/mod/forum/discuss.php +++ b/mod/forum/discuss.php @@ -156,13 +156,6 @@ /// 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 '
'; // groups selector not needed here @@ -171,8 +164,11 @@ forum_print_mode_form($discussion->id, $displaymode); echo ""; - if (isset($portfolio)) { - echo $portfolio . ''; + 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 ''; } if ($forum->type != 'single' diff --git a/mod/forum/lib.php b/mod/forum/lib.php index c7afd49075..dbe3afef11 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -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 '
'; @@ -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 .= "$iconimage "; $output .= "".s($filename).""; 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 .= "
"; @@ -3932,35 +3930,17 @@ function forum_print_attachments($post, $cm, $type) { // Image attachments don't get printed as links $imagereturn .= "
\"\""; 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 .= "$iconimage "; $output .= filter_text("".s($filename).""); 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 .= '
'; } @@ -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'); } diff --git a/mod/glossary/export.php b/mod/glossary/export.php index 9a26d068de..c64c768b3b 100644 --- a/mod/glossary/export.php +++ b/mod/glossary/export.php @@ -60,10 +60,9 @@ // 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); diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index f15ab95237..c19abb0f05 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -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 .= "  "; // 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'); } diff --git a/mod/resource/lib.php b/mod/resource/lib.php index 71833bd783..31e09fcf56 100644 --- a/mod/resource/lib.php +++ b/mod/resource/lib.php @@ -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() { diff --git a/portfolio/add.php b/portfolio/add.php index 86e77b2443..32116e84cc 100644 --- a/portfolio/add.php +++ b/portfolio/add.php @@ -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()); } -- 2.39.5