MDL-53402 mod_data: field values of 0 are now displayed.
[moodle.git] / mod / data / field / checkbox / field.class.php
CommitLineData
1adbd2c3 1<?php
4728968f 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 //
4728968f 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
4728968f 25class data_field_checkbox extends data_field_base {
26
27 var $type = 'checkbox';
aab98aaf 28
b89cca19 29 function display_add_field($recordid = 0, $formdata = null) {
1c3b2058 30 global $CFG, $DB, $OUTPUT;
aab98aaf 31
c87fbb27 32 $content = array();
0997e51a 33
b89cca19
DW
34 if ($formdata) {
35 $fieldname = 'field_' . $this->field->id;
36 $content = $formdata->$fieldname;
37 } else if ($recordid) {
a656d951 38 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
0997e51a 39 $content = explode('##', $content);
d31629c6
DC
40 } else {
41 $content = array();
4728968f 42 }
0997e51a 43
1c3b2058
JO
44 $str = '<div title="' . s($this->field->description) . '">';
45 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name;
b89cca19 46 if ($this->field->required) {
1c3b2058
JO
47 $str .= '$nbsp;' . get_string('requiredelement', 'form');
48 $str .= '</span></legend>';
c07c5e7b 49 $image = html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'),
1c3b2058 50 array('class' => 'req', 'title' => get_string('requiredelement', 'form')));
c07c5e7b 51 $str .= html_writer::div($image, 'inline-req');
b89cca19 52 } else {
1c3b2058 53 $str .= '</span></legend>';
b89cca19 54 }
aab98aaf 55
e357c206 56 $i = 0;
423bd918 57 foreach (explode("\n", $this->field->param1) as $checkbox) {
58 $checkbox = trim($checkbox);
6c31d376 59 if ($checkbox === '') {
60 continue; // skip empty lines
61 }
f62f7d8f 62 $str .= '<input type="hidden" name="field_' . $this->field->id . '[]" value="" />';
e357c206 63 $str .= '<input type="checkbox" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '[]" ';
c07c5e7b 64 $str .= 'value="' . s($checkbox) . '" class="mod-data-input" ';
aab98aaf 65
4728968f 66 if (array_search($checkbox, $content) !== false) {
d31629c6 67 $str .= 'checked />';
0997e51a 68 } else {
4728968f 69 $str .= '/>';
70 }
e357c206 71 $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$checkbox.'</label><br />';
72 $i++;
4728968f 73 }
aab98aaf 74 $str .= '</fieldset>';
bbe39b6c 75 $str .= '</div>';
4728968f 76 return $str;
77 }
78
7900ecb0 79 function display_search_field($value='') {
15a18282
EL
80 global $CFG, $DB;
81
cf353d88 82 if (is_array($value)) {
d31629c6 83 $content = $value['checked'];
15a18282 84 $allrequired = $value['allrequired'] ? true : false;
d31629c6
DC
85 } else {
86 $content = array();
15a18282 87 $allrequired = false;
d31629c6
DC
88 }
89
90 $str = '';
91 $found = false;
92 foreach (explode("\n",$this->field->param1) as $checkbox) {
93 $checkbox = trim($checkbox);
94
24b81889 95 if (in_array($checkbox, $content)) {
2f0e96e4 96 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), true, $checkbox);
cf353d88 97 } else {
98 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), false, $checkbox);
7900ecb0 99 }
64b9afa6 100 $str .= html_writer::empty_tag('br');
d31629c6
DC
101 $found = true;
102 }
103 if (!$found) {
104 return '';
7900ecb0 105 }
d31629c6 106
15a18282 107 $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data'));
d31629c6 108 return $str;
7900ecb0 109 }
1adbd2c3 110
7900ecb0 111 function parse_search_field() {
18bd7573 112 $selected = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS);
d31629c6
DC
113 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
114 if (empty($selected)) {
115 // no searching
116 return '';
117 }
118 return array('checked'=>$selected, 'allrequired'=>$allrequired);
7900ecb0 119 }
1adbd2c3 120
7900ecb0 121 function generate_sql($tablealias, $value) {
15a18282
EL
122 global $DB;
123
e3487936 124 static $i=0;
125 $i++;
15a18282
EL
126 $name = "df_checkbox_{$i}_";
127 $params = array();
128 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
129
d31629c6
DC
130 $allrequired = $value['allrequired'];
131 $selected = $value['checked'];
132
133 if ($selected) {
134 $conditions = array();
15a18282 135 $j=0;
d31629c6 136 foreach ($selected as $sel) {
15a18282
EL
137 $j++;
138 $xname = $name.$j;
d31629c6
DC
139 $likesel = str_replace('%', '\%', $sel);
140 $likeselsel = str_replace('_', '\_', $likesel);
15a18282
EL
141 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
142 OR {$tablealias}.content LIKE :{$xname}b
143 OR {$tablealias}.content LIKE :{$xname}c
144 OR {$tablealias}.content LIKE :{$xname}d))";
145 $params[$xname.'a'] = $sel;
146 $params[$xname.'b'] = "$likesel##%";
147 $params[$xname.'c'] = "%##$likesel";
148 $params[$xname.'d'] = "%##$likesel##%";
d31629c6
DC
149 }
150 if ($allrequired) {
15a18282 151 return array(" (".implode(" AND ", $conditions).") ", $params);
d31629c6 152 } else {
15a18282 153 return array(" (".implode(" OR ", $conditions).") ", $params);
d31629c6
DC
154 }
155 } else {
156 return array(" ", array());
157 }
7900ecb0 158 }
159
0997e51a 160 function update_content($recordid, $value, $name='') {
a656d951 161 global $DB;
162
39790bd8 163 $content = new stdClass();
0997e51a 164 $content->fieldid = $this->field->id;
4728968f 165 $content->recordid = $recordid;
166 $content->content = $this->format_data_field_checkbox_content($value);
aab98aaf 167
a656d951 168 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
4728968f 169 $content->id = $oldcontent->id;
a656d951 170 return $DB->update_record('data_content', $content);
e1791b72 171 } else {
a656d951 172 return $DB->insert_record('data_content', $content);
e1791b72 173 }
4728968f 174 }
aab98aaf 175
0997e51a 176 function display_browse_field($recordid, $template) {
a656d951 177 global $DB;
aab98aaf 178
a656d951 179 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
4937a061 180 if (strval($content->content) === '') {
d31629c6 181 return false;
4728968f 182 }
d31629c6
DC
183
184 $options = explode("\n",$this->field->param1);
185 $options = array_map('trim', $options);
186
187 $contentArr = explode('##', $content->content);
4728968f 188 $str = '';
189 foreach ($contentArr as $line) {
d31629c6
DC
190 if (!in_array($line, $options)) {
191 // hmm, looks like somebody edited the field definition
192 continue;
193 }
4728968f 194 $str .= $line . "<br />\n";
195 }
196 return $str;
197 }
198 return false;
199 }
0997e51a 200
201 function format_data_field_checkbox_content($content) {
202 if (!is_array($content)) {
d31629c6
DC
203 return NULL;
204 }
205 $options = explode("\n", $this->field->param1);
206 $options = array_map('trim', $options);
207
208 $vals = array();
209 foreach ($content as $key=>$val) {
210 if ($key === 'xxx') {
211 continue;
0997e51a 212 }
798b0b33 213 if (!in_array($val, $options)) {
d31629c6
DC
214 continue;
215
216 }
217 $vals[] = $val;
0997e51a 218 }
d31629c6
DC
219
220 if (empty($vals)) {
221 return NULL;
222 }
223
224 return implode('##', $vals);
0997e51a 225 }
d31629c6 226
b89cca19
DW
227 /**
228 * Check whether any boxes in the checkbox where checked.
229 *
230 * @param mixed $value The submitted values
231 * @param mixed $name
232 * @return bool
233 */
234 function notemptyfield($value, $name) {
235 $found = false;
236 foreach ($value as $checkboxitem) {
9b5cf165 237 if (strval($checkboxitem) !== '') {
b89cca19
DW
238 $found = true;
239 break;
240 }
241 }
242 return $found;
243 }
1adbd2c3 244
b89cca19 245}