1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Backup user interface stages
19 *
20 * This file contains the classes required to manage the stages that make up the
21 * backup user interface.
22 * These will be primarily operated a {@link base_ui} instance.
23 *
24 * @package   core_backup
25 * @copyright 2010 Sam Hemelryk
26 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29/**
30 * Abstract stage class
31 *
32 * This class should be extended by all backup stages (a requirement of many backup ui functions).
33 * Each stage must then define two abstract methods
34 *  - process : To process the stage
35 *  - initialise_stage_form : To get a backup_moodleform instance for the stage
36 *
37 * @package   core_backup
38 * @copyright 2010 Sam Hemelryk
39 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41abstract class base_ui_stage {
42
43    /**
44     * The current stage
45     * @var int
46     */
47    protected $stage = 1;
48
49    /**
50     * The backuck UI object
51     * @var base_ui
52     */
53    protected $ui;
54
55    /**
56     * The moodleform for this stage
57     * @var base_moodleform
58     */
59    protected $stageform = null;
60
61    /**
62     * Custom form params that will be added as hidden inputs
63     * @var array
64     */
65    protected $params = null;
66
67    /**
68     * Constructor
69     *
70     * @param base_ui $ui
71     * @param array $params
72     */
73    public function __construct(base_ui $ui, array $params = null) {
74        $this->ui = $ui;
75        $this->params = $params;
76    }
77
78    /**
79     * Returns the custom params for this stage
80     * @return array|null
81     */
82    final public function get_params() {
83        return $this->params;
84    }
85
86    /**
87     * The current stage
88     * @return int
89     */
90    final public function get_stage() {
91        return $this->stage;
92    }
93
94    /**
95     * The next stage
96     * @return int
97     */
98    public function get_next_stage() {
99        return floor($this->stage * 2);
100    }
101
102    /**
103     * The previous stage
104     * @return int
105     */
106    final public function get_prev_stage() {
107        return floor($this->stage / 2);
108    }
109
110    /**
111     * The name of this stage
112     * @return string
113     */
114    public function get_name() {
115        return get_string('currentstage' . $this->stage, 'backup');
116    }
117
118    /**
119     * The backup id from the backup controller
120     * @return string
121     */
122    final public function get_uniqueid() {
123        return $this->ui->get_uniqueid();
124    }
125
126    /**
127     * Displays the stage.
128     *
129     * By default this involves instantiating the form for the stage and the calling
130     * it to display.
131     *
132     * @param core_backup_renderer $renderer
133     * @return string HTML code to echo
134     */
135    public function display(core_backup_renderer $renderer) {
136
137        $form = $this->initialise_stage_form();
138        // A nasty hack follows to work around the sad fact that moodle quickforms
139        // do not allow to actually return the HTML content, just to echo it.
140        flush();
141        ob_start();
142        $form->display();
143        $output = ob_get_contents();
144        ob_end_clean();
145
146        return $output;
147    }
148
149    /**
150     * Processes the stage.
151     *
152     * This must be overridden by every stage as it will be different for every stage
153     *
154     * @abstract
155     * @param base_moodleform $form
156     */
157    abstract public function process(base_moodleform $form = null);
158
159    /**
160     * Creates an instance of the correct moodleform properly populated and all
161     * dependencies instantiated
162     *
163     * @abstract
164     * @return backup_moodleform
165     */
166    abstract protected function initialise_stage_form();
167
168    /**
169     * Returns the base UI class
170     * @return base_ui
171     */
172    final public function get_ui() {
173        return $this->ui;
174    }
175
176    /**
177     * Returns true if this stage is the first stage.
178     * @return bool
179     */
180    public function is_first_stage() {
181        return $this->stage == 1;
182    }
183}
184