/admin/xmldb/actions/create_xml_file/create_xml_file.class.php

https://github.com/jarednipper/HSU-common-code · PHP · 108 lines · 44 code · 18 blank · 46 comment · 3 complexity · 2acede63cf056f0a757c5d131530352e MD5 · raw file

  1. <?php // $Id: create_xml_file.class.php,v 1.5 2007/10/10 05:25:28 nicolasconnault Exp $
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.com //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
  10. // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
  11. // //
  12. // This program is free software; you can redistribute it and/or modify //
  13. // it under the terms of the GNU General Public License as published by //
  14. // the Free Software Foundation; either version 2 of the License, or //
  15. // (at your option) any later version. //
  16. // //
  17. // This program is distributed in the hope that it will be useful, //
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  20. // GNU General Public License for more details: //
  21. // //
  22. // http://www.gnu.org/copyleft/gpl.html //
  23. // //
  24. ///////////////////////////////////////////////////////////////////////////
  25. /// This class will
  26. class create_xml_file extends XMLDBAction {
  27. /**
  28. * Init method, every subclass will have its own
  29. */
  30. function init() {
  31. parent::init();
  32. /// Set own core attributes
  33. $this->can_subaction = ACTION_NONE;
  34. //$this->can_subaction = ACTION_HAVE_SUBACTIONS;
  35. /// Set own custom attributes
  36. /// Get needed strings
  37. $this->loadStrings(array(
  38. /// 'key' => 'module',
  39. ));
  40. }
  41. /**
  42. * Invoke method, every class will have its own
  43. * returns true/false on completion, setting both
  44. * errormsg and output as necessary
  45. */
  46. function invoke() {
  47. parent::invoke();
  48. $result = true;
  49. /// Set own core attributes
  50. $this->does_generate = ACTION_NONE;
  51. //$this->does_generate = ACTION_GENERATE_HTML;
  52. /// These are always here
  53. global $CFG, $XMLDB;
  54. /// Do the job, setting result as needed
  55. /// Get the dir containing the file
  56. $dirpath = required_param('dir', PARAM_PATH);
  57. $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
  58. $file = $dirpath . '/install.xml';
  59. /// Some variables
  60. $xmlpath = dirname(str_replace($CFG->dirroot . '/', '', $file));
  61. $xmlversion = userdate(time(), '%Y%m%d', 99, false);
  62. $xmlcomment = 'XMLDB file for Moodle ' . dirname($xmlpath);
  63. $xmltable = strtolower(basename(dirname($xmlpath)));
  64. /// Initial contents
  65. $c = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
  66. $c.= ' <XMLDB PATH="' . $xmlpath . '" VERSION="' . $xmlversion .'" COMMENT="' . $xmlcomment .'">' . "\n";
  67. $c.= ' <TABLES>' . "\n";
  68. $c.= ' <TABLE NAME="' . $xmltable . '" COMMENT="Default comment for ' . $xmltable .', please edit me">' . "\n";
  69. $c.= ' <FIELDS>' . "\n";
  70. $c.= ' <FIELD NAME="id" TYPE="int" LENGTH="10" UNSIGNED="true" NOTNULL="true" SEQUENCE="true" />' . "\n";
  71. $c.= ' </FIELDS>' . "\n";
  72. $c.= ' <KEYS>' . "\n";
  73. $c.= ' <KEY NAME="primary" TYPE="primary" FIELDS="id" />' . "\n";
  74. $c.= ' </KEYS>' . "\n";
  75. $c.= ' </TABLE>' . "\n";
  76. $c.= ' </TABLES>' . "\n";
  77. $c.= ' </XMLDB>';
  78. if (!file_put_contents($file, $c)) {
  79. $errormsg = 'Error creando fichero ' . $file;
  80. $result = false;
  81. }
  82. /// Launch postaction if exists
  83. if ($this->getPostAction() && $result) {
  84. return $this->launch($this->getPostAction());
  85. }
  86. /// Return ok if arrived here
  87. return $result;
  88. }
  89. }
  90. ?>