Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
16
 
17
defined('MOODLE_INTERNAL') OR die('not allowed');
18
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
19
 
20
class feedback_item_textarea extends feedback_item_base {
21
    protected $type = "textarea";
22
 
23
    public function build_editform($item, $feedback, $cm) {
24
        global $DB, $CFG;
25
        require_once('textarea_form.php');
26
 
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;
34
        } else {
35
            $i_formselect_last = $lastposition;
36
            $i_formselect_value = $item->position;
37
        }
38
        //the elements for position dropdownlist
39
        $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
40
 
41
        $item->presentation = empty($item->presentation) ? '' : $item->presentation;
42
 
43
        $width_and_height = explode('|', $item->presentation);
44
 
45
        if (isset($width_and_height[0]) AND $width_and_height[0] >= 5) {
46
            $itemwidth = $width_and_height[0];
47
        } else {
48
            $itemwidth = 30;
49
        }
50
 
51
        if (isset($width_and_height[1])) {
52
            $itemheight = $width_and_height[1];
53
        } else {
54
            $itemheight = 5;
55
        }
56
        $item->itemwidth = $itemwidth;
57
        $item->itemheight = $itemheight;
58
 
59
        //all items for dependitem
60
        $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
61
        $commonparams = array('cmid'=>$cm->id,
62
                             'id'=>isset($item->id) ? $item->id : null,
63
                             'typ'=>$item->typ,
64
                             'items'=>$feedbackitems,
65
                             'feedback'=>$feedback->id);
66
 
67
        //build the form
68
        $customdata = array('item' => $item,
69
                            'common' => $commonparams,
70
                            'positionlist' => $positionlist,
71
                            'position' => $position);
72
 
73
        $this->item_form = new feedback_textarea_form('edit_item.php', $customdata);
74
    }
75
 
76
    public function save_item() {
77
        global $DB;
78
 
79
        if (!$this->get_data()) {
80
            return false;
81
        }
82
        $item = $this->item;
83
 
84
        if (isset($item->clone_item) AND $item->clone_item) {
85
            $item->id = ''; //to clone this item
86
            $item->position++;
87
        }
88
 
89
        $item->hasvalue = $this->get_hasvalue();
90
        if (!$item->id) {
91
            $item->id = $DB->insert_record('feedback_item', $item);
92
        } else {
93
            $DB->update_record('feedback_item', $item);
94
        }
95
 
96
        return $DB->get_record('feedback_item', array('id'=>$item->id));
97
    }
98
 
99
    /**
100
     * Helper function for collected data for exporting to excel
101
     *
102
     * @param stdClass $item the db-object from feedback_item
103
     * @param int $groupid
104
     * @param int $courseid
105
     * @param bool $excel Indicate if being used for Excel
106
     * @return stdClass
107
     */
108
    protected function get_analysed($item, $groupid = false, $courseid = false, bool $excel = false) {
109
        global $DB;
110
 
111
        $analysed_val = new stdClass();
112
        $analysed_val->data = array();
113
        $analysed_val->name = $item->name;
114
 
115
        $values = feedback_get_group_values($item, $groupid, $courseid);
116
        if ($values) {
117
            $data = array();
118
            foreach ($values as $value) {
119
                // Convert line breaks except for Excel.
120
                $data[] = $excel ? $value->value : str_replace("\n", '<br />', $value->value);
121
            }
122
            $analysed_val->data = $data;
123
        }
124
        return $analysed_val;
125
    }
126
 
127
    public function get_printval($item, $value) {
128
 
129
        if (!isset($value->value)) {
130
            return '';
131
        }
132
 
133
        return $value->value;
134
    }
135
 
136
    public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
137
        $values = feedback_get_group_values($item, $groupid, $courseid);
138
        if ($values) {
139
            echo "<table class=\"analysis itemtype_{$item->typ}\">";
140
            echo '<tr><th class="text-left">';
141
            echo $itemnr . ' ';
142
            if (strval($item->label) !== '') {
143
                echo '('. format_string($item->label).') ';
144
            }
145
            echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false));
146
            echo '</th></tr>';
147
            foreach ($values as $value) {
148
                $class = strlen(trim($value->value)) ? '' : ' class="isempty"';
149
                echo '<tr'.$class.'>';
150
                echo '<td class="singlevalue">';
151
                echo str_replace("\n", '<br />', $value->value);
152
                echo '</td>';
153
                echo '</tr>';
154
            }
155
            echo '</table>';
156
        }
157
    }
158
 
159
    public function excelprint_item(&$worksheet, $row_offset,
160
                             $xls_formats, $item,
161
                             $groupid, $courseid = false) {
162
 
163
        $analyseditem = $this->get_analysed($item, $groupid, $courseid, true);
164
 
165
        $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
166
        $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
167
        $data = $analyseditem->data;
168
        if (is_array($data)) {
169
            if (isset($data[0])) {
170
                $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);
171
            }
172
            $row_offset++;
173
            $sizeofdata = count($data);
174
            for ($i = 1; $i < $sizeofdata; $i++) {
175
                $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[$i], ENT_QUOTES), $xls_formats->default);
176
                $row_offset++;
177
            }
178
        }
179
        $row_offset++;
180
        return $row_offset;
181
    }
182
 
183
    /**
184
     * Adds an input element to the complete form
185
     *
186
     * @param stdClass $item
187
     * @param mod_feedback_complete_form $form
188
     */
189
    public function complete_form_element($item, $form) {
190
        $name = $this->get_display_name($item);
191
        $inputname = $item->typ . '_' . $item->id;
192
        list($cols, $rows) = explode ("|", $item->presentation);
193
        $form->add_form_element($item,
194
            ['textarea', $inputname, $name, array('rows' => $rows, 'cols' => $cols)]);
195
        $form->set_element_type($inputname, PARAM_NOTAGS);
196
    }
197
 
198
    public function create_value($data) {
199
        return s($data);
200
    }
201
 
202
    /**
203
     * Return the analysis data ready for external functions.
204
     *
205
     * @param stdClass $item     the item (question) information
206
     * @param int      $groupid  the group id to filter data (optional)
207
     * @param int      $courseid the course id (optional)
208
     * @return array an array of data with non scalar types json encoded
209
     * @since  Moodle 3.3
210
     */
211
    public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
212
 
213
        $externaldata = array();
214
        $data = $this->get_analysed($item, $groupid, $courseid);
215
 
216
        if (is_array($data->data)) {
217
            return $data->data; // No need to json, scalar type.
218
        }
219
        return $externaldata;
220
    }
221
}