PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/feedback/item/textfield/lib.php

https://bitbucket.org/kudutest/moodlegit
PHP | 331 lines | 228 code | 50 blank | 53 comment | 30 complexity | 892487957a9d35250bde48c47f76034b MD5 | raw file
  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. defined('MOODLE_INTERNAL') OR die('not allowed');
  17. require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
  18. class feedback_item_textfield extends feedback_item_base {
  19. protected $type = "textfield";
  20. private $commonparams;
  21. private $item_form;
  22. private $item;
  23. public function init() {
  24. }
  25. public function build_editform($item, $feedback, $cm) {
  26. global $DB, $CFG;
  27. require_once('textfield_form.php');
  28. //get the lastposition number of the feedback_items
  29. $position = $item->position;
  30. $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
  31. if ($position == -1) {
  32. $i_formselect_last = $lastposition + 1;
  33. $i_formselect_value = $lastposition + 1;
  34. $item->position = $lastposition + 1;
  35. } else {
  36. $i_formselect_last = $lastposition;
  37. $i_formselect_value = $item->position;
  38. }
  39. //the elements for position dropdownlist
  40. $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
  41. $item->presentation = empty($item->presentation) ? '' : $item->presentation;
  42. $size_and_length = explode('|', $item->presentation);
  43. if (isset($size_and_length[0]) AND $size_and_length[0] >= 5) {
  44. $itemsize = $size_and_length[0];
  45. } else {
  46. $itemsize = 30;
  47. }
  48. $itemlength = isset($size_and_length[1]) ? $size_and_length[1] : 5;
  49. $item->itemsize = $itemsize;
  50. $item->itemmaxlength = $itemlength;
  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,
  55. 'typ' => $item->typ,
  56. 'items' => $feedbackitems,
  57. 'feedback' => $feedback->id);
  58. //build the form
  59. $customdata = array('item' => $item,
  60. 'common' => $commonparams,
  61. 'positionlist' => $positionlist,
  62. 'position' => $position);
  63. $this->item_form = new feedback_textfield_form('edit_item.php', $customdata);
  64. }
  65. //this function only can used after the call of build_editform()
  66. public function show_editform() {
  67. $this->item_form->display();
  68. }
  69. public function is_cancelled() {
  70. return $this->item_form->is_cancelled();
  71. }
  72. public function get_data() {
  73. if ($this->item = $this->item_form->get_data()) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. public function save_item() {
  79. global $DB;
  80. if (!$item = $this->item_form->get_data()) {
  81. return false;
  82. }
  83. if (isset($item->clone_item) AND $item->clone_item) {
  84. $item->id = ''; //to clone this item
  85. $item->position++;
  86. }
  87. $item->hasvalue = $this->get_hasvalue();
  88. if (!$item->id) {
  89. $item->id = $DB->insert_record('feedback_item', $item);
  90. } else {
  91. $DB->update_record('feedback_item', $item);
  92. }
  93. return $DB->get_record('feedback_item', array('id'=>$item->id));
  94. }
  95. //liefert eine Struktur ->name, ->data = array(mit Antworten)
  96. public function get_analysed($item, $groupid = false, $courseid = false) {
  97. global $DB;
  98. $analysed_val = new stdClass();
  99. $analysed_val->data = null;
  100. $analysed_val->name = $item->name;
  101. $values = feedback_get_group_values($item, $groupid, $courseid);
  102. if ($values) {
  103. $data = array();
  104. foreach ($values as $value) {
  105. $data[] = str_replace("\n", '<br />', $value->value);
  106. }
  107. $analysed_val->data = $data;
  108. }
  109. return $analysed_val;
  110. }
  111. public function get_printval($item, $value) {
  112. if (!isset($value->value)) {
  113. return '';
  114. }
  115. return $value->value;
  116. }
  117. public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
  118. $values = feedback_get_group_values($item, $groupid, $courseid);
  119. if ($values) {
  120. echo '<tr><th colspan="2" align="left">';
  121. echo $itemnr.'&nbsp;('.$item->label.') '.$item->name;
  122. echo '</th></tr>';
  123. foreach ($values as $value) {
  124. echo '<tr><td colspan="2" valign="top" align="left">';
  125. echo '-&nbsp;&nbsp;'.str_replace("\n", '<br />', $value->value);
  126. echo '</td></tr>';
  127. }
  128. }
  129. }
  130. public function excelprint_item(&$worksheet, $row_offset,
  131. $xls_formats, $item,
  132. $groupid, $courseid = false) {
  133. $analysed_item = $this->get_analysed($item, $groupid, $courseid);
  134. $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
  135. $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
  136. $data = $analysed_item->data;
  137. if (is_array($data)) {
  138. $worksheet->write_string($row_offset, 2, $data[0], $xls_formats->value_bold);
  139. $row_offset++;
  140. $sizeofdata = count($data);
  141. for ($i = 1; $i < $sizeofdata; $i++) {
  142. $worksheet->write_string($row_offset, 2, $data[$i], $xls_formats->default);
  143. $row_offset++;
  144. }
  145. }
  146. $row_offset++;
  147. return $row_offset;
  148. }
  149. /**
  150. * print the item at the edit-page of feedback
  151. *
  152. * @global object
  153. * @param object $item
  154. * @return void
  155. */
  156. public function print_item_preview($item) {
  157. global $OUTPUT, $DB;
  158. $align = right_to_left() ? 'right' : 'left';
  159. $str_required_mark = '<span class="feedback_required_mark">*</span>';
  160. $presentation = explode ("|", $item->presentation);
  161. $requiredmark = ($item->required == 1) ? $str_required_mark : '';
  162. //print the question and label
  163. echo '<div class="feedback_item_label_'.$align.'">';
  164. echo '('.$item->label.') ';
  165. echo format_text($item->name.$requiredmark, true, false, false);
  166. if ($item->dependitem) {
  167. if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
  168. echo ' <span class="feedback_depend">';
  169. echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
  170. echo '</span>';
  171. }
  172. }
  173. echo '</div>';
  174. //print the presentation
  175. echo '<div class="feedback_item_presentation_'.$align.'">';
  176. echo '<span class="feedback_item_textfield">';
  177. echo '<input type="text" '.
  178. 'name="'.$item->typ.'_'.$item->id.'" '.
  179. 'size="'.$presentation[0].'" '.
  180. 'maxlength="'.$presentation[1].'" '.
  181. 'value="" />';
  182. echo '</span>';
  183. echo '</div>';
  184. }
  185. /**
  186. * print the item at the complete-page of feedback
  187. *
  188. * @global object
  189. * @param object $item
  190. * @param string $value
  191. * @param bool $highlightrequire
  192. * @return void
  193. */
  194. public function print_item_complete($item, $value = '', $highlightrequire = false) {
  195. global $OUTPUT;
  196. $align = right_to_left() ? 'right' : 'left';
  197. $str_required_mark = '<span class="feedback_required_mark">*</span>';
  198. $presentation = explode ("|", $item->presentation);
  199. if ($highlightrequire AND $item->required AND strval($value) == '') {
  200. $highlight = ' missingrequire';
  201. } else {
  202. $highlight = '';
  203. }
  204. $requiredmark = ($item->required == 1) ? $str_required_mark : '';
  205. //print the question and label
  206. echo '<div class="feedback_item_label_'.$align.$highlight.'">';
  207. echo format_text($item->name.$requiredmark, true, false, false);
  208. echo '</div>';
  209. //print the presentation
  210. echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
  211. echo '<span class="feedback_item_textfield">';
  212. echo '<input type="text" '.
  213. 'name="'.$item->typ.'_'.$item->id.'" '.
  214. 'size="'.$presentation[0].'" '.
  215. 'maxlength="'.$presentation[1].'" '.
  216. 'value="'.$value.'" />';
  217. echo '</span>';
  218. echo '</div>';
  219. }
  220. /**
  221. * print the item at the complete-page of feedback
  222. *
  223. * @global object
  224. * @param object $item
  225. * @param string $value
  226. * @return void
  227. */
  228. public function print_item_show_value($item, $value = '') {
  229. global $OUTPUT;
  230. $align = right_to_left() ? 'right' : 'left';
  231. $str_required_mark = '<span class="feedback_required_mark">*</span>';
  232. $presentation = explode ("|", $item->presentation);
  233. $requiredmark = ($item->required == 1) ? $str_required_mark : '';
  234. //print the question and label
  235. echo '<div class="feedback_item_label_'.$align.'">';
  236. echo '('.$item->label.') ';
  237. echo format_text($item->name . $requiredmark, true, false, false);
  238. echo '</div>';
  239. echo $OUTPUT->box_start('generalbox boxalign'.$align);
  240. echo $value ? $value : '&nbsp;';
  241. echo $OUTPUT->box_end();
  242. }
  243. public function check_value($value, $item) {
  244. //if the item is not required, so the check is true if no value is given
  245. if ((!isset($value) OR $value == '') AND $item->required != 1) {
  246. return true;
  247. }
  248. if ($value == "") {
  249. return false;
  250. }
  251. return true;
  252. }
  253. public function create_value($data) {
  254. $data = s($data);
  255. return $data;
  256. }
  257. //compares the dbvalue with the dependvalue
  258. //dbvalue is the value put in by the user
  259. //dependvalue is the value that is compared
  260. public function compare_value($item, $dbvalue, $dependvalue) {
  261. if ($dbvalue == $dependvalue) {
  262. return true;
  263. }
  264. return false;
  265. }
  266. public function get_presentation($data) {
  267. return $data->itemsize . '|'. $data->itemmaxlength;
  268. }
  269. public function get_hasvalue() {
  270. return 1;
  271. }
  272. public function can_switch_require() {
  273. return true;
  274. }
  275. public function value_type() {
  276. return PARAM_RAW;
  277. }
  278. public function clean_input_value($value) {
  279. return s($value);
  280. }
  281. }