MDL-59495 block_community: search courses on moodle.net only
[moodle.git] / blocks / community / communitycourse.php
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.
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/>.
18 /**
19  * Controller for various actions of the block.
20  *
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.
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
29  */
31 require('../../config.php');
32 require_once($CFG->dirroot . '/blocks/community/locallib.php');
33 require_once($CFG->dirroot . '/blocks/community/forms.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('incourse');
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 require_capability('moodle/community:add', $context);
51 $usercandownload = has_capability('moodle/community:download', $context);
53 $communitymanager = new block_community_manager();
54 $renderer = $PAGE->get_renderer('block_community');
56 /// Check if the page has been called with trust argument
57 $add = optional_param('add', -1, PARAM_INT);
58 $confirm = optional_param('confirmed', false, PARAM_INT);
59 if ($add != -1 and $confirm and confirm_sesskey()) {
60     $course = new stdClass();
61     $course->name = optional_param('coursefullname', '', PARAM_TEXT);
62     $course->description = optional_param('coursedescription', '', PARAM_TEXT);
63     $course->url = optional_param('courseurl', '', PARAM_URL);
64     $course->imageurl = optional_param('courseimageurl', '', PARAM_URL);
65     $communitymanager->block_community_add_course($course, $USER->id);
66     echo $OUTPUT->header();
67     echo $renderer->save_link_success(
68             new moodle_url('/course/view.php', array('id' => $courseid)));
69     echo $OUTPUT->footer();
70     die();
71 }
73 /// Delete temp file when cancel restore
74 $cancelrestore = optional_param('cancelrestore', false, PARAM_INT);
75 if ($usercandownload and $cancelrestore and confirm_sesskey()) {
76     $filename = optional_param('filename', '', PARAM_ALPHANUMEXT);
77     //delete temp file
78     unlink($CFG->tempdir . '/backup/' . $filename . ".mbz");
79 }
81 /// Download
82 $download = optional_param('download', -1, PARAM_INT);
83 $downloadcourseid = optional_param('downloadcourseid', '', PARAM_INT);
84 $coursefullname = optional_param('coursefullname', '', PARAM_ALPHANUMEXT);
85 $backupsize = optional_param('backupsize', 0, PARAM_INT);
86 if ($usercandownload and $download != -1 and !empty($downloadcourseid) and confirm_sesskey()) {
87     //OUTPUT: display restore choice page
88     echo $OUTPUT->header();
89     echo $OUTPUT->heading(get_string('downloadingcourse', 'block_community'), 3, 'main');
90     $sizeinfo = new stdClass();
91     $sizeinfo->total = number_format($backupsize / 1000000, 2);
92     echo html_writer::tag('div', get_string('downloadingsize', 'block_community', $sizeinfo),
93             array('class' => 'textinfo'));
94     if (ob_get_level()) {
95         ob_flush();
96     }
97     flush();
98     list($privatefilename, $tmpfilename) = \core\hub\publication::download_course_backup($downloadcourseid, $coursefullname);
99     echo html_writer::tag('div', get_string('downloaded', 'block_community'),
100             array('class' => 'textinfo'));
101     echo $OUTPUT->notification(get_string('downloadconfirmed', 'block_community',
102                     $privatefilename), 'notifysuccess');
103     echo $renderer->restore_confirmation_box($tmpfilename, $context);
104     echo $OUTPUT->footer();
105     die();
108 /// Remove community
109 $remove = optional_param('remove', '', PARAM_INT);
110 $communityid = optional_param('communityid', '', PARAM_INT);
111 if ($remove != -1 and !empty($communityid) and confirm_sesskey()) {
112     $communitymanager->block_community_remove_course($communityid, $USER->id);
113     echo $OUTPUT->header();
114     echo $renderer->remove_success(new moodle_url('/course/view.php', array('id' => $courseid)));
115     echo $OUTPUT->footer();
116     die();
119 //Get form default/current values
120 $fromformdata['coverage'] = optional_param('coverage', 'all', PARAM_TEXT);
121 $fromformdata['licence'] = optional_param('licence', 'all', PARAM_ALPHANUMEXT);
122 $fromformdata['subject'] = optional_param('subject', 'all', PARAM_ALPHANUMEXT);
123 $fromformdata['audience'] = optional_param('audience', 'all', PARAM_ALPHANUMEXT);
124 $fromformdata['language'] = optional_param('language', current_language(), PARAM_ALPHANUMEXT);
125 $fromformdata['educationallevel'] = optional_param('educationallevel', 'all', PARAM_ALPHANUMEXT);
126 $fromformdata['downloadable'] = optional_param('downloadable', $usercandownload, PARAM_ALPHANUM);
127 $fromformdata['orderby'] = optional_param('orderby', 'newest', PARAM_ALPHA);
128 $fromformdata['search'] = $search;
129 $fromformdata['courseid'] = $courseid;
130 $hubselectorform = new community_hub_search_form('', $fromformdata);
131 $hubselectorform->set_data($fromformdata);
133 //Retrieve courses by web service
134 $courses = null;
135 if (optional_param('executesearch', 0, PARAM_INT) and confirm_sesskey()) {
136     $downloadable = optional_param('downloadable', false, PARAM_INT);
138     $options = new stdClass();
139     if (!empty($fromformdata['coverage'])) {
140         $options->coverage = $fromformdata['coverage'];
141     }
142     if ($fromformdata['licence'] != 'all') {
143         $options->licenceshortname = $fromformdata['licence'];
144     }
145     if ($fromformdata['subject'] != 'all') {
146         $options->subject = $fromformdata['subject'];
147     }
148     if ($fromformdata['audience'] != 'all') {
149         $options->audience = $fromformdata['audience'];
150     }
151     if ($fromformdata['educationallevel'] != 'all') {
152         $options->educationallevel = $fromformdata['educationallevel'];
153     }
154     if ($fromformdata['language'] != 'all') {
155         $options->language = $fromformdata['language'];
156     }
158     $options->orderby = $fromformdata['orderby'];
160     //the range of course requested
161     $options->givememore = optional_param('givememore', 0, PARAM_INT);
163     list($courses, $coursetotal) = \core\hub\publication::search($search, $downloadable, $options);
166 // OUTPUT
167 echo $OUTPUT->header();
168 echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
169 echo $renderer->moodlenet_info();
171 $hubselectorform->display();
172 if (!empty($errormessage)) {
173     echo $errormessage;
176 //load javascript
177 $commentedcourseids = array(); //result courses with comments only
178 $courseids = array(); //all result courses
179 $courseimagenumbers = array(); //number of screenshots of all courses (must be exact same order than $courseids)
180 if (!empty($courses)) {
181     foreach ($courses as $course) {
182         if (!empty($course['comments'])) {
183             $commentedcourseids[] = $course['id'];
184         }
185         $courseids[] = $course['id'];
186         $courseimagenumbers[] = $course['screenshots'];
187     }
189 $PAGE->requires->yui_module('moodle-block_community-comments', 'M.blocks_community.init_comments',
190         array(array('commentids' => $commentedcourseids, 'closeButtonTitle' => get_string('close', 'editor'))));
191 $PAGE->requires->yui_module('moodle-block_community-imagegallery', 'M.blocks_community.init_imagegallery',
192         array(array('imageids' => $courseids, 'imagenumbers' => $courseimagenumbers,
193                 'huburl' => HUB_MOODLEORGHUBURL, 'closeButtonTitle' => get_string('close', 'editor'))));
195 echo highlight($search, $renderer->course_list($courses, null, $courseid));
197 //display givememore/Next link if more course can be displayed
198 if (!empty($courses)) {
199     if (($options->givememore + count($courses)) < $coursetotal) {
200         $fromformdata['givememore'] = count($courses) + $options->givememore;
201         $fromformdata['executesearch'] = true;
202         $fromformdata['sesskey'] = sesskey();
203         echo $renderer->next_button($fromformdata);
204     }
207 echo $OUTPUT->footer();