/**
  * Validate zip file name construction for logging emails related to an
  * import plugin
  */
 public function test_getemailarchivefilenameformanual()
 {
     $time = time();
     $zipname = rlip_email_archive_name('dhimport_version1', $time, true);
     $datedisplay = date('M_d_Y_His', $time);
     $expectedfilename = 'import_version1_manual_' . $datedisplay . '.zip';
     $this->assertEquals($zipname, $expectedfilename);
 }
Exemple #2
0
/**
 * Compress logs for emailing
 *
 * @param string $plugin The plugin for which we are sending logs
 * @param array $logids The list of database record ids pointing to log files
 * @param boolean $manual True if manual, false if scheduled
 * @return string The name of the appropriate zip file
 */
function rlip_compress_logs_email($plugin, $logids, $manual = false)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/filestorage/zip_archive.php';
    if (empty($logids)) {
        // Nothing to compress.
        return false;
    }
    // Set up the archive.
    $archive_name = rlip_email_archive_name($plugin, 0, $manual);
    $path = $CFG->dataroot . '/' . $archive_name;
    $archive = new zip_archive();
    $result = $archive->open($path, file_archive::CREATE);
    // SQL fragments to get the logs.
    list($sql, $params) = $DB->get_in_or_equal($logids);
    $select = "id {$sql}";
    // Add files from log records, tracking whether a valid log path was found.
    $found = false;
    if ($records = $DB->get_records_select(RLIP_LOG_TABLE, $select, $params)) {
        foreach ($records as $record) {
            if ($record->logpath != NULL) {
                $archive->add_file_from_pathname(basename($record->logpath), $record->logpath);
                // Have at least one file in the zip.
                $found = true;
            }
        }
    }
    $archive->close();
    if (!$found) {
        // No logs, so delete the empty archive file and signal that we don't need to send the email.
        if (file_exists($path)) {
            @unlink($path);
        }
        return false;
    }
    return $archive_name;
}