/**
  * 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 . $dirpath;
     /// Get the correct dirs
     if (!empty($XMLDB->dbdirs)) {
         $dbdir =& $XMLDB->dbdirs[$dirpath];
     } else {
         return false;
     }
     if (!empty($XMLDB->editeddirs)) {
         $editeddir =& $XMLDB->editeddirs[$dirpath];
         $structure =& $editeddir->xml_file->getStructure();
     }
     /// ADD YOUR CODE HERE
     $statementparam = optional_param('statement', NULL, PARAM_CLEAN);
     /// If no statement, then we are coming for a new one. Look for
     /// type and table and build the correct statementparam
     if (!$statementparam) {
         $typeparam = optional_param('type', NULL, PARAM_CLEAN);
         $tableparam = optional_param('table', NULL, PARAM_CLEAN);
         $typename = xmldb_statement::getXMLDBStatementName($typeparam);
         $statementparam = trim(strtolower($typename . ' ' . $tableparam));
     }
     if (!($statement =& $structure->getStatement($statementparam))) {
         /// Arriving here from a name change, looking for the new statement name
         $statementname = required_param('name', PARAM_CLEAN);
         $statement =& $structure->getStatement($statementparam);
     }
     $dbdir =& $XMLDB->dbdirs[$dirpath];
     $origstructure =& $dbdir->xml_file->getStructure();
     /// Add the main form
     $o = '<form id="form" action="index.php" method="post">';
     $o .= '<div>';
     $o .= '    <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
     $o .= '    <input type="hidden" name ="statement" value="' . $statementparam . '" />';
     $o .= '    <input type="hidden" name ="action" value="edit_statement_save" />';
     $o .= '    <input type="hidden" name ="postaction" value="edit_statement" />';
     $o .= '    <table id="formelements" class="boxaligncenter">';
     $o .= '      <tr valign="top"><td>Name:</td><td><input type="hidden" name ="name" value="' . s($statement->getName()) . '" />' . s($statement->getName()) . '</td></tr>';
     $o .= '      <tr valign="top"><td><label for="comment" accesskey="c">Comment:</label></td><td><textarea name="comment" rows="3" cols="80" id="comment">' . s($statement->getComment()) . '</textarea></td></tr>';
     $o .= '      <tr valign="top"><td>&nbsp;</td><td><input type="submit" value="' . $this->str['change'] . '" /></td></tr>';
     $o .= '    </table>';
     $o .= '</div></form>';
     /// Calculate the buttons
     $b = ' <p class="centerpara buttons">';
     /// The view original XML button
     if ($origstructure->getStatement($statementparam)) {
         $b .= '&nbsp;<a href="index.php?action=view_statement_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=original&amp;statement=' . $statementparam . '">[' . $this->str['vieworiginal'] . ']</a>';
     } else {
         $b .= '&nbsp;[' . $this->str['vieworiginal'] . ']';
     }
     /// The view edited XML button
     if ($statement->hasChanged()) {
         $b .= '&nbsp;<a href="index.php?action=view_statement_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=edited&amp;statement=' . $statementparam . '">[' . $this->str['viewedited'] . ']</a>';
     } else {
         $b .= '&nbsp;[' . $this->str['viewedited'] . ']';
     }
     /// The new sentence button
     $b .= '&nbsp;<a href="index.php?action=new_sentence&amp;postaction=edit_sentence&amp;statement=' . $statementparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['newsentence'] . ']</a>';
     /// The back to edit xml file button
     $b .= '&nbsp;<a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
     $b .= '</p>';
     $o .= $b;
     /// Delete any 'changeme' sentence
     ///$statement->deleteSentence('changeme');
     /// Add the fields list
     $sentences =& $statement->getSentences();
     if (!empty($sentences)) {
         $o .= '<h3 class="main">' . $this->str['sentences'] . '</h3>';
         $o .= '<table id="listfields" border="0" cellpadding="5" cellspacing="1" class="boxaligncenter flexible">';
         $row = 0;
         foreach ($sentences as $key => $sentence) {
             /// Prepend some SQL
             if ($statement->getType() == XMLDB_STATEMENT_INSERT) {
                 $p = 'INSERT INTO ' . $statement->getTable() . ' ';
             } else {
                 $p = 'UNSUPPORTED SENTENCE TYPE ';
             }
             /// Calculate buttons
             $b = '</td><td class="button cell">';
             /// The edit button
             $b .= '<a href="index.php?action=edit_sentence&amp;sentence=' . $key . '&amp;statement=' . urlencode($statement->getName()) . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['edit'] . ']</a>';
             $b .= '</td><td class="button cell">';
             /// The duplicate button
             $b .= '<a href="index.php?action=new_sentence&amp;postaction=edit_sentence&amp;basesentence=' . $key . '&amp;statement=' . urlencode($statement->getName()) . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['duplicate'] . ']</a>';
             $b .= '</td><td class="button cell">';
             /// The delete button
             $b .= '<a href="index.php?action=delete_sentence&amp;sentence=' . $key . '&amp;statement=' . urlencode($statement->getName()) . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['delete'] . ']</a>';
             $b .= '</td>';
             /// Print table row
             $o .= '<tr class="r' . $row . '"><td class="table cell">' . $p . $sentence . $b . '</tr>';
             $row = ($row + 1) % 2;
         }
         $o .= '</table>';
     }
     $this->output = $o;
     /// Launch postaction if exists (leave this here!)
     if ($this->getPostAction() && $result) {
         return $this->launch($this->getPostAction());
     }
     /// Return ok if arrived here
     return $result;
 }
 /**
  * Load data from XML to the structure
  */
 function arr2xmldb_structure($xmlarr)
 {
     global $CFG;
     $result = true;
     /// Debug the structure
     /// traverse_xmlize($xmlarr);                   //Debug
     /// print_object ($GLOBALS['traverse_array']);  //Debug
     /// $GLOBALS['traverse_array']="";              //Debug
     /// Process structure attributes (path, comment and version)
     if (isset($xmlarr['XMLDB']['@']['PATH'])) {
         $this->path = trim($xmlarr['XMLDB']['@']['PATH']);
     } else {
         $this->errormsg = 'Missing PATH attribute';
         $this->debug($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['XMLDB']['@']['VERSION'])) {
         $this->version = trim($xmlarr['XMLDB']['@']['VERSION']);
     } else {
         $this->errormsg = 'Missing VERSION attribute';
         $this->debug($this->errormsg);
         $result = false;
     }
     if (isset($xmlarr['XMLDB']['@']['COMMENT'])) {
         $this->comment = trim($xmlarr['XMLDB']['@']['COMMENT']);
     } else {
         if (!empty($CFG->xmldbdisablecommentchecking)) {
             $this->comment = '';
         } else {
             $this->errormsg = 'Missing COMMENT attribute';
             $this->debug($this->errormsg);
             $result = false;
         }
     }
     /// Iterate over tables
     if (isset($xmlarr['XMLDB']['#']['TABLES']['0']['#']['TABLE'])) {
         foreach ($xmlarr['XMLDB']['#']['TABLES']['0']['#']['TABLE'] as $xmltable) {
             if (!$result) {
                 //Skip on error
                 continue;
             }
             $name = trim($xmltable['@']['NAME']);
             $table = new xmldb_table($name);
             $table->arr2xmldb_table($xmltable);
             $this->tables[] = $table;
             if (!$table->isLoaded()) {
                 $this->errormsg = 'Problem loading table ' . $name;
                 $this->debug($this->errormsg);
                 $result = false;
             }
         }
     } else {
         $this->errormsg = 'Missing TABLES section';
         $this->debug($this->errormsg);
         $result = false;
     }
     /// Perform some general checks over tables
     if ($result && $this->tables) {
         /// Check tables names are ok (lowercase, a-z _-)
         if (!$this->checkNameValues($this->tables)) {
             $this->errormsg = 'Some TABLES name values are incorrect';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// Check previous & next are ok (duplicates and existing tables)
         $this->fixPrevNext($this->tables);
         if ($result && !$this->checkPreviousNextValues($this->tables)) {
             $this->errormsg = 'Some TABLES previous/next values are incorrect';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// Order tables
         if ($result && !$this->orderTables($this->tables)) {
             $this->errormsg = 'Error ordering the tables';
             $this->debug($this->errormsg);
             $result = false;
         }
     }
     /// Iterate over statements
     if (isset($xmlarr['XMLDB']['#']['STATEMENTS']['0']['#']['STATEMENT'])) {
         foreach ($xmlarr['XMLDB']['#']['STATEMENTS']['0']['#']['STATEMENT'] as $xmlstatement) {
             if (!$result) {
                 //Skip on error
                 continue;
             }
             $name = trim($xmlstatement['@']['NAME']);
             $statement = new xmldb_statement($name);
             $statement->arr2xmldb_statement($xmlstatement);
             $this->statements[] = $statement;
             if (!$statement->isLoaded()) {
                 $this->errormsg = 'Problem loading statement ' . $name;
                 $this->debug($this->errormsg);
                 $result = false;
             }
         }
     }
     /// Perform some general checks over statements
     if ($result && $this->statements) {
         /// Check statements names are ok (lowercase, a-z _-)
         if (!$this->checkNameValues($this->statements)) {
             $this->errormsg = 'Some STATEMENTS name values are incorrect';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// Check previous & next are ok (duplicates and existing statements)
         $this->fixPrevNext($this->statements);
         if ($result && !$this->checkPreviousNextValues($this->statements)) {
             $this->errormsg = 'Some STATEMENTS previous/next values are incorrect';
             $this->debug($this->errormsg);
             $result = false;
         }
         /// Order statements
         if ($result && !$this->orderStatements($this->statements)) {
             $this->errormsg = 'Error ordering the statements';
             $this->debug($this->errormsg);
             $result = false;
         }
     }
     /// Set some attributes
     if ($result) {
         $this->loaded = true;
     }
     $this->calculateHash();
     return $result;
 }
 function __construct($name)
 {
     parent::__construct($name);
 }
 /**
  * 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, $DB, $OUTPUT;
     /// Do the job, setting result as needed
     /// Get the dir containing the file
     $dirpath = required_param('dir', PARAM_PATH);
     $dirpath = $CFG->dirroot . $dirpath;
     /// Get the correct dirs
     if (!empty($XMLDB->dbdirs)) {
         $dbdir =& $XMLDB->dbdirs[$dirpath];
     } else {
         return false;
     }
     if (!empty($XMLDB->editeddirs)) {
         $editeddir =& $XMLDB->editeddirs[$dirpath];
         $structure =& $editeddir->xml_file->getStructure();
     }
     /// ADD YOUR CODE HERE
     $tableparam = optional_param('table', NULL, PARAM_CLEAN);
     $typeparam = optional_param('type', NULL, PARAM_CLEAN);
     /// If no table or type, show form
     if (!$tableparam || !$typeparam) {
         /// No postaction here
         $this->postaction = NULL;
         /// Get list of tables
         $selecttables = $DB->get_tables();
         /// Get list of statement types
         $typeoptions = array(XMLDB_STATEMENT_INSERT => xmldb_statement::getXMLDBStatementName(XMLDB_STATEMENT_INSERT), XMLDB_STATEMENT_UPDATE => xmldb_statement::getXMLDBStatementName(XMLDB_STATEMENT_UPDATE), XMLDB_STATEMENT_DELETE => xmldb_statement::getXMLDBStatementName(XMLDB_STATEMENT_DELETE), XMLDB_STATEMENT_CUSTOM => xmldb_statement::getXMLDBStatementName(XMLDB_STATEMENT_CUSTOM));
         if (!$selecttables) {
             $this->errormsg = 'No tables available to create statements';
             return false;
         }
         /// Now build the form
         $o = '<form id="form" action="index.php" method="post">';
         $o .= '<div>';
         $o .= '    <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
         $o .= '    <input type="hidden" name ="action" value="new_statement" />';
         $o .= '    <input type="hidden" name ="postaction" value="edit_statement" />';
         $o .= '    <table id="formelements" class="boxaligncenter" cellpadding="5">';
         $o .= '      <tr><td><label for="type" accesskey="t">' . $this->str['statementtype'] . ' </label>' . $OUTPUT->select(html_select::make($typeoptions, 'type')) . '<label for="table" accesskey="a">' . $this->str['statementtable'] . ' </label>' . $OUTPUT->select(html_select::make($selecttables, 'table')) . '</td></tr>';
         $o .= '      <tr><td colspan="2" align="center"><input type="submit" value="' . $this->str['create'] . '" /></td></tr>';
         $o .= '      <tr><td colspan="2" align="center"><a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a></td></tr>';
         $o .= '    </table>';
         $o .= '</div></form>';
         $this->output = $o;
         /// If table, retrofit information and, if everything works,
         /// go to the table edit action
     } else {
         /// Get some params (table is mandatory here)
         $tableparam = required_param('table', PARAM_CLEAN);
         $typeparam = required_param('type', PARAM_CLEAN);
         /// Only insert is allowed :-/
         if ($typeparam != XMLDB_STATEMENT_INSERT) {
             $this->errormsg = 'Only insert of records is supported';
             return false;
         }
         /// Calculate the name of the statement
         $typename = xmldb_statement::getXMLDBStatementName($typeparam);
         $name = trim(strtolower($typename . ' ' . $tableparam));
         /// Check that this Statement hasn't been created before
         if ($structure->getStatement($name)) {
             $this->errormsg = 'The statement "' . $name . '" already exists, please use it to add more sentences';
             return false;
         }
         /// Create one new xmldb_statement
         $statement = new xmldb_statement($name);
         $statement->setType($typeparam);
         $statement->setTable($tableparam);
         $statement->setComment('Initial ' . $typename . ' of records on table ' . $tableparam);
         /// Finally, add the whole retroffited table to the structure
         /// in the place specified
         $structure->addStatement($statement);
     }
     /// Launch postaction if exists (leave this here!)
     if ($this->getPostAction() && $result) {
         return $this->launch($this->getPostAction());
     }
     /// Return ok if arrived here
     return $result;
 }