MDL-53402 mod_data: field values of 0 are now displayed.
[moodle.git] / mod / data / field / multimenu / field.class.php
CommitLineData
1adbd2c3 1<?php
2c5c1418 2///////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
0997e51a 9// Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
2c5c1418 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
2c5c1418 25class data_field_multimenu extends data_field_base {
26
27 var $type = 'multimenu';
aab98aaf 28
b89cca19 29 function display_add_field($recordid = 0, $formdata = null) {
1c3b2058 30 global $DB, $OUTPUT;
0997e51a 31
b89cca19
DW
32 if ($formdata) {
33 $fieldname = 'field_' . $this->field->id;
1c3b2058
JO
34 if (isset($formdata->$fieldname)) {
35 $content = $formdata->$fieldname;
36 } else {
37 $content = array();
38 }
39 } else if ($recordid) {
a656d951 40 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
0997e51a 41 $content = explode('##', $content);
42 } else {
43 $content = array();
2c5c1418 44 }
0997e51a 45
e357c206 46 $str = '<div title="'.s($this->field->description).'">';
593b0229 47 $str .= '<input name="field_' . $this->field->id . '[xxx]" type="hidden" value="xxx"/>'; // hidden field - needed for empty selection
1c3b2058
JO
48
49 $str .= '<label for="field_' . $this->field->id . '">';
50 $str .= html_writer::span($this->field->name, 'accesshide');
51 if ($this->field->required) {
c07c5e7b 52 $str .= '<div class="inline-req">';
1c3b2058
JO
53 $str .= html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'),
54 array('class' => 'req', 'title' => get_string('requiredelement', 'form')));
55 $str .= '</div>';
56 }
57 $str .= '</label>';
c07c5e7b
JO
58 $str .= '<select name="field_' . $this->field->id . '[]" id="field_' . $this->field->id . '"';
59 $str .= ' multiple="multiple" class="mod-data-input">';
aab98aaf 60
1c3b2058 61 foreach (explode("\n", $this->field->param1) as $option) {
0997e51a 62 $option = trim($option);
e357c206 63 $str .= '<option value="' . s($option) . '"';
2c5c1418 64
593b0229 65 if (in_array($option, $content)) {
2c5c1418 66 // Selected by user.
593b0229 67 $str .= ' selected = "selected"';
2c5c1418 68 }
593b0229 69
70 $str .= '>';
2c5c1418 71 $str .= $option . '</option>';
72 }
73 $str .= '</select>';
bbe39b6c 74 $str .= '</div>';
aab98aaf 75
2c5c1418 76 return $str;
77 }
9a36be02 78
7900ecb0 79 function display_search_field($value = '') {
a656d951 80 global $CFG, $DB;
0d505ace 81
82 if (is_array($value)){
83 $content = $value['selected'];
15a18282 84 $allrequired = $value['allrequired'] ? true : false;
0d505ace 85 } else {
86 $content = array();
15a18282 87 $allrequired = false;
0d505ace 88 }
89
90 static $c = 0;
91
01f8e80d
RW
92 $str = '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>';
93 $str .= '<select id="f_'.$this->field->id.'" name="f_'.$this->field->id.'[]" multiple="multiple">';
0d505ace 94
9a36be02 95 // display only used options
eca89126
EL
96 $varcharcontent = $DB->sql_compare_text('content', 255);
97 $sql = "SELECT DISTINCT $varcharcontent AS content
a656d951 98 FROM {data_content}
99 WHERE fieldid=? AND content IS NOT NULL";
eca89126
EL
100
101 $usedoptions = array();
a656d951 102 if ($used = $DB->get_records_sql($sql, array($this->field->id))) {
9a36be02 103 foreach ($used as $data) {
104 $valuestr = $data->content;
a4e3818f 105 if ($valuestr === '') {
9a36be02 106 continue;
107 }
108 $values = explode('##', $valuestr);
109 foreach ($values as $value) {
110 $usedoptions[$value] = $value;
111 }
112 }
113 }
114
115 $found = false;
0d505ace 116 foreach (explode("\n",$this->field->param1) as $option) {
117 $option = trim($option);
9a36be02 118 if (!isset($usedoptions[$option])) {
119 continue;
120 }
121 $found = true;
0d505ace 122 $str .= '<option value="' . s($option) . '"';
123
a656d951 124 if (in_array($option, $content)) {
0d505ace 125 // Selected by user.
593b0229 126 $str .= ' selected = "selected"';
7900ecb0 127 }
593b0229 128 $str .= '>' . $option . '</option>';
7900ecb0 129 }
9a36be02 130 if (!$found) {
131 // oh, nothing to search for
132 return '';
133 }
134
0d505ace 135 $str .= '</select>';
136
15a18282 137 $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data'));
0d505ace 138
139 return $str;
140
7900ecb0 141 }
9a36be02 142
7900ecb0 143 function parse_search_field() {
18bd7573 144 $selected = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS);
0d505ace 145 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
146 if (empty($selected)) {
147 // no searching
148 return '';
149 }
150 return array('selected'=>$selected, 'allrequired'=>$allrequired);
7900ecb0 151 }
152
153 function generate_sql($tablealias, $value) {
15a18282
EL
154 global $DB;
155
e3487936 156 static $i=0;
157 $i++;
158 $name = "df_multimenu_{$i}_";
159 $params = array();
15a18282 160 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
e3487936 161
0d505ace 162 $allrequired = $value['allrequired'];
163 $selected = $value['selected'];
164
165 if ($selected) {
166 $conditions = array();
e3487936 167 $j=0;
0d505ace 168 foreach ($selected as $sel) {
e3487936 169 $j++;
170 $xname = $name.$j;
9a36be02 171 $likesel = str_replace('%', '\%', $sel);
172 $likeselsel = str_replace('_', '\_', $likesel);
15a18282 173 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
e3487936 174 OR {$tablealias}.content LIKE :{$xname}b
175 OR {$tablealias}.content LIKE :{$xname}c
176 OR {$tablealias}.content LIKE :{$xname}d))";
177 $params[$xname.'a'] = $sel;
178 $params[$xname.'b'] = "$likesel##%";
179 $params[$xname.'c'] = "%##$likesel";
180 $params[$xname.'d'] = "%##$likesel##%";
0d505ace 181 }
182 if ($allrequired) {
e3487936 183 return array(" (".implode(" AND ", $conditions).") ", $params);
0d505ace 184 } else {
e3487936 185 return array(" (".implode(" OR ", $conditions).") ", $params);
0d505ace 186 }
187 } else {
e3487936 188 return array(" ", array());
9a36be02 189 }
7900ecb0 190 }
2c5c1418 191
0997e51a 192 function update_content($recordid, $value, $name='') {
a656d951 193 global $DB;
194
39790bd8 195 $content = new stdClass();
593b0229 196 $content->fieldid = $this->field->id;
2c5c1418 197 $content->recordid = $recordid;
593b0229 198 $content->content = $this->format_data_field_multimenu_content($value);
0997e51a 199
a656d951 200 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
2c5c1418 201 $content->id = $oldcontent->id;
a656d951 202 return $DB->update_record('data_content', $content);
0997e51a 203 } else {
a656d951 204 return $DB->insert_record('data_content', $content);
2c5c1418 205 }
206 }
aab98aaf 207
e1791b72 208 function format_data_field_multimenu_content($content) {
209 if (!is_array($content)) {
593b0229 210 return NULL;
211 }
212 $options = explode("\n", $this->field->param1);
213 $options = array_map('trim', $options);
214
215 $vals = array();
216 foreach ($content as $key=>$val) {
217 if ($key === 'xxx') {
218 continue;
219 }
a656d951 220 if (!in_array($val, $options)) {
593b0229 221 continue;
e1791b72 222 }
593b0229 223 $vals[] = $val;
e1791b72 224 }
593b0229 225
226 if (empty($vals)) {
227 return NULL;
228 }
229
230 return implode('##', $vals);
2c5c1418 231 }
aab98aaf 232
233
0997e51a 234 function display_browse_field($recordid, $template) {
a656d951 235 global $DB;
2c5c1418 236
a656d951 237 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
4937a061 238 if (strval($content->content) === '') {
9a36be02 239 return false;
bbe39b6c 240 }
9a36be02 241
242 $options = explode("\n",$this->field->param1);
243 $options = array_map('trim', $options);
244
245 $contentArr = explode('##', $content->content);
bbe39b6c 246 $str = '';
247 foreach ($contentArr as $line) {
9a36be02 248 if (!in_array($line, $options)) {
249 // hmm, looks like somebody edited the field definition
250 continue;
251 }
bbe39b6c 252 $str .= $line . "<br />\n";
253 }
254 return $str;
2c5c1418 255 }
256 return false;
257 }
1c3b2058
JO
258
259 /**
260 * Check if a field from an add form is empty
261 *
262 * @param mixed $value
263 * @param mixed $name
264 * @return bool
265 */
266 function notemptyfield($value, $name) {
267 unset($value['xxx']);
268 return !empty($value);
269 }
2c5c1418 270}