PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/blocks/community/forms.php

https://bitbucket.org/ngmares/moodle
PHP | 301 lines | 234 code | 30 blank | 37 comment | 37 complexity | 2a3af0689c5e6ef2d8931ee25a60c9d0 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  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. * @package blocks
  23. * @subpackage community
  24. * @author Jerome Mouneyrac <jerome@mouneyrac.com>
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
  26. * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
  27. *
  28. * Form for community search
  29. */
  30. require_once($CFG->libdir . '/formslib.php');
  31. require_once($CFG->dirroot . '/course/publish/lib.php');
  32. require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
  33. class community_hub_search_form extends moodleform {
  34. public function definition() {
  35. global $CFG, $USER, $OUTPUT;
  36. $strrequired = get_string('required');
  37. $mform = & $this->_form;
  38. //set default value
  39. $search = $this->_customdata['search'];
  40. if (isset($this->_customdata['coverage'])) {
  41. $coverage = $this->_customdata['coverage'];
  42. } else {
  43. $coverage = 'all';
  44. }
  45. if (isset($this->_customdata['licence'])) {
  46. $licence = $this->_customdata['licence'];
  47. } else {
  48. $licence = 'all';
  49. }
  50. if (isset($this->_customdata['subject'])) {
  51. $subject = $this->_customdata['subject'];
  52. } else {
  53. $subject = 'all';
  54. }
  55. if (isset($this->_customdata['audience'])) {
  56. $audience = $this->_customdata['audience'];
  57. } else {
  58. $audience = 'all';
  59. }
  60. if (isset($this->_customdata['language'])) {
  61. $language = $this->_customdata['language'];
  62. } else {
  63. $language = current_language();
  64. }
  65. if (isset($this->_customdata['educationallevel'])) {
  66. $educationallevel = $this->_customdata['educationallevel'];
  67. } else {
  68. $educationallevel = 'all';
  69. }
  70. if (isset($this->_customdata['downloadable'])) {
  71. $downloadable = $this->_customdata['downloadable'];
  72. } else {
  73. $downloadable = 0;
  74. }
  75. if (isset($this->_customdata['orderby'])) {
  76. $orderby = $this->_customdata['orderby'];
  77. } else {
  78. $orderby = 'newest';
  79. }
  80. if (isset($this->_customdata['huburl'])) {
  81. $huburl = $this->_customdata['huburl'];
  82. } else {
  83. $huburl = HUB_MOODLEORGHUBURL;
  84. }
  85. $mform->addElement('header', 'site', get_string('search', 'block_community'));
  86. //add the course id (of the context)
  87. $mform->addElement('hidden', 'courseid', $this->_customdata['courseid']);
  88. $mform->addElement('hidden', 'executesearch', 1);
  89. //retrieve the hub list on the hub directory by web service
  90. $function = 'hubdirectory_get_hubs';
  91. $params = array();
  92. $serverurl = HUB_HUBDIRECTORYURL . "/local/hubdirectory/webservice/webservices.php";
  93. require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
  94. $xmlrpcclient = new webservice_xmlrpc_client($serverurl, 'publichubdirectory');
  95. try {
  96. $hubs = $xmlrpcclient->call($function, $params);
  97. } catch (Exception $e) {
  98. $hubs = array();
  99. $error = $OUTPUT->notification(get_string('errorhublisting', 'block_community', $e->getMessage()));
  100. $mform->addElement('static', 'errorhub', '', $error);
  101. }
  102. //display list of registered on hub
  103. $registrationmanager = new registration_manager();
  104. $registeredhubs = $registrationmanager->get_registered_on_hubs();
  105. //retrieve some additional hubs that we will add to
  106. //the hub list got from the hub directory
  107. $additionalhubs = array();
  108. foreach ($registeredhubs as $registeredhub) {
  109. $inthepubliclist = false;
  110. foreach ($hubs as $hub) {
  111. if ($hub['url'] == $registeredhub->huburl) {
  112. $inthepubliclist = true;
  113. $hub['registeredon'] = true;
  114. }
  115. }
  116. if (!$inthepubliclist) {
  117. $additionalhub = array();
  118. $additionalhub['name'] = $registeredhub->hubname;
  119. $additionalhub['url'] = $registeredhub->huburl;
  120. $additionalhubs[] = $additionalhub;
  121. }
  122. }
  123. if (!empty($additionalhubs)) {
  124. $hubs = array_merge($hubs, $additionalhubs);
  125. }
  126. if (!empty($hubs)) {
  127. //TODO: sort hubs by trusted/prioritize
  128. //Public hub list
  129. $options = array();
  130. $firsthub = false;
  131. foreach ($hubs as $hub) {
  132. if (key_exists('id', $hub)) {
  133. $params = array('hubid' => $hub['id'],
  134. 'filetype' => HUB_HUBSCREENSHOT_FILE_TYPE);
  135. $imgurl = new moodle_url(HUB_HUBDIRECTORYURL .
  136. "/local/hubdirectory/webservice/download.php", $params);
  137. $ascreenshothtml = html_writer::empty_tag('img',
  138. array('src' => $imgurl, 'alt' => $hub['name']));
  139. $hubdescription = html_writer::tag('a', $hub['name'],
  140. array('class' => 'hublink clearfix', 'href' => $hub['url'],
  141. 'onclick' => 'this.target="_blank"'));
  142. $hubdescription .= html_writer::tag('span', $ascreenshothtml,
  143. array('class' => 'hubscreenshot'));
  144. $hubdescriptiontext = html_writer::tag('span', format_text($hub['description'], FORMAT_PLAIN),
  145. array('class' => 'hubdescription'));
  146. if (isset($hub['enrollablecourses'])) { //check needed to avoid warnings for Moodle version < 2011081700
  147. $additionaldesc = get_string('enrollablecourses', 'block_community') . ': ' . $hub['enrollablecourses'] . ' - ' .
  148. get_string('downloadablecourses', 'block_community') . ': ' . $hub['downloadablecourses'];
  149. $hubdescriptiontext .= html_writer::tag('span', $additionaldesc,
  150. array('class' => 'hubadditionaldesc'));
  151. }
  152. if ($hub['trusted']) {
  153. $hubtrusted = get_string('hubtrusted', 'block_community');
  154. $hubdescriptiontext .= html_writer::tag('span',
  155. $hubtrusted . ' ' . $OUTPUT->doc_link('trusted_hubs'),
  156. array('class' => 'trusted'));
  157. }
  158. $hubdescriptiontext = html_writer::tag('span', $hubdescriptiontext,
  159. array('class' => 'hubdescriptiontext'));
  160. $hubdescription = html_writer::tag('span',
  161. $hubdescription . $hubdescriptiontext,
  162. array('class' => $hub['trusted'] ? 'hubtrusted' : 'hubnottrusted'));
  163. } else {
  164. $hubdescription = html_writer::tag('a', $hub['name'],
  165. array('class' => 'hublink hubtrusted', 'href' => $hub['url']));
  166. }
  167. if (empty($firsthub)) {
  168. $mform->addElement('radio', 'huburl', get_string('selecthub', 'block_community'),
  169. $hubdescription, $hub['url']);
  170. $mform->setDefault('huburl', $huburl);
  171. $firsthub = true;
  172. } else {
  173. $mform->addElement('radio', 'huburl', '', $hubdescription, $hub['url']);
  174. }
  175. }
  176. //display enrol/download select box if the USER has the download capability on the course
  177. if (has_capability('moodle/community:download',
  178. get_context_instance(CONTEXT_COURSE, $this->_customdata['courseid']))) {
  179. $options = array(0 => get_string('enrollable', 'block_community'),
  180. 1 => get_string('downloadable', 'block_community'));
  181. $mform->addElement('select', 'downloadable', get_string('enroldownload', 'block_community'),
  182. $options);
  183. $mform->addHelpButton('downloadable', 'enroldownload', 'block_community');
  184. } else {
  185. $mform->addElement('hidden', 'downloadable', 0);
  186. }
  187. $options = array();
  188. $options['all'] = get_string('any');
  189. $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
  190. $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
  191. $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
  192. $mform->addElement('select', 'audience', get_string('audience', 'block_community'), $options);
  193. $mform->setDefault('audience', $audience);
  194. unset($options);
  195. $mform->addHelpButton('audience', 'audience', 'block_community');
  196. $options = array();
  197. $options['all'] = get_string('any');
  198. $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
  199. $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
  200. $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
  201. $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
  202. $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
  203. $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
  204. $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
  205. $mform->addElement('select', 'educationallevel',
  206. get_string('educationallevel', 'block_community'), $options);
  207. $mform->setDefault('educationallevel', $educationallevel);
  208. unset($options);
  209. $mform->addHelpButton('educationallevel', 'educationallevel', 'block_community');
  210. $publicationmanager = new course_publish_manager();
  211. $options = $publicationmanager->get_sorted_subjects();
  212. foreach ($options as $key => &$option) {
  213. $keylength = strlen($key);
  214. if ($keylength == 10) {
  215. $option = "&nbsp;&nbsp;" . $option;
  216. } else if ($keylength == 12) {
  217. $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
  218. }
  219. }
  220. $options = array_merge(array('all' => get_string('any')), $options);
  221. $mform->addElement('select', 'subject', get_string('subject', 'block_community'),
  222. $options, array('id' => 'communitysubject'));
  223. $mform->setDefault('subject', $subject);
  224. unset($options);
  225. $mform->addHelpButton('subject', 'subject', 'block_community');
  226. $this->init_javascript_enhancement('subject', 'smartselect',
  227. array('selectablecategories' => true, 'mode' => 'compact'));
  228. require_once($CFG->libdir . "/licenselib.php");
  229. $licensemanager = new license_manager();
  230. $licences = $licensemanager->get_licenses();
  231. $options = array();
  232. $options['all'] = get_string('any');
  233. foreach ($licences as $license) {
  234. $options[$license->shortname] = get_string($license->shortname, 'license');
  235. }
  236. $mform->addElement('select', 'licence', get_string('licence', 'block_community'), $options);
  237. unset($options);
  238. $mform->addHelpButton('licence', 'licence', 'block_community');
  239. $mform->setDefault('licence', $licence);
  240. $languages = get_string_manager()->get_list_of_languages();
  241. collatorlib::asort($languages);
  242. $languages = array_merge(array('all' => get_string('any')), $languages);
  243. $mform->addElement('select', 'language', get_string('language'), $languages);
  244. $mform->setDefault('language', $language);
  245. $mform->addHelpButton('language', 'language', 'block_community');
  246. $mform->addElement('radio', 'orderby', get_string('orderby', 'block_community'),
  247. get_string('orderbynewest', 'block_community'), 'newest');
  248. $mform->addElement('radio', 'orderby', null,
  249. get_string('orderbyeldest', 'block_community'), 'eldest');
  250. $mform->addElement('radio', 'orderby', null,
  251. get_string('orderbyname', 'block_community'), 'fullname');
  252. $mform->addElement('radio', 'orderby', null,
  253. get_string('orderbypublisher', 'block_community'), 'publisher');
  254. $mform->addElement('radio', 'orderby', null,
  255. get_string('orderbyratingaverage', 'block_community'), 'ratingaverage');
  256. $mform->setDefault('orderby', $orderby);
  257. $mform->setType('orderby', PARAM_ALPHA);
  258. $mform->addElement('text', 'search', get_string('keywords', 'block_community'));
  259. $mform->addHelpButton('search', 'keywords', 'block_community');
  260. $mform->addElement('submit', 'submitbutton', get_string('search', 'block_community'));
  261. }
  262. }
  263. function validation($data, $files) {
  264. global $CFG;
  265. $errors = array();
  266. if (empty($this->_form->_submitValues['huburl'])) {
  267. $errors['huburl'] = get_string('nohubselected', 'hub');
  268. }
  269. return $errors;
  270. }
  271. }