MDL-15758 - slight change to the portfolio_add_button contract to be more flexible.

This commit is contained in:
mjollnir_ 2008-08-14 16:01:12 +00:00
parent 5e7f2b0b25
commit 866d543fde
10 changed files with 96 additions and 36 deletions

View File

@ -33,6 +33,7 @@ $string['instanceismisconfigured'] = 'Portfolio instance is misconfigured, skipp
$string['instancenotsaved'] = 'Failed to save portfolio';
$string['instancenotdelete'] = 'Failed to delete portfolio';
$string['instancesaved'] = 'Portfolio saved successfully';
$string['invalidaddformat'] = 'Invalid add format passed to portfolio_add_button. ($a) Must be one of PORTFOLIO_ADD_XXX';
$string['invalidtempid'] = 'Invalid export id. maybe it has expired';
$string['invalidformat'] = 'Something is exporting an invalid format, $a';
$string['invalidinstance'] = 'Could not find that portfolio instance';

View File

@ -113,6 +113,34 @@ define('PORTFOLIO_TIME_MODERATE', 'moderate');
*/
define('PORTFOLIO_TIME_HIGH', 'high');
// ************************************************** //
// available ways to add the portfolio export to a page
// ************************************************** //
/**
* a whole form, containing a drop down menu (where necessary)
* and a submit button
*/
define('PORTFOLIO_ADD_FULL_FORM', 1);
/**
* a whole form, containing a drop down menu (where necessary)
* but has an icon instead of a button to submit
*/
define('PORTFOLIO_ADD_ICON_FORM', 2);
/**
* just an icon with a link around it (yuk, as will result in a long url
* only use where necessary)
*/
define('PORTFOLIO_ADD_ICON_LINK', 3);
/**
* just some text with a link around it (yuk, as will result in a long url
* only use where necessary)
*/
define('PORTFOLIO_ADD_TEXT_LINK', 4);
/**
* entry point to add an 'add to portfolio' button somewhere in moodle
@ -131,12 +159,14 @@ define('PORTFOLIO_TIME_HIGH', 'high');
* but more often, the caller is a script.php and the class in a lib.php
* so you can pass it here if necessary.
* this path should be relative (ie, not include) dirroot
* @param boolean $fullform either display the fullform with the dropmenu of available instances
* or just a small icon (which will trigger instance selection in a new screen)
* optional, defaults to true.
* @param int $format format to display the button or form or icon or link.
* See constants PORTFOLIO_ADD_XXX for more info.
* optional, defaults to PORTFOLI_ADD_FULL_FORM
* @param str $addstr string to use for the button or icon alt text or link text.
* this is whole string, not key. optional, defaults to 'Add to portfolio';
* @param boolean $return whether to echo or return content (optional defaults to false (echo)
*/
function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null, $fullform=true, $return=false) {
function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null, $format=PORTFOLIO_ADD_FULL_FORM, $addstr=null, $return=false) {
global $SESSION, $CFG, $COURSE, $USER;
@ -177,7 +207,8 @@ function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null,
$callersupports = call_user_func(array($callbackclass, 'supported_formats'));
$output = '<form method="post" action="' . $CFG->wwwroot . '/portfolio/add.php" id="portfolio-add-button">' . "\n";
$formoutput = '<form method="post" action="' . $CFG->wwwroot . '/portfolio/add.php" id="portfolio-add-button">' . "\n";
$linkoutput = '<a href="' . $CFG->wwwroot . '/portfolio/add.php?';
foreach ($callbackargs as $key => $value) {
if (!empty($value) && !is_string($value) && !is_numeric($value)) {
$a->key = $key;
@ -185,11 +216,14 @@ function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null,
debugging(get_string('nonprimative', 'portfolio', $a));
return;
}
$output .= "\n" . '<input type="hidden" name="ca_' . $key . '" value="' . $value . '" />';
$linkoutput .= 'ca_' . $key . '=' . $value . '&amp;';
$formoutput .= "\n" . '<input type="hidden" name="ca_' . $key . '" value="' . $value . '" />';
}
$output .= "\n" . '<input type="hidden" name="callbackfile" value="' . $callbackfile . '" />';
$output .= "\n" . '<input type="hidden" name="callbackclass" value="' . $callbackclass . '" />';
$output .= "\n" . '<input type="hidden" name="course" value="' . (!empty($COURSE) ? $COURSE->id : 0) . '" />';
$formoutput .= "\n" . '<input type="hidden" name="callbackfile" value="' . $callbackfile . '" />';
$formoutput .= "\n" . '<input type="hidden" name="callbackclass" value="' . $callbackclass . '" />';
$formoutput .= "\n" . '<input type="hidden" name="course" value="' . (!empty($COURSE) ? $COURSE->id : 0) . '" />';
$linkoutput .= 'callbackfile=' . $callbackfile . '&amp;callbackclass='
. $callbackclass . '&amp;course=' . (!empty($COURSE) ? $COURSE->id : 0);
$selectoutput = '';
if (count($instances) == 1) {
$instance = array_shift($instances);
@ -203,22 +237,41 @@ function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null,
debugging(get_string('instancemisconfigured', 'portfolio', get_string($error[$instance->get('id')], 'portfolio_' . $instance->get('plugin'))));
return;
}
$output .= "\n" . '<input type="hidden" name="instance" value="' . $instance->get('id') . '" />';
$formoutput .= "\n" . '<input type="hidden" name="instance" value="' . $instance->get('id') . '" />';
$linkoutput .= '&amp;instance=' . $instance->get('id');
}
else {
$selectoutput = portfolio_instance_select($instances, $callersupports, $callbackclass, 'instance', true);
}
if ($fullform) {
$output .= $selectoutput;
$output .= "\n" . '<input type="submit" value="' . get_string('addtoportfolio', 'portfolio') .'" />';
} else {
$output .= "\n" . '<input type="image" src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . get_string('addtoportfolio', 'portfolio') .'" />';
//@todo replace this with a little icon
if (empty($addstr)) {
$addstr = get_string('addtoportfolio', 'portfolio');
}
$output .= "\n" . '</form>';
if (empty($format)) {
$format = PORTFOLIO_ADD_FULL_FORM;
}
switch ($format) {
case PORTFOLIO_ADD_FULL_FORM:
$formoutput .= $selectoutput;
$formoutput .= "\n" . '<input type="submit" value="' . $addstr .'" />';
$formoutput .= "\n" . '</form>';
break;
case PORTFOLIO_ADD_ICON_FORM:
$formoutput .= $selectoutput;
$formoutput .= "\n" . '<input type="image" src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . $addstr .'" />';
$formoutput .= "\n" . '</form>';
break;
case PORTFOLIO_ADD_ICON_LINK:
$linkoutput .= '"><img src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . $addstr .'" /></a>';
break;
case PORTFOLIO_ADD_TEXT_LINK:
$linkoutput .= '">' . $addstr .'</a>';
break;
default:
debugging('asdfd');
print_error('invalidaddformat', 'portfolio', '', $format);
}
$output = (in_array($format, array(PORTFOLIO_ADD_FULL_FORM, PORTFOLIO_ADD_ICON_FORM)) ? $formoutput : $linkoutput);
if ($return) {
return $output;
} else {

View File

@ -1715,13 +1715,13 @@ class assignment_base {
$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();
$output .= portfolio_add_button('assignment_portfolio_caller', $p, null, false, true);
$output .= portfolio_add_button('assignment_portfolio_caller', $p, null, PORTFOLIO_ADD_ICON_LINK, null, true);
}
$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, true, true);
$output .= '<br />' . portfolio_add_button('assignment_portfolio_caller', $p, null, PORTFOLIO_ADD_FULL_FORM, null, true);
}
}

