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_textarea extends feedback_item_base {
21 protected $type = "textarea";
22 private $commonparams;
26 public function init() {
30 public function build_editform($item, $feedback, $cm) {
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;
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 $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];
58 if (isset($width_and_height[1])) {
59 $itemheight = $width_and_height[1];
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,
71 'items'=>$feedbackitems,
72 'feedback'=>$feedback->id);
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);
83 //this function only can used after the call of build_editform()
84 public function show_editform() {
85 $this->item_form->display();
88 public function is_cancelled() {
89 return $this->item_form->is_cancelled();
92 public function get_data() {
93 if ($this->item = $this->item_form->get_data()) {
99 public function save_item() {
102 if (!$item = $this->item_form->get_data()) {
106 if (isset($item->clone_item) AND $item->clone_item) {
107 $item->id = ''; //to clone this item
111 $item->hasvalue = $this->get_hasvalue();
113 $item->id = $DB->insert_record('feedback_item', $item);
115 $DB->update_record('feedback_item', $item);
118 return $DB->get_record('feedback_item', array('id'=>$item->id));
122 //liefert eine Struktur ->name, ->data = array(mit Antworten)
123 public function get_analysed($item, $groupid = false, $courseid = false) {
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);
133 foreach ($values as $value) {
134 $data[] = str_replace("\n", '<br />', $value->value);
136 $analysed_val->data = $data;
138 return $analysed_val;
141 public function get_printval($item, $value) {
143 if (!isset($value->value)) {
147 return $value->value;
150 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
151 $values = feedback_get_group_values($item, $groupid, $courseid);
153 echo '<tr><th colspan="2" align="left">';
155 if (strval($item->label) !== '') {
156 echo '('. format_string($item->label).') ';
158 echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false));
160 foreach ($values as $value) {
162 echo '<td colspan="2" valign="top" align="left">';
163 echo '- ';
164 echo str_replace("\n", '<br />', $value->value);
171 public function excelprint_item(&$worksheet, $row_offset,
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);
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);
196 * print the item at the edit-page of feedback
199 * @param object $item
202 public function print_item_preview($item) {
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).') ';
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).'->'.$item->dependvalue.')';
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].'">';
242 * print the item at the complete-page of feedback
245 * @param object $item
246 * @param string $value
247 * @param bool $highlightrequire
250 public function print_item_complete($item, $value = '', $highlightrequire = false) {
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" >';
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].'">';
285 * print the item at the complete-page of feedback
288 * @param object $item
289 * @param string $value
292 public function print_item_show_value($item, $value = '') {
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).') ';
306 echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
309 //print the presentation
310 echo $OUTPUT->box_start('generalbox boxalign'.$align);
311 echo $value ? str_replace("\n", '<br />', $value) : ' ';
312 echo $OUTPUT->box_end();
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) {
326 public function create_value($data) {
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) {
341 public function get_presentation($data) {
342 return $data->itemwidth.'|'.$data->itemheight;
345 public function get_hasvalue() {
349 public function can_switch_require() {
353 public function value_type() {
357 public function clean_input_value($value) {