MDL-31831 whitespace clean - take2
[moodle.git] / blocks / community / communitycourse.php
blobcf9e4a86d884f9b9c13bba3be89729b79fb235bc
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
19 * @package blocks
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');
35 require_login();
36 $courseid = required_param('courseid', PARAM_INT); //if no courseid is given
37 $parentcourse = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
39 $context = get_context_instance(CONTEXT_COURSE, $courseid);
40 $PAGE->set_course($parentcourse);
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->add(get_string('searchcourse', 'block_community'));
47 $search = optional_param('search', null, PARAM_TEXT);
49 //if no capability to search course, display an error message
50 $usercansearch = has_capability('moodle/community:add', $context);
51 $usercandownload = has_capability('moodle/community:download', $context);
52 if (empty($usercansearch)) {
53 $notificationerror = get_string('cannotsearchcommunity', 'hub');
54 } else if (!extension_loaded('xmlrpc')) {
55 $notificationerror = $OUTPUT->doc_link('admin/environment/php_extension/xmlrpc', '');
56 $notificationerror .= get_string('xmlrpcdisabledcommunity', 'hub');
58 if (!empty($notificationerror)) {
59 echo $OUTPUT->header();
60 echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
61 echo $OUTPUT->notification($notificationerror);
62 echo $OUTPUT->footer();
63 die();
66 $communitymanager = new block_community_manager();
67 $renderer = $PAGE->get_renderer('block_community');
69 /// Check if the page has been called with trust argument
70 $add = optional_param('add', -1, PARAM_INTEGER);
71 $confirm = optional_param('confirmed', false, PARAM_INTEGER);
72 if ($add != -1 and $confirm and confirm_sesskey()) {
73 $course = new stdClass();
74 $course->name = optional_param('coursefullname', '', PARAM_TEXT);
75 $course->description = optional_param('coursedescription', '', PARAM_TEXT);
76 $course->url = optional_param('courseurl', '', PARAM_URL);
77 $course->imageurl = optional_param('courseimageurl', '', PARAM_URL);
78 $communitymanager->block_community_add_course($course, $USER->id);
79 echo $OUTPUT->header();
80 echo $renderer->save_link_success(
81 new moodle_url('/course/view.php', array('id' => $courseid)));
82 echo $OUTPUT->footer();
83 die();
86 /// Delete temp file when cancel restore
87 $cancelrestore = optional_param('cancelrestore', false, PARAM_INT);
88 if ($usercandownload and $cancelrestore and confirm_sesskey()) {
89 $filename = optional_param('filename', '', PARAM_ALPHANUMEXT);
90 //delete temp file
91 unlink($CFG->dataroot . '/temp/backup/' . $filename . ".mbz");
94 /// Download
95 $huburl = optional_param('huburl', false, PARAM_URL);
96 $download = optional_param('download', -1, PARAM_INTEGER);
97 $downloadcourseid = optional_param('downloadcourseid', '', PARAM_INTEGER);
98 $coursefullname = optional_param('coursefullname', '', PARAM_ALPHANUMEXT);
99 $backupsize = optional_param('backupsize', 0, PARAM_INT);
100 if ($usercandownload and $download != -1 and !empty($downloadcourseid) and confirm_sesskey()) {
101 $course = new stdClass();
102 $course->fullname = $coursefullname;
103 $course->id = $downloadcourseid;
104 $course->huburl = $huburl;
106 //OUTPUT: display restore choice page
107 echo $OUTPUT->header();
108 echo $OUTPUT->heading(get_string('downloadingcourse', 'block_community'), 3, 'main');
109 $sizeinfo = new stdClass();
110 $sizeinfo->total = number_format($backupsize / 1000000, 2);
111 echo html_writer::tag('div', get_string('downloadingsize', 'block_community', $sizeinfo),
112 array('class' => 'textinfo'));
113 flush();
114 $filenames = $communitymanager->block_community_download_course_backup($course);
115 echo html_writer::tag('div', get_string('downloaded', 'block_community'),
116 array('class' => 'textinfo'));
117 echo $OUTPUT->notification(get_string('downloadconfirmed', 'block_community',
118 '/downloaded_backup/' . $filenames['privatefile']), 'notifysuccess');
119 echo $renderer->restore_confirmation_box($filenames['tmpfile'], $context);
120 echo $OUTPUT->footer();
121 die();
124 /// Remove community
125 $remove = optional_param('remove', '', PARAM_INTEGER);
126 $communityid = optional_param('communityid', '', PARAM_INTEGER);
127 if ($remove != -1 and !empty($communityid) and confirm_sesskey()) {
128 $communitymanager->block_community_remove_course($communityid, $USER->id);
129 echo $OUTPUT->header();
130 echo $renderer->remove_success(new moodle_url(get_referer(false)));
131 echo $OUTPUT->footer();
132 die();
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', current_language(), 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
151 $courses = null;
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);
196 try {
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()));
206 // OUTPUT
207 echo $OUTPUT->header();
208 echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
209 $hubselectorform->display();
210 if (!empty($errormessage)) {
211 echo $errormessage;
214 //load javascript
215 $commentedcourseids = array(); //result courses with comments only
216 $courseids = array(); //all result courses
217 $courseimagenumbers = array(); //number of screenshots of all courses (must be exact same order than $courseids)
218 if (!empty($courses)) {
219 foreach ($courses as $course) {
220 if (!empty($course['comments'])) {
221 $commentedcourseids[] = $course['id'];
223 $courseids[] = $course['id'];
224 $courseimagenumbers[] = $course['screenshots'];
227 $PAGE->requires->yui_module('moodle-block_community-comments', 'M.blocks_community.init_comments',
228 array(array('commentids' => $commentedcourseids)));
229 $PAGE->requires->yui_module('moodle-block_community-imagegallery', 'M.blocks_community.init_imagegallery',
230 array(array('imageids' => $courseids, 'imagenumbers' => $courseimagenumbers,
231 'huburl' => $huburl)));
233 echo highlight($search, $renderer->course_list($courses, $huburl, $courseid));
235 //display givememore/Next link if more course can be displayed
236 if (!empty($courses)) {
237 if (($options->givememore + count($courses)) < $coursetotal) {
238 $fromformdata['givememore'] = count($courses) + $options->givememore;
239 $fromformdata['executesearch'] = true;
240 $fromformdata['sesskey'] = sesskey();
241 echo $renderer->next_button($fromformdata);
245 echo $OUTPUT->footer();