mod-data MDL-23656 Convert mod-data-preset to use the new file API

This commit is contained in:
Sam Hemelryk 2010-08-25 01:22:15 +00:00
parent 86e0afcfaa
commit 8aff1574dc
3 changed files with 161 additions and 59 deletions

View file

@ -32,6 +32,11 @@ define ('DATA_TIMEADDED', 0);
define ('DATA_TIMEMODIFIED', -4); define ('DATA_TIMEMODIFIED', -4);
define ('DATA_CAP_EXPORT', 'mod/data:viewalluserpresets'); define ('DATA_CAP_EXPORT', 'mod/data:viewalluserpresets');
define('DATA_PRESET_COMPONENT', 'mod_data');
define('DATA_PRESET_FILEAREA', 'site_presets');
define('DATA_PRESET_CONTEXT', SYSCONTEXTID);
// Users having assigned the default role "Non-editing teacher" can export database records // Users having assigned the default role "Non-editing teacher" can export database records
// Using the mod/data capability "viewalluserpresets" existing in Moodle 1.9.x. // Using the mod/data capability "viewalluserpresets" existing in Moodle 1.9.x.
// In Moodle >= 2, new roles may be introduced and used instead. // In Moodle >= 2, new roles may be introduced and used instead.
@ -1836,10 +1841,8 @@ function data_preset_name($shortname, $path) {
} }
/** /**
* Returns an array of all the available presets * Returns an array of all the available presets.
* *
* @global object
* @global object
* @return array * @return array
*/ */
function data_get_available_presets($context) { function data_get_available_presets($context) {
@ -1847,10 +1850,10 @@ function data_get_available_presets($context) {
$presets = array(); $presets = array();
// First load the ratings sub plugins that exist within the modules preset dir
if ($dirs = get_list_of_plugins('mod/data/preset')) { if ($dirs = get_list_of_plugins('mod/data/preset')) {
foreach ($dirs as $dir) { foreach ($dirs as $dir) {
$fulldir = $CFG->dirroot.'/mod/data/preset/'.$dir; $fulldir = $CFG->dirroot.'/mod/data/preset/'.$dir;
if (is_directory_a_preset($fulldir)) { if (is_directory_a_preset($fulldir)) {
$preset = new object; $preset = new object;
$preset->path = $fulldir; $preset->path = $fulldir;
@ -1868,46 +1871,70 @@ function data_get_available_presets($context) {
} }
} }
} }
// Now add to that the site presets that people have saved
if ($userids = get_list_of_plugins('data/preset', '', $CFG->dataroot)) { $presets = data_get_available_site_presets($context, $presets);
$canviewall = has_capability('mod/data:viewalluserpresets', $context);
foreach ($userids as $userid) {
$fulldir = $CFG->dataroot.'/data/preset/'.$userid;
if ($userid == 0 || $USER->id == $userid || $canviewall) {
if ($dirs = get_list_of_plugins('data/preset/'.$userid, '', $CFG->dataroot)) {
foreach ($dirs as $dir) {
$fulldir = $CFG->dataroot.'/data/preset/'.$userid.'/'.$dir;
if (is_directory_a_preset($fulldir)) {
$preset = new object;
$preset->path = $fulldir;
$preset->userid = $userid;
$preset->shortname = $dir;
$preset->name = $preset->shortname;
if (file_exists($fulldir.'/screenshot.jpg')) {
$preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.jpg';
} else if (file_exists($fulldir.'/screenshot.png')) {
$preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.png';
} else if (file_exists($fulldir.'/screenshot.gif')) {
$preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.gif';
}
$presets[] = $preset;
}
}
}
}
}
}
return $presets; return $presets;
} }
/** /**
* @global object * Gets an array of all of the presets that users have saved to the site.
* @global string *
* @global string * @param stdClass $context The context that we are looking from.
* @param object $course * @param array $presets
* @param object $cm * @return array An array of presets
* @param object $data */
function data_get_available_site_presets($context, array $presets=array()) {
$fs = get_file_storage();
$files = $fs->get_area_files(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA);
$canviewall = has_capability('mod/data:viewalluserpresets', $context);
if (empty($files)) {
return $presets;
}
foreach ($files as $file) {
if (($file->is_directory() && $file->get_filepath()=='/') || !$file->is_directory() || (!$canviewall && $file->get_userid() != $USER->id)) {
continue;
}
$preset = new stdClass;
$preset->path = $file->get_filepath();
$preset->name = trim($preset->path, '/');
$preset->shortname = $preset->name;
$preset->userid = $file->get_userid();
$preset->id = $file->get_id();
$preset->storedfile = $file;
$presets[] = $preset;
}
return $presets;
}
/**
* Deletes a saved preset.
*
* @param string $name
* @return bool
*/
function data_delete_site_preset($name) {
$fs = get_file_storage();
$files = $fs->get_directory_files(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA, 0, '/'.$name.'/');
if (!empty($files)) {
foreach ($files as $file) {
$file->delete();
}
}
$dir = $fs->get_file(DATA_PRESET_CONTEXT, DATA_PRESET_COMPONENT, DATA_PRESET_FILEAREA, 0, '/'.$name.'/', '.');
if (!empty($dir)) {
$dir->delete();
}
return true;
}
/**
* Prints the heads for a page
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $data
* @param string $currenttab * @param string $currenttab
*/ */
function data_print_header($course, $cm, $data, $currenttab='') { function data_print_header($course, $cm, $data, $currenttab='') {
@ -2857,13 +2884,72 @@ function data_extend_settings_navigation(settings_navigation $settings, navigati
} }
} }
function data_presets_export($course, $cm, $data) { /**
global $CFG, $DB; * Save the database configuration as a preset.
$presetname = clean_filename($data->name) . '-preset-' . gmdate("Ymd_Hi"); *
$exportsubdir = "$course->id/moddata/data/$data->id/$presetname"; * @param stdClass $course The course the database module belongs to.
make_upload_directory($exportsubdir); * @param stdClass $cm The course module record
$exportdir = "$CFG->dataroot/$exportsubdir"; * @param stdClass $data The database record
* @param string $path
* @return bool
*/
function data_presets_save($course, $cm, $data, $path) {
$fs = get_file_storage();
$filerecord = new stdClass;
$filerecord->contextid = DATA_PRESET_CONTEXT;
$filerecord->component = DATA_PRESET_COMPONENT;
$filerecord->filearea = DATA_PRESET_FILEAREA;
$filerecord->itemid = 0;
$filerecord->filepath = '/'.$path.'/';
$filerecord->filename = 'preset.xml';
$fs->create_file_from_string($filerecord, data_presets_generate_xml($course, $cm, $data));
$filerecord->filename = 'singletemplate.html';
$fs->create_file_from_string($filerecord, $data->singletemplate);
$filerecord->filename = 'listtemplateheader.html';
$fs->create_file_from_string($filerecord, $data->listtemplateheader);
$filerecord->filename = 'listtemplate.html';
$fs->create_file_from_string($filerecord, $data->listtemplate);
$filerecord->filename = 'listtemplatefooter.html';
$fs->create_file_from_string($filerecord, $data->listtemplatefooter);
$filerecord->filename = 'addtemplate.html';
$fs->create_file_from_string($filerecord, $data->addtemplate);
$filerecord->filename = 'rsstemplate.html';
$fs->create_file_from_string($filerecord, $data->rsstemplate);
$filerecord->filename = 'rsstitletemplate.html';
$fs->create_file_from_string($filerecord, $data->rsstitletemplate);
$filerecord->filename = 'csstemplate.css';
$fs->create_file_from_string($filerecord, $data->csstemplate);
$filerecord->filename = 'jstemplate.js';
$fs->create_file_from_string($filerecord, $data->jstemplate);
$filerecord->filename = 'asearchtemplate.html';
$fs->create_file_from_string($filerecord, $data->asearchtemplate);
return true;
}
/**
* Generates the XML for the database module provided
*
* @global moodle_database $DB
* @param stdClass $course The course the database module belongs to.
* @param stdClass $cm The course module record
* @param stdClass $data The database record
* @return string The XML for the preset
*/
function data_presets_generate_xml($course, $cm, $data) {
global $DB;
// Assemble "preset.xml": // Assemble "preset.xml":
$presetxmldata = "<preset>\n\n"; $presetxmldata = "<preset>\n\n";
@ -2892,7 +2978,6 @@ function data_presets_export($course, $cm, $data) {
$presetxmldata .= "<defaultsort>0</defaultsort>\n"; $presetxmldata .= "<defaultsort>0</defaultsort>\n";
} }
$presetxmldata .= "</settings>\n\n"; $presetxmldata .= "</settings>\n\n";
// Now for the fields. Grab all that are non-empty // Now for the fields. Grab all that are non-empty
$fields = $DB->get_records('data_fields', array('dataid'=>$data->id)); $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
ksort($fields); ksort($fields);
@ -2908,6 +2993,19 @@ function data_presets_export($course, $cm, $data) {
} }
} }
$presetxmldata .= '</preset>'; $presetxmldata .= '</preset>';
return $presetxmldata;
}
function data_presets_export($course, $cm, $data, $tostorage=false) {
global $CFG, $DB;
$presetname = clean_filename($data->name) . '-preset-' . gmdate("Ymd_Hi");
$exportsubdir = "temp/mod_data/presetexport/$presetname";
make_upload_directory($exportsubdir);
$exportdir = "$CFG->dataroot/$exportsubdir";
// Assemble "preset.xml":
$presetxmldata = data_presets_generate_xml($course, $cm, $data);
// After opening a file in write mode, close it asap // After opening a file in write mode, close it asap
$presetxmlfile = fopen($exportdir . '/preset.xml', 'w'); $presetxmlfile = fopen($exportdir . '/preset.xml', 'w');
@ -2978,8 +3076,12 @@ function data_presets_export($course, $cm, $data) {
$filelist[$key] = $exportdir . '/' . $filelist[$key]; $filelist[$key] = $exportdir . '/' . $filelist[$key];
} }
$exportfile = "$CFG->dataroot/$course->id/moddata/data/$data->id/$presetname.zip"; $exportfile = $exportdir.'.zip';
file_exists($exportfile) && unlink($exportfile); file_exists($exportfile) && unlink($exportfile);
$fp = get_file_packer();
$fp->archive_to_pathname($files, $archivefile);
$status = zip_files($filelist, $exportfile); $status = zip_files($filelist, $exportfile);
// ToDo: status check // ToDo: status check
foreach ($filelist as $file) { foreach ($filelist as $file) {

View file

@ -135,21 +135,21 @@ if (optional_param('sesskey', false, PARAM_BOOL) && confirm_sesskey()) {
} else if ($formdata = $form_save->get_data()) { } else if ($formdata = $form_save->get_data()) {
$presetdirectory = "/data/preset/$USER->id/{$formdata->name}"; if (!empty($formdata->overwrite)) {
data_delete_site_preset($formdata->name);
}
if (file_exists($CFG->dataroot.$presetdirectory)) { // If the preset exists now then we need to throw an error.
if (!$formdata->overwrite) { $sitepresets = data_get_available_site_presets($context);
foreach ($sitepresets as $key=>$preset) {
if ($formdata->name == $preset->name) {
print_error('errorpresetexists', 'preset'); print_error('errorpresetexists', 'preset');
} else {
fulldelete($CFG->dataroot.$presetdirectory);
} }
} }
make_upload_directory($presetdirectory);
$file = data_presets_export($course, $cm, $data); // Save the preset now
if (!unzip_file($file, $CFG->dataroot.$presetdirectory, false)) { data_presets_save($course, $cm, $data, $formdata->name);
print_error('cannotunziptopreset', 'data');
}
echo $OUTPUT->notification(get_string('savesuccess', 'data'), 'notifysuccess'); echo $OUTPUT->notification(get_string('savesuccess', 'data'), 'notifysuccess');
echo $OUTPUT->continue_button($PAGE->url); echo $OUTPUT->continue_button($PAGE->url);
echo $OUTPUT->footer(); echo $OUTPUT->footer();

View file

@ -51,7 +51,7 @@ class data_save_preset_form extends moodleform {
$this->_form->addElement('text', 'name', get_string('shortname')); $this->_form->addElement('text', 'name', get_string('shortname'));
$this->_form->setType('name', PARAM_FILE); $this->_form->setType('name', PARAM_FILE);
$this->_form->addRule('name', null, 'required'); $this->_form->addRule('name', null, 'required');
$this->_form->addElement('checkbox', 'override', get_string('overwrite', 'data'), get_string('overrwritedesc', 'data')); $this->_form->addElement('checkbox', 'overwrite', get_string('overwrite', 'data'), get_string('overrwritedesc', 'data'));
$this->_form->addElement('submit', 'saveaspreset', get_string('continue')); $this->_form->addElement('submit', 'saveaspreset', get_string('continue'));
} }
} }