PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/registration/forms.php

https://bitbucket.org/ngmares/moodle
PHP | 469 lines | 333 code | 77 blank | 59 comment | 19 complexity | 92188f7e90d6810db7f5e8838abe99fb 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 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 (textlib::strlen($hub['name']) > 100) {
  115. $hubname = textlib::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 = format_string($site->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID)));
  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. $emailalert = ($emailalert === 0) ? 0 : 1;
  203. $coursesnumber = get_config('hub', 'site_coursesnumber_' . $cleanhuburl);
  204. $usersnumber = get_config('hub', 'site_usersnumber_' . $cleanhuburl);
  205. $roleassignmentsnumber = get_config('hub', 'site_roleassignmentsnumber_' . $cleanhuburl);
  206. $postsnumber = get_config('hub', 'site_postsnumber_' . $cleanhuburl);
  207. $questionsnumber = get_config('hub', 'site_questionsnumber_' . $cleanhuburl);
  208. $resourcesnumber = get_config('hub', 'site_resourcesnumber_' . $cleanhuburl);
  209. $mediancoursesize = get_config('hub', 'site_mediancoursesize_' . $cleanhuburl);
  210. //hidden parameters
  211. $mform->addElement('hidden', 'huburl', $huburl);
  212. $mform->setType('huburl', PARAM_URL);
  213. $mform->addElement('hidden', 'hubname', $hubname);
  214. $mform->setType('hubname', PARAM_TEXT);
  215. $mform->addElement('hidden', 'password', $password);
  216. $mform->setType('password', PARAM_RAW);
  217. //the input parameters
  218. $mform->addElement('header', 'moodle', get_string('registrationinfo', 'hub'));
  219. $mform->addElement('text', 'name', get_string('sitename', 'hub'),
  220. array('class' => 'registration_textfield'));
  221. $mform->addRule('name', $strrequired, 'required', null, 'client');
  222. $mform->setType('name', PARAM_TEXT);
  223. $mform->setDefault('name', $sitename);
  224. $mform->addHelpButton('name', 'sitename', 'hub');
  225. $options = array();
  226. $registrationmanager = new registration_manager();
  227. $options[HUB_SITENOTPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITENOTPUBLISHED);
  228. $options[HUB_SITENAMEPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITENAMEPUBLISHED);
  229. $options[HUB_SITELINKPUBLISHED] = $registrationmanager->get_site_privacy_string(HUB_SITELINKPUBLISHED);
  230. $mform->addElement('select', 'privacy', get_string('siteprivacy', 'hub'), $options);
  231. $mform->setDefault('privacy', $privacy);
  232. $mform->addHelpButton('privacy', 'privacy', 'hub');
  233. unset($options);
  234. $mform->addElement('textarea', 'description', get_string('sitedesc', 'hub'),
  235. array('rows' => 8, 'cols' => 41));
  236. $mform->addRule('description', $strrequired, 'required', null, 'client');
  237. $mform->setDefault('description', $sitedescription);
  238. $mform->setType('description', PARAM_TEXT);
  239. $mform->addHelpButton('description', 'sitedesc', 'hub');
  240. $languages = get_string_manager()->get_list_of_languages();
  241. collatorlib::asort($languages);
  242. $mform->addElement('select', 'language', get_string('sitelang', 'hub'),
  243. $languages);
  244. $mform->setType('language', PARAM_ALPHANUMEXT);
  245. $mform->addHelpButton('language', 'sitelang', 'hub');
  246. $mform->setDefault('language', $language);
  247. $mform->addElement('textarea', 'address', get_string('postaladdress', 'hub'),
  248. array('rows' => 4, 'cols' => 41));
  249. $mform->setType('address', PARAM_TEXT);
  250. $mform->setDefault('address', $address);
  251. $mform->addHelpButton('address', 'postaladdress', 'hub');
  252. //TODO: use the region array I generated
  253. // $mform->addElement('select', 'region', get_string('selectaregion'), array('-' => '-'));
  254. // $mform->setDefault('region', $region);
  255. $mform->addElement('hidden', 'regioncode', '-');
  256. $mform->setType('regioncode', PARAM_ALPHANUMEXT);
  257. $countries = get_string_manager()->get_list_of_countries();
  258. $mform->addElement('select', 'countrycode', get_string('sitecountry', 'hub'), $countries);
  259. $mform->setDefault('countrycode', $country);
  260. $mform->addHelpButton('countrycode', 'sitecountry', 'hub');
  261. $mform->addElement('text', 'geolocation', get_string('sitegeolocation', 'hub'),
  262. array('class' => 'registration_textfield'));
  263. $mform->setDefault('geolocation', $geolocation);
  264. $mform->addHelpButton('geolocation', 'sitegeolocation', 'hub');
  265. $mform->addElement('text', 'contactname', get_string('siteadmin', 'hub'),
  266. array('class' => 'registration_textfield'));
  267. $mform->addRule('contactname', $strrequired, 'required', null, 'client');
  268. $mform->setType('contactname', PARAM_TEXT);
  269. $mform->setDefault('contactname', $contactname);
  270. $mform->addHelpButton('contactname', 'siteadmin', 'hub');
  271. $mform->addElement('text', 'contactphone', get_string('sitephone', 'hub'),
  272. array('class' => 'registration_textfield'));
  273. $mform->setType('contactphone', PARAM_TEXT);
  274. $mform->addHelpButton('contactphone', 'sitephone', 'hub');
  275. $mform->addElement('text', 'contactemail', get_string('siteemail', 'hub'),
  276. array('class' => 'registration_textfield'));
  277. $mform->addRule('contactemail', $strrequired, 'required', null, 'client');
  278. $mform->setType('contactemail', PARAM_TEXT);
  279. $mform->setDefault('contactemail', $contactemail);
  280. $mform->addHelpButton('contactemail', 'siteemail', 'hub');
  281. $options = array();
  282. $options[0] = get_string("registrationcontactno");
  283. $options[1] = get_string("registrationcontactyes");
  284. $mform->addElement('select', 'contactable', get_string('siteregistrationcontact', 'hub'), $options);
  285. $mform->setDefault('contactable', $contactable);
  286. $mform->addHelpButton('contactable', 'siteregistrationcontact', 'hub');
  287. unset($options);
  288. $options = array();
  289. $options[0] = get_string("registrationno");
  290. $options[1] = get_string("registrationyes");
  291. $mform->addElement('select', 'emailalert', get_string('siteregistrationemail', 'hub'), $options);
  292. $mform->setDefault('emailalert', $emailalert);
  293. $mform->addHelpButton('emailalert', 'siteregistrationemail', 'hub');
  294. unset($options);
  295. //TODO site logo
  296. $mform->addElement('hidden', 'imageurl', ''); //TODO: temporary
  297. $mform->setType('imageurl', PARAM_URL);
  298. $mform->addElement('static', 'urlstring', get_string('siteurl', 'hub'), $CFG->wwwroot);
  299. $mform->addHelpButton('urlstring', 'siteurl', 'hub');
  300. $mform->addElement('static', 'versionstring', get_string('siteversion', 'hub'), $CFG->version);
  301. $mform->addElement('hidden', 'moodleversion', $CFG->version);
  302. $mform->setType('moodleversion', PARAM_INT);
  303. $mform->addHelpButton('versionstring', 'siteversion', 'hub');
  304. $mform->addElement('static', 'releasestring', get_string('siterelease', 'hub'), $CFG->release);
  305. $mform->addElement('hidden', 'moodlerelease', $CFG->release);
  306. $mform->setType('moodlerelease', PARAM_TEXT);
  307. $mform->addHelpButton('releasestring', 'siterelease', 'hub');
  308. /// Display statistic that are going to be retrieve by the hub
  309. $coursecount = $DB->count_records('course') - 1;
  310. $usercount = $DB->count_records('user', array('deleted' => 0));
  311. $roleassigncount = $DB->count_records('role_assignments');
  312. $postcount = $DB->count_records('forum_posts');
  313. $questioncount = $DB->count_records('question');
  314. $resourcecount = $DB->count_records('resource');
  315. require_once($CFG->dirroot . "/course/lib.php");
  316. $participantnumberaverage = number_format(average_number_of_participants(), 2);
  317. $modulenumberaverage = number_format(average_number_of_courses_modules(), 2);
  318. if (HUB_MOODLEORGHUBURL != $huburl) {
  319. $mform->addElement('checkbox', 'courses', get_string('sendfollowinginfo', 'hub'),
  320. " " . get_string('coursesnumber', 'hub', $coursecount));
  321. $mform->setDefault('courses', true);
  322. $mform->addHelpButton('courses', 'sendfollowinginfo', 'hub');
  323. $mform->addElement('checkbox', 'users', '',
  324. " " . get_string('usersnumber', 'hub', $usercount));
  325. $mform->setDefault('users', true);
  326. $mform->addElement('checkbox', 'roleassignments', '',
  327. " " . get_string('roleassignmentsnumber', 'hub', $roleassigncount));
  328. $mform->setDefault('roleassignments', true);
  329. $mform->addElement('checkbox', 'posts', '',
  330. " " . get_string('postsnumber', 'hub', $postcount));
  331. $mform->setDefault('posts', true);
  332. $mform->addElement('checkbox', 'questions', '',
  333. " " . get_string('questionsnumber', 'hub', $questioncount));
  334. $mform->setDefault('questions', true);
  335. $mform->addElement('checkbox', 'resources', '',
  336. " " . get_string('resourcesnumber', 'hub', $resourcecount));
  337. $mform->setDefault('resources', true);
  338. $mform->addElement('checkbox', 'participantnumberaverage', '',
  339. " " . get_string('participantnumberaverage', 'hub', $participantnumberaverage));
  340. $mform->setDefault('participantnumberaverage', true);
  341. $mform->addElement('checkbox', 'modulenumberaverage', '',
  342. " " . get_string('modulenumberaverage', 'hub', $modulenumberaverage));
  343. $mform->setDefault('modulenumberaverage', true);
  344. } else {
  345. $mform->addElement('static', 'courseslabel', get_string('sendfollowinginfo', 'hub'),
  346. " " . get_string('coursesnumber', 'hub', $coursecount));
  347. $mform->addElement('hidden', 'courses', true);
  348. $mform->setType('courses', PARAM_FLOAT);
  349. $mform->addHelpButton('courseslabel', 'sendfollowinginfo', 'hub');
  350. $mform->addElement('static', 'userslabel', '',
  351. " " . get_string('usersnumber', 'hub', $usercount));
  352. $mform->addElement('hidden', 'users', true);
  353. $mform->setType('users', PARAM_FLOAT);
  354. $mform->addElement('static', 'roleassignmentslabel', '',
  355. " " . get_string('roleassignmentsnumber', 'hub', $roleassigncount));
  356. $mform->addElement('hidden', 'roleassignments', true);
  357. $mform->setType('roleassignments', PARAM_FLOAT);
  358. $mform->addElement('static', 'postslabel', '',
  359. " " . get_string('postsnumber', 'hub', $postcount));
  360. $mform->addElement('hidden', 'posts', true);
  361. $mform->setType('posts', PARAM_FLOAT);
  362. $mform->addElement('static', 'questionslabel', '',
  363. " " . get_string('questionsnumber', 'hub', $questioncount));
  364. $mform->addElement('hidden', 'questions', true);
  365. $mform->setType('questions', PARAM_FLOAT);
  366. $mform->addElement('static', 'resourceslabel', '',
  367. " " . get_string('resourcesnumber', 'hub', $resourcecount));
  368. $mform->addElement('hidden', 'resources', true);
  369. $mform->setType('resources', PARAM_FLOAT);
  370. $mform->addElement('static', 'participantnumberaveragelabel', '',
  371. " " . get_string('participantnumberaverage', 'hub', $participantnumberaverage));
  372. $mform->addElement('hidden', 'participantnumberaverage', true);
  373. $mform->setType('participantnumberaverage', PARAM_FLOAT);
  374. $mform->addElement('static', 'modulenumberaveragelabel', '',
  375. " " . get_string('modulenumberaverage', 'hub', $modulenumberaverage));
  376. $mform->addElement('hidden', 'modulenumberaverage', true);
  377. $mform->setType('modulenumberaverage', PARAM_FLOAT);
  378. }
  379. //check if it's a first registration or update
  380. $hubregistered = $registrationmanager->get_registeredhub($huburl);
  381. if (!empty($hubregistered)) {
  382. $buttonlabel = get_string('updatesite', 'hub',
  383. !empty($hubname) ? $hubname : $huburl);
  384. $mform->addElement('hidden', 'update', true);
  385. $mform->setType('update', PARAM_BOOL);
  386. } else {
  387. $buttonlabel = get_string('registersite', 'hub',
  388. !empty($hubname) ? $hubname : $huburl);
  389. }
  390. $this->add_action_buttons(false, $buttonlabel);
  391. }
  392. }