Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / data / field / multimenu / field.class.php
blob038a494faee5d92d1559f587c267262629f1e34f
1 <?php // $Id$
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_multimenu extends data_field_base {
27 var $type = 'multimenu';
29 function data_field_multimenu($field=0, $data=0) {
30 parent::data_field_base($field, $data);
34 function display_add_field($recordid=0) {
36 if ($recordid){
37 $content = get_field('data_content', 'content', 'fieldid', $this->field->id, 'recordid', $recordid);
38 $content = explode('##', $content);
39 } else {
40 $content = array();
43 $str = '<div title="'.s($this->field->description).'">';
44 $str .= '<input name="field_' . $this->field->id . '[xxx]" type="hidden" value="xxx"/>'; // hidden field - needed for empty selection
45 $str .= '<select name="field_' . $this->field->id . '[]" id="field_' . $this->field->id . '" multiple="multiple">';
47 foreach (explode("\n",$this->field->param1) as $option) {
48 $option = trim($option);
49 $str .= '<option value="' . s($option) . '"';
51 if (in_array($option, $content)) {
52 // Selected by user.
53 $str .= ' selected = "selected"';
56 $str .= '>';
57 $str .= $option . '</option>';
59 $str .= '</select>';
60 $str .= '</div>';
62 return $str;
65 function display_search_field($value = '') {
66 global $CFG;
68 if (is_array($value)){
69 $content = $value['selected'];
70 $allrequired = $value['allrequired'] ? 'checked = "checked"' : '';
71 } else {
72 $content = array();
73 $allrequired = '';
76 static $c = 0;
78 $str = '<select name="f_'.$this->field->id.'[]" multiple="multiple">';
80 // display only used options
81 $usedoptions = array();
82 $sql = "SELECT DISTINCT content
83 FROM {$CFG->prefix}data_content
84 WHERE fieldid={$this->field->id} AND content IS NOT NULL";
85 if ($used = get_records_sql($sql)) {
86 foreach ($used as $data) {
87 $valuestr = $data->content;
88 if ($valuestr === '') {
89 continue;
91 $values = explode('##', $valuestr);
92 foreach ($values as $value) {
93 $usedoptions[$value] = $value;
98 $found = false;
99 foreach (explode("\n",$this->field->param1) as $option) {
100 $option = trim($option);
101 if (!isset($usedoptions[$option])) {
102 continue;
104 $found = true;
105 $str .= '<option value="' . s($option) . '"';
107 if (in_array(addslashes($option), $content)) {
108 // Selected by user.
109 $str .= ' selected = "selected"';
111 $str .= '>' . $option . '</option>';
113 if (!$found) {
114 // oh, nothing to search for
115 return '';
118 $str .= '</select>';
120 $str .= '&nbsp;<input name="f_'.$this->field->id.'_allreq" id="f_'.$this->field->id.'_allreq'.$c.'" type="checkbox" '.$allrequired.'/>';
121 $str .= '<label for="f_'.$this->field->id.'_allreq'.$c.'">'.get_string('selectedrequired', 'data').'</label>';
122 $c++;
124 return $str;
128 function parse_search_field() {
129 $selected = optional_param('f_'.$this->field->id, array(), PARAM_NOTAGS);
130 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
131 if (empty($selected)) {
132 // no searching
133 return '';
135 return array('selected'=>$selected, 'allrequired'=>$allrequired);
138 function generate_sql($tablealias, $value) {
139 $allrequired = $value['allrequired'];
140 $selected = $value['selected'];
142 if ($selected) {
143 $conditions = array();
144 foreach ($selected as $sel) {
145 $likesel = str_replace('%', '\%', $sel);
146 $likeselsel = str_replace('_', '\_', $likesel);
147 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$tablealias}.content = '$sel'
148 OR {$tablealias}.content LIKE '$likesel##%'
149 OR {$tablealias}.content LIKE '%##$likesel'
150 OR {$tablealias}.content LIKE '%##$likesel##%'))";
152 if ($allrequired) {
153 return " (".implode(" AND ", $conditions).") ";
154 } else {
155 return " (".implode(" OR ", $conditions).") ";
157 } else {
158 return " ";
162 function update_content($recordid, $value, $name='') {
163 $content = new object;
164 $content->fieldid = $this->field->id;
165 $content->recordid = $recordid;
166 $content->content = $this->format_data_field_multimenu_content($value);
168 if ($oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) {
169 $content->id = $oldcontent->id;
170 return update_record('data_content', $content);
171 } else {
172 return insert_record('data_content', $content);
176 function format_data_field_multimenu_content($content) {
177 if (!is_array($content)) {
178 return NULL;
180 $options = explode("\n", $this->field->param1);
181 $options = array_map('trim', $options);
183 $vals = array();
184 foreach ($content as $key=>$val) {
185 if ($key === 'xxx') {
186 continue;
188 if (!in_array(stripslashes($val), $options)) {
189 continue;
191 $vals[] = $val;
194 if (empty($vals)) {
195 return NULL;
198 return implode('##', $vals);
202 function display_browse_field($recordid, $template) {
204 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) {
205 if (empty($content->content)) {
206 return false;
209 $options = explode("\n",$this->field->param1);
210 $options = array_map('trim', $options);
212 $contentArr = explode('##', $content->content);
213 $str = '';
214 foreach ($contentArr as $line) {
215 if (!in_array($line, $options)) {
216 // hmm, looks like somebody edited the field definition
217 continue;
219 $str .= $line . "<br />\n";
221 return $str;
223 return false;