MDL-8774: Also an effort to refactorise the poorly designed mod/data/Preset code.

This commit is contained in:
nicolasconnault 2007-04-03 03:32:46 +00:00
parent 97c3141cc3
commit bfe1e789de
7 changed files with 1065 additions and 2 deletions

View File

@ -105,7 +105,7 @@
<?php
}
$fullnav = str_replace('->', '&raquo;', "$course->shortname -> $fullnav");
$fullnav = str_replace('->', '&raquo;', format_string($course->shortname) . " -> $fullnav");
echo '<div id="nav-bar">'.$fullnav.'</div>';
if ($course->id == SITEID and $wdir != "/backupdata") {

View File

@ -63,6 +63,7 @@ $string['deleted'] = 'deleted';
$string['deletefield'] = 'Delete an existing field';
$string['deletewarning'] = 'Are you sure you want to delete this preset?';
$string['descending'] = 'Descending';
$string['directorynotapreset'] = '$a->directory Not a preset: missing files: $a->missing_files';
$string['download'] = 'Download';
$string['edit'] = 'Edit';
$string['editcomment'] = 'Edit comment';
@ -79,6 +80,7 @@ $string['example'] = 'Database module example';
$string['export'] = 'Export';
$string['exportaszip'] = 'Export as zip';
$string['exportedtozip'] = 'Exported to temporary zip...';
$string['failedpresetdelete'] = 'Error deleting a preset!';
$string['fieldadded'] = 'Field added';
$string['fieldallowautolink'] = 'Allow autolink';
$string['fielddeleted'] = 'Field deleted';
@ -98,6 +100,7 @@ $string['fieldwidth'] = 'Width';
$string['fieldwidthlistview'] = 'Width in list view';
$string['fieldwidthsingleview'] = 'Width in single view';
$string['file'] = 'File';
$string['filesnotgenerated'] = 'Not all files were generated: $a';
$string['filtername'] = 'Database Auto-linking';
$string['footer'] = 'Footer';
$string['forcelinkname'] = 'Forced name for the link';
@ -134,6 +137,7 @@ $string['modulename'] = 'Database';
$string['modulenameplural'] = 'Databases';
$string['more'] = 'More';
$string['moreurl'] = 'More URL';
$string['movezipfailed'] = 'Can\'t move zip';
$string['multientry'] = 'Repeated entry';
$string['multimenu'] = 'Menu (Multi-select)';
$string['multipletags'] = 'Multiple tags found! Template not saved';
@ -152,12 +156,14 @@ $string['nameurl'] = 'URL field';
$string['newentry'] = 'New entry';
$string['newfield'] = 'Create a new field';
$string['noaccess'] = 'You do not have access to this page';
$string['nodefinedfields'] = 'New preset has no defined fields!';
$string['nofieldindatabase'] = 'There are no fields defined for this database.';
$string['nolisttemplate'] = 'List template is not yet defined';
$string['nomatch'] = 'No matching entries found!';
$string['nomaximum'] = 'No maximum';
$string['norecords'] = 'No entries in database';
$string['nosingletemplate'] = 'Single template is not yet defined';
$string['notinjectivemap'] = 'Not an injective map';
$string['number'] = 'Number';
$string['numberrssarticles'] = 'RSS articles';
$string['numnotapproved'] = 'Pending';
@ -215,6 +221,7 @@ $string['text'] = 'Text';
$string['textarea'] = 'Textarea';
$string['todatabase'] = 'to this database.';
$string['type'] = 'Field type';
$string['undefinedprocessactionmethod'] = 'No action method defined in Data_Preset to handle action \"$a\".';
$string['updatefield'] = 'Update an existing field';
$string['uploadfile'] = 'Upload file';
$string['uploadrecords'] = 'Upload entries from a file';
@ -222,5 +229,5 @@ $string['url'] = 'Url';
$string['usestandard'] = 'Use a preset';
$string['viewfromdate'] = 'Viewable from';
$string['viewtodate'] = 'Viewable to';
$string['wrongdataid'] = 'Wrong data id provided';
?>

View File

@ -37,6 +37,7 @@ $string['invalidcourse'] = 'Invalid course';
$string['invalidfieldname'] = '\"$a\" is not a valid field name';
$string['invalidfiletype'] = '\"$a\" is not a valid file type';
$string['invalidmd5'] = 'Invalid md5';
$string['invalidrequest'] = 'Invalid request';
$string['invalidrole'] = 'Invalid role';
$string['invalidxmlfile'] = '\"$a\" is not a valid XML file';
$string['loginasonecourse'] = 'You can not enter this course.<br /> You have to terminate the \"Login as\" session before entering any other course.';

812
mod/data/preset_class.php Normal file
View File

@ -0,0 +1,812 @@
<?php
// $Id$
/**
* This object is a representation of a file-based preset for database activities
*/
class Data_Preset
{
/**
* Required files in a preset directory
* @var array $required_files
*/
var $required_files = array('singletemplate.html',
'listtemplate.html',
'listtemplateheader.html',
'listtemplatefooter.html',
'addtemplate.html',
'rsstemplate.html',
'csstemplate.css',
'jstemplate.js',
'preset.xml');
/**
* The preset's shortname.
* TODO document
* @var string $shortname
*/
var $shortname;
/**
* A database activity object
* @var object $data
*/
var $data;
/**
* Directory mapped to this Preset object.
* @var string $directory
*/
var $directory;
/**
* This Preset's singletemplate
* @var string $singletemplate
*/
var $singletemplate;
/**
* This Preset's listtemplate
* @var string $listtemplate
*/
var $listtemplate;
/**
* This Preset's listtemplateheader
* @var string $listtemplateheader
*/
var $listtemplateheader;
/**
* This Preset's listtemplatefooter
* @var string $listtemplatefooter
*/
var $listtemplatefooter;
/**
* This Preset's addtemplate
* @var string $addtemplate
*/
var $addtemplate;
/**
* This Preset's rsstemplate
* @var string $rsstemplate
*/
var $rsstemplate;
/**
* This Preset's csstemplate
* @var string $csstemplate
*/
var $csstemplate;
/**
* This Preset's jstemplate
* @var string $jstemplate
*/
var $jstemplate;
/**
* This Preset's xml
* @var string $xml
*/
var $xml;
var $user_id;
/**
* Constructor
*/
function Data_Preset($shortname = null, $data_id = null, $directory = null, $user_id = null)
{
$this->shortname = $shortname;
$this->user_id = $user_id;
if (empty($directory)) {
$this->directory = $this->get_path();
} else {
$this->directory = $directory;
}
if (!empty($data_id)) {
if (!$this->data = get_record('data', 'id', $data_id)) {
print_error('wrongdataid','data');
} else {
$this->listtemplate = $this->data->listtemplate;
$this->singletemplate = $this->data->singletemplate;
$this->listtemplateheader = $this->data->listtemplateheader;
$this->listtemplatefooter = $this->data->listtemplatefooter;
$this->addtemplate = $this->data->addtemplate;
$this->rsstemplate = $this->data->rsstemplate;
$this->csstemplate = $this->data->csstemplate;
$this->jstemplate = $this->data->jstemplate;
}
}
}
/*
* Returns the best name to show for a preset
* If the shortname has spaces in it, replace them with underscores.
* Convert the name to lower case.
*/
function best_name($shortname = null) {
if (empty($shortname)) {
$shortname = $this->shortname;
}
/// We are looking inside the preset itself as a first choice, but also in normal data directory
$string = get_string('presetname'.$shortname, 'data', NULL, $this->directory.'/lang/');
if (substr($string, 0, 1) == '[') {
return strtolower(str_replace(' ', '_', $shortname));
} else {
return $string;
}
}
/**
* TODO figure out what's going on here with the user id. This method doesn't look quite right to me.
*/
function get_path() {
global $USER, $CFG, $COURSE;
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
if ($this->user_id > 0 && ($this->user_id == $USER->id || has_capability('mod/data:viewalluserpresets', $context))) {
return $CFG->dataroot.'/data/preset/'.$this->user_id.'/'.$this->shortname;
} else if ($this->user_id == 0) {
return $CFG->dirroot.'/mod/data/preset/'.$this->shortname;
} else if ($this->user_id < 0) {
return $CFG->dataroot.'/temp/data/'.-$this->user_id.'/'.$this->shortname;
}
return 'Does it disturb you that this code will never run?';
}
/**
* A preset is a directory with a number of required files.
* This function verifies that the given directory contains
* all these files, thus validating as a preset directory.
*
* @param string $directory An optional directory to check. Will use this Preset's directory if not provided.
* @return mixed True if the directory contains all the files required to qualify as Preset object;
* an array of the missing filename is returned otherwise
*/
function has_all_required_files($directory = null)
{
if (empty($directory)) {
$directory = $this->directory;
} else {
$directory = rtrim($directory, '/\\') . '/';
}
$missing_files = array();
foreach ($this->required_files as $file) {
if(!file_exists($directory . '/' . $file)) {
$missing_files[] = $file;
}
}
if (!empty($missing_files)) {
return $missing_files;
}
return true;
}
/**
* Deletes all the files in the directory mapped by this Preset object.
*
* @return boolean False if an error occured while trying to delete one of the files, true otherwise.
*/
function clean_files()
{
foreach ($this->required_files as $file) {
if (!unlink($this->directory . '/' . $file)) {
return $file;
}
}
return true;
}
function get_template_files()
{
$template_files = array();
foreach ($this->required_files as $file) {
if (preg_match('/^([a-z]+template[a-z]?)\.[a-z]{2,4}$/', $file, $matches)) {
$template_files[$matches[1]] = $file;
}
}
return $template_files;
}
/**
* Exports this Preset object as a series of files in the Preset's directory.
* @return string The path/name of the resulting zip file if successful.
*/
function export() {
global $CFG;
$this->directory = $CFG->dataroot.'/temp';
// write all templates, but not the xml yet
$template_files = $this->get_template_files();
foreach ($template_files as $var => $file) {
$handle = fopen($this->directory . '/' . $file, 'w');
fwrite($handle, $this->$var);
fclose($handle);
}
/* All the display data is now done. Now assemble preset.xml */
$fields = get_records('data_fields', 'dataid', $this->data->id);
$presetfile = fopen($this->directory.'/preset.xml', 'w');
$presetxml = "<preset>\n\n";
/* Database settings first. Name not included? */
$settingssaved = array('intro',
'comments',
'requiredentries',
'requiredentriestoview',
'maxentries',
'rssarticles',
'approval',
'scale',
'assessed',
'defaultsort',
'defaultsortdir',
'editany');
$presetxml .= "<settings>\n";
foreach ($settingssaved as $setting) {
$presetxml .= "<$setting>{$this->data->$setting}</$setting>\n";
}
$presetxml .= "</settings>\n\n";
/* Now for the fields. Grabs all settings that are non-empty */
if (!empty($fields)) {
foreach ($fields as $field) {
$presetxml .= "<field>\n";
foreach ($field as $key => $value) {
if ($value != '' && $key != 'id' && $key != 'dataid') {
$presetxml .= "<$key>$value</$key>\n";
}
}
$presetxml .= "</field>\n\n";
}
}
$presetxml .= "</preset>";
fwrite($presetfile, $presetxml);
fclose($presetfile);
/* Check all is well */
if (is_array($missing_files = $this->has_all_required_files())) {
$missing_files = implode(', ', $missing_files);
print_error('filesnotgenerated', 'data', null, $missing_files);
}
// Remove export.zip
@unlink($this->directory.'/export.zip');
$filelist = array();
foreach ($this->required_files as $file) {
$filelist[$file] = $this->directory . '/' . $file;
}
// zip_files is part of moodlelib
$status = zip_files($filelist, $this->directory.'/export.zip');
/* made the zip... now return the filename for storage.*/
return $this->directory.'/export.zip';
}
/**
* Loads the contents of the preset folder to initialise this Preset object.
* TODO document
*/
function load_from_file($directory = null) {
global $CFG;
if (empty($directory) && empty($this->directory)) {
$this->directory = $this->get_path();
}
if (is_array($missing_files = $this->has_all_required_files())) {
$a = new StdClass();
$a->missing_files = implode(', ', $missing_files);
$a->directory = $this->directory;
print_error('directorynotapreset','data', null, $a);
}
/* Grab XML */
$presetxml = file_get_contents($this->directory.'/preset.xml');
$parsedxml = xmlize($presetxml);
/* First, do settings. Put in user friendly array. */
$settingsarray = $parsedxml['preset']['#']['settings'][0]['#'];
$settings = new StdClass();
foreach ($settingsarray as $setting => $value) {
$settings->$setting = $value[0]['#'];
}
/* Now work out fields to user friendly array */
$fieldsarray = $parsedxml['preset']['#']['field'];
$fields = array();
foreach ($fieldsarray as $field) {
$f = new StdClass();
foreach ($field['#'] as $param => $value) {
$f->$param = $value[0]['#'];
}
$f->dataid = $this->data->id;
$f->type = clean_param($f->type, PARAM_ALPHA);
$fields[] = $f;
}
/* Now add the HTML templates to the settings array so we can update d */
$template_files = $this->get_template_files();
foreach ($template_files as $var => $file) {
$settings->$var = file_get_contents($this->directory . '/' . $file);
}
$settings->instance = $this->data->id;
/* Now we look at the current structure (if any) to work out whether we need to clear db
or save the data */
$currentfields = array();
$currentfields = get_records('data_fields', 'dataid', $this->data->id);
$currentfields = array_merge($currentfields);
return array($settings, $fields, $currentfields);
}
/**
* Import options
* TODO document
* TODO replace all output by a return value
*/
function get_import_html() {
if (!confirm_sesskey()) {
print_error("confirmsesskeybad");
}
$strblank = get_string('blank', 'data');
$strnofields = get_string('nofields', 'data');
$strcontinue = get_string('continue');
$strwarning = get_string('mappingwarning', 'data');
$strfieldmappings = get_string('fieldmappings', 'data');
$strnew = get_string('new');
$strold = get_string('old');
$sesskey = sesskey();
list($settings, $newfields, $currentfields) = $this->load_from_file();
$html = '';
$html .= '<div style="text-align:center"><form action="preset.php" method="post">';
$html .= '<fieldset class="invisiblefieldset">';
$html .= '<input type="hidden" name="action" value="finishimport" />';
$html .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
$html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
$html .= '<input type="hidden" name="fullname" value="'.$this->user_id.'/'.$this->shortname.'" />';
if (!empty($currentfields) && !empty($newfields)) {
$html .= "<h3>$strfieldmappings ";
helpbutton('fieldmappings', '', 'data');
$html .= '</h3><table>';
foreach ($newfields as $nid => $newfield) {
$html .= "<tr><td><label for=\"id_$newfield->name\">$newfield->name</label></td>";
$html .= '<td><select name="field_'.$nid.'" id="id_'.$newfield->name.'">';
$selected = false;
foreach ($currentfields as $cid => $currentfield) {
if ($currentfield->type == $newfield->type) {
if ($currentfield->name == $newfield->name) {
$html .= '<option value="'.$cid.'" selected="selected">'.$currentfield->name.'</option>';
$selected=true;
}
else {
$html .= '<option value="$cid">'.$currentfield->name.'</option>';
}
}
}
if ($selected)
$html .= '<option value="-1">-</option>';
else
$html .= '<option value="-1" selected="selected">-</option>';
$html .= '</select></td></tr>';
}
$html .= '</table>';
$html .= "<p>$strwarning</p>";
}
else if (empty($newfields)) {
print_error('nodefinedfields', 'data');
}
$html .= '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
return $html;
}
/**
* import()
* TODO document
*/
function import() {
global $CFG;
list($settings, $newfields, $currentfields) = $this->load_from_file();
$preservedfields = array();
/* Maps fields and makes new ones */
if (!empty($newfields)) {
/* We require an injective mapping, and need to know what to protect */
foreach ($newfields as $nid => $newfield) {
$cid = optional_param("field_$nid", -1, PARAM_INT);
if ($cid == -1) continue;
if (array_key_exists($cid, $preservedfields)) {
print_error('notinjectivemap', 'data');
} else {
$preservedfields[$cid] = true;
}
}
foreach ($newfields as $nid => $newfield) {
$cid = optional_param("field_$nid", -1, PARAM_INT);
/* A mapping. Just need to change field params. Data kept. */
if ($cid != -1 and isset($currentfelds[$cid])) {
$fieldobject = data_get_field_from_id($currentfields[$cid]->id, $this->data);
foreach ($newfield as $param => $value) {
if ($param != "id") {
$fieldobject->field->$param = $value;
}
}
unset($fieldobject->field->similarfield);
$fieldobject->update_field();
unset($fieldobject);
}
/* Make a new field */
else {
include_once("field/$newfield->type/field.class.php");
if (!isset($newfield->description)) {
$newfield->description = '';
}
$classname = 'data_field_'.$newfield->type;
$fieldclass = new $classname($newfield, $this->data);
$fieldclass->insert_field();
unset($fieldclass);
}
}
}
/* Get rid of all old unused data */
if (!empty($preservedfields)) {
foreach ($currentfields as $cid => $currentfield) {
if (!array_key_exists($cid, $preservedfields)) {
/* Data not used anymore so wipe! */
print "Deleting field $currentfield->name<br />";
$id = $currentfield->id;
// Why delete existing data records and related comments/ratings ??
/*
if ($content = get_records('data_content', 'fieldid', $id)) {
foreach ($content as $item) {
delete_records('data_ratings', 'recordid', $item->recordid);
delete_records('data_comments', 'recordid', $item->recordid);
delete_records('data_records', 'id', $item->recordid);
}
}
*/
delete_records('data_content', 'fieldid', $id);
delete_records('data_fields', 'id', $id);
}
}
}
data_update_instance(addslashes_object($settings));
if (strstr($this->directory, '/temp/')) clean_preset($this->directory); /* Removes the temporary files */
return true;
}
/**
* Runs the Preset action method that matches the given action string.
* @param string $action
* @return string html
*/
function process_action($action, $params)
{
echo $action;
if (in_array("action_$action", get_class_methods(get_class($this)))) {
return $this->{"action_$action"}($params);
} else {
print_error('undefinedprocessactionmethod', 'data', null, $action);
}
}
////////////////////
// ACTION METHODS //
////////////////////
function action_base($params)
{
return null;
}
function action_confirmdelete($params)
{
global $CFG, $USER;
$html = '';
$course = $params['course'];
$shortname = $params['shortname'];
if (!confirm_sesskey()) { // GET request ok here
print_error('confirmsesskeybad');
}
$this->user_id = $params['userid'];
if ($this->user_id > 0 and ($this->user_id == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
//ok can delete
} else {
print_error('invalidrequest');
}
$path = $this->get_path();
$strwarning = get_string('deletewarning', 'data').'<br />'.
data_preset_name($shortname, $path);
$options = new object();
$options->fullname = $this->user_id.'/'.$shortname;
$options->action = 'delete';
$options->d = $this->data->id;
$options->sesskey = sesskey();
$optionsno = new object();
$optionsno->d = $this->data->id;
notice_yesno($strwarning, 'preset.php', 'preset.php', $options, $optionsno, 'post', 'get');
$html .= print_footer($course, null, true);
echo $html;
exit();
}
function action_delete($params)
{
global $CFG, $USER;
$shortname = $params['shortname'];
if (!data_submitted() and !confirm_sesskey()) {
print_error('invalidrequest');
}
if ($this->user_id > 0 and ($this->user_id == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
//ok can delete
} else {
print_error('invalidrequest');
}
$this->shortname = $this->best_name($shortname);
$this->path = $this->get_path();
$this->directory = $this->path;
if (!$this->clean_files()) {
print_error('failedpresetdelete', 'data');
}
rmdir($this->path);
$strdeleted = get_string('deleted', 'data');
notify("$shortname $strdeleted", 'notifysuccess');
}
function action_importpreset($params)
{
$course = $params['course'];
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');
}
$this->shortname = $params['shortname'];
$this->data = $params['data'];
$this->user_id = $params['userid'];
$html = '';
$html .= $this->get_import_html();
$html .= print_footer($course, null, true);
echo $html;
exit();
}
function action_importzip($params)
{
global $CFG, $USER;
$course = $params['course'];
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalid_request');
}
if (!make_upload_directory('temp/data/'.$USER->id)) {
print_error('errorcreatingdirectory', null, null, 'temp/data/' . $USER->id);
}
$this->file = $CFG->dataroot.'/temp/data/'.$USER->id;
$this->directory = $this->file;
$this->user_id = $USER->id;
$this->clean_files($this->file);
if (!unzip_file($CFG->dataroot."/$USER->id/$file", $this->file, false)) {
}
$html .= $this->get_import_html();
$html .= print_footer($course, null, true);
echo $html;
exit();
}
function action_finishimport($params)
{
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');
}
$this->shortname = $this->best_name($this->data->name);
$this->directory = $this->get_path();
$this->import();
$strimportsuccess = get_string('importsuccess', 'data');
$straddentries = get_string('addentries', 'data');
$strtodatabase = get_string('todatabase', 'data');
if (!get_records('data_records', 'dataid', $this->data->id)) {
notify('$strimportsuccess <a href="edit.php?d=' . $this->data->id . "\">$straddentries</a> $strtodatabase", 'notifysuccess');
} else {
notify("$strimportsuccess", 'notifysuccess');
}
}
function action_export($params)
{
global $CFG, $USER;
$course = $params['course'];
$html = '';
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalid_request');
}
$this->shortname = $params['shortname'];
$this->data = $params['data'];
$html .= '<div style="text-align:center">';
$file = $this->export();
$html .= get_string('exportedtozip', 'data')."<br />";
$permanentfile = $CFG->dataroot.'/' . $course->id . '/moddata/data/' . $this->data->id . '/preset.zip';
@unlink($permanentfile);
/* is this created elsewhere? sometimes its not present... */
make_upload_directory($course->id . '/moddata/data/' . $this->data->id);
/* now just move the zip into this folder to allow a nice download */
if (!rename($file, $permanentfile)) {
print_error('movezipfailed', 'data');
}
$html .= '<a href="' . $CFG->wwwroot . '/file.php/' . $course->id . '/moddata/data/' . $this->data->id . '/preset.zip">'.get_string('download', 'data')."</a>";
$html .= '</div>';
return $html;
}
/**
* First stage of saving a Preset: ask for a name
*/
function action_save1($params)
{
$html = '';
$sesskey = $params['sesskey'];
$course = $params['course'];
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalid_request');
}
$strcontinue = get_string('continue');
$strwarning = get_string('presetinfo', 'data');
$strname = get_string('shortname');
$html .= '<div style="text-align:center">';
$html .= '<p>'.$strwarning.'</p>';
$html .= '<form action="preset.php" method="post">';
$html .= '<fieldset class="invisiblefieldset">';
$html .= '<label for="shortname">'.$strname.'</label> <input type="text" id="shortname" name="name" value="'.$this->best_name($this->data->name).'" />';
$html .= '<input type="hidden" name="action" value="save2" />';
$html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
$html .= '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
$html .= '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
$html .= print_footer($course, null, true);
echo $html;
exit();
}
/**
* Second stage of saving a preset: If the given name already exists,
* suggest to use a different name or offer to overwrite the existing preset.
*/
function action_save2($params)
{
$course = $params['course'];
$this->data = $params['data'];
global $CFG, $USER;
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalid_request');
}
$strcontinue = get_string('continue');
$stroverwrite = get_string('overwrite', 'data');
$strname = get_string('shortname');
$name = $this->best_name(optional_param('name', $this->data->name, PARAM_FILE));
$this->shortname = $name;
if (!is_array($this->has_all_required_files("$CFG->dataroot/data/preset/$USER->id/$name"))) {
notify("Preset already exists: Pick another name or overwrite ($CFG->dataroot/data/preset/$USER->id/$name)");
$html .= '<div style="text-align:center">';
$html .= '<form action="preset.php" method="post">';
$html .= '<fieldset class="invisiblefieldset">';
$html .= '<label for="shortname">'.$strname.'</label> <input id="shortname" type="text" name="name" value="'.$name.'" />';
$html .= '<input type="hidden" name="action" value="save2" />';
$html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
$html .= '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
$html .= '<input type="submit" value="'.$strcontinue.'" /></fieldset></form>';
$html .= '<form action="preset.php" method="post">';
$html .= '<div>';
$html .= '<input type="hidden" name="name" value="'.$name.'" />';
$html .= '<input type="hidden" name="action" value="save3" />';
$html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
$html .= '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
$html .= '<input type="submit" value="'.$stroverwrite.'" /></div></form>';
$html .= '</div>';
$html .= print_footer($course, null, true);
echo $html;
exit();
}
}
/**
* Third stage of saving a preset, overwrites an existing preset with the new one.
*/
function action_save3($params)
{
global $CFG, $USER;
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');
}
$name = $this->best_name(optional_param('name', $this->data->name, PARAM_FILE));
$this->directory = "/data/preset/$USER->id/$name";
$this->shortname = $name;
$this->user_id = $USER->id;
make_upload_directory($this->directory);
$this->clean_files($CFG->dataroot.$this->directory);
$file = $this->export();
if (!unzip_file($file, $CFG->dataroot.$this->directory, false)) {
print_error('cannotunzipfile');
}
notify(get_string('savesuccess', 'data'), 'notifysuccess');
}
}
?>

