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

/admin/roles/classes/preset_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 120 lines | 57 code | 23 blank | 40 comment | 4 complexity | fa63403f6731354547477c2ead18ad09 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. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Role add/reset selection form.
  18. *
  19. * @package core_role
  20. * @copyright 2013 Petr Skoda {@link http://skodak.org}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once("$CFG->libdir/formslib.php");
  25. /**
  26. * Role add/reset selection form.
  27. *
  28. * @package core_role
  29. * @copyright 2013 Petr Skoda {@link http://skodak.org}
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class core_role_preset_form extends moodleform {
  33. /**
  34. * Definition of this form.
  35. */
  36. protected function definition() {
  37. $mform = $this->_form;
  38. $data = $this->_customdata;
  39. $options = array();
  40. $group = get_string('other');
  41. $options[$group] = array();
  42. $options[$group][0] = get_string('norole', 'core_role');
  43. $group = get_string('role', 'core');
  44. $options[$group] = array();
  45. foreach (role_get_names(null, ROLENAME_BOTH) as $role) {
  46. // Allow reset to self too, it may be useful when importing incomplete XML preset.
  47. $options[$group][$role->id] = $role->localname;
  48. }
  49. $group = get_string('archetype', 'core_role');
  50. $options[$group] = array();
  51. foreach (get_role_archetypes() as $type) {
  52. $options[$group][$type] = get_string('archetype'.$type, 'core_role');
  53. }
  54. $mform->addElement('header', 'presetheader', get_string('roleresetdefaults', 'core_role'));
  55. $mform->addElement('selectgroups', 'resettype', get_string('roleresetrole', 'core_role'), $options);
  56. $mform->addElement('filepicker', 'rolepreset', get_string('rolerepreset', 'core_role'));
  57. if ($data['roleid']) {
  58. $mform->addElement('header', 'resetheader', get_string('resetrole', 'core_role'));
  59. $mform->addElement('advcheckbox', 'shortname', get_string('roleshortname', 'core_role'));
  60. $mform->addElement('advcheckbox', 'name', get_string('customrolename', 'core_role'));
  61. $mform->addElement('advcheckbox', 'description', get_string('customroledescription', 'core_role'));
  62. $mform->addElement('advcheckbox', 'archetype', get_string('archetype', 'core_role'));
  63. $mform->addElement('advcheckbox', 'contextlevels', get_string('maybeassignedin', 'core_role'));
  64. $mform->addElement('advcheckbox', 'allowassign', get_string('allowassign', 'core_role'));
  65. $mform->addElement('advcheckbox', 'allowoverride', get_string('allowoverride', 'core_role'));
  66. $mform->addElement('advcheckbox', 'allowswitch', get_string('allowswitch', 'core_role'));
  67. $mform->addElement('advcheckbox', 'permissions', get_string('permissions', 'core_role'));
  68. }
  69. $mform->addElement('hidden', 'roleid');
  70. $mform->setType('roleid', PARAM_INT);
  71. $mform->addElement('hidden', 'action');
  72. $mform->setType('action', PARAM_ALPHA);
  73. $mform->addElement('hidden', 'return');
  74. $mform->setType('return', PARAM_ALPHA);
  75. $this->add_action_buttons(true, get_string('continue', 'core'));
  76. $this->set_data($data);
  77. }
  78. /**
  79. * Validate this form.
  80. *
  81. * @param array $data submitted data
  82. * @param array $files not used
  83. * @return array errors
  84. */
  85. public function validation($data, $files) {
  86. $errors = parent::validation($data, $files);
  87. if ($files = $this->get_draft_files('rolepreset')) {
  88. /** @var stored_file $file */
  89. $file = reset($files);
  90. $xml = $file->get_content();
  91. if (!core_role_preset::is_valid_preset($xml)) {
  92. $errors['rolepreset'] = get_string('invalidpresetfile', 'core_role');
  93. }
  94. }
  95. return $errors;
  96. }
  97. }