Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/feedback/item/numeric/lib.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 defined('MOODLE_INTERNAL') OR die('not allowed');
00018 require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
00019 
00020 class feedback_item_numeric extends feedback_item_base {
00021     protected $type = "numeric";
00022     public $sep_dec, $sep_thous;
00023     private $commonparams;
00024     private $item_form;
00025     private $item;
00026 
00027     public function init() {
00028         $this->sep_dec = get_string('separator_decimal', 'feedback');
00029         if (substr($this->sep_dec, 0, 2) == '[[') {
00030             $this->sep_dec = FEEDBACK_DECIMAL;
00031         }
00032 
00033         $this->sep_thous = get_string('separator_thousand', 'feedback');
00034         if (substr($this->sep_thous, 0, 2) == '[[') {
00035             $this->sep_thous = FEEDBACK_THOUSAND;
00036         }
00037     }
00038 
00039     public function build_editform($item, $feedback, $cm) {
00040         global $DB, $CFG;
00041         require_once('numeric_form.php');
00042 
00043         //get the lastposition number of the feedback_items
00044         $position = $item->position;
00045         $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
00046         if ($position == -1) {
00047             $i_formselect_last = $lastposition + 1;
00048             $i_formselect_value = $lastposition + 1;
00049             $item->position = $lastposition + 1;
00050         } else {
00051             $i_formselect_last = $lastposition;
00052             $i_formselect_value = $item->position;
00053         }
00054         //the elements for position dropdownlist
00055         $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
00056 
00057         $item->presentation = empty($item->presentation) ? '' : $item->presentation;
00058 
00059         $range_from_to = explode('|', $item->presentation);
00060         if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
00061             $range_from = str_replace(FEEDBACK_DECIMAL,
00062                                 $this->sep_dec,
00063                                 floatval($range_from_to[0]));
00064         } else {
00065             $range_from = '-';
00066         }
00067 
00068         if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
00069             $range_to = str_replace(FEEDBACK_DECIMAL,
00070                                 $this->sep_dec,
00071                                 floatval($range_from_to[1]));
00072         } else {
00073             $range_to = '-';
00074         }
00075 
00076         $item->rangefrom = $range_from;
00077         $item->rangeto = $range_to;
00078 
00079         //all items for dependitem
00080         $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
00081         $commonparams = array('cmid'=>$cm->id,
00082                              'id'=>isset($item->id) ? $item->id : null,
00083                              'typ'=>$item->typ,
00084                              'items'=>$feedbackitems,
00085                              'feedback'=>$feedback->id);
00086 
00087         //build the form
00088         $customdata = array('item' => $item,
00089                             'common' => $commonparams,
00090                             'positionlist' => $positionlist,
00091                             'position' => $position);
00092 
00093         $this->item_form = new feedback_numeric_form('edit_item.php', $customdata);
00094     }
00095 
00096     //this function only can used after the call of build_editform()
00097     public function show_editform() {
00098         $this->item_form->display();
00099     }
00100 
00101     public function is_cancelled() {
00102         return $this->item_form->is_cancelled();
00103     }
00104 
00105     public function get_data() {
00106         if ($this->item = $this->item_form->get_data()) {
00107             return true;
00108         }
00109         return false;
00110     }
00111 
00112     public function save_item() {
00113         global $DB;
00114 
00115         if (!$item = $this->item_form->get_data()) {
00116             return false;
00117         }
00118 
00119         if (isset($item->clone_item) AND $item->clone_item) {
00120             $item->id = ''; //to clone this item
00121             $item->position++;
00122         }
00123 
00124         $item->hasvalue = $this->get_hasvalue();
00125         if (!$item->id) {
00126             $item->id = $DB->insert_record('feedback_item', $item);
00127         } else {
00128             $DB->update_record('feedback_item', $item);
00129         }
00130 
00131         return $DB->get_record('feedback_item', array('id'=>$item->id));
00132     }
00133 
00134 
00135     //liefert eine Struktur ->name, ->data = array(mit Antworten)
00136     public function get_analysed($item, $groupid = false, $courseid = false) {
00137         global $DB;
00138 
00139         $analysed = null;
00140         $analysed->data = array();
00141         $analysed->name = $item->name;
00142         $values = feedback_get_group_values($item, $groupid, $courseid);
00143 
00144         $avg = 0.0;
00145         $counter = 0;
00146         if ($values) {
00147             $data = array();
00148             foreach ($values as $value) {
00149                 if (is_numeric($value->value)) {
00150                     $data[] = $value->value;
00151                     $avg += $value->value;
00152                     $counter++;
00153                 }
00154             }
00155             $avg = $counter > 0 ? $avg / $counter : 0;
00156             $analysed->data = $data;
00157             $analysed->avg = $avg;
00158         }
00159         return $analysed;
00160     }
00161 
00162     public function get_printval($item, $value) {
00163         if (!isset($value->value)) {
00164             return '';
00165         }
00166 
00167         return $value->value;
00168     }
00169 
00170     public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
00171 
00172         $values = $this->get_analysed($item, $groupid, $courseid);
00173 
00174         if (isset($values->data) AND is_array($values->data)) {
00175             echo '<tr><th colspan="2" align="left">';
00176             echo $itemnr.'&nbsp;('.$item->label.') '.$item->name;
00177             echo '</th></tr>';
00178 
00179             foreach ($values->data as $value) {
00180                 echo '<tr><td colspan="2" valign="top" align="left">';
00181                 echo '-&nbsp;&nbsp;'.number_format($value, 2, $this->sep_dec, $this->sep_thous);
00182                 echo '</td></tr>';
00183             }
00184 
00185             if (isset($values->avg)) {
00186                 $avg = number_format($values->avg, 2, $this->sep_dec, $this->sep_thous);
00187             } else {
00188                 $avg = number_format(0, 2, $this->sep_dec, $this->sep_thous);
00189             }
00190             echo '<tr><td align="left" colspan="2"><b>';
00191             echo get_string('average', 'feedback').': '.$avg;
00192             echo '</b></td></tr>';
00193         }
00194     }
00195 
00196     public function excelprint_item(&$worksheet, $row_offset,
00197                              $xls_formats, $item,
00198                              $groupid, $courseid = false) {
00199 
00200         $analysed_item = $this->get_analysed($item, $groupid, $courseid);
00201 
00202         $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
00203         $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
00204         $data = $analysed_item->data;
00205         if (is_array($data)) {
00206 
00207             //mittelwert anzeigen
00208             $worksheet->write_string($row_offset,
00209                                      2,
00210                                      get_string('average', 'feedback'),
00211                                      $xls_formats->value_bold);
00212 
00213             $worksheet->write_number($row_offset + 1,
00214                                      2,
00215                                      $analysed_item->avg,
00216                                      $xls_formats->value_bold);
00217             $row_offset++;
00218         }
00219         $row_offset++;
00220         return $row_offset;
00221     }
00222 
00230     public function print_item_preview($item) {
00231         global $OUTPUT, $DB;
00232 
00233         $align = right_to_left() ? 'right' : 'left';
00234         $str_required_mark = '<span class="feedback_required_mark">*</span>';
00235 
00236         //get the range
00237         $range_from_to = explode('|', $item->presentation);
00238 
00239         //get the min-value
00240         if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
00241             $range_from = floatval($range_from_to[0]);
00242         } else {
00243             $range_from = 0;
00244         }
00245 
00246         //get the max-value
00247         if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
00248             $range_to = floatval($range_from_to[1]);
00249         } else {
00250             $range_to = 0;
00251         }
00252 
00253         $requiredmark =  ($item->required == 1) ? $str_required_mark : '';
00254         //print the question and label
00255         echo '<div class="feedback_item_label_'.$align.'">';
00256         echo '('.$item->label.') ';
00257         echo format_text($item->name . $requiredmark, true, false, false);
00258         if ($item->dependitem) {
00259             $params = array('id'=>$item->dependitem);
00260             if ($dependitem = $DB->get_record('feedback_item', $params)) {
00261                 echo ' <span class="feedback_depend">';
00262                 echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
00263                 echo '</span>';
00264             }
00265         }
00266         echo '<span class="feedback_item_numinfo">';
00267         switch(true) {
00268             case ($range_from === '-' AND is_numeric($range_to)):
00269                 echo ' ('.get_string('maximal', 'feedback').
00270                         ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
00271                 break;
00272             case (is_numeric($range_from) AND $range_to === '-'):
00273                 echo ' ('.get_string('minimal', 'feedback').
00274                         ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
00275                 break;
00276             case ($range_from === '-' AND $range_to === '-'):
00277                 break;
00278             default:
00279                 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
00280                         ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
00281                 break;
00282         }
00283         echo '</span>';
00284         echo '</div>';
00285 
00286         //print the presentation
00287         echo '<div class="feedback_item_presentation_'.$align.'">';
00288         echo '<span class="feedback_item_textfield">';
00289         echo '<input type="text" '.
00290                     'name="'.$item->typ.'_'.$item->id.'" '.
00291                     'size="10" '.
00292                     'maxlength="10" '.
00293                     'value="" />';
00294 
00295         echo '</span>';
00296         echo '</div>';
00297     }
00298 
00308     public function print_item_complete($item, $value = '', $highlightrequire = false) {
00309         global $OUTPUT;
00310         $align = right_to_left() ? 'right' : 'left';
00311         $str_required_mark = '<span class="feedback_required_mark">*</span>';
00312 
00313         //get the range
00314         $range_from_to = explode('|', $item->presentation);
00315 
00316         //get the min-value
00317         if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
00318             $range_from = floatval($range_from_to[0]);
00319         } else {
00320             $range_from = 0;
00321         }
00322 
00323         //get the max-value
00324         if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
00325             $range_to = floatval($range_from_to[1]);
00326         } else {
00327             $range_to = 0;
00328         }
00329 
00330         if ($highlightrequire AND (!$this->check_value($value, $item))) {
00331             $highlight = ' missingrequire';
00332         } else {
00333             $highlight = '';
00334         }
00335         $requiredmark = ($item->required == 1) ? $str_required_mark : '';
00336 
00337         //print the question and label
00338         echo '<div class="feedback_item_label_'.$align.$highlight.'">';
00339         echo format_text($item->name . $requiredmark, true, false, false);
00340         echo '<span class="feedback_item_numinfo">';
00341         switch(true) {
00342             case ($range_from === '-' AND is_numeric($range_to)):
00343                 echo ' ('.get_string('maximal', 'feedback').
00344                         ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
00345                 break;
00346             case (is_numeric($range_from) AND $range_to === '-'):
00347                 echo ' ('.get_string('minimal', 'feedback').
00348                         ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
00349                 break;
00350             case ($range_from === '-' AND $range_to === '-'):
00351                 break;
00352             default:
00353                 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
00354                         ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
00355                 break;
00356         }
00357         echo '</span>';
00358         echo '</div>';
00359 
00360         //print the presentation
00361         echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
00362         echo '<span class="feedback_item_textfield">';
00363         echo '<input type="text" '.
00364                      'name="'.$item->typ.'_'.$item->id.'" '.
00365                      'size="10" '.
00366                      'maxlength="10" '.
00367                      'value="'.($value ? $value : '').'" />';
00368 
00369         echo '</span>';
00370         echo '</div>';
00371     }
00372 
00381     public function print_item_show_value($item, $value = '') {
00382         global $OUTPUT;
00383         $align = right_to_left() ? 'right' : 'left';
00384         $str_required_mark = '<span class="feedback_required_mark">*</span>';
00385 
00386         //get the range
00387         $range_from_to = explode('|', $item->presentation);
00388         //get the min-value
00389         if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
00390             $range_from = floatval($range_from_to[0]);
00391         } else {
00392             $range_from = 0;
00393         }
00394         //get the max-value
00395         if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
00396             $range_to = floatval($range_from_to[1]);
00397         } else {
00398             $range_to = 0;
00399         }
00400         $requiredmark = ($item->required == 1) ? $str_required_mark : '';
00401 
00402         //print the question and label
00403         echo '<div class="feedback_item_label_'.$align.'">';
00404         echo '('.$item->label.') ';
00405         echo format_text($item->name . $requiredmark, true, false, false);
00406         switch(true) {
00407             case ($range_from === '-' AND is_numeric($range_to)):
00408                 echo ' ('.get_string('maximal', 'feedback').
00409                     ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
00410                 break;
00411             case (is_numeric($range_from) AND $range_to === '-'):
00412                 echo ' ('.get_string('minimal', 'feedback').
00413                     ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
00414                 break;
00415             case ($range_from === '-' AND $range_to === '-'):
00416                 break;
00417             default:
00418                 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
00419                     ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
00420                 break;
00421         }
00422         echo '</div>';
00423 
00424         //print the presentation
00425         echo '<div class="feedback_item_presentation_'.$align.'">';
00426         echo $OUTPUT->box_start('generalbox boxalign'.$align);
00427         if (is_numeric($value)) {
00428             $str_num_value = number_format($value, 2, $this->sep_dec, $this->sep_thous);
00429         } else {
00430             $str_num_value = '&nbsp;';
00431         }
00432         echo $str_num_value;
00433         echo $OUTPUT->box_end();
00434         echo '</div>';
00435     }
00436 
00437     public function check_value($value, $item) {
00438         $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
00439         //if the item is not required, so the check is true if no value is given
00440         if ((!isset($value) OR $value == '') AND $item->required != 1) {
00441             return true;
00442         }
00443         if (!is_numeric($value)) {
00444             return false;
00445         }
00446 
00447         $range_from_to = explode('|', $item->presentation);
00448         if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
00449             $range_from = floatval($range_from_to[0]);
00450         } else {
00451             $range_from = '-';
00452         }
00453         if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
00454             $range_to = floatval($range_from_to[1]);
00455         } else {
00456             $range_to = '-';
00457         }
00458 
00459         switch(true) {
00460             case ($range_from === '-' AND is_numeric($range_to)):
00461                 if (floatval($value) <= $range_to) {
00462                     return true;
00463                 }
00464                 break;
00465             case (is_numeric($range_from) AND $range_to === '-'):
00466                 if (floatval($value) >= $range_from) {
00467                     return true;
00468                 }
00469                 break;
00470             case ($range_from === '-' AND $range_to === '-'):
00471                 return true;
00472                 break;
00473             default:
00474                 if (floatval($value) >= $range_from AND floatval($value) <= $range_to) {
00475                     return true;
00476                 }
00477                 break;
00478         }
00479 
00480         return false;
00481     }
00482 
00483     public function create_value($data) {
00484         $data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data);
00485 
00486         if (is_numeric($data)) {
00487             $data = floatval($data);
00488         } else {
00489             $data = '';
00490         }
00491         return $data;
00492     }
00493 
00494     //compares the dbvalue with the dependvalue
00495     //dbvalue is the number put in by the user
00496     //dependvalue is the value that is compared
00497     public function compare_value($item, $dbvalue, $dependvalue) {
00498         if ($dbvalue == $dependvalue) {
00499             return true;
00500         }
00501         return false;
00502     }
00503 
00504     public function get_presentation($data) {
00505         $num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom);
00506         if (is_numeric($num1)) {
00507             $num1 = floatval($num1);
00508         } else {
00509             $num1 = '-';
00510         }
00511 
00512         $num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto);
00513         if (is_numeric($num2)) {
00514             $num2 = floatval($num2);
00515         } else {
00516             $num2 = '-';
00517         }
00518 
00519         if ($num1 === '-' OR $num2 === '-') {
00520             return $num1 . '|'. $num2;
00521         }
00522 
00523         if ($num1 > $num2) {
00524             return $num2 . '|'. $num1;
00525         } else {
00526             return $num1 . '|'. $num2;
00527         }
00528     }
00529 
00530     public function get_hasvalue() {
00531         return 1;
00532     }
00533 
00534     public function can_switch_require() {
00535         return true;
00536     }
00537 }
 All Data Structures Namespaces Files Functions Variables Enumerations