From 8429163db991a34532932f96c47ee38ffb31a219 Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 14 Sep 2008 08:22:44 +0000 Subject: [PATCH] MDL-16493 file and picture element migrated; waiting for file picker to mature, presets not finished yet --- mod/data/db/upgrade.php | 88 ++- mod/data/field/checkbox/field.class.php | 4 - mod/data/field/date/field.class.php | 4 - mod/data/field/file/field.class.php | 120 +-- mod/data/field/latlong/field.class.php | 4 - mod/data/field/menu/field.class.php | 4 - mod/data/field/multimenu/field.class.php | 5 - mod/data/field/number/field.class.php | 4 - mod/data/field/picture/field.class.php | 285 +++---- mod/data/field/radiobutton/field.class.php | 5 - mod/data/field/text/field.class.php | 4 - mod/data/field/textarea/field.class.php | 5 - mod/data/field/url/field.class.php | 4 - mod/data/lib.php | 241 ++++-- mod/data/preset_class.php | 821 --------------------- mod/data/preset_new.php | 172 ----- mod/data/version.php | 2 +- mod/data/view.php | 20 +- 18 files changed, 448 insertions(+), 1344 deletions(-) delete mode 100644 mod/data/preset_class.php delete mode 100755 mod/data/preset_new.php diff --git a/mod/data/db/upgrade.php b/mod/data/db/upgrade.php index e0731452ec..5ffbde45fa 100644 --- a/mod/data/db/upgrade.php +++ b/mod/data/db/upgrade.php @@ -40,7 +40,7 @@ function xmldb_data_upgrade($oldversion) { upgrade_mod_savepoint($result, 2007101512, 'data'); } - if ($result && $oldversion < 2007101513) { + if ($result && $oldversion < 2007101513) { // Upgrade all the data->notification currently being // NULL to 0 $sql = "UPDATE {data} SET notification=0 WHERE notification IS NULL"; @@ -56,15 +56,97 @@ function xmldb_data_upgrade($oldversion) { } if ($result && $oldversion < 2008081400) { - if ($datainstances = $DB->get_records('data')) { + if ($rs = $DB->get_recordset('data')) { $pattern = '/\#\#delete\#\#(\s+)\#\#approve\#\#/'; $replacement = '##delete##$1##approve##$1##export##'; - foreach ($datainstances as $data) { + foreach ($rs as $data) { $data->listtemplate = preg_replace($pattern, $replacement, $data->listtemplate); $data->singletemplate = preg_replace($pattern, $replacement, $data->singletemplate); $DB->update_record('data', $data); } + $rs->close(); } + upgrade_mod_savepoint($result, 2008081400, 'data'); + } + + if ($result && $oldversion < 2008091400) { + + ///////////////////////////////////// + /// new file storage upgrade code /// + ///////////////////////////////////// + + $fs = get_file_storage(); + + $empty = $DB->sql_empty(); // silly oracle empty string handling workaround + + $sqlfrom = "FROM {data_content} c + JOIN {data_fields} f ON f.id = c.fieldid + JOIN {data_records} r ON r.id = c.recordid + JOIN {data} d ON d.id = r.dataid + JOIN {modules} m ON m.name = 'data' + JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = d.id) + WHERE c.content <> '$empty' AND c.content IS NOT NULL + AND (f.type = 'file' OR f.type = 'picture')"; + + $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom"); + + if ($rs = $DB->get_recordset_sql("SELECT c.id, f.type, r.dataid, c.recordid, f.id AS fieldid, r.userid, c.content, c.content1, d.course, r.userid, cm.id AS cmid $sqlfrom ORDER BY d.course, d.id")) { + + $pbar = new progress_bar('migratedatafiles', 500, true); + + $olddebug = $DB->get_debug(); + $DB->set_debug(false); // lower debug level, there might be very many files + $i = 0; + foreach ($rs as $content) { + $i++; + upgrade_set_timeout(60); // set up timeout, may also abort execution + $pbar->update($i, $count, "Migrating data entries - $i/$count."); + + $filepath = "$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid/$content->fieldid/$content->recordid/$content->content"; + $context = get_context_instance(CONTEXT_MODULE, $content->cmid); + + if (!file_exists($filepath)) { + continue; + } + + $filearea = 'data_content'; + $oldfilename = $content->content; + $filename = clean_param($oldfilename, PARAM_FILE); + if ($filename === '') { + continue; + } + if (!$fs->file_exists($context->id, $filearea, $content->id, '/', $filename)) { + $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$content->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$content->userid); + if ($fs->create_file_from_pathname($file_record, $filepath)) { + unlink($filepath); + if ($oldfilename !== $filename) { + // update filename if needed + $DB->set_field('data_content', 'content', $filename, array('id'=>$content->id)); + } + if ($content->type == 'picture') { + // migrate thumb + $filepath = "$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid/$content->fieldid/$content->recordid/thumb/$content->content"; + if (!$fs->file_exists($context->id, $filearea, $content->id, '/', 'thumb_'.$filename)) { + $file_record['filename'] = 'thumb_'.$file_record['filename']; + $fs->create_file_from_pathname($file_record, $filepath); + unlink($filepath); + } + } + } + } + + // remove dirs if empty + @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid/$content->fieldid/$content->recordid/thumb"); + @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid/$content->fieldid/$content->recordid"); + @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid/$content->fieldid"); + @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid"); + @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data"); + @rmdir("$CFG->dataroot/$content->course/$CFG->moddata"); + } + $DB->set_debug($olddebug); // reset debug level + $rs->close(); + } + upgrade_mod_savepoint($result, 2008091400, 'data'); } return $result; diff --git a/mod/data/field/checkbox/field.class.php b/mod/data/field/checkbox/field.class.php index 607ea4171a..b319df79c4 100755 --- a/mod/data/field/checkbox/field.class.php +++ b/mod/data/field/checkbox/field.class.php @@ -26,10 +26,6 @@ class data_field_checkbox extends data_field_base { var $type = 'checkbox'; - function data_field_checkbox($field=0, $data=0) { - parent::data_field_base($field, $data); - } - function display_add_field($recordid=0) { global $CFG, $DB; diff --git a/mod/data/field/date/field.class.php b/mod/data/field/date/field.class.php index ab438375bc..6d7297cd11 100755 --- a/mod/data/field/date/field.class.php +++ b/mod/data/field/date/field.class.php @@ -34,10 +34,6 @@ class data_field_date extends data_field_base { var $month = 0; var $year = 0; - function data_field_date($field=0, $data=0) { - parent::data_field_base($field, $data); - } - function display_add_field($recordid=0) { global $DB; diff --git a/mod/data/field/file/field.class.php b/mod/data/field/file/field.class.php index fff1789db3..f83b68ff19 100755 --- a/mod/data/field/file/field.class.php +++ b/mod/data/field/file/field.class.php @@ -25,47 +25,44 @@ class data_field_file extends data_field_base { var $type = 'file'; - function data_field_file($field=0, $data=0) { - parent::data_field_base($field, $data); - } - function display_add_field($recordid=0) { global $CFG, $DB; + + $file = false; + $content = false; + $displayname = ''; + $fs = get_file_storage(); if ($recordid){ if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { - $contents[0] = $content->content; - $contents[1] = $content->content1; - } else { - $contents[0] = ''; - $contents[1] = ''; + if (!empty($content->content)) { + if ($file = $FS->get_file($this->context->id, 'data_content', $content->id, '/', $content->content)) { + if (empty($content->content1)) { + $displayname = $file->get_filename(); + } else { + $displayname = $content->content1; + } + } + } } - $src = empty($contents[0]) ? '' : $contents[0]; - $name = empty($contents[1]) ? $src : $contents[1]; - $displayname = empty($contents[1]) ? '' : $contents[1]; - require_once($CFG->libdir.'/filelib.php'); - $source = get_file_url($this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid); - } else { - $src = ''; - $name = ''; - $displayname = ''; - $source = ''; } - $str = '
'; + + $str = '
'; $str .= '
'.$this->field->name.''; $str .= ''; - $str .= get_string('file','data'). '
'; - $str .= get_string('optionalfilename','data').'
'; - $str .= ''; + //$str .= ''; $str .= '
'; $str .= '
'; - if ($recordid and isset($content) and !empty($content->content)) { - // Print icon - require_once($CFG->libdir.'/filelib.php'); - $icon = mimeinfo('icon', $src); + if ($file) { + // Print icon if file already exists + $browser = get_file_browser(); + $icon = mimeinfo_from_type('icon', $file->get_mimetype()); + $src = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename()); $str .= ''.$icon.''. - ''.$name.''; + ''.s($file->get_filename()).''; } return $str; } @@ -93,20 +90,27 @@ class data_field_file extends data_field_base { global $CFG, $DB; if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { - return false; + return ''; } - $width = $this->field->param1 ? ' width = "'.s($this->field->param1).'" ':' '; - $height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' '; + if (empty($content->content)) { return ''; } - require_once($CFG->libdir.'/filelib.php'); - $src = $content->content; - $name = empty($content->content1) ? $src : $content->content1; - $source = get_file_url($this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid); - $icon = mimeinfo('icon', $src); + + $fs = get_file_storage(); + $browser = get_file_browser(); + if (!$file = $fs->get_file($this->context->id, 'data_content', $content->id, '/', $content->content)) { + return ''; + } + + $name = empty($content->content1) ? $file->get_filename() : $content->content1; + $icon = mimeinfo_from_type('icon', $file->get_mimetype()); + $src = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename()); + $width = $this->field->param1 ? ' width = "'.s($this->field->param1).'" ':' '; + $height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' '; + $str = ''.$icon.' '. - ''.$name.''; + ''.s($name).''; return $str; } @@ -115,34 +119,32 @@ class data_field_file extends data_field_base { function update_content($recordid, $value, $name) { global $CFG, $DB; - if (!$oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { + if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { // Quickly make one now! - $oldcontent = new object; - $oldcontent->fieldid = $this->field->id; - $oldcontent->recordid = $recordid; - if ($oldcontent->id = $DB->insert_record('data_content', $oldcontent)) { + $content = new object(); + $content->fieldid = $this->field->id; + $content->recordid = $recordid; + if ($id = $DB->insert_record('data_content', $content)) { print_error('cannotinsertempty', 'data'); } + $content = $DB->get_record('data_content', array('id'=>$id)); } - $content = new object; - $content->id = $oldcontent->id; - $names = explode('_',$name); + + $names = explode('_', $name); switch ($names[2]) { case 'file': // file just uploaded -# $course = get_course('course', 'id', $this->data->course); - $filename = $_FILES[$names[0].'_'.$names[1]]; - $filename = $filename['name']; - $dir = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid; - // only use the manager if file is present, to avoid "are you sure you selected a file to upload" msg + $tmpfile = $_FILES[$names[0].'_'.$names[1]]; + $filename = $tmpfile['name']; + $pathanme = $tmpfile['tmp_name']; if ($filename){ - require_once($CFG->libdir.'/uploadlib.php'); - // FIX ME: $course not defined here - $um = new upload_manager($names[0].'_'.$names[1],true,false,$this->data->course,false,$this->field->param3); - if ($um->process_file_uploads($dir)) { - $newfile_name = $um->get_new_filename(); - $content->content = $newfile_name; - $DB->update_record('data_content',$content); + $fs = get_file_storage(); + // TODO: uploaded file processing will be in file picker ;-) + $fs->delete_area_files($this->context->id, 'data_content', $content->id); + $file_record = array('contextid'=>$this->context->id, 'filearea'=>'data_content', 'itemid'=>$content->id, 'filepath'=>'/', 'filename'=>$filename); + if ($file = $fs->create_file_from_pathname($file_record, $pathanme)) { + $content->content = $file->get_filename(); + $DB->update_record('data_content', $content); } } break; @@ -172,6 +174,10 @@ class data_field_file extends data_field_base { return false; } + function file_ok($path) { + return true; + } + } ?> diff --git a/mod/data/field/latlong/field.class.php b/mod/data/field/latlong/field.class.php index 348289038f..9985c84d14 100755 --- a/mod/data/field/latlong/field.class.php +++ b/mod/data/field/latlong/field.class.php @@ -43,10 +43,6 @@ class data_field_latlong extends data_field_base { ); // Other map sources listed at http://kvaleberg.com/extensions/mapsources/index.php?params=51_30.4167_N_0_7.65_W_region:earth - function data_field_latlong($field=0, $data=0) { - parent::data_field_base($field, $data); - } - function display_add_field($recordid=0) { global $CFG, $DB; diff --git a/mod/data/field/menu/field.class.php b/mod/data/field/menu/field.class.php index ff002da3ed..c855a90ce3 100755 --- a/mod/data/field/menu/field.class.php +++ b/mod/data/field/menu/field.class.php @@ -26,10 +26,6 @@ class data_field_menu extends data_field_base { var $type = 'menu'; - function data_field_menu($field=0, $data=0) { - parent::data_field_base($field, $data); - } - function display_add_field($recordid=0) { global $DB; diff --git a/mod/data/field/multimenu/field.class.php b/mod/data/field/multimenu/field.class.php index 0101e901db..030ef4d149 100755 --- a/mod/data/field/multimenu/field.class.php +++ b/mod/data/field/multimenu/field.class.php @@ -26,11 +26,6 @@ class data_field_multimenu extends data_field_base { var $type = 'multimenu'; - function data_field_multimenu($field=0, $data=0) { - parent::data_field_base($field, $data); - } - - function display_add_field($recordid=0) { global $DB; diff --git a/mod/data/field/number/field.class.php b/mod/data/field/number/field.class.php index fc6eeaec87..23835e29a9 100755 --- a/mod/data/field/number/field.class.php +++ b/mod/data/field/number/field.class.php @@ -25,10 +25,6 @@ class data_field_number extends data_field_base { var $type = 'number'; - function data_field_number($field=0, $data=0) { - parent::data_field_base($field, $data); - } - function update_content($recordid, $value, $name='') { global $DB; diff --git a/mod/data/field/picture/field.class.php b/mod/data/field/picture/field.class.php index 6bc8b1c2e2..5f299f8df3 100755 --- a/mod/data/field/picture/field.class.php +++ b/mod/data/field/picture/field.class.php @@ -22,45 +22,49 @@ // // /////////////////////////////////////////////////////////////////////////// -require_once($CFG->dirroot.'/mod/data/field/file/field.class.php'); -// Base class is 'file' - -class data_field_picture extends data_field_file { +class data_field_picture extends data_field_base { var $type = 'picture'; var $previewwidth = 50; var $previewheight = 50; - function data_field_picture($field=0, $data=0) { - parent::data_field_base($field, $data); - } - function display_add_field($recordid=0) { global $CFG, $DB; - $filepath = ''; - $filename = ''; - $description = ''; - if ($recordid) { + $file = false; + $content = false; + $displayname = ''; + $alttext = ''; + $fs = get_file_storage(); + if ($recordid){ if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { - $filename = $content->content; - $description = $content->content1; + if (!empty($content->content)) { + if ($file = $FS->get_file($this->context->id, 'data_content', $content->id, '/', $content->content)) { + if (empty($content->content1)) { + $displayname = $file->get_filename(); + } else { + $displayname = $content->content1; + } + } + } + $alttext = $content->content1; } - $path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid; - require_once($CFG->libdir.'/filelib.php'); - $filepath = get_file_url("$path/$filename"); } + $str = '
'; $str .= '
'.$this->field->name.''; $str .= ''; $str .= ' 
'; - $str .= ' 
'; - $str .= ''; - if ($filepath) { - $str .= ''; + $str .= ' 
'; + //$str .= ''; + if ($file) { + $browser = get_file_browser(); + $src = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename()); + $str .= ''; } $str .= '
'; $str .= '
'; + return $str; } @@ -86,38 +90,32 @@ class data_field_picture extends data_field_file { function display_browse_field($recordid, $template) { global $CFG, $DB; - if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { - if (isset($content->content)) { - $contents[0] = $content->content; - $contents[1] = $content->content1; - } - if (empty($contents[0])) { - // Nothing to show - return ''; - } - $alt = empty($contents[1])? '':$contents[1]; - $title = empty($contents[1])? '':$contents[1]; - $src = $contents[0]; - $path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid; + if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { + return false; + } - $thumbnaillocation = $CFG->dataroot .'/'. $path .'/thumb/'.$src; - require_once($CFG->libdir.'/filelib.php'); - $source = get_file_url("$path/$src"); - $thumbnailsource = file_exists($thumbnaillocation) ? get_file_url("$path/thumb/$src") : $source; + if (empty($content->content)) { + return ''; + } - if ($template == 'listtemplate') { - $width = $this->field->param4 ? ' width="'.s($this->field->param4).'" ' : ' '; - $height = $this->field->param5 ? ' height="'.s($this->field->param5).'" ' : ' '; - $str = ''.s($alt).''; - } else { - $width = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' '; - $height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' '; - $str = ''.s($alt).''; - } - return $str; + $browser = get_file_browser(); + + $alt = $content->content1; + $title = $alt; + + if ($template == 'listtemplate') { + $src = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.'thumb_'.$content->content); + // no need to add width/height, because the thumb is resized properly + $str = ''.s($alt).''; + + } else { + $src = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.$content->content); + $width = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' '; + $height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' '; + $str = ''.s($alt).''; } - return false; + + return $str; } function update_field() { @@ -134,6 +132,7 @@ class data_field_picture extends data_field_file { if ($oldfield && ($oldfield->param4 != $this->field->param4 || $oldfield->param5 != $this->field->param5)) { // Check through all existing records and update the thumbnail if ($contents = $DB->get_records('data_content', array('fieldid'=>$this->field->id))) { + $fs = get_file_storage(); if (count($contents) > 20) { notify(get_string('resizingimages', 'data'), 'notifysuccess'); echo "\n\n"; @@ -141,9 +140,15 @@ class data_field_picture extends data_field_file { ob_flush(); } foreach ($contents as $content) { + if (!$file = $fs->get_file($this->context->id, 'data_content', $content->id, '/', $content->content)) { + continue; + } + if ($thumbfile = $fs->get_file($this->context->id, 'data_content', $content->id, '/', 'thumb_'.$content->content)) { + $thumbfile->delete(); + } @set_time_limit(300); // Might be slow! - $this->update_thumbnail($content); + $this->update_thumbnail($content, $file); } } } @@ -151,135 +156,85 @@ class data_field_picture extends data_field_file { } function update_content($recordid, $value, $name) { - global $DB; - - parent::update_content($recordid, $value, $name); - $content = $DB->get_record('data_content',array('fieldid'=>$this->field->id, 'recordid'=>$recordid)); - $this->update_thumbnail($content); - // Regenerate the thumbnail - } + global $CFG, $DB; - function update_thumbnail($content) { - // (Re)generate thumbnail image according to the dimensions specified in the field settings. - // If thumbnail width and height are BOTH not specified then no thumbnail is generated, and - // additionally an attempted delete of the existing thumbnail takes place. - global $CFG; - require_once($CFG->libdir . '/gdlib.php'); - $datalocation = $CFG->dataroot .'/'.$this->data->course.'/'.$CFG->moddata.'/data/'. - $this->data->id.'/'.$this->field->id.'/'.$content->recordid; - $originalfile = $datalocation.'/'.$content->content; - if (!file_exists($originalfile)) { - return; - } - if (!file_exists($datalocation.'/thumb')) { - mkdir($datalocation.'/thumb', 0777); - // robertall: Why hardcode 0777?? - } - $thumbnaillocation = $datalocation.'/thumb/'.$content->content; - $imageinfo = GetImageSize($originalfile); - $image->width = $imageinfo[0]; - $image->height = $imageinfo[1]; - $image->type = $imageinfo[2]; - if (!$image->width || !$image->height) { - // Should not happen - return; + if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { + // Quickly make one now! + $content = new object(); + $content->fieldid = $this->field->id; + $content->recordid = $recordid; + if ($id = $DB->insert_record('data_content', $content)) { + print_error('cannotinsertempty', 'data'); + } + $content = $DB->get_record('data_content', array('id'=>$id)); } - switch ($image->type) { - case 1: - if (function_exists('ImageCreateFromGIF')) { - $im = ImageCreateFromGIF($originalfile); - } else { - return; + + $names = explode('_', $name); + switch ($names[2]) { + case 'file': + // file just uploaded + $tmpfile = $_FILES[$names[0].'_'.$names[1]]; + $filename = $tmpfile['name']; + $pathanme = $tmpfile['tmp_name']; + if ($filename){ + $fs = get_file_storage(); + // TODO: uploaded file processing will be in file picker ;-) + $fs->delete_area_files($this->context->id, 'data_content', $content->id); + $file_record = array('contextid'=>$this->context->id, 'filearea'=>'data_content', 'itemid'=>$content->id, 'filepath'=>'/', 'filename'=>$filename); + if ($file = $fs->create_file_from_pathname($file_record, $pathanme)) { + $content->content = $file->get_filename(); + $DB->update_record('data_content', $content); + // Regenerate the thumbnail + $this->update_thumbnail($content, $file); + } } break; - case 2: - if (function_exists('ImageCreateFromJPEG')) { - $im = ImageCreateFromJPEG($originalfile); - } else { - return; - } + + case 'alttext': + // only changing alt tag + $content->content1 = clean_param($value, PARAM_NOTAGS); + $DB->update_record('data_content', $content); break; - case 3: - if (function_exists('ImageCreateFromPNG')) { - $im = ImageCreateFromPNG($originalfile); - } else { - return; - } + + default: break; } - $thumbwidth = isset($this->field->param4)?$this->field->param4:''; - $thumbheight = isset($this->field->param5)?$this->field->param5:''; - if ($thumbwidth || $thumbheight) { - // Only if either width OR height specified do we want a thumbnail - $wcrop = $image->width; - $hcrop = $image->height; - if ($thumbwidth && !$thumbheight) { - $thumbheight = $image->height * $thumbwidth / $image->width; - } else if($thumbheight && !$thumbwidth) { - $thumbwidth = $image->width * $thumbheight / $image->height; - } else { - // BOTH are set - may need to crop if aspect ratio differs - $hratio = $image->height / $thumbheight; - $wratio = $image->width / $thumbwidth; - if ($wratio > $hratio) { - // Crop the width - $wcrop = intval($thumbwidth * $hratio); - } elseif($hratio > $wratio) { - //Crop the height - $hcrop = intval($thumbheight * $wratio); - } - } - - // At this point both $thumbwidth and $thumbheight are set, and $wcrop and $hcrop - - if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) { - $im1 = ImageCreateTrueColor($thumbwidth,$thumbheight); - } else { - $im1 = ImageCreate($thumbwidth,$thumbheight); - } - if ($image->type == 3) { - // Prevent alpha blending for PNG images - imagealphablending($im1, false); - } - $cx = $image->width / 2; - $cy = $image->height / 2; - - // These "half" measurements use the "crop" values rather than the actual dimensions - $halfwidth = floor($wcrop * 0.5); - $halfheight = floor($hcrop * 0.5); + } - ImageCopyBicubic($im1, $im, 0, 0, $cx-$halfwidth, $cy-$halfheight, - $thumbwidth, $thumbheight, $halfwidth*2, $halfheight*2); + function update_thumbnail($content, $file) { + // (Re)generate thumbnail image according to the dimensions specified in the field settings. + // If thumbnail width and height are BOTH not specified then no thumbnail is generated, and + // additionally an attempted delete of the existing thumbnail takes place. + $fs = get_file_storage(); + $file_record = array('contextid'=>$file->get_contextid(), 'filearea'=>$file->get_filearea(), + 'itemid'=>$file->get_itemid(), 'filepath'=>$file->get_filepath(), + 'filename'=>'thumb_'.$file->get_filename(), 'userid'=>$file->get_userid()); + try { + // this may fail for various reasons + $fs->convert_image($file_record, $file, $this->field->param4, $this->field->param5, true); + return true; + } catch (Exception $e) { + return false; + } + } - if ($image->type == 3) { - // Save alpha transparency for PNG images - imagesavealpha($im1, true); - } - if (function_exists('ImageJpeg') && $image->type != 3) { - @touch($thumbnaillocation); - // Helps in Safe mode - if (ImageJpeg($im1, $thumbnaillocation, 90)) { - @chmod($thumbnaillocation, 0666); - // robertall: Why hardcode 0666?? - } - } elseif (function_exists('ImagePng') && $image->type == 3) { - @touch($thumbnaillocation); - // Helps in Safe mode - if (ImagePng($im1, $thumbnaillocation, 9)) { - @chmod($thumbnaillocation, 0666); - // robertall: Why hardcode 0666?? - } - } - } else { - // Try to remove the thumbnail - we don't want thumbnailing active - @unlink($thumbnaillocation); + function notemptyfield($value, $name) { + $names = explode('_',$name); + if ($names[2] == 'file') { + $filename = $_FILES[$names[0].'_'.$names[1]]; + return !empty($filename['name']); + // if there's a file in $_FILES, not empty } + return false; } function text_export_supported() { return false; } + function file_ok($path) { + return true; + } } ?> diff --git a/mod/data/field/radiobutton/field.class.php b/mod/data/field/radiobutton/field.class.php index 68f7af6a3c..6f024a38bc 100755 --- a/mod/data/field/radiobutton/field.class.php +++ b/mod/data/field/radiobutton/field.class.php @@ -26,11 +26,6 @@ class data_field_radiobutton extends data_field_base { var $type = 'radiobutton'; - function data_field_radiobutton($field=0, $data=0) { - parent::data_field_base($field, $data); - } - - function display_add_field($recordid=0) { global $CFG, $DB; diff --git a/mod/data/field/text/field.class.php b/mod/data/field/text/field.class.php index 31df447b37..2b42d5ea87 100755 --- a/mod/data/field/text/field.class.php +++ b/mod/data/field/text/field.class.php @@ -26,10 +26,6 @@ class data_field_text extends data_field_base { var $type = 'text'; - function data_field_text($field=0, $data=0) { - parent::data_field_base($field, $data); - } - function display_search_field($value = '') { return ''; } diff --git a/mod/data/field/textarea/field.class.php b/mod/data/field/textarea/field.class.php index 4933a08ddd..0146103ad6 100755 --- a/mod/data/field/textarea/field.class.php +++ b/mod/data/field/textarea/field.class.php @@ -26,11 +26,6 @@ class data_field_textarea extends data_field_base { var $type = 'textarea'; - function data_field_textarea($field=0, $data=0) { - parent::data_field_base($field, $data); - } - - function display_add_field($recordid=0) { global $CFG, $DB; diff --git a/mod/data/field/url/field.class.php b/mod/data/field/url/field.class.php index e8c3ee54f9..ab972617e4 100755 --- a/mod/data/field/url/field.class.php +++ b/mod/data/field/url/field.class.php @@ -25,10 +25,6 @@ class data_field_url extends data_field_base { var $type = 'url'; - function data_field_text($field=0, $data=0) { - parent::data_field_base($field, $data); - } - function display_add_field($recordid=0) { global $CFG, $DB; diff --git a/mod/data/lib.php b/mod/data/lib.php index 5dd7e7b838..6c61fb9ca5 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -35,7 +35,7 @@ define ('DATA_TIMEMODIFIED', -4); define ('DATA_CAP_EXPORT', 'mod/data:viewalluserpresets'); // Users having assigned the default role "Non-editing teacher" can export database records // Using the mod/data capability "viewalluserpresets" existing in Moodle 1.9.x. -// In Moodle >= 2, new roles may be introduced and used instead. +// In Moodle >= 2, new roles may be introduced and used instead. class data_field_base { // Base class for Database Field Types (see field/*/field.class.php) @@ -46,9 +46,11 @@ class data_field_base { // Base class for Database Field Types (see field/*/ var $iconwidth = 16; // Width of the icon for this fieldtype var $iconheight = 16; // Width of the icon for this fieldtype + var $cm; // course module or cmifno + var $context; // activity context // Constructor function - function data_field_base($field=0, $data=0) { // Field or data or both, each can be id or object + function __construct($field=0, $data=0, $cm=0) { // Field or data or both, each can be id or object global $DB; if (empty($field) && empty($data)) { @@ -80,9 +82,17 @@ class data_field_base { // Base class for Database Field Types (see field/*/ } } + if ($cm) { + $this->cm = $cm; + } else { + $this->cm = get_coursemodule_from_instance('data', $this->data->id); + } + if (empty($this->field)) { // We need to define some default values $this->define_default_field(); } + + $this->context = get_context_instance(CONTEXT_MODULE, $this->cm->id); } @@ -165,8 +175,8 @@ class data_field_base { // Base class for Database Field Types (see field/*/ global $DB; if (!empty($this->field->id)) { - $DB->delete_records('data_fields', array('id'=>$this->field->id)); $this->delete_content(); + $DB->delete_records('data_fields', array('id'=>$this->field->id)); } return true; } @@ -268,30 +278,23 @@ class data_field_base { // Base class for Database Field Types (see field/*/ function delete_content($recordid=0) { global $DB; - $this->delete_content_files($recordid); - if ($recordid) { - return $DB->delete_records('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)); + $conditions = array('fieldid'=>$this->field->id, 'recordid'=>$recordid); } else { - return $DB->delete_records('data_content', array('fieldid'=>$this->field->id)); + $conditions = array('fieldid'=>$this->field->id); } - } - -// Deletes any files associated with this field - function delete_content_files($recordid='') { - global $CFG; - - require_once($CFG->libdir.'/filelib.php'); - $dir = $CFG->dataroot.'/'.$this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id; - if ($recordid) { - $dir .= '/'.$recordid; + if ($rs = $DB->get_recordset('data_content', $conditions)) { + $fs = get_file_storage(); + foreach ($rs as $content) { + $fs->delete_area_files($this->context->id, 'data_content', $content->id); + } + $rs->close(); } - return fulldelete($dir); + return $DB->delete_records('data_content', $conditions); } - // Check if a field from an add form is empty function notemptyfield($value, $name) { return !empty($value); @@ -332,18 +335,21 @@ class data_field_base { // Base class for Database Field Types (see field/*/ return $str; } -// Per default, it is assumed that fields support text exporting. Override this (return false) on fields not supporting text exporting. +// Per default, it is assumed that fields support text exporting. Override this (return false) on fields not supporting text exporting. function text_export_supported() { return true; } -// Per default, return the record's text value only from the "content" field. Override this in fields class if necesarry. +// Per default, return the record's text value only from the "content" field. Override this in fields class if necesarry. function export_text_value($record) { if ($this->text_export_supported()) { return $record->content; } } + function file_ok($relativepath) { + return false; + } } @@ -379,7 +385,7 @@ function data_generate_default_template(&$data, $template, $recordid=0, $form=fa // $str .= ' for="[['.$field->name.'#id]]"'; //} //$str .= '>'.$field->name.''; - + //} else { $str .= $field->name.': '; //} @@ -546,35 +552,37 @@ function data_get_field_new($type, $data) { * invoke plugin methods * * input: $param $field - record from db * ************************************************************************/ -function data_get_field($field, $data) { +function data_get_field($field, $data, $cm=null) { global $CFG; if ($field) { require_once('field/'.$field->type.'/field.class.php'); $newfield = 'data_field_'.$field->type; - $newfield = new $newfield($field, $data); + $newfield = new $newfield($field, $data, $cm); return $newfield; } } -/*************************************************************************** - * given record id, returns true if the record belongs to the current user * - * input @param $rid - record id * - * output bool * - ***************************************************************************/ -function data_isowner($rid){ +/** + * Given record object (or id), returns true if the record belongs to the current user + * @param mixed $rid - record object or id + * @return bool + */ +function data_isowner($record) { global $USER, $DB; - if (empty($USER->id)) { + if (!isloggedin()) { return false; } - if ($record = $DB->get_record('data_records', array('id'=>$rid))) { - return ($record->userid == $USER->id); + if (!is_object($record)) { + if (!$record = $DB->get_record('data_records', array('id'=>$record))) { + return false; + } } - return false; + return ($record->userid == $USER->id); } /*********************************************************************** @@ -598,7 +606,7 @@ function data_atmaxentries($data){ * output int * **********************************************************************/ function data_numentries($data){ - global $USER, $CFG, $DB; + global $USER, $DB; $sql = 'SELECT COUNT(*) FROM {data_records} WHERE dataid=? AND userid=?'; return $DB->count_records_sql($sql, array($data->id, $USER->id)); } @@ -659,7 +667,7 @@ function data_tags_check($dataid, $template) { * Adds an instance of a data * ************************************************************************/ function data_add_instance($data) { - global $CFG, $DB; + global $DB; if (empty($data->assessed)) { $data->assessed = 0; @@ -680,7 +688,7 @@ function data_add_instance($data) { * updates an instance of a data * ************************************************************************/ function data_update_instance($data) { - global $CFG, $DB; + global $DB; $data->timemodified = time(); $data->id = $data->instance; @@ -707,38 +715,36 @@ function data_update_instance($data) { * deletes an instance of a data * ************************************************************************/ function data_delete_instance($id) { // takes the dataid - global $CFG, $DB; + global $DB; - if (! $data = $DB->get_record('data', array('id'=>$id))) { + if (!$data = $DB->get_record('data', array('id'=>$id))) { return false; } - // Delete all the associated information + $cm = get_coursemodule_from_instance('data', $data->id); + $context = get_context_instance(CONTEXT_MODULE, $cm->id); - // get all the records in this data - $sql = 'SELECT c.* FROM {data_records} r LEFT JOIN {data_content} c ON c.recordid = r.id WHERE r.dataid =?'; +/// Delete all the associated information - if ($contents = $DB->get_records_sql($sql, array($id))){ + // files + $fs = get_file_storage(); + $fs->delete_area_files($context->id, 'data_content'); - foreach($contents as $content){ + // get all the records in this data + $sql = "SELECT r.id + FROM {data_records} r + WHERE r.dataid = ?"; - $field = $DB->get_record('data_fields', array('id'=>$content->fieldid)); - if ($g = data_get_field($field, $data)){ - $g->delete_content_files($id, $content->recordid, $content->content); - } - //delete the content itself - $DB->delete_records('data_content', array('id'=>$content->id)); - } - } + $DB->delete_records_select('data_content', "recordid IN ($sql)", array($id)); // delete all the records and fields $DB->delete_records('data_records', array('dataid'=>$id)); $DB->delete_records('data_fields', array('dataid'=>$id)); // Delete the instance itself - $result = $DB->delete_records('data', array('id'=>$id)); + // cleanup gradebook data_grade_item_delete($data); return $result; @@ -748,7 +754,7 @@ function data_delete_instance($id) { // takes the dataid * returns a summary of data activity of this user * ************************************************************************/ function data_user_outline($course, $user, $mod, $data) { - global $CFG, $DB; + global $DB; if ($countrecords = $DB->count_records('data_records', array('dataid'=>$data->id, 'userid'=>$user->id))) { $result = new object(); @@ -781,7 +787,7 @@ function data_user_complete($course, $user, $mod, $data) { * @return array array of grades, false if none */ function data_get_user_grades($data, $userid=0) { - global $CFG, $DB; + global $DB; $user = $userid ? "AND u.id = :userid" : ""; $params = array('userid'=>$userid, 'dataid'=>$data->id); @@ -908,7 +914,7 @@ function data_grade_item_delete($data) { function data_get_participants($dataid) { // Returns the users with data in one data // (users with records in data_records, data_comments and data_ratings) - global $CFG, $DB; + global $DB; $records = $DB->get_records_sql("SELECT DISTINCT u.id, u.id FROM {user} u, {data_records} r @@ -924,17 +930,17 @@ function data_get_participants($dataid) { $participants = array(); - if ($records){ + if ($records) { foreach ($records as $record) { $participants[$record->id] = $record; } } - if ($comments){ + if ($comments) { foreach ($comments as $comment) { $participants[$comment->id] = $comment; } } - if ($ratings){ + if ($ratings) { foreach ($ratings as $rating) { $participants[$rating->id] = $rating; } @@ -1027,9 +1033,9 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re } else { $replacement[] = ''; } - + $patterns[] = '##timeadded##'; - $replacement[] = userdate($record->timecreated); + $replacement[] = userdate($record->timecreated); $patterns[] = '##timemodified##'; $replacement [] = userdate($record->timemodified); @@ -1093,7 +1099,7 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re ************************************************************************/ function data_print_preference_form($data, $perpage, $search, $sort='', $order='ASC', $search_array = '', $advanced = 0, $mode= ''){ global $CFG, $DB; - + $cm = get_coursemodule_from_instance('data', $data->id); $context = get_context_instance(CONTEXT_MODULE, $cm->id); echo '
'; @@ -1169,7 +1175,7 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order=' $checked = ''; } print ' - +