/admin/tool/messageinbound/classes/edit_handler_form.php

https://github.com/dongsheng/moodle · PHP · 143 lines · 86 code · 16 blank · 41 comment · 11 complexity · 42eab4ee9442168daa963c56830ab206 MD5 · raw file

  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. * Form to edit handlers.
  18. *
  19. * @package tool_messageinbound
  20. * @copyright 2014 Andrew Nicols
  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. * Form to edit handlers.
  27. *
  28. * @copyright 2014 Andrew Nicols
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class tool_messageinbound_edit_handler_form extends moodleform {
  32. /**
  33. * The form definition
  34. */
  35. public function definition() {
  36. $mform = $this->_form;
  37. $handler = $this->_customdata['handler'];
  38. // Set up the options for formatting text for descriptions, etc.
  39. $formatoptions = new stdClass();
  40. $formatoptions->trusted = false;
  41. $formatoptions->noclean = false;
  42. $formatoptions->smiley = false;
  43. $formatoptions->filter = false;
  44. $formatoptions->para = true;
  45. $formatoptions->newlines = false;
  46. $formatoptions->overflowdiv = true;
  47. // General information about the handler.
  48. $mform->addElement('header', 'general', get_string('general'));
  49. $mform->addElement('static', 'name', get_string('name', 'tool_messageinbound'),
  50. $handler->name);
  51. $mform->addElement('static', 'classname', get_string('classname', 'tool_messageinbound'));
  52. $description = format_text($handler->description, FORMAT_MARKDOWN, $formatoptions);
  53. $mform->addElement('static', 'description', get_string('description', 'tool_messageinbound'),
  54. $description);
  55. // Items which can be configured.
  56. $mform->addElement('header', 'configuration', get_string('configuration'));
  57. if ($handler->can_change_defaultexpiration()) {
  58. // Show option to change expiry only if the handler supports it.
  59. $options = array(
  60. HOURSECS => get_string('onehour', 'tool_messageinbound'),
  61. DAYSECS => get_string('oneday', 'tool_messageinbound'),
  62. WEEKSECS => get_string('oneweek', 'tool_messageinbound'),
  63. YEARSECS => get_string('oneyear', 'tool_messageinbound'),
  64. 0 => get_string('noexpiry', 'tool_messageinbound'),
  65. );
  66. $mform->addElement('select', 'defaultexpiration', get_string('defaultexpiration', 'tool_messageinbound'), $options);
  67. $mform->addHelpButton('defaultexpiration', 'defaultexpiration', 'tool_messageinbound');
  68. } else {
  69. $text = $this->get_defaultexpiration_text($handler);
  70. $mform->addElement('static', 'defaultexpiration_fake', get_string('defaultexpiration', 'tool_messageinbound'), $text);
  71. $mform->addElement('hidden', 'defaultexpiration');
  72. $mform->addHelpButton('defaultexpiration_fake', 'defaultexpiration', 'tool_messageinbound');
  73. $mform->setType('defaultexpiration', PARAM_INT);
  74. }
  75. if ($handler->can_change_validateaddress()) {
  76. $mform->addElement('checkbox', 'validateaddress', get_string('requirevalidation', 'tool_messageinbound'));
  77. $mform->addHelpButton('validateaddress', 'validateaddress', 'tool_messageinbound');
  78. } else {
  79. if ($handler->validateaddress) {
  80. $text = get_string('yes');
  81. } else {
  82. $text = get_string('no');
  83. }
  84. $mform->addElement('static', 'validateaddress_fake', get_string('requirevalidation', 'tool_messageinbound'), $text);
  85. $mform->addElement('hidden', 'validateaddress');
  86. $mform->addHelpButton('validateaddress_fake', 'fixedvalidateaddress', 'tool_messageinbound');
  87. $mform->setType('validateaddress', PARAM_INT);
  88. }
  89. if ($handler->can_change_enabled()) {
  90. $mform->addElement('checkbox', 'enabled', get_string('enabled', 'tool_messageinbound'));
  91. } else {
  92. if ($handler->enabled) {
  93. $text = get_string('yes');
  94. } else {
  95. $text = get_string('no');
  96. }
  97. $mform->addElement('static', 'enabled_fake', get_string('enabled', 'tool_messageinbound'), $text);
  98. $mform->addHelpButton('enabled', 'fixedenabled', 'tool_messageinbound');
  99. $mform->addElement('hidden', 'enabled');
  100. $mform->setType('enabled', PARAM_INT);
  101. }
  102. $this->add_action_buttons(true, get_string('savechanges'));
  103. }
  104. /**
  105. * Return a text string representing the selected default expiration for the handler.
  106. *
  107. * @param \core\message\inbound\handler $handler handler instance.
  108. *
  109. * @return string localised text string.
  110. */
  111. protected function get_defaultexpiration_text(\core\message\inbound\handler $handler) {
  112. switch($handler->defaultexpiration) {
  113. case HOURSECS :
  114. return get_string('onehour', 'tool_messageinbound');
  115. case DAYSECS :
  116. return get_string('oneday', 'tool_messageinbound');
  117. case WEEKSECS :
  118. return get_string('oneweek', 'tool_messageinbound');
  119. case YEARSECS :
  120. return get_string('oneyear', 'tool_messageinbound');
  121. case 0:
  122. return get_string('noexpiry', 'tool_messageinbound');
  123. default:
  124. return ''; // Should never happen.
  125. }
  126. }
  127. }