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_textarea extends feedback_item_base {
  21      protected $type = "textarea";
  22  
  23      public function build_editform($item, $feedback, $cm) {
  24          global $DB, $CFG;
  25          require_once ('textarea_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          $width_and_height = explode('|', $item->presentation);
  44  
  45          if (isset($width_and_height[0]) AND $width_and_height[0] >= 5) {
  46              $itemwidth = $width_and_height[0];
  47          } else {
  48              $itemwidth = 30;
  49          }
  50  
  51          if (isset($width_and_height[1])) {
  52              $itemheight = $width_and_height[1];
  53          } else {
  54              $itemheight = 5;
  55          }
  56          $item->itemwidth = $itemwidth;
  57          $item->itemheight = $itemheight;
  58  
  59          //all items for dependitem
  60          $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
  61          $commonparams = array('cmid'=>$cm->id,
  62                               'id'=>isset($item->id) ? $item->id : null,
  63                               'typ'=>$item->typ,
  64                               'items'=>$feedbackitems,
  65                               'feedback'=>$feedback->id);
  66  
  67          //build the form
  68          $customdata = array('item' => $item,
  69                              'common' => $commonparams,
  70                              'positionlist' => $positionlist,
  71                              'position' => $position);
  72  
  73          $this->item_form = new feedback_textarea_form('edit_item.php', $customdata);
  74      }
  75  
  76      public function save_item() {
  77          global $DB;
  78  
  79          if (!$this->get_data()) {
  80              return false;
  81          }
  82          $item = $this->item;
  83  
  84          if (isset($item->clone_item) AND $item->clone_item) {
  85              $item->id = ''; //to clone this item
  86              $item->position++;
  87          }
  88  
  89          $item->hasvalue = $this->get_hasvalue();
  90          if (!$item->id) {
  91              $item->id = $DB->insert_record('feedback_item', $item);
  92          } else {
  93              $DB->update_record('feedback_item', $item);
  94          }
  95  
  96          return $DB->get_record('feedback_item', array('id'=>$item->id));
  97      }
  98  
  99      /**
 100       * Helper function for collected data for exporting to excel
 101       *
 102       * @param stdClass $item the db-object from feedback_item
 103       * @param int $groupid
 104       * @param int $courseid
 105       * @return stdClass
 106       */
 107      protected function get_analysed($item, $groupid = false, $courseid = false) {
 108          global $DB;
 109  
 110          $analysed_val = new stdClass();
 111          $analysed_val->data = array();
 112          $analysed_val->name = $item->name;
 113  
 114          $values = feedback_get_group_values($item, $groupid, $courseid);
 115          if ($values) {
 116              $data = array();
 117              foreach ($values as $value) {
 118                  $data[] = str_replace("\n", '<br />', $value->value);
 119              }
 120              $analysed_val->data = $data;
 121          }
 122          return $analysed_val;
 123      }
 124  
 125      public function get_printval($item, $value) {
 126  
 127          if (!isset($value->value)) {
 128              return '';
 129          }
 130  
 131          return $value->value;
 132      }
 133  
 134      public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
 135          $values = feedback_get_group_values($item, $groupid, $courseid);
 136          if ($values) {
 137              echo "<table class=\"analysis itemtype_{$item->typ}\">";
 138              echo '<tr><th class="text-left">';
 139              echo $itemnr . ' ';
 140              if (strval($item->label) !== '') {
 141                  echo '('. format_string($item->label).') ';
 142              }
 143              echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false));
 144              echo '</th></tr>';
 145              foreach ($values as $value) {
 146                  $class = strlen(trim($value->value)) ? '' : ' class="isempty"';
 147                  echo '<tr'.$class.'>';
 148                  echo '<td class="singlevalue">';
 149                  echo str_replace("\n", '<br />', $value->value);
 150                  echo '</td>';
 151                  echo '</tr>';
 152              }
 153              echo '</table>';
 154          }
 155      }
 156  
 157      public function excelprint_item(&$worksheet, $row_offset,
 158                               $xls_formats, $item,
 159                               $groupid, $courseid = false) {
 160  
 161          $analysed_item = $this->get_analysed($item, $groupid, $courseid);
 162  
 163          $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
 164          $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
 165          $data = $analysed_item->data;
 166          if (is_array($data)) {
 167              if (isset($data[0])) {
 168                  $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);
 169              }
 170              $row_offset++;
 171              $sizeofdata = count($data);
 172              for ($i = 1; $i < $sizeofdata; $i++) {
 173                  $worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[$i], ENT_QUOTES), $xls_formats->default);
 174                  $row_offset++;
 175              }
 176          }
 177          $row_offset++;
 178          return $row_offset;
 179      }
 180  
 181      /**
 182       * Adds an input element to the complete form
 183       *
 184       * @param stdClass $item
 185       * @param mod_feedback_complete_form $form
 186       */
 187      public function complete_form_element($item, $form) {
 188          $name = $this->get_display_name($item);
 189          $inputname = $item->typ . '_' . $item->id;
 190          list($cols, $rows) = explode ("|", $item->presentation);
 191          $form->add_form_element($item,
 192              ['textarea', $inputname, $name, array('rows' => $rows, 'cols' => $cols)]);
 193          $form->set_element_type($inputname, PARAM_NOTAGS);
 194      }
 195  
 196      public function create_value($data) {
 197          return s($data);
 198      }
 199  
 200      /**
 201       * Return the analysis data ready for external functions.
 202       *
 203       * @param stdClass $item     the item (question) information
 204       * @param int      $groupid  the group id to filter data (optional)
 205       * @param int      $courseid the course id (optional)
 206       * @return array an array of data with non scalar types json encoded
 207       * @since  Moodle 3.3
 208       */
 209      public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
 210  
 211          $externaldata = array();
 212          $data = $this->get_analysed($item, $groupid, $courseid);
 213  
 214          if (is_array($data->data)) {
 215              return $data->data; // No need to json, scalar type.
 216          }
 217          return $externaldata;
 218      }
 219  }