From a8f201f88c6d35b51a9ddbea6e29fca4fdef867f Mon Sep 17 00:00:00 2001 From: jerome mouneyrac Date: Mon, 3 May 2010 11:37:00 +0000 Subject: [PATCH] course publication MDL-19315 work version of course sharing --- blocks/community/communitycourse.php | 5 ++- blocks/community/locallib.php | 50 +++++++++++++++++++++++++--- blocks/community/renderer.php | 12 +++---- course/publish/forms.php | 14 ++++---- course/publish/publish.php | 18 +++++++--- lang/en/hub.php | 1 + 6 files changed, 76 insertions(+), 24 deletions(-) diff --git a/blocks/community/communitycourse.php b/blocks/community/communitycourse.php index 51caa553fbf..dc4194a71e4 100644 --- a/blocks/community/communitycourse.php +++ b/blocks/community/communitycourse.php @@ -60,7 +60,9 @@ $huburl = optional_param('huburl', false, PARAM_URL); $download = optional_param('download', -1, PARAM_INTEGER); $courseid = optional_param('courseid', '', PARAM_INTEGER); if ($download != -1 and !empty($courseid) and confirm_sesskey()) { - $community->get_community_course_backup($courseid, $huburl); + $community->download_community_course_backup($courseid, $huburl); + $downloadmessage = $OUTPUT->notification(get_string('downloadconfirmed', 'hub', 'backup_'.$courseid.".zip"), + 'notifysuccess'); } $renderer = $PAGE->get_renderer('block_community'); @@ -84,6 +86,7 @@ if (!empty($fromform)) { // OUTPUT echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('addcommunitycourse', 'block_community'), 3, 'main'); +echo $downloadmessage; $hubselectorform->display(); echo $renderer->course_list($courses, $huburl); echo $OUTPUT->footer(); \ No newline at end of file diff --git a/blocks/community/locallib.php b/blocks/community/locallib.php index 43d22b9add2..2ad0ce5d966 100644 --- a/blocks/community/locallib.php +++ b/blocks/community/locallib.php @@ -56,18 +56,58 @@ class community { } /** - * + * Download the community course backup and save it in file API * @param $courseid * @param $huburl */ - public function get_community_course_backup($courseid, $huburl) { - global $CFG; + public function download_community_course_backup($courseid, $huburl) { + global $CFG, $USER; require_once($CFG->dirroot. "/lib/filelib.php"); require_once($CFG->dirroot. "/lib/hublib.php"); - $curl = new curl(); + //$curl = new curl(); $params['courseid'] = $courseid; $params['filetype'] = BACKUP_FILE_TYPE; - $filecontent = $curl->get($huburl.'/local/hub/webservice/download.php', $params); + + $url = new moodle_url($huburl.'/local/hub/webservice/download.php', $params); + varlog($url); + $path = $CFG->dataroot.'/temp/download/'.'backup_'.$courseid.".zip"; + $fp = fopen($path, 'w'); + $ch = curl_init($huburl.'/local/hub/webservice/download.php?filetype='.BACKUP_FILE_TYPE.'&courseid='.$courseid); + curl_setopt($ch, CURLOPT_FILE, $fp); + $data = curl_exec($ch); + curl_close($ch); + fclose($fp); + + $record->contextid = get_context_instance(CONTEXT_USER, $USER->id)->id; + $record->filearea = 'community_backups'; + $record->itemid = $courseid; + $record->filename = 'backup_'.$courseid.".zip"; + $record->filepath = '/'; + $fs = get_file_storage(); + $fs->create_file_from_pathname($record, $CFG->dataroot.'/temp/download/'.'backup_'.$courseid.".zip"); + } + + + /** + * Decide where to save the file, can be + * reused by sub class + * @param string filename + */ + public function prepare_file($filename) { + global $CFG; + if (!file_exists($CFG->dataroot.'/temp/download')) { + mkdir($CFG->dataroot.'/temp/download/', 0777, true); + } + if (is_dir($CFG->dataroot.'/temp/download')) { + $dir = $CFG->dataroot.'/temp/download/'; + } + if (empty($filename)) { + $filename = uniqid('repo').'_'.time().'.tmp'; + } + if (file_exists($dir.$filename)) { + $filename = uniqid('m').$filename; + } + return $dir.$filename; } } \ No newline at end of file diff --git a/blocks/community/renderer.php b/blocks/community/renderer.php index a54d1b45ddc..5bb1f7575b2 100644 --- a/blocks/community/renderer.php +++ b/blocks/community/renderer.php @@ -102,13 +102,13 @@ class block_community_renderer extends plugin_renderer_base { 'coursedescription' => $course->description)); $addlinkhtml = html_writer::tag('a', get_string('add'), array('href' => $addurl)); } 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)); +// 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)); - $addlinkhtml = "Download not implemented yet"; +// $addlinkhtml = "Download not implemented yet"; } // add a row to the table diff --git a/course/publish/forms.php b/course/publish/forms.php index 1c264528a84..9e3629977cb 100644 --- a/course/publish/forms.php +++ b/course/publish/forms.php @@ -216,13 +216,13 @@ class course_publication_form extends moodleform { - $mform->addElement('filemanager', 'screenshots', get_string('screenshots','hub'), null, - array('subdirs'=>0, - 'maxbytes'=>1000000, - 'maxfiles'=>3 - )); - $mform->setHelpButton('screenshots', array('screenshots', get_string('screenshots', 'hub'), 'hub')); - $mform->addHelpButton('screenshots', 'screenshots', 'hub'); +// $mform->addElement('filemanager', 'screenshots', get_string('screenshots','hub'), null, +// array('subdirs'=>0, +// 'maxbytes'=>1000000, +// 'maxfiles'=>3 +// )); +// $mform->setHelpButton('screenshots', array('screenshots', get_string('screenshots', 'hub'), 'hub')); +// $mform->addHelpButton('screenshots', 'screenshots', 'hub'); $this->add_action_buttons(false, $buttonlabel); } diff --git a/course/publish/publish.php b/course/publish/publish.php index 897c07910d8..062d23a389e 100644 --- a/course/publish/publish.php +++ b/course/publish/publish.php @@ -128,7 +128,12 @@ if (has_capability('moodle/course:publish', get_context_instance(CONTEXT_COURSE, $serverurl = $huburl."/local/hub/webservice/webservices.php"; require_once($CFG->dirroot."/webservice/xmlrpc/lib.php"); $xmlrpcclient = new webservice_xmlrpc_client(); - $result = $xmlrpcclient->call($serverurl, $registeredhub->token, $function, $params); + $courseids = $xmlrpcclient->call($serverurl, $registeredhub->token, $function, $params); + varlog('The course id after retrieveing from ws:'); + varlog($courseids); + if (count($courseids) != 1) { + throw new moodle_exception('coursewronglypublished'); + } $courseregisteredmsg = $OUTPUT->notification(get_string('coursepublished', 'hub'), 'notifysuccess'); @@ -151,11 +156,14 @@ if (has_capability('moodle/course:publish', get_context_instance(CONTEXT_COURSE, // send backup if ($share) { - $params['filetype'] = BACKUP_FILE_TYPE; - $params['file'] = $backupfile; - $params['filename'] = $backupfile->get_filename(); - $curl->post($huburl."/local/hub/webservice/upload.php", $params); + foreach ($courseids as $courseid) { + $params['filetype'] = BACKUP_FILE_TYPE; + $params['courseid'] = $courseid; + $params['file'] = $backupfile; + $curl->post($huburl."/local/hub/webservice/upload.php", $params); + } } + //TODO: Delete the backup from user_tohub diff --git a/lang/en/hub.php b/lang/en/hub.php index d0b21cf901a..7b1cfe45e97 100644 --- a/lang/en/hub.php +++ b/lang/en/hub.php @@ -65,6 +65,7 @@ $string['demourl'] = 'Demo URL'; $string['demourl_help'] = 'Enter the demo URL of your course. By default it is the URL of your course. The demo url is displayed as a link in a search result.'; $string['description'] = 'Description'; $string['downloadable'] = 'Downloadable'; +$string['downloadconfirmed'] = 'The backup has been saved into your personal area'; $string['educationallevel'] = 'Educational level'; $string['educationallevel_help'] = ''; $string['edulevelassociation'] = 'Association';