[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/feedback/item/textfield/ -> lib.php (source)

   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_textfield extends feedback_item_base {
  21      protected $type = "textfield";
  22      private $commonparams;
  23      private $item_form;
  24      private $item;
  25  
  26      public function init() {
  27  
  28      }
  29  
  30      public function build_editform($item, $feedback, $cm) {
  31          global $DB, $CFG;
  32          require_once ('textfield_form.php');
  33  
  34          //get the lastposition number of the feedback_items
  35          $position = $item->position;
  36          $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
  37          if ($position == -1) {
  38              $i_formselect_last = $lastposition + 1;
  39              $i_formselect_value = $lastposition + 1;
  40              $item->position = $lastposition + 1;
  41          } else {
  42              $i_formselect_last = $lastposition;
  43              $i_formselect_value = $item->position;
  44          }
  45          //the elements for position dropdownlist
  46          $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
  47  
  48          $item->presentation = empty($item->presentation) ? '' : $item->presentation;
  49  
  50          $size_and_length = explode('|', $item->presentation);
  51  
  52          if (isset($size_and_length[0]) AND $size_and_length[0] >= 5) {
  53              $itemsize = $size_and_length[0];
  54          } else {
  55              $itemsize = 30;
  56          }
  57  
  58          $itemlength = isset($size_and_length[1]) ? $size_and_length[1] : 5;
  59  
  60          $item->itemsize = $itemsize;
  61          $item->itemmaxlength = $itemlength;
  62  
  63          //all items for dependitem
  64          $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
  65          $commonparams = array('cmid' => $cm->id,
  66                               'id' => isset($item->id) ? $item->id : null,
  67                               'typ' => $item->typ,
  68                               'items' => $feedbackitems,
  69                               'feedback' => $feedback->id);
  70  
  71          //build the form
  72          $customdata = array('item' => $item,
  73                              'common' => $commonparams,
  74                              'positionlist' => $positionlist,
  75                              'position' => $position);
  76  
  77          $this->item_form = new feedback_textfield_form('edit_item.php', $customdata);
  78      }
  79  
  80      //this function only can used after the call of build_editform()
  81      public function show_editform() {
  82          $this->item_form->display();
  83      }
  84  
  85      public function is_cancelled() {
  86          return $this->item_form->is_cancelled();
  87      }
  88  
  89      public function get_data() {
  90          if ($this->item = $this->item_form->get_data()) {
  91              return true;
  92          }
  93          return false;
  94      }
  95  
  96      public function save_item() {
  97          global $DB;
  98  
  99          if (!$item = $this->item_form->get_data()) {
 100              return false;
 101          }
 102  
 103          if (isset($item->clone_item) AND $item->clone_item) {
 104              $item->id = ''; //to clone this item
 105              $item->position++;
 106          }
 107  
 108          $item->hasvalue = $this->get_hasvalue();
 109          if (!$item->id) {
 110              $item->id = $DB->insert_record('feedback_item', $item);
 111          } else {
 112              $DB->update_record('feedback_item', $item);
 113          }
 114  
 115          return $DB->get_record('feedback_item', array('id'=>$item->id));
 116      }
 117  
 118  
 119      //liefert eine Struktur ->name, ->data = array(mit Antworten)
 120      public function get_analysed($item, $groupid = false, $courseid = false) {
 121          global $DB;
 122  
 123          $analysed_val = new stdClass();
 124          $analysed_val->data = null;
 125          $analysed_val->name = $item->name;
 126  
 127          $values = feedback_get_group_values($item, $groupid, $courseid);
 128          if ($values) {
 129              $data = array();
 130              foreach ($values as $value) {
 131                  $data[] = str_replace("\n", '<br />', $value->value);
 132              }
 133              $analysed_val->data = $data;
 134          }
 135          return $analysed_val;
 136      }
 137  
 138      public function get_printval($item, $value) {
 139  
 140          if (!isset($value->value)) {
 141              return '';
 142          }
 143          return $value->value;
 144      }
 145  
 146      public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
 147          $values = feedback_get_group_values($item, $groupid, $courseid);
 148          if ($values) {
 149              echo '<tr><th colspan="2" align="left">';
 150              echo $itemnr.'&nbsp;('.$item->label.') '.$item->name;
 151              echo '</th></tr>';
 152              foreach ($values as $value) {
 153                  echo '<tr><td colspan="2" valign="top" align="left">';
 154                  echo '-&nbsp;&nbsp;'.str_replace("\n", '<br />', $value->value);
 155                  echo '</td></tr>';
 156              }
 157          }
 158      }
 159  
 160      public function excelprint_item(&$worksheet, $row_offset,
 161                               $xls_formats, $item,
 162                               $groupid, $courseid = false) {
 163  
 164          $analysed_item = $this->get_analysed($item, $groupid, $courseid);
 165  
 166          $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
 167          $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
 168          $data = $analysed_item->data;
 169          if (is_array($data)) {
 170              $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);
 171              $row_offset++;
 172              $sizeofdata = count($data);
 173              for ($i = 1; $i < $sizeofdata; $i++) {
 174                  $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[$i], ENT_QUOTES), $xls_formats->default);
 175                  $row_offset++;
 176              }
 177          }
 178          $row_offset++;
 179          return $row_offset;
 180      }
 181  
 182      /**
 183       * print the item at the edit-page of feedback
 184       *
 185       * @global object
 186       * @param object $item
 187       * @return void
 188       */
 189      public function print_item_preview($item) {
 190          global $OUTPUT, $DB;
 191          $align = right_to_left() ? 'right' : 'left';
 192          $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
 193              get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
 194  
 195          $presentation = explode ("|", $item->presentation);
 196          $requiredmark = ($item->required == 1) ? $strrequiredmark : '';
 197          //print the question and label
 198          $inputname = $item->typ . '_' . $item->id;
 199          echo '<div class="feedback_item_label_'.$align.'">';
 200          echo '<label for="'. $inputname .'">';
 201          echo '('.$item->label.') ';
 202          echo format_text($item->name.$requiredmark, true, false, false);
 203          if ($item->dependitem) {
 204              if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
 205                  echo ' <span class="feedback_depend">';
 206                  echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
 207                  echo '</span>';
 208              }
 209          }
 210          echo '</label>';
 211          echo '</div>';
 212  
 213          //print the presentation
 214          echo '<div class="feedback_item_presentation_'.$align.'">';
 215          echo '<span class="feedback_item_textfield">';
 216          echo '<input type="text" '.
 217                      'id="'.$inputname.'" '.
 218                      'name="'.$inputname.'" '.
 219                      'size="'.$presentation[0].'" '.
 220                      'maxlength="'.$presentation[1].'" '.
 221                      'value="" />';
 222          echo '</span>';
 223          echo '</div>';
 224      }
 225  
 226      /**
 227       * print the item at the complete-page of feedback
 228       *
 229       * @global object
 230       * @param object $item
 231       * @param string $value
 232       * @param bool $highlightrequire
 233       * @return void
 234       */
 235      public function print_item_complete($item, $value = '', $highlightrequire = false) {
 236          global $OUTPUT;
 237          $align = right_to_left() ? 'right' : 'left';
 238          $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
 239              get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
 240  
 241          $presentation = explode ("|", $item->presentation);
 242          $requiredmark = ($item->required == 1) ? $strrequiredmark : '';
 243  
 244          //print the question and label
 245          $inputname = $item->typ . '_' . $item->id;
 246          echo '<div class="feedback_item_label_'.$align.'">';
 247          echo '<label for="'. $inputname .'">';
 248              echo format_text($item->name.$requiredmark, true, false, false);
 249          if ($highlightrequire AND $item->required AND strval($value) == '') {
 250              echo '<br class="error"><span id="id_error_'.$inputname.'" class="error"> '.get_string('err_required', 'form').
 251                  '</span><br id="id_error_break_'.$inputname.'" class="error" >';
 252          }
 253          echo '</label>';
 254          echo '</div>';
 255  
 256          //print the presentation
 257          echo '<div class="feedback_item_presentation_'.$align.'">';
 258          echo '<span class="feedback_item_textfield">';
 259          echo '<input type="text" '.
 260                      'id="'.$inputname.'" '.
 261                      'name="'.$inputname.'" '.
 262                      'size="'.$presentation[0].'" '.
 263                      'maxlength="'.$presentation[1].'" '.
 264                      'value="'.$value.'" />';
 265          echo '</span>';
 266          echo '</div>';
 267      }
 268  
 269      /**
 270       * print the item at the complete-page of feedback
 271       *
 272       * @global object
 273       * @param object $item
 274       * @param string $value
 275       * @return void
 276       */
 277      public function print_item_show_value($item, $value = '') {
 278          global $OUTPUT;
 279          $align = right_to_left() ? 'right' : 'left';
 280          $strrequiredmark = '<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
 281              get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />';
 282  
 283          $presentation = explode ("|", $item->presentation);
 284          $requiredmark = ($item->required == 1) ? $strrequiredmark : '';
 285  
 286          //print the question and label
 287          echo '<div class="feedback_item_label_'.$align.'">';
 288              echo '('.$item->label.') ';
 289              echo format_text($item->name . $requiredmark, true, false, false);
 290          echo '</div>';
 291          echo $OUTPUT->box_start('generalbox boxalign'.$align);
 292          echo $value ? $value : '&nbsp;';
 293          echo $OUTPUT->box_end();
 294      }
 295  
 296      public function check_value($value, $item) {
 297          //if the item is not required, so the check is true if no value is given
 298          if ((!isset($value) OR $value == '') AND $item->required != 1) {
 299              return true;
 300          }
 301          if ($value == "") {
 302              return false;
 303          }
 304          return true;
 305      }
 306  
 307      public function create_value($data) {
 308          $data = s($data);
 309          return $data;
 310      }
 311  
 312      //compares the dbvalue with the dependvalue
 313      //dbvalue is the value put in by the user
 314      //dependvalue is the value that is compared
 315      public function compare_value($item, $dbvalue, $dependvalue) {
 316          if ($dbvalue == $dependvalue) {
 317              return true;
 318          }
 319          return false;
 320      }
 321  
 322      public function get_presentation($data) {
 323          return $data->itemsize . '|'. $data->itemmaxlength;
 324      }
 325  
 326      public function get_hasvalue() {
 327          return 1;
 328      }
 329  
 330      public function can_switch_require() {
 331          return true;
 332      }
 333  
 334      public function value_type() {
 335          return PARAM_RAW;
 336      }
 337  
 338      public function clean_input_value($value) {
 339          return s($value);
 340      }
 341  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1