PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/registration/forms.php

https://github.com/mylescarrick/moodle
PHP | 467 lines | 332 code | 76 blank | 59 comment | 19 complexity | 8026df2132282e195d61049b0ecb5448 MD5 | raw file
Possible License(s): GPL-3.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. * @package moodle
  23. * @subpackage registration
  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. * The forms needed by registration pages.
  29. */
  30. require_once($CFG->libdir . '/formslib.php');
  31. require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
  32. /**
  33. * This form display a unregistration form.
  34. */
  35. class site_unregistration_form extends moodleform {
  36. public function definition() {
  37. $mform = & $this->_form;
  38. $mform->addElement('header', 'site', get_string('unregister', 'hub'));
  39. $huburl = $this->_customdata['huburl'];
  40. $hubname = $this->_customdata['hubname'];
  41. $unregisterlabel = get_string('unregister', 'hub');
  42. $mform->addElement('checkbox', 'unpublishalladvertisedcourses', '',
  43. ' ' . get_string('unpublishalladvertisedcourses', 'hub'));
  44. $mform->addElement('checkbox', 'unpublishalluploadedcourses', '',
  45. ' ' . get_string('unpublishalluploadedcourses', 'hub'));
  46. $mform->addElement('hidden', 'confirm', 1);
  47. $mform->setType('confirm', PARAM_INT);
  48. $mform->addElement('hidden', 'unregistration', 1);
  49. $mform->setType('unregistration', PARAM_INT);
  50. $mform->addElement('hidden', 'huburl', $huburl);
  51. $mform->setType('huburl', PARAM_URL);
  52. $mform->addElement('hidden', 'hubname', $hubname);
  53. $mform->setType('hubname', PARAM_TEXT);
  54. $this->add_action_buttons(true, $unregisterlabel);
  55. }
  56. }
  57. /**
  58. * This form display a clean registration data form.
  59. */
  60. class site_clean_registration_data_form extends moodleform {
  61. public function definition() {
  62. $mform = & $this->_form;
  63. $mform->addElement('header', 'site', get_string('unregister', 'hub'));
  64. $huburl = $this->_customdata['huburl'];
  65. $hubname = $this->_customdata['hubname'];
  66. $unregisterlabel = get_string('forceunregister', 'hub');
  67. $mform->addElement('static', '', get_string('warning', 'hub'), get_string('forceunregisterconfirmation', 'hub', $hubname));
  68. $mform->addElement('hidden', 'confirm', 1);
  69. $mform->setType('confirm', PARAM_INT);
  70. $mform->addElement('hidden', 'unregistration', 1);
  71. $mform->setType('unregistration', PARAM_INT);
  72. $mform->addElement('hidden', 'cleanregdata', 1);
  73. $mform->setType('cleanregdata', PARAM_INT);
  74. $mform->addElement('hidden', 'huburl', $huburl);
  75. $mform->setType('huburl', PARAM_URL);
  76. $mform->addElement('hidden', 'hubname', $hubname);
  77. $mform->setType('hubname', PARAM_TEXT);
  78. $this->add_action_buttons(true, $unregisterlabel);
  79. }
  80. }
  81. /**
  82. * This form display a hub selector.
  83. * The hub list is retrieved from Moodle.org hub directory.
  84. * Also displayed, a text field to enter private hub url + its password
  85. */
  86. class hub_selector_form extends moodleform {
  87. public function definition() {
  88. global $CFG, $OUTPUT;
  89. $mform = & $this->_form;
  90. $mform->addElement('header', 'site', get_string('selecthub', 'hub'));
  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. $error = $OUTPUT->notification(get_string('errorhublisting', 'hub', $e->getMessage()));
  101. $mform->addElement('static', 'errorhub', '', $error);
  102. $hubs = array();
  103. }
  104. //remove moodle.org from the hub list
  105. foreach ($hubs as $key => $hub) {
  106. if ($hub['url'] == HUB_MOODLEORGHUBURL) {
  107. unset($hubs[$key]);
  108. }
  109. }
  110. //Public hub list
  111. $options = array();
  112. foreach ($hubs as $hub) {
  113. //to not display a name longer than 100 character (too big)
  114. if (strlen($hub['name']) > 100) {
  115. $hubname = substr($hub['name'], 0, 100);
  116. $hubname = $hubname . "...";
  117. } else {
  118. $hubname = $hub['name'];
  119. }
  120. $options[$hub['url']] = $hubname;
  121. $mform->addElement('hidden', clean_param($hub['url'], PARAM_ALPHANUMEXT), $hubname);
  122. $mform->setType(clean_param($hub['url'], PARAM_ALPHANUMEXT), PARAM_ALPHANUMEXT);
  123. }
  124. if (!empty($hubs)) {
  125. $mform->addElement('select', 'publichub', get_string('publichub', 'hub'),
  126. $options, array("size" => 15));
  127. }
  128. $mform->addElement('static', 'or', '', get_string('orenterprivatehub', 'hub'));
  129. //Private hub
  130. $mform->addElement('text', 'unlistedurl', get_string('privatehuburl', 'hub'),
  131. array('class' => 'registration_textfield'));
  132. $mform->addElement('text', 'password', get_string('password'),
  133. array('class' => 'registration_textfield'));
  134. $this->add_action_buttons(false, get_string('selecthub', 'hub'));
  135. }
  136. /**
  137. * Check the unlisted URL is a URL
  138. */
  139. function validation($data, $files) {
  140. global $CFG;
  141. $errors = parent::validation($data, $files);
  142. $unlistedurl = $this->_form->_submitValues['unlistedurl'];
  143. if (!empty($unlistedurl)) {
  144. $unlistedurltotest = clean_param($unlistedurl, PARAM_URL);
  145. if (empty($unlistedurltotest)) {
  146. $errors['unlistedurl'] = get_string('badurlformat', 'hub');
  147. }
  148. }
  149. return $errors;
  150. }
  151. }
  152. /**
  153. * The site registration form. Information will be sent to a given hub.
  154. */
  155. class site_registration_form extends moodleform {
  156. public function definition() {
  157. global $CFG, $DB;
  158. $strrequired = get_string('required');
  159. $mform = & $this->_form;
  160. $huburl = $this->_customdata['huburl'];
  161. $hubname = $this->_customdata['hubname'];
  162. $password = $this->_customdata['password'];
  163. $admin = get_admin();
  164. $site = get_site();
  165. //retrieve config for this hub and set default if they don't exist
  166. $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT);
  167. $sitename = get_config('hub', 'site_name_' . $cleanhuburl);
  168. if ($sitename === false) {
  169. $sitename = $site->fullname;
  170. }
  171. $sitedescription = get_config('hub', 'site_description_' . $cleanhuburl);
  172. if ($sitedescription === false) {
  173. $sitedescription = $site->summary;
  174. }
  175. $contactname = get_config('hub', 'site_contactname_' . $cleanhuburl);
  176. if ($contactname === false) {
  177. $contactname = fullname($admin, true);
  178. }
  179. $contactemail = get_config('hub', 'site_contactemail_' . $cleanhuburl);
  180. if ($contactemail === false) {
  181. $contactemail = $admin->email;
  182. }
  183. $contactphone = get_config('hub', 'site_contactphone_' . $cleanhuburl);
  184. if ($contactphone === false) {
  185. $contactphone = $admin->phone1;
  186. }
  187. $imageurl = get_config('hub', 'site_imageurl_' . $cleanhuburl);
  188. $privacy = get_config('hub', 'site_privacy_' . $cleanhuburl);
  189. $address = get_config('hub', 'site_address_' . $cleanhuburl);
  190. $region = get_config('hub', 'site_region_' . $cleanhuburl);
  191. $country = get_config('hub', 'site_country_' . $cleanhuburl);
  192. if ($country === false) {
  193. $country = $admin->country;
  194. }
  195. $language = get_config('hub', 'site_language_' . $cleanhuburl);
  196. if ($language === false) {
  197. $language = current_language();
  198. }
  199. $geolocation = get_config('hub', 'site_geolocation_' . $cleanhuburl);
  200. $contactable = get_config('hub', 'site_contactable_' . $cleanhuburl);
  201. $emailalert = get_config('hub', 'site_emailalert_' . $cleanhuburl);
  202. $coursesnumber = get_config('hub', 'site_coursesnumber_' . $cleanhuburl);
  203. $usersnumber = get_config('hub', 'site_usersnumber_' . $cleanhuburl);
  204. $roleassignmentsnumber = get_config('hub', 'site_roleassignmentsnumber_' . $cleanhuburl);
  205. $postsnumber = get_config('hub', 'site_postsnumber_' . $cleanhuburl);
  206. $questionsnumber = get_config('hub', 'site_questionsnumber_' . $cleanhuburl);
  207. $resourcesnumber = get_config('hub', 'site_resourcesnumber_' . $cleanhuburl);
  208. $mediancoursesize = get_config('hub', 'site_mediancoursesize_' . $cleanhuburl);
  209. //hidden parameters
  210. $mform->addElement('hidden', 'huburl', $huburl);
  211. $mform->setType('huburl', PARAM_URL);
  212. $mform->addElement('hidden', 'hubname', $hubname);
  213. $mform->setType('hubname', PARAM_TEXT);
  214. $mform->addElement('hidden', 'password', $password);
  215. $mform->setType('password', PARAM_RAW);
  216. //the input parameters
  217. $mform->addElement('header', 'moodle', get_string('registrationinfo', 'hub'));
  218. $mform->addElement('text', 'name', get_string('sitename', 'hub'),
  219. array('class' => 'registration_textfield'));
  220. $mform->addRule('name', $strrequired, 'required', null, 'client');
  221. $mform->setType('name', PARAM_TEXT);
  222. $mform->setDefault('name', $sitename);
  223. $mform->addHelpButton('name', 'sitename', 'hub');
  224. $options = array();
  225. $registrationmanager = new registration_manager();
  226. $options[HUB_SITENOTPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITENOTPUBLISHED);
  227. $options[HUB_SITENAMEPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITENAMEPUBLISHED);
  228. $options[HUB_SITELINKPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITELINKPUBLISHED);
  229. $mform->addElement('select', 'privacy', get_string('siteprivacy', 'hub'), $options);
  230. $mform->setDefault('privacy', $privacy);
  231. $mform->addHelpButton('privacy', 'privacy', 'hub');
  232. unset($options);
  233. $mform->addElement('textarea', 'description', get_string('sitedesc', 'hub'),
  234. array('rows' => 8, 'cols' => 41));
  235. $mform->addRule('description', $strrequired, 'required', null, 'client');
  236. $mform->setDefault('description', $sitedescription);
  237. $mform->setType('description', PARAM_TEXT);
  238. $mform->addHelpButton('description', 'sitedesc', 'hub');
  239. $mform->addElement('static', 'urlstring', get_string('siteurl', 'hub'), $CFG->wwwroot);
  240. $mform->addHelpButton('urlstring', 'siteurl', 'hub');
  241. $languages = get_string_manager()->get_list_of_languages();
  242. textlib_get_instance()->asort($languages);
  243. $mform->addElement('select', 'language', get_string('sitelang', 'hub'),
  244. $languages);
  245. $mform->setType('language', PARAM_ALPHANUMEXT);
  246. $mform->addHelpButton('language', 'sitelang', 'hub');
  247. $mform->setDefault('language', $language);
  248. $mform->addElement('static', 'versionstring', get_string('siteversion', 'hub'), $CFG->version);
  249. $mform->addElement('hidden', 'moodleversion', $CFG->version);
  250. $mform->setType('moodleversion', PARAM_INT);
  251. $mform->addHelpButton('versionstring', 'siteversion', 'hub');
  252. $mform->addElement('static', 'releasestring', get_string('siterelease', 'hub'), $CFG->release);
  253. $mform->addElement('hidden', 'moodlerelease', $CFG->release);
  254. $mform->setType('moodlerelease', PARAM_TEXT);
  255. $mform->addHelpButton('releasestring', 'siterelease', 'hub');
  256. $mform->addElement('textarea', 'address', get_string('postaladdress', 'hub'),
  257. array('rows' => 4, 'cols' => 41));
  258. $mform->setType('address', PARAM_TEXT);
  259. $mform->setDefault('address', $address);
  260. $mform->addHelpButton('address', 'postaladdress', 'hub');
  261. //TODO: use the region array I generated
  262. // $mform->addElement('select', 'region', get_string('selectaregion'), array('-' => '-'));
  263. // $mform->setDefault('region', $region);
  264. $mform->addElement('hidden', 'regioncode', '-');
  265. $mform->setType('regioncode', PARAM_ALPHANUMEXT);
  266. $countries = get_string_manager()->get_list_of_countries();
  267. $mform->addElement('select', 'countrycode', get_string('sitecountry', 'hub'), $countries);
  268. $mform->setDefault('countrycode', $country);
  269. $mform->addHelpButton('countrycode', 'sitecountry', 'hub');
  270. $mform->addElement('text', 'geolocation', get_string('sitegeolocation', 'hub'),
  271. array('class' => 'registration_textfield'));
  272. $mform->setDefault('geolocation', $geolocation);
  273. $mform->addHelpButton('geolocation', 'sitegeolocation', 'hub');
  274. $mform->addElement('text', 'contactname', get_string('siteadmin', 'hub'),
  275. array('class' => 'registration_textfield'));
  276. $mform->addRule('contactname', $strrequired, 'required', null, 'client');
  277. $mform->setType('contactname', PARAM_TEXT);
  278. $mform->setDefault('contactname', $contactname);
  279. $mform->addHelpButton('contactname', 'siteadmin', 'hub');
  280. $mform->addElement('text', 'contactphone', get_string('sitephone', 'hub'),
  281. array('class' => 'registration_textfield'));
  282. $mform->setType('contactphone', PARAM_TEXT);
  283. $mform->addHelpButton('contactphone', 'sitephone', 'hub');
  284. $mform->addElement('text', 'contactemail', get_string('siteemail', 'hub'),
  285. array('class' => 'registration_textfield'));
  286. $mform->addRule('contactemail', $strrequired, 'required', null, 'client');
  287. $mform->setType('contactemail', PARAM_TEXT);
  288. $mform->setDefault('contactemail', $contactemail);
  289. $mform->addHelpButton('contactemail', 'siteemail', 'hub');
  290. $options = array();
  291. $options[0] = get_string("registrationcontactno");
  292. $options[1] = get_string("registrationcontactyes");
  293. $mform->addElement('select', 'contactable', get_string('siteregistrationcontact', 'hub'), $options);
  294. $mform->setDefault('contactable', $contactable);
  295. $mform->addHelpButton('contactable', 'siteregistrationcontact', 'hub');
  296. unset($options);
  297. $options = array();
  298. $options[0] = get_string("registrationno");
  299. $options[1] = get_string("registrationyes");
  300. $mform->addElement('select', 'emailalert', get_string('siteregistrationemail', 'hub'), $options);
  301. $mform->setDefault('emailalert', $emailalert);
  302. $mform->addHelpButton('emailalert', 'siteregistrationemail', 'hub');
  303. unset($options);
  304. //TODO site logo
  305. $mform->addElement('hidden', 'imageurl', ''); //TODO: temporary
  306. $mform->setType('imageurl', PARAM_URL);
  307. /// Display statistic that are going to be retrieve by the hub
  308. $coursecount = $DB->count_records('course') - 1;
  309. $usercount = $DB->count_records('user', array('deleted' => 0));
  310. $roleassigncount = $DB->count_records('role_assignments');
  311. $postcount = $DB->count_records('forum_posts');
  312. $questioncount = $DB->count_records('question');
  313. $resourcecount = $DB->count_records('resource');
  314. require_once($CFG->dirroot . "/course/lib.php");
  315. $participantnumberaverage = number_format(average_number_of_participants(), 2);
  316. $modulenumberaverage = number_format(average_number_of_courses_modules(), 2);
  317. if (HUB_MOODLEORGHUBURL != $huburl) {
  318. $mform->addElement('checkbox', 'courses', get_string('sendfollowinginfo', 'hub'),
  319. " " . get_string('coursesnumber', 'hub', $coursecount));
  320. $mform->setDefault('courses', true);
  321. $mform->addHelpButton('courses', 'sendfollowinginfo', 'hub');
  322. $mform->addElement('checkbox', 'users', '',
  323. " " . get_string('usersnumber', 'hub', $usercount));
  324. $mform->setDefault('users', true);
  325. $mform->addElement('checkbox', 'roleassignments', '',
  326. " " . get_string('roleassignmentsnumber', 'hub', $roleassigncount));
  327. $mform->setDefault('roleassignments', true);
  328. $mform->addElement('checkbox', 'posts', '',
  329. " " . get_string('postsnumber', 'hub', $postcount));
  330. $mform->setDefault('posts', true);
  331. $mform->addElement('checkbox', 'questions', '',
  332. " " . get_string('questionsnumber', 'hub', $questioncount));
  333. $mform->setDefault('questions', true);
  334. $mform->addElement('checkbox', 'resources', '',
  335. " " . get_string('resourcesnumber', 'hub', $resourcecount));
  336. $mform->setDefault('resources', true);
  337. $mform->addElement('checkbox', 'participantnumberaverage', '',
  338. " " . get_string('participantnumberaverage', 'hub', $participantnumberaverage));
  339. $mform->setDefault('participantnumberaverage', true);
  340. $mform->addElement('checkbox', 'modulenumberaverage', '',
  341. " " . get_string('modulenumberaverage', 'hub', $modulenumberaverage));
  342. $mform->setDefault('modulenumberaverage', true);
  343. } else {
  344. $mform->addElement('static', 'courseslabel', get_string('sendfollowinginfo', 'hub'),
  345. " " . get_string('coursesnumber', 'hub', $coursecount));
  346. $mform->addElement('hidden', 'courses', true);
  347. $mform->setType('courses', PARAM_FLOAT);
  348. $mform->addHelpButton('courseslabel', 'sendfollowinginfo', 'hub');
  349. $mform->addElement('static', 'userslabel', '',
  350. " " . get_string('usersnumber', 'hub', $usercount));
  351. $mform->addElement('hidden', 'users', true);
  352. $mform->setType('users', PARAM_FLOAT);
  353. $mform->addElement('static', 'roleassignmentslabel', '',
  354. " " . get_string('roleassignmentsnumber', 'hub', $roleassigncount));
  355. $mform->addElement('hidden', 'roleassignments', true);
  356. $mform->setType('roleassignments', PARAM_FLOAT);
  357. $mform->addElement('static', 'postslabel', '',
  358. " " . get_string('postsnumber', 'hub', $postcount));
  359. $mform->addElement('hidden', 'posts', true);
  360. $mform->setType('posts', PARAM_FLOAT);
  361. $mform->addElement('static', 'questionslabel', '',
  362. " " . get_string('questionsnumber', 'hub', $questioncount));
  363. $mform->addElement('hidden', 'questions', true);
  364. $mform->setType('questions', PARAM_FLOAT);
  365. $mform->addElement('static', 'resourceslabel', '',
  366. " " . get_string('resourcesnumber', 'hub', $resourcecount));
  367. $mform->addElement('hidden', 'resources', true);
  368. $mform->setType('resources', PARAM_FLOAT);
  369. $mform->addElement('static', 'participantnumberaveragelabel', '',
  370. " " . get_string('participantnumberaverage', 'hub', $participantnumberaverage));
  371. $mform->addElement('hidden', 'participantnumberaverage', true);
  372. $mform->setType('participantnumberaverage', PARAM_FLOAT);
  373. $mform->addElement('static', 'modulenumberaveragelabel', '',
  374. " " . get_string('modulenumberaverage', 'hub', $modulenumberaverage));
  375. $mform->addElement('hidden', 'modulenumberaverage', true);
  376. $mform->setType('modulenumberaverage', PARAM_FLOAT);
  377. }
  378. //check if it's a first registration or update
  379. $hubregistered = $registrationmanager->get_registeredhub($huburl);
  380. if (!empty($hubregistered)) {
  381. $buttonlabel = get_string('updatesite', 'hub',
  382. !empty($hubname) ? $hubname : $huburl);
  383. $mform->addElement('hidden', 'update', true);
  384. $mform->setType('update', PARAM_BOOL);
  385. } else {
  386. $buttonlabel = get_string('registersite', 'hub',
  387. !empty($hubname) ? $hubname : $huburl);
  388. }
  389. $this->add_action_buttons(false, $buttonlabel);
  390. }
  391. }