Fix for Bug #4954
[moodle.git] / mod / data / field / multimenu / field.class.php
CommitLineData
2c5c1418 1<?php ///Class file for textarea field, extends base_field
2///////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
9// Copyright (C) 2005 Martin Dougiamas http://dougiamas.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///////////////////////////////////////////////////////////////////////////
24
25/// Please refer to lib.php for method comments
26
27class data_field_multimenu extends data_field_base {
28
29 var $type = 'multimenu';
30 var $id;
31
32
33 function data_field_multimenu($fid=0){
34 parent::data_field_base($fid);
35 }
36
37
38 /***********************************************
39 * Saves the field into the database *
40 ***********************************************/
41 function insert_field($dataid, $type='multimenu', $name, $desc='', $options='') {
42 $newfield = new object;
43 $newfield->dataid = $dataid;
44 $newfield->type = $type;
45 $newfield->name = $name;
46 $newfield->description = $desc;
47 $newfield->param1 = $options;
48
49 if (!insert_record('data_fields', $newfield)) {
50 notify('Insertion of new field failed!');
51 }
52 }
53
54
55 /***********************************************
56 * Prints the form element in the add template *
57 ***********************************************/
58 function display_add_field($id, $rid=0) {
59 global $CFG;
60 if (!$field = get_record('data_fields', 'id', $id)){
61 notify('That is not a valid field id!');
62 exit;
63 }
c87fbb27 64 $content = array();
65
2c5c1418 66 if ($rid) {
c87fbb27 67 $dbcontent = get_record('data_content', 'fieldid', $id, 'recordid', $rid);
68 if (isset($dbcontent->content)) {
69 $content = $dbcontent->content;
2c5c1418 70 $content = explode('##', $content);
71 }
72 }
2c5c1418 73 $str = '';
bbe39b6c 74 /*
2c5c1418 75 if ($field->description) {
76 $str .= '<img src="'.$CFG->pixpath.'/help.gif" alt="'.$field->description.'" title="'.$field->description.'" />&nbsp;';
77 }
bbe39b6c 78 */
79 $str .= '<div title="'.$field->description.'">';
2c5c1418 80 $str .= '<select name="field_' . $field->id . '[]" id="field_' . $field->id . '" multiple="multiple">';
81
82 foreach (explode("\n",$field->param1) as $option) {
83 $option = ltrim(rtrim($option));
84 $str .= '<option value="' . $option . '"';
85
86 if (array_search($option, $content) !== false) {
87 // Selected by user.
88 $str .= ' selected >';
89 }
90 else {
91 $str .= '>';
92 }
93 $str .= $option . '</option>';
94 }
95 $str .= '</select>';
bbe39b6c 96 $str .= '</div>';
2c5c1418 97
98 return $str;
99 }
100
101
102 function display_edit_field($id, $mode=0) {
103 parent::display_edit_field($id, $mode);
104 }
105
88f47f81 106
2c5c1418 107
108 function update($fieldobject) {
109 $fieldobject->param2 = trim($fieldobject->param1);
110
111 if (!update_record('data_fields',$fieldobject)){
112 notify ('upate failed');
113 }
114 }
115
116
117 function store_data_content($fieldid, $recordid, $value) {
118 $content = new object;
119 $content->fieldid = $fieldid;
120 $content->recordid = $recordid;
121 $content->content = $this->format_data_field_multimenu_content($value);
122 insert_record('data_content', $content);
123 }
124
125
126 function update_data_content($fieldid, $recordid, $value) {
127 $content = new object;
128 $content->fieldid = $fieldid;
129 $content->recordid = $recordid;
130 $content->content = $this->format_data_field_multimenu_content($value);
131
132 if ($oldcontent = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)) {
133 $content->id = $oldcontent->id;
134 update_record('data_content', $content);
135 }
136 else {
137 $this->store_data_content($fieldid, $recordid, $value);
138 }
139 }
140
141
e1791b72 142 function format_data_field_multimenu_content($content) {
143 if (!is_array($content)) {
144 $str = $content;
145 } else {
146 $str = '';
147 foreach ($content as $val) {
148 $str .= $val . '##';
149 }
150 $str = substr($str, 0, -2);
151 }
152 $str = clean_param($str, PARAM_NOTAGS);
153 return $str;
2c5c1418 154 }
155
156
cd2f6950 157 function display_browse_field($fieldid, $recordid, $template) {
2c5c1418 158 global $CFG, $USER, $course;
159
160 $field = get_record('data_fields', 'id', $fieldid);
161
c87fbb27 162 if ($content = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)){
bbe39b6c 163 $contentArr = array();
164 if (!empty($content->content)) {
165 $contentArr = explode('##', $content->content);
166 }
167 $str = '';
168 foreach ($contentArr as $line) {
169 $str .= $line . "<br />\n";
170 }
171 return $str;
2c5c1418 172 }
173 return false;
174 }
175}
e1791b72 176?>