PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/xmldb/actions/edit_sentence_save/edit_sentence_save.class.php

https://github.com/nadavkav/MoodleTAO
PHP | 157 lines | 81 code | 27 blank | 49 comment | 19 complexity | 38942a74362060752968d67dfe5c610b MD5 | raw file
  1. <?php // $Id$
  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 save the changes performed to one sentence
  26. class edit_sentence_save extends XMLDBAction {
  27. /**
  28. * Init method, every subclass will have its own
  29. */
  30. function init() {
  31. parent::init();
  32. /// Set own custom attributes
  33. /// Get needed strings
  34. $this->loadStrings(array(
  35. 'cannotuseidfield' => 'xmldb',
  36. 'missingfieldsinsentence' => 'xmldb',
  37. 'missingvaluesinsentence' => 'xmldb',
  38. 'wrongnumberoffieldsorvalues' => 'xmldb',
  39. 'administration' => ''
  40. ));
  41. }
  42. /**
  43. * Invoke method, every class will have its own
  44. * returns true/false on completion, setting both
  45. * errormsg and output as necessary
  46. */
  47. function invoke() {
  48. parent::invoke();
  49. $result = true;
  50. /// Set own core attributes
  51. $this->does_generate = ACTION_NONE;
  52. //$this->does_generate = ACTION_GENERATE_HTML;
  53. /// These are always here
  54. global $CFG, $XMLDB;
  55. /// Do the job, setting result as needed
  56. /// Get parameters
  57. $dirpath = required_param('dir', PARAM_PATH);
  58. $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
  59. $statementparam = strtolower(required_param('statement', PARAM_CLEAN));
  60. $sentenceparam = strtolower(required_param('sentence', PARAM_ALPHANUM));
  61. $fields = required_param('fields', PARAM_CLEAN);
  62. $fields = trim(stripslashes_safe($fields));
  63. $values = required_param('values', PARAM_CLEAN);
  64. $values = trim(stripslashes_safe($values));
  65. $editeddir =& $XMLDB->editeddirs[$dirpath];
  66. $structure =& $editeddir->xml_file->getStructure();
  67. $statement =& $structure->getStatement($statementparam);
  68. $sentences =& $statement->getSentences();
  69. $oldsentence = $sentences[$sentenceparam];
  70. if (!$statement) {
  71. $this->errormsg = 'Wrong statement specified: ' . $statementparam;
  72. return false;
  73. }
  74. /// For now, only insert sentences are allowed
  75. if ($statement->getType() != XMLDB_STATEMENT_INSERT) {
  76. $this->errormsg = 'Wrong Statement Type. Only INSERT allowed';
  77. return false;
  78. }
  79. $errors = array(); /// To store all the errors found
  80. /// Build the whole sentence
  81. $sentence = '(' . $fields . ') VALUES (' . $values . ')';
  82. /// Perform some checks
  83. $fields = $statement->getFieldsFromInsertSentence($sentence);
  84. $values = $statement->getValuesFromInsertSentence($sentence);
  85. if (in_array('id', $fields)) {
  86. $errors[] = $this->str['cannotuseidfield'];
  87. }
  88. if ($result && count($fields) == 0) {
  89. $errors[] = $this->str['missingfieldsinsentence'];
  90. }
  91. if ($result && count($values) == 0) {
  92. $errors[] = $this->str['missingvaluesinsentence'];
  93. }
  94. if ($result && count($fields) != count($values)) {
  95. $errors[] = $this->str['wrongnumberoffieldsorvalues'];
  96. }
  97. if (!empty($errors)) {
  98. /// Prepare the output
  99. $site = get_site();
  100. $navlinks = array();
  101. $navlinks[] = array('name' => $this->str['administration'], 'link' => '../index.php', 'type' => 'misc');
  102. $navlinks[] = array('name' => 'XMLDB', 'link' => 'index.php', 'type' => 'misc');
  103. $navigation = build_navigation($navlinks);
  104. print_header("$site->shortname: XMLDB", "$site->fullname", $navigation);
  105. notice ('<p>' .implode(', ', $errors) . '</p>
  106. <p>' . s($sentence),
  107. 'index.php?action=edit_sentence&amp;sentence=' .$sentenceparam . '&amp;statement=' . urlencode($statementparam) . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)));
  108. die; /// re-die :-P
  109. }
  110. /// Continue if we aren't under errors
  111. if (empty($errors)) {
  112. $sentences[$sentenceparam] = $sentence;
  113. /// If the sentence has changed from the old one, change the version
  114. /// and mark the statement and structure as changed
  115. if ($oldsentence != $sentence) {
  116. $statement->setChanged(true);
  117. $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
  118. /// Mark as changed
  119. $structure->setChanged(true);
  120. }
  121. /// Launch postaction if exists (leave this here!)
  122. if ($this->getPostAction() && $result) {
  123. return $this->launch($this->getPostAction());
  124. }
  125. }
  126. /// Return ok if arrived here
  127. return $result;
  128. }
  129. }
  130. ?>