PageRenderTime 83ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/data/field/textarea/field.class.php

https://github.com/mchurchward/moodle
PHP | 315 lines | 208 code | 32 blank | 75 comment | 32 complexity | adde46b97c7c34dd9c30a73b4eb86907 MD5 | raw file
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.org //
  8. // //
  9. // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
  10. // //
  11. // This program is free software; you can redistribute it and/or modify //
  12. // it under the terms of the GNU General Public License as published by //
  13. // the Free Software Foundation; either version 2 of the License, or //
  14. // (at your option) any later version. // // //
  15. // This program is distributed in the hope that it will be useful, //
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  18. // GNU General Public License for more details: //
  19. // //
  20. // http://www.gnu.org/copyleft/gpl.html //
  21. // //
  22. ///////////////////////////////////////////////////////////////////////////
  23. require_once($CFG->dirroot.'/lib/filelib.php');
  24. require_once($CFG->dirroot.'/repository/lib.php');
  25. class data_field_textarea extends data_field_base {
  26. var $type = 'textarea';
  27. /**
  28. * priority for globalsearch indexing
  29. *
  30. * @var int
  31. */
  32. protected static $priority = self::LOW_PRIORITY;
  33. /**
  34. * Returns options for embedded files
  35. *
  36. * @return array
  37. */
  38. private function get_options() {
  39. if (!isset($this->field->param5)) {
  40. $this->field->param5 = 0;
  41. }
  42. $options = array();
  43. $options['trusttext'] = false;
  44. $options['forcehttps'] = false;
  45. $options['subdirs'] = false;
  46. $options['maxfiles'] = -1;
  47. $options['context'] = $this->context;
  48. $options['maxbytes'] = $this->field->param5;
  49. $options['changeformat'] = 0;
  50. $options['noclean'] = false;
  51. return $options;
  52. }
  53. function display_add_field($recordid = 0, $formdata = null) {
  54. global $CFG, $DB, $OUTPUT, $PAGE;
  55. $text = '';
  56. $format = 0;
  57. $str = '<div title="' . s($this->field->description) . '" class="d-inline-flex">';
  58. $str .= '<label for="field_' . $this->field->id . '">';
  59. $str .= html_writer::span($this->field->name, 'accesshide');
  60. if ($this->field->required) {
  61. $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
  62. $str .= html_writer::div($image, 'inline-req');
  63. }
  64. $str .= '</label>';
  65. editors_head_setup();
  66. $options = $this->get_options();
  67. $itemid = $this->field->id;
  68. $field = 'field_'.$itemid;
  69. if ($formdata) {
  70. $fieldname = 'field_' . $this->field->id . '_content1';
  71. if (isset($formdata->$fieldname)) {
  72. $format = $formdata->$fieldname;
  73. } else {
  74. $format = file_get_unused_draft_itemid();
  75. }
  76. $fieldname = 'field_' . $this->field->id . '_itemid';
  77. if (isset($formdata->$fieldname)) {
  78. $draftitemid = clean_param($formdata->$fieldname, PARAM_INT);
  79. } else {
  80. $draftitemid = file_get_unused_draft_itemid();
  81. }
  82. $fieldname = 'field_' . $this->field->id;
  83. if (isset($formdata->$fieldname)) {
  84. $text = $formdata->$fieldname;
  85. }
  86. } else if ($recordid &&
  87. $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
  88. $format = $content->content1;
  89. $text = clean_text($content->content, $format);
  90. $text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text);
  91. } else {
  92. $draftitemid = file_get_unused_draft_itemid();
  93. $format = FORMAT_HTML;
  94. }
  95. // get filepicker info
  96. //
  97. $fpoptions = array();
  98. if ($options['maxfiles'] != 0 ) {
  99. $args = new stdClass();
  100. // need these three to filter repositories list
  101. $args->accepted_types = array('web_image');
  102. $args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
  103. $args->context = $this->context;
  104. $args->env = 'filepicker';
  105. // advimage plugin
  106. $image_options = initialise_filepicker($args);
  107. $image_options->context = $this->context;
  108. $image_options->client_id = uniqid();
  109. $image_options->maxbytes = $options['maxbytes'];
  110. $image_options->env = 'editor';
  111. $image_options->itemid = $draftitemid;
  112. // moodlemedia plugin
  113. $args->accepted_types = array('video', 'audio');
  114. $media_options = initialise_filepicker($args);
  115. $media_options->context = $this->context;
  116. $media_options->client_id = uniqid();
  117. $media_options->maxbytes = $options['maxbytes'];
  118. $media_options->env = 'editor';
  119. $media_options->itemid = $draftitemid;
  120. // advlink plugin
  121. $args->accepted_types = '*';
  122. $link_options = initialise_filepicker($args);
  123. $link_options->context = $this->context;
  124. $link_options->client_id = uniqid();
  125. $link_options->maxbytes = $options['maxbytes'];
  126. $link_options->env = 'editor';
  127. $link_options->itemid = $draftitemid;
  128. $fpoptions['image'] = $image_options;
  129. $fpoptions['media'] = $media_options;
  130. $fpoptions['link'] = $link_options;
  131. }
  132. $editor = editors_get_preferred_editor($format);
  133. $strformats = format_text_menu();
  134. $formats = $editor->get_supported_formats();
  135. foreach ($formats as $fid) {
  136. $formats[$fid] = $strformats[$fid];
  137. }
  138. $editor->set_text($text);
  139. $editor->use_editor($field, $options, $fpoptions);
  140. $str .= '<input type="hidden" name="'.$field.'_itemid" value="'.s($draftitemid).'" />';
  141. $str .= '<div class="mod-data-input">';
  142. $str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" cols="'.$this->field->param2.'" spellcheck="true">'.s($text).'</textarea></div>';
  143. $str .= '<div><label class="accesshide" for="' . $field . '_content1">' . get_string('format') . '</label>';
  144. $str .= '<select id="' . $field . '_content1" name="'.$field.'_content1">';
  145. foreach ($formats as $key=>$desc) {
  146. $selected = ($format == $key) ? 'selected="selected"' : '';
  147. $str .= '<option value="'.s($key).'" '.$selected.'>'.$desc.'</option>';
  148. }
  149. $str .= '</select>';
  150. $str .= '</div>';
  151. $str .= '</div>';
  152. $str .= '</div>';
  153. return $str;
  154. }
  155. function display_search_field($value = '') {
  156. return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
  157. '<input type="text" size="16" id="f_' . $this->field->id . '" name="f_' . $this->field->id . '" ' .
  158. 'value="' . s($value) . '" class="form-control"/>';
  159. }
  160. public function parse_search_field($defaults = null) {
  161. $param = 'f_'.$this->field->id;
  162. if (empty($defaults[$param])) {
  163. $defaults = array($param => '');
  164. }
  165. return optional_param($param, $defaults[$param], PARAM_NOTAGS);
  166. }
  167. function generate_sql($tablealias, $value) {
  168. global $DB;
  169. static $i=0;
  170. $i++;
  171. $name = "df_textarea_$i";
  172. return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
  173. }
  174. function print_after_form() {
  175. }
  176. function update_content($recordid, $value, $name='') {
  177. global $DB;
  178. $content = new stdClass();
  179. $content->fieldid = $this->field->id;
  180. $content->recordid = $recordid;
  181. $names = explode('_', $name);
  182. if (!empty($names[2])) {
  183. if ($names[2] == 'itemid') {
  184. // the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
  185. return true;
  186. } else {
  187. $content->{$names[2]} = clean_param($value, PARAM_NOTAGS); // content[1-4]
  188. }
  189. } else {
  190. $content->content = clean_param($value, PARAM_CLEAN);
  191. }
  192. if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
  193. $content->id = $oldcontent->id;
  194. } else {
  195. $content->id = $DB->insert_record('data_content', $content);
  196. if (!$content->id) {
  197. return false;
  198. }
  199. }
  200. if (!empty($content->content)) {
  201. $draftitemid = file_get_submitted_draft_itemid('field_'. $this->field->id. '_itemid');
  202. $options = $this->get_options();
  203. $content->content = file_save_draft_area_files($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $content->content);
  204. }
  205. $rv = $DB->update_record('data_content', $content);
  206. return $rv;
  207. }
  208. /**
  209. * Display the content of the field in browse mode
  210. *
  211. * @param int $recordid
  212. * @param object $template
  213. * @return bool|string
  214. */
  215. function display_browse_field($recordid, $template) {
  216. global $DB;
  217. if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
  218. if (isset($content->content)) {
  219. $options = new stdClass();
  220. if ($this->field->param1 == '1') { // We are autolinking this field, so disable linking within us
  221. $options->filter = false;
  222. }
  223. $options->para = false;
  224. $str = file_rewrite_pluginfile_urls($content->content, 'pluginfile.php', $this->context->id, 'mod_data', 'content', $content->id, $this->get_options());
  225. $str = format_text($str, $content->content1, $options);
  226. } else {
  227. $str = '';
  228. }
  229. return $str;
  230. }
  231. return false;
  232. }
  233. /**
  234. * Whether this module support files
  235. *
  236. * @param string $relativepath
  237. * @return bool
  238. */
  239. function file_ok($relativepath) {
  240. return true;
  241. }
  242. /**
  243. * Only look at the first item (second is format)
  244. *
  245. * @param string $value
  246. * @param string $name
  247. * @return bool
  248. */
  249. function notemptyfield($value, $name) {
  250. $names = explode('_', $name);
  251. // Clean first.
  252. if (count($names) == 2) {
  253. // Don't assume that this is coming from a text editor with tags.
  254. return strval($value) !== '';
  255. }
  256. return false;
  257. }
  258. /**
  259. * Returns the presentable string value for a field content.
  260. *
  261. * The returned string should be plain text.
  262. *
  263. * @param stdClass $content
  264. * @return string
  265. */
  266. public static function get_content_value($content) {
  267. return content_to_text($content->content, $content->content1);
  268. }
  269. /**
  270. * Return the plugin configs for external functions.
  271. *
  272. * @return array the list of config parameters
  273. * @since Moodle 3.3
  274. */
  275. public function get_config_for_external() {
  276. // Return all the config parameters.
  277. $configs = [];
  278. for ($i = 1; $i <= 10; $i++) {
  279. $configs["param$i"] = $this->field->{"param$i"};
  280. }
  281. return $configs;
  282. }
  283. }