1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/Component/classes/class.ilPlugin.php';
5
6/**
7 * @author  Michael Jansen <mjansen@databay.de>
8 * @version $Id$
9 * @ingroup ModulesTest
10 */
11class ilTestExportFilename
12{
13    /**
14     * @var ilObjTest
15     */
16    protected $test;
17
18    /**
19     * @var int
20     */
21    protected $timestamp = 0;
22
23    /**
24     * @param ilObjTest $test
25     */
26    public function __construct(ilObjTest $test)
27    {
28        $this->test = $test;
29        $this->timestamp = time();
30    }
31
32    /**
33     * @return int
34     */
35    public function getTimestamp()
36    {
37        return $this->timestamp;
38    }
39
40    /**
41     * @param string $extension
42     * @param string $additional
43     * @return string
44     * @throws ilException
45     */
46    public function getPathname($extension, $additional = '')
47    {
48        if (!is_string($extension) || !strlen($extension)) {
49            throw new ilException('Missing file extension! Please pass a file extension of type string.');
50        } elseif (substr_count($extension, '.') > 1 || (strpos($extension, '.') !== false && strpos($extension, '.') !== 0)) {
51            throw new ilException('Please use at most one dot in your file extension.');
52        } elseif (strpos($extension, '.') === 0) {
53            $extension = substr($extension, 1);
54        }
55
56        if (!is_string($additional)) {
57        } elseif (strlen($additional)) {
58            if (strpos($additional, '__') === 0) {
59                throw new ilException('The additional file part may not contain __ at the beginning!');
60            }
61
62            $additional = '__' . $additional . '_';
63        } else {
64            $additional = '_';
65        }
66
67        return $this->test->getExportDirectory() . DIRECTORY_SEPARATOR . $this->getTimestamp() . '__' . IL_INST_ID . '__' . $this->test->getType() . $additional . $this->test->getId() . '.' . $extension;
68    }
69}
70