Commit 7274231f authored by Penny Leach's avatar Penny Leach
Browse files

xmldb

parent 318820ef
<?php // $Id: XMLDBAction.class.php,v 1.3 2006/09/20 21:00:48 skodak Exp $
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.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 //
// //
///////////////////////////////////////////////////////////////////////////
/// This is the main action class. It implements all the basic
/// functionalities to be shared by each action.
class XMLDBAction {
var $does_generate; //Type of value returned by the invoke method
//ACTION_GENERATE_HTML have contents to show
//set by each specialized invoke
var $title; //Title of the Action (class name, by default)
//set by parent init automatically
var $str; //Strings used by the action
//set by each specialized init, calling loadStrings
var $output; //Output of the action
//set by each specialized invoke, get with getOutput
var $errormsg; //Last Error produced. Check when any invoke returns false
//get with getError
var $postaction; //Action to execute at the end of the invoke script
/**
* Constructor
*/
function XMLDBAction() {
$this->init();
}
/**
* Constructor to keep PHP5 happy
*/
function __construct() {
$this->XMLDBAction();
}
/**
* Init method, every subclass will have its own,
* always calling the parent one
*/
function init() {
$this->does_generate = ACTION_NONE;
$this->title = strtolower(get_class($this));
$this->str = array();
$this->output = NULL;
$this->errormsg = NULL;
$this->subaction = NULL;
}
/**
* returns the type of output of the file
*/
function getDoesGenerate() {
return $this->does_generate;
}
/**
* getError method, returns the last error string.
* Used if the invoke() methods returns false
*/
function getError() {
return $this->errormsg;
}
/**
* getOutput method, returns the output generated by the action.
* Used after execution of the invoke() methods if they return true
*/
function getOutput() {
return $this->output;
}
/**
* getPostAtion method, returns the action to launch after executing
* another one
*/
function getPostAction() {
return $this->postaction;
}
/**
* getTitle method returns the title of the action (that is part
* of the $str array attribute
*/
function getTitle() {
return $this->str['title'];
}
/**
* loadStrings method, loads the required strings specified in the
* array parameter
*/
function loadStrings($strings) {
/// Load some commonly used strings
$this->str['title'] = get_string($this->title, 'xmldb');
/// Now process the $strings array loading it in the $str atribute
if ($strings) {
foreach ($strings as $key => $module) {
$this->str[$key] = get_string($key, $module);
}
}
}
/**
* main invoke method, it simply sets the postaction attribute
* if possible
*/
function invoke() {
global $SESSION;
/// If we are used any dir, save it in the lastused session object
/// Some actions can use it to perform positioning
if ($lastused = optional_param ('dir', NULL, PARAM_PATH)) {
$SESSION->lastused = stripslashes_safe($lastused);
}
$this->postaction = optional_param ('postaction', NULL, PARAM_ALPHAEXT);
/// Avoid being recursive
if ($this->title == $this->postaction) {
$this->postaction = NULL;
}
}
/**
* launch method, used to easily call invoke methods between actions
*/
function launch($action) {
global $CFG;
/// Get the action path and invoke it
$actionsroot = "$CFG->dirroot/$CFG->admin/xmldb/actions";
$actionclass = $action . '.class.php';
$actionpath = "$actionsroot/$action/$actionclass";
/// Load and invoke the proper action
$result = false;
if (file_exists($actionpath) && is_readable($actionpath)) {
require_once($actionpath);
if ($xmldb_action = new $action) {
$result = $xmldb_action->invoke();
if ($result) {
if ($xmldb_action->does_generate != ACTION_NONE &&
$xmldb_action->getOutput()) {
$this->does_generate = $xmldb_action->does_generate;
$this->title = $xmldb_action->title;
$this->str = $xmldb_action->str;
$this->output .= $xmldb_action->getOutput();
}
} else {
$this->errormsg = $xmldb_action->getError();
}
} else {
$this->errormsg = "Error: cannot instantiate class (actions/$action/$actionclass)";
}
} else {
$this->errormsg = "Error: wrong action specified ($action)";
}
return $result;
}
}
?>
<?php // $Id: create_xml_file.class.php,v 1.3 2006/09/20 21:00:49 skodak Exp $
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.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 //
// //
///////////////////////////////////////////////////////////////////////////
/// This class will
class create_xml_file extends XMLDBAction {
/**
* Init method, every subclass will have its own
*/
function init() {
parent::init();
/// Set own core attributes
$this->can_subaction = ACTION_NONE;
//$this->can_subaction = ACTION_HAVE_SUBACTIONS;
/// Set own custom attributes
/// Get needed strings
$this->loadStrings(array(
/// 'key' => 'module',
));
}
/**
* Invoke method, every class will have its own
* returns true/false on completion, setting both
* errormsg and output as necessary
*/
function invoke() {
parent::invoke();
$result = true;
/// Set own core attributes
$this->does_generate = ACTION_NONE;
//$this->does_generate = ACTION_GENERATE_HTML;
/// These are always here
global $CFG, $XMLDB;
/// Do the job, setting result as needed
/// Get the dir containing the file
$dirpath = required_param('dir', PARAM_PATH);
$dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
$file = $dirpath . '/install.xml';
/// Some variables
$xmlpath = dirname(str_replace($CFG->dirroot . '/', '', $file));
$xmlversion = userdate(time(), '%Y%m%d', 99, false);
$xmlcomment = 'XMLDB file for Moodle ' . dirname($xmlpath);
$xmltable = strtolower(basename(dirname($xmlpath)));
/// Initial contents
$c = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
$c.= ' <XMLDB PATH="' . $xmlpath . '" VERSION="' . $xmlversion .'" COMMENT="' . $xmlcomment .'">' . "\n";
$c.= ' <TABLES>' . "\n";
$c.= ' <TABLE NAME="' . $xmltable . '" COMMENT="Default comment for ' . $xmltable .', please edit me">' . "\n";
$c.= ' <FIELDS>' . "\n";
$c.= ' <FIELD NAME="id" TYPE="int" LENGTH="10" UNSIGNED="true" NOTNULL="true" SEQUENCE="true" COMMENT="id of the table, please edit me" />' . "\n";
$c.= ' </FIELDS>' . "\n";
$c.= ' <KEYS>' . "\n";
$c.= ' <KEY NAME="primary" COMMENT="Primary key for ' . $xmltable . '" TYPE="primary" FIELDS="id" />' . "\n";
$c.= ' </KEYS>' . "\n";
$c.= ' </TABLE>' . "\n";
$c.= ' </TABLES>' . "\n";
$c.= ' </XMLDB>';
if (!file_put_contents($file, $c)) {
$errormsg = 'Error creando fichero ' . $file;
$result = false;
}
/// Launch postaction if exists
if ($this->getPostAction() && $result) {
return $this->launch($this->getPostAction());
}
/// Return ok if arrived here
return $result;
}
}
?>
<?php // $Id: delete_field.class.php,v 1.3 2006/09/20 21:00:50 skodak Exp $
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.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 //
// //
///////////////////////////////////////////////////////////////////////////
/// This class will delete completely one field
class delete_field extends XMLDBAction {
/**
* Init method, every subclass will have its own
*/
function init() {
parent::init();
/// Set own custom attributes
/// Get needed strings
$this->loadStrings(array(
'confirmdeletefield' => 'xmldb',
'yes' => '',
'no' => ''
));
}
/**
* Invoke method, every class will have its own
* returns true/false on completion, setting both
* errormsg and output as necessary
*/
function invoke() {
parent::invoke();
$result = true;
/// Set own core attributes
$this->does_generate = ACTION_GENERATE_HTML;
/// These are always here
global $CFG, $XMLDB;
/// Do the job, setting result as needed
/// Get the dir containing the file
$dirpath = required_param('dir', PARAM_PATH);
$dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
$tableparam = required_param('table', PARAM_CLEAN);
$fieldparam = required_param('field', PARAM_CLEAN);
$confirmed = optional_param('confirmed', false, PARAM_BOOL);
/// If not confirmed, show confirmation box
if (!$confirmed) {
$o = '<table align="center" width="60" class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">';
$o.= ' <tr><td class="generalboxcontent">';
$o.= ' <p align="center">' . $this->str['confirmdeletefield'] . '<br /><br />' . $fieldparam . '</p>';
$o.= ' <table align="center" cellpadding="20"><tr><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=delete_field&amp;confirmed=yes&amp;postaction=edit_table&amp;field=' . $fieldparam . '&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '" method="post">';
$o.= ' <input type="submit" value="'. $this->str['yes'] .'" /></form></div>';
$o.= ' </td><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=edit_table&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '" method="post">';
$o.= ' <input type="submit" value="'. $this->str['no'] .'" /></form></div>';
$o.= ' </td></tr>';
$o.= ' </table>';
$o.= ' </td></tr>';
$o.= '</table>';
$this->output = $o;
} else {
/// Get the edited dir
if (!empty($XMLDB->editeddirs)) {
if (isset($XMLDB->editeddirs[$dirpath])) {
$dbdir =& $XMLDB->dbdirs[$dirpath];
$editeddir =& $XMLDB->editeddirs[$dirpath];
if ($editeddir) {
$structure =& $editeddir->xml_file->getStructure();
/// Move adjacent fields prev and next attributes
$tables =& $structure->getTables();
$table =& $structure->getTable($tableparam);
$fields =& $table->getFields();
$field =& $table->getField($fieldparam);
if ($field->getPrevious()) {
$prev =& $table->getField($field->getPrevious());
$prev->setNext($field->getNext());
}
if ($field->getNext()) {
$next =& $table->getField($field->getNext());
$next->setPrevious($field->getPrevious());
}
/// Remove the field
$table->deleteField($fieldparam);
/// Recalculate the hash
$structure->calculateHash(true);
/// If the hash has changed from the original one, change the version
/// and mark the structure as changed
$origstructure =& $dbdir->xml_file->getStructure();
if ($structure->getHash() != $origstructure->getHash()) {
$structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
$structure->setChanged(true);
}
}
}
}
}
/// Launch postaction if exists (leave this here!)
if ($this->getPostAction() && $result) {
return $this->launch($this->getPostAction());
}
/// Return ok if arrived here
return $result;
}
}
?>
<?php // $Id: delete_index.class.php,v 1.3 2006/09/20 21:00:47 skodak Exp $
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.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 //
// //
///////////////////////////////////////////////////////////////////////////
/// This class will delete completely one index
class delete_index extends XMLDBAction {
/**
* Init method, every subclass will have its own
*/
function init() {
parent::init();
/// Set own custom attributes
/// Get needed strings
$this->loadStrings(array(
'confirmdeleteindex' => 'xmldb',
'yes' => '',
'no' => ''
));
}
/**
* Invoke method, every class will have its own
* returns true/false on completion, setting both
* errormsg and output as necessary
*/
function invoke() {
parent::invoke();
$result = true;
/// Set own core attributes
$this->does_generate = ACTION_GENERATE_HTML;
/// These are always here
global $CFG, $XMLDB;
/// Do the job, setting result as needed
/// Get the dir containing the file
$dirpath = required_param('dir', PARAM_PATH);
$dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
$tableparam = required_param('table', PARAM_PATH);
$indexparam = required_param('index', PARAM_PATH);
$confirmed = optional_param('confirmed', false, PARAM_BOOL);
/// If not confirmed, show confirmation box
if (!$confirmed) {
$o = '<table align="center" width="60" class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">';
$o.= ' <tr><td class="generalboxcontent">';
$o.= ' <p align="center">' . $this->str['confirmdeleteindex'] . '<br /><br />' . $indexparam . '</p>';
$o.= ' <table align="center" cellpadding="20"><tr><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=delete_index&amp;confirmed=yes&amp;postaction=edit_table&amp;index=' . $indexparam . '&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '" method="post">';
$o.= ' <input type="submit" value="'. $this->str['yes'] .'" /></form></div>';
$o.= ' </td><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=edit_table&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '" method="post">';
$o.= ' <input type="submit" value="'. $this->str['no'] .'" /></form></div>';
$o.= ' </td></tr>';
$o.= ' </table>';
$o.= ' </td></tr>';
$o.= '</table>';
$this->output = $o;
} else {
/// Get the edited dir
if (!empty($XMLDB->editeddirs)) {
if (isset($XMLDB->editeddirs[$dirpath])) {
$dbdir =& $XMLDB->dbdirs[$dirpath];
$editeddir =& $XMLDB->editeddirs[$dirpath];
if ($editeddir) {
$structure =& $editeddir->xml_file->getStructure();
/// Move adjacent indexes prev and next attributes
$tables =& $structure->getTables();
$table =& $structure->getTable($tableparam);
$indexes =& $table->getIndexes();
$index =& $table->getIndex($indexparam);
if ($index->getPrevious()) {
$prev =& $table->getIndex($index->getPrevious());
$prev->setNext($index->getNext());
}
if ($index->getNext()) {
$next =& $table->getIndex($index->getNext());
$next->setPrevious($index->getPrevious());
}
/// Remove the index
$table->deleteIndex($indexparam);
/// Recalculate the hash
$structure->calculateHash(true);
/// If the hash has changed from the original one, change the version
/// and mark the structure as changed
$origstructure =& $dbdir->xml_file->getStructure();
if ($structure->getHash() != $origstructure->getHash()) {
$structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
$structure->setChanged(true);
}
}
}
}
}
/// Launch postaction if exists (leave this here!)
if ($this->getPostAction() && $result) {
return $this->launch($this->getPostAction());
}
/// Return ok if arrived here
return $result;
}
}
?>
<?php // $Id: delete_key.class.php,v 1.3 2006/09/20 21:00:54 skodak Exp $
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //