Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/blocks/community/locallib.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 /*
00018  * @package    blocks
00019  * @subpackage community
00020  * @author     Jerome Mouneyrac <jerome@mouneyrac.com>
00021  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
00022  * @copyright  (C) 1999 onwards Martin Dougiamas  http://dougiamas.com
00023  *
00024  * Community library
00025 */
00026 
00027 class block_community_manager {
00028 
00035     public function block_community_add_course($course, $userid) {
00036         global $DB;
00037 
00038         $community = $this->block_community_get_course($course->url, $userid);
00039 
00040         if (empty($community)) {
00041             $community->userid = $userid;
00042             $community->coursename = $course->name;
00043             $community->coursedescription = $course->description;
00044             $community->courseurl = $course->url;
00045             $community->imageurl = $course->imageurl;
00046             return $DB->insert_record('block_community', $community);
00047         } else {
00048             return false;
00049         }
00050     }
00051 
00057     public function block_community_get_courses($userid) {
00058         global $DB;
00059         return $DB->get_records('block_community', array('userid' => $userid), 'coursename');
00060     }
00061 
00068     public function block_community_get_course($courseurl, $userid) {
00069         global $DB;
00070         return $DB->get_record('block_community',
00071                 array('courseurl' => $courseurl, 'userid' => $userid));
00072     }
00073 
00081     public function block_community_download_course_backup($course) {
00082         global $CFG, $USER;
00083         require_once($CFG->libdir . "/filelib.php");
00084         require_once($CFG->dirroot. "/course/publish/lib.php");
00085              
00086         $params['courseid'] = $course->id;
00087         $params['filetype'] = HUB_BACKUP_FILE_TYPE;
00088 
00089         make_temp_directory('backup');
00090 
00091         $filename = md5(time() . '-' . $course->id . '-'. $USER->id . '-'. random_string(20));
00092 
00093         $url  = new moodle_url($course->huburl.'/local/hub/webservice/download.php', $params);
00094         $path = $CFG->tempdir.'/backup/'.$filename.".mbz";
00095         $fp = fopen($path, 'w');
00096         $curlurl = $course->huburl.'/local/hub/webservice/download.php?filetype='
00097                 .HUB_BACKUP_FILE_TYPE.'&courseid='.$course->id;
00098 
00099         //send an identification token if the site is registered on the hub
00100         require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
00101         $registrationmanager = new registration_manager();
00102         $registeredhub = $registrationmanager->get_registeredhub($course->huburl);
00103         if (!empty($registeredhub)) {
00104             $token = $registeredhub->token;
00105             $curlurl .= '&token='.$token;
00106         }
00107         
00108         $ch = curl_init($curlurl);
00109         curl_setopt($ch, CURLOPT_FILE, $fp);
00110         $data = curl_exec($ch);
00111         curl_close($ch);
00112         fclose($fp);
00113 
00114         $fs = get_file_storage();
00115         $record = new stdClass();
00116         $record->contextid = get_context_instance(CONTEXT_USER, $USER->id)->id;
00117         $record->component = 'user';
00118         $record->filearea = 'private';
00119         $record->itemid = 0;
00120         $record->filename = urlencode($course->fullname)."_".time().".mbz";
00121         $record->filepath = '/downloaded_backup/';
00122         if (!$fs->file_exists($record->contextid, $record->component,
00123                 $record->filearea, 0, $record->filepath, $record->filename)) {
00124             $fs->create_file_from_pathname($record,
00125                     $CFG->tempdir.'/backup/'.$filename.".mbz");
00126         }
00127 
00128         $filenames = array();
00129         $filenames['privatefile'] = $record->filename;
00130         $filenames['tmpfile'] = $filename;
00131         return $filenames;
00132     }
00133 
00140     public function block_community_remove_course($communityid, $userid) {
00141         global $DB, $USER;
00142         return $DB->delete_records('block_community',
00143                 array('userid' => $userid, 'id' => $communityid));
00144     }
00145 
00146 }
 All Data Structures Namespaces Files Functions Variables Enumerations