lib.php 9.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage export-html
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

defined('INTERNAL') || die();

/**
 * HTML export plugin
 */
class PluginExportHtml extends PluginExport {

    /**
     * name of resultant zipfile
     */
    protected $zipfile;

    /**
     * The time the export occured
     */
    protected $export_time;

    /**
     * The name of the directory under which all the other directories and 
     * files will be placed in the export
     */
    protected $rootdir;

    /**
    * constructor.  overrides the parent class
    * to set up smarty and the attachment directory
    */
    public function __construct(User $user, $views, $artefacts) {
        parent::__construct($user, $views, $artefacts);
Nigel McNie's avatar
Nigel McNie committed
56
        // TODO move this normalisation into a method
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        $this->rootdir = 'portfolio-for-' . preg_replace('#[^a-zA-Z0-9_-]+#', '-', $user->get('username'));

        // Create basic required directories
        foreach (array('files', 'views', 'static') as $directory) {
            $directory = "{$this->exportdir}/{$this->rootdir}/{$directory}/";
            if (!check_dir_exists($directory)) {
                throw new SystemException("Couldn't create the temporary export directory $directory");
            }
        }
        $this->zipfile = 'mahara-export-html-user'
            . $this->get('user')->get('id') . '-' . $this->export_time . '.zip';
    }

    /**
    * main export routine
    */
    public function export() {
Nigel McNie's avatar
Nigel McNie committed
74
75
        // For each artefact plugin, if it implements leap export, ask it to 
        // dump out its structure
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        $summaries = array();
        foreach (plugins_installed('artefact', true) as $plugin) {
            $plugin = $plugin->name;
            if (safe_require('export', 'html/' . $plugin, 'lib.php', 'require_once', true)) {
                $classname = 'HtmlExport' . ucfirst($plugin);
                if (!is_subclass_of($classname, 'HtmlExportArtefactPlugin')) {
                    throw new SystemException("Class $classname does not extend HtmlExportArtefactPlugin as it should");
                }

                safe_require('artefact', $plugin);

                $artefactexporter = new $classname($this);
                $artefactexporter->dump_export_data();
                $summaries[$plugin] = array($artefactexporter->get_summary_weight(), $artefactexporter->get_summary());
            }
        }

Nigel McNie's avatar
Nigel McNie committed
93
94
95
96
        // Get the view data
        $this->dump_view_export_data();
        $summaries['view'] = array(100, $this->get_view_summary());

97
98
99
100
101
102
        // Sort by weight (then drop the weight information)
        uasort($summaries, create_function('$a, $b', 'return $a[0] > $b[0];'));
        foreach ($summaries as &$summary) {
            $summary = $summary[1];
        }

Nigel McNie's avatar
Nigel McNie committed
103
        // Build index.html
104
        $this->build_index_page($summaries);
105
106
107

        // Copy all static files into the export
        $this->copy_static_files();
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        

        // zip everything up
        $cwd = getcwd();
        $command = sprintf('%s %s %s %s',
            get_config('pathtozip'),
            get_config('ziprecursearg'),
            escapeshellarg($this->exportdir .  $this->zipfile),
            escapeshellarg($this->rootdir)
        );
        $output = array();
        chdir($this->exportdir);
        exec($command, $output, $returnvar);
        chdir($cwd);
        if ($returnvar != 0) {
            throw new SystemException('Failed to zip the export file');
        }
        return $this->zipfile;
    }

    public function cleanup() {
        // @todo remove temporary files and directories
        // @todo maybe move the zip file somewhere else - like to files/export or something
    }

    public function get_smarty($rootpath='') {
        $smarty = smarty_core();
        $smarty->assign('user', $this->get('user'));
        $smarty->assign('rootpath', $rootpath);
137
        $smarty->assign('export_time', $this->export_time);
138
139
140
141

        return $smarty;
    }

142
143
144
145
146
147
148
149
150
151
152
    /**
     * Converts the passed text, which is assumed to be a reasonably short 
     * string, into a a form that could be used in a URL.
     *
     * @param string $text The text to convert
     * @return string      The converted text
     */
    public static function text_to_path($text) {
        return preg_replace('#[^a-zA-Z0-9_-]+#', '-', $text);
    }

153
154
155
156
157
158
159
    private function build_index_page($summaries) {
        $smarty = $this->get_smarty();
        $smarty->assign('summaries', $summaries);
        $content = $smarty->fetch('export:html:index.tpl');
        file_put_contents($this->exportdir . '/' . $this->rootdir . '/index.html', $content);
    }

Nigel McNie's avatar
Nigel McNie committed
160
161
162
163
164
165
166
167
168
169
    /**
     * Dumps all views into the HTML export
     *
     * TODO: respect $this->views
     */
    private function dump_view_export_data() {
        if ($viewids = get_column('view', 'id', 'owner', $this->get('user')->get('id'), 'type', 'portfolio')) {
            $smarty = $this->get_smarty('../../');
            foreach ($viewids as $viewid) {
                $view = new View($viewid);
170
171
172
173
                $smarty->assign('breadcrumbs', array(
                    array('text' => get_string('Views', 'view')),
                    array('text' => $view->get('title'), 'path' => 'index.html'),
                ));
Nigel McNie's avatar
Nigel McNie committed
174

175
                $directory = $this->exportdir . '/' . $this->rootdir . '/views/' . self::text_to_path($view->get('title'));
Nigel McNie's avatar
Nigel McNie committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
                if (!check_dir_exists($directory)) {
                    throw new SystemException("Could not create directory for view $viewid");
                }

                $smarty->assign('view', $view->build_columns());
                $content = $smarty->fetch('export:html:view.tpl');
                if (!file_put_contents("$directory/index.html", $content)) {
                    throw new SystemException("Could not write view page for view $viewid");
                }
            }
        }
    }

    private function get_view_summary() {
        $smarty = $this->get_smarty('../');

        $views = array();
        foreach ($this->views as $view) {
            if ($view->get('type') != 'profile') {
                $views[] = array(
                    'title' => $view->get('title'),
197
                    'folder' => self::text_to_path($view->get('title')),
Nigel McNie's avatar
Nigel McNie committed
198
199
200
201
                );
            }
        }
        $smarty->assign('views', $views);
202
203
204
205
206
207
208
209
210
211

        if ($views) {
            $stryouhaveviews = (count($views) == 1)
                ? get_string('youhaveoneview', 'view')
                : get_string('youhaveviews', 'view', count($views));
        }
        else {
            $stryouhaveviews = get_string('youhavenoviews', 'view');
        }
        $smarty->assign('stryouhaveviews', $stryouhaveviews);
Nigel McNie's avatar
Nigel McNie committed
212
213

        return array(
214
            'title' => get_string('Views', 'view'),
Nigel McNie's avatar
Nigel McNie committed
215
216
217
218
            'description' => $smarty->fetch('export:html:viewsummary.tpl'),
        );
    }

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    /**
     * Copies the static files (stylesheets etc.) into the export
     */
    private function copy_static_files() {
        $staticdir = $this->get('exportdir') . '/' . $this->get('rootdir') . '/static/';
        foreach (new RecursiveDirectoryIterator(get_config('docroot') . 'export/html/static/') as $fileinfo) {
            if (!$fileinfo->isFile() || substr($fileinfo->getFilename(), 0, 1) == '.') {
                continue;
            }
            if (!copy($fileinfo->getPathname(), $staticdir . $fileinfo->getFilename())) {
                throw new SystemException("Could not copy static file " . $fileinfo->getPathname());
            }
        }
    }

234
235
236
237
238
239
240
241
242
243
244
245
246
}

abstract class HtmlExportArtefactPlugin {

    protected $exporter;

    protected $fileroot;

    public function __construct(PluginExportHTML $exporter) {
        $this->exporter = $exporter;
        $pluginname = strtolower(substr(get_class($this), strlen('HtmlExport')));
        $this->fileroot = $this->exporter->get('exportdir') . '/' . $this->exporter->get('rootdir') . '/files/' . $pluginname . '/';
        if (!check_dir_exists($this->fileroot)) {
Nigel McNie's avatar
Nigel McNie committed
247
            throw new SystemException("Could not create the temporary export directory $this->fileroot");
248
249
250
251
252
253
254
255
256
257
258
259
        }
    }

    abstract public function dump_export_data();

    abstract public function get_summary();

    abstract public function get_summary_weight();

}

?>