PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/blocks/community/forms.php

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