Beispiel #1
0
/**
 * Create a zip archive containing the exported data.
 *
 * @param array $listing The list of usernames that were exported
 * @param array $files A list of archive files for each user
 */
function create_zipfile($listing, $files)
{
    global $USER;
    if (empty($listing) or empty($files)) {
        return false;
    }
    if (count($listing) != count($files)) {
        throw new MaharaException("Files and listing don't match.");
    }
    // create temporary directories for the export
    $exportdir = get_config('dataroot') . 'export/' . $USER->get('id') . '/' . time() . '/';
    if (!check_dir_exists($exportdir)) {
        throw new SystemException("Couldn't create the temporary export directory {$exportdir}");
    }
    $usersdir = 'users/';
    if (!check_dir_exists($exportdir . $usersdir)) {
        throw new SystemException("Couldn't create the temporary export directory {$usersdir}");
    }
    // move user zipfiles into the export directory
    foreach ($files as $filename) {
        if (copy($filename, $exportdir . $usersdir . basename($filename))) {
            unlink($filename);
        } else {
            throw new SystemException("Couldn't move {$filename} to {$usersdir}");
        }
    }
    // write username listing to a file
    $listingfile = 'usernames.csv';
    if (!file_put_contents($exportdir . $listingfile, data_to_csv($listing))) {
        throw new SystemException("Couldn't write usernames to a file");
    }
    // zip everything up
    $filename = 'mahara-bulk-export-' . time() . '.zip';
    try {
        create_zip_archive($exportdir, $filename, array($listingfile, $usersdir));
    } catch (SystemException $e) {
        throw new SystemException('Failed to zip the export file: ' . $e->getMessage());
    }
    return $exportdir . $filename;
}
Beispiel #2
0
 /**
  * Main export routine
  */
 public function export()
 {
     global $THEME, $SESSION;
     raise_memory_limit('128M');
     $summaries = array();
     $plugins = plugins_installed('artefact', true);
     $exportplugins = array();
     $progressstart = 15;
     $progressend = 25;
     $plugincount = count($plugins);
     // First pass: find out which plugins are exporting like us, and ask
     // them about the static data they want to include in every template
     $i = 0;
     foreach ($plugins as $plugin) {
         $plugin = $plugin->name;
         $this->notify_progress_callback(intval($progressstart + ++$i / $plugincount * ($progressend - $progressstart)), get_string('preparing', 'export.html', $plugin));
         if (safe_require('export', 'html/' . $plugin, 'lib.php', 'require_once', true)) {
             $exportplugins[] = $plugin;
             $classname = 'HtmlExport' . ucfirst($plugin);
             if (!is_subclass_of($classname, 'HtmlExportArtefactPlugin')) {
                 throw new SystemException("Class {$classname} does not extend HtmlExportArtefactPlugin as it should");
             }
             safe_require('artefact', $plugin);
             // Find out whether the plugin has static data for us
             $themestaticdirs = array_reverse($THEME->get_path('', true, 'artefact/' . $plugin . '/export/html'));
             foreach ($themestaticdirs as $dir) {
                 $staticdir = substr($dir, strlen(get_config('docroot') . 'artefact/'));
                 $this->pluginstaticdirs[] = $staticdir;
                 foreach (array('style.css') as $stylesheet) {
                     if (is_readable($dir . 'style/' . $stylesheet)) {
                         $this->stylesheets[$plugin][] = str_replace('export/html/', '', $staticdir) . 'style/' . $stylesheet;
                     }
                 }
             }
         }
     }
     // Second pass: actually dump data for active export plugins
     $progressstart = 25;
     $progressend = 50;
     $i = 0;
     foreach ($exportplugins as $plugin) {
         $this->notify_progress_callback(intval($progressstart + ++$i / $plugincount * ($progressend - $progressstart)), get_string('exportingdatafor', 'export.html', $plugin));
         $classname = 'HtmlExport' . ucfirst($plugin);
         $artefactexporter = new $classname($this);
         $artefactexporter->dump_export_data();
         // If just exporting a list of views, we don't care about the summaries for each artefact plugin
         if (!(($this->viewexportmode == PluginExport::EXPORT_LIST_OF_VIEWS || $this->viewexportmode == PluginExport::EXPORT_COLLECTIONS) && $this->artefactexportmode == PluginExport::EXPORT_ARTEFACTS_FOR_VIEWS)) {
             $summaries[$plugin] = array($artefactexporter->get_summary_weight(), $artefactexporter->get_summary());
         }
     }
     // Views in collections
     if (!$this->exportingoneview && $this->collections) {
         $viewlist = join(',', array_keys($this->views));
         $collectionlist = join(',', array_keys($this->collections));
         $records = get_records_select_array('collection_view', "view IN ({$viewlist}) AND collection IN ({$collectionlist})");
         if ($records) {
             foreach ($records as &$r) {
                 $this->collectionview[$r->collection][] = $r->view;
                 $this->viewcollection[$r->view] = $r->collection;
             }
         }
     }
     // Get the view data
     $this->notify_progress_callback(55, get_string('exportingviews', 'export'));
     $this->dump_view_export_data();
     if (!$this->exportingoneview) {
         $summaries['view'] = array(100, $this->get_view_summary());
         // Sort by weight (then drop the weight information)
         $this->notify_progress_callback(75, get_string('buildingindexpage', 'export.html'));
         uasort($summaries, create_function('$a, $b', 'return $a[0] > $b[0];'));
         foreach ($summaries as &$summary) {
             $summary = $summary[1];
         }
         // Build index.html
         $this->build_index_page($summaries);
     }
     // Copy all static files into the export
     $this->notify_progress_callback(80, get_string('copyingextrafiles', 'export.html'));
     $this->copy_static_files();
     // Copy all resized images that were found while rewriting the HTML
     $copyproxy = HtmlExportCopyProxy::singleton();
     $copydata = $copyproxy->get_copy_data();
     foreach ($copydata as $from => $to) {
         $to = $this->get('exportdir') . '/' . $this->get('rootdir') . $to;
         if (!check_dir_exists(dirname($to))) {
             $SESSION->add_error_msg(get_string('couldnotcreatedirectory', 'export', $todir));
         }
         if (!copy($from, $to)) {
             $SESSION->add_error_msg(get_string('couldnotcopystaticfile', 'export', $from));
         }
     }
     // zip everything up
     $this->notify_progress_callback(90, get_string('creatingzipfile', 'export'));
     try {
         create_zip_archive($this->exportdir, $this->zipfile, array($this->rootdir));
     } catch (SystemException $e) {
         throw new SystemException('Failed to zip the export file: ' . $e->getMessage());
     }
     $this->notify_progress_callback(100, get_string('Done', 'export'));
     return $this->zipfile;
 }
