polished.
[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
88f47f81 105
2c5c1418 106
107 function update($fieldobject) {
108 $fieldobject->param2 = trim($fieldobject->param1);
109
110 if (!update_record('data_fields',$fieldobject)){
111 notify ('upate failed');
112 }
113 }
114
115
116 function store_data_content($fieldid, $recordid, $value) {
117 $content = new object;
118 $content->fieldid = $fieldid;
119 $content->recordid = $recordid;
120 $content->content = $this->format_data_field_multimenu_content($value);
121 insert_record('data_content', $content);
122 }
123
124
125 function update_data_content($fieldid, $recordid, $value) {
126 $content = new object;
127 $content->fieldid = $fieldid;
128 $content->recordid = $recordid;
129 $content->content = $this->format_data_field_multimenu_content($value);
130
131 if ($oldcontent = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)) {
132 $content->id = $oldcontent->id;
133 update_record('data_content', $content);
134 }
135 else {
136 $this->store_data_content($fieldid, $recordid, $value);
137 }
138 }
139
140
141 function format_data_field_multimenu_content($contentArr) {
142 $str = '';
143 foreach ($contentArr as $val) {
144 $str .= $val . '##';
145 }
146 $str = substr($str, 0, -2);
147 $str = clean_param($str, PARAM_NOTAGS);
148 return $str;
149 }
150
151
152 function display_browse_field($fieldid, $recordid) {
153 global $CFG, $USER, $course;
154
155 $field = get_record('data_fields', 'id', $fieldid);
156
88f47f81 157 if ($content = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)) {
158 return $content->content;
2c5c1418 159 }
160 return false;
161 }
162}
163?>