Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Fixes #40: Attachment filearea refactored;improved
Browse files Browse the repository at this point in the history
  • Loading branch information
philcali committed Jun 11, 2012
1 parent ed83d36 commit 048a1cf
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
21 changes: 21 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,26 @@ function xmldb_block_quickmail_upgrade($oldversion) {
upgrade_block_savepoint($result, 2012021014, 'quickmail');
}

if ($oldversion < 2012061112) {
// Restructure database references to the new filearea locations
foreach (array('log', 'drafts') as $type) {
$params = array(
'component' => 'block_quickmail_' . $type,
'filearea' => 'attachment'
);

$attachments = $DB->get_records('files', $params);

foreach ($attachments as $attachment) {
$attachment->filearea = 'attachment_' . $type;
$attachment->component = 'block_quickmail';

$result = $result && $DB->update_record('files', $attachment);
}
}

upgrade_block_savepoint($result, 2012061112, 'quickmail');
}

return $result;
}
7 changes: 5 additions & 2 deletions email.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@

// An instance id is needed before storing the file repository
file_save_draft_area_files($data->attachments, $context->id,
'block_quickmail_'.$table, 'attachment', $data->id);
'block_quickmail', 'attachment_' . $table, $data->id);

// Send emails
if (isset($data->send)) {
Expand Down Expand Up @@ -283,7 +283,10 @@
if (empty($email->attachments)) {
if(!empty($type)) {
$attachid = file_get_submitted_draft_itemid('attachment');
file_prepare_draft_area($attachid, $context->id, 'block_quickmail_'.$type, 'attachment', $typeid);
file_prepare_draft_area(
$attachid, $context->id, 'block_quickmail',
'attachment_' . $type, $typeid
);
$email->attachments = $attachid;
}
}
Expand Down
2 changes: 1 addition & 1 deletion emaillog.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

switch ($action) {
case "confirm":
if(quickmail::cleanup($dbtable, $typeid)) {
if (quickmail::cleanup($dbtable, $context->id, $typeid)) {
$url = new moodle_url('/blocks/quickmail/emaillog.php', array(
'courseid' => $courseid,
'type' => $type
Expand Down
31 changes: 19 additions & 12 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,35 @@ static function format_time($time) {
return userdate($time, '%A, %d %B %Y, %I:%M %P');
}

static function cleanup($table, $itemid) {
static function cleanup($table, $contextid, $itemid) {
global $DB;

// Clean up the files associated with this email
// Fortunately, they are only db references, but
// they shouldn't be there, nonetheless.
$params = array('component' => $table, 'itemid' => $itemid);
$filearea = end(explode('_', $table));

$result = (
$DB->delete_records('files', $params) and
$DB->delete_records($table, array('id' => $itemid))
$fs = get_file_storage();

$fs->delete_area_files(
$contextid, 'block_quickmail',
'attachment_' . $filearea, $itemid
);

$fs->delete_area_files(
$contextid, 'block_quickmail',
$filearea, $itemid
);

return $result;
return $DB->delete_records($table, array('id' => $itemid));
}

static function history_cleanup($itemid) {
return quickmail::cleanup('block_quickmail_log', $itemid);
static function history_cleanup($contextid, $itemid) {
return quickmail::cleanup('block_quickmail_log', $contextid, $itemid);
}

static function draft_cleanup($itemid) {
return quickmail::cleanup('block_quickmail_drafts', $itemid);
static function draft_cleanup($contextid, $itemid) {
return quickmail::cleanup('block_quickmail_drafts', $contextid, $itemid);
}

static function process_attachments($context, $email, $table, $id) {
Expand All @@ -59,8 +66,8 @@ static function process_attachments($context, $email, $table, $id) {

$files = $fs->get_area_files(
$context->id,
'block_quickmail_'.$table,
'attachment',
'block_quickmail',
'attachment_' . $table,
$id,
'id'
);
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

// Written at Louisiana State University

$plugin->version = 2012021014;
$plugin->version = 2012061112;

0 comments on commit 048a1cf

Please sign in to comment.