Skip to content

Commit

Permalink
MDL-14589 initial file storage implementation, temporary file manager…
Browse files Browse the repository at this point in the history
…, migration of course files; blog conversion MDL-15905; assignment conversion MDL-15904; fromslib related file improvements MDL-15906
  • Loading branch information
skodak committed Jul 31, 2008
1 parent 1e908ab commit 172dd12
Show file tree
Hide file tree
Showing 48 changed files with 3,559 additions and 1,510 deletions.
2 changes: 1 addition & 1 deletion admin/roles/manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
case 'add':
if ($data = data_submitted() and confirm_sesskey()) {

$shortname = moodle_strtolower(clean_param(clean_filename($shortname), PARAM_SAFEDIR)); // only lowercase safe ASCII characters
$shortname = moodle_strtolower(clean_filename($shortname)); // only lowercase safe ASCII characters
$legacytype = required_param('legacytype', PARAM_RAW);

$legacyroles = get_legacy_roles();
Expand Down
8 changes: 4 additions & 4 deletions admin/uploadpicture.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@

// Create a unique temporary directory, to process the zip file
// contents.
$zipdir = my_mktempdir($CFG->dataroot.'/temp/', 'usrpic');
$zipodir = my_mktempdir($CFG->dataroot.'/temp/', 'usrpic');
$dstfile = $zipodir.'/images.zip';

if (!$mform->save_files($zipdir)) {
if (!$mform->save_file('userfile', $dstfile, true)) {
notify(get_string('uploadpicture_cannotmovezip','admin'));
@remove_dir($zipdir);
} else {
$dstfile = $zipdir.'/'.$mform->get_new_filename();
if(!unzip_file($dstfile, $zipdir, false)) {
if (!unzip_file($dstfile, $zipdir, false)) {
notify(get_string('uploadpicture_cannotunzip','admin'));
@remove_dir($zipdir);
} else {
Expand Down
22 changes: 14 additions & 8 deletions blog/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@
function do_delete($post) {
global $returnurl, $DB;

blog_delete_attachments($post);

$status = $DB->delete_records('post', array('id'=>$post->id));
tag_set('post', $post->id, array());

blog_delete_old_attachments($post);

add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);

if (!$status) {
Expand All @@ -182,10 +182,12 @@ function do_add($post, $blogeditform) {
if ($id = $DB->insert_record('post', $post)) {
$post->id = $id;
// add blog attachment
$dir = blog_file_area_name($post);
if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
$DB->set_field("post", "attachment", $newfilename, array("id"=>$post->id));
if ($blogeditform->get_new_filename('attachment')) {
if ($blogeditform->save_stored_file('attachment', SYSCONTEXTID, 'blog', $post->id, '/', false, $USER->id)) {
$DB->set_field("post", "attachment", 1, array("id"=>$post->id));
}
}

add_tags_info($post->id);
add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);

Expand All @@ -205,9 +207,13 @@ function do_edit($post, $blogeditform) {

$post->lastmodified = time();

$dir = blog_file_area_name($post);
if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
$post->attachment = $newfilename;
if ($blogeditform->get_new_filename('attachment')) {
blog_delete_attachments($post);
if ($blogeditform->save_stored_file('attachment', SYSCONTEXTID, 'blog', $post->id, '/', false, $USER->id)) {
$post->attachment = 1;
} else {
$post->attachment = 1;
}
}

// update record
Expand Down
3 changes: 0 additions & 3 deletions blog/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ function definition() {
$post = $this->_customdata['existing'];
$sitecontext = $this->_customdata['sitecontext'];

// the upload manager is used directly in entry processing, moodleform::save_files() is not used yet
$this->set_upload_manager(new upload_manager('attachment', true, false, $COURSE, false, 0, true, true, false));

$mform->addElement('header', 'general', get_string('general', 'form'));
$mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), 'size="60"');
$mform->setType('subject', PARAM_TEXT);
Expand Down
90 changes: 37 additions & 53 deletions blog/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filters
if($blogEntry->created != $blogEntry->lastmodified){
$template['lastmod'] = userdate($blogEntry->lastmodified);
}

$template['publishstate'] = $blogEntry->publishstate;

/// preventing user to browse blogs that they aren't supposed to see
Expand Down Expand Up @@ -232,7 +232,7 @@ function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filters
print(get_string('tags', 'tag') .': '. $blogtags);
}
echo '</div>';
}
}

/// Commands

Expand All @@ -257,36 +257,12 @@ function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filters

}

/**
* Creates a directory file name, suitable for make_upload_directory()
* $CFG->dataroot/blog/attachments/xxxx/file.jpg
*/
function blog_file_area_name($blogentry) {
return "blog/attachments/$blogentry->id";
}

function blog_file_area($blogentry) {
return make_upload_directory( blog_file_area_name($blogentry) );
}

/**
* Deletes all the user files in the attachments area for a post
* EXCEPT for any file named $exception
*/
function blog_delete_old_attachments($post, $exception="") {
if ($basedir = blog_file_area($post)) {
if ($files = get_directory_list($basedir)) {
foreach ($files as $file) {
if ($file != $exception) {
unlink("$basedir/$file");
notify("Existing file '$file' has been deleted!");
}
}
}
if (!$exception) { // Delete directory as well, if empty
rmdir("$basedir");
}
}
function blog_delete_attachments($post) {
$fs = get_file_storage();
$fs->delete_area_files(SYSCONTEXTID, 'blog', $post->id);
}

/**
Expand All @@ -297,36 +273,44 @@ function blog_delete_old_attachments($post, $exception="") {
function blog_print_attachments($blogentry, $return=NULL) {
global $CFG;

$filearea = blog_file_area_name($blogentry);
require_once($CFG->libdir.'/filelib.php');

$fs = get_file_storage();
$browser = get_file_browser();

$files = $fs->get_area_files(SYSCONTEXTID, 'blog', $blogentry->id);

$imagereturn = "";
$output = "";

if ($basedir = blog_file_area($blogentry)) {
if ($files = get_directory_list($basedir)) {
$strattachment = get_string("attachment", "forum");
foreach ($files as $file) {
include_once($CFG->libdir.'/filelib.php');
$icon = mimeinfo("icon", $file);
$type = mimeinfo("type", $file);
$ffurl = get_file_url("$filearea/$file");
$image = "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"\" />";
$strattachment = get_string("attachment", "forum");

if ($return == "html") {
$output .= "<a href=\"$ffurl\">$image</a> ";
$output .= "<a href=\"$ffurl\">$file</a><br />";
foreach ($files as $file) {
if ($file->is_directory()) {
continue;
}

} else if ($return == "text") {
$output .= "$strattachment $file:\n$ffurl\n";
$filename = $file->get_filename();
$ffurl = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/'.$blogentry->id.'/'.$filename);
$type = $file->get_mimetype();
$icon = mimeinfo_from_type("icon", $type);
$type = mimeinfo_from_type("type", $type);

} else {
if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) { // Image attachments don't get printed as links
$imagereturn .= "<br /><img src=\"$ffurl\" alt=\"\" />";
} else {
echo "<a href=\"$ffurl\">$image</a> ";
echo filter_text("<a href=\"$ffurl\">$file</a><br />");
}
}
$image = "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"\" />";

if ($return == "html") {
$output .= "<a href=\"$ffurl\">$image</a> ";
$output .= "<a href=\"$ffurl\">$filename</a><br />";

} else if ($return == "text") {
$output .= "$strattachment $filename:\n$ffurl\n";

} else {
if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) { // Image attachments don't get printed as links
$imagereturn .= "<br /><img src=\"$ffurl\" alt=\"\" />";
} else {
echo "<a href=\"$ffurl\">$image</a> ";
echo filter_text("<a href=\"$ffurl\">$filename</a><br />");
}
}
}
Expand Down Expand Up @@ -699,7 +683,7 @@ function get_baseurl($filtertype, $filterselect) {
function blog_get_participants() {
global $CFG, $DB;

return $DB->get_records_sql("SELECT userid AS id
return $DB->get_records_sql("SELECT userid AS id
FROM {post}
WHERE module = 'blog' AND courseid = 0");
}
Expand Down
7 changes: 0 additions & 7 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,6 @@
// $CFG->defaultblocks = 'participants,activity_modules,search_forums,admin,course_list:news_items,calendar_upcoming,recent_activity';
//
//
// Allow unicode characters in uploaded files, generated reports, etc.
// This setting is new and not much tested, there are known problems
// with backup/restore that will not be solved, because native infozip
// binaries are doing some weird conversions - use internal PHP zipping instead.
// NOT RECOMMENDED FOR PRODUCTION SITES
// $CFG->unicodecleanfilename = true;
//
// Seconds for files to remain in caches. Decrease this if you are worried
// about students being served outdated versions of uploaded files.
// $CFG->filelifetime = 86400;
Expand Down
1 change: 1 addition & 0 deletions draftfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php //$Id$
Loading

0 comments on commit 172dd12

Please sign in to comment.