Beispiel #3
0
 /**
  * main export routine
  */
 public function export()
 {
     global $SESSION;
     // the xml stuff
     $this->export_header();
     $this->setup_links();
     $this->notify_progress_callback(10, get_string('exportingviews', 'export'));
     if ($this->viewexportmode == PluginExport::EXPORT_LIST_OF_COLLECTIONS || $this->viewexportmode == PluginExport::EXPORT_ALL_VIEWS_COLLECTIONS) {
         $this->export_collections();
     }
     $this->export_views();
     $this->notify_progress_callback(50, get_string('exportingartefacts', 'export'));
     $this->export_artefacts();
     $this->notify_progress_callback(80, get_string('exportingartefactplugindata', 'export'));
     $internal = null;
     foreach ($this->specialcases as $plugin => $artefacts) {
         if ($plugin == 'internal') {
             $internal = $artefacts;
             continue;
             // do it last so other plugins can inject persondata
         }
         $classname = 'LeapExport' . ucfirst($plugin);
         $pluginexport = new $classname($this, $artefacts);
         $this->xml .= $pluginexport->get_export_xml();
     }
     if (!empty($internal)) {
         $pluginexport = new LeapExportInternal($this, $internal);
         $this->xml .= $pluginexport->get_export_xml();
     }
     $this->notify_progress_callback(85, get_string('exportingfooter', 'export'));
     $this->export_footer();
     $this->notify_progress_callback(90, get_string('writingfiles', 'export'));
     // Filter invalid XML characters out of the final product
     require_once 'file.php';
     $this->xml = preg_replace(xml_filter_regex(), '', $this->xml);
     // write out xml to a file
     if (!file_put_contents($this->exportdir . $this->leapfile, $this->xml)) {
         $SESSION->add_error_msg(get_string('couldnotwriteLEAPdata', 'export'));
     }
     // copy attachments over
     foreach ($this->attachments as $id => $fileinfo) {
         $existingfile = $fileinfo->file;
         $desiredname = $fileinfo->name;
         if (!is_file($existingfile) || !copy($existingfile, $this->exportdir . $this->filedir . $id . '-' . $desiredname)) {
             $SESSION->add_error_msg(get_string('couldnotcopyattachment', 'export', $desiredname));
         }
     }
     $this->notify_progress_callback(95, get_string('creatingzipfile', 'export'));
     // zip everything up
     try {
         create_zip_archive($this->exportdir, $this->zipfile, array($this->leapfile, $this->filedir));
     } catch (SystemException $e) {
         throw new SystemException('Failed to zip the export file: ' . $e->getMessage());
     }
     $this->notify_progress_callback(100, get_string('Done', 'export'));
     return $this->zipfile;
 }
 /**
  * Copy submissions to the server and in the appropriate place
  */
 function upload_submissions($files, $match_details)
 {
     require_once '..' . DS . 'lib' . DS . 'createZipArchive.php';
     //Is there a session running
     if (!isset($_SESSION['user_row_data'])) {
         return false;
     }
     $ud = $_SESSION['user_row_data'];
     $source_file_details = $this->get_source_file_details($files);
     $user_source_file_name = $ud['nick_name'] . $match_details['id'] . '.zip';
     $this->source_file_name = $user_source_file_name;
     $this->tmp_source_file = $ud['nick_name'] . $match_details['id'] . $source_file_details['extension'];
     //Zip and upload the source file, this is important when downloading source files by other users in CodeZone practice mode
     copy($files['source_file']['tmp_name'], '..' . DS . 'competition_uploads' . DS . $match_details['match_table_name'] . DS . $this->tmp_source_file);
     chdir('..' . DS . 'competition_uploads' . DS . $match_details['match_table_name']);
     $status = create_zip_archive(array($this->tmp_source_file), $user_source_file_name, true);
     chdir('..' . DS . '..' . DS . 'ajphp');
     #go back to previous directory
     return $status;
 }