MDL-35701 respect case of external column names in enrol_database
[moodle.git] / blocks / community / communitycourse.php
blob3e6497f11611c1e212d309508e5f513b74511ca0
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 = context_course::instance($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_INT);
71 $confirm = optional_param('confirmed', false, PARAM_INT);
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->tempdir . '/backup/' . $filename . ".mbz");
94 /// Download
95 $huburl = optional_param('huburl', false, PARAM_URL);
96 $download = optional_param('download', -1, PARAM_INT);
97 $downloadcourseid = optional_param('downloadcourseid', '', PARAM_INT);
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 if (ob_get_level()) {
114 ob_flush();
116 flush();
117 $filenames = $communitymanager->block_community_download_course_backup($course);
118 echo html_writer::tag('div', get_string('downloaded', 'block_community'),
119 array('class' => 'textinfo'));
120 echo $OUTPUT->notification(get_string('downloadconfirmed', 'block_community',
121 '/downloaded_backup/' . $filenames['privatefile']), 'notifysuccess');
122 echo $renderer->restore_confirmation_box($filenames['tmpfile'], $context);
123 echo $OUTPUT->footer();
124 die();
127 /// Remove community
128 $remove = optional_param('remove', '', PARAM_INT);
129 $communityid = optional_param('communityid', '', PARAM_INT);
130 if ($remove != -1 and !empty($communityid) and confirm_sesskey()) {
131 $communitymanager->block_community_remove_course($communityid, $USER->id);
132 echo $OUTPUT->header();
133 echo $renderer->remove_success(new moodle_url(get_referer(false)));
134 echo $OUTPUT->footer();
135 die();
138 //Get form default/current values
139 $fromformdata['coverage'] = optional_param('coverage', 'all', PARAM_TEXT);
140 $fromformdata['licence'] = optional_param('licence', 'all', PARAM_ALPHANUMEXT);
141 $fromformdata['subject'] = optional_param('subject', 'all', PARAM_ALPHANUMEXT);
142 $fromformdata['audience'] = optional_param('audience', 'all', PARAM_ALPHANUMEXT);
143 $fromformdata['language'] = optional_param('language', current_language(), PARAM_ALPHANUMEXT);
144 $fromformdata['educationallevel'] = optional_param('educationallevel', 'all', PARAM_ALPHANUMEXT);
145 $fromformdata['downloadable'] = optional_param('downloadable', 0, PARAM_ALPHANUM);
146 $fromformdata['orderby'] = optional_param('orderby', 'newest', PARAM_ALPHA);
147 $fromformdata['huburl'] = optional_param('huburl', HUB_MOODLEORGHUBURL, PARAM_URL);
148 $fromformdata['search'] = $search;
149 $fromformdata['courseid'] = $courseid;
150 $hubselectorform = new community_hub_search_form('', $fromformdata);
151 $hubselectorform->set_data($fromformdata);
153 //Retrieve courses by web service
154 $courses = null;
155 if (optional_param('executesearch', 0, PARAM_INT) and confirm_sesskey()) {
156 $downloadable = optional_param('downloadable', false, PARAM_INT);
158 $options = new stdClass();
159 if (!empty($fromformdata['coverage'])) {
160 $options->coverage = $fromformdata['coverage'];
162 if ($fromformdata['licence'] != 'all') {
163 $options->licenceshortname = $fromformdata['licence'];
165 if ($fromformdata['subject'] != 'all') {
166 $options->subject = $fromformdata['subject'];
168 if ($fromformdata['audience'] != 'all') {
169 $options->audience = $fromformdata['audience'];
171 if ($fromformdata['educationallevel'] != 'all') {
172 $options->educationallevel = $fromformdata['educationallevel'];
174 if ($fromformdata['language'] != 'all') {
175 $options->language = $fromformdata['language'];
178 $options->orderby = $fromformdata['orderby'];
180 //the range of course requested
181 $options->givememore = optional_param('givememore', 0, PARAM_INT);
183 //check if the selected hub is from the registered list (in this case we use the private token)
184 $token = 'publichub';
185 $registrationmanager = new registration_manager();
186 $registeredhubs = $registrationmanager->get_registered_on_hubs();
187 foreach ($registeredhubs as $registeredhub) {
188 if ($huburl == $registeredhub->huburl) {
189 $token = $registeredhub->token;
193 $function = 'hub_get_courses';
194 $params = array('search' => $search, 'downloadable' => $downloadable,
195 'enrollable' => !$downloadable, 'options' => $options);
196 $serverurl = $huburl . "/local/hub/webservice/webservices.php";
197 require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
198 $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $token);
199 try {
200 $result = $xmlrpcclient->call($function, $params);
201 $courses = $result['courses'];
202 $coursetotal = $result['coursetotal'];
203 } catch (Exception $e) {
204 $errormessage = $OUTPUT->notification(
205 get_string('errorcourselisting', 'block_community', $e->getMessage()));
209 // OUTPUT
210 echo $OUTPUT->header();
211 echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
212 $hubselectorform->display();
213 if (!empty($errormessage)) {
214 echo $errormessage;
217 //load javascript
218 $commentedcourseids = array(); //result courses with comments only
219 $courseids = array(); //all result courses
220 $courseimagenumbers = array(); //number of screenshots of all courses (must be exact same order than $courseids)
221 if (!empty($courses)) {
222 foreach ($courses as $course) {
223 if (!empty($course['comments'])) {
224 $commentedcourseids[] = $course['id'];
226 $courseids[] = $course['id'];
227 $courseimagenumbers[] = $course['screenshots'];
230 $PAGE->requires->yui_module('moodle-block_community-comments', 'M.blocks_community.init_comments',
231 array(array('commentids' => $commentedcourseids)));
232 $PAGE->requires->yui_module('moodle-block_community-imagegallery', 'M.blocks_community.init_imagegallery',
233 array(array('imageids' => $courseids, 'imagenumbers' => $courseimagenumbers,
234 'huburl' => $huburl)));
236 echo highlight($search, $renderer->course_list($courses, $huburl, $courseid));
238 //display givememore/Next link if more course can be displayed
239 if (!empty($courses)) {
240 if (($options->givememore + count($courses)) < $coursetotal) {
241 $fromformdata['givememore'] = count($courses) + $options->givememore;
242 $fromformdata['executesearch'] = true;
243 $fromformdata['sesskey'] = sesskey();
244 echo $renderer->next_button($fromformdata);
248 echo $OUTPUT->footer();