MDL-21437 data - number and date fields search under mssql and oracle
[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
0997e51a 29 function display_add_field($recordid=0) {
a656d951 30 global $CFG, $DB;
aab98aaf 31
c87fbb27 32 $content = array();
0997e51a 33
34 if ($recordid) {
a656d951 35 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
0997e51a 36 $content = explode('##', $content);
d31629c6
DC
37 } else {
38 $content = array();
4728968f 39 }
0997e51a 40
e357c206 41 $str = '<div title="'.s($this->field->description).'">';
42 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
aab98aaf 43
e357c206 44 $i = 0;
423bd918 45 foreach (explode("\n", $this->field->param1) as $checkbox) {
46 $checkbox = trim($checkbox);
6c31d376 47 if ($checkbox === '') {
48 continue; // skip empty lines
49 }
f62f7d8f 50 $str .= '<input type="hidden" name="field_' . $this->field->id . '[]" value="" />';
e357c206 51 $str .= '<input type="checkbox" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '[]" ';
52 $str .= 'value="' . s($checkbox) . '" ';
aab98aaf 53
4728968f 54 if (array_search($checkbox, $content) !== false) {
d31629c6 55 $str .= 'checked />';
0997e51a 56 } else {
4728968f 57 $str .= '/>';
58 }
e357c206 59 $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$checkbox.'</label><br />';
60 $i++;
4728968f 61 }
aab98aaf 62 $str .= '</fieldset>';
bbe39b6c 63 $str .= '</div>';
4728968f 64 return $str;
65 }
66
7900ecb0 67 function display_search_field($value='') {
601104f2 68 global $CFG, $DB, $OUTPUT;
cf353d88 69 if (is_array($value)) {
d31629c6
DC
70 $content = $value['checked'];
71 $allrequired = $value['allrequired'] ? 'checked = "checked"' : '';
72 } else {
73 $content = array();
74 $allrequired = '';
75 }
76
77 $str = '';
78 $found = false;
79 foreach (explode("\n",$this->field->param1) as $checkbox) {
80 $checkbox = trim($checkbox);
81
24b81889 82 if (in_array($checkbox, $content)) {
2f0e96e4 83 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), true, $checkbox);
cf353d88 84 } else {
85 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), false, $checkbox);
7900ecb0 86 }
d31629c6
DC
87 $found = true;
88 }
89 if (!$found) {
90 return '';
7900ecb0 91 }
d31629c6 92
2f0e96e4 93 $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, false, get_string('selectedrequired', 'data'));
d31629c6 94 return $str;
7900ecb0 95 }
1adbd2c3 96
7900ecb0 97 function parse_search_field() {
d31629c6
DC
98 $selected = optional_param('f_'.$this->field->id, array(), PARAM_NOTAGS);
99 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
100 if (empty($selected)) {
101 // no searching
102 return '';
103 }
104 return array('checked'=>$selected, 'allrequired'=>$allrequired);
7900ecb0 105 }
1adbd2c3 106
7900ecb0 107 function generate_sql($tablealias, $value) {
e3487936 108 static $i=0;
109 $i++;
110 $name = "df_checkbox_$i";
d31629c6
DC
111 $allrequired = $value['allrequired'];
112 $selected = $value['checked'];
113
114 if ($selected) {
115 $conditions = array();
116 foreach ($selected as $sel) {
117 $likesel = str_replace('%', '\%', $sel);
118 $likeselsel = str_replace('_', '\_', $likesel);
119 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$tablealias}.content = '$sel'
120 OR {$tablealias}.content LIKE '$likesel##%'
121 OR {$tablealias}.content LIKE '%##$likesel'
122 OR {$tablealias}.content LIKE '%##$likesel##%'))";
123 }
124 if ($allrequired) {
125 return array(" (".implode(" AND ", $conditions).") ", array($name=>$value));
126 } else {
127 return array(" (".implode(" OR ", $conditions).") ", array($name=>$value));
128 }
129 } else {
130 return array(" ", array());
131 }
7900ecb0 132 }
133
0997e51a 134 function update_content($recordid, $value, $name='') {
a656d951 135 global $DB;
136
39790bd8 137 $content = new stdClass();
0997e51a 138 $content->fieldid = $this->field->id;
4728968f 139 $content->recordid = $recordid;
140 $content->content = $this->format_data_field_checkbox_content($value);
aab98aaf 141
a656d951 142 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
4728968f 143 $content->id = $oldcontent->id;
a656d951 144 return $DB->update_record('data_content', $content);
e1791b72 145 } else {
a656d951 146 return $DB->insert_record('data_content', $content);
e1791b72 147 }
4728968f 148 }
aab98aaf 149
0997e51a 150 function display_browse_field($recordid, $template) {
a656d951 151 global $DB;
aab98aaf 152
a656d951 153 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
d31629c6
DC
154 if (empty($content->content)) {
155 return false;
4728968f 156 }
d31629c6
DC
157
158 $options = explode("\n",$this->field->param1);
159 $options = array_map('trim', $options);
160
161 $contentArr = explode('##', $content->content);
4728968f 162 $str = '';
163 foreach ($contentArr as $line) {
d31629c6
DC
164 if (!in_array($line, $options)) {
165 // hmm, looks like somebody edited the field definition
166 continue;
167 }
4728968f 168 $str .= $line . "<br />\n";
169 }
170 return $str;
171 }
172 return false;
173 }
0997e51a 174
175 function format_data_field_checkbox_content($content) {
176 if (!is_array($content)) {
d31629c6
DC
177 return NULL;
178 }
179 $options = explode("\n", $this->field->param1);
180 $options = array_map('trim', $options);
181
182 $vals = array();
183 foreach ($content as $key=>$val) {
184 if ($key === 'xxx') {
185 continue;
0997e51a 186 }
798b0b33 187 if (!in_array($val, $options)) {
d31629c6
DC
188 continue;
189
190 }
191 $vals[] = $val;
0997e51a 192 }
d31629c6
DC
193
194 if (empty($vals)) {
195 return NULL;
196 }
197
198 return implode('##', $vals);
0997e51a 199 }
d31629c6 200
4728968f 201}
1adbd2c3 202