PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/feedback/item/numeric/lib.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 553 lines | 417 code | 72 blank | 64 comment | 70 complexity | f105100aa9b7f1d1e403745336605a77 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_numeric extends feedback_item_base {
  19. protected $type = "numeric";
  20. public $sep_dec, $sep_thous;
  21. private $commonparams;
  22. private $item_form;
  23. private $item;
  24. public function init() {
  25. $this->sep_dec = get_string('separator_decimal', 'feedback');
  26. if (substr($this->sep_dec, 0, 2) == '[[') {
  27. $this->sep_dec = FEEDBACK_DECIMAL;
  28. }
  29. $this->sep_thous = get_string('separator_thousand', 'feedback');
  30. if (substr($this->sep_thous, 0, 2) == '[[') {
  31. $this->sep_thous = FEEDBACK_THOUSAND;
  32. }
  33. }
  34. public function build_editform($item, $feedback, $cm) {
  35. global $DB, $CFG;
  36. require_once('numeric_form.php');
  37. //get the lastposition number of the feedback_items
  38. $position = $item->position;
  39. $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
  40. if ($position == -1) {
  41. $i_formselect_last = $lastposition + 1;
  42. $i_formselect_value = $lastposition + 1;
  43. $item->position = $lastposition + 1;
  44. } else {
  45. $i_formselect_last = $lastposition;
  46. $i_formselect_value = $item->position;
  47. }
  48. //the elements for position dropdownlist
  49. $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
  50. $item->presentation = empty($item->presentation) ? '' : $item->presentation;
  51. $range_from_to = explode('|', $item->presentation);
  52. if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
  53. $range_from = str_replace(FEEDBACK_DECIMAL,
  54. $this->sep_dec,
  55. floatval($range_from_to[0]));
  56. } else {
  57. $range_from = '-';
  58. }
  59. if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
  60. $range_to = str_replace(FEEDBACK_DECIMAL,
  61. $this->sep_dec,
  62. floatval($range_from_to[1]));
  63. } else {
  64. $range_to = '-';
  65. }
  66. $item->rangefrom = $range_from;
  67. $item->rangeto = $range_to;
  68. //all items for dependitem
  69. $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
  70. $commonparams = array('cmid'=>$cm->id,
  71. 'id'=>isset($item->id) ? $item->id : null,
  72. 'typ'=>$item->typ,
  73. 'items'=>$feedbackitems,
  74. 'feedback'=>$feedback->id);
  75. //build the form
  76. $customdata = array('item' => $item,
  77. 'common' => $commonparams,
  78. 'positionlist' => $positionlist,
  79. 'position' => $position);
  80. $this->item_form = new feedback_numeric_form('edit_item.php', $customdata);
  81. }
  82. //this function only can used after the call of build_editform()
  83. public function show_editform() {
  84. $this->item_form->display();
  85. }
  86. public function is_cancelled() {
  87. return $this->item_form->is_cancelled();
  88. }
  89. public function get_data() {
  90. if ($this->item = $this->item_form->get_data()) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. public function save_item() {
  96. global $DB;
  97. if (!$item = $this->item_form->get_data()) {
  98. return false;
  99. }
  100. if (isset($item->clone_item) AND $item->clone_item) {
  101. $item->id = ''; //to clone this item
  102. $item->position++;
  103. }
  104. $item->hasvalue = $this->get_hasvalue();
  105. if (!$item->id) {
  106. $item->id = $DB->insert_record('feedback_item', $item);
  107. } else {
  108. $DB->update_record('feedback_item', $item);
  109. }
  110. return $DB->get_record('feedback_item', array('id'=>$item->id));
  111. }
  112. //liefert eine Struktur ->name, ->data = array(mit Antworten)
  113. public function get_analysed($item, $groupid = false, $courseid = false) {
  114. global $DB;
  115. $analysed = new stdClass();
  116. $analysed->data = array();
  117. $analysed->name = $item->name;
  118. $values = feedback_get_group_values($item, $groupid, $courseid);
  119. $avg = 0.0;
  120. $counter = 0;
  121. if ($values) {
  122. $data = array();
  123. foreach ($values as $value) {
  124. if (is_numeric($value->value)) {
  125. $data[] = $value->value;
  126. $avg += $value->value;
  127. $counter++;
  128. }
  129. }
  130. $avg = $counter > 0 ? $avg / $counter : 0;
  131. $analysed->data = $data;
  132. $analysed->avg = $avg;
  133. }
  134. return $analysed;
  135. }
  136. public function get_printval($item, $value) {
  137. if (!isset($value->value)) {
  138. return '';
  139. }
  140. return $value->value;
  141. }
  142. public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
  143. $values = $this->get_analysed($item, $groupid, $courseid);
  144. if (isset($values->data) AND is_array($values->data)) {
  145. echo '<tr><th colspan="2" align="left">';
  146. echo $itemnr.'&nbsp;('.$item->label.') '.$item->name;
  147. echo '</th></tr>';
  148. foreach ($values->data as $value) {
  149. echo '<tr><td colspan="2" valign="top" align="left">';
  150. echo '-&nbsp;&nbsp;'.number_format($value, 2, $this->sep_dec, $this->sep_thous);
  151. echo '</td></tr>';
  152. }
  153. if (isset($values->avg)) {
  154. $avg = number_format($values->avg, 2, $this->sep_dec, $this->sep_thous);
  155. } else {
  156. $avg = number_format(0, 2, $this->sep_dec, $this->sep_thous);
  157. }
  158. echo '<tr><td align="left" colspan="2"><b>';
  159. echo get_string('average', 'feedback').': '.$avg;
  160. echo '</b></td></tr>';
  161. }
  162. }
  163. public function excelprint_item(&$worksheet, $row_offset,
  164. $xls_formats, $item,
  165. $groupid, $courseid = false) {
  166. $analysed_item = $this->get_analysed($item, $groupid, $courseid);
  167. $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
  168. $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
  169. $data = $analysed_item->data;
  170. if (is_array($data)) {
  171. //mittelwert anzeigen
  172. $worksheet->write_string($row_offset,
  173. 2,
  174. get_string('average', 'feedback'),
  175. $xls_formats->value_bold);
  176. $worksheet->write_number($row_offset + 1,
  177. 2,
  178. $analysed_item->avg,
  179. $xls_formats->value_bold);
  180. $row_offset++;
  181. }
  182. $row_offset++;
  183. return $row_offset;
  184. }
  185. /**
  186. * print the item at the edit-page of feedback
  187. *
  188. * @global object
  189. * @param object $item
  190. * @return void
  191. */
  192. public function print_item_preview($item) {
  193. global $OUTPUT, $DB;
  194. $align = right_to_left() ? 'right' : 'left';
  195. $str_required_mark = '<span class="feedback_required_mark">*</span>';
  196. //get the range
  197. $range_from_to = explode('|', $item->presentation);
  198. //get the min-value
  199. if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
  200. $range_from = floatval($range_from_to[0]);
  201. } else {
  202. $range_from = 0;
  203. }
  204. //get the max-value
  205. if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
  206. $range_to = floatval($range_from_to[1]);
  207. } else {
  208. $range_to = 0;
  209. }
  210. $requiredmark = ($item->required == 1) ? $str_required_mark : '';
  211. //print the question and label
  212. echo '<div class="feedback_item_label_'.$align.'">';
  213. echo '('.$item->label.') ';
  214. echo format_text($item->name . $requiredmark, true, false, false);
  215. if ($item->dependitem) {
  216. $params = array('id'=>$item->dependitem);
  217. if ($dependitem = $DB->get_record('feedback_item', $params)) {
  218. echo ' <span class="feedback_depend">';
  219. echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
  220. echo '</span>';
  221. }
  222. }
  223. echo '<span class="feedback_item_numinfo">';
  224. switch(true) {
  225. case ($range_from === '-' AND is_numeric($range_to)):
  226. echo ' ('.get_string('maximal', 'feedback').
  227. ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
  228. break;
  229. case (is_numeric($range_from) AND $range_to === '-'):
  230. echo ' ('.get_string('minimal', 'feedback').
  231. ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
  232. break;
  233. case ($range_from === '-' AND $range_to === '-'):
  234. break;
  235. default:
  236. echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
  237. ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
  238. break;
  239. }
  240. echo '</span>';
  241. echo '</div>';
  242. //print the presentation
  243. echo '<div class="feedback_item_presentation_'.$align.'">';
  244. echo '<span class="feedback_item_textfield">';
  245. echo '<input type="text" '.
  246. 'name="'.$item->typ.'_'.$item->id.'" '.
  247. 'size="10" '.
  248. 'maxlength="10" '.
  249. 'value="" />';
  250. echo '</span>';
  251. echo '</div>';
  252. }
  253. /**
  254. * print the item at the complete-page of feedback
  255. *
  256. * @global object
  257. * @param object $item
  258. * @param string $value
  259. * @param bool $highlightrequire
  260. * @return void
  261. */
  262. public function print_item_complete($item, $value = '', $highlightrequire = false) {
  263. global $OUTPUT;
  264. $align = right_to_left() ? 'right' : 'left';
  265. $str_required_mark = '<span class="feedback_required_mark">*</span>';
  266. //get the range
  267. $range_from_to = explode('|', $item->presentation);
  268. //get the min-value
  269. if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
  270. $range_from = floatval($range_from_to[0]);
  271. } else {
  272. $range_from = 0;
  273. }
  274. //get the max-value
  275. if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
  276. $range_to = floatval($range_from_to[1]);
  277. } else {
  278. $range_to = 0;
  279. }
  280. if ($highlightrequire AND (!$this->check_value($value, $item))) {
  281. $highlight = ' missingrequire';
  282. } else {
  283. $highlight = '';
  284. }
  285. $requiredmark = ($item->required == 1) ? $str_required_mark : '';
  286. //print the question and label
  287. echo '<div class="feedback_item_label_'.$align.$highlight.'">';
  288. echo format_text($item->name . $requiredmark, true, false, false);
  289. echo '<span class="feedback_item_numinfo">';
  290. switch(true) {
  291. case ($range_from === '-' AND is_numeric($range_to)):
  292. echo ' ('.get_string('maximal', 'feedback').
  293. ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
  294. break;
  295. case (is_numeric($range_from) AND $range_to === '-'):
  296. echo ' ('.get_string('minimal', 'feedback').
  297. ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
  298. break;
  299. case ($range_from === '-' AND $range_to === '-'):
  300. break;
  301. default:
  302. echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
  303. ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
  304. break;
  305. }
  306. echo '</span>';
  307. echo '</div>';
  308. //print the presentation
  309. echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
  310. echo '<span class="feedback_item_textfield">';
  311. echo '<input type="text" '.
  312. 'name="'.$item->typ.'_'.$item->id.'" '.
  313. 'size="10" '.
  314. 'maxlength="10" '.
  315. 'value="'.$value.'" />';
  316. echo '</span>';
  317. echo '</div>';
  318. }
  319. /**
  320. * print the item at the complete-page of feedback
  321. *
  322. * @global object
  323. * @param object $item
  324. * @param string $value
  325. * @return void
  326. */
  327. public function print_item_show_value($item, $value = '') {
  328. global $OUTPUT;
  329. $align = right_to_left() ? 'right' : 'left';
  330. $str_required_mark = '<span class="feedback_required_mark">*</span>';
  331. //get the range
  332. $range_from_to = explode('|', $item->presentation);
  333. //get the min-value
  334. if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
  335. $range_from = floatval($range_from_to[0]);
  336. } else {
  337. $range_from = 0;
  338. }
  339. //get the max-value
  340. if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
  341. $range_to = floatval($range_from_to[1]);
  342. } else {
  343. $range_to = 0;
  344. }
  345. $requiredmark = ($item->required == 1) ? $str_required_mark : '';
  346. //print the question and label
  347. echo '<div class="feedback_item_label_'.$align.'">';
  348. echo '('.$item->label.') ';
  349. echo format_text($item->name . $requiredmark, true, false, false);
  350. switch(true) {
  351. case ($range_from === '-' AND is_numeric($range_to)):
  352. echo ' ('.get_string('maximal', 'feedback').
  353. ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
  354. break;
  355. case (is_numeric($range_from) AND $range_to === '-'):
  356. echo ' ('.get_string('minimal', 'feedback').
  357. ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
  358. break;
  359. case ($range_from === '-' AND $range_to === '-'):
  360. break;
  361. default:
  362. echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
  363. ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
  364. break;
  365. }
  366. echo '</div>';
  367. //print the presentation
  368. echo '<div class="feedback_item_presentation_'.$align.'">';
  369. echo $OUTPUT->box_start('generalbox boxalign'.$align);
  370. if (is_numeric($value)) {
  371. $str_num_value = number_format($value, 2, $this->sep_dec, $this->sep_thous);
  372. } else {
  373. $str_num_value = '&nbsp;';
  374. }
  375. echo $str_num_value;
  376. echo $OUTPUT->box_end();
  377. echo '</div>';
  378. }
  379. public function check_value($value, $item) {
  380. $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
  381. //if the item is not required, so the check is true if no value is given
  382. if ((!isset($value) OR $value == '') AND $item->required != 1) {
  383. return true;
  384. }
  385. if (!is_numeric($value)) {
  386. return false;
  387. }
  388. $range_from_to = explode('|', $item->presentation);
  389. if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
  390. $range_from = floatval($range_from_to[0]);
  391. } else {
  392. $range_from = '-';
  393. }
  394. if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
  395. $range_to = floatval($range_from_to[1]);
  396. } else {
  397. $range_to = '-';
  398. }
  399. switch(true) {
  400. case ($range_from === '-' AND is_numeric($range_to)):
  401. if (floatval($value) <= $range_to) {
  402. return true;
  403. }
  404. break;
  405. case (is_numeric($range_from) AND $range_to === '-'):
  406. if (floatval($value) >= $range_from) {
  407. return true;
  408. }
  409. break;
  410. case ($range_from === '-' AND $range_to === '-'):
  411. return true;
  412. break;
  413. default:
  414. if (floatval($value) >= $range_from AND floatval($value) <= $range_to) {
  415. return true;
  416. }
  417. break;
  418. }
  419. return false;
  420. }
  421. public function create_value($data) {
  422. $data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data);
  423. if (is_numeric($data)) {
  424. $data = floatval($data);
  425. } else {
  426. $data = '';
  427. }
  428. return $data;
  429. }
  430. //compares the dbvalue with the dependvalue
  431. //dbvalue is the number put in by the user
  432. //dependvalue is the value that is compared
  433. public function compare_value($item, $dbvalue, $dependvalue) {
  434. if ($dbvalue == $dependvalue) {
  435. return true;
  436. }
  437. return false;
  438. }
  439. public function get_presentation($data) {
  440. $num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom);
  441. if (is_numeric($num1)) {
  442. $num1 = floatval($num1);
  443. } else {
  444. $num1 = '-';
  445. }
  446. $num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto);
  447. if (is_numeric($num2)) {
  448. $num2 = floatval($num2);
  449. } else {
  450. $num2 = '-';
  451. }
  452. if ($num1 === '-' OR $num2 === '-') {
  453. return $num1 . '|'. $num2;
  454. }
  455. if ($num1 > $num2) {
  456. return $num2 . '|'. $num1;
  457. } else {
  458. return $num1 . '|'. $num2;
  459. }
  460. }
  461. public function get_hasvalue() {
  462. return 1;
  463. }
  464. public function can_switch_require() {
  465. return true;
  466. }
  467. public function value_type() {
  468. return PARAM_FLOAT;
  469. }
  470. public function clean_input_value($value) {
  471. $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
  472. if (!is_numeric($value)) {
  473. if ($value == '') {
  474. return null; //an empty string should be null
  475. } else {
  476. return clean_param($value, PARAM_TEXT); //we have to know the value if it is wrong
  477. }
  478. }
  479. return clean_param($value, $this->value_type());
  480. }
  481. }