MDL-9907 xhtml strict; merged from MOODLE_19_STABLE
[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) {
35
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();
2c5c1418 41 }
0997e51a 42
e357c206 43 $str = '<div title="'.s($this->field->description).'">';
593b0229 44 $str .= '<input name="field_' . $this->field->id . '[xxx]" type="hidden" value="xxx"/>'; // hidden field - needed for empty selection
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 }
7900ecb0 64
65 function display_search_field($value = '') {
66 global $CFG;
0d505ace 67
68 if (is_array($value)){
69 $content = $value['selected'];
70 $allrequired = $value['allrequired'] ? 'checked = "checked"' : '';
71 } else {
72 $content = array();
73 $allrequired = '';
74 }
75
76 static $c = 0;
77
78 $str = '<select name="f_'.$this->field->id.'[]" multiple="multiple">';
79
80 foreach (explode("\n",$this->field->param1) as $option) {
81 $option = trim($option);
82 $str .= '<option value="' . s($option) . '"';
83
593b0229 84 if (in_array(addslashes($option), $content)) {
0d505ace 85 // Selected by user.
593b0229 86 $str .= ' selected = "selected"';
7900ecb0 87 }
593b0229 88 $str .= '>' . $option . '</option>';
7900ecb0 89 }
0d505ace 90 $str .= '</select>';
91
92 $str .= '&nbsp;<input name="f_'.$this->field->id.'_allreq" id="f_'.$this->field->id.'_allreq'.$c.'" type="checkbox" '.$allrequired.'/>';
93 $str .= '<label for="f_'.$this->field->id.'_allreq'.$c.'">'.get_string('selectedrequired', 'data').'</label>';
94 $c++;
95
96 return $str;
97
7900ecb0 98 }
99
100 function parse_search_field() {
0d505ace 101 $selected = optional_param('f_'.$this->field->id, array(), PARAM_NOTAGS);
102 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
103 if (empty($selected)) {
104 // no searching
105 return '';
106 }
107 return array('selected'=>$selected, 'allrequired'=>$allrequired);
7900ecb0 108 }
109
110 function generate_sql($tablealias, $value) {
0d505ace 111 $allrequired = $value['allrequired'];
112 $selected = $value['selected'];
113
114 if ($selected) {
115 $conditions = array();
116 foreach ($selected as $sel) {
117 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$tablealias}.content LIKE '$sel##%' OR {$tablealias}.content LIKE '%##$sel' OR {$tablealias}.content LIKE '%##$sel##%'))";
118 }
119 if ($allrequired) {
120 return " (".implode(" AND ", $conditions).") ";
121 } else {
122 return " (".implode(" OR ", $conditions).") ";
123 }
124 } else {
125 return " ";
126 }
7900ecb0 127 }
2c5c1418 128
0997e51a 129 function update_content($recordid, $value, $name='') {
2c5c1418 130 $content = new object;
593b0229 131 $content->fieldid = $this->field->id;
2c5c1418 132 $content->recordid = $recordid;
593b0229 133 $content->content = $this->format_data_field_multimenu_content($value);
0997e51a 134
1b0d9dca 135 if ($oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) {
2c5c1418 136 $content->id = $oldcontent->id;
0997e51a 137 return update_record('data_content', $content);
138 } else {
139 return insert_record('data_content', $content);
2c5c1418 140 }
141 }
aab98aaf 142
e1791b72 143 function format_data_field_multimenu_content($content) {
144 if (!is_array($content)) {
593b0229 145 return NULL;
146 }
147 $options = explode("\n", $this->field->param1);
148 $options = array_map('trim', $options);
149
150 $vals = array();
151 foreach ($content as $key=>$val) {
152 if ($key === 'xxx') {
153 continue;
154 }
155 if (!in_array(stripslashes($val), $options)) {
156 continue;
e1791b72 157 }
593b0229 158 $vals[] = $val;
e1791b72 159 }
593b0229 160
161 if (empty($vals)) {
162 return NULL;
163 }
164
165 return implode('##', $vals);
2c5c1418 166 }
aab98aaf 167
168
0997e51a 169 function display_browse_field($recordid, $template) {
2c5c1418 170
0997e51a 171 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)){
bbe39b6c 172 $contentArr = array();
173 if (!empty($content->content)) {
174 $contentArr = explode('##', $content->content);
175 }
176 $str = '';
177 foreach ($contentArr as $line) {
178 $str .= $line . "<br />\n";
179 }
180 return $str;
2c5c1418 181 }
182 return false;
183 }
184}
0997e51a 185?>