MDL-44151 Amend subplugin packages.
[moodle.git] / blocks / community / communitycourse.php
CommitLineData
07ab0c80 1<?php
178f282b 2
07ab0c80 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.
14//
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/>.
17
f25a6839 18/**
993414fd
EL
19 * Controller for various actions of the block.
20 *
07ab0c80 21 * This page display the community course search form.
22 * It also handles adding a course to the community block.
23 * It also handles downloading a course template.
f25a6839
SH
24 *
25 * @package block_community
26 * @author Jerome Mouneyrac <jerome@mouneyrac.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
28 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
178f282b 29 */
07ab0c80 30
31require('../../config.php');
178f282b 32require_once($CFG->dirroot . '/blocks/community/locallib.php');
33require_once($CFG->dirroot . '/blocks/community/forms.php');
a8e174cc 34require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
07ab0c80 35
36require_login();
0d731457 37$courseid = required_param('courseid', PARAM_INT); //if no courseid is given
74df2951 38$parentcourse = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
b48c4478 39
fe2fdd11 40$context = context_course::instance($courseid);
34613fb8 41$PAGE->set_course($parentcourse);
07ab0c80 42$PAGE->set_url('/blocks/community/communitycourse.php');
43$PAGE->set_heading($SITE->fullname);
44$PAGE->set_pagelayout('course');
45$PAGE->set_title(get_string('searchcourse', 'block_community'));
07ab0c80 46$PAGE->navbar->add(get_string('searchcourse', 'block_community'));
47
178f282b 48$search = optional_param('search', null, PARAM_TEXT);
07ab0c80 49
755f96f8 50//if no capability to search course, display an error message
b48c4478 51$usercansearch = has_capability('moodle/community:add', $context);
52$usercandownload = has_capability('moodle/community:download', $context);
755f96f8 53if (empty($usercansearch)) {
b48c4478 54 $notificationerror = get_string('cannotsearchcommunity', 'hub');
5cbbcc1a 55} else if (!extension_loaded('xmlrpc')) {
0d5fae12 56 $notificationerror = $OUTPUT->doc_link('admin/environment/php_extension/xmlrpc', '');
5cbbcc1a 57 $notificationerror .= get_string('xmlrpcdisabledcommunity', 'hub');
58}
b48c4478 59if (!empty($notificationerror)) {
755f96f8 60 echo $OUTPUT->header();
61 echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
5cbbcc1a 62 echo $OUTPUT->notification($notificationerror);
755f96f8 63 echo $OUTPUT->footer();
64 die();
65}
66
1c2b7a90 67$communitymanager = new block_community_manager();
b48c4478 68$renderer = $PAGE->get_renderer('block_community');
07ab0c80 69
70/// Check if the page has been called with trust argument
1e12c120
RT
71$add = optional_param('add', -1, PARAM_INT);
72$confirm = optional_param('confirmed', false, PARAM_INT);
07ab0c80 73if ($add != -1 and $confirm and confirm_sesskey()) {
74 $course = new stdClass();
178f282b 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);
1c2b7a90 79 $communitymanager->block_community_add_course($course, $USER->id);
9e32912b 80 echo $OUTPUT->header();
81 echo $renderer->save_link_success(
82 new moodle_url('/course/view.php', array('id' => $courseid)));
83 echo $OUTPUT->footer();
84 die();
07ab0c80 85}
86
b48c4478 87/// Delete temp file when cancel restore
88$cancelrestore = optional_param('cancelrestore', false, PARAM_INT);
89if ($usercandownload and $cancelrestore and confirm_sesskey()) {
90 $filename = optional_param('filename', '', PARAM_ALPHANUMEXT);
91 //delete temp file
7aa06e6d 92 unlink($CFG->tempdir . '/backup/' . $filename . ".mbz");
b48c4478 93}
94
07ab0c80 95/// Download
178f282b 96$huburl = optional_param('huburl', false, PARAM_URL);
1e12c120
RT
97$download = optional_param('download', -1, PARAM_INT);
98$downloadcourseid = optional_param('downloadcourseid', '', PARAM_INT);
178f282b 99$coursefullname = optional_param('coursefullname', '', PARAM_ALPHANUMEXT);
19eb73cd 100$backupsize = optional_param('backupsize', 0, PARAM_INT);
b48c4478 101if ($usercandownload and $download != -1 and !empty($downloadcourseid) and confirm_sesskey()) {
78f00eab 102 $course = new stdClass();
103 $course->fullname = $coursefullname;
b48c4478 104 $course->id = $downloadcourseid;
78f00eab 105 $course->huburl = $huburl;
b48c4478 106
107 //OUTPUT: display restore choice page
108 echo $OUTPUT->header();
19eb73cd 109 echo $OUTPUT->heading(get_string('downloadingcourse', 'block_community'), 3, 'main');
19eb73cd 110 $sizeinfo = new stdClass();
ba14d721 111 $sizeinfo->total = number_format($backupsize / 1000000, 2);
19eb73cd 112 echo html_writer::tag('div', get_string('downloadingsize', 'block_community', $sizeinfo),
113 array('class' => 'textinfo'));
c78e1133
JM
114 if (ob_get_level()) {
115 ob_flush();
116 }
19eb73cd 117 flush();
65a00309 118 $filenames = $communitymanager->block_community_download_course_backup($course);
19eb73cd 119 echo html_writer::tag('div', get_string('downloaded', 'block_community'),
120 array('class' => 'textinfo'));
b48c4478 121 echo $OUTPUT->notification(get_string('downloadconfirmed', 'block_community',
43f258cd 122 '/downloaded_backup/' . $filenames['privatefile']), 'notifysuccess');
b48c4478 123 echo $renderer->restore_confirmation_box($filenames['tmpfile'], $context);
124 echo $OUTPUT->footer();
125 die();
07ab0c80 126}
127
276f590e 128/// Remove community
1e12c120
RT
129$remove = optional_param('remove', '', PARAM_INT);
130$communityid = optional_param('communityid', '', PARAM_INT);
276f590e 131if ($remove != -1 and !empty($communityid) and confirm_sesskey()) {
5dbfbacc 132 $communitymanager->block_community_remove_course($communityid, $USER->id);
38e22fe9 133 echo $OUTPUT->header();
12ceaa55 134 echo $renderer->remove_success(new moodle_url('/course/view.php', array('id' => $courseid)));
38e22fe9 135 echo $OUTPUT->footer();
136 die();
276f590e 137}
138
c48ab36a 139//Get form default/current values
140$fromformdata['coverage'] = optional_param('coverage', 'all', PARAM_TEXT);
141$fromformdata['licence'] = optional_param('licence', 'all', PARAM_ALPHANUMEXT);
142$fromformdata['subject'] = optional_param('subject', 'all', PARAM_ALPHANUMEXT);
143$fromformdata['audience'] = optional_param('audience', 'all', PARAM_ALPHANUMEXT);
fb5302b5 144$fromformdata['language'] = optional_param('language', current_language(), PARAM_ALPHANUMEXT);
c48ab36a 145$fromformdata['educationallevel'] = optional_param('educationallevel', 'all', PARAM_ALPHANUMEXT);
e7dbb039 146$fromformdata['downloadable'] = optional_param('downloadable', $usercandownload, PARAM_ALPHANUM);
c48ab36a 147$fromformdata['orderby'] = optional_param('orderby', 'newest', PARAM_ALPHA);
148$fromformdata['huburl'] = optional_param('huburl', HUB_MOODLEORGHUBURL, PARAM_URL);
149$fromformdata['search'] = $search;
150$fromformdata['courseid'] = $courseid;
151$hubselectorform = new community_hub_search_form('', $fromformdata);
152$hubselectorform->set_data($fromformdata);
153
07ab0c80 154//Retrieve courses by web service
c48ab36a 155$courses = null;
1e12c120
RT
156if (optional_param('executesearch', 0, PARAM_INT) and confirm_sesskey()) {
157 $downloadable = optional_param('downloadable', false, PARAM_INT);
c2047913 158
159 $options = new stdClass();
c48ab36a 160 if (!empty($fromformdata['coverage'])) {
161 $options->coverage = $fromformdata['coverage'];
c2047913 162 }
c48ab36a 163 if ($fromformdata['licence'] != 'all') {
164 $options->licenceshortname = $fromformdata['licence'];
c2047913 165 }
c48ab36a 166 if ($fromformdata['subject'] != 'all') {
167 $options->subject = $fromformdata['subject'];
c2047913 168 }
c48ab36a 169 if ($fromformdata['audience'] != 'all') {
170 $options->audience = $fromformdata['audience'];
c2047913 171 }
c48ab36a 172 if ($fromformdata['educationallevel'] != 'all') {
173 $options->educationallevel = $fromformdata['educationallevel'];
c2047913 174 }
c48ab36a 175 if ($fromformdata['language'] != 'all') {
176 $options->language = $fromformdata['language'];
c2047913 177 }
178
c48ab36a 179 $options->orderby = $fromformdata['orderby'];
180
181 //the range of course requested
1e12c120 182 $options->givememore = optional_param('givememore', 0, PARAM_INT);
43a58be7 183
ac90abb4 184 //check if the selected hub is from the registered list (in this case we use the private token)
185 $token = 'publichub';
186 $registrationmanager = new registration_manager();
187 $registeredhubs = $registrationmanager->get_registered_on_hubs();
188 foreach ($registeredhubs as $registeredhub) {
189 if ($huburl == $registeredhub->huburl) {
190 $token = $registeredhub->token;
191 }
192 }
193
07ab0c80 194 $function = 'hub_get_courses';
efba7034 195 $params = array('search' => $search, 'downloadable' => $downloadable,
196 'enrollable' => !$downloadable, 'options' => $options);
178f282b 197 $serverurl = $huburl . "/local/hub/webservice/webservices.php";
198 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
001251f2 199 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $token);
178f282b 200 try {
c48ab36a 201 $result = $xmlrpcclient->call($function, $params);
202 $courses = $result['courses'];
203 $coursetotal = $result['coursetotal'];
178f282b 204 } catch (Exception $e) {
b48c4478 205 $errormessage = $OUTPUT->notification(
206 get_string('errorcourselisting', 'block_community', $e->getMessage()));
178f282b 207 }
07ab0c80 208}
209
210// OUTPUT
211echo $OUTPUT->header();
f1b4e19e 212echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
07ab0c80 213$hubselectorform->display();
178f282b 214if (!empty($errormessage)) {
215 echo $errormessage;
216}
1ce282c8 217
218//load javascript
20e033bd 219$commentedcourseids = array(); //result courses with comments only
220$courseids = array(); //all result courses
221$courseimagenumbers = array(); //number of screenshots of all courses (must be exact same order than $courseids)
1ce282c8 222if (!empty($courses)) {
223 foreach ($courses as $course) {
224 if (!empty($course['comments'])) {
20e033bd 225 $commentedcourseids[] = $course['id'];
1ce282c8 226 }
20e033bd 227 $courseids[] = $course['id'];
228 $courseimagenumbers[] = $course['screenshots'];
1ce282c8 229 }
230}
231$PAGE->requires->yui_module('moodle-block_community-comments', 'M.blocks_community.init_comments',
4bcbdb3e 232 array(array('commentids' => $commentedcourseids, 'closeButtonTitle' => get_string('close', 'editor'))));
20e033bd 233$PAGE->requires->yui_module('moodle-block_community-imagegallery', 'M.blocks_community.init_imagegallery',
234 array(array('imageids' => $courseids, 'imagenumbers' => $courseimagenumbers,
4bcbdb3e 235 'huburl' => $huburl, 'closeButtonTitle' => get_string('close', 'editor'))));
1ce282c8 236
b48c4478 237echo highlight($search, $renderer->course_list($courses, $huburl, $courseid));
c48ab36a 238
239//display givememore/Next link if more course can be displayed
240if (!empty($courses)) {
241 if (($options->givememore + count($courses)) < $coursetotal) {
242 $fromformdata['givememore'] = count($courses) + $options->givememore;
243 $fromformdata['executesearch'] = true;
244 $fromformdata['sesskey'] = sesskey();
245 echo $renderer->next_button($fromformdata);
246 }
247}
248
5dbfbacc 249echo $OUTPUT->footer();