MDL-5583 mod_data: Add required fields
[moodle.git] / mod / data / field / multimenu / field.class.php
CommitLineData
1adbd2c3 1<?php
2c5c1418 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 //
2c5c1418 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
2c5c1418 25class data_field_multimenu extends data_field_base {
26
27 var $type = 'multimenu';
aab98aaf 28
b89cca19 29 function display_add_field($recordid = 0, $formdata = null) {
a656d951 30 global $DB;
0997e51a 31
b89cca19
DW
32 if ($formdata) {
33 $fieldname = 'field_' . $this->field->id;
34 $content = $formdata->$fieldname;
35 } else if ($recordid){
a656d951 36 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
0997e51a 37 $content = explode('##', $content);
38 } else {
39 $content = array();
2c5c1418 40 }
0997e51a 41
e357c206 42 $str = '<div title="'.s($this->field->description).'">';
593b0229 43 $str .= '<input name="field_' . $this->field->id . '[xxx]" type="hidden" value="xxx"/>'; // hidden field - needed for empty selection
24a0e859 44 $str .= '<label class="accesshide" for="field_' . $this->field->id . '">' . $this->field->name. '</label>';
0997e51a 45 $str .= '<select name="field_' . $this->field->id . '[]" id="field_' . $this->field->id . '" multiple="multiple">';
aab98aaf 46
0997e51a 47 foreach (explode("\n",$this->field->param1) as $option) {
48 $option = trim($option);
e357c206 49 $str .= '<option value="' . s($option) . '"';
2c5c1418 50
593b0229 51 if (in_array($option, $content)) {
2c5c1418 52 // Selected by user.
593b0229 53 $str .= ' selected = "selected"';
2c5c1418 54 }
593b0229 55
56 $str .= '>';
2c5c1418 57 $str .= $option . '</option>';
58 }
59 $str .= '</select>';
bbe39b6c 60 $str .= '</div>';
aab98aaf 61
2c5c1418 62 return $str;
63 }
9a36be02 64
7900ecb0 65 function display_search_field($value = '') {
a656d951 66 global $CFG, $DB;
0d505ace 67
68 if (is_array($value)){
69 $content = $value['selected'];
15a18282 70 $allrequired = $value['allrequired'] ? true : false;
0d505ace 71 } else {
72 $content = array();
15a18282 73 $allrequired = false;
0d505ace 74 }
75
76 static $c = 0;
77
01f8e80d
RW
78 $str = '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>';
79 $str .= '<select id="f_'.$this->field->id.'" name="f_'.$this->field->id.'[]" multiple="multiple">';
0d505ace 80
9a36be02 81 // display only used options
eca89126
EL
82 $varcharcontent = $DB->sql_compare_text('content', 255);
83 $sql = "SELECT DISTINCT $varcharcontent AS content
a656d951 84 FROM {data_content}
85 WHERE fieldid=? AND content IS NOT NULL";
eca89126
EL
86
87 $usedoptions = array();
a656d951 88 if ($used = $DB->get_records_sql($sql, array($this->field->id))) {
9a36be02 89 foreach ($used as $data) {
90 $valuestr = $data->content;
a4e3818f 91 if ($valuestr === '') {
9a36be02 92 continue;
93 }
94 $values = explode('##', $valuestr);
95 foreach ($values as $value) {
96 $usedoptions[$value] = $value;
97 }
98 }
99 }
100
101 $found = false;
0d505ace 102 foreach (explode("\n",$this->field->param1) as $option) {
103 $option = trim($option);
9a36be02 104 if (!isset($usedoptions[$option])) {
105 continue;
106 }
107 $found = true;
0d505ace 108 $str .= '<option value="' . s($option) . '"';
109
a656d951 110 if (in_array($option, $content)) {
0d505ace 111 // Selected by user.
593b0229 112 $str .= ' selected = "selected"';
7900ecb0 113 }
593b0229 114 $str .= '>' . $option . '</option>';
7900ecb0 115 }
9a36be02 116 if (!$found) {
117 // oh, nothing to search for
118 return '';
119 }
120
0d505ace 121 $str .= '</select>';
122
15a18282 123 $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data'));
0d505ace 124
125 return $str;
126
7900ecb0 127 }
9a36be02 128
7900ecb0 129 function parse_search_field() {
18bd7573 130 $selected = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS);
0d505ace 131 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
132 if (empty($selected)) {
133 // no searching
134 return '';
135 }
136 return array('selected'=>$selected, 'allrequired'=>$allrequired);
7900ecb0 137 }
138
139 function generate_sql($tablealias, $value) {
15a18282
EL
140 global $DB;
141
e3487936 142 static $i=0;
143 $i++;
144 $name = "df_multimenu_{$i}_";
145 $params = array();
15a18282 146 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
e3487936 147
0d505ace 148 $allrequired = $value['allrequired'];
149 $selected = $value['selected'];
150
151 if ($selected) {
152 $conditions = array();
e3487936 153 $j=0;
0d505ace 154 foreach ($selected as $sel) {
e3487936 155 $j++;
156 $xname = $name.$j;
9a36be02 157 $likesel = str_replace('%', '\%', $sel);
158 $likeselsel = str_replace('_', '\_', $likesel);
15a18282 159 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
e3487936 160 OR {$tablealias}.content LIKE :{$xname}b
161 OR {$tablealias}.content LIKE :{$xname}c
162 OR {$tablealias}.content LIKE :{$xname}d))";
163 $params[$xname.'a'] = $sel;
164 $params[$xname.'b'] = "$likesel##%";
165 $params[$xname.'c'] = "%##$likesel";
166 $params[$xname.'d'] = "%##$likesel##%";
0d505ace 167 }
168 if ($allrequired) {
e3487936 169 return array(" (".implode(" AND ", $conditions).") ", $params);
0d505ace 170 } else {
e3487936 171 return array(" (".implode(" OR ", $conditions).") ", $params);
0d505ace 172 }
173 } else {
e3487936 174 return array(" ", array());
9a36be02 175 }
7900ecb0 176 }
2c5c1418 177
0997e51a 178 function update_content($recordid, $value, $name='') {
a656d951 179 global $DB;
180
39790bd8 181 $content = new stdClass();
593b0229 182 $content->fieldid = $this->field->id;
2c5c1418 183 $content->recordid = $recordid;
593b0229 184 $content->content = $this->format_data_field_multimenu_content($value);
0997e51a 185
a656d951 186 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
2c5c1418 187 $content->id = $oldcontent->id;
a656d951 188 return $DB->update_record('data_content', $content);
0997e51a 189 } else {
a656d951 190 return $DB->insert_record('data_content', $content);
2c5c1418 191 }
192 }
aab98aaf 193
e1791b72 194 function format_data_field_multimenu_content($content) {
195 if (!is_array($content)) {
593b0229 196 return NULL;
197 }
198 $options = explode("\n", $this->field->param1);
199 $options = array_map('trim', $options);
200
201 $vals = array();
202 foreach ($content as $key=>$val) {
203 if ($key === 'xxx') {
204 continue;
205 }
a656d951 206 if (!in_array($val, $options)) {
593b0229 207 continue;
e1791b72 208 }
593b0229 209 $vals[] = $val;
e1791b72 210 }
593b0229 211
212 if (empty($vals)) {
213 return NULL;
214 }
215
216 return implode('##', $vals);
2c5c1418 217 }
aab98aaf 218
219
0997e51a 220 function display_browse_field($recordid, $template) {
a656d951 221 global $DB;
2c5c1418 222
a656d951 223 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
9a36be02 224 if (empty($content->content)) {
225 return false;
bbe39b6c 226 }
9a36be02 227
228 $options = explode("\n",$this->field->param1);
229 $options = array_map('trim', $options);
230
231 $contentArr = explode('##', $content->content);
bbe39b6c 232 $str = '';
233 foreach ($contentArr as $line) {
9a36be02 234 if (!in_array($line, $options)) {
235 // hmm, looks like somebody edited the field definition
236 continue;
237 }
bbe39b6c 238 $str .= $line . "<br />\n";
239 }
240 return $str;
2c5c1418 241 }
242 return false;
243 }
244}