Added multi-selection menu field support.
[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 }
64 if ($rid) {
65 $content = get_record('data_content', 'fieldid', $id, 'recordid', $rid);
66 if (isset($content->content)) {
67 $content = $content->content;
68 $content = explode('##', $content);
69 }
70 }
71 else {
72 $content = array();
73 }
74 $str = '';
75
76 if ($field->description) {
77 $str .= '<img src="'.$CFG->pixpath.'/help.gif" alt="'.$field->description.'" title="'.$field->description.'" />&nbsp;';
78 }
79
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>';
96
97 return $str;
98 }
99
100
101 function display_edit_field($id, $mode=0) {
102 parent::display_edit_field($id, $mode);
103 }
104
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
151 function display_browse_field($fieldid, $recordid) {
152 global $CFG, $USER, $course;
153
154 $field = get_record('data_fields', 'id', $fieldid);
155
156 if ($content = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)){
157 $contentArr = array();
158 if (!empty($content->content)) {
159 $contentArr = explode('##', $content->content);
160 }
161 $str = '';
162 foreach ($contentArr as $line) {
163 $str .= $line . "<br />\n";
164 }
165 return $str;
166 }
167 return false;
168 }
169}
170?>