From 78f00eab268b294fbe878eff607bfc5ca7c356b9 Mon Sep 17 00:00:00 2001 From: jerome mouneyrac Date: Wed, 26 May 2010 05:06:02 +0000 Subject: [PATCH] community block MDL-19314 improve the download form (check that if the file already exist in the file aera, change link for button, delete temp downloaded file, change name of the backup file including course name) --- blocks/community/communitycourse.php | 10 ++++++-- blocks/community/lang/en/block_community.php | 1 + blocks/community/locallib.php | 26 ++++++++++++++------ blocks/community/renderer.php | 26 +++++++++++--------- lang/en/hub.php | 2 +- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/blocks/community/communitycourse.php b/blocks/community/communitycourse.php index d0e18b51699..cf84595d302 100644 --- a/blocks/community/communitycourse.php +++ b/blocks/community/communitycourse.php @@ -62,9 +62,15 @@ if ($add != -1 and $confirm and confirm_sesskey()) { $huburl = optional_param('huburl', false, PARAM_URL); $download = optional_param('download', -1, PARAM_INTEGER); $courseid = optional_param('courseid', '', PARAM_INTEGER); +$coursefullname = optional_param('coursefullname', '', PARAM_ALPHANUMEXT); if ($download != -1 and !empty($courseid) and confirm_sesskey()) { - $community->download_community_course_backup($courseid, $huburl); - $notificationmessage = $OUTPUT->notification(get_string('downloadconfirmed', 'hub', 'backup_'.$courseid.".zip"), + $course = new stdClass(); + $course->fullname = $coursefullname; + $course->id = $courseid; + $course->huburl = $huburl; + $community->download_community_course_backup($course); + $filename = 'backup_'.$course->fullname."_".$course->id.".zip"; + $notificationmessage = $OUTPUT->notification(get_string('downloadconfirmed', 'hub', $filename), 'notifysuccess'); } diff --git a/blocks/community/lang/en/block_community.php b/blocks/community/lang/en/block_community.php index cd0d5dadd09..7a822eede6b 100644 --- a/blocks/community/lang/en/block_community.php +++ b/blocks/community/lang/en/block_community.php @@ -24,6 +24,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['add'] = 'Add'; $string['addtocommunityblock'] = 'Add to community block'; $string['addcommunitycourse'] = 'Add community course'; $string['additionalcoursedesc'] = '{$a->contributornames} - {$a->coverage} - Creator: {$a->creatorname} - Publisher: {$a->publishername} - Subject: {$a->subject} diff --git a/blocks/community/locallib.php b/blocks/community/locallib.php index af12b262f5e..58698e4d593 100644 --- a/blocks/community/locallib.php +++ b/blocks/community/locallib.php @@ -79,30 +79,40 @@ class community { * @param integer $courseid * @param string $huburl */ - public function download_community_course_backup($courseid, $huburl) { + public function download_community_course_backup($course) { global $CFG, $USER; require_once($CFG->dirroot. "/lib/filelib.php"); require_once($CFG->dirroot. "/lib/hublib.php"); //$curl = new curl(); - $params['courseid'] = $courseid; + $params['courseid'] = $course->id; $params['filetype'] = BACKUP_FILE_TYPE; - $url = new moodle_url($huburl.'/local/hub/webservice/download.php', $params); - $path = $CFG->dataroot.'/temp/download/'.'backup_'.$courseid.".zip"; + if (!file_exists($CFG->dataroot.'/temp/communitydownload')) { + mkdir($CFG->dataroot.'/temp/communitydownload/', 0777, true); + } + + //get hub id to make the + + $url = new moodle_url($course->huburl.'/local/hub/webservice/download.php', $params); + $path = $CFG->dataroot.'/temp/communitydownload/'.'backup_'.$course->fullname."_".$course->id.".zip"; $fp = fopen($path, 'w'); - $ch = curl_init($huburl.'/local/hub/webservice/download.php?filetype='.BACKUP_FILE_TYPE.'&courseid='.$courseid); + $ch = curl_init($course->huburl.'/local/hub/webservice/download.php?filetype='.BACKUP_FILE_TYPE.'&courseid='.$course->id); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); curl_close($ch); fclose($fp); + $fs = get_file_storage(); $record->contextid = get_context_instance(CONTEXT_USER, $USER->id)->id; $record->filearea = 'user_backup'; $record->itemid = 0; - $record->filename = 'backup_'.$courseid.".zip"; + $record->filename = 'backup_'.$course->fullname."_".$course->id.".zip"; $record->filepath = '/'; - $fs = get_file_storage(); - $fs->create_file_from_pathname($record, $CFG->dataroot.'/temp/download/'.'backup_'.$courseid.".zip"); + if (!$fs->file_exists($record->contextid, $record->filearea, 0, $record->filepath, $record->filename)) { + $fs->create_file_from_pathname($record, $CFG->dataroot.'/temp/communitydownload/'.'backup_'.$course->fullname."_".$course->id.".zip"); + } + //delete temp file + unlink($path); } /** diff --git a/blocks/community/renderer.php b/blocks/community/renderer.php index af8ad08c490..bb5e76778c0 100644 --- a/blocks/community/renderer.php +++ b/blocks/community/renderer.php @@ -105,23 +105,25 @@ class block_community_renderer extends plugin_renderer_base { $language= ''; } - if ($course->enrollable) { - //Add link TODO make it a button and send by post - $addurl = new moodle_url("/blocks/community/communitycourse.php", - array('sesskey' => sesskey(), 'add' => 1, 'confirmed' => 1, + if ($course->enrollable) { + $params = array('sesskey' => sesskey(), 'add' => 1, 'confirmed' => 1, 'coursefullname' => $course->fullname, 'courseurl' => $courseurl, - 'coursedescription' => $course->description)); - $addlinkhtml = html_writer::tag('a', get_string('addtocommunityblock', 'block_community'), array('href' => $addurl)); + 'coursedescription' => $course->description); + $addurl = new moodle_url("/blocks/community/communitycourse.php", $params); + $addbutton = new single_button($addurl, get_string('addtocommunityblock', 'block_community')); + $addbutton->class = 'centeredbutton'; + $addbuttonhtml = $OUTPUT->render($addbutton); } else { -// Add link TODO make it a button and send by post - $addurl = new moodle_url("/blocks/community/communitycourse.php", - array('sesskey' => sesskey(), 'download' => 1, 'confirmed' => 1, - 'courseid' => $course->id, 'huburl' => $huburl)); - $addlinkhtml = html_writer::tag('a', get_string('download', 'block_community'), array('href' => $addurl)); + $params = array('sesskey' => sesskey(), 'download' => 1, 'confirmed' => 1, + 'courseid' => $course->id, 'huburl' => $huburl, 'coursefullname' => $course->fullname); + $addurl = new moodle_url("/blocks/community/communitycourse.php", $params); + $downloadbutton = new single_button($addurl, get_string('download', 'block_community')); + $downloadbutton->class = 'centeredbutton'; + $addbuttonhtml = $OUTPUT->render($downloadbutton); } // add a row to the table - $cells = array($coursenamehtml, $deschtml, $language, $addlinkhtml); + $cells = array($coursenamehtml, $deschtml, $language, $addbuttonhtml); $row = new html_table_row($cells); diff --git a/lang/en/hub.php b/lang/en/hub.php index 4a7af37304d..f83195c218c 100644 --- a/lang/en/hub.php +++ b/lang/en/hub.php @@ -74,7 +74,7 @@ $string['description'] = 'Description'; $string['description_help'] = 'This description text will be showing in the course listing on the hub.'; $string['detectednotexistingpublication'] = '{$a->hubname} is listing a publication to this course which seems to not exist anymore. Alert this hub administrator that the publication number {$a->id} should not be listed anymore.'; $string['downloadable'] = 'Downloadable'; -$string['downloadconfirmed'] = 'The backup has been saved into your personal area'; +$string['downloadconfirmed'] = 'The backup has been saved into your backup area under the name {$a}'; $string['educationallevel'] = 'Educational level'; $string['educationallevel_help'] = ''; $string['edulevelassociation'] = 'Association';