PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/data/field/multimenu/field.class.php

https://github.com/mrmark/moodle
PHP | 296 lines | 204 code | 44 blank | 48 comment | 29 complexity | e4098beacbcd4ef58ef34e864f3a275e 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. // //
  16. // This program is distributed in the hope that it will be useful, //
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  19. // GNU General Public License for more details: //
  20. // //
  21. // http://www.gnu.org/copyleft/gpl.html //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. class data_field_multimenu extends data_field_base {
  25. var $type = 'multimenu';
  26. /**
  27. * priority for globalsearch indexing
  28. *
  29. * @var int
  30. * */
  31. protected static $priority = self::LOW_PRIORITY;
  32. function display_add_field($recordid = 0, $formdata = null) {
  33. global $DB, $OUTPUT;
  34. if ($formdata) {
  35. $fieldname = 'field_' . $this->field->id;
  36. if (isset($formdata->$fieldname)) {
  37. $content = $formdata->$fieldname;
  38. } else {
  39. $content = array();
  40. }
  41. } else if ($recordid) {
  42. $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
  43. $content = explode('##', $content);
  44. } else {
  45. $content = array();
  46. }
  47. $str = '<div title="'.s($this->field->description).'">';
  48. $str .= '<input name="field_' . $this->field->id . '[xxx]" type="hidden" value="xxx"/>'; // hidden field - needed for empty selection
  49. $str .= '<label for="field_' . $this->field->id . '" class="accesshide">';
  50. $str .= html_writer::span($this->field->name);
  51. if ($this->field->required) {
  52. $str .= '<div class="inline-req">';
  53. $str .= $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
  54. $str .= '</div>';
  55. }
  56. $str .= '</label>';
  57. $str .= '<select name="field_' . $this->field->id . '[]" id="field_' . $this->field->id . '"';
  58. $str .= ' multiple="multiple" class="mod-data-input form-control">';
  59. foreach (explode("\n", $this->field->param1) as $option) {
  60. $option = trim($option);
  61. $str .= '<option value="' . s($option) . '"';
  62. if (in_array($option, $content)) {
  63. // Selected by user.
  64. $str .= ' selected = "selected"';
  65. }
  66. $str .= '>';
  67. $str .= $option . '</option>';
  68. }
  69. $str .= '</select>';
  70. $str .= '</div>';
  71. return $str;
  72. }
  73. function display_search_field($value = '') {
  74. global $CFG, $DB;
  75. if (is_array($value)){
  76. $content = $value['selected'];
  77. $allrequired = $value['allrequired'] ? true : false;
  78. } else {
  79. $content = array();
  80. $allrequired = false;
  81. }
  82. static $c = 0;
  83. $str = '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>';
  84. $str .= '<select id="f_'.$this->field->id.'" name="f_'.$this->field->id.'[]" multiple="multiple" class="form-control">';
  85. // display only used options
  86. $varcharcontent = $DB->sql_compare_text('content', 255);
  87. $sql = "SELECT DISTINCT $varcharcontent AS content
  88. FROM {data_content}
  89. WHERE fieldid=? AND content IS NOT NULL";
  90. $usedoptions = array();
  91. if ($used = $DB->get_records_sql($sql, array($this->field->id))) {
  92. foreach ($used as $data) {
  93. $valuestr = $data->content;
  94. if ($valuestr === '') {
  95. continue;
  96. }
  97. $values = explode('##', $valuestr);
  98. foreach ($values as $value) {
  99. $usedoptions[$value] = $value;
  100. }
  101. }
  102. }
  103. $found = false;
  104. foreach (explode("\n",$this->field->param1) as $option) {
  105. $option = trim($option);
  106. if (!isset($usedoptions[$option])) {
  107. continue;
  108. }
  109. $found = true;
  110. $str .= '<option value="' . s($option) . '"';
  111. if (in_array($option, $content)) {
  112. // Selected by user.
  113. $str .= ' selected = "selected"';
  114. }
  115. $str .= '>' . $option . '</option>';
  116. }
  117. if (!$found) {
  118. // oh, nothing to search for
  119. return '';
  120. }
  121. $str .= '</select>';
  122. $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired,
  123. get_string('selectedrequired', 'data'), array('class' => 'm-r-1'));
  124. return $str;
  125. }
  126. function parse_search_field() {
  127. $selected = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS);
  128. $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
  129. if (empty($selected)) {
  130. // no searching
  131. return '';
  132. }
  133. return array('selected'=>$selected, 'allrequired'=>$allrequired);
  134. }
  135. function generate_sql($tablealias, $value) {
  136. global $DB;
  137. static $i=0;
  138. $i++;
  139. $name = "df_multimenu_{$i}_";
  140. $params = array();
  141. $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
  142. $allrequired = $value['allrequired'];
  143. $selected = $value['selected'];
  144. if ($selected) {
  145. $conditions = array();
  146. $j=0;
  147. foreach ($selected as $sel) {
  148. $j++;
  149. $xname = $name.$j;
  150. $likesel = str_replace('%', '\%', $sel);
  151. $likeselsel = str_replace('_', '\_', $likesel);
  152. $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
  153. OR {$tablealias}.content LIKE :{$xname}b
  154. OR {$tablealias}.content LIKE :{$xname}c
  155. OR {$tablealias}.content LIKE :{$xname}d))";
  156. $params[$xname.'a'] = $sel;
  157. $params[$xname.'b'] = "$likesel##%";
  158. $params[$xname.'c'] = "%##$likesel";
  159. $params[$xname.'d'] = "%##$likesel##%";
  160. }
  161. if ($allrequired) {
  162. return array(" (".implode(" AND ", $conditions).") ", $params);
  163. } else {
  164. return array(" (".implode(" OR ", $conditions).") ", $params);
  165. }
  166. } else {
  167. return array(" ", array());
  168. }
  169. }
  170. function update_content($recordid, $value, $name='') {
  171. global $DB;
  172. $content = new stdClass();
  173. $content->fieldid = $this->field->id;
  174. $content->recordid = $recordid;
  175. $content->content = $this->format_data_field_multimenu_content($value);
  176. if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
  177. $content->id = $oldcontent->id;
  178. return $DB->update_record('data_content', $content);
  179. } else {
  180. return $DB->insert_record('data_content', $content);
  181. }
  182. }
  183. function format_data_field_multimenu_content($content) {
  184. if (!is_array($content)) {
  185. return NULL;
  186. }
  187. $options = explode("\n", $this->field->param1);
  188. $options = array_map('trim', $options);
  189. $vals = array();
  190. foreach ($content as $key=>$val) {
  191. if ($key === 'xxx') {
  192. continue;
  193. }
  194. if (!in_array($val, $options)) {
  195. continue;
  196. }
  197. $vals[] = $val;
  198. }
  199. if (empty($vals)) {
  200. return NULL;
  201. }
  202. return implode('##', $vals);
  203. }
  204. function display_browse_field($recordid, $template) {
  205. global $DB;
  206. if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
  207. if (strval($content->content) === '') {
  208. return false;
  209. }
  210. $options = explode("\n",$this->field->param1);
  211. $options = array_map('trim', $options);
  212. $contentArr = explode('##', $content->content);
  213. $str = '';
  214. foreach ($contentArr as $line) {
  215. if (!in_array($line, $options)) {
  216. // hmm, looks like somebody edited the field definition
  217. continue;
  218. }
  219. $str .= $line . "<br />\n";
  220. }
  221. return $str;
  222. }
  223. return false;
  224. }
  225. /**
  226. * Check if a field from an add form is empty
  227. *
  228. * @param mixed $value
  229. * @param mixed $name
  230. * @return bool
  231. */
  232. function notemptyfield($value, $name) {
  233. unset($value['xxx']);
  234. return !empty($value);
  235. }
  236. /**
  237. * Returns the presentable string value for a field content.
  238. *
  239. * The returned string should be plain text.
  240. *
  241. * @param stdClass $content
  242. * @return string
  243. */
  244. public static function get_content_value($content) {
  245. $arr = explode('##', $content->content);
  246. $strvalue = '';
  247. foreach ($arr as $a) {
  248. $strvalue .= $a . ' ';
  249. }
  250. return trim($strvalue, "\r\n ");
  251. }
  252. }