MDL-14391 - Timestart shown twice in quiz overview report, instead of timefinish
[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).'">';
0997e51a 44 $str .= '<select name="field_' . $this->field->id . '[]" id="field_' . $this->field->id . '" multiple="multiple">';
aab98aaf 45
0997e51a 46 foreach (explode("\n",$this->field->param1) as $option) {
47 $option = trim($option);
e357c206 48 $str .= '<option value="' . s($option) . '"';
2c5c1418 49
50 if (array_search($option, $content) !== false) {
51 // Selected by user.
52 $str .= ' selected >';
0997e51a 53 } else {
2c5c1418 54 $str .= '>';
55 }
56 $str .= $option . '</option>';
57 }
58 $str .= '</select>';
bbe39b6c 59 $str .= '</div>';
aab98aaf 60
2c5c1418 61 return $str;
62 }
7900ecb0 63
64 function display_search_field($value = '') {
65 global $CFG;
0d505ace 66
67 if (is_array($value)){
68 $content = $value['selected'];
69 $allrequired = $value['allrequired'] ? 'checked = "checked"' : '';
70 } else {
71 $content = array();
72 $allrequired = '';
73 }
74
75 static $c = 0;
76
77 $str = '<select name="f_'.$this->field->id.'[]" multiple="multiple">';
78
79 foreach (explode("\n",$this->field->param1) as $option) {
80 $option = trim($option);
81 $str .= '<option value="' . s($option) . '"';
82
83 if (array_search($option, $content) !== false) {
84 // Selected by user.
85 $str .= ' selected >';
86 } else {
87 $str .= '>';
7900ecb0 88 }
0d505ace 89 $str .= $option . '</option>';
7900ecb0 90 }
0d505ace 91 $str .= '</select>';
92
93 $str .= '&nbsp;<input name="f_'.$this->field->id.'_allreq" id="f_'.$this->field->id.'_allreq'.$c.'" type="checkbox" '.$allrequired.'/>';
94 $str .= '<label for="f_'.$this->field->id.'_allreq'.$c.'">'.get_string('selectedrequired', 'data').'</label>';
95 $c++;
96
97 return $str;
98
7900ecb0 99 }
100
101 function parse_search_field() {
0d505ace 102 $selected = optional_param('f_'.$this->field->id, array(), PARAM_NOTAGS);
103 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
104 if (empty($selected)) {
105 // no searching
106 return '';
107 }
108 return array('selected'=>$selected, 'allrequired'=>$allrequired);
7900ecb0 109 }
110
111 function generate_sql($tablealias, $value) {
0d505ace 112 $allrequired = $value['allrequired'];
113 $selected = $value['selected'];
114
115 if ($selected) {
116 $conditions = array();
117 foreach ($selected as $sel) {
118 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$tablealias}.content LIKE '$sel##%' OR {$tablealias}.content LIKE '%##$sel' OR {$tablealias}.content LIKE '%##$sel##%'))";
119 }
120 if ($allrequired) {
121 return " (".implode(" AND ", $conditions).") ";
122 } else {
123 return " (".implode(" OR ", $conditions).") ";
124 }
125 } else {
126 return " ";
127 }
7900ecb0 128 }
2c5c1418 129
0997e51a 130 function update_content($recordid, $value, $name='') {
2c5c1418 131 $content = new object;
0997e51a 132 $content->fieldid = $this->field->id;
2c5c1418 133 $content->recordid = $recordid;
134 $content->content = $this->format_data_field_multimenu_content($value);
0997e51a 135
1b0d9dca 136 if ($oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) {
2c5c1418 137 $content->id = $oldcontent->id;
0997e51a 138 return update_record('data_content', $content);
139 } else {
140 return insert_record('data_content', $content);
2c5c1418 141 }
142 }
aab98aaf 143
e1791b72 144 function format_data_field_multimenu_content($content) {
145 if (!is_array($content)) {
146 $str = $content;
147 } else {
148 $str = '';
149 foreach ($content as $val) {
150 $str .= $val . '##';
151 }
152 $str = substr($str, 0, -2);
153 }
154 $str = clean_param($str, PARAM_NOTAGS);
155 return $str;
2c5c1418 156 }
aab98aaf 157
158
0997e51a 159 function display_browse_field($recordid, $template) {
2c5c1418 160
0997e51a 161 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)){
bbe39b6c 162 $contentArr = array();
163 if (!empty($content->content)) {
164 $contentArr = explode('##', $content->content);
165 }
166 $str = '';
167 foreach ($contentArr as $line) {
168 $str .= $line . "<br />\n";
169 }
170 return $str;
2c5c1418 171 }
172 return false;
173 }
174}
0997e51a 175?>