Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / xmldb / actions / XMLDBAction.class.php
blobc1eaa9364bc1c8b0348f710d1652a5ba99e0b737
1 <?php // $Id$
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 ///////////////////////////////////////////////////////////////////////////
27 /// This is the main action class. It implements all the basic
28 /// functionalities to be shared by each action.
30 class XMLDBAction {
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
46 //get with getError
48 var $postaction; //Action to execute at the end of the invoke script
50 /**
51 * Constructor
53 function XMLDBAction() {
54 $this->init();
57 /**
58 * Constructor to keep PHP5 happy
60 function __construct() {
61 $this->XMLDBAction();
64 /**
65 * Init method, every subclass will have its own,
66 * always calling the parent one
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;
77 /**
78 * returns the type of output of the file
80 function getDoesGenerate() {
81 return $this->does_generate;
84 /**
85 * getError method, returns the last error string.
86 * Used if the invoke() methods returns false
88 function getError() {
89 return $this->errormsg;
92 /**
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() {
97 return $this->output;
101 * getPostAtion method, returns the action to launch after executing
102 * another one
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
118 * array parameter
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
125 if ($strings) {
126 foreach ($strings as $key => $module) {
127 $this->str[$key] = get_string($key, $module);
133 * main invoke method, it simply sets the postaction attribute
134 * if possible
136 function invoke() {
138 global $SESSION;
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) {
158 global $CFG;
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
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();
179 } else {
180 $this->errormsg = $xmldb_action->getError();
182 } else {
183 $this->errormsg = "Error: cannot instantiate class (actions/$action/$actionclass)";
185 } else {
186 $this->errormsg = "Error: wrong action specified ($action)";
188 return $result;