172
mod/data/preset_new.php Executable file
View File

@ -0,0 +1,172 @@
<?php // $Id$
/* Preset Menu
*
* This is the page that is the menu item in the config database
* pages.
*/
require_once('../../config.php');
require_once('lib.php');
require_once($CFG->libdir.'/uploadlib.php');
require_once($CFG->libdir.'/xmlize.php');
require_once('preset_class.php');
$id = optional_param('id', 0, PARAM_INT); // course module id
$d = optional_param('d', 0, PARAM_INT); // database activity id
$action = optional_param('action', 'base', PARAM_ALPHANUM); // current action
$fullname = optional_param('fullname', '', PARAM_PATH); // directory the preset is in
$file = optional_param('file', '', PARAM_FILE); // uploaded file
// find out preset owner userid and shortname
$parts = explode('/', $fullname);
$userid = empty($parts[0]) ? 0 : (int)$parts[0];
$shortname = empty($parts[1]) ? '' : $parts[1];
unset($parts);
unset($fullname);
if ($id) {
if (! $cm = get_coursemodule_from_id('data', $id)) {
error('Course Module ID was incorrect');
}
if (! $course = get_record('course', 'id', $cm->course)) {
error('Course is misconfigured');
}
if (! $data = get_record('data', 'id', $cm->instance)) {
error('Module Incorrect');
}
} else if ($d) {
if (! $data = get_record('data', 'id', $d)) {
error('Database ID Incorrect');
}
if (! $course = get_record('course', 'id', $data->course)) {
error('Course is misconfigured');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
error('Course Module ID was incorrect');
}
} else {
error('Parameter missing');
}
if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
error('Could not find context');
}
require_login($course->id, false, $cm);
require_capability('mod/data:managetemplates', $context);
if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
error('You are not allowed to access presets from other users');
}
/* Need sesskey security check here for import instruction */
$sesskey = sesskey();
/********************************************************************/
/* Output */
data_print_header($course, $cm, $data, 'presets');
$preset = new Data_Preset($shortname, $data->id, null, $userid);
echo $preset->process_action($action, compact('shortname', 'fullname', 'data', 'userid', 'file', 'course', 'sesskey'));
$presets = data_get_available_presets($context);
$strimport = get_string('import');
$strfromfile = get_string('fromfile', 'data');
$strchooseorupload = get_string('chooseorupload', 'data');
$strusestandard = get_string('usestandard', 'data');
$strchoose = get_string('choose');
$strexport = get_string('export', 'data');
$strexportaszip = get_string('exportaszip', 'data');
$strsaveaspreset = get_string('saveaspreset', 'data');
$strsave = get_string('save', 'data');
$strdelete = get_string('delete');
echo '<div style="text-align:center">';
echo '<table class="presets" cellpadding="5">';
echo '<tr><td valign="top" colspan="2" align="center"><h3>'.$strexport.'</h3></td></tr>';
echo '<tr><td><label>'.$strexportaszip.'</label>';
helpbutton('exportzip', '', 'data');
echo '</td><td>';
$options = new object();
$options->action = 'export';
$options->d = $data->id;
$options->sesskey = sesskey();
print_single_button('preset.php', $options, $strexport, 'post');
echo '</td></tr>';
echo '<tr><td><label>'.$strsaveaspreset.'</label>';
helpbutton('savepreset', '', 'data');
echo '</td><td>';
$options = new object();
$options->action = 'save1';
$options->d = $data->id;
$options->sesskey = sesskey();
print_single_button('preset.php', $options, $strsave, 'post');
echo '</td></tr>';
echo '<tr><td valign="top" colspan="2" align="center"><h3>'.$strimport.'</h3></td></tr>';
echo '<tr><td><label for="fromfile">'.$strfromfile.'</label>';
helpbutton('importfromfile', '', 'data');
echo '</td><td>';
echo '<form id="uploadpreset" method="post" action="preset.php">';
echo '<fieldset class="invisiblefieldset">';
echo '<input type="hidden" name="d" value="'.$data->id.'" />';
echo '<input type="hidden" name="action" value="importzip" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input name="file" size="20" value="" id="fromfile" type="text" /><input name="coursefiles" value="'.$strchooseorupload.'" onclick="return openpopup('."'/files/index.php?id=2&amp;choose=uploadpreset.file', 'coursefiles', 'menubar=0,location=0,scrollbars,resizable,width=750,height=500', 0".');" type="button" />';
echo '<input type="submit" value="'.$strimport.'" />';
echo '</fieldset></form>';
echo '</td></tr>';
echo '<tr valign="top"><td><label>'.$strusestandard.'</label>';
helpbutton('usepreset', '', 'data');
echo '</td><td>';
echo '<form id="presets" method="post" action="preset.php" >';
echo '<fieldset class="invisiblefieldset">';
echo '<input type="hidden" name="d" value="'.$data->id.'" />';
echo '<input type="hidden" name="action" value="importpreset" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
$i = 0;
foreach ($presets as $id => $preset) {
$screenshot = '';
if (!empty($preset->user_id)) {
$user = get_record('user', 'id', $preset->user_id);
$desc = $preset->name.' ('.fullname($user, true).')';
} else {
$desc = $preset->name;
}
if (!empty($preset->screenshot)) {
$screenshot = '<img width="150" class="presetscreenshot" src="'.$preset->screenshot.'" alt="'.get_string('screenshot').' '.$desc.'" />&nbsp;';
}
$fullname = $preset->user_id.'/'.$preset->shortname;
$dellink = '';
if ($preset->user_id > 0 and ($preset->user_id == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
$dellink = '&nbsp;<a href="preset.php?d='.$data->id.'&amp;action=confirmdelete&amp;fullname='.$fullname.'&amp;sesskey='.sesskey().'">'.
'<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.' '.$desc.'" /></a>';
}
echo '<input type="radio" name="fullname" id="usepreset'.$i.'" value="'.$fullname.'" /><label for="usepreset'.$i++.'">'.$desc.'</label>'.$dellink.'<br />';
}
echo '<br />';
echo '<input type="submit" value="'.$strchoose.'" />';
echo '</fieldset></form>';
echo '</td></tr>';
echo '</table>';
echo '</div>';
print_footer($course);
?>

View File

@ -0,0 +1,65 @@
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/**
* Unit tests for mod/data/preset_class.php.
*
* @author nicolas@moodle.com
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package mod_data
*/
/** $Id */
require_once(dirname(__FILE__) . '/../../config.php');
global $CFG;
require_once($CFG->libdir . '/simpletestlib.php');
require_once('../preset_class.php');
class data_preset_test extends UnitTestCase {
function setUp() {
}
function tearDown() {
}
function test_address_in_subnet() {
}
/**
* Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
* works as expected.
*/
function test_check_browser_version()
{
}
function test_optional_param()
{
}
}
?>

View File

@ -2677,6 +2677,12 @@ body#user-index .rolesform {
.datapreferences {
text-align:center;
}
table.presets {
margin-left: auto;
margin-right: auto;
}
/***
*** Modules: Forum
***/