Checkbox field support
[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 }
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 foreach (explode("\n",$field->param1) as $checkbox) {
81 $checkbox = ltrim(rtrim($checkbox));
82 $str .= '<input type="checkbox" name="field_' . $field->id . '[]" id="field_';
83 $str .= $field->id . '[]" value="' . $checkbox . '" ';
84
85 if (array_search($checkbox, $content) !== false) {
86 // Selected by user.
87 $str .= 'checked />';
88 }
89 else {
90 $str .= '/>';
91 }
92
93 $str .= $checkbox . '<br />';
94 }
95 return $str;
96 }
97
98
99 function display_edit_field($id, $mode=0) {
100 parent::display_edit_field($id, $mode);
101 }
102
103
104 function update($fieldobject) {
105 $fieldobject->param2 = trim($fieldobject->param1);
106
107 if (!update_record('data_fields',$fieldobject)){
108 notify ('upate failed');
109 }
110 }
111
112
113 function store_data_content($fieldid, $recordid, $value) {
114 $content = new object;
115 $content->fieldid = $fieldid;
116 $content->recordid = $recordid;
117 $content->content = $this->format_data_field_checkbox_content($value);
118 insert_record('data_content', $content);
119 }
120
121
122 function update_data_content($fieldid, $recordid, $value) {
123 $content = new object;
124 $content->fieldid = $fieldid;
125 $content->recordid = $recordid;
126 $content->content = $this->format_data_field_checkbox_content($value);
127
128 if ($oldcontent = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)) {
129 $content->id = $oldcontent->id;
130 update_record('data_content', $content);
131 }
132 else {
133 $this->store_data_content($fieldid, $recordid, $value);
134 }
135 }
136
137
138 function format_data_field_checkbox_content($contentArr) {
139 $str = '';
140 foreach ($contentArr as $val) {
141 $str .= $val . '##';
142 }
143 $str = substr($str, 0, -2);
144 $str = clean_param($str, PARAM_NOTAGS);
145 return $str;
146 }
147
148
149 function display_browse_field($fieldid, $recordid) {
150 global $CFG, $USER, $course;
151
152 $field = get_record('data_fields', 'id', $fieldid);
153
154 if ($content = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)){
155 $contentArr = array();
156 if (!empty($content->content)) {
157 $contentArr = explode('##', $content->content);
158 }
159 $str = '';
160 foreach ($contentArr as $line) {
161 $str .= $line . "<br />\n";
162 }
163 return $str;
164 }
165 return false;
166 }
167}
168?>