[ Index ]

PHP Cross Reference of Unnamed Project

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  
  23      public function build_editform($item, $feedback, $cm) {
  24          global $DB, $CFG;
  25          require_once ('textfield_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          $size_and_length = explode('|', $item->presentation);
  44  
  45          if (isset($size_and_length[0]) AND $size_and_length[0] >= 5) {
  46              $itemsize = $size_and_length[0];
  47          } else {
  48              $itemsize = 30;
  49          }
  50  
  51          $itemlength = isset($size_and_length[1]) ? $size_and_length[1] : 255;
  52  
  53          $item->itemsize = $itemsize;
  54          $item->itemmaxlength = $itemlength;
  55  
  56          //all items for dependitem
  57          $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
  58          $commonparams = array('cmid' => $cm->id,
  59                               'id' => isset($item->id) ? $item->id : null,
  60                               'typ' => $item->typ,
  61                               'items' => $feedbackitems,
  62                               'feedback' => $feedback->id);
  63  
  64          //build the form
  65          $customdata = array('item' => $item,
  66                              'common' => $commonparams,
  67                              'positionlist' => $positionlist,
  68                              'position' => $position);
  69  
  70          $this->item_form = new feedback_textfield_form('edit_item.php', $customdata);
  71      }
  72  
  73      public function save_item() {
  74          global $DB;
  75  
  76          if (!$item = $this->item_form->get_data()) {
  77              return false;
  78          }
  79  
  80          if (isset($item->clone_item) AND $item->clone_item) {
  81              $item->id = ''; //to clone this item
  82              $item->position++;
  83          }
  84  
  85          $item->hasvalue = $this->get_hasvalue();
  86          if (!$item->id) {
  87              $item->id = $DB->insert_record('feedback_item', $item);
  88          } else {
  89              $DB->update_record('feedback_item', $item);
  90          }
  91  
  92          return $DB->get_record('feedback_item', array('id'=>$item->id));
  93      }
  94  
  95  
  96      /**
  97       * Helper function for collected data for exporting to excel
  98       *
  99       * @param stdClass $item the db-object from feedback_item
 100       * @param int $groupid
 101       * @param int $courseid
 102       * @return stdClass
 103       */
 104      protected function get_analysed($item, $groupid = false, $courseid = false) {
 105  
 106          $analysed_val = new stdClass();
 107          $analysed_val->data = null;
 108          $analysed_val->name = $item->name;
 109  
 110          $values = feedback_get_group_values($item, $groupid, $courseid);
 111          if ($values) {
 112              $data = array();
 113              foreach ($values as $value) {
 114                  $data[] = str_replace("\n", '<br />', $value->value);
 115              }
 116              $analysed_val->data = $data;
 117          }
 118          return $analysed_val;
 119      }
 120  
 121      public function get_printval($item, $value) {
 122  
 123          if (!isset($value->value)) {
 124              return '';
 125          }
 126          return $value->value;
 127      }
 128  
 129      public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
 130          $values = feedback_get_group_values($item, $groupid, $courseid);
 131          if ($values) {
 132              echo "<table class=\"analysis itemtype_{$item->typ}\">";
 133              echo '<tr><th colspan="2" align="left">';
 134              echo $itemnr . ' ';
 135              if (strval($item->label) !== '') {
 136                  echo '('. format_string($item->label).') ';
 137              }
 138              echo $this->get_display_name($item);
 139              echo '</th></tr>';
 140              foreach ($values as $value) {
 141                  $class = strlen(trim($value->value)) ? '' : ' class="isempty"';
 142                  echo '<tr'.$class.'><td colspan="2" class="singlevalue">';
 143                  echo str_replace("\n", '<br />', $value->value);
 144                  echo '</td></tr>';
 145              }
 146              echo '</table>';
 147          }
 148      }
 149  
 150      public function excelprint_item(&$worksheet, $row_offset,
 151                               $xls_formats, $item,
 152                               $groupid, $courseid = false) {
 153  
 154          $analysed_item = $this->get_analysed($item, $groupid, $courseid);
 155  
 156          $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
 157          $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
 158          $data = $analysed_item->data;
 159          if (is_array($data)) {
 160              $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);
 161              $row_offset++;
 162              $sizeofdata = count($data);
 163              for ($i = 1; $i < $sizeofdata; $i++) {
 164                  $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[$i], ENT_QUOTES), $xls_formats->default);
 165                  $row_offset++;
 166              }
 167          }
 168          $row_offset++;
 169          return $row_offset;
 170      }
 171  
 172      /**
 173       * Adds an input element to the complete form
 174       *
 175       * @param stdClass $item
 176       * @param mod_feedback_complete_form $form
 177       */
 178      public function complete_form_element($item, $form) {
 179          $name = $this->get_display_name($item);
 180          $inputname = $item->typ . '_' . $item->id;
 181          list($size, $maxlength) = explode ("|", $item->presentation);
 182          $form->add_form_element($item,
 183                  ['text', $inputname, $name, ['maxlength' => $maxlength, 'size' => $size]]);
 184          $form->set_element_type($inputname, PARAM_NOTAGS);
 185  
 186          $form->add_element_rule($inputname, get_string('maximumchars', '', $maxlength), 'maxlength', $maxlength, 'client');
 187      }
 188  
 189      /**
 190       * Converts the value from complete_form data to the string value that is stored in the db.
 191       * @param mixed $value element from mod_feedback_complete_form::get_data() with the name $item->typ.'_'.$item->id
 192       * @return string
 193       */
 194      public function create_value($value) {
 195          return s($value);
 196      }
 197  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1