2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * @subpackage community
20 * @author Jerome Mouneyrac <jerome@mouneyrac.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
22 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
27 class block_community_manager
{
30 * Add a community course
31 * @param object $course
32 * @param integer $userid
33 * @return id of course or false if already added
35 public function block_community_add_course($course, $userid) {
38 $community = $this->block_community_get_course($course->url
, $userid);
40 if (empty($community)) {
41 $community->userid
= $userid;
42 $community->coursename
= $course->name
;
43 $community->coursedescription
= $course->description
;
44 $community->courseurl
= $course->url
;
45 $community->imageurl
= $course->imageurl
;
46 return $DB->insert_record('block_community', $community);
53 * Return all community courses of a user
54 * @param integer $userid
55 * @return array of course
57 public function block_community_get_courses($userid) {
59 return $DB->get_records('block_community', array('userid' => $userid), 'coursename');
63 * Return a community courses of a user
64 * @param integer $userid
65 * @param integer $userid
66 * @return array of course
68 public function block_community_get_course($courseurl, $userid) {
70 return $DB->get_record('block_community',
71 array('courseurl' => $courseurl, 'userid' => $userid));
75 * Download the community course backup and save it in file API
76 * @param integer $courseid
77 * @param string $huburl
78 * @return array 'privatefile' the file name saved in private area
79 * 'tmpfile' the file name saved in the moodledata temp dir (for restore)
81 public function block_community_download_course_backup($course) {
83 require_once($CFG->libdir
. "/filelib.php");
84 require_once($CFG->dirroot
. "/course/publish/lib.php");
86 $params['courseid'] = $course->id
;
87 $params['filetype'] = HUB_BACKUP_FILE_TYPE
;
89 make_upload_directory('temp/backup');
91 $filename = md5(time() . '-' . $course->id
. '-'. $USER->id
. '-'. random_string(20));
93 $url = new moodle_url($course->huburl
.'/local/hub/webservice/download.php', $params);
94 $path = $CFG->dataroot
.'/temp/backup/'.$filename.".mbz";
95 $fp = fopen($path, 'w');
96 $curlurl = $course->huburl
.'/local/hub/webservice/download.php?filetype='
97 .HUB_BACKUP_FILE_TYPE
.'&courseid='.$course->id
;
99 //send an identification token if the site is registered on the hub
100 require_once($CFG->dirroot
. '/' . $CFG->admin
. '/registration/lib.php');
101 $registrationmanager = new registration_manager();
102 $registeredhub = $registrationmanager->get_registeredhub($course->huburl
);
103 if (!empty($registeredhub)) {
104 $token = $registeredhub->token
;
105 $curlurl .= '&token='.$token;
108 $ch = curl_init($curlurl);
109 curl_setopt($ch, CURLOPT_FILE
, $fp);
110 $data = curl_exec($ch);
114 $fs = get_file_storage();
115 $record = new stdClass();
116 $record->contextid
= get_context_instance(CONTEXT_USER
, $USER->id
)->id
;
117 $record->component
= 'user';
118 $record->filearea
= 'private';
120 $record->filename
= urlencode($course->fullname
)."_".time().".mbz";
121 $record->filepath
= '/downloaded_backup/';
122 if (!$fs->file_exists($record->contextid
, $record->component
,
123 $record->filearea
, 0, $record->filepath
, $record->filename
)) {
124 $fs->create_file_from_pathname($record,
125 $CFG->dataroot
.'/temp/backup/'.$filename.".mbz");
128 $filenames = array();
129 $filenames['privatefile'] = $record->filename
;
130 $filenames['tmpfile'] = $filename;
135 * Delete a community course
136 * @param integer $communityid
137 * @param integer $userid
140 public function block_community_remove_course($communityid, $userid) {
142 return $DB->delete_records('block_community',
143 array('userid' => $userid, 'id' => $communityid));