From b48c4478ce0b3dc2cbb390e88347637759bb920e Mon Sep 17 00:00:00 2001 From: jerome mouneyrac Date: Mon, 26 Jul 2010 06:58:55 +0000 Subject: [PATCH] community block MDL-23481 integrate restore operation --- blocks/community/block_community.php | 8 +- blocks/community/communitycourse.php | 55 ++++++---- blocks/community/forms.php | 3 + blocks/community/lang/en/block_community.php | 6 ++ blocks/community/locallib.php | 40 +++++--- blocks/community/renderer.php | 100 +++++++++++++------ lang/en/hub.php | 1 - 7 files changed, 145 insertions(+), 68 deletions(-) diff --git a/blocks/community/block_community.php b/blocks/community/block_community.php index 4ef486084f2..1af1fa41769 100644 --- a/blocks/community/block_community.php +++ b/blocks/community/block_community.php @@ -62,9 +62,11 @@ class block_community extends block_list { if (!isloggedin()) { return $this->content; } - + + $addcourseurl = new moodle_url('/blocks/community/communitycourse.php', + array('add' => true, 'courseid' => $this->page->course->id)); $searchlink = html_writer::tag('a', get_string('addcourse', 'block_community'), - array('href' => $CFG->wwwroot.'/blocks/community/communitycourse.php?add=true')); + array('href' => $addcourseurl->out(false))); $this->content->items[] = $searchlink; $icon = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('i/group'), 'class' => 'icon', 'alt' => get_string('addcourse', 'block_community'))); @@ -83,7 +85,7 @@ class block_community extends block_list { $deleteicon = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('i/cross_red_small'), 'alt' => get_string('removecommunitycourse', 'block_community'))); - $deleteurl = new moodle_url($CFG->wwwroot.'/blocks/community/communitycourse.php', + $deleteurl = new moodle_url('/blocks/community/communitycourse.php', array('remove'=>true, 'communityid'=> $course->id, 'sesskey' => sesskey())); $deleteatag = html_writer::tag('a', $deleteicon, array('href' => $deleteurl)); diff --git a/blocks/community/communitycourse.php b/blocks/community/communitycourse.php index 7ab000a7db8..654d870009c 100644 --- a/blocks/community/communitycourse.php +++ b/blocks/community/communitycourse.php @@ -34,7 +34,10 @@ require_once($CFG->dirroot . '/admin/registration/lib.php'); require_login(); -$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); +$courseid = optional_param('courseid', 0, PARAM_INT); +$context = get_context_instance(CONTEXT_COURSE, $courseid); + +$PAGE->set_context($context); $PAGE->set_url('/blocks/community/communitycourse.php'); $PAGE->set_heading($SITE->fullname); $PAGE->set_pagelayout('course'); @@ -45,15 +48,15 @@ $PAGE->navbar->add(get_string('searchcourse', 'block_community')); $search = optional_param('search', null, PARAM_TEXT); //if no capability to search course, display an error message -$usercansearch = has_capability('moodle/community:add', get_context_instance(CONTEXT_USER, $USER->id)); -$usercandownload = has_capability('moodle/community:download', get_context_instance(CONTEXT_USER, $USER->id)); +$usercansearch = has_capability('moodle/community:add', $context); +$usercandownload = has_capability('moodle/community:download', $context); if (empty($usercansearch)) { - $notificationerror = get_string('cannotsearchcommunity', 'hub'); + $notificationerror = get_string('cannotsearchcommunity', 'hub'); } else if (!extension_loaded('xmlrpc')) { $notificationerror = $OUTPUT->doc_link('admin/environment/php_extension/xmlrpc', ''); $notificationerror .= get_string('xmlrpcdisabledcommunity', 'hub'); } -if (!empty($notificationerror) ) { +if (!empty($notificationerror)) { echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main'); echo $OUTPUT->notification($notificationerror); @@ -62,6 +65,7 @@ if (!empty($notificationerror) ) { } $communitymanager = new block_community_manager(); +$renderer = $PAGE->get_renderer('block_community'); /// Check if the page has been called with trust argument $add = optional_param('add', -1, PARAM_INTEGER); @@ -77,20 +81,35 @@ if ($add != -1 and $confirm and confirm_sesskey()) { 'notifysuccess'); } +/// Delete temp file when cancel restore +$cancelrestore = optional_param('cancelrestore', false, PARAM_INT); +if ($usercandownload and $cancelrestore and confirm_sesskey()) { + $filename = optional_param('filename', '', PARAM_ALPHANUMEXT); + //delete temp file + unlink($CFG->dataroot . '/temp/backup/' . $filename . ".zip"); +} + /// Download $huburl = optional_param('huburl', false, PARAM_URL); $download = optional_param('download', -1, PARAM_INTEGER); -$courseid = optional_param('courseid', '', PARAM_INTEGER); +$downloadcourseid = optional_param('downloadcourseid', '', PARAM_INTEGER); $coursefullname = optional_param('coursefullname', '', PARAM_ALPHANUMEXT); -if ($usercandownload and $download != -1 and !empty($courseid) and confirm_sesskey()) { +if ($usercandownload and $download != -1 and !empty($downloadcourseid) and confirm_sesskey()) { $course = new stdClass(); $course->fullname = $coursefullname; - $course->id = $courseid; + $course->id = $downloadcourseid; $course->huburl = $huburl; - $communitymanager->block_community_download_course_backup($course); - $filename = 'backup_' . $course->fullname . "_" . $course->id . ".zip"; - $notificationmessage = $OUTPUT->notification(get_string('downloadconfirmed', 'hub', $filename), - 'notifysuccess'); + $filenames = $communitymanager->block_community_download_course_backup($course); + + //OUTPUT: display restore choice page + echo $OUTPUT->header(); + echo $OUTPUT->heading(get_string('restorecourse', 'block_community'), 3, 'main'); + echo $OUTPUT->notification(get_string('downloadconfirmed', 'block_community', + '/' . get_string('backupfoldername', 'block_community') + . '/' . $filenames['privatefile']), 'notifysuccess'); + echo $renderer->restore_confirmation_box($filenames['tmpfile'], $context); + echo $OUTPUT->footer(); + die(); } /// Remove community @@ -102,11 +121,9 @@ if ($remove != -1 and !empty($communityid) and confirm_sesskey()) { 'notifysuccess'); } - -$renderer = $PAGE->get_renderer('block_community'); - //forms -$hubselectorform = new community_hub_search_form('', array('search' => $search)); +$hubselectorform = new community_hub_search_form('', + array('search' => $search, 'courseid' => $courseid)); $fromform = $hubselectorform->get_data(); $courses = null; //Retrieve courses by web service @@ -152,8 +169,8 @@ if (!empty($fromform)) { try { $courses = $xmlrpcclient->call($function, $params); } catch (Exception $e) { - $hubs = array(); - $errormessage = $OUTPUT->notification(get_string('errorcourselisting', 'block_community', $e->getMessage())); + $errormessage = $OUTPUT->notification( + get_string('errorcourselisting', 'block_community', $e->getMessage())); } } @@ -167,5 +184,5 @@ $hubselectorform->display(); if (!empty($errormessage)) { echo $errormessage; } -echo highlight($search, $renderer->course_list($courses, $huburl)); +echo highlight($search, $renderer->course_list($courses, $huburl, $courseid)); echo $OUTPUT->footer(); \ No newline at end of file diff --git a/blocks/community/forms.php b/blocks/community/forms.php index 413afeea70c..d082e40c346 100644 --- a/blocks/community/forms.php +++ b/blocks/community/forms.php @@ -43,6 +43,9 @@ class community_hub_search_form extends moodleform { $search = $this->_customdata['search']; $mform->addElement('header', 'site', get_string('search', 'block_community')); + //add the course id (of the context) + $mform->addElement('hidden', 'courseid', $this->_customdata['courseid']); + //retrieve the hub list on the hub directory by web service $function = 'hubdirectory_get_hubs'; $params = array(); diff --git a/blocks/community/lang/en/block_community.php b/blocks/community/lang/en/block_community.php index fd03fc245c9..e56907ee28d 100644 --- a/blocks/community/lang/en/block_community.php +++ b/blocks/community/lang/en/block_community.php @@ -33,6 +33,7 @@ $string['additionalcoursedesc'] = '{$a->lang} Creator: {$a->creatorname} - Publi $string['addcourse'] = 'Search'; $string['audience'] = 'Designed for'; $string['audience_help'] = 'What kind of course are you looking for? As well as traditional courses intended for students, you might search for communities of Educators or Moodle Administrators'; +$string['backupfoldername'] = 'Downloaded backup'; $string['blocks'] = 'Blocks'; $string['cannotselecttopsubject'] = 'Cannot select a top subject level'; $string['contributors'] = ' - Contributors: {$a}'; @@ -41,8 +42,11 @@ $string['courselang'] = 'Language'; $string['coursename'] = 'Name'; $string['courses'] = 'Courses'; $string['coverage'] = 'Tags: {$a}'; +$string['donotrestore'] = 'No'; +$string['dorestore'] = 'Yes'; $string['download'] = 'Download'; $string['downloadable'] = 'courses I can download'; +$string['downloadconfirmed'] = 'The backup has been saved in your private files {$a}'; $string['downloadtemplate'] = 'Create course from template'; $string['educationallevel'] = 'Educational level'; $string['educationallevel_help'] = 'What educational level are you searching for? In the case of communities of educators, this level describes the level they are teaching.'; @@ -69,6 +73,8 @@ $string['nocourse'] = 'No courses found'; $string['operation'] = 'Operation'; $string['pluginname'] = 'Community'; $string['removecommunitycourse'] = 'Remove community course'; +$string['restorecourse'] = 'Restore course'; +$string['restorecourseinfo'] = 'Restore the course?'; $string['screenshots'] = 'Screenshots'; $string['search'] = 'Search'; $string['searchcommunitycourse'] = 'Search community course'; diff --git a/blocks/community/locallib.php b/blocks/community/locallib.php index 2f843d186f1..550c14f17d5 100644 --- a/blocks/community/locallib.php +++ b/blocks/community/locallib.php @@ -67,30 +67,36 @@ class block_community_manager { */ public function block_community_get_course($courseurl, $userid) { global $DB; - return $DB->get_record('block_community', array('courseurl' => $courseurl, 'userid' => $userid)); + return $DB->get_record('block_community', + array('courseurl' => $courseurl, 'userid' => $userid)); } /** * Download the community course backup and save it in file API * @param integer $courseid * @param string $huburl + * @return array 'privatefile' the file name saved in private area + * 'tmpfile' the file name saved in the moodledata temp dir (for restore) */ public function block_community_download_course_backup($course) { global $CFG, $USER; require_once($CFG->dirroot. "/lib/filelib.php"); require_once($CFG->dirroot. "/course/publish/lib.php"); - //$curl = new curl(); + $params['courseid'] = $course->id; $params['filetype'] = HUB_BACKUP_FILE_TYPE; - if (!file_exists($CFG->dataroot.'/temp/communitydownload')) { - mkdir($CFG->dataroot.'/temp/communitydownload/', 0777, true); + if (!file_exists($CFG->dataroot.'/temp/backup')) { + mkdir($CFG->dataroot.'/temp/backup/', 0777, true); } + $filename = md5(time() . '-' . $course->id . '-'. $USER->id . '-'. random_string(20)); + $url = new moodle_url($course->huburl.'/local/hub/webservice/download.php', $params); - $path = $CFG->dataroot.'/temp/communitydownload/'.'backup_'.$course->fullname."_".$course->id.".zip"; + $path = $CFG->dataroot.'/temp/backup/'.$filename.".zip"; $fp = fopen($path, 'w'); - $ch = curl_init($course->huburl.'/local/hub/webservice/download.php?filetype='.HUB_BACKUP_FILE_TYPE.'&courseid='.$course->id); + $ch = curl_init($course->huburl.'/local/hub/webservice/download.php?filetype=' + .HUB_BACKUP_FILE_TYPE.'&courseid='.$course->id); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); curl_close($ch); @@ -99,15 +105,20 @@ class block_community_manager { $fs = get_file_storage(); $record->contextid = get_context_instance(CONTEXT_USER, $USER->id)->id; $record->component = 'user'; - $record->filearea = 'backup'; + $record->filearea = 'private'; $record->itemid = 0; - $record->filename = 'backup_'.$course->fullname."_".$course->id.".zip"; - $record->filepath = '/'; - if (!$fs->file_exists($record->contextid, $record->component, $record->filearea, 0, $record->filepath, $record->filename)) { - $fs->create_file_from_pathname($record, $CFG->dataroot.'/temp/communitydownload/'.'backup_'.$course->fullname."_".$course->id.".zip"); + $record->filename = urlencode($course->fullname)."_".time().".zip"; + $record->filepath = '/'.get_string('backupfoldername', 'block_community').'/'; + if (!$fs->file_exists($record->contextid, $record->component, + $record->filearea, 0, $record->filepath, $record->filename)) { + $fs->create_file_from_pathname($record, + $CFG->dataroot.'/temp/backup/'.$filename.".zip"); } - //delete temp file - unlink($path); + + $filenames = array(); + $filenames['privatefile'] = $record->filename; + $filenames['tmpfile'] = $filename; + return $filenames; } /** @@ -118,7 +129,8 @@ class block_community_manager { */ public function block_community_remove_course($communityid, $userid) { global $DB, $USER; - return $DB->delete_records('block_community', array('userid' => $userid, 'id' => $communityid)); + return $DB->delete_records('block_community', + array('userid' => $userid, 'id' => $communityid)); } } \ No newline at end of file diff --git a/blocks/community/renderer.php b/blocks/community/renderer.php index e1bc28321ad..f2782ebbb7c 100644 --- a/blocks/community/renderer.php +++ b/blocks/community/renderer.php @@ -30,14 +30,30 @@ */ class block_community_renderer extends plugin_renderer_base { + public function restore_confirmation_box($filename, $context) { + $restoreurl = new moodle_url('/backup/restore.php', + array('filename' => $filename.".zip", 'contextid' => $context->id)); + $searchurl = new moodle_url('/blocks/community/communitycourse.php', + array('add' => 1, 'courseid' => $context->instanceid, + 'cancelrestore' => 1, 'sesskey' => sesskey(), + 'filename' => $filename)); + $formrestore = new single_button($restoreurl, + get_string('dorestore', 'block_community')); + $formsearch = new single_button($searchurl, + get_string('donotrestore', 'block_community')); + return $this->output->confirm(get_string('restorecourseinfo', 'block_community'), + $formrestore, $formsearch); + } + /** * Display a list of courses * @param array $courses * @param boolean $withwriteaccess + * @param int $contextcourseid context course id * @return string */ - public function course_list($courses, $huburl) { - global $OUTPUT, $CFG; + public function course_list($courses, $huburl, $contextcourseid) { + global $CFG; $renderedhtml = ''; @@ -77,7 +93,8 @@ class block_community_renderer extends plugin_renderer_base { } else { $courseurl = new moodle_url($course->demourl); } - $courseatag = html_writer::tag('a', $course->fullname, array('href' => $courseurl)); + $courseatag = html_writer::tag('a', $course->fullname, + array('href' => $courseurl)); $coursenamehtml = html_writer::tag('span', $courseatag, array()); @@ -86,13 +103,16 @@ class block_community_renderer extends plugin_renderer_base { $screenshothtml = ''; if (!empty($course->screenshots)) { $images = array(); - $baseurl = new moodle_url($huburl.'/local/hub/webservice/download.php', - array('courseid' => $course->id, 'filetype' => HUB_SCREENSHOT_FILE_TYPE)); + $baseurl = new moodle_url($huburl . '/local/hub/webservice/download.php', + array('courseid' => $course->id, + 'filetype' => HUB_SCREENSHOT_FILE_TYPE)); for ($i = 1; $i <= $course->screenshots; $i = $i + 1) { $params['screenshotnumber'] = $i; $images[] = array( - 'thumburl' => new moodle_url($baseurl, array('screenshotnumber' => $i)), - 'imageurl' => new moodle_url($baseurl, array('screenshotnumber' => $i, 'imagewidth' => 'original')), + 'thumburl' => new moodle_url($baseurl, + array('screenshotnumber' => $i)), + 'imageurl' => new moodle_url($baseurl, + array('screenshotnumber' => $i, 'imagewidth' => 'original')), 'title' => $course->fullname, 'alt' => $course->fullname ); @@ -101,28 +121,33 @@ class block_community_renderer extends plugin_renderer_base { $imagegallery->displayfirstimageonly = true; $screenshothtml = $this->output->render($imagegallery); } - $deschtml = html_writer::tag('div', $screenshothtml, array('class' => 'coursescreenshot')); + $deschtml = html_writer::tag('div', $screenshothtml, + array('class' => 'coursescreenshot')); //create description to display $course->subject = get_string($course->subject, 'edufields'); $course->audience = get_string('audience' . $course->audience, 'hub'); - $course->educationallevel = get_string('edulevel' . $course->educationallevel, 'hub'); + $course->educationallevel = get_string('edulevel' . + $course->educationallevel, 'hub'); $deschtml .= $course->description; //the description /// courses and sites number display under the description, in smaller $deschtml .= html_writer::empty_tag('br'); $additionaldesc = ''; - if ($course->contributornames) { - $additionaldesc .= get_string('contributors', 'block_community', $course->contributornames); + if ($course->contributornames) { + $additionaldesc .= get_string('contributors', + 'block_community', $course->contributornames); $additionaldesc .= ' - '; } if ($course->coverage) { - $additionaldesc .= get_string('coverage', 'block_community', $course->coverage); + $additionaldesc .= get_string('coverage', + 'block_community', $course->coverage); $additionaldesc .= ' - '; } //retrieve language string if (!empty($course->language)) { $languages = get_string_manager()->get_list_of_languages(); - $course->lang = get_string('langdesc', 'block_community', $languages[$course->language]); + $course->lang = get_string('langdesc', + 'block_community', $languages[$course->language]); } else { $course->lang = ''; } @@ -137,11 +162,13 @@ class block_community_renderer extends plugin_renderer_base { } } - $additionaldesc .= get_string('additionalcoursedesc', 'block_community', $course); + $additionaldesc .= get_string('additionalcoursedesc', + 'block_community', $course); - $deschtml .= html_writer::tag('span', $additionaldesc, array('class' => 'additionaldesc')); + $deschtml .= html_writer::tag('span', $additionaldesc, + array('class' => 'additionaldesc')); //add content to the course description if (!empty($course->contents)) { $activitieshtml = ''; @@ -151,45 +178,56 @@ class block_community_renderer extends plugin_renderer_base { if (!empty($blockhtml)) { $blockhtml .= ' - '; } - $blockhtml .= get_string('pluginname', 'block_'.$content['modulename']). " (".$content['contentcount'].")"; + $blockhtml .= get_string('pluginname', + 'block_' . $content['modulename']) + . " (" . $content['contentcount'] . ")"; } else { if (!empty($activitieshtml)) { $activitieshtml .= ' - '; } - $activitieshtml .= get_string('modulename', $content['modulename']). " (".$content['contentcount'].")"; + $activitieshtml .= get_string('modulename', + $content['modulename']) . " (" + . $content['contentcount'] . ")"; } } $blocksandactivities = html_writer::tag('span', - get_string('activities', 'block_community')." : ". $activitieshtml); - $blocksandactivities .= html_writer::empty_tag('br').html_writer::tag('span', - get_string('blocks', 'block_community')." : ". $blockhtml); + get_string('activities', 'block_community') + . " : " . $activitieshtml); + $blocksandactivities .= html_writer::empty_tag('br') . html_writer::tag('span', + get_string('blocks', 'block_community') + . " : " . $blockhtml); $deschtml .= print_collapsible_region($blocksandactivities, 'blockdescription', - 'blocksandactivities-'.$course->id.'-'.clean_param($courseurl, PARAM_ALPHANUMEXT), - get_string('moredetails','block_community'), '', false,true); + 'blocksandactivities-' . $course->id . '-' + . clean_param($courseurl, PARAM_ALPHANUMEXT), + get_string('moredetails', 'block_community'), '', false, true); } - + if ($course->enrollable) { $params = array('sesskey' => sesskey(), 'add' => 1, 'confirmed' => 1, 'coursefullname' => $course->fullname, 'courseurl' => $courseurl, - 'coursedescription' => $course->description); + 'coursedescription' => $course->description, + 'courseid' => $contextcourseid); $addurl = new moodle_url("/blocks/community/communitycourse.php", $params); - $addbutton = new single_button($addurl, get_string('addtocommunityblock', 'block_community')); + $addbutton = new single_button($addurl, + get_string('addtocommunityblock', 'block_community')); $addbutton->class = 'centeredbutton'; - $addbuttonhtml = $OUTPUT->render($addbutton); + $addbuttonhtml = $this->output->render($addbutton); } else { $params = array('sesskey' => sesskey(), 'download' => 1, 'confirmed' => 1, - 'remotemoodleurl' => $CFG->wwwroot, - 'courseid' => $course->id, 'huburl' => $huburl, 'coursefullname' => $course->fullname); + 'remotemoodleurl' => $CFG->wwwroot, 'courseid' => $contextcourseid, + 'downloadcourseid' => $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 = new single_button($addurl, + get_string('download', 'block_community')); $downloadbutton->class = 'centeredbutton'; - $addbuttonhtml = $OUTPUT->render($downloadbutton); + $addbuttonhtml = $this->output->render($downloadbutton); } - + $table->data[] = array($coursenamehtml, $deschtml, $addbuttonhtml); } $renderedhtml .= html_writer::table($table); diff --git a/lang/en/hub.php b/lang/en/hub.php index 30dee3e167e..a41a226bc3e 100644 --- a/lang/en/hub.php +++ b/lang/en/hub.php @@ -77,7 +77,6 @@ $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 course that does not exist anymore. Alert this hub administrator that the publication number {$a->id} should be removed.'; $string['downloadable'] = 'Downloadable'; -$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';