/** * Validate that the export generates the correct filename */ public function test_version1exportcreatescorrectfilename() { global $USER; if (empty($USER->timezone)) { $USER->timezone = 99; } $plugin = 'dhexport_version1'; $baseexportfile = 'export_version1.csv'; set_config('export_file', $baseexportfile, $plugin); set_config('export_file_timestamp', false, $plugin); // Base export file w/o timestamp. $exportfile = rlip_get_export_filename($plugin, $USER->timezone); $this->assertEquals(basename($exportfile), $baseexportfile); set_config('export_file_timestamp', true, $plugin); // Export file WITH timestamp. $exportfile = rlip_get_export_filename($plugin, $USER->timezone); $parts = explode('_', basename($exportfile)); $this->assertEquals($parts[0], 'export'); $this->assertEquals($parts[1], 'version1'); $timestamp = userdate(time(), get_string('export_file_timestamp', $plugin), $USER->timezone); $dateparts = explode('_', $timestamp); if (count($dateparts) == 4) { $this->assertEquals($parts[2], $dateparts[0]); // MMM. $this->assertEquals($parts[3], $dateparts[1]); // DD. $this->assertEquals($parts[4], $dateparts[2]); // YYYY. $this->assertLessThanOrEqual($parts[5], $dateparts[3]); // Hhmmss. } }
/** * Validate export file specifies RLIP_EXPORT_TEMPDIR as path */ public function test_exportfilenameincorrecttempdir() { global $CFG; require_once $CFG->dirroot . '/local/datahub/lib.php'; $plugin = 'test_dhexport_version1'; set_config('export_file', "/tmp/{$plugin}/{$plugin}.csv", $plugin); $exportfilename = rlip_get_export_filename($plugin, 99); $targetpath = $CFG->dataroot . sprintf(RLIP_EXPORT_TEMPDIR, $plugin); $this->assertEquals($targetpath, dirname($exportfilename) . '/'); // Clean-up created export temp dirs for this bogus plugin. @rmdir(dirname($exportfilename)); @rmdir(dirname(dirname($exportfilename))); }
/** * sub-function for running scheduled IP jobs * * @param string $prefix mtrace prefix string * @param string $plugin The plugin name * @param string $type The plugin type (i.e. dhimport, dhexport) * @param int $userid the scheduled job's Moodle userid * @param object $state the scheduled job's past state object * @uses $CFG * @uses $DB * @return object import/export instance to run, null on error (for unsupported plugin) */ function rlip_get_run_instance($prefix, $plugin, $type, $userid, $state) { global $CFG, $DB; $instance = null; $rlipshortname = 'DH'; switch ($type) { // TBD case 'dhimport': $baseinstance = rlip_dataplugin_factory::factory($plugin); $entity_types = $baseinstance->get_import_entities(); $files = array(); $dataroot = rtrim($CFG->dataroot, DIRECTORY_SEPARATOR); $path = $dataroot . DIRECTORY_SEPARATOR . trim(get_config($plugin, 'schedule_files_path'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; $temppath = sprintf($dataroot . RLIP_IMPORT_TEMPDIR, $plugin); if (!file_exists($temppath) && !@mkdir($temppath, 0777, true)) { mtrace("{$prefix}: Error creating directory '{$temppath}' ... using '{$path}'"); //TBD*** just use main directory??? $temppath = $path; } foreach ($entity_types as $entity) { $entity_filename = get_config($plugin, $entity . '_schedule_file'); if (empty($entity_filename)) { // TBD: need dummy so we're not testing directories! $entity_filename = $entity . '.csv'; } //echo "\n get_config('{$plugin}', '{$entity}_schedule_file') => {$entity_filename}"; if ($state == null && $path !== $temppath && file_exists($path . $entity_filename) && !@rename($path . $entity_filename, $temppath . $entity_filename)) { $message = "{$prefix}: Error moving '" . $path . $entity_filename . "' to '" . $temppath . $entity_filename . "'"; mtrace($message); continue; // Let's skip any unmovable files. } $files[$entity] = $temppath . $entity_filename; } $importprovider = new rlip_importprovider_csv($entity_types, $files); $instance = rlip_dataplugin_factory::factory($plugin, $importprovider); break; case 'dhexport': $tz = $DB->get_field('user', 'timezone', array('id' => $userid)); $export = rlip_get_export_filename($plugin, $tz === false ? 99 : $tz); $fileplugin = rlip_fileplugin_factory::factory($export, NULL, false); $instance = rlip_dataplugin_factory::factory($plugin, NULL, $fileplugin); break; default: mtrace("{$prefix}: {$rlipshortname} plugin '{$plugin}' not supported!"); break; } return $instance; }
//determine which plugin we're using $plugin = required_param('plugin', PARAM_CLEAN); // Need base URL for form and Moodle block management $baseurl = $CFG->wwwroot . '/local/datahub/exportplugins/manualrun.php?plugin=' . $plugin; //page setup $plugin_display = get_string('pluginname', $plugin); rlip_manualrun_page_setup($baseurl, $plugin_display); //javascript library needed by the form $PAGE->requires->js('/local/datahub/js/lib.js'); //create our basic form $form = new rlip_manualexport_form(); $form->set_data(array('plugin' => $plugin)); //run the export before printing a page header if ($data = $form->get_data()) { //run the export $filename = rlip_get_export_filename($plugin, $USER->timezone); $fileplugin = rlip_fileplugin_factory::factory($filename, NULL, false, true); //indicate to the factory class that this is a manual run $manual = true; $instance = rlip_dataplugin_factory::factory($plugin, NULL, $fileplugin, $manual); ob_start(); $result = $instance->run(0, 0, rlip_get_maxruntime()); $errors = ob_get_contents(); ob_end_clean(); if ($result !== null) { // Error running export (probably time limit exceeded) echo $OUTPUT->header(); //display errors in a span so we can clear it using javascript when needed $attributes = array('id' => 'rlipexporterrors'); echo html_writer::tag('span', $errors, $attributes); //display the form