Fix for Bug #4954
[moodle.git] / mod / data / field / checkbox / field.class.php
CommitLineData
4728968f 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_checkbox extends data_field_base {
28
29 var $type = 'checkbox';
30 var $id;
31
32
33 function data_field_checkbox($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='checkbox', $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
4728968f 66 if ($rid) {
c87fbb27 67 $dbcontent = get_record('data_content', 'fieldid', $id, 'recordid', $rid);
68 if (isset($dbcontent->content)) {
4728968f 69 $content = $content->content;
70 $content = explode('##', $content);
71 }
72 }
4728968f 73 $str = '';
bbe39b6c 74 /*
4728968f 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.'">';
4728968f 80
c87fbb27 81 foreach (explode("\n", $field->param1) as $checkbox) {
4728968f 82 $checkbox = ltrim(rtrim($checkbox));
af25f45e 83 $str .= '<input type="checkbox" name="field_' . $field->id . '[]" ';
84 $str .= 'value="' . $checkbox . '" ';
c87fbb27 85
4728968f 86 if (array_search($checkbox, $content) !== false) {
87 // Selected by user.
88 $str .= 'checked />';
89 }
90 else {
91 $str .= '/>';
92 }
4728968f 93 $str .= $checkbox . '<br />';
94 }
bbe39b6c 95 $str .= '</div>';
4728968f 96 return $str;
97 }
98
99
100 function display_edit_field($id, $mode=0) {
101 parent::display_edit_field($id, $mode);
102 }
103
104
105 function update($fieldobject) {
106 $fieldobject->param2 = trim($fieldobject->param1);
107
108 if (!update_record('data_fields',$fieldobject)){
109 notify ('upate failed');
110 }
111 }
112
113
114 function store_data_content($fieldid, $recordid, $value) {
115 $content = new object;
116 $content->fieldid = $fieldid;
117 $content->recordid = $recordid;
118 $content->content = $this->format_data_field_checkbox_content($value);
119 insert_record('data_content', $content);
120 }
121
122
123 function update_data_content($fieldid, $recordid, $value) {
124 $content = new object;
125 $content->fieldid = $fieldid;
126 $content->recordid = $recordid;
127 $content->content = $this->format_data_field_checkbox_content($value);
128
129 if ($oldcontent = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)) {
130 $content->id = $oldcontent->id;
131 update_record('data_content', $content);
132 }
133 else {
134 $this->store_data_content($fieldid, $recordid, $value);
135 }
136 }
137
138
e1791b72 139 function format_data_field_checkbox_content($content) {
140 if (!is_array($content)) {
141 $str = $content;
142 } else {
143 $str = '';
144 foreach ($content as $val) {
145 $str .= $val . '##';
146 }
147 $str = substr($str, 0, -2);
148 }
149 $str = clean_param($str, PARAM_NOTAGS);
150 return $str;
4728968f 151 }
152
153
cd2f6950 154 function display_browse_field($fieldid, $recordid, $template) {
4728968f 155 global $CFG, $USER, $course;
156
157 $field = get_record('data_fields', 'id', $fieldid);
158
159 if ($content = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)){
160 $contentArr = array();
161 if (!empty($content->content)) {
162 $contentArr = explode('##', $content->content);
163 }
164 $str = '';
165 foreach ($contentArr as $line) {
166 $str .= $line . "<br />\n";
167 }
168 return $str;
169 }
170 return false;
171 }
172}
e1791b72 173?>