PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/blocks/community/forms.php

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