Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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  require_once($CFG->libdir.'/formslib.php');
  20  
  21  class feedback_item_label extends feedback_item_base {
  22      protected $type = "label";
  23      private $presentationoptions = null;
  24      private $context;
  25  
  26      /**
  27       * Constructor
  28       */
  29      public function __construct() {
  30          $this->presentationoptions = array('maxfiles' => EDITOR_UNLIMITED_FILES,
  31                                             'trusttext'=>true);
  32  
  33      }
  34  
  35      public function build_editform($item, $feedback, $cm) {
  36          global $DB, $CFG;
  37          require_once ('label_form.php');
  38  
  39          //get the lastposition number of the feedback_items
  40          $position = $item->position;
  41          $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
  42          if ($position == -1) {
  43              $i_formselect_last = $lastposition + 1;
  44              $i_formselect_value = $lastposition + 1;
  45              $item->position = $lastposition + 1;
  46          } else {
  47              $i_formselect_last = $lastposition;
  48              $i_formselect_value = $item->position;
  49          }
  50          //the elements for position dropdownlist
  51          $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
  52  
  53          //all items for dependitem
  54          $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
  55          $commonparams = array('cmid'=>$cm->id,
  56                               'id'=>isset($item->id) ? $item->id : null,
  57                               'typ'=>$item->typ,
  58                               'items'=>$feedbackitems,
  59                               'feedback'=>$feedback->id);
  60  
  61          $this->context = context_module::instance($cm->id);
  62  
  63          //preparing the editor for new file-api
  64          $item->presentationformat = FORMAT_HTML;
  65          $item->presentationtrust = 1;
  66  
  67          // Append editor context to presentation options, giving preference to existing context.
  68          $this->presentationoptions = array_merge(array('context' => $this->context),
  69                                                   $this->presentationoptions);
  70  
  71          $item = file_prepare_standard_editor($item,
  72                                              'presentation', //name of the form element
  73                                              $this->presentationoptions,
  74                                              $this->context,
  75                                              'mod_feedback',
  76                                              'item', //the filearea
  77                                              $item->id);
  78  
  79          //build the form
  80          $customdata = array('item' => $item,
  81                              'common' => $commonparams,
  82                              'positionlist' => $positionlist,
  83                              'position' => $position,
  84                              'presentationoptions' => $this->presentationoptions);
  85  
  86          $this->item_form = new feedback_label_form('edit_item.php', $customdata);
  87      }
  88  
  89      public function save_item() {
  90          global $DB;
  91  
  92          if (!$this->get_data()) {
  93              return false;
  94          }
  95          $item = $this->item;
  96  
  97          if (isset($item->clone_item) AND $item->clone_item) {
  98              $item->id = ''; //to clone this item
  99              $item->position++;
 100          }
 101  
 102          $item->presentation = '';
 103  
 104          $item->hasvalue = $this->get_hasvalue();
 105          if (!$item->id) {
 106              $item->id = $DB->insert_record('feedback_item', $item);
 107          } else {
 108              $DB->update_record('feedback_item', $item);
 109          }
 110  
 111          $item = file_postupdate_standard_editor($item,
 112                                                  'presentation',
 113                                                  $this->presentationoptions,
 114                                                  $this->context,
 115                                                  'mod_feedback',
 116                                                  'item',
 117                                                  $item->id);
 118  
 119          $DB->update_record('feedback_item', $item);
 120  
 121          return $DB->get_record('feedback_item', array('id'=>$item->id));
 122      }
 123  
 124      /**
 125       * prepares the item for output or export to file
 126       * @param stdClass $item
 127       * @return string
 128       */
 129      private function print_item($item) {
 130          global $DB, $CFG;
 131  
 132          require_once($CFG->libdir . '/filelib.php');
 133  
 134          //is the item a template?
 135          if (!$item->feedback AND $item->template) {
 136              $template = $DB->get_record('feedback_template', array('id'=>$item->template));
 137              if ($template->ispublic) {
 138                  $context = context_system::instance();
 139              } else {
 140                  $context = context_course::instance($template->course);
 141              }
 142              $filearea = 'template';
 143          } else {
 144              $cm = get_coursemodule_from_instance('feedback', $item->feedback);
 145              $context = context_module::instance($cm->id);
 146              $filearea = 'item';
 147          }
 148  
 149          $item->presentationformat = FORMAT_HTML;
 150          $item->presentationtrust = 1;
 151  
 152          $output = file_rewrite_pluginfile_urls($item->presentation,
 153                                                 'pluginfile.php',
 154                                                 $context->id,
 155                                                 'mod_feedback',
 156                                                 $filearea,
 157                                                 $item->id);
 158  
 159          $formatoptions = array('overflowdiv'=>true, 'trusted'=>$CFG->enabletrusttext);
 160          echo format_text($output, FORMAT_HTML, $formatoptions);
 161      }
 162  
 163      /**
 164       * @param stdClass $item
 165       * @param bool|true $withpostfix
 166       * @return string
 167       */
 168      public function get_display_name($item, $withpostfix = true) {
 169          return '';
 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          global $DB;
 180          if (!$item->feedback AND $item->template) {
 181              // This is a template.
 182              $template = $DB->get_record('feedback_template', array('id' => $item->template));
 183              if ($template->ispublic) {
 184                  $context = context_system::instance();
 185              } else {
 186                  $context = context_course::instance($template->course);
 187              }
 188              $filearea = 'template';
 189          } else {
 190              // This is a question in the current feedback.
 191              $context = $form->get_cm()->context;
 192              $filearea = 'item';
 193          }
 194          $output = file_rewrite_pluginfile_urls($item->presentation, 'pluginfile.php',
 195                  $context->id, 'mod_feedback', $filearea, $item->id);
 196          $formatoptions = array('overflowdiv' => true, 'noclean' => true);
 197          $output = format_text($output, FORMAT_HTML, $formatoptions);
 198          $output = html_writer::div($output, '', ['id' => 'feedback_item_' . $item->id]);
 199  
 200          $inputname = $item->typ . '_' . $item->id;
 201  
 202          $name = $this->get_display_name($item);
 203          $form->add_form_element($item, ['static', $inputname, $name, $output], false, false);
 204      }
 205  
 206      public function compare_value($item, $dbvalue, $dependvalue) {
 207          return false;
 208      }
 209  
 210      public function postupdate($item) {
 211          global $DB;
 212  
 213          $context = context_module::instance($item->cmid);
 214          $item = file_postupdate_standard_editor($item,
 215                                                  'presentation',
 216                                                  $this->presentationoptions,
 217                                                  $context,
 218                                                  'mod_feedback',
 219                                                  'item',
 220                                                  $item->id);
 221  
 222          $DB->update_record('feedback_item', $item);
 223          return $item->id;
 224      }
 225  
 226      public function get_hasvalue() {
 227          return 0;
 228      }
 229  
 230      public function can_switch_require() {
 231          return false;
 232      }
 233  
 234      public function excelprint_item(&$worksheet,
 235                               $row_offset,
 236                               $xls_formats,
 237                               $item,
 238                               $groupid,
 239                               $courseid = false) {
 240      }
 241  
 242      public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
 243      }
 244      public function get_printval($item, $value) {
 245      }
 246  
 247      /**
 248       * Return the analysis data ready for external functions.
 249       *
 250       * @param stdClass $item     the item (question) information
 251       * @param int      $groupid  the group id to filter data (optional)
 252       * @param int      $courseid the course id (optional)
 253       * @return array an array of data with non scalar types json encoded
 254       * @since  Moodle 3.3
 255       */
 256      public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
 257          return [];
 258      }
 259  }