[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/admin/xmldb/actions/edit_sentence_save/ -> edit_sentence_save.class.php (source)

   1  <?php // $Id: edit_sentence_save.class.php,v 1.5 2007/10/10 05:25:25 nicolasconnault Exp $
   2  
   3  ///////////////////////////////////////////////////////////////////////////
   4  //                                                                       //
   5  // NOTICE OF COPYRIGHT                                                   //
   6  //                                                                       //
   7  // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
   8  //          http://moodle.com                                            //
   9  //                                                                       //
  10  // Copyright (C) 1999 onwards Martin Dougiamas        http://dougiamas.com  //
  11  //           (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com  //
  12  //                                                                       //
  13  // This program is free software; you can redistribute it and/or modify  //
  14  // it under the terms of the GNU General Public License as published by  //
  15  // the Free Software Foundation; either version 2 of the License, or     //
  16  // (at your option) any later version.                                   //
  17  //                                                                       //
  18  // This program is distributed in the hope that it will be useful,       //
  19  // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
  20  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
  21  // GNU General Public License for more details:                          //
  22  //                                                                       //
  23  //          http://www.gnu.org/copyleft/gpl.html                         //
  24  //                                                                       //
  25  ///////////////////////////////////////////////////////////////////////////
  26  
  27  /// This class will save the changes performed to one sentence
  28  
  29  class edit_sentence_save extends XMLDBAction {
  30  
  31      /**
  32       * Init method, every subclass will have its own
  33       */
  34      function init() {
  35          parent::init();
  36  
  37      /// Set own custom attributes
  38  
  39      /// Get needed strings
  40          $this->loadStrings(array(
  41              'cannotuseidfield' => 'xmldb',
  42              'missingfieldsinsentence' => 'xmldb',
  43              'missingvaluesinsentence' => 'xmldb',
  44              'wrongnumberoffieldsorvalues' => 'xmldb',
  45              'administration' => ''
  46          ));
  47      }
  48  
  49      /**
  50       * Invoke method, every class will have its own
  51       * returns true/false on completion, setting both
  52       * errormsg and output as necessary
  53       */
  54      function invoke() {
  55          parent::invoke();
  56  
  57          $result = true;
  58  
  59      /// Set own core attributes
  60          $this->does_generate = ACTION_NONE;
  61          //$this->does_generate = ACTION_GENERATE_HTML;
  62  
  63      /// These are always here
  64          global $CFG, $XMLDB;
  65  
  66      /// Do the job, setting result as needed
  67  
  68      /// Get parameters
  69          $dirpath = required_param('dir', PARAM_PATH);
  70          $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
  71  
  72          $statementparam = strtolower(required_param('statement', PARAM_CLEAN));
  73          $sentenceparam = strtolower(required_param('sentence', PARAM_ALPHANUM));
  74  
  75          $fields = required_param('fields', PARAM_CLEAN);
  76          $fields = trim(stripslashes_safe($fields));
  77          $values = required_param('values', PARAM_CLEAN);
  78          $values = trim(stripslashes_safe($values));
  79  
  80          $editeddir =& $XMLDB->editeddirs[$dirpath];
  81          $structure =& $editeddir->xml_file->getStructure();
  82          $statement =& $structure->getStatement($statementparam);
  83          $sentences =& $statement->getSentences();
  84  
  85          $oldsentence = $sentences[$sentenceparam];
  86  
  87          if (!$statement) {
  88              $this->errormsg = 'Wrong statement specified: ' . $statementparam;
  89              return false;
  90          }
  91  
  92      /// For now, only insert sentences are allowed
  93          if ($statement->getType() != XMLDB_STATEMENT_INSERT) {
  94              $this->errormsg = 'Wrong Statement Type. Only INSERT allowed';
  95              return false;
  96          }
  97  
  98          $errors = array();    /// To store all the errors found
  99  
 100      /// Build the whole sentence
 101          $sentence = '(' . $fields . ') VALUES (' . $values . ')';
 102  
 103      /// Perform some checks
 104          $fields = $statement->getFieldsFromInsertSentence($sentence);
 105          $values = $statement->getValuesFromInsertSentence($sentence);
 106  
 107          if (in_array('id', $fields)) {
 108              $errors[] = $this->str['cannotuseidfield'];
 109          }
 110          if ($result && count($fields) == 0) {
 111              $errors[] = $this->str['missingfieldsinsentence'];
 112          }
 113          if ($result && count($values) == 0) {
 114              $errors[] = $this->str['missingvaluesinsentence'];
 115          }
 116          if ($result && count($fields) != count($values)) {
 117              $errors[] = $this->str['wrongnumberoffieldsorvalues'];
 118          }
 119  
 120          if (!empty($errors)) {
 121          /// Prepare the output
 122              $site = get_site();
 123              $navlinks = array();
 124              $navlinks[] = array('name' => $this->str['administration'], 'link' => '../index.php', 'type' => 'misc');
 125              $navlinks[] = array('name' => 'XMLDB', 'link' => 'index.php', 'type' => 'misc');
 126              $navigation = build_navigation($navlinks);
 127              print_header("$site->shortname: XMLDB", "$site->fullname", $navigation);
 128              notice ('<p>' .implode(', ', $errors) . '</p>
 129                       <p>' . s($sentence),
 130                      'index.php?action=edit_sentence&amp;sentence=' .$sentenceparam . '&amp;statement=' . urlencode($statementparam) . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)));
 131              die; /// re-die :-P
 132          }
 133  
 134      /// Continue if we aren't under errors
 135          if (empty($errors)) {
 136              $sentences[$sentenceparam] = $sentence;
 137  
 138          /// If the sentence has changed from the old one, change the version
 139          /// and mark the statement and structure as changed
 140              if ($oldsentence != $sentence) {
 141                  $statement->setChanged(true);
 142                  $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
 143              /// Mark as changed
 144                  $structure->setChanged(true);
 145              }
 146  
 147          /// Launch postaction if exists (leave this here!)
 148              if ($this->getPostAction() && $result) {
 149                  return $this->launch($this->getPostAction());
 150              }
 151          }
 152  
 153      /// Return ok if arrived here
 154          return $result;
 155      }
 156  }
 157  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7