From 3efe78dfc412f984abe052f73a63aaaa5e252402 Mon Sep 17 00:00:00 2001 From: mjollnir_ Date: Mon, 28 Jul 2008 08:19:14 +0000 Subject: [PATCH] MDL-15758 - added add to portfolio button to resource/text & html --- lang/en_utf8/resource.php | 1 + mod/resource/lib.php | 71 +++++++++++++++++++++++ mod/resource/type/html/resource.class.php | 13 +++++ mod/resource/type/text/resource.class.php | 12 ++++ 4 files changed, 97 insertions(+) diff --git a/lang/en_utf8/resource.php b/lang/en_utf8/resource.php index b01d768053..e8342cfb37 100644 --- a/lang/en_utf8/resource.php +++ b/lang/en_utf8/resource.php @@ -98,6 +98,7 @@ $string['parameter'] = 'Parameter'; $string['parameters'] = 'Parameters'; $string['popupresource'] = 'This resource should appear in a popup window.'; $string['popupresourcelink'] = 'If it didn\'t, click here: $a'; +$string['portfolionotimplemented'] = 'You are trying to export content from a resource type that doesn\'t support it!'; $string['preview'] = 'Preview'; $string['previous'] = 'Previous'; $string['redeploy'] = 'Deploy again'; diff --git a/mod/resource/lib.php b/mod/resource/lib.php index 82d5aad924..ecaf5801c1 100644 --- a/mod/resource/lib.php +++ b/mod/resource/lib.php @@ -249,6 +249,23 @@ class resource_base { //override to add your own options } + function portfolio_prepare_package_uploaded($tempdir) { + // @todo penny implement later - see MDL-15758 + + } + + function portfolio_prepare_package_online($tempdir, $text=false) { + //@todo penny use the files api here + $status = $handle = fopen($tempdir . '/' . clean_filename($this->cm->name . '.' . (($text) ? 'txt' : 'html')), 'w'); + $formatoptions = new object(); + $formatoptions->noclean = true; + $format = (($text) ? FORMAT_MOODLE : FORMAT_HTML); + $content = format_text($this->resource->alltext, $format, $formatoptions, $this->course->id); + $status = $status && fwrite($handle, $content); + $status = $status && fclose($handle); + return $status; + } + } /// end of class definition @@ -681,4 +698,58 @@ function resource_get_extra_capabilities() { return array('moodle/site:accessallgroups'); } +require_once($CFG->libdir . '/portfoliolib.php'); +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'])) { + print_error('invalidid'); + } + $this->cm->type = $DB->get_field('resource', 'type', array('id' => $this->cm->instance)); + $resourceclass = 'resource_'. $this->cm->type; + $this->resourcefile = $CFG->dirroot.'/mod/resource/type/'.$this->cm->type.'/resource.class.php'; + require_once($this->resourcefile); + $this->resource= new $resourceclass($this->cm->id); + } + + public function __wakeup() { + require_once($this->resourcefile); + $this->resource = unserialize(serialize($this->resource)); + } + + public function expected_time() { + // @todo penny check filesize if the type is uploadey + return PORTFOLIO_TIME_LOW; + } + + public function prepare_package($tempdir) { + if (!is_callable(array($this->resource, 'portfolio_prepare_package'))) { + portfolio_exporter::raise_error('portfolionotimplemented', 'resource'); + } + return $this->resource->portfolio_prepare_package($tempdir); + } + + public static function supported_formats() { + return array(PORTFOLIO_FORMAT_FILE); + } + + public function check_permissions() { + return true; + } + + public static function add_button($resource, $fullform=true, $return=false) { + // @todo penny can we put the capability check in here? + if (!is_callable(array($resource, 'portfolio_prepare_package'))) { + 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); + } +} + ?> diff --git a/mod/resource/type/html/resource.class.php b/mod/resource/type/html/resource.class.php index a89c582040..e1952364cf 100644 --- a/mod/resource/type/html/resource.class.php +++ b/mod/resource/type/html/resource.class.php @@ -65,6 +65,9 @@ function display() { parent::display_course_blocks_start(); echo format_text($this->resource->alltext, FORMAT_HTML, $formatoptions, $this->course->id); + if (true) { //@todo penny replace later with capability check + resource_portfolio_caller::add_button($this); + } parent::display_course_blocks_end(); @@ -82,6 +85,9 @@ function display() { print_header(); print_simple_box(format_text($resource->alltext, FORMAT_HTML, $formatoptions, $course->id), "center clearfix", "", "", "20"); + if (true) { //@todo penny replace later with capability check + resource_portfolio_caller::add_button($this); + } print_footer($course); } else { /// Make a page and a pop-up window $navigation = build_navigation($this->navlinks, $cm); @@ -118,6 +124,9 @@ function display() { navmenu($course, $cm)); print_simple_box(format_text($resource->alltext, FORMAT_HTML, $formatoptions, $course->id), "center clearfix", "", "", "20"); + if (true) { //@todo penny replace later with capability check + resource_portfolio_caller::add_button($this); + } $strlastmodified = get_string("lastmodified"); echo "
$strlastmodified: ".userdate($resource->timemodified)."
"; @@ -184,6 +193,10 @@ function setup_elements(&$mform) { } } +function portfolio_prepare_package($tempdir) { + return parent::portfolio_prepare_package_online($tempdir); +} + } diff --git a/mod/resource/type/text/resource.class.php b/mod/resource/type/text/resource.class.php index 0f2d47f493..064d664998 100644 --- a/mod/resource/type/text/resource.class.php +++ b/mod/resource/type/text/resource.class.php @@ -56,6 +56,9 @@ function display() { if (trim(strip_tags($this->resource->alltext))) { echo format_text($this->resource->alltext, FORMAT_MOODLE, $formatoptions, $this->course->id); + if (true) { //@todo penny replace later with capability check + resource_portfolio_caller::add_button($this); + } } parent::display_course_blocks_end(); @@ -81,6 +84,9 @@ function display() { print_simple_box(format_text($resource->alltext, $resource->reference, $formatoptions, $course->id), "center", "", "", "20"); print_footer($course); + if (true) { //@todo penny replace later with capability check + resource_portfolio_caller::add_button($this); + } } else { /// Make a page and a pop-up window $navigation = build_navigation($this->navlinks, $cm); @@ -120,6 +126,9 @@ function display() { print_simple_box(format_text($resource->alltext, $resource->reference, $formatoptions, $course->id), "center", "", "", "20"); + if (true) { //@todo penny replace later with capability check + resource_portfolio_caller::add_button($this); + } $strlastmodified = get_string("lastmodified"); echo "
$strlastmodified: ".userdate($resource->timemodified)."
"; @@ -190,6 +199,9 @@ function setup_elements(&$mform) { } +function portfolio_prepare_package($tempdir) { + return parent::portfolio_prepare_package_online($tempdir, true); +} } ?> -- 2.39.5