[ Index ]

PHP Cross Reference of moodle-2.8

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) {
  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 ($recordid){
  40              if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
  41  
  42                  file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
  43  
  44                  if (!empty($content->content)) {
  45                      if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
  46                          $usercontext = context_user::instance($USER->id);
  47                          if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) {
  48                              return false;
  49                          }
  50                          if (empty($content->content1)) {
  51                              // Print icon if file already exists
  52                              $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename());
  53                              $displayname = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')). '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
  54  
  55                          } else {
  56                              $displayname = 'no file added';
  57                          }
  58                      }
  59                  }
  60              }
  61          } else {
  62              $itemid = file_get_unused_draft_itemid();
  63          }
  64  
  65          $html = '';
  66          // database entry label
  67          $html .= '<div title="'.s($this->field->description).'">';
  68          $html .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
  69  
  70          // itemid element
  71          $html .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.$itemid.'" />';
  72  
  73          $options = new stdClass();
  74          $options->maxbytes = $this->field->param3;
  75          $options->maxfiles  = 1; // Limit to one file for the moment, this may be changed if requested as a feature in the future.
  76          $options->itemid    = $itemid;
  77          $options->accepted_types = '*';
  78          $options->return_types = FILE_INTERNAL;
  79          $options->context = $PAGE->context;
  80  
  81          $fm = new form_filemanager($options);
  82          // Print out file manager.
  83  
  84          $output = $PAGE->get_renderer('core', 'files');
  85          $html .= $output->render($fm);
  86  
  87          $html .= '</fieldset>';
  88          $html .= '</div>';
  89  
  90          return $html;
  91      }
  92  
  93      function display_search_field($value = '') {
  94          return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' .
  95                 '<input type="text" size="16" id="f_'.$this->field->id.'" name="f_'.$this->field->id.'" value="'.$value.'" />';
  96      }
  97  
  98      function generate_sql($tablealias, $value) {
  99          global $DB;
 100  
 101          static $i=0;
 102          $i++;
 103          $name = "df_file_$i";
 104          return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
 105      }
 106  
 107      function parse_search_field() {
 108          return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
 109      }
 110  
 111      function get_file($recordid, $content=null) {
 112          global $DB;
 113          if (empty($content)) {
 114              if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 115                  return null;
 116              }
 117          }
 118          $fs = get_file_storage();
 119          if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
 120              return null;
 121          }
 122  
 123          return $file;
 124      }
 125  
 126      function display_browse_field($recordid, $template) {
 127          global $CFG, $DB, $OUTPUT;
 128  
 129          if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 130              return '';
 131          }
 132  
 133          if (empty($content->content)) {
 134              return '';
 135          }
 136  
 137          if (!$file = $this->get_file($recordid, $content)) {
 138              return '';
 139          }
 140  
 141          $name   = empty($content->content1) ? $file->get_filename() : $content->content1;
 142          $src    = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename());
 143          $width  = $this->field->param1 ? ' width  = "'.s($this->field->param1).'" ':' ';
 144          $height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' ';
 145  
 146          $str = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('width' => 16, 'height' => 16)). '&nbsp;'.
 147                 '<a href="'.$src.'" >'.s($name).'</a>';
 148          return $str;
 149      }
 150  
 151  
 152      // content: "a##b" where a is the file name, b is the display name
 153      function update_content($recordid, $value, $name='') {
 154          global $CFG, $DB, $USER;
 155          $fs = get_file_storage();
 156  
 157          if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 158  
 159          // Quickly make one now!
 160              $content = new stdClass();
 161              $content->fieldid  = $this->field->id;
 162              $content->recordid = $recordid;
 163              $id = $DB->insert_record('data_content', $content);
 164              $content = $DB->get_record('data_content', array('id'=>$id));
 165          }
 166  
 167          // delete existing files
 168          $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id);
 169  
 170          $usercontext = context_user::instance($USER->id);
 171          $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value, 'timecreated DESC');
 172  
 173          if (count($files)<2) {
 174              // no file
 175          } else {
 176              foreach ($files as $draftfile) {
 177                  if (!$draftfile->is_directory()) {
 178                      $file_record = array(
 179                          'contextid' => $this->context->id,
 180                          'component' => 'mod_data',
 181                          'filearea' => 'content',
 182                          'itemid' => $content->id,
 183                          'filepath' => '/',
 184                          'filename' => $draftfile->get_filename(),
 185                      );
 186  
 187                      $content->content = $file_record['filename'];
 188  
 189                      $fs->create_file_from_storedfile($file_record, $draftfile);
 190                      $DB->update_record('data_content', $content);
 191  
 192                      // Break from the loop now to avoid overwriting the uploaded file record
 193                      break;
 194                  }
 195              }
 196          }
 197      }
 198  
 199      function text_export_supported() {
 200          return false;
 201      }
 202  
 203      function file_ok($path) {
 204          return true;
 205      }
 206  
 207  }
 208  
 209  


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