View File

@ -362,13 +362,13 @@ class assignment_upload extends assignment_base {
if (has_capability('mod/assignment:exportownsubmission', $this->context)) {
$p['file'] = $file->get_id();
$output .= portfolio_add_button('assignment_portfolio_caller', $p, '/mod/assignment/lib.php', false, true);
$output .= portfolio_add_button('assignment_portfolio_caller', $p, '/mod/assignment/lib.php', PORTFOLIO_ADD_ICON_LINK, null, true);
}
$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', true, true);
$output .= '<br />' . portfolio_add_button('assignment_portfolio_caller', $p, '/mod/assignment/lib.php', null, null, true);
}
}

View File

@ -1003,7 +1003,7 @@ 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, false, true);
$replacement[] = portfolio_add_button('data_portfolio_caller', array('id' => $cm->id, 'record' => $record->id), null, PORTFOLIO_ADD_ICON_LINK, null, true);
} else {
$replacement[] = '';
}

View File

@ -161,7 +161,8 @@
$p = array(
'discussionid' => $discussion->id,
);
//$portfolio = portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', true, true);
// @todo penny check these arguments when uncommenting
//$portfolio = portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', null, null, true);
}
echo '<table width="100%" class="discussioncontrols"><tr><td>';

View File

@ -3112,7 +3112,8 @@ 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', false, true);
// @todo penny check these arguments when uncommenting
//$commands[] = portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', PORTFOLIO_ADD_TEXT_LINK, null, true);
}
echo '<div class="commands">';
@ -3880,7 +3881,8 @@ function forum_print_attachments($post, $return=NULL) {
'postid' => $post->id,
'attachment' => 1,
);
//$output .= portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', false, true);
// @todo penny check these arguments when uncommenting
//$output .= portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', PORTFOLIO_ADD_ICON_FORM, null, true);
}
$output .= "<br />";
@ -3899,7 +3901,8 @@ function forum_print_attachments($post, $return=NULL) {
'postid' => $post->id,
'attachment' => 1,
);
//portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', false);
// @todo penny check these arguments when uncommenting
//portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', PORTFOLIO_ADD_ICON_FORM);
}
echo '<br />';
}

