PageRenderTime 65ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/feedback/item/textarea/lib.php

https://github.com/raymanuk/moodle
PHP | 354 lines | 248 code | 52 blank | 54 comment | 32 complexity | 4c59b6230ef7ced580e3cea0fd56badc 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_textarea extends feedback_item_base {
  19. protected $type = "textarea";
  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('textarea_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. $width_and_height = explode('|', $item->presentation);
  43. if (isset($width_and_height[0]) AND $width_and_height[0] >= 5) {
  44. $itemwidth = $width_and_height[0];
  45. } else {
  46. $itemwidth = 30;
  47. }
  48. if (isset($width_and_height[1])) {
  49. $itemheight = $width_and_height[1];
  50. } else {
  51. $itemheight = 5;
  52. }
  53. $item->itemwidth = $itemwidth;
  54. $item->itemheight = $itemheight;
  55. //all items for dependitem
  56. $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
  57. $commonparams = array('cmid'=>$cm->id,
  58. 'id'=>isset($item->id) ? $item->id : null,
  59. 'typ'=>$item->typ,
  60. 'items'=>$feedbackitems,
  61. 'feedback'=>$feedback->id);
  62. //build the form
  63. $customdata = array('item' => $item,
  64. 'common' => $commonparams,
  65. 'positionlist' => $positionlist,
  66. 'position' => $position);
  67. $this->item_form = new feedback_textarea_form('edit_item.php', $customdata);
  68. }
  69. //this function only can used after the call of build_editform()
  70. public function show_editform() {
  71. $this->item_form->display();
  72. }
  73. public function is_cancelled() {
  74. return $this->item_form->is_cancelled();
  75. }
  76. public function get_data() {
  77. if ($this->item = $this->item_form->get_data()) {
  78. return true;
  79. }
  80. return false;
  81. }
  82. public function save_item() {
  83. global $DB;
  84. if (!$item = $this->item_form->get_data()) {
  85. return false;
  86. }
  87. if (isset($item->clone_item) AND $item->clone_item) {
  88. $item->id = ''; //to clone this item
  89. $item->position++;
  90. }
  91. $item->hasvalue = $this->get_hasvalue();
  92. if (!$item->id) {
  93. $item->id = $DB->insert_record('feedback_item', $item);
  94. } else {
  95. $DB->update_record('feedback_item', $item);
  96. }
  97. return $DB->get_record('feedback_item', array('id'=>$item->id));
  98. }
  99. //liefert eine Struktur ->name, ->data = array(mit Antworten)
  100. public function get_analysed($item, $groupid = false, $courseid = false) {
  101. global $DB;
  102. $analysed_val = new stdClass();
  103. $analysed_val->data = array();
  104. $analysed_val->name = $item->name;
  105. $values = feedback_get_group_values($item, $groupid, $courseid);
  106. if ($values) {
  107. $data = array();
  108. foreach ($values as $value) {
  109. $data[] = str_replace("\n", '<br />', $value->value);
  110. }
  111. $analysed_val->data = $data;
  112. }
  113. return $analysed_val;
  114. }
  115. public function get_printval($item, $value) {
  116. if (!isset($value->value)) {
  117. return '';
  118. }
  119. return $value->value;
  120. }
  121. public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
  122. $values = feedback_get_group_values($item, $groupid, $courseid);
  123. if ($values) {
  124. echo '<tr><th colspan="2" align="left">';
  125. echo $itemnr.'&nbsp;('.$item->label.') '.$item->name;
  126. echo '</th></tr>';
  127. foreach ($values as $value) {
  128. echo '<tr>';
  129. echo '<td valign="top" align="left">';
  130. echo '-&nbsp;&nbsp;';
  131. echo '</td>';
  132. echo '<td align="left" valign="top">';
  133. echo str_replace("\n", '<br />', $value->value);
  134. echo '</td>';
  135. echo '</tr>';
  136. }
  137. }
  138. }
  139. public function excelprint_item(&$worksheet, $row_offset,
  140. $xls_formats, $item,
  141. $groupid, $courseid = false) {
  142. $analysed_item = $this->get_analysed($item, $groupid, $courseid);
  143. $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
  144. $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
  145. $data = $analysed_item->data;
  146. if (is_array($data)) {
  147. if (isset($data[0])) {
  148. $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);
  149. }
  150. $row_offset++;
  151. $sizeofdata = count($data);
  152. for ($i = 1; $i < $sizeofdata; $i++) {
  153. $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[$i], ENT_QUOTES), $xls_formats->default);
  154. $row_offset++;
  155. }
  156. }
  157. $row_offset++;
  158. return $row_offset;
  159. }
  160. /**
  161. * print the item at the edit-page of feedback
  162. *
  163. * @global object
  164. * @param object $item
  165. * @return void
  166. */
  167. public function print_item_preview($item) {
  168. global $OUTPUT, $DB;
  169. $align = right_to_left() ? 'right' : 'left';
  170. $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
  171. get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
  172. $presentation = explode ("|", $item->presentation);
  173. $requiredmark = ($item->required == 1) ? $strrequiredmark : '';
  174. //print the question and label
  175. $inputname = $item->typ . '_' . $item->id;
  176. echo '<div class="feedback_item_label_'.$align.'">';
  177. echo '<label for="'. $inputname .'">';
  178. echo '('.$item->label.') ';
  179. echo format_text($item->name.$requiredmark, true, false, false);
  180. if ($item->dependitem) {
  181. if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
  182. echo ' <span class="feedback_depend">';
  183. echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
  184. echo '</span>';
  185. }
  186. }
  187. echo '</label>';
  188. echo '</div>';
  189. //print the presentation
  190. echo '<div class="feedback_item_presentation_'.$align.'">';
  191. echo '<span class="feedback_item_textarea">';
  192. echo '<textarea id="'.$inputname.'" '.
  193. 'name="'.$inputname.'" '.
  194. 'cols="'.$presentation[0].'" '.
  195. 'rows="'.$presentation[1].'">';
  196. echo '</textarea>';
  197. echo '</span>';
  198. echo '</div>';
  199. }
  200. /**
  201. * print the item at the complete-page of feedback
  202. *
  203. * @global object
  204. * @param object $item
  205. * @param string $value
  206. * @param bool $highlightrequire
  207. * @return void
  208. */
  209. public function print_item_complete($item, $value = '', $highlightrequire = false) {
  210. global $OUTPUT;
  211. $align = right_to_left() ? 'right' : 'left';
  212. $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
  213. get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
  214. $presentation = explode ("|", $item->presentation);
  215. $requiredmark = ($item->required == 1) ? $strrequiredmark :'';
  216. //print the question and label
  217. $inputname = $item->typ . '_' . $item->id;
  218. echo '<div class="feedback_item_label_'.$align.'">';
  219. echo '<label for="'. $inputname .'">';
  220. echo format_text($item->name . $requiredmark, true, false, false);
  221. if ($highlightrequire AND $item->required AND strval($value) == '') {
  222. echo '<br class="error"><span id="id_error_'.$inputname.'" class="error"> '.get_string('err_required', 'form').
  223. '</span><br id="id_error_break_'.$inputname.'" class="error" >';
  224. }
  225. echo '</label>';
  226. echo '</div>';
  227. //print the presentation
  228. echo '<div class="feedback_item_presentation_'.$align.'">';
  229. echo '<span class="feedback_item_textarea">';
  230. echo '<textarea id="'.$inputname.'" '.
  231. 'name="'.$inputname.'" '.
  232. 'cols="'.$presentation[0].'" '.
  233. 'rows="'.$presentation[1].'">';
  234. echo $value;
  235. echo '</textarea>';
  236. echo '</span>';
  237. echo '</div>';
  238. }
  239. /**
  240. * print the item at the complete-page of feedback
  241. *
  242. * @global object
  243. * @param object $item
  244. * @param string $value
  245. * @return void
  246. */
  247. public function print_item_show_value($item, $value = '') {
  248. global $OUTPUT;
  249. $align = right_to_left() ? 'right' : 'left';
  250. $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
  251. get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
  252. $presentation = explode ("|", $item->presentation);
  253. $requiredmark = ($item->required == 1) ? $strrequiredmark : '';
  254. //print the question and label
  255. echo '<div class="feedback_item_label_'.$align.'">';
  256. echo '('.$item->label.') ';
  257. echo format_text($item->name . $requiredmark, true, false, false);
  258. echo '</div>';
  259. //print the presentation
  260. echo $OUTPUT->box_start('generalbox boxalign'.$align);
  261. echo $value ? str_replace("\n", '<br />', $value) : '&nbsp;';
  262. echo $OUTPUT->box_end();
  263. }
  264. public function check_value($value, $item) {
  265. //if the item is not required, so the check is true if no value is given
  266. if ((!isset($value) OR $value == '') AND $item->required != 1) {
  267. return true;
  268. }
  269. if ($value == "") {
  270. return false;
  271. }
  272. return true;
  273. }
  274. public function create_value($data) {
  275. $data = s($data);
  276. return $data;
  277. }
  278. //compares the dbvalue with the dependvalue
  279. //dbvalue is the value put in by the user
  280. //dependvalue is the value that is compared
  281. public function compare_value($item, $dbvalue, $dependvalue) {
  282. if ($dbvalue == $dependvalue) {
  283. return true;
  284. }
  285. return false;
  286. }
  287. public function get_presentation($data) {
  288. return $data->itemwidth.'|'.$data->itemheight;
  289. }
  290. public function get_hasvalue() {
  291. return 1;
  292. }
  293. public function can_switch_require() {
  294. return true;
  295. }
  296. public function value_type() {
  297. return PARAM_RAW;
  298. }
  299. public function clean_input_value($value) {
  300. return s($value);
  301. }
  302. }