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

Backing up and restoring of email history #39

Merged
merged 8 commits into from
Jun 21, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
## v1.2.8

- Fixes broken delete link for admins [4166c8][4166c8]
- Improved attachment filearea [#40][40]
- Type needs to be included in pagination [3333e6][3333e6]
- Fix upgrade failure point [#41][41] ([mackensen][mackensen])
- Use correct user time in history and drafts [#42][42]

[4166c8]: https://github.com/lsuits/quickmail/commit/4166c828d531e4ef2538fbae2f156c49bb627cdb
[3333e6]: https://github.com/lsuits/quickmail/commit/3333e643606947254b5cb1cdf5beeb33b7ea1bb7
[40]: https://github.com/lsuits/quickmail/issues/40
[41]: https://github.com/lsuits/quickmail/pull/41
Expand Down
32 changes: 32 additions & 0 deletions backup/moodle2/backup_quickmail_block_task.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

require_once $CFG->dirroot . '/blocks/quickmail/backup/moodle2/backup_quickmail_stepslib.php';

class backup_quickmail_block_task extends backup_block_task {
protected function define_my_settings() {
$include_history = new backup_generic_setting('include_quickmail_log', base_setting::IS_BOOLEAN, true);
$include_history->get_ui()->set_label(get_string('backup_history', 'block_quickmail'));
$this->add_setting($include_history);

$this->plan->get_setting('users')->add_dependency($include_history);
$this->plan->get_setting('blocks')->add_dependency($include_history);
}

protected function define_my_steps() {
// TODO: additional steps for drafts and alternate emails
$this->add_step(new backup_quickmail_block_structure_step('quickmail_structure', 'emaillogs.xml'));
}

public function get_fileareas() {
return array();
}

public function get_configdata_encoded_attributes() {
return array();
}

static public function encode_content_links($content) {
// TODO: perhaps needing this when moving away from email zip attaches
return $content;
}
}
38 changes: 38 additions & 0 deletions backup/moodle2/backup_quickmail_stepslib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

class backup_quickmail_block_structure_step extends backup_block_structure_step {
protected function define_structure() {
global $DB;

$params = array('courseid' => $this->get_courseid());
$context = get_context_instance(CONTEXT_COURSE, $params['courseid']);

$quickmail_logs = $DB->get_records('block_quickmail_log', $params);
$include_history = $this->get_setting_value('include_quickmail_log');

$backup_logs = new backup_nested_element('emaillogs', array('courseid'), null);

$log = new backup_nested_element('log', array('id'), array(
'userid', 'courseid', 'alternateid', 'mailto', 'subject',
'message', 'attachment', 'format', 'time'
));

$backup_logs->add_child($log);

$backup_logs->set_source_array(array((object)$params));

if (!empty($quickmail_logs) and $include_history) {
$log->set_source_sql(
'SELECT * FROM {block_quickmail_log}
WHERE courseid = ?', array(array('sqlparam' => $this->get_courseid()))
);
}

$log->annotate_ids('user', 'userid');

$log->annotate_files('block_quickmail', 'log', 'id', $context->id);
$log->annotate_files('block_quickmail', 'attachment_log', 'id', $context->id);

return $this->prepare_block_structure($backup_logs);
}
}
74 changes: 74 additions & 0 deletions backup/moodle2/restore_quickmail_block_task.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

require_once $CFG->dirroot . '/blocks/quickmail/backup/moodle2/restore_quickmail_stepslib.php';

class restore_quickmail_block_task extends restore_block_task {
protected function define_my_settings() {
$rootsettings = $this->get_info()->root_settings;

$defaultvalue = false;
$changeable = false;

$is_blocks = isset($rootsettings['blocks']) && $rootsettings['blocks'];
$is_users = isset($rootsettings['users']) && $rootsettings['users'];

if ($is_blocks and $is_users) {
$defaultvalue = true;
$changeable = true;
}

$restore_history = new restore_generic_setting('restore_quickmail_history',
base_setting::IS_BOOLEAN, $defaultvalue);
$restore_history->set_ui(new backup_setting_ui_select(
$restore_history, get_string('restore_history', 'block_quickmail'),
array(1 => get_string('yes'), 0 => get_string('no'))
));

if (!$changeable) {
$restore_history->set_value($defaultvalue);
$restore_history->set_status(backup_setting::LOCKED_BY_CONFIG);
$restore_history->set_visibility(backup_setting::HIDDEN);
}

$this->add_setting($restore_history);
$this->get_setting('users')->add_dependency($restore_history);
$this->get_setting('blocks')->add_dependency($restore_history);

$overwrite_history = new restore_course_generic_setting('overwrite_quickmail_history', base_setting::IS_BOOLEAN, false);
$overwrite_history->set_ui(new backup_setting_ui_select(
$overwrite_history,
get_string('overwrite_history', 'block_quickmail'),
array(1 => get_string('yes'), 0 => get_string('no'))
));

if ($this->get_target() != backup::TARGET_CURRENT_DELETING and $this->get_target() != backup::TARGET_EXISTING_DELETING) {

$overwrite_history->set_value(false);
$overwrite_history->set_status(backup_setting::LOCKED_BY_CONFIG);
}

$this->add_setting($overwrite_history);
$restore_history->add_dependency($overwrite_history);
}

protected function define_my_steps() {
$this->add_step(new restore_quickmail_log_structure_step('quickmail_structure', 'emaillogs.xml'));
}

public function get_fileareas() {
return array();
}

public function get_configdata_encoded_attributes() {
return array();
}

static public function define_decode_contents() {
// TODO: perhaps needing this when moving away from email zip attaches
return array();
}

static public function define_decode_rules() {
return array();
}
}
87 changes: 87 additions & 0 deletions backup/moodle2/restore_quickmail_stepslib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

class restore_quickmail_log_structure_step extends restore_structure_step {
protected function define_structure() {
$paths = array();

$paths[] = new restore_path_element('block', '/block', true);
$paths[] = new restore_path_element('log', '/block/emaillogs/log');

return $paths;
}

protected function process_block($data) {
global $DB;

$data = (object) $data;

$restore = $this->get_setting_value('restore_quickmail_history');
$overwrite = $this->get_setting_value('overwrite_quickmail_history');

// Delete current history, if any
if ($overwrite) {
$params = array('courseid' => $this->get_courseid());
$DB->delete_records('block_quickmail_log', $params);
}

if ($restore and isset($data->emaillogs['log'])) {
global $DB;

$current = get_context_instance(CONTEXT_COURSE, $this->get_courseid());

$params = array(
'backupid' => $this->get_restoreid(),
'itemname' => 'context',
'newitemid' => $current->id
);

$id = $DB->get_record('backup_ids_temp', $params)->itemid;

foreach ($data->emaillogs['log'] as $log) {
$this->process_log($log, $id, $current);
}
}
}

protected function process_log($log, $oldctx, $context) {
global $DB;

$log = (object) $log;
$oldid = $log->id;

$mailedusers = explode(',', $log->mailto);
$validusers = array();

foreach ($mailedusers as $userid) {
$validusers[] = $this->get_mappingid('user', $userid);
}

$log->courseid = $this->get_courseid();
$log->userid = $this->get_mappingid('user', $log->userid);
$log->mailto = implode(',', $validusers);
$log->time = $this->apply_date_offset($log->time);

// TODO: correctly convert alternate ids
$log->alternateid = null;

$newid = $DB->insert_record('block_quickmail_log', $log);

$this->set_mapping('log', $oldid, $newid);

foreach (array('log', 'attachment_log') as $filearea) {
restore_dbops::send_files_to_pool(
$this->get_basepath(), $this->get_restoreid(),
'block_quickmail', $filearea, $oldctx, $log->userid
);

$sql = 'UPDATE {files} SET
itemid = :newid WHERE contextid = :ctxt AND itemid = :oldid';

$params = array(
'newid' => $newid, 'oldid' => $oldid, 'ctxt' => $context->id
);

$DB->execute($sql, $params);
}
}
}
3 changes: 3 additions & 0 deletions lang/en/block_quickmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
$string['quickmail:canconfig'] = "Allows users to configure Quickmail instance.";
$string['quickmail:canimpersonate'] = "Allows users to log in as other users and view history.";
$string['quickmail:allowalternate'] = "Allows users to add an alternate email for courses.";
$string['backup_history'] = 'Include Quickmail History';
$string['restore_history'] = 'Restore Quickmail History';
$string['overwrite_history'] = 'Overwrite Quickmail History';
$string['alternate'] = 'Alternate Emails';
$string['composenew'] = 'Compose New Email';
$string['email'] = 'Email';
Expand Down
9 changes: 6 additions & 3 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@ function list_entries($courseid, $type, $page, $perpage, $userid, $count, $can_d
$actions[] = $open_link;

if ($can_delete) {
$delete_params = $params + array(
'userid' => $userid,
'action' => 'delete'
);

$delete_link = html_writer::link (
new moodle_url('/blocks/quickmail/emaillog.php',
$params + array('action' => 'delete')
),
new moodle_url('/blocks/quickmail/emaillog.php', $delete_params),
$OUTPUT->pix_icon("i/cross_red_big", "Delete Email")
);

Expand Down