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";
23 public function build_editform($item, $feedback, $cm) {
25 require_once('textfield_form.php');
27 //get the lastposition number of the feedback_items
28 $position = $item->position;
29 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
30 if ($position == -1) {
31 $i_formselect_last = $lastposition + 1;
32 $i_formselect_value = $lastposition + 1;
33 $item->position = $lastposition + 1;
35 $i_formselect_last = $lastposition;
36 $i_formselect_value = $item->position;
38 //the elements for position dropdownlist
39 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
41 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
43 $size_and_length = explode('|', $item->presentation);
45 if (isset($size_and_length[0]) AND $size_and_length[0] >= 5) {
46 $itemsize = $size_and_length[0];
51 $itemlength = isset($size_and_length[1]) ? $size_and_length[1] : 255;
53 $item->itemsize = $itemsize;
54 $item->itemmaxlength = $itemlength;
56 //all items for dependitem
57 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
58 $commonparams = array('cmid' => $cm->id,
59 'id' => isset($item->id) ? $item->id : null,
61 'items' => $feedbackitems,
62 'feedback' => $feedback->id);
65 $customdata = array('item' => $item,
66 'common' => $commonparams,
67 'positionlist' => $positionlist,
68 'position' => $position);
70 $this->item_form = new feedback_textfield_form('edit_item.php', $customdata);
73 public function save_item() {
76 if (!$item = $this->item_form->get_data()) {
80 if (isset($item->clone_item) AND $item->clone_item) {
81 $item->id = ''; //to clone this item
85 $item->hasvalue = $this->get_hasvalue();
87 $item->id = $DB->insert_record('feedback_item', $item);
89 $DB->update_record('feedback_item', $item);
92 return $DB->get_record('feedback_item', array('id'=>$item->id));
97 * Helper function for collected data for exporting to excel
99 * @param stdClass $item the db-object from feedback_item
100 * @param int $groupid
101 * @param int $courseid
104 protected function get_analysed($item, $groupid = false, $courseid = false) {
106 $analysed_val = new stdClass();
107 $analysed_val->data = null;
108 $analysed_val->name = $item->name;
110 $values = feedback_get_group_values($item, $groupid, $courseid);
113 foreach ($values as $value) {
114 $data[] = str_replace("\n", '<br />', $value->value);
116 $analysed_val->data = $data;
118 return $analysed_val;
121 public function get_printval($item, $value) {
123 if (!isset($value->value)) {
126 return $value->value;
129 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
130 $values = feedback_get_group_values($item, $groupid, $courseid);
132 echo '<tr><th colspan="2" align="left">';
134 if (strval($item->label) !== '') {
135 echo '('. format_string($item->label).') ';
137 echo $this->get_display_name($item);
139 foreach ($values as $value) {
140 echo '<tr><td colspan="2" valign="top" align="left">';
141 echo '- '.str_replace("\n", '<br />', $value->value);
147 public function excelprint_item(&$worksheet, $row_offset,
149 $groupid, $courseid = false) {
151 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
153 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
154 $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
155 $data = $analysed_item->data;
156 if (is_array($data)) {
157 $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);
159 $sizeofdata = count($data);
160 for ($i = 1; $i < $sizeofdata; $i++) {
161 $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[$i], ENT_QUOTES), $xls_formats->default);
170 * Adds an input element to the complete form
172 * @param stdClass $item
173 * @param mod_feedback_complete_form $form
175 public function complete_form_element($item, $form) {
176 $name = $this->get_display_name($item);
177 $inputname = $item->typ . '_' . $item->id;
178 list($size, $maxlength) = explode ("|", $item->presentation);
179 $form->add_form_element($item,
180 ['text', $inputname, $name, ['maxlength' => $maxlength, 'size' => $size]]);
181 $form->set_element_type($inputname, PARAM_NOTAGS);
183 $form->add_element_rule($inputname, get_string('maximumchars', '', $maxlength), 'maxlength', $maxlength, 'client');
187 * Converts the value from complete_form data to the string value that is stored in the db.
188 * @param mixed $value element from mod_feedback_complete_form::get_data() with the name $item->typ.'_'.$item->id
191 public function create_value($value) {