[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/feedback/item/info/ -> 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_info extends feedback_item_base {
  21      protected $type = "info";
  22  
  23      /** Mode recording response time (for non-anonymous feedbacks only) */
  24      const MODE_RESPONSETIME = 1;
  25      /** Mode recording current course */
  26      const MODE_COURSE = 2;
  27      /** Mode recording current course category */
  28      const MODE_CATEGORY = 3;
  29  
  30      /** Special constant to keep the current timestamp as value for the form element */
  31      const CURRENTTIMESTAMP = '__CURRENT__TIMESTAMP__';
  32  
  33      public function build_editform($item, $feedback, $cm) {
  34          global $DB, $CFG;
  35          require_once ('info_form.php');
  36  
  37          //get the lastposition number of the feedback_items
  38          $position = $item->position;
  39          $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
  40          if ($position == -1) {
  41              $i_formselect_last = $lastposition + 1;
  42              $i_formselect_value = $lastposition + 1;
  43              $item->position = $lastposition + 1;
  44          } else {
  45              $i_formselect_last = $lastposition;
  46              $i_formselect_value = $item->position;
  47          }
  48          //the elements for position dropdownlist
  49          $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
  50  
  51          $item->presentation = empty($item->presentation) ? self::MODE_COURSE : $item->presentation;
  52          $item->required = 0;
  53  
  54          //all items for dependitem
  55          $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
  56          $commonparams = array('cmid'=>$cm->id,
  57                               'id'=>isset($item->id) ? $item->id : null,
  58                               'typ'=>$item->typ,
  59                               'items'=>$feedbackitems,
  60                               'feedback'=>$feedback->id);
  61  
  62          // Options for the 'presentation' select element.
  63          $presentationoptions = array();
  64          if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO || $item->presentation == self::MODE_RESPONSETIME) {
  65              // "Response time" is hidden anyway in case of anonymous feedback, no reason to offer this option.
  66              // However if it was already selected leave it in the dropdown.
  67              $presentationoptions[self::MODE_RESPONSETIME] = get_string('responsetime', 'feedback');
  68          }
  69          $presentationoptions[self::MODE_COURSE]  = get_string('course');
  70          $presentationoptions[self::MODE_CATEGORY]  = get_string('coursecategory');
  71  
  72          //build the form
  73          $this->item_form = new feedback_info_form('edit_item.php',
  74                                                    array('item'=>$item,
  75                                                    'common'=>$commonparams,
  76                                                    'positionlist'=>$positionlist,
  77                                                    'position' => $position,
  78                                                    'presentationoptions' => $presentationoptions));
  79      }
  80  
  81      public function save_item() {
  82          global $DB;
  83  
  84          if (!$item = $this->item_form->get_data()) {
  85              return false;
  86          }
  87  
  88          if (isset($item->clone_item) AND $item->clone_item) {
  89              $item->id = ''; //to clone this item
  90              $item->position++;
  91          }
  92  
  93          $item->hasvalue = $this->get_hasvalue();
  94          if (!$item->id) {
  95              $item->id = $DB->insert_record('feedback_item', $item);
  96          } else {
  97              $DB->update_record('feedback_item', $item);
  98          }
  99  
 100          return $DB->get_record('feedback_item', array('id'=>$item->id));
 101      }
 102  
 103      /**
 104       * Helper function for collected data, both for analysis page and export to excel
 105       *
 106       * @param stdClass $item the db-object from feedback_item
 107       * @param int|false $groupid
 108       * @param int $courseid
 109       * @return stdClass
 110       */
 111      protected function get_analysed($item, $groupid = false, $courseid = false) {
 112  
 113          $presentation = $item->presentation;
 114          $analysed_val = new stdClass();
 115          $analysed_val->data = null;
 116          $analysed_val->name = $item->name;
 117          $values = feedback_get_group_values($item, $groupid, $courseid);
 118          if ($values) {
 119              $data = array();
 120              foreach ($values as $value) {
 121                  $datavalue = new stdClass();
 122  
 123                  switch($presentation) {
 124                      case self::MODE_RESPONSETIME:
 125                          $datavalue->value = $value->value;
 126                          $datavalue->show = $value->value ? userdate($datavalue->value) : '';
 127                          break;
 128                      case self::MODE_COURSE:
 129                          $datavalue->value = $value->value;
 130                          $datavalue->show = $datavalue->value;
 131                          break;
 132                      case self::MODE_CATEGORY:
 133                          $datavalue->value = $value->value;
 134                          $datavalue->show = $datavalue->value;
 135                          break;
 136                  }
 137  
 138                  $data[] = $datavalue;
 139              }
 140              $analysed_val->data = $data;
 141          }
 142          return $analysed_val;
 143      }
 144  
 145      public function get_printval($item, $value) {
 146  
 147          if (strval($value->value) === '') {
 148              return '';
 149          }
 150          return $item->presentation == self::MODE_RESPONSETIME ?
 151                  userdate($value->value) : $value->value;
 152      }
 153  
 154      public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
 155          echo "<table class=\"analysis itemtype_{$item->typ}\">";
 156          $analysed_item = $this->get_analysed($item, $groupid, $courseid);
 157          $data = $analysed_item->data;
 158          if (is_array($data)) {
 159              echo '<tr><th colspan="2" align="left">';
 160              echo $itemnr . ' ';
 161              if (strval($item->label) !== '') {
 162                  echo '('. format_string($item->label).') ';
 163              }
 164              echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false));
 165              echo '</th></tr>';
 166              $sizeofdata = count($data);
 167              for ($i = 0; $i < $sizeofdata; $i++) {
 168                  $class = strlen(trim($data[$i]->show)) ? '' : ' class="isempty"';
 169                  echo '<tr'.$class.'><td colspan="2" class="singlevalue">';
 170                  echo str_replace("\n", '<br />', $data[$i]->show);
 171                  echo '</td></tr>';
 172              }
 173          }
 174          echo '</table>';
 175      }
 176  
 177      public function excelprint_item(&$worksheet, $row_offset,
 178                               $xls_formats, $item,
 179                               $groupid, $courseid = false) {
 180          $analysed_item = $this->get_analysed($item, $groupid, $courseid);
 181  
 182          $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
 183          $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
 184          $data = $analysed_item->data;
 185          if (is_array($data)) {
 186              $worksheet->write_string($row_offset, 2, $data[0]->show, $xls_formats->value_bold);
 187              $row_offset++;
 188              $sizeofdata = count($data);
 189              for ($i = 1; $i < $sizeofdata; $i++) {
 190                  $worksheet->write_string($row_offset, 2, $data[$i]->show, $xls_formats->default);
 191                  $row_offset++;
 192              }
 193          }
 194          $row_offset++;
 195          return $row_offset;
 196      }
 197  
 198      /**
 199       * Calculates the value of the item (time, course, course category)
 200       *
 201       * @param stdClass $item
 202       * @param stdClass $feedback
 203       * @param int $courseid
 204       * @return string
 205       */
 206      protected function get_current_value($item, $feedback, $courseid) {
 207          global $DB;
 208          switch ($item->presentation) {
 209              case self::MODE_RESPONSETIME:
 210                  if ($feedback->anonymous != FEEDBACK_ANONYMOUS_YES) {
 211                      // Response time is not allowed in anonymous feedbacks.
 212                      return time();
 213                  }
 214                  break;
 215              case self::MODE_COURSE:
 216                  $course = get_course($courseid);
 217                  return format_string($course->shortname, true,
 218                          array('context' => context_course::instance($course->id)));
 219                  break;
 220              case self::MODE_CATEGORY:
 221                  if ($courseid !== SITEID) {
 222                      $coursecategory = $DB->get_record_sql('SELECT cc.id, cc.name FROM {course_categories} cc, {course} c '
 223                              . 'WHERE c.category = cc.id AND c.id = ?', array($courseid));
 224                      return format_string($coursecategory->name, true,
 225                              array('context' => context_coursecat::instance($coursecategory->id)));
 226                  }
 227                  break;
 228          }
 229          return '';
 230      }
 231  
 232      /**
 233       * Adds an input element to the complete form
 234       *
 235       * @param stdClass $item
 236       * @param mod_feedback_complete_form $form
 237       */
 238      public function complete_form_element($item, $form) {
 239          if ($form->get_mode() == mod_feedback_complete_form::MODE_VIEW_RESPONSE) {
 240              $value = strval($form->get_item_value($item));
 241          } else {
 242              $value = $this->get_current_value($item,
 243                      $form->get_feedback(), $form->get_current_course_id());
 244          }
 245          $printval = $this->get_printval($item, (object)['value' => $value]);
 246  
 247          $class = '';
 248          switch ($item->presentation) {
 249              case self::MODE_RESPONSETIME:
 250                  $class = 'info-responsetime';
 251                  $value = $value ? self::CURRENTTIMESTAMP : '';
 252                  break;
 253              case self::MODE_COURSE:
 254                  $class = 'info-course';
 255                  break;
 256              case self::MODE_CATEGORY:
 257                  $class = 'info-category';
 258                  break;
 259          }
 260  
 261          $name = $this->get_display_name($item);
 262          $inputname = $item->typ . '_' . $item->id;
 263  
 264          $element = $form->add_form_element($item,
 265                  ['select', $inputname, $name,
 266                      array($value => $printval),
 267                      array('class' => $class)],
 268                  false,
 269                  false);
 270          $form->set_element_default($inputname, $value);
 271          $element->freeze();
 272          if ($form->get_mode() == mod_feedback_complete_form::MODE_COMPLETE) {
 273              $element->setPersistantFreeze(true);
 274          }
 275      }
 276  
 277      /**
 278       * Converts the value from complete_form data to the string value that is stored in the db.
 279       * @param mixed $value element from mod_feedback_complete_form::get_data() with the name $item->typ.'_'.$item->id
 280       * @return string
 281       */
 282      public function create_value($value) {
 283          if ($value === self::CURRENTTIMESTAMP) {
 284              return strval(time());
 285          }
 286          return parent::create_value($value);
 287      }
 288  
 289      public function can_switch_require() {
 290          return false;
 291      }
 292  }


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