PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/blocks/community/communitycourse.php

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