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