Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/feedback/item/info/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_info extends feedback_item_base {
00021     protected $type = "info";
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('info_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) ? 1 : $item->presentation;
00049         $item->required = 0;
00050 
00051         //all items for dependitem
00052         $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
00053         $commonparams = array('cmid'=>$cm->id,
00054                              'id'=>isset($item->id) ? $item->id : null,
00055                              'typ'=>$item->typ,
00056                              'items'=>$feedbackitems,
00057                              'feedback'=>$feedback->id);
00058 
00059         //build the form
00060         $this->item_form = new feedback_info_form('edit_item.php',
00061                                                   array('item'=>$item,
00062                                                   'common'=>$commonparams,
00063                                                   'positionlist'=>$positionlist,
00064                                                   'position' => $position));
00065     }
00066 
00067     //this function only can used after the call of build_editform()
00068     public function show_editform() {
00069         $this->item_form->display();
00070     }
00071 
00072     public function is_cancelled() {
00073         return $this->item_form->is_cancelled();
00074     }
00075 
00076     public function get_data() {
00077         if ($this->item = $this->item_form->get_data()) {
00078             return true;
00079         }
00080         return false;
00081     }
00082 
00083     public function save_item() {
00084         global $DB;
00085 
00086         if (!$item = $this->item_form->get_data()) {
00087             return false;
00088         }
00089 
00090         if (isset($item->clone_item) AND $item->clone_item) {
00091             $item->id = ''; //to clone this item
00092             $item->position++;
00093         }
00094 
00095         $item->hasvalue = $this->get_hasvalue();
00096         if (!$item->id) {
00097             $item->id = $DB->insert_record('feedback_item', $item);
00098         } else {
00099             $DB->update_record('feedback_item', $item);
00100         }
00101 
00102         return $DB->get_record('feedback_item', array('id'=>$item->id));
00103     }
00104 
00105     //liefert eine Struktur ->name, ->data = array(mit Antworten)
00106     public function get_analysed($item, $groupid = false, $courseid = false) {
00107 
00108         $presentation = $item->presentation;
00109         $analysed_val = null;
00110         $analysed_val->data = null;
00111         $analysed_val->name = $item->name;
00112         $values = feedback_get_group_values($item, $groupid, $courseid);
00113         if ($values) {
00114             $data = array();
00115             $datavalue = new stdClass();
00116             foreach ($values as $value) {
00117 
00118                 switch($presentation) {
00119                     case 1:
00120                         $datavalue->value = $value->value;
00121                         $datavalue->show = userdate($datavalue->value);
00122                         break;
00123                     case 2:
00124                         $datavalue->value = $value->value;
00125                         $datavalue->show = $datavalue->value;
00126                         break;
00127                     case 3:
00128                         $datavalue->value = $value->value;
00129                         $datavalue->show = $datavalue->value;
00130                         break;
00131                 }
00132 
00133                 $data[] = $datavalue;
00134             }
00135             $analysed_val->data = $data;
00136         }
00137         return $analysed_val;
00138     }
00139 
00140     public function get_printval($item, $value) {
00141 
00142         if (!isset($value->value)) {
00143             return '';
00144         }
00145         return userdate($value->value);
00146     }
00147 
00148     public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
00149         $analysed_item = $this->get_analysed($item, $groupid, $courseid);
00150         $data = $analysed_item->data;
00151         if (is_array($data)) {
00152             echo '<tr><th colspan="2" align="left">';
00153             echo $itemnr.'&nbsp;('.$item->label.') '.$item->name;
00154             echo '</th></tr>';
00155             $sizeofdata = count($data);
00156             for ($i = 0; $i < $sizeofdata; $i++) {
00157                 echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;';
00158                 echo str_replace("\n", '<br />', $data[$i]->show);
00159                 echo '</td></tr>';
00160             }
00161         }
00162     }
00163 
00164     public function excelprint_item(&$worksheet, $row_offset,
00165                              $xls_formats, $item,
00166                              $groupid, $courseid = false) {
00167         $analysed_item = $this->get_analysed($item, $groupid, $courseid);
00168 
00169         $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
00170         $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
00171         $data = $analysed_item->data;
00172         if (is_array($data)) {
00173             $worksheet->write_string($row_offset, 2, $data[0]->show, $xls_formats->value_bold);
00174             $row_offset++;
00175             $sizeofdata = count($data);
00176             for ($i = 1; $i < $sizeofdata; $i++) {
00177                 $worksheet->write_string($row_offset, 2, $data[$i]->show, $xls_formats->default);
00178                 $row_offset++;
00179             }
00180         }
00181         $row_offset++;
00182         return $row_offset;
00183     }
00184 
00192     public function print_item_preview($item) {
00193         global $USER, $DB, $OUTPUT;
00194 
00195         $align = right_to_left() ? 'right' : 'left';
00196         $presentation = $item->presentation;
00197         $requiredmark =  ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
00198 
00199         if ($item->feedback) {
00200             $courseid = $DB->get_field('feedback', 'course', array('id'=>$item->feedback));
00201         } else { // the item must be a template item
00202             $cmid = required_param('id', PARAM_INT);
00203             $courseid = $DB->get_field('course_modules', 'course', array('id'=>$cmid));
00204         }
00205         if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
00206             print_error('error');
00207         }
00208         if ($course->id !== SITEID) {
00209             $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
00210         } else {
00211             $coursecategory = false;
00212         }
00213         switch($presentation) {
00214             case 1:
00215                 $itemvalue = time();
00216                 $itemshowvalue = userdate($itemvalue);
00217                 break;
00218             case 2:
00219                 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00220                 $itemvalue = format_string($course->shortname,
00221                                            true,
00222                                            array('context' => $coursecontext));
00223 
00224                 $itemshowvalue = $itemvalue;
00225                 break;
00226             case 3:
00227                 if ($coursecategory) {
00228                     $category_context = get_context_instance(CONTEXT_COURSECAT, $coursecategory->id);
00229                     $itemvalue = format_string($coursecategory->name,
00230                                                true,
00231                                                array('context' => $category_context));
00232 
00233                     $itemshowvalue = $itemvalue;
00234                 } else {
00235                     $itemvalue = '';
00236                     $itemshowvalue = '';
00237                 }
00238                 break;
00239         }
00240 
00241         //print the question and label
00242         echo '<div class="feedback_item_label_'.$align.'">';
00243         echo '('.$item->label.') ';
00244         echo format_text($item->name.$requiredmark, true, false, false);
00245         if ($item->dependitem) {
00246             if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
00247                 echo ' <span class="feedback_depend">';
00248                 echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
00249                 echo '</span>';
00250             }
00251         }
00252         echo '</div>';
00253         //print the presentation
00254         echo '<div class="feedback_item_presentation_'.$align.'">';
00255         echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
00256         echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
00257         echo '</div>';
00258     }
00259 
00269     public function print_item_complete($item, $value = '', $highlightrequire = false) {
00270         global $USER, $DB, $OUTPUT;
00271         $align = right_to_left() ? 'right' : 'left';
00272 
00273         $presentation = $item->presentation;
00274         if ($highlightrequire AND $item->required AND strval($value) == '') {
00275             $highlight = ' missingrequire';
00276         } else {
00277             $highlight = '';
00278         }
00279         $requiredmark =  ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
00280 
00281         $feedback = $DB->get_record('feedback', array('id'=>$item->feedback));
00282         $course = $DB->get_record('course', array('id'=>$feedback->course));
00283 
00284         if ($course->id !== SITEID) {
00285             $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
00286         } else {
00287             $coursecategory = false;
00288         }
00289 
00290         switch($presentation) {
00291             case 1:
00292                 $itemvalue = time();
00293                 $itemshowvalue = userdate($itemvalue);
00294                 break;
00295             case 2:
00296                 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00297                 $itemvalue = format_string($course->shortname,
00298                                            true,
00299                                            array('context' => $coursecontext));
00300 
00301                 $itemshowvalue = $itemvalue;
00302                 break;
00303             case 3:
00304                 if ($coursecategory) {
00305                     $category_context = get_context_instance(CONTEXT_COURSECAT, $coursecategory->id);
00306                     $itemvalue = format_string($coursecategory->name,
00307                                                true,
00308                                                array('context' => $category_context));
00309 
00310                     $itemshowvalue = $itemvalue;
00311                 } else {
00312                     $itemvalue = '';
00313                     $itemshowvalue = '';
00314                 }
00315                 break;
00316         }
00317 
00318         //print the question and label
00319         echo '<div class="feedback_item_label_'.$align.$highlight.'">';
00320             echo format_text($item->name.$requiredmark, true, false, false);
00321         echo '</div>';
00322 
00323         //print the presentation
00324         echo '<div class="feedback_item_presentation_'.$align.'">';
00325         echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
00326         echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
00327         echo '</div>';
00328     }
00329 
00338     public function print_item_show_value($item, $value = '') {
00339         global $USER, $DB, $OUTPUT;
00340         $align = right_to_left() ? 'right' : 'left';
00341 
00342         $presentation = $item->presentation;
00343         $requiredmark =  ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
00344 
00345         if ($presentation == 1) {
00346             $value = $value ? userdate($value) : '&nbsp;';
00347         }
00348 
00349         //print the question and label
00350         echo '<div class="feedback_item_label_'.$align.'">';
00351             echo '('.$item->label.') ';
00352             echo format_text($item->name . $requiredmark, true, false, false);
00353         echo '</div>';
00354 
00355         //print the presentation
00356         echo $OUTPUT->box_start('generalbox boxalign'.$align);
00357         echo $value;
00358         echo $OUTPUT->box_end();
00359     }
00360 
00361     public function check_value($value, $item) {
00362         return true;
00363     }
00364 
00365     public function create_value($data) {
00366         $data = clean_text($data);
00367         return $data;
00368     }
00369 
00370     //compares the dbvalue with the dependvalue
00371     //the values can be the shortname of a course or the category name
00372     //the date is not compareable :(.
00373     public function compare_value($item, $dbvalue, $dependvalue) {
00374         if ($dbvalue == $dependvalue) {
00375             return true;
00376         }
00377         return false;
00378     }
00379 
00380     public function get_presentation($data) {
00381         return $data->infotype;
00382     }
00383 
00384     public function get_hasvalue() {
00385         return 1;
00386     }
00387 
00388     public function can_switch_require() {
00389         return false;
00390     }
00391 }
 All Data Structures Namespaces Files Functions Variables Enumerations