[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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_numeric extends feedback_item_base { 21 protected $type = "numeric"; 22 23 public function build_editform($item, $feedback, $cm) { 24 global $DB, $CFG; 25 require_once ('numeric_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 $range_from_to = explode('|', $item->presentation); 44 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) { 45 $range_from = $this->format_float($range_from_to[0]); 46 } else { 47 $range_from = '-'; 48 } 49 50 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) { 51 $range_to = $this->format_float($range_from_to[1]); 52 } else { 53 $range_to = '-'; 54 } 55 56 $item->rangefrom = $range_from; 57 $item->rangeto = $range_to; 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_numeric_form('edit_item.php', $customdata); 74 } 75 76 public function save_item() { 77 global $DB; 78 79 if (!$item = $this->item_form->get_data()) { 80 return false; 81 } 82 83 if (isset($item->clone_item) AND $item->clone_item) { 84 $item->id = ''; //to clone this item 85 $item->position++; 86 } 87 88 $item->hasvalue = $this->get_hasvalue(); 89 if (!$item->id) { 90 $item->id = $DB->insert_record('feedback_item', $item); 91 } else { 92 $DB->update_record('feedback_item', $item); 93 } 94 95 return $DB->get_record('feedback_item', array('id'=>$item->id)); 96 } 97 98 /** 99 * Helper function for collected data, both for analysis page and export to excel 100 * 101 * @param stdClass $item the db-object from feedback_item 102 * @param int $groupid 103 * @param int $courseid 104 * @return stdClass 105 */ 106 protected function get_analysed($item, $groupid = false, $courseid = false) { 107 global $DB; 108 109 $analysed = new stdClass(); 110 $analysed->data = array(); 111 $analysed->name = $item->name; 112 $values = feedback_get_group_values($item, $groupid, $courseid); 113 114 $avg = 0.0; 115 $counter = 0; 116 if ($values) { 117 $data = array(); 118 foreach ($values as $value) { 119 if (is_numeric($value->value)) { 120 $data[] = $value->value; 121 $avg += $value->value; 122 $counter++; 123 } 124 } 125 $avg = $counter > 0 ? $avg / $counter : null; 126 $analysed->data = $data; 127 $analysed->avg = $avg; 128 } 129 return $analysed; 130 } 131 132 public function get_printval($item, $value) { 133 if (!isset($value->value)) { 134 return ''; 135 } 136 137 return $value->value; 138 } 139 140 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) { 141 142 $values = $this->get_analysed($item, $groupid, $courseid); 143 144 if (isset($values->data) AND is_array($values->data)) { 145 echo "<table class=\"analysis itemtype_{$item->typ}\">"; 146 echo '<tr><th colspan="2" align="left">'; 147 echo $itemnr . ' '; 148 if (strval($item->label) !== '') { 149 echo '('. format_string($item->label).') '; 150 } 151 echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false)); 152 echo '</th></tr>'; 153 154 foreach ($values->data as $value) { 155 echo '<tr><td colspan="2" class="singlevalue">'; 156 echo $this->format_float($value); 157 echo '</td></tr>'; 158 } 159 160 if (isset($values->avg)) { 161 $avg = format_float($values->avg, 2); 162 } else { 163 $avg = '-'; 164 } 165 echo '<tr><td colspan="2"><b>'; 166 echo get_string('average', 'feedback').': '.$avg; 167 echo '</b></td></tr>'; 168 echo '</table>'; 169 } 170 } 171 172 public function excelprint_item(&$worksheet, $row_offset, 173 $xls_formats, $item, 174 $groupid, $courseid = false) { 175 176 $analysed_item = $this->get_analysed($item, $groupid, $courseid); 177 178 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2); 179 $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2); 180 $data = $analysed_item->data; 181 if (is_array($data)) { 182 183 // Export average. 184 $worksheet->write_string($row_offset, 185 2, 186 get_string('average', 'feedback'), 187 $xls_formats->value_bold); 188 189 if (isset($analysed_item->avg)) { 190 $worksheet->write_number($row_offset + 1, 191 2, 192 $analysed_item->avg, 193 $xls_formats->value_bold); 194 } else { 195 $worksheet->write_string($row_offset + 1, 196 2, 197 '', 198 $xls_formats->value_bold); 199 } 200 $row_offset++; 201 } 202 $row_offset++; 203 return $row_offset; 204 } 205 206 /** 207 * Prints the float nicely in the localized format 208 * 209 * Similar to format_float() but automatically calculates the number of decimal places 210 * 211 * @param float $value The float to print 212 * @return string 213 */ 214 protected function format_float($value) { 215 if (!is_numeric($value)) { 216 return null; 217 } 218 $decimal = is_int($value) ? 0 : strlen(substr(strrchr($value, '.'), 1)); 219 return format_float($value, $decimal); 220 } 221 222 /** 223 * Returns human-readable boundaries (min - max) 224 * @param stdClass $item 225 * @return string 226 */ 227 protected function get_boundaries_for_display($item) { 228 list($rangefrom, $rangeto) = explode('|', $item->presentation); 229 if (!isset($rangefrom) || !is_numeric($rangefrom)) { 230 $rangefrom = null; 231 } 232 if (!isset($rangeto) || !is_numeric($rangeto)) { 233 $rangeto = null; 234 } 235 236 if (is_null($rangefrom) && is_numeric($rangeto)) { 237 return ' (' . get_string('maximal', 'feedback') . 238 ': ' . $this->format_float($rangeto) . ')'; 239 } 240 if (is_numeric($rangefrom) && is_null($rangeto)) { 241 return ' (' . get_string('minimal', 'feedback') . 242 ': ' . $this->format_float($rangefrom) . ')'; 243 } 244 if (is_null($rangefrom) && is_null($rangeto)) { 245 return ''; 246 } 247 return ' (' . $this->format_float($rangefrom) . 248 ' - ' . $this->format_float($rangeto) . ')'; 249 } 250 251 /** 252 * Returns the postfix to be appended to the display name that is based on other settings 253 * 254 * @param stdClass $item 255 * @return string 256 */ 257 public function get_display_name_postfix($item) { 258 return html_writer::span($this->get_boundaries_for_display($item), 'boundaries'); 259 } 260 261 /** 262 * Adds an input element to the complete form 263 * 264 * @param stdClass $item 265 * @param mod_feedback_complete_form $form 266 */ 267 public function complete_form_element($item, $form) { 268 $name = $this->get_display_name($item); 269 $inputname = $item->typ . '_' . $item->id; 270 $form->add_form_element($item, 271 ['text', $inputname, $name], 272 true, 273 false 274 ); 275 $form->set_element_type($inputname, PARAM_NOTAGS); 276 $tmpvalue = $this->format_float($form->get_item_value($item)); 277 $form->set_element_default($inputname, $tmpvalue); 278 279 // Add form validation rule to check for boundaries. 280 $form->add_validation_rule(function($values, $files) use ($item) { 281 $inputname = $item->typ . '_' . $item->id; 282 list($rangefrom, $rangeto) = explode('|', $item->presentation); 283 if (!isset($values[$inputname]) || trim($values[$inputname]) === '') { 284 return $item->required ? array($inputname => get_string('required')) : true; 285 } 286 $value = unformat_float($values[$inputname], true); 287 if ($value === false) { 288 return array($inputname => get_string('invalidnum', 'error')); 289 } 290 if ((is_numeric($rangefrom) && $value < floatval($rangefrom)) || 291 (is_numeric($rangeto) && $value > floatval($rangeto))) { 292 return array($inputname => get_string('numberoutofrange', 'feedback')); 293 } 294 return true; 295 }); 296 } 297 298 public function create_value($data) { 299 $data = unformat_float($data, true); 300 301 if (is_numeric($data)) { 302 $data = floatval($data); 303 } else { 304 $data = ''; 305 } 306 return $data; 307 } 308 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |