[ Index ]

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

title

Body

[close]

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

   1  <?php // $Id: XMLDBAction.class.php,v 1.4 2007/10/10 05:25:31 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 is the main action class. It implements all the basic
  28  /// functionalities to be shared by each action.
  29  
  30  class XMLDBAction {
  31  
  32      var $does_generate;  //Type of value returned by the invoke method
  33                           //ACTION_GENERATE_HTML have contents to show
  34                           //set by each specialized invoke
  35  
  36      var $title;          //Title of the Action (class name, by default)
  37                           //set by parent init automatically
  38  
  39      var $str;            //Strings used by the action
  40                           //set by each specialized init, calling loadStrings
  41  
  42      var $output;         //Output of the action
  43                           //set by each specialized invoke, get with getOutput
  44  
  45      var $errormsg;       //Last Error produced. Check when any invoke returns false
  46                           //get with getError
  47  
  48      var $postaction;     //Action to execute at the end of the invoke script
  49  
  50      /**
  51       * Constructor
  52       */
  53      function XMLDBAction() {
  54          $this->init();
  55      }
  56  
  57      /**
  58       * Constructor to keep PHP5 happy
  59       */
  60      function __construct() {
  61          $this->XMLDBAction();
  62      }
  63  
  64      /**
  65       * Init method, every subclass will have its own,
  66       * always calling the parent one
  67       */
  68      function init() {
  69          $this->does_generate = ACTION_NONE;
  70          $this->title     = strtolower(get_class($this));
  71          $this->str       = array();
  72          $this->output    = NULL;
  73          $this->errormsg  = NULL;
  74          $this->subaction = NULL;
  75      }
  76  
  77      /**
  78       * returns the type of output of the file
  79       */
  80      function getDoesGenerate() {
  81          return $this->does_generate;
  82      }
  83  
  84      /**
  85       * getError method, returns the last error string.
  86       * Used if the invoke() methods returns false
  87       */
  88      function getError() {
  89          return $this->errormsg;
  90      }
  91  
  92      /**
  93       * getOutput method, returns the output generated by the action.
  94       * Used after execution of the invoke() methods if they return true
  95       */
  96      function getOutput() {
  97          return $this->output;
  98      }
  99  
 100      /**
 101       * getPostAtion method, returns the action to launch after executing
 102       * another one
 103       */
 104      function getPostAction() {
 105          return $this->postaction;
 106      }
 107  
 108      /**
 109       * getTitle method returns the title of the action (that is part
 110       * of the $str array attribute
 111       */
 112      function getTitle() {
 113          return $this->str['title'];
 114      }
 115  
 116      /**
 117       * loadStrings method, loads the required strings specified in the
 118       * array parameter
 119       */
 120      function loadStrings($strings) {
 121      /// Load some commonly used strings
 122          $this->str['title'] = get_string($this->title, 'xmldb');
 123  
 124      /// Now process the $strings array loading it in the $str atribute
 125          if ($strings) {
 126              foreach ($strings as $key => $module) {
 127                  $this->str[$key] = get_string($key, $module);
 128              }
 129          }
 130      }
 131  
 132      /**
 133       * main invoke method, it simply sets the postaction attribute
 134       * if possible
 135       */
 136      function invoke() {
 137  
 138          global $SESSION;
 139  
 140      /// If we are used any dir, save it in the lastused session object
 141      /// Some actions can use it to perform positioning
 142          if ($lastused = optional_param ('dir', NULL, PARAM_PATH)) {
 143              $SESSION->lastused = stripslashes_safe($lastused);
 144          }
 145  
 146          $this->postaction = optional_param ('postaction', NULL, PARAM_ALPHAEXT);
 147      /// Avoid being recursive
 148          if ($this->title == $this->postaction) {
 149              $this->postaction = NULL;
 150          }
 151      }
 152  
 153      /**
 154       * launch method, used to easily call invoke methods between actions
 155       */
 156      function launch($action) {
 157  
 158          global $CFG;
 159  
 160      /// Get the action path and invoke it
 161          $actionsroot = "$CFG->dirroot/$CFG->admin/xmldb/actions";
 162          $actionclass = $action . '.class.php';
 163          $actionpath = "$actionsroot/$action/$actionclass";
 164  
 165      /// Load and invoke the proper action
 166          $result = false;
 167          if (file_exists($actionpath) && is_readable($actionpath)) {
 168              require_once($actionpath);
 169              if ($xmldb_action = new $action) {
 170                  $result = $xmldb_action->invoke();
 171                  if ($result) {
 172                      if ($xmldb_action->does_generate != ACTION_NONE &&
 173                          $xmldb_action->getOutput()) {
 174                          $this->does_generate = $xmldb_action->does_generate;
 175                          $this->title = $xmldb_action->title;
 176                          $this->str = $xmldb_action->str;
 177                          $this->output .= $xmldb_action->getOutput();
 178                      }
 179                  } else {
 180                      $this->errormsg = $xmldb_action->getError();
 181                  }
 182              } else {
 183                  $this->errormsg = "Error: cannot instantiate class (actions/$action/$actionclass)";
 184              }
 185          } else {
 186              $this->errormsg = "Error: wrong action specified ($action)";
 187          }
 188          return $result;
 189      }
 190  }
 191  ?>


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