Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace tool_usertours;
18
 
19
/**
20
 * Target class.
21
 *
22
 * @package    tool_usertours
23
 * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
class target {
27
    /**
28
     * @var TARGET_SELECTOR The target is a CSS selector.
29
     */
30
    const TARGET_SELECTOR = 0;
31
 
32
    /**
33
     * @var TARGET_BLOCK The target is a block.
34
     */
35
    const TARGET_BLOCK = 1;
36
 
37
    /**
38
     * @var TARGET_UNATTACHED The target is unattached to any specific node.
39
     */
40
    const TARGET_UNATTACHED = 2;
41
 
42
    /**
43
     * @var     array   $mapping    The list of target type to target name.
44
     */
45
    protected static $mapping = [
46
        self::TARGET_BLOCK      => 'block',
47
        self::TARGET_SELECTOR   => 'selector',
48
        self::TARGET_UNATTACHED => 'unattached',
49
    ];
50
 
51
    /**
52
     * Return the name of the class for this target type.
53
     *
54
     * @param   int     $type       The type of target.
55
     * @return  string              The class name.
56
     */
57
    public static function get_classname($type) {
58
        $targettype = self::$mapping[self::get_target_constant($type)];
59
        return "\\tool_usertours\\local\\target\\{$targettype}";
60
    }
61
 
62
    /**
63
     * Return the instance of the class for this target.
64
     *
65
     * @param   step    $step       The step.
66
     * @return  target              The target instance.
67
     */
68
    public static function get_target_type(step $step) {
69
        if (!isset(self::$mapping[$step->get_targettype()])) {
70
            throw new \moodle_exception('Unknown Target type');
71
        }
72
 
73
        $targettype = self::$mapping[$step->get_targettype()];
74
        return "\\tool_usertours\\local\\target\\{$targettype}";
75
    }
76
 
77
    /**
78
     * Return the constant used to describe this target.
79
     *
80
     * @param   string  $type       The type of the target.
81
     * @return  int                 The constant for this target.
82
     */
83
    public static function get_target_constant($type) {
84
        return array_search($type, self::$mapping);
85
    }
86
 
87
    /**
88
     * Return the constant used to describe this class.
89
     *
90
     * @param   string  $classname  The fully-qualified class name of the target
91
     * @return  int                 The constant for this target.
92
     */
93
    public static function get_target_constant_for_class($classname) {
94
        $rc = new \ReflectionClass($classname);
95
 
96
        return self::get_target_constant($rc->getShortName());
97
    }
98
 
99
    /**
100
     * Return the instance of the class for this target.
101
     *
102
     * @param   step    $step       The step.
103
     * @return  target              The target instance.
104
     */
105
    public static function get_target_instance(step $step) {
106
        $targetclass = self::get_target_type($step);
107
        return new $targetclass($step);
108
    }
109
 
110
    /**
111
     * Return the complete lits of target types.
112
     *
113
     * @return  array
114
     */
115
    public static function get_target_types() {
116
        return self::$mapping;
117
    }
118
}