bfb2f0c916934b30c135a6a22f8ba59940101c0a
[moodle.git] / mod / feedback / item / textarea / lib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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_textarea extends feedback_item_base {
21     protected $type = "textarea";
22     private $commonparams;
23     private $item_form;
24     private $item;
26     public function init() {
28     }
30     public function build_editform($item, $feedback, $cm) {
31         global $DB, $CFG;
32         require_once('textarea_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;
41         } else {
42             $i_formselect_last = $lastposition;
43             $i_formselect_value = $item->position;
44         }
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         $width_and_height = explode('|', $item->presentation);
52         if (isset($width_and_height[0]) AND $width_and_height[0] >= 5) {
53             $itemwidth = $width_and_height[0];
54         } else {
55             $itemwidth = 30;
56         }
58         if (isset($width_and_height[1])) {
59             $itemheight = $width_and_height[1];
60         } else {
61             $itemheight = 5;
62         }
63         $item->itemwidth = $itemwidth;
64         $item->itemheight = $itemheight;
66         //all items for dependitem
67         $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
68         $commonparams = array('cmid'=>$cm->id,
69                              'id'=>isset($item->id) ? $item->id : null,
70                              'typ'=>$item->typ,
71                              'items'=>$feedbackitems,
72                              'feedback'=>$feedback->id);
74         //build the form
75         $customdata = array('item' => $item,
76                             'common' => $commonparams,
77                             'positionlist' => $positionlist,
78                             'position' => $position);
80         $this->item_form = new feedback_textarea_form('edit_item.php', $customdata);
81     }
83     //this function only can used after the call of build_editform()
84     public function show_editform() {
85         $this->item_form->display();
86     }
88     public function is_cancelled() {
89         return $this->item_form->is_cancelled();
90     }
92     public function get_data() {
93         if ($this->item = $this->item_form->get_data()) {
94             return true;
95         }
96         return false;
97     }
99     public function save_item() {
100         global $DB;
102         if (!$item = $this->item_form->get_data()) {
103             return false;
104         }
106         if (isset($item->clone_item) AND $item->clone_item) {
107             $item->id = ''; //to clone this item
108             $item->position++;
109         }
111         $item->hasvalue = $this->get_hasvalue();
112         if (!$item->id) {
113             $item->id = $DB->insert_record('feedback_item', $item);
114         } else {
115             $DB->update_record('feedback_item', $item);
116         }
118         return $DB->get_record('feedback_item', array('id'=>$item->id));
119     }
122     //liefert eine Struktur ->name, ->data = array(mit Antworten)
123     public function get_analysed($item, $groupid = false, $courseid = false) {
124         global $DB;
126         $analysed_val = new stdClass();
127         $analysed_val->data = array();
128         $analysed_val->name = $item->name;
130         $values = feedback_get_group_values($item, $groupid, $courseid);
131         if ($values) {
132             $data = array();
133             foreach ($values as $value) {
134                 $data[] = str_replace("\n", '<br />', $value->value);
135             }
136             $analysed_val->data = $data;
137         }
138         return $analysed_val;
139     }
141     public function get_printval($item, $value) {
143         if (!isset($value->value)) {
144             return '';
145         }
147         return $value->value;
148     }
150     public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
151         $values = feedback_get_group_values($item, $groupid, $courseid);
152         if ($values) {
153             echo '<tr><th colspan="2" align="left">';
154             echo $itemnr . ' ';
155             if (strval($item->label) !== '') {
156                 echo '('. format_string($item->label).') ';
157             }
158             echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false));
159             echo '</th></tr>';
160             foreach ($values as $value) {
161                 echo '<tr>';
162                 echo '<td colspan="2" valign="top" align="left">';
163                 echo '-&nbsp;&nbsp;';
164                 echo str_replace("\n", '<br />', $value->value);
165                 echo '</td>';
166                 echo '</tr>';
167             }
168         }
169     }
171     public function excelprint_item(&$worksheet, $row_offset,
172                              $xls_formats, $item,
173                              $groupid, $courseid = false) {
175         $analysed_item = $this->get_analysed($item, $groupid, $courseid);
177         $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
178         $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
179         $data = $analysed_item->data;
180         if (is_array($data)) {
181             if (isset($data[0])) {
182                 $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);
183             }
184             $row_offset++;
185             $sizeofdata = count($data);
186             for ($i = 1; $i < $sizeofdata; $i++) {
187                 $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[$i], ENT_QUOTES), $xls_formats->default);
188                 $row_offset++;
189             }
190         }
191         $row_offset++;
192         return $row_offset;
193     }
195     /**
196      * print the item at the edit-page of feedback
197      *
198      * @global object
199      * @param object $item
200      * @return void
201      */
202     public function print_item_preview($item) {
203         global $OUTPUT, $DB;
205         $align = right_to_left() ? 'right' : 'left';
206         $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
207             get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
209         $presentation = explode ("|", $item->presentation);
210         $requiredmark = ($item->required == 1) ? $strrequiredmark : '';
211         //print the question and label
212         $inputname = $item->typ . '_' . $item->id;
213         echo '<div class="feedback_item_label_'.$align.'">';
214         echo '<label for="'. $inputname .'">';
215         if (strval($item->label) !== '') {
216             echo '('. format_string($item->label).') ';
217         }
218         echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
219         if ($item->dependitem) {
220             if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
221                 echo ' <span class="feedback_depend">';
222                 echo '('.format_string($dependitem->label).'-&gt;'.$item->dependvalue.')';
223                 echo '</span>';
224             }
225         }
226         echo '</label>';
227         echo '</div>';
229         //print the presentation
230         echo '<div class="feedback_item_presentation_'.$align.'">';
231         echo '<span class="feedback_item_textarea">';
232         echo '<textarea id="'.$inputname.'" '.
233                        'name="'.$inputname.'" '.
234                        'cols="'.$presentation[0].'" '.
235                        'rows="'.$presentation[1].'">';
236         echo '</textarea>';
237         echo '</span>';
238         echo '</div>';
239     }
241     /**
242      * print the item at the complete-page of feedback
243      *
244      * @global object
245      * @param object $item
246      * @param string $value
247      * @param bool $highlightrequire
248      * @return void
249      */
250     public function print_item_complete($item, $value = '', $highlightrequire = false) {
251         global $OUTPUT;
252         $align = right_to_left() ? 'right' : 'left';
253         $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
254             get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
256         $presentation = explode ("|", $item->presentation);
257         $requiredmark = ($item->required == 1) ? $strrequiredmark :'';
259         //print the question and label
260         $inputname = $item->typ . '_' . $item->id;
261         echo '<div class="feedback_item_label_'.$align.'">';
262         echo '<label for="'. $inputname .'">';
263         echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
264         if ($highlightrequire AND $item->required AND strval($value) == '') {
265             echo '<br class="error"><span id="id_error_'.$inputname.'" class="error"> '.get_string('err_required', 'form').
266                 '</span><br id="id_error_break_'.$inputname.'" class="error" >';
267         }
268         echo '</label>';
269         echo '</div>';
271         //print the presentation
272         echo '<div class="feedback_item_presentation_'.$align.'">';
273         echo '<span class="feedback_item_textarea">';
274         echo '<textarea id="'.$inputname.'" '.
275                        'name="'.$inputname.'" '.
276                        'cols="'.$presentation[0].'" '.
277                        'rows="'.$presentation[1].'">';
278         echo $value;
279         echo '</textarea>';
280         echo '</span>';
281         echo '</div>';
282     }
284     /**
285      * print the item at the complete-page of feedback
286      *
287      * @global object
288      * @param object $item
289      * @param string $value
290      * @return void
291      */
292     public function print_item_show_value($item, $value = '') {
293         global $OUTPUT;
294         $align = right_to_left() ? 'right' : 'left';
295         $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
296             get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
298         $presentation = explode ("|", $item->presentation);
299         $requiredmark = ($item->required == 1) ? $strrequiredmark : '';
301         //print the question and label
302         echo '<div class="feedback_item_label_'.$align.'">';
303         if (strval($item->label) !== '') {
304             echo '('. format_string($item->label).') ';
305         }
306         echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
307         echo '</div>';
309         //print the presentation
310         echo $OUTPUT->box_start('generalbox boxalign'.$align);
311         echo $value ? str_replace("\n", '<br />', $value) : '&nbsp;';
312         echo $OUTPUT->box_end();
313     }
315     public function check_value($value, $item) {
316         //if the item is not required, so the check is true if no value is given
317         if ((!isset($value) OR $value == '') AND $item->required != 1) {
318             return true;
319         }
320         if ($value == "") {
321             return false;
322         }
323         return true;
324     }
326     public function create_value($data) {
327         $data = s($data);
328         return $data;
329     }
331     //compares the dbvalue with the dependvalue
332     //dbvalue is the value put in by the user
333     //dependvalue is the value that is compared
334     public function compare_value($item, $dbvalue, $dependvalue) {
335         if ($dbvalue == $dependvalue) {
336             return true;
337         }
338         return false;
339     }
341     public function get_presentation($data) {
342         return $data->itemwidth.'|'.$data->itemheight;
343     }
345     public function get_hasvalue() {
346         return 1;
347     }
349     public function can_switch_require() {
350         return true;
351     }
353     public function value_type() {
354         return PARAM_RAW;
355     }
357     public function clean_input_value($value) {
358         return s($value);
359     }