Search moodle.org's
Developer Documentation

   1  <?php
   2  
   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  
  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   */
  30  
  31  require('../../config.php');
  32  require_once($CFG->dirroot . '/blocks/community/locallib.php');
  33  require_once($CFG->dirroot . '/blocks/community/forms.php');
  34  require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
  35  
  36  require_login();
  37  $courseid = required_param('courseid', PARAM_INT); //if no courseid is given
  38  $parentcourse = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  39  
  40  $context = context_course::instance($courseid);
  41  $PAGE->set_course($parentcourse);
  42  $PAGE->set_url('/blocks/community/communitycourse.php');
  43  $PAGE->set_heading($SITE->fullname);
  44  $PAGE->set_pagelayout('incourse');
  45  $PAGE->set_title(get_string('searchcourse', 'block_community'));
  46  $PAGE->navbar->add(get_string('searchcourse', 'block_community'));
  47  
  48  $search = optional_param('search', null, PARAM_TEXT);
  49  
  50  //if no capability to search course, display an error message
  51  $usercansearch = has_capability('moodle/community:add', $context);
  52  $usercandownload = has_capability('moodle/community:download', $context);
  53  if (empty($usercansearch)) {
  54      $notificationerror = get_string('cannotsearchcommunity', 'hub');
  55  } else if (!extension_loaded('xmlrpc')) {
  56      $notificationerror = $OUTPUT->doc_link('admin/environment/php_extension/xmlrpc', '');
  57      $notificationerror .= get_string('xmlrpcdisabledcommunity', 'hub');
  58  }
  59  if (!empty($notificationerror)) {
  60      echo $OUTPUT->header();
  61      echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
  62      echo $OUTPUT->notification($notificationerror);
  63      echo $OUTPUT->footer();
  64      die();
  65  }
  66  
  67  $communitymanager = new block_community_manager();
  68  $renderer = $PAGE->get_renderer('block_community');
  69  
  70  /// Check if the page has been called with trust argument
  71  $add = optional_param('add', -1, PARAM_INT);
  72  $confirm = optional_param('confirmed', false, PARAM_INT);
  73  if ($add != -1 and $confirm and confirm_sesskey()) {
  74      $course = new stdClass();
  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);
  79      $communitymanager->block_community_add_course($course, $USER->id);
  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();
  85  }
  86  
  87  /// Delete temp file when cancel restore
  88  $cancelrestore = optional_param('cancelrestore', false, PARAM_INT);
  89  if ($usercandownload and $cancelrestore and confirm_sesskey()) {
  90      $filename = optional_param('filename', '', PARAM_ALPHANUMEXT);
  91      //delete temp file
  92      unlink($CFG->tempdir . '/backup/' . $filename . ".mbz");
  93  }
  94  
  95  /// Download
  96  $huburl = optional_param('huburl', false, PARAM_URL);
  97  $download = optional_param('download', -1, PARAM_INT);
  98  $downloadcourseid = optional_param('downloadcourseid', '', PARAM_INT);
  99  $coursefullname = optional_param('coursefullname', '', PARAM_ALPHANUMEXT);
 100  $backupsize = optional_param('backupsize', 0, PARAM_INT);
 101  if ($usercandownload and $download != -1 and !empty($downloadcourseid) and confirm_sesskey()) {
 102      $course = new stdClass();
 103      $course->fullname = $coursefullname;
 104      $course->id = $downloadcourseid;
 105      $course->huburl = $huburl;
 106  
 107      //OUTPUT: display restore choice page
 108      echo $OUTPUT->header();
 109      echo $OUTPUT->heading(get_string('downloadingcourse', 'block_community'), 3, 'main');
 110      $sizeinfo = new stdClass();
 111      $sizeinfo->total = number_format($backupsize / 1000000, 2);
 112      echo html_writer::tag('div', get_string('downloadingsize', 'block_community', $sizeinfo),
 113              array('class' => 'textinfo'));
 114      if (ob_get_level()) {
 115          ob_flush();
 116      }
 117      flush();
 118      $filenames = $communitymanager->block_community_download_course_backup($course);
 119      echo html_writer::tag('div', get_string('downloaded', 'block_community'),
 120              array('class' => 'textinfo'));
 121      echo $OUTPUT->notification(get_string('downloadconfirmed', 'block_community',
 122                      '/downloaded_backup/' . $filenames['privatefile']), 'notifysuccess');
 123      echo $renderer->restore_confirmation_box($filenames['tmpfile'], $context);
 124      echo $OUTPUT->footer();
 125      die();
 126  }
 127  
 128  /// Remove community
 129  $remove = optional_param('remove', '', PARAM_INT);
 130  $communityid = optional_param('communityid', '', PARAM_INT);
 131  if ($remove != -1 and !empty($communityid) and confirm_sesskey()) {
 132      $communitymanager->block_community_remove_course($communityid, $USER->id);
 133      echo $OUTPUT->header();
 134      echo $renderer->remove_success(new moodle_url('/course/view.php', array('id' => $courseid)));
 135      echo $OUTPUT->footer();
 136      die();
 137  }
 138  
 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);
 144  $fromformdata['language'] = optional_param('language', current_language(), PARAM_ALPHANUMEXT);
 145  $fromformdata['educationallevel'] = optional_param('educationallevel', 'all', PARAM_ALPHANUMEXT);
 146  $fromformdata['downloadable'] = optional_param('downloadable', $usercandownload, PARAM_ALPHANUM);
 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  
 154  //Retrieve courses by web service
 155  $courses = null;
 156  if (optional_param('executesearch', 0, PARAM_INT) and confirm_sesskey()) {
 157      $downloadable = optional_param('downloadable', false, PARAM_INT);
 158  
 159      $options = new stdClass();
 160      if (!empty($fromformdata['coverage'])) {
 161          $options->coverage = $fromformdata['coverage'];
 162      }
 163      if ($fromformdata['licence'] != 'all') {
 164          $options->licenceshortname = $fromformdata['licence'];
 165      }
 166      if ($fromformdata['subject'] != 'all') {
 167          $options->subject = $fromformdata['subject'];
 168      }
 169      if ($fromformdata['audience'] != 'all') {
 170          $options->audience = $fromformdata['audience'];
 171      }
 172      if ($fromformdata['educationallevel'] != 'all') {
 173          $options->educationallevel = $fromformdata['educationallevel'];
 174      }
 175      if ($fromformdata['language'] != 'all') {
 176          $options->language = $fromformdata['language'];
 177      }
 178  
 179      $options->orderby = $fromformdata['orderby'];
 180  
 181      //the range of course requested
 182      $options->givememore = optional_param('givememore', 0, PARAM_INT);
 183  
 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  
 194      $function = 'hub_get_courses';
 195      $params = array('search' => $search, 'downloadable' => $downloadable,
 196          'enrollable' => !$downloadable, 'options' => $options);
 197      $serverurl = $huburl . "/local/hub/webservice/webservices.php";
 198      require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
 199      $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $token);
 200      try {
 201          $result = $xmlrpcclient->call($function, $params);
 202          $courses = $result['courses'];
 203          $coursetotal = $result['coursetotal'];
 204      } catch (Exception $e) {
 205          $errormessage = $OUTPUT->notification(
 206                          get_string('errorcourselisting', 'block_community', $e->getMessage()));
 207      }
 208  }
 209  
 210  // OUTPUT
 211  echo $OUTPUT->header();
 212  echo $OUTPUT->heading(get_string('searchcommunitycourse', 'block_community'), 3, 'main');
 213  $hubselectorform->display();
 214  if (!empty($errormessage)) {
 215      echo $errormessage;
 216  }
 217  
 218  //load javascript
 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)
 222  if (!empty($courses)) {
 223      foreach ($courses as $course) {
 224          if (!empty($course['comments'])) {
 225              $commentedcourseids[] = $course['id'];
 226          }
 227          $courseids[] = $course['id'];
 228          $courseimagenumbers[] = $course['screenshots'];
 229      }
 230  }
 231  $PAGE->requires->yui_module('moodle-block_community-comments', 'M.blocks_community.init_comments',
 232          array(array('commentids' => $commentedcourseids, 'closeButtonTitle' => get_string('close', 'editor'))));
 233  $PAGE->requires->yui_module('moodle-block_community-imagegallery', 'M.blocks_community.init_imagegallery',
 234          array(array('imageids' => $courseids, 'imagenumbers' => $courseimagenumbers,
 235                  'huburl' => $huburl, 'closeButtonTitle' => get_string('close', 'editor'))));
 236  
 237  echo highlight($search, $renderer->course_list($courses, $huburl, $courseid));
 238  
 239  //display givememore/Next link if more course can be displayed
 240  if (!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  
 249  echo $OUTPUT->footer();

Search This Site: