[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/blocks/community/ -> forms.php (source)

   1  <?php
   2  ///////////////////////////////////////////////////////////////////////////
   3  //                                                                       //
   4  // This file is part of Moodle - http://moodle.org/                      //
   5  // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
   6  //                                                                       //
   7  // Moodle is free software: you can redistribute it and/or modify        //
   8  // it under the terms of the GNU General Public License as published by  //
   9  // the Free Software Foundation, either version 3 of the License, or     //
  10  // (at your option) any later version.                                   //
  11  //                                                                       //
  12  // Moodle is distributed in the hope that it will be useful,             //
  13  // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
  14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
  15  // GNU General Public License for more details.                          //
  16  //                                                                       //
  17  // You should have received a copy of the GNU General Public License     //
  18  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.       //
  19  //                                                                       //
  20  ///////////////////////////////////////////////////////////////////////////
  21  
  22  /**
  23   * Form for community search
  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_once($CFG->libdir . '/formslib.php');
  32  require_once($CFG->dirroot . '/course/publish/lib.php');
  33  require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
  34  
  35  class community_hub_search_form extends moodleform {
  36  
  37      public function definition() {
  38          global $CFG, $USER, $OUTPUT;
  39          $strrequired = get_string('required');
  40          $mform = & $this->_form;
  41  
  42          //set default value
  43          $search = $this->_customdata['search'];
  44          if (isset($this->_customdata['coverage'])) {
  45              $coverage = $this->_customdata['coverage'];
  46          } else {
  47              $coverage = 'all';
  48          }
  49          if (isset($this->_customdata['licence'])) {
  50              $licence = $this->_customdata['licence'];
  51          } else {
  52              $licence = 'all';
  53          }
  54          if (isset($this->_customdata['subject'])) {
  55              $subject = $this->_customdata['subject'];
  56          } else {
  57              $subject = 'all';
  58          }
  59          if (isset($this->_customdata['audience'])) {
  60              $audience = $this->_customdata['audience'];
  61          } else {
  62              $audience = 'all';
  63          }
  64          if (isset($this->_customdata['language'])) {
  65              $language = $this->_customdata['language'];
  66          } else {
  67              $language = current_language();
  68          }
  69          if (isset($this->_customdata['educationallevel'])) {
  70              $educationallevel = $this->_customdata['educationallevel'];
  71          } else {
  72              $educationallevel = 'all';
  73          }
  74          if (isset($this->_customdata['downloadable'])) {
  75              $downloadable = $this->_customdata['downloadable'];
  76          } else {
  77              $downloadable = 1;
  78          }
  79          if (isset($this->_customdata['orderby'])) {
  80              $orderby = $this->_customdata['orderby'];
  81          } else {
  82              $orderby = 'newest';
  83          }
  84          if (isset($this->_customdata['huburl'])) {
  85              $huburl = $this->_customdata['huburl'];
  86          } else {
  87              $huburl = HUB_MOODLEORGHUBURL;
  88          }
  89  
  90          $mform->addElement('header', 'site', get_string('search', 'block_community'));
  91  
  92          //add the course id (of the context)
  93          $mform->addElement('hidden', 'courseid', $this->_customdata['courseid']);
  94          $mform->setType('courseid', PARAM_INT);
  95          $mform->addElement('hidden', 'executesearch', 1);
  96          $mform->setType('executesearch', PARAM_INT);
  97  
  98          //retrieve the hub list on the hub directory by web service
  99          $function = 'hubdirectory_get_hubs';
 100          $params = array();
 101          $serverurl = HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/webservices.php";
 102          require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
 103          $xmlrpcclient = new webservice_xmlrpc_client($serverurl, 'publichubdirectory');
 104          try {
 105              $hubs = $xmlrpcclient->call($function, $params);
 106          } catch (Exception $e) {
 107              $hubs = array();
 108              $error = $OUTPUT->notification(get_string('errorhublisting', 'block_community', $e->getMessage()));
 109              $mform->addElement('static', 'errorhub', '', $error);
 110          }
 111  
 112          //display list of registered on hub
 113          $registrationmanager = new registration_manager();
 114          $registeredhubs = $registrationmanager->get_registered_on_hubs();
 115          //retrieve some additional hubs that we will add to
 116          //the hub list got from the hub directory
 117          $additionalhubs = array();
 118          foreach ($registeredhubs as $registeredhub) {
 119              $inthepubliclist = false;
 120              foreach ($hubs as $hub) {
 121                  if ($hub['url'] == $registeredhub->huburl) {
 122                      $inthepubliclist = true;
 123                      $hub['registeredon'] = true;
 124                  }
 125              }
 126              if (!$inthepubliclist) {
 127                  $additionalhub = array();
 128                  $additionalhub['name'] = $registeredhub->hubname;
 129                  $additionalhub['url'] = $registeredhub->huburl;
 130                  $additionalhubs[] = $additionalhub;
 131              }
 132          }
 133          if (!empty($additionalhubs)) {
 134              $hubs = array_merge($hubs, $additionalhubs);
 135          }
 136  
 137          if (!empty($hubs)) {
 138              $htmlhubs = array();
 139              foreach ($hubs as $hub) {
 140                  // Name can come from hub directory - need some cleaning.
 141                  $hubname = clean_text($hub['name'], PARAM_TEXT);
 142                  $smalllogohtml = '';
 143                  if (array_key_exists('id', $hub)) {
 144  
 145                      // Retrieve hub logo + generate small logo.
 146                      $params = array('hubid' => $hub['id'], 'filetype' => HUB_HUBSCREENSHOT_FILE_TYPE);
 147                      $imgurl = new moodle_url(HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/download.php", $params);
 148                      $imgsize = getimagesize($imgurl->out(false));
 149                      if ($imgsize[0] > 1) {
 150                          $ascreenshothtml = html_writer::empty_tag('img', array('src' => $imgurl, 'alt' => $hubname));
 151                          $smalllogohtml = html_writer::empty_tag('img', array('src' => $imgurl, 'alt' => $hubname
 152                                          , 'height' => 30, 'width' => 40));
 153                      } else {
 154                          $ascreenshothtml = '';
 155                      }
 156                      $hubimage = html_writer::tag('div', $ascreenshothtml, array('class' => 'hubimage'));
 157  
 158                      // Statistics + trusted info.
 159                      $hubstats = '';
 160                      if (isset($hub['enrollablecourses'])) { //check needed to avoid warnings for Moodle version < 2011081700
 161                          $additionaldesc = get_string('enrollablecourses', 'block_community') . ': ' . $hub['enrollablecourses'] . ' - ' .
 162                                  get_string('downloadablecourses', 'block_community') . ': ' . $hub['downloadablecourses'];
 163                          $hubstats .= html_writer::tag('div', $additionaldesc);
 164                      }
 165                      if ($hub['trusted']) {
 166                          $hubtrusted =  get_string('hubtrusted', 'block_community');
 167                          $hubstats .= $OUTPUT->doc_link('trusted_hubs') . html_writer::tag('div', $hubtrusted);
 168                      }
 169                      $hubstats = html_writer::tag('div', $hubstats, array('class' => 'hubstats'));
 170  
 171                      // hub name link + hub description.
 172                      $hubnamelink = html_writer::link($hub['url'], html_writer::tag('h2',$hubname),
 173                                      array('class' => 'hubtitlelink'));
 174                      // The description can come from the hub directory - need to clean.
 175                      $hubdescription = clean_param($hub['description'], PARAM_TEXT);
 176                      $hubdescriptiontext = html_writer::tag('div', format_text($hubdescription, FORMAT_PLAIN),
 177                                      array('class' => 'hubdescription'));
 178  
 179                      $hubtext = html_writer::tag('div', $hubdescriptiontext . $hubstats, array('class' => 'hubtext'));
 180  
 181                      $hubimgandtext = html_writer::tag('div', $hubimage . $hubtext, array('class' => 'hubimgandtext'));
 182  
 183                      $hubfulldesc = html_writer::tag('div', $hubnamelink . $hubimgandtext, array('class' => 'hubmainhmtl'));
 184                  } else {
 185                      $hubfulldesc = html_writer::link($hub['url'], $hubname);
 186                  }
 187  
 188                  // Add hub to the hub items.
 189                  $hubinfo = new stdClass();
 190                  $hubinfo->mainhtml = $hubfulldesc;
 191                  $hubinfo->rowhtml = html_writer::tag('div', $smalllogohtml , array('class' => 'hubsmalllogo')) . $hubname;
 192                  $hubitems[$hub['url']] = $hubinfo;
 193              }
 194  
 195              // Hub listing form element.
 196              $mform->addElement('listing','huburl', '', '', array('items' => $hubitems,
 197                  'showall' => get_string('showall', 'block_community'),
 198                  'hideall' => get_string('hideall', 'block_community')));
 199              $mform->setDefault('huburl', $huburl);
 200  
 201              //display enrol/download select box if the USER has the download capability on the course
 202              if (has_capability('moodle/community:download',
 203                              context_course::instance($this->_customdata['courseid']))) {
 204                  $options = array(0 => get_string('enrollable', 'block_community'),
 205                      1 => get_string('downloadable', 'block_community'));
 206                  $mform->addElement('select', 'downloadable', get_string('enroldownload', 'block_community'),
 207                          $options);
 208                  $mform->addHelpButton('downloadable', 'enroldownload', 'block_community');
 209  
 210                  $mform->setDefault('downloadable', $downloadable);
 211              } else {
 212                  $mform->addElement('hidden', 'downloadable', 0);
 213              }
 214              $mform->setType('downloadable', PARAM_INT);
 215  
 216              $options = array();
 217              $options['all'] = get_string('any');
 218              $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
 219              $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
 220              $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
 221              $mform->addElement('select', 'audience', get_string('audience', 'block_community'), $options);
 222              $mform->setDefault('audience', $audience);
 223              unset($options);
 224              $mform->addHelpButton('audience', 'audience', 'block_community');
 225  
 226              $options = array();
 227              $options['all'] = get_string('any');
 228              $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
 229              $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
 230              $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
 231              $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
 232              $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
 233              $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
 234              $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
 235              $mform->addElement('select', 'educationallevel',
 236                      get_string('educationallevel', 'block_community'), $options);
 237              $mform->setDefault('educationallevel', $educationallevel);
 238              unset($options);
 239              $mform->addHelpButton('educationallevel', 'educationallevel', 'block_community');
 240  
 241              $publicationmanager = new course_publish_manager();
 242              $options = $publicationmanager->get_sorted_subjects();
 243              foreach ($options as $key => &$option) {
 244                  $keylength = strlen($key);
 245                  if ($keylength == 10) {
 246                      $option = "&nbsp;&nbsp;" . $option;
 247                  } else if ($keylength == 12) {
 248                      $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
 249                  }
 250              }
 251              $options = array_merge(array('all' => get_string('any')), $options);
 252              $mform->addElement('select', 'subject', get_string('subject', 'block_community'),
 253                      $options, array('id' => 'communitysubject'));
 254              $mform->setDefault('subject', $subject);
 255              unset($options);
 256              $mform->addHelpButton('subject', 'subject', 'block_community');
 257              $this->init_javascript_enhancement('subject', 'smartselect',
 258                      array('selectablecategories' => true, 'mode' => 'compact'));
 259  
 260              require_once($CFG->libdir . "/licenselib.php");
 261              $licensemanager = new license_manager();
 262              $licences = $licensemanager->get_licenses();
 263              $options = array();
 264              $options['all'] = get_string('any');
 265              foreach ($licences as $license) {
 266                  $options[$license->shortname] = get_string($license->shortname, 'license');
 267              }
 268              $mform->addElement('select', 'licence', get_string('licence', 'block_community'), $options);
 269              unset($options);
 270              $mform->addHelpButton('licence', 'licence', 'block_community');
 271              $mform->setDefault('licence', $licence);
 272  
 273              $languages = get_string_manager()->get_list_of_languages();
 274              core_collator::asort($languages);
 275              $languages = array_merge(array('all' => get_string('any')), $languages);
 276              $mform->addElement('select', 'language', get_string('language'), $languages);
 277  
 278              $mform->setDefault('language', $language);
 279              $mform->addHelpButton('language', 'language', 'block_community');
 280  
 281              $mform->addElement('select', 'orderby', get_string('orderby', 'block_community'),
 282                  array('newest' => get_string('orderbynewest', 'block_community'),
 283                      'eldest' => get_string('orderbyeldest', 'block_community'),
 284                      'fullname' => get_string('orderbyname', 'block_community'),
 285                      'publisher' => get_string('orderbypublisher', 'block_community'),
 286                      'ratingaverage' => get_string('orderbyratingaverage', 'block_community')));
 287  
 288              $mform->setDefault('orderby', $orderby);
 289              $mform->addHelpButton('orderby', 'orderby', 'block_community');
 290              $mform->setType('orderby', PARAM_ALPHA);
 291  
 292              $mform->setAdvanced('audience');
 293              $mform->setAdvanced('educationallevel');
 294              $mform->setAdvanced('subject');
 295              $mform->setAdvanced('licence');
 296              $mform->setAdvanced('language');
 297              $mform->setAdvanced('orderby');
 298  
 299              $mform->addElement('text', 'search', get_string('keywords', 'block_community'),
 300                  array('size' => 30));
 301              $mform->addHelpButton('search', 'keywords', 'block_community');
 302              $mform->setType('search', PARAM_NOTAGS);
 303  
 304              $mform->addElement('submit', 'submitbutton', get_string('search', 'block_community'));
 305          }
 306      }
 307  
 308      function validation($data, $files) {
 309          global $CFG;
 310  
 311          $errors = array();
 312  
 313          if (empty($this->_form->_submitValues['huburl'])) {
 314              $errors['huburl'] = get_string('nohubselected', 'hub');
 315          }
 316  
 317          return $errors;
 318      }
 319  
 320  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1