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_info extends feedback_item_base {
21 protected $type = "info";
22 private $commonparams;
26 public function init() {
30 public function build_editform($item, $feedback, $cm) {
32 require_once('info_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) ? 1 : $item->presentation;
51 //all items for dependitem
52 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
53 $commonparams = array('cmid'=>$cm->id,
54 'id'=>isset($item->id) ? $item->id : null,
56 'items'=>$feedbackitems,
57 'feedback'=>$feedback->id);
60 $this->item_form = new feedback_info_form('edit_item.php',
62 'common'=>$commonparams,
63 'positionlist'=>$positionlist,
64 'position' => $position));
67 //this function only can used after the call of build_editform()
68 public function show_editform() {
69 $this->item_form->display();
72 public function is_cancelled() {
73 return $this->item_form->is_cancelled();
76 public function get_data() {
77 if ($this->item = $this->item_form->get_data()) {
83 public function save_item() {
86 if (!$item = $this->item_form->get_data()) {
90 if (isset($item->clone_item) AND $item->clone_item) {
91 $item->id = ''; //to clone this item
95 $item->hasvalue = $this->get_hasvalue();
97 $item->id = $DB->insert_record('feedback_item', $item);
99 $DB->update_record('feedback_item', $item);
102 return $DB->get_record('feedback_item', array('id'=>$item->id));
105 //liefert eine Struktur ->name, ->data = array(mit Antworten)
106 public function get_analysed($item, $groupid = false, $courseid = false) {
108 $presentation = $item->presentation;
109 $analysed_val = new stdClass();;
110 $analysed_val->data = null;
111 $analysed_val->name = $item->name;
112 $values = feedback_get_group_values($item, $groupid, $courseid);
115 $datavalue = new stdClass();
116 foreach ($values as $value) {
118 switch($presentation) {
120 $datavalue->value = $value->value;
121 $datavalue->show = userdate($datavalue->value);
124 $datavalue->value = $value->value;
125 $datavalue->show = $datavalue->value;
128 $datavalue->value = $value->value;
129 $datavalue->show = $datavalue->value;
133 $data[] = $datavalue;
135 $analysed_val->data = $data;
137 return $analysed_val;
140 public function get_printval($item, $value) {
142 if (!isset($value->value)) {
145 return userdate($value->value);
148 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
149 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
150 $data = $analysed_item->data;
151 if (is_array($data)) {
152 echo '<tr><th colspan="2" align="left">';
153 echo $itemnr.' ('.$item->label.') '.$item->name;
155 $sizeofdata = count($data);
156 for ($i = 0; $i < $sizeofdata; $i++) {
157 echo '<tr><td colspan="2" valign="top" align="left">- ';
158 echo str_replace("\n", '<br />', $data[$i]->show);
164 public function excelprint_item(&$worksheet, $row_offset,
166 $groupid, $courseid = false) {
167 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
169 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
170 $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
171 $data = $analysed_item->data;
172 if (is_array($data)) {
173 $worksheet->write_string($row_offset, 2, $data[0]->show, $xls_formats->value_bold);
175 $sizeofdata = count($data);
176 for ($i = 1; $i < $sizeofdata; $i++) {
177 $worksheet->write_string($row_offset, 2, $data[$i]->show, $xls_formats->default);
186 * print the item at the edit-page of feedback
189 * @param object $item
192 public function print_item_preview($item) {
193 global $USER, $DB, $OUTPUT;
195 $align = right_to_left() ? 'right' : 'left';
196 $presentation = $item->presentation;
197 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
199 if ($item->feedback) {
200 $courseid = $DB->get_field('feedback', 'course', array('id'=>$item->feedback));
201 } else { // the item must be a template item
202 $cmid = required_param('id', PARAM_INT);
203 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$cmid));
205 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
206 print_error('error');
208 if ($course->id !== SITEID) {
209 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
211 $coursecategory = false;
213 switch($presentation) {
216 $itemshowvalue = userdate($itemvalue);
219 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
220 $itemvalue = format_string($course->shortname,
222 array('context' => $coursecontext));
224 $itemshowvalue = $itemvalue;
227 if ($coursecategory) {
228 $category_context = get_context_instance(CONTEXT_COURSECAT, $coursecategory->id);
229 $itemvalue = format_string($coursecategory->name,
231 array('context' => $category_context));
233 $itemshowvalue = $itemvalue;
241 //print the question and label
242 echo '<div class="feedback_item_label_'.$align.'">';
243 echo '('.$item->label.') ';
244 echo format_text($item->name.$requiredmark, true, false, false);
245 if ($item->dependitem) {
246 if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
247 echo ' <span class="feedback_depend">';
248 echo '('.$dependitem->label.'->'.$item->dependvalue.')';
253 //print the presentation
254 echo '<div class="feedback_item_presentation_'.$align.'">';
255 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
256 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
261 * print the item at the complete-page of feedback
264 * @param object $item
265 * @param string $value
266 * @param bool $highlightrequire
269 public function print_item_complete($item, $value = '', $highlightrequire = false) {
270 global $USER, $DB, $OUTPUT;
271 $align = right_to_left() ? 'right' : 'left';
273 $presentation = $item->presentation;
274 if ($highlightrequire AND $item->required AND strval($value) == '') {
275 $highlight = ' missingrequire';
279 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
281 $feedback = $DB->get_record('feedback', array('id'=>$item->feedback));
282 $course = $DB->get_record('course', array('id'=>$feedback->course));
284 if ($course->id !== SITEID) {
285 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
287 $coursecategory = false;
290 switch($presentation) {
293 $itemshowvalue = userdate($itemvalue);
296 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
297 $itemvalue = format_string($course->shortname,
299 array('context' => $coursecontext));
301 $itemshowvalue = $itemvalue;
304 if ($coursecategory) {
305 $category_context = get_context_instance(CONTEXT_COURSECAT, $coursecategory->id);
306 $itemvalue = format_string($coursecategory->name,
308 array('context' => $category_context));
310 $itemshowvalue = $itemvalue;
318 //print the question and label
319 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
320 echo format_text($item->name.$requiredmark, true, false, false);
323 //print the presentation
324 echo '<div class="feedback_item_presentation_'.$align.'">';
325 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
326 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
331 * print the item at the complete-page of feedback
334 * @param object $item
335 * @param string $value
338 public function print_item_show_value($item, $value = '') {
339 global $USER, $DB, $OUTPUT;
340 $align = right_to_left() ? 'right' : 'left';
342 $presentation = $item->presentation;
343 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
345 if ($presentation == 1) {
346 $value = $value ? userdate($value) : ' ';
349 //print the question and label
350 echo '<div class="feedback_item_label_'.$align.'">';
351 echo '('.$item->label.') ';
352 echo format_text($item->name . $requiredmark, true, false, false);
355 //print the presentation
356 echo $OUTPUT->box_start('generalbox boxalign'.$align);
358 echo $OUTPUT->box_end();
361 public function check_value($value, $item) {
365 public function create_value($data) {
366 $data = clean_text($data);
370 //compares the dbvalue with the dependvalue
371 //the values can be the shortname of a course or the category name
372 //the date is not compareable :(.
373 public function compare_value($item, $dbvalue, $dependvalue) {
374 if ($dbvalue == $dependvalue) {
380 public function get_presentation($data) {
381 return $data->infotype;
384 public function get_hasvalue() {
388 public function can_switch_require() {
392 public function value_type() {
396 public function clean_input_value($value) {
397 return clean_param($value, $this->value_type());