/customfield/field/textarea/classes/data_controller.php

https://github.com/markn86/moodle · PHP · 186 lines · 92 code · 18 blank · 76 comment · 10 complexity · ac7c535abc1795129894881f0fcba4a8 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. * Customfields textarea plugin
  18. *
  19. * @package customfield_textarea
  20. * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace customfield_textarea;
  24. defined('MOODLE_INTERNAL') || die;
  25. /**
  26. * Class data
  27. *
  28. * @package customfield_textarea
  29. * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com>
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class data_controller extends \core_customfield\data_controller {
  33. /**
  34. * Return the name of the field where the information is stored
  35. * @return string
  36. */
  37. public function datafield() : string {
  38. return 'value';
  39. }
  40. /**
  41. * Options for the editor
  42. *
  43. * @return array
  44. */
  45. protected function value_editor_options() {
  46. /** @var field_controller $field */
  47. $field = $this->get_field();
  48. return $field->value_editor_options($this->get('id') ? $this->get_context() : null);
  49. }
  50. /**
  51. * Returns the name of the field to be used on HTML forms.
  52. *
  53. * @return string
  54. */
  55. public function get_form_element_name() : string {
  56. return parent::get_form_element_name() . '_editor';
  57. }
  58. /**
  59. * Add fields for editing a textarea field.
  60. *
  61. * @param \MoodleQuickForm $mform
  62. */
  63. public function instance_form_definition(\MoodleQuickForm $mform) {
  64. $field = $this->get_field();
  65. $desceditoroptions = $this->value_editor_options();
  66. $elementname = $this->get_form_element_name();
  67. $mform->addElement('editor', $elementname, $this->get_field()->get_formatted_name(), null, $desceditoroptions);
  68. if ($field->get_configdata_property('required')) {
  69. $mform->addRule($elementname, null, 'required', null, 'client');
  70. }
  71. }
  72. /**
  73. * Saves the data coming from form
  74. *
  75. * @param \stdClass $datanew data coming from the form
  76. */
  77. public function instance_form_save(\stdClass $datanew) {
  78. $fieldname = $this->get_form_element_name();
  79. if (!property_exists($datanew, $fieldname)) {
  80. return;
  81. }
  82. $fromform = $datanew->$fieldname;
  83. if (!$this->get('id')) {
  84. $this->data->set('value', '');
  85. $this->data->set('valueformat', FORMAT_MOODLE);
  86. $this->save();
  87. }
  88. if (array_key_exists('text', $fromform)) {
  89. $textoptions = $this->value_editor_options();
  90. $data = (object) ['field_editor' => $fromform];
  91. $data = file_postupdate_standard_editor($data, 'field', $textoptions, $textoptions['context'],
  92. 'customfield_textarea', 'value', $this->get('id'));
  93. $this->data->set('value', $data->field);
  94. $this->data->set('valueformat', $data->fieldformat);
  95. $this->save();
  96. }
  97. }
  98. /**
  99. * Prepares the custom field data related to the object to pass to mform->set_data() and adds them to it
  100. *
  101. * This function must be called before calling $form->set_data($object);
  102. *
  103. * @param \stdClass $instance the entity that has custom fields, if 'id' attribute is present the custom
  104. * fields for this entity will be added, otherwise the default values will be added.
  105. */
  106. public function instance_form_before_set_data(\stdClass $instance) {
  107. $textoptions = $this->value_editor_options();
  108. if ($this->get('id')) {
  109. $text = $this->get('value');
  110. $format = $this->get('valueformat');
  111. $temp = (object)['field' => $text, 'fieldformat' => $format];
  112. file_prepare_standard_editor($temp, 'field', $textoptions, $textoptions['context'], 'customfield_textarea',
  113. 'value', $this->get('id'));
  114. $value = $temp->field_editor;
  115. } else {
  116. $text = $this->get_field()->get_configdata_property('defaultvalue');
  117. $format = $this->get_field()->get_configdata_property('defaultvalueformat');
  118. $temp = (object)['field' => $text, 'fieldformat' => $format];
  119. file_prepare_standard_editor($temp, 'field', $textoptions, $textoptions['context'], 'customfield_textarea',
  120. 'defaultvalue', $this->get_field()->get('id'));
  121. $value = $temp->field_editor;
  122. }
  123. $instance->{$this->get_form_element_name()} = $value;
  124. }
  125. /**
  126. * Delete data
  127. *
  128. * @return bool
  129. */
  130. public function delete() {
  131. get_file_storage()->delete_area_files($this->get('contextid'), 'customfield_textarea',
  132. 'value', $this->get('id'));
  133. return parent::delete();
  134. }
  135. /**
  136. * Returns the default value as it would be stored in the database (not in human-readable format).
  137. *
  138. * @return mixed
  139. */
  140. public function get_default_value() {
  141. return $this->get_field()->get_configdata_property('defaultvalue');
  142. }
  143. /**
  144. * Returns value in a human-readable format
  145. *
  146. * @return mixed|null value or null if empty
  147. */
  148. public function export_value() {
  149. $value = $this->get_value();
  150. if ($this->is_empty($value)) {
  151. return null;
  152. }
  153. if ($dataid = $this->get('id')) {
  154. $context = $this->get_context();
  155. $processed = file_rewrite_pluginfile_urls($value, 'pluginfile.php',
  156. $context->id, 'customfield_textarea', 'value', $dataid);
  157. $value = format_text($processed, $this->get('valueformat'), ['context' => $context]);
  158. } else {
  159. $fieldid = $this->get_field()->get('id');
  160. $configcontext = $this->get_field()->get_handler()->get_configuration_context();
  161. $processed = file_rewrite_pluginfile_urls($value, 'pluginfile.php',
  162. $configcontext->id, 'customfield_textarea', 'defaultvalue', $fieldid);
  163. $valueformat = $this->get_field()->get_configdata_property('defaultvalueformat');
  164. $value = format_text($processed, $valueformat, ['context' => $configcontext]);
  165. }
  166. return $value;
  167. }
  168. }