Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / xmldb / actions / move_updown_table / move_updown_table.class.php
blob25f3911e09a705616e200bb9a5f3575eccd75a4e
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 class will will move one table down
29 class move_updown_table extends XMLDBAction {
31 /**
32 * Init method, every subclass will have its own
34 function init() {
35 parent::init();
37 /// Set own custom attributes
39 /// Get needed strings
40 $this->loadStrings(array(
41 /// 'key' => 'module',
42 ));
45 /**
46 * Invoke method, every class will have its own
47 * returns true/false on completion, setting both
48 * errormsg and output as necessary
50 function invoke() {
51 parent::invoke();
53 $result = true;
55 /// Set own core attributes
56 $this->does_generate = ACTION_NONE;
57 //$this->does_generate = ACTION_GENERATE_HTML;
59 /// These are always here
60 global $CFG, $XMLDB;
62 /// Do the job, setting result as needed
63 /// Get the dir containing the file
64 $dirpath = required_param('dir', PARAM_PATH);
65 $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
67 /// Get the correct dirs
68 if (!empty($XMLDB->dbdirs)) {
69 $dbdir =& $XMLDB->dbdirs[$dirpath];
70 } else {
71 return false;
73 if (!empty($XMLDB->editeddirs)) {
74 $editeddir =& $XMLDB->editeddirs[$dirpath];
75 $structure =& $editeddir->xml_file->getStructure();
77 /// ADD YOUR CODE HERE
78 $prev = NULL;
79 $next = NULL;
80 $tableparam = required_param('table', PARAM_CLEAN);
81 $direction = required_param('direction', PARAM_ALPHA);
82 $tables =& $structure->getTables();
83 if ($direction == 'down') {
84 $table =& $structure->getTable($tableparam);
85 $swap =& $structure->getTable($table->getNext());
86 } else {
87 $swap =& $structure->getTable($tableparam);
88 $table =& $structure->getTable($swap->getPrevious());
91 /// Change the table before the pair
92 if ($table->getPrevious()) {
93 $prev =& $structure->getTable($table->getPrevious());
94 $prev->setNext($swap->getName());
95 $swap->setPrevious($prev->getName());
96 $prev->setChanged(true);
97 } else {
98 $swap->setPrevious(NULL);
100 /// Change the table after the pair
101 if ($swap->getNext()) {
102 $next =& $structure->getTable($swap->getNext());
103 $next->setPrevious($table->getName());
104 $table->setNext($next->getName());
105 $next->setChanged(true);
106 } else {
107 $table->setNext(NULL);
109 /// Swap the tables
110 $table->setPrevious($swap->getName());
111 $swap->setNext($table->getName());
113 /// Table has changed
114 $table->setChanged(true);
116 /// Reorder the structure
117 $structure->orderTables($tables);
118 /// Send tables back to structure (the order above break refs)
119 $structure->setTables($tables);
120 /// Recalculate the hash
121 $structure->calculateHash(true);
123 /// If the hash has changed from the original one, change the version
124 /// and mark the structure as changed
125 $origstructure =& $dbdir->xml_file->getStructure();
126 if ($structure->getHash() != $origstructure->getHash()) {
127 $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
128 $structure->setChanged(true);
131 /// Launch postaction if exists (leave this here!)
132 if ($this->getPostAction() && $result) {
133 return $this->launch($this->getPostAction());
136 /// Return ok if arrived here
137 return $result;