3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * @subpackage 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
25 * This page display the community course search form.
26 * It also handles adding a course to the community block.
27 * It also handles downloading a course template.
30 require('../../config.php');
31 require_once($CFG->dirroot . '/blocks/community/locallib.php');
32 require_once($CFG->dirroot . '/blocks/community/forms.php');
33 require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
37 $courseid = optional_param('courseid', $SITE->id, PARAM_INT); //if no courseid is given
38 $context = get_context_instance(CONTEXT_COURSE, $courseid);
40 $PAGE->set_context($context);
41 $PAGE->set_url('/blocks/community/communitycourse.php');
42 $PAGE->set_heading($SITE->fullname);
43 $PAGE->set_pagelayout('course');
44 $PAGE->set_title(get_string('searchcourse', 'block_community'));
45 $PAGE->navbar->ignore_active(true);
46 $PAGE->navbar->add(get_string('searchcourse', 'block_community'));
48 $search = optional_param('search', null, PARAM_TEXT);
50 //if no capability to search course, display an error message
51 $usercansearch = has_capability('moodle/community:add', $context);
52 $usercandownload = has_capability('moodle/community:download', $context);
53 if (empty($usercansearch)) {
54 $notificationerror = get_string('cannotsearchcommunity', 'hub');
55 } else if (!extension_loaded('xmlrpc')) {
56 $notificationerror = $OUTPUT->doc_link('admin/environment/php_extension/xmlrpc', '');
57 $notificationerror .= get_string('xmlrpcdisabledcommunity', 'hub');
59 if (!empty($notificationerror)) {
60 echo $OUTPUT->header();
61 echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
62 echo $OUTPUT->notification($notificationerror);
63 echo $OUTPUT->footer();
67 $communitymanager = new block_community_manager();
68 $renderer = $PAGE->get_renderer('block_community');
70 /// Check if the page has been called with trust argument
71 $add = optional_param('add', -1, PARAM_INTEGER);
72 $confirm = optional_param('confirmed', false, PARAM_INTEGER);
73 if ($add != -1 and $confirm and confirm_sesskey()) {
74 $course = new stdClass();
75 $course->name = optional_param('coursefullname', '', PARAM_TEXT);
76 $course->description = optional_param('coursedescription', '', PARAM_TEXT);
77 $course->url = optional_param('courseurl', '', PARAM_URL);
78 $course->imageurl = optional_param('courseimageurl', '', PARAM_URL);
79 $communitymanager->block_community_add_course($course, $USER->id);
80 $notificationmessage = $OUTPUT->notification(get_string('addedtoblock', 'hub'),
84 /// Delete temp file when cancel restore
85 $cancelrestore = optional_param('cancelrestore', false, PARAM_INT);
86 if ($usercandownload and $cancelrestore and confirm_sesskey()) {
87 $filename = optional_param('filename', '', PARAM_ALPHANUMEXT);
89 unlink($CFG->dataroot . '/temp/backup/' . $filename . ".mbz");
93 $huburl = optional_param('huburl', false, PARAM_URL);
94 $download = optional_param('download', -1, PARAM_INTEGER);
95 $downloadcourseid = optional_param('downloadcourseid', '', PARAM_INTEGER);
96 $coursefullname = optional_param('coursefullname', '', PARAM_ALPHANUMEXT);
97 $backupsize = optional_param('backupsize', 0, PARAM_INT);
98 if ($usercandownload and $download != -1 and !empty($downloadcourseid) and confirm_sesskey()) {
99 $course = new stdClass();
100 $course->fullname = $coursefullname;
101 $course->id = $downloadcourseid;
102 $course->huburl = $huburl;
104 //OUTPUT: display restore choice page
105 echo $OUTPUT->header();
106 echo $OUTPUT->heading(get_string('downloadingcourse', 'block_community'), 3, 'main');
107 echo html_writer::tag('div', get_string('downloading', 'block_community'),
108 array('class' => 'textinfo'));
109 $sizeinfo = new stdClass();
110 $sizeinfo->total = $backupsize / 1000000;
111 $sizeinfo->modem = (int) ($backupsize / 5000);
112 $sizeinfo->dsl = (int) $sizeinfo->total;
113 echo html_writer::tag('div', get_string('downloadingsize', 'block_community', $sizeinfo),
114 array('class' => 'textinfo'));
116 echo html_writer::tag('div', get_string('downloaded', 'block_community'),
117 array('class' => 'textinfo'));
118 $filenames = $communitymanager->block_community_download_course_backup($course);
119 echo $OUTPUT->notification(get_string('downloadconfirmed', 'block_community',
120 '/downloaded_backup/' . $filenames['privatefile']), 'notifysuccess');
121 echo $renderer->restore_confirmation_box($filenames['tmpfile'], $context);
122 echo $OUTPUT->footer();
127 $remove = optional_param('remove', '', PARAM_INTEGER);
128 $communityid = optional_param('communityid', '', PARAM_INTEGER);
129 if ($remove != -1 and !empty($communityid) and confirm_sesskey()) {
130 $communitymanager->block_community_remove_course($communityid, $USER->id);
131 $notificationmessage = $OUTPUT->notification(get_string('communityremoved', 'hub'),
135 //Get form default/current values
136 $fromformdata['coverage'] = optional_param('coverage', 'all', PARAM_TEXT);
137 $fromformdata['licence'] = optional_param('licence', 'all', PARAM_ALPHANUMEXT);
138 $fromformdata['subject'] = optional_param('subject', 'all', PARAM_ALPHANUMEXT);
139 $fromformdata['audience'] = optional_param('audience', 'all', PARAM_ALPHANUMEXT);
140 $fromformdata['language'] = optional_param('language', 'all', PARAM_ALPHANUMEXT);
141 $fromformdata['educationallevel'] = optional_param('educationallevel', 'all', PARAM_ALPHANUMEXT);
142 $fromformdata['downloadable'] = optional_param('downloadable', 0, PARAM_ALPHANUM);
143 $fromformdata['orderby'] = optional_param('orderby', 'newest', PARAM_ALPHA);
144 $fromformdata['huburl'] = optional_param('huburl', HUB_MOODLEORGHUBURL, PARAM_URL);
145 $fromformdata['search'] = $search;
146 $fromformdata['courseid'] = $courseid;
147 $hubselectorform = new community_hub_search_form('', $fromformdata);
148 $hubselectorform->set_data($fromformdata);
150 //Retrieve courses by web service
152 if (optional_param('executesearch', 0, PARAM_INTEGER) and confirm_sesskey()) {
153 $downloadable = optional_param('downloadable', false, PARAM_INTEGER);
155 $options = new stdClass();
156 if (!empty($fromformdata['coverage'])) {
157 $options->coverage = $fromformdata['coverage'];
159 if ($fromformdata['licence'] != 'all') {
160 $options->licenceshortname = $fromformdata['licence'];
162 if ($fromformdata['subject'] != 'all') {
163 $options->subject = $fromformdata['subject'];
165 if ($fromformdata['audience'] != 'all') {
166 $options->audience = $fromformdata['audience'];
168 if ($fromformdata['educationallevel'] != 'all') {
169 $options->educationallevel = $fromformdata['educationallevel'];
171 if ($fromformdata['language'] != 'all') {
172 $options->language = $fromformdata['language'];
175 $options->orderby = $fromformdata['orderby'];
177 //the range of course requested
178 $options->givememore = optional_param('givememore', 0, PARAM_INTEGER);
180 //check if the selected hub is from the registered list (in this case we use the private token)
181 $token = 'publichub';
182 $registrationmanager = new registration_manager();
183 $registeredhubs = $registrationmanager->get_registered_on_hubs();
184 foreach ($registeredhubs as $registeredhub) {
185 if ($huburl == $registeredhub->huburl) {
186 $token = $registeredhub->token;
190 $function = 'hub_get_courses';
191 $params = array('search' => $search, 'downloadable' => $downloadable,
192 'enrollable' => !$downloadable, 'options' => $options);
193 $serverurl = $huburl . "/local/hub/webservice/webservices.php";
194 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
195 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $token);
197 $result = $xmlrpcclient->call($function, $params);
198 $courses = $result['courses'];
199 $coursetotal = $result['coursetotal'];
200 } catch (Exception $e) {
201 $errormessage = $OUTPUT->notification(
202 get_string('errorcourselisting', 'block_community', $e->getMessage()));
207 echo $OUTPUT->header();
208 echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
209 if (!empty($notificationmessage)) {
210 echo $notificationmessage;
212 $hubselectorform->display();
213 if (!empty($errormessage)) {
218 $courseids = array();
219 if (!empty($courses)) {
220 foreach ($courses as $course) {
221 if (!empty($course['comments'])) {
222 $courseids[] = $course['id'];
226 $PAGE->requires->yui_module('moodle-block_community-comments', 'M.blocks_community.init_comments',
227 array(array('commentids' => $courseids)));
229 echo highlight($search, $renderer->course_list($courses, $huburl, $courseid));
231 //display givememore/Next link if more course can be displayed
232 if (!empty($courses)) {
233 if (($options->givememore + count($courses)) < $coursetotal) {
234 $fromformdata['givememore'] = count($courses) + $options->givememore;
235 $fromformdata['executesearch'] = true;
236 $fromformdata['sesskey'] = sesskey();
237 echo $renderer->next_button($fromformdata);
241 echo $OUTPUT->footer();