bug fix for 4670
[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 = '';
74
75 if ($field->description) {
76 $str .= '<img src="'.$CFG->pixpath.'/help.gif" alt="'.$field->description.'" title="'.$field->description.'" />&nbsp;';
77 }
78
79 $str .= '<select name="field_' . $field->id . '[]" id="field_' . $field->id . '" multiple="multiple">';
80
81 foreach (explode("\n",$field->param1) as $option) {
82 $option = ltrim(rtrim($option));
83 $str .= '<option value="' . $option . '"';
84
85 if (array_search($option, $content) !== false) {
86 // Selected by user.
87 $str .= ' selected >';
88 }
89 else {
90 $str .= '>';
91 }
92 $str .= $option . '</option>';
93 }
94 $str .= '</select>';
95
96 return $str;
97 }
98
99
100 function display_edit_field($id, $mode=0) {
101 parent::display_edit_field($id, $mode);
102 }
103
88f47f81 104
2c5c1418 105
106 function update($fieldobject) {
107 $fieldobject->param2 = trim($fieldobject->param1);
108
109 if (!update_record('data_fields',$fieldobject)){
110 notify ('upate failed');
111 }
112 }
113
114
115 function store_data_content($fieldid, $recordid, $value) {
116 $content = new object;
117 $content->fieldid = $fieldid;
118 $content->recordid = $recordid;
119 $content->content = $this->format_data_field_multimenu_content($value);
120 insert_record('data_content', $content);
121 }
122
123
124 function update_data_content($fieldid, $recordid, $value) {
125 $content = new object;
126 $content->fieldid = $fieldid;
127 $content->recordid = $recordid;
128 $content->content = $this->format_data_field_multimenu_content($value);
129
130 if ($oldcontent = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)) {
131 $content->id = $oldcontent->id;
132 update_record('data_content', $content);
133 }
134 else {
135 $this->store_data_content($fieldid, $recordid, $value);
136 }
137 }
138
139
140 function format_data_field_multimenu_content($contentArr) {
141 $str = '';
142 foreach ($contentArr as $val) {
143 $str .= $val . '##';
144 }
145 $str = substr($str, 0, -2);
146 $str = clean_param($str, PARAM_NOTAGS);
147 return $str;
148 }
149
150
cd2f6950 151 function display_browse_field($fieldid, $recordid, $template) {
2c5c1418 152 global $CFG, $USER, $course;
153
154 $field = get_record('data_fields', 'id', $fieldid);
155
c87fbb27 156 if ($content = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)){
88f47f81 157 return $content->content;
2c5c1418 158 }
159 return false;
160 }
161}
162?>