[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/data/field/file/ -> 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_file extends data_field_base {
  26      var $type = 'file';
  27  
  28      function display_add_field($recordid = 0, $formdata = null) {
  29          global $CFG, $DB, $OUTPUT, $PAGE, $USER;
  30  
  31          $file        = false;
  32          $content     = false;
  33          $displayname = '';
  34          $fs = get_file_storage();
  35          $context = $PAGE->context;
  36          $itemid = null;
  37  
  38          // editing an existing database entry
  39          if ($formdata) {
  40              $fieldname = 'field_' . $this->field->id . '_file';
  41              $itemid = clean_param($formdata->$fieldname, PARAM_INT);
  42          } else if ($recordid) {
  43              if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
  44  
  45                  file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
  46  
  47                  if (!empty($content->content)) {
  48                      if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
  49                          $usercontext = context_user::instance($USER->id);
  50                          if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) {
  51                              return false;
  52                          }
  53                          if (empty($content->content1)) {
  54                              // Print icon if file already exists
  55                              $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename());
  56                              $displayname = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')). '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
  57  
  58                          } else {
  59                              $displayname = 'no file added';
  60                          }
  61                      }
  62                  }
  63              }
  64          } else {
  65              $itemid = file_get_unused_draft_itemid();
  66          }
  67  
  68          // database entry label
  69          $html = '<div title="' . s($this->field->description) . '">';
  70          $html .= '<fieldset><legend><span class="accesshide">'.$this->field->name;
  71  
  72          if ($this->field->required) {
  73              $html .= '&nbsp;' . get_string('requiredelement', 'form') . '</span></legend>';
  74              $image = html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'),
  75                                       array('class' => 'req', 'title' => get_string('requiredelement', 'form')));
  76              $html .= html_writer::div($image, 'inline-req');
  77          } else {
  78              $html .= '</span></legend>';
  79          }
  80  
  81          // itemid element
  82          $html .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.s($itemid).'" />';
  83  
  84          $options = new stdClass();
  85          $options->maxbytes = $this->field->param3;
  86          $options->maxfiles  = 1; // Limit to one file for the moment, this may be changed if requested as a feature in the future.
  87          $options->itemid    = $itemid;
  88          $options->accepted_types = '*';
  89          $options->return_types = FILE_INTERNAL;
  90          $options->context = $PAGE->context;
  91  
  92          $fm = new form_filemanager($options);
  93          // Print out file manager.
  94  
  95          $output = $PAGE->get_renderer('core', 'files');
  96          $html .= '<div class="mod-data-input">';
  97          $html .= $output->render($fm);
  98          $html .= '</div>';
  99          $html .= '</fieldset>';
 100          $html .= '</div>';
 101  
 102          return $html;
 103      }
 104  
 105      function display_search_field($value = '') {
 106          return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
 107                 '<input type="text" size="16" id="f_'.$this->field->id.'" name="f_'.$this->field->id.'" value="'.s($value).'" />';
 108      }
 109  
 110      function generate_sql($tablealias, $value) {
 111          global $DB;
 112  
 113          static $i=0;
 114          $i++;
 115          $name = "df_file_$i";
 116          return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
 117      }
 118  
 119      function parse_search_field() {
 120          return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
 121      }
 122  
 123      function get_file($recordid, $content=null) {
 124          global $DB;
 125          if (empty($content)) {
 126              if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 127                  return null;
 128              }
 129          }
 130          $fs = get_file_storage();
 131          if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
 132              return null;
 133          }
 134  
 135          return $file;
 136      }
 137  
 138      function display_browse_field($recordid, $template) {
 139          global $CFG, $DB, $OUTPUT;
 140  
 141          if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 142              return '';
 143          }
 144  
 145          if (empty($content->content)) {
 146              return '';
 147          }
 148  
 149          if (!$file = $this->get_file($recordid, $content)) {
 150              return '';
 151          }
 152  
 153          $name   = empty($content->content1) ? $file->get_filename() : $content->content1;
 154          $src    = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename());
 155          $width  = $this->field->param1 ? ' width  = "'.s($this->field->param1).'" ':' ';
 156          $height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' ';
 157  
 158          $str = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('width' => 16, 'height' => 16)). '&nbsp;'.
 159                 '<a href="'.$src.'" >'.s($name).'</a>';
 160          return $str;
 161      }
 162  
 163  
 164      // content: "a##b" where a is the file name, b is the display name
 165      function update_content($recordid, $value, $name='') {
 166          global $CFG, $DB, $USER;
 167          $fs = get_file_storage();
 168  
 169          if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 170  
 171          // Quickly make one now!
 172              $content = new stdClass();
 173              $content->fieldid  = $this->field->id;
 174              $content->recordid = $recordid;
 175              $id = $DB->insert_record('data_content', $content);
 176              $content = $DB->get_record('data_content', array('id'=>$id));
 177          }
 178  
 179          file_save_draft_area_files($value, $this->context->id, 'mod_data', 'content', $content->id);
 180  
 181          $usercontext = context_user::instance($USER->id);
 182          $files = $fs->get_area_files($this->context->id, 'mod_data', 'content', $content->id, 'itemid, filepath, filename', false);
 183  
 184          // We expect no or just one file (maxfiles = 1 option is set for the form_filemanager).
 185          if (count($files) == 0) {
 186              $content->content = null;
 187          } else {
 188              $content->content = array_values($files)[0]->get_filename();
 189              if (count($files) > 1) {
 190                  // This should not happen with a consistent database. Inform admins/developers about the inconsistency.
 191                  debugging('more then one file found in mod_data instance {$this->data->id} file field (field id: {$this->field->id}) area during update data record {$recordid} (content id: {$content->id})', DEBUG_NORMAL);
 192              }
 193          }
 194          $DB->update_record('data_content', $content);
 195      }
 196  
 197      function text_export_supported() {
 198          return false;
 199      }
 200  
 201      function file_ok($path) {
 202          return true;
 203      }
 204  
 205      /**
 206       * Custom notempty function
 207       *
 208       * @param string $value
 209       * @param string $name
 210       * @return bool
 211       */
 212      function notemptyfield($value, $name) {
 213          global $USER;
 214  
 215          $names = explode('_', $name);
 216          if ($names[2] == 'file') {
 217              $usercontext = context_user::instance($USER->id);
 218              $fs = get_file_storage();
 219              $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value);
 220              return count($files) >= 2;
 221          }
 222          return false;
 223      }
 224  
 225  }


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