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/>.
20 * @package block_community
21 * @author Jerome Mouneyrac <jerome@mouneyrac.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
23 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
28 class block_community_manager
{
31 * Add a community course
32 * @param object $course
33 * @param integer $userid
34 * @return id of course or false if already added
36 public function block_community_add_course($course, $userid) {
39 $community = $this->block_community_get_course($course->url
, $userid);
41 if (empty($community)) {
42 $community = new stdClass();
43 $community->userid
= $userid;
44 $community->coursename
= $course->name
;
45 $community->coursedescription
= $course->description
;
46 $community->courseurl
= $course->url
;
47 $community->imageurl
= $course->imageurl
;
48 return $DB->insert_record('block_community', $community);
55 * Return all community courses of a user
56 * @param integer $userid
57 * @return array of course
59 public function block_community_get_courses($userid) {
61 return $DB->get_records('block_community', array('userid' => $userid), 'coursename');
65 * Return a community courses of a user
66 * @param integer $userid
67 * @param integer $userid
68 * @return array of course
70 public function block_community_get_course($courseurl, $userid) {
72 return $DB->get_record('block_community',
73 array('courseurl' => $courseurl, 'userid' => $userid));
77 * Download the community course backup and save it in file API
78 * @param integer $courseid
79 * @param string $huburl
80 * @return array 'privatefile' the file name saved in private area
81 * 'tmpfile' the file name saved in the moodledata temp dir (for restore)
83 public function block_community_download_course_backup($course) {
85 require_once($CFG->libdir
. "/filelib.php");
86 require_once($CFG->dirroot
. "/course/publish/lib.php");
88 $params['courseid'] = $course->id
;
89 $params['filetype'] = HUB_BACKUP_FILE_TYPE
;
91 make_temp_directory('backup');
93 $filename = md5(time() . '-' . $course->id
. '-'. $USER->id
. '-'. random_string(20));
95 $url = new moodle_url($course->huburl
.'/local/hub/webservice/download.php', $params);
96 $path = $CFG->tempdir
.'/backup/'.$filename.".mbz";
97 $fp = fopen($path, 'w');
98 $curlurl = $course->huburl
.'/local/hub/webservice/download.php?filetype='
99 .HUB_BACKUP_FILE_TYPE
.'&courseid='.$course->id
;
101 //send an identification token if the site is registered on the hub
102 require_once($CFG->dirroot
. '/' . $CFG->admin
. '/registration/lib.php');
103 $registrationmanager = new registration_manager();
104 $registeredhub = $registrationmanager->get_registeredhub($course->huburl
);
105 if (!empty($registeredhub)) {
106 $token = $registeredhub->token
;
107 $curlurl .= '&token='.$token;
110 $ch = curl_init($curlurl);
111 curl_setopt($ch, CURLOPT_FILE
, $fp);
112 $data = curl_exec($ch);
116 $fs = get_file_storage();
117 $record = new stdClass();
118 $record->contextid
= context_user
::instance($USER->id
)->id
;
119 $record->component
= 'user';
120 $record->filearea
= 'private';
122 $record->filename
= urlencode($course->fullname
)."_".time().".mbz";
123 $record->filepath
= '/downloaded_backup/';
124 if (!$fs->file_exists($record->contextid
, $record->component
,
125 $record->filearea
, 0, $record->filepath
, $record->filename
)) {
126 $fs->create_file_from_pathname($record,
127 $CFG->tempdir
.'/backup/'.$filename.".mbz");
130 $filenames = array();
131 $filenames['privatefile'] = $record->filename
;
132 $filenames['tmpfile'] = $filename;
137 * Delete a community course
138 * @param integer $communityid
139 * @param integer $userid
142 public function block_community_remove_course($communityid, $userid) {
144 return $DB->delete_records('block_community',
145 array('userid' => $userid, 'id' => $communityid));