Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 39 and 402]

   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 (!$this->get_data()) {
  77              return false;
  78          }
  79          $item = $this->item;
  80  
  81          if (isset($item->clone_item) AND $item->clone_item) {
  82              $item->id = ''; //to clone this item
  83              $item->position++;
  84          }
  85  
  86          $item->hasvalue = $this->get_hasvalue();
  87          if (!$item->id) {
  88              $item->id = $DB->insert_record('feedback_item', $item);
  89          } else {
  90              $DB->update_record('feedback_item', $item);
  91          }
  92  
  93          return $DB->get_record('feedback_item', array('id'=>$item->id));
  94      }
  95  
  96  
  97      /**
  98       * Helper function for collected data for exporting to excel
  99       *
 100       * @param stdClass $item the db-object from feedback_item
 101       * @param int $groupid
 102       * @param int $courseid
 103       * @return stdClass
 104       */
 105      protected function get_analysed($item, $groupid = false, $courseid = false) {
 106  
 107          $analysed_val = new stdClass();
 108          $analysed_val->data = null;
 109          $analysed_val->name = $item->name;
 110  
 111          $values = feedback_get_group_values($item, $groupid, $courseid);
 112          if ($values) {
 113              $data = array();
 114              foreach ($values as $value) {
 115                  $data[] = str_replace("\n", '<br />', $value->value);
 116              }
 117              $analysed_val->data = $data;
 118          }
 119          return $analysed_val;
 120      }
 121  
 122      public function get_printval($item, $value) {
 123  
 124          if (!isset($value->value)) {
 125              return '';
 126          }
 127          return $value->value;
 128      }
 129  
 130      public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
 131          $values = feedback_get_group_values($item, $groupid, $courseid);
 132          if ($values) {
 133              echo "<table class=\"analysis itemtype_{$item->typ}\">";
 134              echo '<tr><th class="text-left">';
 135              echo $itemnr . ' ';
 136              if (strval($item->label) !== '') {
 137                  echo '('. format_string($item->label).') ';
 138              }
 139              echo $this->get_display_name($item);
 140              echo '</th></tr>';
 141              foreach ($values as $value) {
 142                  $class = strlen(trim($value->value)) ? '' : ' class="isempty"';
 143                  echo '<tr'.$class.'><td class="singlevalue">';
 144                  echo str_replace("\n", '<br />', $value->value);
 145                  echo '</td></tr>';
 146              }
 147              echo '</table>';
 148          }
 149      }
 150  
 151      public function excelprint_item(&$worksheet, $row_offset,
 152                               $xls_formats, $item,
 153                               $groupid, $courseid = false) {
 154  
 155          $analysed_item = $this->get_analysed($item, $groupid, $courseid);
 156  
 157          $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
 158          $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
 159          $data = $analysed_item->data;
 160          if (is_array($data)) {
 161              $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);
 162              $row_offset++;
 163              $sizeofdata = count($data);
 164              for ($i = 1; $i < $sizeofdata; $i++) {
 165                  $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[$i], ENT_QUOTES), $xls_formats->default);
 166                  $row_offset++;
 167              }
 168          }
 169          $row_offset++;
 170          return $row_offset;
 171      }
 172  
 173      /**
 174       * Adds an input element to the complete form
 175       *
 176       * @param stdClass $item
 177       * @param mod_feedback_complete_form $form
 178       */
 179      public function complete_form_element($item, $form) {
 180          $name = $this->get_display_name($item);
 181          $inputname = $item->typ . '_' . $item->id;
 182          list($size, $maxlength) = explode ("|", $item->presentation);
 183          $form->add_form_element($item,
 184                  ['text', $inputname, $name, ['maxlength' => $maxlength, 'size' => $size]]);
 185          $form->set_element_type($inputname, PARAM_NOTAGS);
 186  
 187          $form->add_element_rule($inputname, get_string('maximumchars', '', $maxlength), 'maxlength', $maxlength, 'client');
 188      }
 189  
 190      /**
 191       * Converts the value from complete_form data to the string value that is stored in the db.
 192       * @param mixed $value element from mod_feedback_complete_form::get_data() with the name $item->typ.'_'.$item->id
 193       * @return string
 194       */
 195      public function create_value($value) {
 196          return s($value);
 197      }
 198  
 199      /**
 200       * Return the analysis data ready for external functions.
 201       *
 202       * @param stdClass $item     the item (question) information
 203       * @param int      $groupid  the group id to filter data (optional)
 204       * @param int      $courseid the course id (optional)
 205       * @return array an array of data with non scalar types json encoded
 206       * @since  Moodle 3.3
 207       */
 208      public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
 209  
 210          $externaldata = array();
 211          $data = $this->get_analysed($item, $groupid, $courseid);
 212  
 213          if (is_array($data->data)) {
 214              return $data->data; // No need to json, scalar type.
 215          }
 216          return $externaldata;
 217      }
 218  }