View File

@ -57,11 +57,13 @@
</div>
</form>
<?php
require_once($CFG->libdir . '/portfoliolib.php');
$p = array(
'id' => $cm->id,
);
portfolio_add_button('glossary_csv_portfolio_caller', $p, '/mod/glossary/lib.php');
if ($DB->count_records('glossary_entries', array('glossaryid' => $glossary->id)) && true) { // @todo penny capability check
require_once($CFG->libdir . '/portfoliolib.php');
$p = array(
'id' => $cm->id,
);
portfolio_add_button('glossary_csv_portfolio_caller', $p, '/mod/glossary/lib.php');
}
print_box_end();
print_footer($course);
?>

View File

@ -866,7 +866,7 @@ function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$h
'id' => $cm->id,
'entryid' => $entry->id,
);
$return .= portfolio_add_button('glossary_entry_portfolio_caller', $p, false, false, true);
$return .= portfolio_add_button('glossary_entry_portfolio_caller', $p, null, PORTFOLIO_ADD_ICON_LINK, null, true);
}
$return .= "&nbsp;&nbsp;"; // just to make up a little the output in Mozilla ;)

View File

@ -746,7 +746,7 @@ class resource_portfolio_caller extends portfolio_module_caller_base {
return has_capability('mod/resource:exportresource', get_context_instance(CONTEXT_MODULE, $this->cm->id));
}
public static function add_button($resource, $fullform=true, $return=false) {
public static function add_button($resource, $format=null, $return=false) {
if (!has_capability('mod/resource:exportresource', get_context_instance(CONTEXT_MODULE, $resource->cm->id))) {
return;
}
@ -754,7 +754,7 @@ class resource_portfolio_caller extends portfolio_module_caller_base {
debugging(get_string('portfolionotimplemented', 'resource'));
return false;
}
return portfolio_add_button('resource_portfolio_caller', array('id' => $resource->cm->instance), '/mod/resource/lib.php', $fullform, $return);
return portfolio_add_button('resource_portfolio_caller', array('id' => $resource->cm->instance), '/mod/resource/lib.php', $format, null, $return);
}
public function get_sha1() {