MDL-5583 behat: Add multiline version of 'And I set the field to'
[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='') {
15a18282
EL
68 global $CFG, $DB;
69
cf353d88 70 if (is_array($value)) {
d31629c6 71 $content = $value['checked'];
15a18282 72 $allrequired = $value['allrequired'] ? true : false;
d31629c6
DC
73 } else {
74 $content = array();
15a18282 75 $allrequired = false;
d31629c6
DC
76 }
77
78 $str = '';
79 $found = false;
80 foreach (explode("\n",$this->field->param1) as $checkbox) {
81 $checkbox = trim($checkbox);
82
24b81889 83 if (in_array($checkbox, $content)) {
2f0e96e4 84 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), true, $checkbox);
cf353d88 85 } else {
86 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), false, $checkbox);
7900ecb0 87 }
d31629c6
DC
88 $found = true;
89 }
90 if (!$found) {
91 return '';
7900ecb0 92 }
d31629c6 93
15a18282 94 $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data'));
d31629c6 95 return $str;
7900ecb0 96 }
1adbd2c3 97
7900ecb0 98 function parse_search_field() {
18bd7573 99 $selected = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS);
d31629c6
DC
100 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
101 if (empty($selected)) {
102 // no searching
103 return '';
104 }
105 return array('checked'=>$selected, 'allrequired'=>$allrequired);
7900ecb0 106 }
1adbd2c3 107
7900ecb0 108 function generate_sql($tablealias, $value) {
15a18282
EL
109 global $DB;
110
e3487936 111 static $i=0;
112 $i++;
15a18282
EL
113 $name = "df_checkbox_{$i}_";
114 $params = array();
115 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
116
d31629c6
DC
117 $allrequired = $value['allrequired'];
118 $selected = $value['checked'];
119
120 if ($selected) {
121 $conditions = array();
15a18282 122 $j=0;
d31629c6 123 foreach ($selected as $sel) {
15a18282
EL
124 $j++;
125 $xname = $name.$j;
d31629c6
DC
126 $likesel = str_replace('%', '\%', $sel);
127 $likeselsel = str_replace('_', '\_', $likesel);
15a18282
EL
128 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
129 OR {$tablealias}.content LIKE :{$xname}b
130 OR {$tablealias}.content LIKE :{$xname}c
131 OR {$tablealias}.content LIKE :{$xname}d))";
132 $params[$xname.'a'] = $sel;
133 $params[$xname.'b'] = "$likesel##%";
134 $params[$xname.'c'] = "%##$likesel";
135 $params[$xname.'d'] = "%##$likesel##%";
d31629c6
DC
136 }
137 if ($allrequired) {
15a18282 138 return array(" (".implode(" AND ", $conditions).") ", $params);
d31629c6 139 } else {
15a18282 140 return array(" (".implode(" OR ", $conditions).") ", $params);
d31629c6
DC
141 }
142 } else {
143 return array(" ", array());
144 }
7900ecb0 145 }
146
0997e51a 147 function update_content($recordid, $value, $name='') {
a656d951 148 global $DB;
149
39790bd8 150 $content = new stdClass();
0997e51a 151 $content->fieldid = $this->field->id;
4728968f 152 $content->recordid = $recordid;
153 $content->content = $this->format_data_field_checkbox_content($value);
aab98aaf 154
a656d951 155 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
4728968f 156 $content->id = $oldcontent->id;
a656d951 157 return $DB->update_record('data_content', $content);
e1791b72 158 } else {
a656d951 159 return $DB->insert_record('data_content', $content);
e1791b72 160 }
4728968f 161 }
aab98aaf 162
0997e51a 163 function display_browse_field($recordid, $template) {
a656d951 164 global $DB;
aab98aaf 165
a656d951 166 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
d31629c6
DC
167 if (empty($content->content)) {
168 return false;
4728968f 169 }
d31629c6
DC
170
171 $options = explode("\n",$this->field->param1);
172 $options = array_map('trim', $options);
173
174 $contentArr = explode('##', $content->content);
4728968f 175 $str = '';
176 foreach ($contentArr as $line) {
d31629c6
DC
177 if (!in_array($line, $options)) {
178 // hmm, looks like somebody edited the field definition
179 continue;
180 }
4728968f 181 $str .= $line . "<br />\n";
182 }
183 return $str;
184 }
185 return false;
186 }
0997e51a 187
188 function format_data_field_checkbox_content($content) {
189 if (!is_array($content)) {
d31629c6
DC
190 return NULL;
191 }
192 $options = explode("\n", $this->field->param1);
193 $options = array_map('trim', $options);
194
195 $vals = array();
196 foreach ($content as $key=>$val) {
197 if ($key === 'xxx') {
198 continue;
0997e51a 199 }
798b0b33 200 if (!in_array($val, $options)) {
d31629c6
DC
201 continue;
202
203 }
204 $vals[] = $val;
0997e51a 205 }
d31629c6
DC
206
207 if (empty($vals)) {
208 return NULL;
209 }
210
211 return implode('##', $vals);
0997e51a 212 }
d31629c6 213
4728968f 214}
1adbd2c3 215