MDL-53402 mod_data: field values of 0 are now displayed.
[moodle.git] / mod / data / field / checkbox / field.class.php
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 ///////////////////////////////////////////////////////////////////////////
25 class data_field_checkbox extends data_field_base {
27     var $type = 'checkbox';
29     function display_add_field($recordid = 0, $formdata = null) {
30         global $CFG, $DB, $OUTPUT;
32         $content = array();
34         if ($formdata) {
35             $fieldname = 'field_' . $this->field->id;
36             $content = $formdata->$fieldname;
37         } else if ($recordid) {
38             $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
39             $content = explode('##', $content);
40         } else {
41             $content = array();
42         }
44         $str = '<div title="' . s($this->field->description) . '">';
45         $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name;
46         if ($this->field->required) {
47             $str .= '$nbsp;' . get_string('requiredelement', 'form');
48             $str .= '</span></legend>';
49             $image = html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'),
50                                      array('class' => 'req', 'title' => get_string('requiredelement', 'form')));
51             $str .= html_writer::div($image, 'inline-req');
52         } else {
53             $str .= '</span></legend>';
54         }
56         $i = 0;
57         foreach (explode("\n", $this->field->param1) as $checkbox) {
58             $checkbox = trim($checkbox);
59             if ($checkbox === '') {
60                 continue; // skip empty lines
61             }
62             $str .= '<input type="hidden" name="field_' . $this->field->id . '[]" value="" />';
63             $str .= '<input type="checkbox" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '[]" ';
64             $str .= 'value="' . s($checkbox) . '" class="mod-data-input" ';
66             if (array_search($checkbox, $content) !== false) {
67                 $str .= 'checked />';
68             } else {
69                 $str .= '/>';
70             }
71             $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$checkbox.'</label><br />';
72             $i++;
73         }
74         $str .= '</fieldset>';
75         $str .= '</div>';
76         return $str;
77     }
79     function display_search_field($value='') {
80         global $CFG, $DB;
82         if (is_array($value)) {
83             $content = $value['checked'];
84             $allrequired = $value['allrequired'] ? true : false;
85         } else {
86             $content = array();
87             $allrequired = false;
88         }
90         $str = '';
91         $found = false;
92         foreach (explode("\n",$this->field->param1) as $checkbox) {
93             $checkbox = trim($checkbox);
95             if (in_array($checkbox, $content)) {
96                 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), true, $checkbox);
97             } else {
98                 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), false, $checkbox);
99             }
100             $str .= html_writer::empty_tag('br');
101             $found = true;
102         }
103         if (!$found) {
104             return '';
105         }
107         $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data'));
108         return $str;
109     }
111     function parse_search_field() {
112         $selected    = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS);
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);
119     }
121     function generate_sql($tablealias, $value) {
122         global $DB;
124         static $i=0;
125         $i++;
126         $name = "df_checkbox_{$i}_";
127         $params = array();
128         $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
130         $allrequired = $value['allrequired'];
131         $selected    = $value['checked'];
133         if ($selected) {
134             $conditions = array();
135             $j=0;
136             foreach ($selected as $sel) {
137                 $j++;
138                 $xname = $name.$j;
139                 $likesel = str_replace('%', '\%', $sel);
140                 $likeselsel = str_replace('_', '\_', $likesel);
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##%";
149             }
150             if ($allrequired) {
151                 return array(" (".implode(" AND ", $conditions).") ", $params);
152             } else {
153                 return array(" (".implode(" OR ", $conditions).") ", $params);
154             }
155         } else {
156             return array(" ", array());
157         }
158     }
160     function update_content($recordid, $value, $name='') {
161         global $DB;
163         $content = new stdClass();
164         $content->fieldid = $this->field->id;
165         $content->recordid = $recordid;
166         $content->content = $this->format_data_field_checkbox_content($value);
168         if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
169             $content->id = $oldcontent->id;
170             return $DB->update_record('data_content', $content);
171         } else {
172             return $DB->insert_record('data_content', $content);
173         }
174     }
176     function display_browse_field($recordid, $template) {
177         global $DB;
179         if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
180             if (strval($content->content) === '') {
181                 return false;
182             }
184             $options = explode("\n",$this->field->param1);
185             $options = array_map('trim', $options);
187             $contentArr = explode('##', $content->content);
188             $str = '';
189             foreach ($contentArr as $line) {
190                 if (!in_array($line, $options)) {
191                     // hmm, looks like somebody edited the field definition
192                     continue;
193                 }
194                 $str .= $line . "<br />\n";
195             }
196             return $str;
197         }
198         return false;
199     }
201     function format_data_field_checkbox_content($content) {
202         if (!is_array($content)) {
203             return NULL;
204         }
205         $options = explode("\n", $this->field->param1);
206         $options = array_map('trim', $options);
208         $vals = array();
209         foreach ($content as $key=>$val) {
210             if ($key === 'xxx') {
211                 continue;
212             }
213             if (!in_array($val, $options)) {
214                 continue;
216             }
217             $vals[] = $val;
218         }
220         if (empty($vals)) {
221             return NULL;
222         }
224         return implode('##', $vals);
225     }
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) {
237             if (strval($checkboxitem) !== '') {
238                 $found = true;
239                 break;
240             }
241         }
242         return $found;
243     }