[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/data/field/picture/ -> field.class.php (source)

   1  <?php
   2  ///////////////////////////////////////////////////////////////////////////
   3  //                                                                       //
   4  // NOTICE OF COPYRIGHT                                                   //
   5  //                                                                       //
   6  // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
   7  //          http://moodle.org                                            //
   8  //                                                                       //
   9  // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
  10  //                                                                       //
  11  // This program is free software; you can redistribute it and/or modify  //
  12  // it under the terms of the GNU General Public License as published by  //
  13  // the Free Software Foundation; either version 2 of the License, or     //
  14  // (at your option) any later version.                                   //
  15  //                                                                       //
  16  // This program is distributed in the hope that it will be useful,       //
  17  // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
  18  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
  19  // GNU General Public License for more details:                          //
  20  //                                                                       //
  21  //          http://www.gnu.org/copyleft/gpl.html                         //
  22  //                                                                       //
  23  ///////////////////////////////////////////////////////////////////////////
  24  
  25  class data_field_picture extends data_field_base {
  26      var $type = 'picture';
  27      var $previewwidth  = 50;
  28      var $previewheight = 50;
  29  
  30      function display_add_field($recordid=0) {
  31          global $CFG, $DB, $OUTPUT, $USER, $PAGE;
  32  
  33          $file        = false;
  34          $content     = false;
  35          $displayname = '';
  36          $alttext     = '';
  37          $itemid = null;
  38          $fs = get_file_storage();
  39  
  40          if ($recordid) {
  41              if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
  42                  file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
  43                  if (!empty($content->content)) {
  44                      if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
  45                          $usercontext = context_user::instance($USER->id);
  46                          if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) {
  47                              return false;
  48                          }
  49                          if ($thumbfile = $fs->get_file($usercontext->id, 'user', 'draft', $itemid, '/', 'thumb_'.$content->content)) {
  50                              $thumbfile->delete();
  51                          }
  52                          if (empty($content->content1)) {
  53                              // Print icon if file already exists
  54                              $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename());
  55                              $displayname = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')). '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
  56  
  57                          } else {
  58                              $displayname = get_string('nofilesattached', 'repository');
  59                          }
  60                      }
  61                  }
  62                  $alttext = $content->content1;
  63              }
  64          } else {
  65              $itemid = file_get_unused_draft_itemid();
  66          }
  67  
  68          $str = '<div title="'.s($this->field->description).'">';
  69          $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
  70          $str .= '<noscript>';
  71          if ($file) {
  72              $src = file_encode_url($CFG->wwwroot.'/pluginfile.php/', $this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename());
  73              $str .= '<img width="'.s($this->previewwidth).'" height="'.s($this->previewheight).'" src="'.$src.'" alt="" />';
  74          }
  75          $str .= '</noscript>';
  76  
  77          $options = new stdClass();
  78          $options->maxbytes  = $this->field->param3;
  79          $options->maxfiles  = 1; // Only one picture permitted.
  80          $options->itemid    = $itemid;
  81          $options->accepted_types = array('web_image');
  82          $options->return_types = FILE_INTERNAL;
  83          $options->context = $PAGE->context;
  84          if (!empty($file)) {
  85              $options->filename = $file->get_filename();
  86              $options->filepath = '/';
  87          }
  88  
  89          $fm = new form_filemanager($options);
  90          // Print out file manager.
  91  
  92          $output = $PAGE->get_renderer('core', 'files');
  93          $str .= $output->render($fm);
  94  
  95          $str .= '<div class="mdl-left">';
  96          $str .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.$itemid.'" />';
  97          $str .= '<label for="field_'.$this->field->id.'_alttext">'.get_string('alttext','data') .'</label>&nbsp;<input type="text" name="field_'
  98                  .$this->field->id.'_alttext" id="field_'.$this->field->id.'_alttext" value="'.s($alttext).'" />';
  99          $str .= '</div>';
 100  
 101          $str .= '</fieldset>';
 102          $str .= '</div>';
 103  
 104          return $str;
 105      }
 106  
 107      // TODO delete this function and instead subclass data_field_file - see MDL-16493
 108  
 109      function get_file($recordid, $content=null) {
 110          global $DB;
 111          if (empty($content)) {
 112              if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 113                  return null;
 114              }
 115          }
 116          $fs = get_file_storage();
 117          if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
 118              return null;
 119          }
 120  
 121          return $file;
 122      }
 123  
 124      function display_search_field($value = '') {
 125          return '<label class="accesshide" for="f_'.$this->field->id.'">' . get_string('fieldname', 'data') . '</label>' .
 126                 '<input type="text" size="16" id="f_'.$this->field->id.'" name="f_'.$this->field->id.'" value="'.$value.'" />';
 127      }
 128  
 129      function parse_search_field() {
 130          return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
 131      }
 132  
 133      function generate_sql($tablealias, $value) {
 134          global $DB;
 135  
 136          static $i=0;
 137          $i++;
 138          $name = "df_picture_$i";
 139          return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
 140      }
 141  
 142      function display_browse_field($recordid, $template) {
 143          global $CFG, $DB;
 144  
 145          if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 146              return false;
 147          }
 148  
 149          if (empty($content->content)) {
 150              return '';
 151          }
 152  
 153          $alt   = $content->content1;
 154          $title = $alt;
 155  
 156          if ($template == 'listtemplate') {
 157              $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.'thumb_'.$content->content);
 158              // no need to add width/height, because the thumb is resized properly
 159              $str = '<a href="view.php?d='.$this->field->dataid.'&amp;rid='.$recordid.'"><img src="'.$src.'" alt="'.s($alt).'" title="'.s($title).'" class="list_picture"/></a>';
 160  
 161          } else {
 162              $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$content->content);
 163              $width  = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' ';
 164              $height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' ';
 165              $str = '<a href="'.$src.'"><img '.$width.$height.' src="'.$src.'" alt="'.s($alt).'" title="'.s($title).'" class="list_picture"/></a>';
 166          }
 167  
 168          return $str;
 169      }
 170  
 171      function update_field() {
 172          global $DB, $OUTPUT;
 173  
 174          // Get the old field data so that we can check whether the thumbnail dimensions have changed
 175          $oldfield = $DB->get_record('data_fields', array('id'=>$this->field->id));
 176          $DB->update_record('data_fields', $this->field);
 177  
 178          // Have the thumbnail dimensions changed?
 179          if ($oldfield && ($oldfield->param4 != $this->field->param4 || $oldfield->param5 != $this->field->param5)) {
 180              // Check through all existing records and update the thumbnail
 181              if ($contents = $DB->get_records('data_content', array('fieldid'=>$this->field->id))) {
 182                  $fs = get_file_storage();
 183                  if (count($contents) > 20) {
 184                      echo $OUTPUT->notification(get_string('resizingimages', 'data'), 'notifysuccess');
 185                      echo "\n\n";
 186                      // To make sure that ob_flush() has the desired effect
 187                      ob_flush();
 188                  }
 189                  foreach ($contents as $content) {
 190                      if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
 191                          continue;
 192                      }
 193                      if ($thumbfile = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', 'thumb_'.$content->content)) {
 194                          $thumbfile->delete();
 195                      }
 196                      core_php_time_limit::raise(300);
 197                      // Might be slow!
 198                      $this->update_thumbnail($content, $file);
 199                  }
 200              }
 201          }
 202          return true;
 203      }
 204  
 205      function update_content($recordid, $value, $name='') {
 206          global $CFG, $DB, $USER;
 207  
 208          if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 209          // Quickly make one now!
 210              $content = new stdClass();
 211              $content->fieldid  = $this->field->id;
 212              $content->recordid = $recordid;
 213              $id = $DB->insert_record('data_content', $content);
 214              $content = $DB->get_record('data_content', array('id'=>$id));
 215          }
 216  
 217          $names = explode('_', $name);
 218          switch ($names[2]) {
 219              case 'file':
 220                  $fs = get_file_storage();
 221                  $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id);
 222                  $usercontext = context_user::instance($USER->id);
 223                  $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value);
 224                  if (count($files)<2) {
 225                      // no file
 226                  } else {
 227                      $count = 0;
 228                      foreach ($files as $draftfile) {
 229                          $file_record = array('contextid'=>$this->context->id, 'component'=>'mod_data', 'filearea'=>'content', 'itemid'=>$content->id, 'filepath'=>'/');
 230                          if (!$draftfile->is_directory()) {
 231                              $file_record['filename'] = $draftfile->get_filename();
 232  
 233                              $content->content = $draftfile->get_filename();
 234  
 235                              $file = $fs->create_file_from_storedfile($file_record, $draftfile);
 236  
 237                              // If the file is not a valid image, redirect back to the upload form.
 238                              if ($file->get_imageinfo() === false) {
 239                                  $url = new moodle_url('/mod/data/edit.php', array('d' => $this->field->dataid));
 240                                  redirect($url, get_string('invalidfiletype', 'error', $file->get_filename()));
 241                              }
 242  
 243                              $DB->update_record('data_content', $content);
 244                              $this->update_thumbnail($content, $file);
 245  
 246                              if ($count > 0) {
 247                                  break;
 248                              } else {
 249                                  $count++;
 250                              }
 251                          }
 252                      }
 253                  }
 254  
 255                  break;
 256  
 257              case 'alttext':
 258                  // only changing alt tag
 259                  $content->content1 = clean_param($value, PARAM_NOTAGS);
 260                  $DB->update_record('data_content', $content);
 261                  break;
 262  
 263              default:
 264                  break;
 265          }
 266      }
 267  
 268      function update_thumbnail($content, $file) {
 269          // (Re)generate thumbnail image according to the dimensions specified in the field settings.
 270          // If thumbnail width and height are BOTH not specified then no thumbnail is generated, and
 271          // additionally an attempted delete of the existing thumbnail takes place.
 272          $fs = get_file_storage();
 273          $file_record = array('contextid'=>$file->get_contextid(), 'component'=>$file->get_component(), 'filearea'=>$file->get_filearea(),
 274                               'itemid'=>$file->get_itemid(), 'filepath'=>$file->get_filepath(),
 275                               'filename'=>'thumb_'.$file->get_filename(), 'userid'=>$file->get_userid());
 276          try {
 277              // this may fail for various reasons
 278              $fs->convert_image($file_record, $file, $this->field->param4, $this->field->param5, true);
 279              return true;
 280          } catch (Exception $e) {
 281              debugging($e->getMessage());
 282              return false;
 283          }
 284      }
 285  
 286      function text_export_supported() {
 287          return false;
 288      }
 289  
 290      function file_ok($path) {
 291          return true;
 292      }
 293  }
 294  
 295  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1