2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 defined('MOODLE_INTERNAL') OR die('not allowed');
18 require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
20 class feedback_item_textfield extends feedback_item_base {
21 protected $type = "textfield";
22 private $commonparams;
26 public function init() {
30 public function build_editform($item, $feedback, $cm) {
32 require_once('textfield_form.php');
34 //get the lastposition number of the feedback_items
35 $position = $item->position;
36 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
37 if ($position == -1) {
38 $i_formselect_last = $lastposition + 1;
39 $i_formselect_value = $lastposition + 1;
40 $item->position = $lastposition + 1;
42 $i_formselect_last = $lastposition;
43 $i_formselect_value = $item->position;
45 //the elements for position dropdownlist
46 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
48 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
50 $size_and_length = explode('|', $item->presentation);
52 if (isset($size_and_length[0]) AND $size_and_length[0] >= 5) {
53 $itemsize = $size_and_length[0];
58 $itemlength = isset($size_and_length[1]) ? $size_and_length[1] : 5;
60 $item->itemsize = $itemsize;
61 $item->itemmaxlength = $itemlength;
63 //all items for dependitem
64 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
65 $commonparams = array('cmid' => $cm->id,
66 'id' => isset($item->id) ? $item->id : null,
68 'items' => $feedbackitems,
69 'feedback' => $feedback->id);
72 $customdata = array('item' => $item,
73 'common' => $commonparams,
74 'positionlist' => $positionlist,
75 'position' => $position);
77 $this->item_form = new feedback_textfield_form('edit_item.php', $customdata);
80 //this function only can used after the call of build_editform()
81 public function show_editform() {
82 $this->item_form->display();
85 public function is_cancelled() {
86 return $this->item_form->is_cancelled();
89 public function get_data() {
90 if ($this->item = $this->item_form->get_data()) {
96 public function save_item() {
99 if (!$item = $this->item_form->get_data()) {
103 if (isset($item->clone_item) AND $item->clone_item) {
104 $item->id = ''; //to clone this item
108 $item->hasvalue = $this->get_hasvalue();
110 $item->id = $DB->insert_record('feedback_item', $item);
112 $DB->update_record('feedback_item', $item);
115 return $DB->get_record('feedback_item', array('id'=>$item->id));
119 //liefert eine Struktur ->name, ->data = array(mit Antworten)
120 public function get_analysed($item, $groupid = false, $courseid = false) {
123 $analysed_val = new stdClass();
124 $analysed_val->data = null;
125 $analysed_val->name = $item->name;
127 $values = feedback_get_group_values($item, $groupid, $courseid);
130 foreach ($values as $value) {
131 $data[] = str_replace("\n", '<br />', $value->value);
133 $analysed_val->data = $data;
135 return $analysed_val;
138 public function get_printval($item, $value) {
140 if (!isset($value->value)) {
143 return $value->value;
146 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
147 $values = feedback_get_group_values($item, $groupid, $courseid);
149 echo '<tr><th colspan="2" align="left">';
150 echo $itemnr.' ('.$item->label.') '.$item->name;
152 foreach ($values as $value) {
153 echo '<tr><td colspan="2" valign="top" align="left">';
154 echo '- '.str_replace("\n", '<br />', $value->value);
160 public function excelprint_item(&$worksheet, $row_offset,
162 $groupid, $courseid = false) {
164 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
166 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
167 $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
168 $data = $analysed_item->data;
169 if (is_array($data)) {
170 $worksheet->write_string($row_offset, 2, $data[0], $xls_formats->value_bold);
172 $sizeofdata = count($data);
173 for ($i = 1; $i < $sizeofdata; $i++) {
174 $worksheet->write_string($row_offset, 2, $data[$i], $xls_formats->default);
183 * print the item at the edit-page of feedback
186 * @param object $item
189 public function print_item_preview($item) {
191 $align = right_to_left() ? 'right' : 'left';
192 $str_required_mark = '<span class="feedback_required_mark">*</span>';
194 $presentation = explode ("|", $item->presentation);
195 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
196 //print the question and label
197 echo '<div class="feedback_item_label_'.$align.'">';
198 echo '('.$item->label.') ';
199 echo format_text($item->name.$requiredmark, true, false, false);
200 if ($item->dependitem) {
201 if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
202 echo ' <span class="feedback_depend">';
203 echo '('.$dependitem->label.'->'.$item->dependvalue.')';
209 //print the presentation
210 echo '<div class="feedback_item_presentation_'.$align.'">';
211 echo '<span class="feedback_item_textfield">';
212 echo '<input type="text" '.
213 'name="'.$item->typ.'_'.$item->id.'" '.
214 'size="'.$presentation[0].'" '.
215 'maxlength="'.$presentation[1].'" '.
222 * print the item at the complete-page of feedback
225 * @param object $item
226 * @param string $value
227 * @param bool $highlightrequire
230 public function print_item_complete($item, $value = '', $highlightrequire = false) {
232 $align = right_to_left() ? 'right' : 'left';
233 $str_required_mark = '<span class="feedback_required_mark">*</span>';
235 $presentation = explode ("|", $item->presentation);
236 if ($highlightrequire AND $item->required AND strval($value) == '') {
237 $highlight = ' missingrequire';
241 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
243 //print the question and label
244 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
245 echo format_text($item->name.$requiredmark, true, false, false);
248 //print the presentation
249 echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
250 echo '<span class="feedback_item_textfield">';
251 echo '<input type="text" '.
252 'name="'.$item->typ.'_'.$item->id.'" '.
253 'size="'.$presentation[0].'" '.
254 'maxlength="'.$presentation[1].'" '.
255 'value="'.$value.'" />';
261 * print the item at the complete-page of feedback
264 * @param object $item
265 * @param string $value
268 public function print_item_show_value($item, $value = '') {
270 $align = right_to_left() ? 'right' : 'left';
271 $str_required_mark = '<span class="feedback_required_mark">*</span>';
273 $presentation = explode ("|", $item->presentation);
274 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
276 //print the question and label
277 echo '<div class="feedback_item_label_'.$align.'">';
278 echo '('.$item->label.') ';
279 echo format_text($item->name . $requiredmark, true, false, false);
281 echo $OUTPUT->box_start('generalbox boxalign'.$align);
282 echo $value ? $value : ' ';
283 echo $OUTPUT->box_end();
286 public function check_value($value, $item) {
287 //if the item is not required, so the check is true if no value is given
288 if ((!isset($value) OR $value == '') AND $item->required != 1) {
297 public function create_value($data) {
302 //compares the dbvalue with the dependvalue
303 //dbvalue is the value put in by the user
304 //dependvalue is the value that is compared
305 public function compare_value($item, $dbvalue, $dependvalue) {
306 if ($dbvalue == $dependvalue) {
312 public function get_presentation($data) {
313 return $data->itemsize . '|'. $data->itemmaxlength;
316 public function get_hasvalue() {
320 public function can_switch_require() {
324 public function value_type() {
328 public function clean_input_value($value) {