Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/feedback/item/textfield/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_textfield extends feedback_item_base {
00021     protected $type = "textfield";
00022     private $commonparams;
00023     private $item_form;
00024     private $item;
00025 
00026     public function init() {
00027 
00028     }
00029 
00030     public function build_editform($item, $feedback, $cm) {
00031         global $DB, $CFG;
00032         require_once('textfield_form.php');
00033 
00034         //get the lastposition number of the feedback_items
00035         $position = $item->position;
00036         $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
00037         if ($position == -1) {
00038             $i_formselect_last = $lastposition + 1;
00039             $i_formselect_value = $lastposition + 1;
00040             $item->position = $lastposition + 1;
00041         } else {
00042             $i_formselect_last = $lastposition;
00043             $i_formselect_value = $item->position;
00044         }
00045         //the elements for position dropdownlist
00046         $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
00047 
00048         $item->presentation = empty($item->presentation) ? '' : $item->presentation;
00049 
00050         $size_and_length = explode('|', $item->presentation);
00051 
00052         if (isset($size_and_length[0]) AND $size_and_length[0] >= 5) {
00053             $itemsize = $size_and_length[0];
00054         } else {
00055             $itemsize = 30;
00056         }
00057 
00058         $itemlength = isset($size_and_length[1]) ? $size_and_length[1] : 5;
00059 
00060         $item->itemsize = $itemsize;
00061         $item->itemmaxlength = $itemlength;
00062 
00063         //all items for dependitem
00064         $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
00065         $commonparams = array('cmid' => $cm->id,
00066                              'id' => isset($item->id) ? $item->id : null,
00067                              'typ' => $item->typ,
00068                              'items' => $feedbackitems,
00069                              'feedback' => $feedback->id);
00070 
00071         //build the form
00072         $customdata = array('item' => $item,
00073                             'common' => $commonparams,
00074                             'positionlist' => $positionlist,
00075                             'position' => $position);
00076 
00077         $this->item_form = new feedback_textfield_form('edit_item.php', $customdata);
00078     }
00079 
00080     //this function only can used after the call of build_editform()
00081     public function show_editform() {
00082         $this->item_form->display();
00083     }
00084 
00085     public function is_cancelled() {
00086         return $this->item_form->is_cancelled();
00087     }
00088 
00089     public function get_data() {
00090         if ($this->item = $this->item_form->get_data()) {
00091             return true;
00092         }
00093         return false;
00094     }
00095 
00096     public function save_item() {
00097         global $DB;
00098 
00099         if (!$item = $this->item_form->get_data()) {
00100             return false;
00101         }
00102 
00103         if (isset($item->clone_item) AND $item->clone_item) {
00104             $item->id = ''; //to clone this item
00105             $item->position++;
00106         }
00107 
00108         $item->hasvalue = $this->get_hasvalue();
00109         if (!$item->id) {
00110             $item->id = $DB->insert_record('feedback_item', $item);
00111         } else {
00112             $DB->update_record('feedback_item', $item);
00113         }
00114 
00115         return $DB->get_record('feedback_item', array('id'=>$item->id));
00116     }
00117 
00118 
00119     //liefert eine Struktur ->name, ->data = array(mit Antworten)
00120     public function get_analysed($item, $groupid = false, $courseid = false) {
00121         global $DB;
00122 
00123         $analysed_val = null;
00124         $analysed_val->data = null;
00125         $analysed_val->name = $item->name;
00126 
00127         $values = feedback_get_group_values($item, $groupid, $courseid);
00128         if ($values) {
00129             $data = array();
00130             foreach ($values as $value) {
00131                 $data[] = str_replace("\n", '<br />', $value->value);
00132             }
00133             $analysed_val->data = $data;
00134         }
00135         return $analysed_val;
00136     }
00137 
00138     public function get_printval($item, $value) {
00139 
00140         if (!isset($value->value)) {
00141             return '';
00142         }
00143         return $value->value;
00144     }
00145 
00146     public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
00147         $values = feedback_get_group_values($item, $groupid, $courseid);
00148         if ($values) {
00149             echo '<tr><th colspan="2" align="left">';
00150             echo $itemnr.'&nbsp;('.$item->label.') '.$item->name;
00151             echo '</th></tr>';
00152             foreach ($values as $value) {
00153                 echo '<tr><td colspan="2" valign="top" align="left">';
00154                 echo '-&nbsp;&nbsp;'.str_replace("\n", '<br />', $value->value);
00155                 echo '</td></tr>';
00156             }
00157         }
00158     }
00159 
00160     public function excelprint_item(&$worksheet, $row_offset,
00161                              $xls_formats, $item,
00162                              $groupid, $courseid = false) {
00163 
00164         $analysed_item = $this->get_analysed($item, $groupid, $courseid);
00165 
00166         $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
00167         $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
00168         $data = $analysed_item->data;
00169         if (is_array($data)) {
00170             $worksheet->write_string($row_offset, 2, $data[0], $xls_formats->value_bold);
00171             $row_offset++;
00172             $sizeofdata = count($data);
00173             for ($i = 1; $i < $sizeofdata; $i++) {
00174                 $worksheet->write_string($row_offset, 2, $data[$i], $xls_formats->default);
00175                 $row_offset++;
00176             }
00177         }
00178         $row_offset++;
00179         return $row_offset;
00180     }
00181 
00189     public function print_item_preview($item) {
00190         global $OUTPUT, $DB;
00191         $align = right_to_left() ? 'right' : 'left';
00192         $str_required_mark = '<span class="feedback_required_mark">*</span>';
00193 
00194         $presentation = explode ("|", $item->presentation);
00195         $requiredmark =  ($item->required == 1) ? $str_required_mark : '';
00196         //print the question and label
00197         echo '<div class="feedback_item_label_'.$align.'">';
00198         echo '('.$item->label.') ';
00199         echo format_text($item->name.$requiredmark, true, false, false);
00200         if ($item->dependitem) {
00201             if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
00202                 echo ' <span class="feedback_depend">';
00203                 echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
00204                 echo '</span>';
00205             }
00206         }
00207         echo '</div>';
00208 
00209         //print the presentation
00210         echo '<div class="feedback_item_presentation_'.$align.'">';
00211         echo '<span class="feedback_item_textfield">';
00212         echo '<input type="text" '.
00213                     'name="'.$item->typ.'_'.$item->id.'" '.
00214                     'size="'.$presentation[0].'" '.
00215                     'maxlength="'.$presentation[1].'" '.
00216                     'value="" />';
00217         echo '</span>';
00218         echo '</div>';
00219     }
00220 
00230     public function print_item_complete($item, $value = '', $highlightrequire = false) {
00231         global $OUTPUT;
00232         $align = right_to_left() ? 'right' : 'left';
00233         $str_required_mark = '<span class="feedback_required_mark">*</span>';
00234 
00235         $presentation = explode ("|", $item->presentation);
00236         if ($highlightrequire AND $item->required AND strval($value) == '') {
00237             $highlight = ' missingrequire';
00238         } else {
00239             $highlight = '';
00240         }
00241         $requiredmark =  ($item->required == 1) ? $str_required_mark : '';
00242 
00243         //print the question and label
00244         echo '<div class="feedback_item_label_'.$align.$highlight.'">';
00245             echo format_text($item->name.$requiredmark, true, false, false);
00246         echo '</div>';
00247 
00248         //print the presentation
00249         echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
00250         echo '<span class="feedback_item_textfield">';
00251         echo '<input type="text" '.
00252                     'name="'.$item->typ.'_'.$item->id.'" '.
00253                     'size="'.$presentation[0].'" '.
00254                     'maxlength="'.$presentation[1].'" '.
00255                     'value="'.($value ? htmlspecialchars($value) : '').'" />';
00256         echo '</span>';
00257         echo '</div>';
00258     }
00259 
00268     public function print_item_show_value($item, $value = '') {
00269         global $OUTPUT;
00270         $align = right_to_left() ? 'right' : 'left';
00271         $str_required_mark = '<span class="feedback_required_mark">*</span>';
00272 
00273         $presentation = explode ("|", $item->presentation);
00274         $requiredmark =  ($item->required == 1) ? $str_required_mark : '';
00275 
00276         //print the question and label
00277         echo '<div class="feedback_item_label_'.$align.'">';
00278             echo '('.$item->label.') ';
00279             echo format_text($item->name . $requiredmark, true, false, false);
00280         echo '</div>';
00281         echo $OUTPUT->box_start('generalbox boxalign'.$align);
00282         echo $value ? $value : '&nbsp;';
00283         echo $OUTPUT->box_end();
00284     }
00285 
00286     public function check_value($value, $item) {
00287         //if the item is not required, so the check is true if no value is given
00288         if ((!isset($value) OR $value == '') AND $item->required != 1) {
00289             return true;
00290         }
00291         if ($value == "") {
00292             return false;
00293         }
00294         return true;
00295     }
00296 
00297     public function create_value($data) {
00298         $data = clean_text($data);
00299         return $data;
00300     }
00301 
00302     //compares the dbvalue with the dependvalue
00303     //dbvalue is the value put in by the user
00304     //dependvalue is the value that is compared
00305     public function compare_value($item, $dbvalue, $dependvalue) {
00306         if ($dbvalue == $dependvalue) {
00307             return true;
00308         }
00309         return false;
00310     }
00311 
00312     public function get_presentation($data) {
00313         return $data->itemsize . '|'. $data->itemmaxlength;
00314     }
00315 
00316     public function get_hasvalue() {
00317         return 1;
00318     }
00319 
00320     public function can_switch_require() {
00321         return true;
00322     }
00323 }
 All Data Structures Namespaces Files Functions Variables Enumerations