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 if ($usercandownload and $download != -1 and !empty($downloadcourseid) and confirm_sesskey()) {
98 $course = new stdClass();
99 $course->fullname = $coursefullname;
100 $course->id = $downloadcourseid;
101 $course->huburl = $huburl;
102 $filenames = $communitymanager->block_community_download_course_backup($course);
104 //OUTPUT: display restore choice page
105 echo $OUTPUT->header();
106 echo $OUTPUT->heading(get_string('restorecourse', 'block_community'), 3, 'main');
107 echo $OUTPUT->notification(get_string('downloadconfirmed', 'block_community',
108 '/downloaded_backup/' . $filenames['privatefile']), 'notifysuccess');
109 echo $renderer->restore_confirmation_box($filenames['tmpfile'], $context);
110 echo $OUTPUT->footer();
115 $remove = optional_param('remove', '', PARAM_INTEGER);
116 $communityid = optional_param('communityid', '', PARAM_INTEGER);
117 if ($remove != -1 and !empty($communityid) and confirm_sesskey()) {
118 $communitymanager->block_community_remove_course($communityid, $USER->id);
119 $notificationmessage = $OUTPUT->notification(get_string('communityremoved', 'hub'),
123 //Get form default/current values
124 $fromformdata['coverage'] = optional_param('coverage', 'all', PARAM_TEXT);
125 $fromformdata['licence'] = optional_param('licence', 'all', PARAM_ALPHANUMEXT);
126 $fromformdata['subject'] = optional_param('subject', 'all', PARAM_ALPHANUMEXT);
127 $fromformdata['audience'] = optional_param('audience', 'all', PARAM_ALPHANUMEXT);
128 $fromformdata['language'] = optional_param('language', 'all', PARAM_ALPHANUMEXT);
129 $fromformdata['educationallevel'] = optional_param('educationallevel', 'all', PARAM_ALPHANUMEXT);
130 $fromformdata['downloadable'] = optional_param('downloadable', 0, PARAM_ALPHANUM);
131 $fromformdata['orderby'] = optional_param('orderby', 'newest', PARAM_ALPHA);
132 $fromformdata['huburl'] = optional_param('huburl', HUB_MOODLEORGHUBURL, PARAM_URL);
133 $fromformdata['search'] = $search;
134 $fromformdata['courseid'] = $courseid;
135 $hubselectorform = new community_hub_search_form('', $fromformdata);
136 $hubselectorform->set_data($fromformdata);
138 //Retrieve courses by web service
140 if (optional_param('executesearch', 0, PARAM_INTEGER) and confirm_sesskey()) {
141 $downloadable = optional_param('downloadable', false, PARAM_INTEGER);
143 $options = new stdClass();
144 if (!empty($fromformdata['coverage'])) {
145 $options->coverage = $fromformdata['coverage'];
147 if ($fromformdata['licence'] != 'all') {
148 $options->licenceshortname = $fromformdata['licence'];
150 if ($fromformdata['subject'] != 'all') {
151 $options->subject = $fromformdata['subject'];
153 if ($fromformdata['audience'] != 'all') {
154 $options->audience = $fromformdata['audience'];
156 if ($fromformdata['educationallevel'] != 'all') {
157 $options->educationallevel = $fromformdata['educationallevel'];
159 if ($fromformdata['language'] != 'all') {
160 $options->language = $fromformdata['language'];
163 $options->orderby = $fromformdata['orderby'];
165 //the range of course requested
166 $options->givememore = optional_param('givememore', 0, PARAM_INTEGER);
168 //check if the selected hub is from the registered list (in this case we use the private token)
169 $token = 'publichub';
170 $registrationmanager = new registration_manager();
171 $registeredhubs = $registrationmanager->get_registered_on_hubs();
172 foreach ($registeredhubs as $registeredhub) {
173 if ($huburl == $registeredhub->huburl) {
174 $token = $registeredhub->token;
178 $function = 'hub_get_courses';
179 $params = array('search' => $search, 'downloadable' => $downloadable,
180 'enrollable' => !$downloadable, 'options' => $options);
181 $serverurl = $huburl . "/local/hub/webservice/webservices.php";
182 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
183 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $token);
185 $result = $xmlrpcclient->call($function, $params);
186 $courses = $result['courses'];
187 $coursetotal = $result['coursetotal'];
188 } catch (Exception $e) {
189 $errormessage = $OUTPUT->notification(
190 get_string('errorcourselisting', 'block_community', $e->getMessage()));
195 echo $OUTPUT->header();
196 echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
197 if (!empty($notificationmessage)) {
198 echo $notificationmessage;
200 $hubselectorform->display();
201 if (!empty($errormessage)) {
206 $courseids = array();
207 if (!empty($courses)) {
208 foreach ($courses as $course) {
209 if (!empty($course['comments'])) {
210 $courseids[] = $course['id'];
214 $PAGE->requires->yui_module('moodle-block_community-comments', 'M.blocks_community.init_comments',
215 array(array('commentids' => $courseids)));
217 echo highlight($search, $renderer->course_list($courses, $huburl, $courseid));
219 //display givememore/Next link if more course can be displayed
220 if (!empty($courses)) {
221 if (($options->givememore + count($courses)) < $coursetotal) {
222 $fromformdata['givememore'] = count($courses) + $options->givememore;
223 $fromformdata['executesearch'] = true;
224 $fromformdata['sesskey'] = sesskey();
225 echo $renderer->next_button($fromformdata);
229 echo $OUTPUT->footer();