Gitlab CSE Unil

tool.php 6.4 KB
Newer Older
1
2
<?php

M. Chardon's avatar
GPL3  
M. Chardon committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This file contains the annotation class for the assignfeedback_editpdfplus plugin
 *
 * @package   assignfeedback_editpdfplus
 * @copyright  2016 Université de Lausanne
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
25
 */

M. Chardon's avatar
M. Chardon committed
26
namespace assignfeedback_editpdfplus\bdd;
27
28
29
30
31
32
33

/**
 * Description of tool
 *
 * @author kury
 */
class tool {
34

35
36
37
38
39
40
41
42
43
44
    /** @var int unique id for this annotation */
    public $id = 0;

    /** @var int contextid for this annotation */
    public $contextid = 0;

    /** @var int axis for this annotation */
    public $axis = 0;

    /** @var int type */
M. Chardon's avatar
M. Chardon committed
45
    public $type = null;
46

M. Chardon's avatar
M. Chardon committed
47
48
49
    /** @var strClass type */
    public $typeObject = null;

50
51
52
53
54
    /** @var string colors used */
    public $colors = '';

    /** @var string cartridge for drawing the annotation. */
    public $cartridge = '';
55

56
57
    /** @var string colors used */
    public $cartridge_color = '';
58
59
60

    /** @var string texts for this annotation. */
    public $texts = '';
61

62
63
64
    /** @var array texts for this annotation. */
    public $textsarray = '';

65
66
67
68
69
    /** @var string label of this annotation */
    public $label = '';

    /** @var boolean, allow reply or not */
    public $reply = 0;
70

71
    /** @var boolean, if tool is actived or not */
M. Chardon's avatar
M. Chardon committed
72
    public $enabled = 1;
73

M. Chardon's avatar
M. Chardon committed
74
    /** @var int order_tool, order in toolbar */
M. Chardon's avatar
M. Chardon committed
75
    public $order_tool = 1000;
76

77
78
79
80
81
82
83
84
85
    /**  @var boolean, can be removed or not */
    public $removable = false;

    /**  @var string, button class, which will a graphic representation  */
    public $button = "";

    /**  @var string, html style which will a graphic representation  */
    public $style = "";

M. Chardon's avatar
M. Chardon committed
86
87
88
    /**  @var string, html class which will a graphic representation  */
    public $displayClass = "";

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    /**
     * Convert a compatible stdClass into an instance of this class.
     * @param stdClass $record
     */
    public function __construct(\stdClass $record = null) {
        if ($record) {
            $intcols = array('reply');
            foreach ($this as $key => $value) {
                if (isset($record->$key)) {
                    if (in_array($key, $intcols)) {
                        $this->$key = intval($record->$key);
                    } else {
                        $this->$key = $record->$key;
                    }
                }
            }
        }
    }
107

108
109
    /**
     * Initialize a minimal tool
M. Chardon's avatar
M. Chardon committed
110
     * @param array $parameters Optionals parameters to initialize a tool
111
     */
M. Chardon's avatar
M. Chardon committed
112
113
    public function init($parameters) {
        $this->contextid = isset($parameters['contextid']) ? $parameters['contextid'] : 0;
114
        $this->enabled = true;
M. Chardon's avatar
M. Chardon committed
115
        $this->axis = isset($parameters['axeid']) ? $parameters['axeid'] : 0;
116
        $this->removable = true;
M. Chardon's avatar
M. Chardon committed
117
        $this->label = isset($parameters['label']) ? $parameters['label'] : "";
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    }

    /**
     * Get text proposals and transform it into an array
     * @return \assignfeedback_editpdfplus\stdClass
     */
    public function initToolTextsArray() {
        if (!$this->texts) {
            $this->textsarray = null;
        } else {
            $tooltextsarray = explode("\",\"", $this->texts);
            $compteur = 0;
            $result = array();
            foreach ($tooltextsarray as $value) {
                if (!$value || $value == '"') {
                    continue;
                }
                $obj = new \stdClass();
                $obj->text = $value;
                if (substr($obj->text, 0, 1) == '"') {
                    $obj->text = substr($obj->text, 1);
                }
                if (substr($obj->text, -1) == '"') {
                    $obj->text = substr($obj->text, 0, -1);
                }
                $obj->index = $compteur;
                $result[] = $obj;
                $compteur++;
            }

            $this->textsarray = $result;
        }
    }

M. Chardon's avatar
M. Chardon committed
152
153
154
    /**
     * Set Style and replace label with format symbol for display purpose
     */
155
    public function setDesign() {
M. Chardon's avatar
M. Chardon committed
156
157
158
159
        $this->label = $this->getButtonLabel();
        $this->style = $this->getStyleButton();
    }

M. Chardon's avatar
M. Chardon committed
160
161
162
163
    /**
     * Calculate a label with format symbol, according to its type
     * @return string
     */
M. Chardon's avatar
M. Chardon committed
164
    private function getButtonLabel() {
165
        if ($this->type == "4") {
M. Chardon's avatar
M. Chardon committed
166
167
168
169
170
171
172
173
            return '| ' . $this->label . ' |';
        }
        if ($this->type == "5") {
            return '| ' . $this->label;
        }
        return $this->label;
    }

M. Chardon's avatar
M. Chardon committed
174
175
176
177
    /**
     * Calculate a sytle for a display in a button, according to its type
     * @return string
     */
M. Chardon's avatar
M. Chardon committed
178
    private function getStyleButton() {
M. Chardon's avatar
M. Chardon committed
179
        $styleTmp = "";
M. Chardon's avatar
M. Chardon committed
180
        if ($this->enabled == "0") {
M. Chardon's avatar
M. Chardon committed
181
            $styleTmp .= "background-image:none;background-color:#CCCCCC;";
182
183
        }
        if ($this->type == "4" || $this->type == "1") {
M. Chardon's avatar
M. Chardon committed
184
            $styleTmp .= "text-decoration: underline;";
M. Chardon's avatar
M. Chardon committed
185
        }
M. Chardon's avatar
M. Chardon committed
186
        return $styleTmp;
M. Chardon's avatar
M. Chardon committed
187
188
    }

M. Chardon's avatar
M. Chardon committed
189
190
191
192
193
    /**
     * Get elements (display label, parameters) to render a button in HTML
     * @param bool $disabled if the button must be disabled
     * @return array
     */
M. Chardon's avatar
M. Chardon committed
194
195
196
197
198
199
200
201
202
203
    public function getRendererBoutonHTMLDisplay($disabled = false) {
        $iconhtml = $this->getButtonLabel();
        if (!$this->typeObject) {
            return array(
                'content' => $iconhtml,
                'parameters' => null
            );
        }
        $datatool = $this->typeObject->label;
        $iconparams = array('data-tool' => $datatool,
204
            'class' => $this->typeObject->label . ' costumtoolbarbutton btn btn-light',
M. Chardon's avatar
M. Chardon committed
205
206
207
208
209
            'id' => 'ctbutton' . $this->id,
            'type' => 'button',
            'style' => $this->getStyleButton());
        if ($disabled) {
            $iconparams['disabled'] = 'true';
210
        }
M. Chardon's avatar
M. Chardon committed
211
212
213
214
        return array(
            'content' => $iconhtml,
            'parameters' => $iconparams
        );
215
216
    }

217
}