2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
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. //
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: //
21 // http://www.gnu.org/copyleft/gpl.html //
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) {
37 $content = get_field('data_content', 'content', 'fieldid', $this->field
->id
, 'recordid', $recordid);
38 $content = explode('##', $content);
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)) {
53 $str .= ' selected = "selected"';
57 $str .= $option . '</option>';
65 function display_search_field($value = '') {
68 if (is_array($value)){
69 $content = $value['selected'];
70 $allrequired = $value['allrequired'] ?
'checked = "checked"' : '';
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 === '') {
91 $values = explode('##', $valuestr);
92 foreach ($values as $value) {
93 $usedoptions[$value] = $value;
99 foreach (explode("\n",$this->field
->param1
) as $option) {
100 $option = trim($option);
101 if (!isset($usedoptions[$option])) {
105 $str .= '<option value="' . s($option) . '"';
107 if (in_array(addslashes($option), $content)) {
109 $str .= ' selected = "selected"';
111 $str .= '>' . $option . '</option>';
114 // oh, nothing to search for
120 $str .= ' <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>';
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)) {
135 return array('selected'=>$selected, 'allrequired'=>$allrequired);
138 function generate_sql($tablealias, $value) {
139 $allrequired = $value['allrequired'];
140 $selected = $value['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##%'))";
153 return " (".implode(" AND ", $conditions).") ";
155 return " (".implode(" OR ", $conditions).") ";
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);
172 return insert_record('data_content', $content);
176 function format_data_field_multimenu_content($content) {
177 if (!is_array($content)) {
180 $options = explode("\n", $this->field
->param1
);
181 $options = array_map('trim', $options);
184 foreach ($content as $key=>$val) {
185 if ($key === 'xxx') {
188 if (!in_array(stripslashes($val), $options)) {
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
)) {
209 $options = explode("\n",$this->field
->param1
);
210 $options = array_map('trim', $options);
212 $contentArr = explode('##', $content->content
);
214 foreach ($contentArr as $line) {
215 if (!in_array($line, $options)) {
216 // hmm, looks like somebody edited the field definition
219 $str .= $line . "<br />\n";