3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
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. //
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: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This is the main action class. It implements all the basic
28 /// functionalities to be shared by each action.
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
36 var $title; //Title of the Action (class name, by default)
37 //set by parent init automatically
39 var $str; //Strings used by the action
40 //set by each specialized init, calling loadStrings
42 var $output; //Output of the action
43 //set by each specialized invoke, get with getOutput
45 var $errormsg; //Last Error produced. Check when any invoke returns false
48 var $postaction; //Action to execute at the end of the invoke script
53 function XMLDBAction() {
58 * Constructor to keep PHP5 happy
60 function __construct() {
65 * Init method, every subclass will have its own,
66 * always calling the parent one
69 $this->does_generate
= ACTION_NONE
;
70 $this->title
= strtolower(get_class($this));
73 $this->errormsg
= NULL;
74 $this->subaction
= NULL;
78 * returns the type of output of the file
80 function getDoesGenerate() {
81 return $this->does_generate
;
85 * getError method, returns the last error string.
86 * Used if the invoke() methods returns false
89 return $this->errormsg
;
93 * getOutput method, returns the output generated by the action.
94 * Used after execution of the invoke() methods if they return true
96 function getOutput() {
101 * getPostAtion method, returns the action to launch after executing
104 function getPostAction() {
105 return $this->postaction
;
109 * getTitle method returns the title of the action (that is part
110 * of the $str array attribute
112 function getTitle() {
113 return $this->str
['title'];
117 * loadStrings method, loads the required strings specified in the
120 function loadStrings($strings) {
121 /// Load some commonly used strings
122 $this->str
['title'] = get_string($this->title
, 'xmldb');
124 /// Now process the $strings array loading it in the $str atribute
126 foreach ($strings as $key => $module) {
127 $this->str
[$key] = get_string($key, $module);
133 * main invoke method, it simply sets the postaction attribute
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);
146 $this->postaction
= optional_param ('postaction', NULL, PARAM_ALPHAEXT
);
147 /// Avoid being recursive
148 if ($this->title
== $this->postaction
) {
149 $this->postaction
= NULL;
154 * launch method, used to easily call invoke methods between actions
156 function launch($action) {
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";
165 /// Load and invoke the proper action
167 if (file_exists($actionpath) && is_readable($actionpath)) {
168 require_once($actionpath);
169 if ($xmldb_action = new $action) {
170 $result = $xmldb_action->invoke();
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();
180 $this->errormsg
= $xmldb_action->getError();
183 $this->errormsg
= "Error: cannot instantiate class (actions/$action/$actionclass)";
186 $this->errormsg
= "Error: wrong action specified ($action)";