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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
<?php
/**
* A question box is used to display a confirmation dialog for a specific
* action, quite similar to the message boxes.
*
* A question box consists at least of a question and some approval parameters
* that will be sent when the question is approved. You may pass additional
* parameters that are sent when the question is disapproved and both urls for
* approving and disapproving may be set.
*
* @author Jan-Hendrik Willms
* @license GPL2 or any later version
* @since Stud.IP 4.2
*/
class QuestionBox implements LayoutMessage
{
/**
* Creates a question object that should be confirmed.
*
* If you want to pass additional parameters, include them in the url. They
* will be extract from the url.
*
* @param string $question The question that should be confirmed
* @param string $accept_url URL to send the acceptance request to
* @param string $decline_url URL to send the declination request to
* @return QuestionBox instance to allow chaining
*/
public static function create($question, $accept_url = '', $decline_url = '')
{
return new static(formatReady($question), $accept_url, $decline_url);
}
/**
* Creates a question object that should be confirmed. The question may
* contain HTML.
*
* If you want to pass additional parameters, include them in the url. They
* will be extract from the url.
*
* @param string $question The question that should be confirmed
* @param string $accept_url URL to send the acceptance request to
* @param string $decline_url URL to send the declination request to
* @return QuestionBox instance to allow chaining
*/
public static function createHTML($question, $accept_url = '', $decline_url = '')
{
return new static($question, $accept_url, $decline_url);
}
protected $question;
protected $accept_url;
protected $accept_parameters = [];
protected $decline_url;
protected $decline_parameters = [];
protected $include_ticket = false;
/**
* Constructs the object. Protected to enforce the use of our static helper
* functions.
*
* @param string $question The question that should be confirmed
*/
protected function __construct($question, $accept_url, $decline_url)
{
$this->question = $question;
$this->setAcceptURL($accept_url);
$this->setDeclineURL($decline_url);
}
/**
* Set the url the acceptance request is sent to.
*
* @param string $url
* @param array $parameters
* @return QuestionBox instance to allow chaining
*/
public function setAcceptURL($url, array $parameters = [])
{
$parameters = array_merge(
$this->extractURLParameters($url),
$parameters
);
$this->accept_url = $url;
$this->accept_parameters = $parameters;
return $this;
}
/**
* Set the url the declination url is sent to.
*
* @param string $url
* @param array $parameters
* @return QuestionBox instance to allow chaining
*/
public function setDeclineURL($url, array $parameters = [])
{
$parameters = array_merge(
$this->extractURLParameters($url),
$parameters
);
$this->decline_url = $url;
$this->decline_parameters = $parameters;
return $this;
}
/**
* Sets boths url for acceptance and declination to the same url.
*
* @param string $url
* @return QuestionBox instance to allow chaining
*/
public function setBaseURL($url)
{
$this->setAcceptURL($url);
$this->setDeclineURL($url);
return $this;
}
/**
* Defines whether a stud.ip ticket should be included in the question.
*
* @param bool $name
* @return QuestionBox instance to allow chaining
*/
public function includeTicket($name = 'studip_ticket')
{
$this->include_ticket = $name;
return $this;
}
/**
* Renders the question box as html.
*
* @return string
*/
public function __toString()
{
// Include fresh ticket
if ($this->include_ticket) {
$this->accept_parameters[$this->include_ticket] = get_ticket();
}
return $GLOBALS['template_factory']->render('shared/question-box', [
'question' => $this->question,
'accept_url' => $this->accept_url,
'accept_parameters' => $this->accept_parameters,
'decline_url' => $this->decline_url,
'decline_parameters' => $this->decline_parameters,
]);
}
/**
* Extracts parameters from a url.
*
* @param string $url
* @return array
*/
protected function extractURLParameters($url)
{
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $parameters);
return $parameters;
}
}
|