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 |
25 | class data_field_multimenu extends data_field_base { |
26 | |
27 | var $type = 'multimenu'; |
2c5c1418 |
28 | |
0997e51a |
29 | function data_field_multimenu($field=0, $data=0) { |
30 | parent::data_field_base($field, $data); |
2c5c1418 |
31 | } |
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">'; |
2c5c1418 |
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>'; |
2c5c1418 |
60 | |
61 | return $str; |
62 | } |
63 | |
0997e51a |
64 | function update_content($recordid, $value, $name='') { |
2c5c1418 |
65 | $content = new object; |
0997e51a |
66 | $content->fieldid = $this->field->id; |
2c5c1418 |
67 | $content->recordid = $recordid; |
68 | $content->content = $this->format_data_field_multimenu_content($value); |
0997e51a |
69 | |
1b0d9dca |
70 | if ($oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) { |
2c5c1418 |
71 | $content->id = $oldcontent->id; |
0997e51a |
72 | return update_record('data_content', $content); |
73 | } else { |
74 | return insert_record('data_content', $content); |
2c5c1418 |
75 | } |
76 | } |
77 | |
e1791b72 |
78 | function format_data_field_multimenu_content($content) { |
79 | if (!is_array($content)) { |
80 | $str = $content; |
81 | } else { |
82 | $str = ''; |
83 | foreach ($content as $val) { |
84 | $str .= $val . '##'; |
85 | } |
86 | $str = substr($str, 0, -2); |
87 | } |
88 | $str = clean_param($str, PARAM_NOTAGS); |
89 | return $str; |
2c5c1418 |
90 | } |
91 | |
92 | |
0997e51a |
93 | function display_browse_field($recordid, $template) { |
2c5c1418 |
94 | |
0997e51a |
95 | if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)){ |
bbe39b6c |
96 | $contentArr = array(); |
97 | if (!empty($content->content)) { |
98 | $contentArr = explode('##', $content->content); |
99 | } |
100 | $str = ''; |
101 | foreach ($contentArr as $line) { |
102 | $str .= $line . "<br />\n"; |
103 | } |
104 | return $str; |
2c5c1418 |
105 | } |
106 | return false; |
107 | } |
108 | } |
0997e51a |
109 | ?> |