5b3cbf725edad7d90f5e0008aa8aab45856c8e15
[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             $found = true;
101         }
102         if (!$found) {
103             return '';
104         }
106         $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data'));
107         return $str;
108     }
110     function parse_search_field() {
111         $selected    = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS);
112         $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
113         if (empty($selected)) {
114             // no searching
115             return '';
116         }
117         return array('checked'=>$selected, 'allrequired'=>$allrequired);
118     }
120     function generate_sql($tablealias, $value) {
121         global $DB;
123         static $i=0;
124         $i++;
125         $name = "df_checkbox_{$i}_";
126         $params = array();
127         $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
129         $allrequired = $value['allrequired'];
130         $selected    = $value['checked'];
132         if ($selected) {
133             $conditions = array();
134             $j=0;
135             foreach ($selected as $sel) {
136                 $j++;
137                 $xname = $name.$j;
138                 $likesel = str_replace('%', '\%', $sel);
139                 $likeselsel = str_replace('_', '\_', $likesel);
140                 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
141                                                                                OR {$tablealias}.content LIKE :{$xname}b
142                                                                                OR {$tablealias}.content LIKE :{$xname}c
143                                                                                OR {$tablealias}.content LIKE :{$xname}d))";
144                 $params[$xname.'a'] = $sel;
145                 $params[$xname.'b'] = "$likesel##%";
146                 $params[$xname.'c'] = "%##$likesel";
147                 $params[$xname.'d'] = "%##$likesel##%";
148             }
149             if ($allrequired) {
150                 return array(" (".implode(" AND ", $conditions).") ", $params);
151             } else {
152                 return array(" (".implode(" OR ", $conditions).") ", $params);
153             }
154         } else {
155             return array(" ", array());
156         }
157     }
159     function update_content($recordid, $value, $name='') {
160         global $DB;
162         $content = new stdClass();
163         $content->fieldid = $this->field->id;
164         $content->recordid = $recordid;
165         $content->content = $this->format_data_field_checkbox_content($value);
167         if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
168             $content->id = $oldcontent->id;
169             return $DB->update_record('data_content', $content);
170         } else {
171             return $DB->insert_record('data_content', $content);
172         }
173     }
175     function display_browse_field($recordid, $template) {
176         global $DB;
178         if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
179             if (empty($content->content)) {
180                 return false;
181             }
183             $options = explode("\n",$this->field->param1);
184             $options = array_map('trim', $options);
186             $contentArr = explode('##', $content->content);
187             $str = '';
188             foreach ($contentArr as $line) {
189                 if (!in_array($line, $options)) {
190                     // hmm, looks like somebody edited the field definition
191                     continue;
192                 }
193                 $str .= $line . "<br />\n";
194             }
195             return $str;
196         }
197         return false;
198     }
200     function format_data_field_checkbox_content($content) {
201         if (!is_array($content)) {
202             return NULL;
203         }
204         $options = explode("\n", $this->field->param1);
205         $options = array_map('trim', $options);
207         $vals = array();
208         foreach ($content as $key=>$val) {
209             if ($key === 'xxx') {
210                 continue;
211             }
212             if (!in_array($val, $options)) {
213                 continue;
215             }
216             $vals[] = $val;
217         }
219         if (empty($vals)) {
220             return NULL;
221         }
223         return implode('##', $vals);
224     }
226     /**
227      * Check whether any boxes in the checkbox where checked.
228      *
229      * @param mixed $value The submitted values
230      * @param mixed $name
231      * @return bool
232      */
233     function notemptyfield($value, $name) {
234         $found = false;
235         foreach ($value as $checkboxitem) {
236             if (!empty($checkboxitem)) {
237                 $found = true;
238                 break;
239             }
240         }
241         return $found;
242     }