layoutpreviewimage.php 3.9 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage flexible layouts
 * @author     Mike Kelly
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
13
14
15
 * @copyright  (C) 2011 Mike Kelly m.f.kelly@arts.ac.uk
 *
 */

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

/* This class is for the construction of a preview image
Aaron Wells's avatar
Aaron Wells committed
16
 * for the various custom layout choices. The image is a visual
17
18
19
20
21
22
 * representation of the layout rows/columns matrix.
 */
class LayoutPreviewImage {

    private $rows = 1;
    private $layout; // contains cols per row data
23
    private $text;
24
25
26
27
28
29
30
    private static $standard_preview_width = 76;
    private static $one_row_height = 48;
    private static $two_row_height = 23;
    private static $three_row_height = 15;
    private static $spacer = 3;

    /* Constructor.
31
32
     * @param data  containing 'layout' (required) that consists of an array of columns per row.
     *              Example:
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
     *              array('row1' => '30-30-30',
     *                    'row2' => '25-25-25-25');
     */
    public function __construct($data = null) {

        if (empty($data)) {
            // we need at least a layout array
            throw new ParamOutOfRangeException("Required data wasn't passed to class " . get_class($this));
        }

        foreach ((array)$data as $field => $value) {
            if (property_exists($this, $field)) {
                $this->{$field} = $value;
            }
        }

        if (!empty($this->layout)) {
            $this->rows = count($this->layout);
        }
    }

54
    /* Generates the preview SVG image.
Aaron Wells's avatar
Aaron Wells committed
55
     *
56
     * @return string SVG of the preview image.
57
58
59
60
     */
    public function create_preview() {
        $width = $this->get_preview_width();
        $height = $this->get_preview_height();
61
62
63
64
65
66
        $id = uniqid('lid');
        $layout = "<svg xmlns=http://www.w3.org/2000/svg role='img' width='{$width}' height='{$height}' aria-labelledby='title{$id} desc{$id}'>";
        if (!empty($this->text)) {
            $layout .= "<title id='title{$id}' >" . get_string('layoutpreviewimage', 'view') . "</title>";
            $layout .= "<desc id='desc{$id}'>" . hsc($this->text) . "</desc>";
        }
67
68
69
70
71

        $x = 0;
        $y = 0;
        $col_height = $this->get_preview_column_height();

72
        $class = true;
73
        foreach ($this->layout as $key => $row) {
74
            $style = 'layout' . (int)$class;
75
76
77
78
            $columns = explode('-', $row);

            foreach ($columns as $column) {
                $col_width = $this->get_percentage_column_width(count($columns), $column);
79
                $layout .= "<rect x='{$x}' y='{$y}' width='{$col_width}' height='{$col_height}' class='{$style}'/>";
80
81
82
83
84
                $x += ($col_width + self::$spacer); // increment x val for next col
            }

            $x = 0;
            $y += ($col_height + self::$spacer); // increment y val for next row
85
            $class = !$class;
86
87
        }

88
        $layout .= '</svg>';
89

90
        return $layout;
91
92
93
94
95
96
    }

    private function get_preview_height() {
        if ($this->rows == 1) {
            return self::$one_row_height;
        }
97
98
99
100
        if ($this->rows > 2) {
            return $this->rows * self::$three_row_height + ($this->rows - 1) * self::$spacer;
        }
        return $this->rows * self::$two_row_height + ($this->rows - 1) * self::$spacer;
101
102
103
104
105
106
    }

    private function get_preview_column_height() {
        if ($this->rows == 1) {
            return self::$one_row_height;
        }
107
108
109
110
        if ($this->rows > 2) {
            return self::$three_row_height;
        }
        return self::$two_row_height;
111
112
113
114
115
116
117
    }

    private function get_preview_width() {
        return self::$standard_preview_width;
    }

    private function get_percentage_column_width($numcols, $percent) {
118
        return (self::$standard_preview_width - self::$spacer * ($numcols - 1)) * $percent / 100;
119
    }
120
}