d16b06c8a1ea7dd5cb8b74dd77588a84cd8e0384
[moodle.git] / blocks / community / communitycourse.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /*
18  * @package    blocks
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
23  *
24  * This page display the community course search form.
25  * It also handles adding a course to the community block.
26  * It also handles downloading a course template.
27 */
29 require('../../config.php');
30 require_once($CFG->dirroot.'/blocks/community/locallib.php');
31 require_once($CFG->dirroot.'/blocks/community/forms.php');
33 require_login();
35 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
36 $PAGE->set_url('/blocks/community/communitycourse.php');
37 $PAGE->set_heading($SITE->fullname);
38 $PAGE->set_pagelayout('course');
39 $PAGE->set_title(get_string('searchcourse', 'block_community'));
40 $PAGE->navbar->ignore_active(true);
41 $PAGE->navbar->add(get_string('searchcourse', 'block_community'));
43 $search  = optional_param('search', null, PARAM_TEXT);
45 //if no capability to search course, display an error message
46 $usercansearch = has_capability('moodle/community:add', get_context_instance(CONTEXT_USER, $USER->id));
47 $usercandownload = has_capability('moodle/community:download', get_context_instance(CONTEXT_USER, $USER->id));
48 if (empty($usercansearch)) {
49     echo $OUTPUT->header();
50     echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
51     echo $OUTPUT->notification(get_string('cannotsearchcommunity', 'hub'));
52     echo $OUTPUT->footer();
53     die();
54 }
56 $communitymanager = new block_community_manager();
58 /// Check if the page has been called with trust argument
59 $add  = optional_param('add', -1, PARAM_INTEGER);
60 $confirm  = optional_param('confirmed', false, PARAM_INTEGER);
61 if ($add != -1 and $confirm and confirm_sesskey()) {
62     $course = new stdClass();
63     $course->name  = optional_param('coursefullname', '', PARAM_TEXT);
64     $course->description  = optional_param('coursedescription', '', PARAM_TEXT);
65     $course->url  = optional_param('courseurl', '', PARAM_URL);
66     $course->imageurl  = optional_param('courseimageurl', '', PARAM_URL);
67     $communitymanager->block_community_add_course($course, $USER->id);
68     $notificationmessage = $OUTPUT->notification(get_string('addedtoblock', 'hub'),
69             'notifysuccess');
70 }
72 /// Download
73 $huburl  = optional_param('huburl', false, PARAM_URL);
74 $download  = optional_param('download', -1, PARAM_INTEGER);
75 $courseid  = optional_param('courseid', '', PARAM_INTEGER);
76 $coursefullname  = optional_param('coursefullname', '', PARAM_ALPHANUMEXT);
77 if ($usercandownload and $download != -1 and !empty($courseid) and confirm_sesskey()) {
78     $course = new stdClass();
79     $course->fullname = $coursefullname;
80     $course->id = $courseid;
81     $course->huburl = $huburl;
82     $communitymanager->block_community_download_course_backup($course);
83     $filename =  'backup_'.$course->fullname."_".$course->id.".zip";
84     $notificationmessage = $OUTPUT->notification(get_string('downloadconfirmed', 'hub', $filename),
85             'notifysuccess');
86 }
88 /// Remove community
89 $remove  = optional_param('remove', '', PARAM_INTEGER);
90 $communityid  = optional_param('communityid', '', PARAM_INTEGER);
91 if ($remove != -1 and !empty($communityid) and confirm_sesskey()) {
92     $communitymanager->block_community_remove_course($communityid, $USER->id);
93     $notificationmessage = $OUTPUT->notification(get_string('communityremoved', 'hub'),
94             'notifysuccess');
95 }
98 $renderer = $PAGE->get_renderer('block_community');
100 //forms
101 $hubselectorform = new community_hub_search_form('', array('search' => $search));
102 $fromform = $hubselectorform->get_data();
103 $courses= null;
104 //Retrieve courses by web service
105 if (!empty($fromform)) {
106     $downloadable  = optional_param('downloadable', false, PARAM_INTEGER);
108     $options = new stdClass();
109     if (!empty($fromform->coverage)) {
110         $options->coverage = $fromform->coverage;
111     }
112     if ($fromform->licence != 'all') {
113         $options->licenceshortname = $fromform->licence;
114     }
115     if ($fromform->subject != 'all') {
116         $options->subject = $fromform->subject;
117     }
118     if ($fromform->audience != 'all') {
119         $options->audience = $fromform->audience;
120     }
121     if ($fromform->educationallevel != 'all') {
122         $options->educationallevel = $fromform->educationallevel;
123     }
124     if ($fromform->language != 'all') {
125         $options->language = $fromform->language;
126     }
129     $function = 'hub_get_courses';
130     $params = array($search, $downloadable, !$downloadable, $options);
131     $serverurl = $huburl."/local/hub/webservice/webservices.php";
132     require_once($CFG->dirroot."/webservice/xmlrpc/lib.php");
133     $xmlrpcclient = new webservice_xmlrpc_client();
134     $courses = $xmlrpcclient->call($serverurl, 'publichub', $function, $params);
137 // OUTPUT
138 echo $OUTPUT->header();
139 echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
140 if (!empty($notificationmessage)) {
141     echo $notificationmessage;
143 $hubselectorform->display();
144 echo $renderer->course_list($courses, $huburl);
145 echo $OUTPUT->footer();