diff --git a/db/upgrade.php b/db/upgrade.php index 89e5c86e..13cdb93d 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -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; } diff --git a/email.php b/email.php index 93122590..4cca0293 100644 --- a/email.php +++ b/email.php @@ -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)) { @@ -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; } } diff --git a/emaillog.php b/emaillog.php index e47a82cf..54968192 100644 --- a/emaillog.php +++ b/emaillog.php @@ -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 diff --git a/lib.php b/lib.php index f7cd3091..524d955d 100644 --- a/lib.php +++ b/lib.php @@ -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) { @@ -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' ); diff --git a/version.php b/version.php index f17b8e14..08b31020 100644 --- a/version.php +++ b/version.php @@ -2,4 +2,4 @@ // Written at Louisiana State University -$plugin->version = 2012021014; +$plugin->version = 2012061112;