-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathTimeTracking.php
More file actions
296 lines (257 loc) · 9.7 KB
/
TimeTracking.php
File metadata and controls
296 lines (257 loc) · 9.7 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/*
Copyright 2011 Michael L. Baker
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Notes: Based on the Time Tracking plugin by Elmar:
2005 by Elmar Schumacher - GAMBIT Consulting GmbH
http://www.mantisbt.org/forums/viewtopic.php?f=4&t=589
*/
class TimeTrackingPlugin extends MantisPlugin {
function register() {
plugin_require_api( 'core/constants.php' );
$this->name = plugin_lang_get( 'plugin_title' );
$this->description = plugin_lang_get( 'plugin_description' );
$this->page = 'config_page';
$this->version = '3.0-dev';
$this->requires = array(
'MantisCore' => '2.13.0'
);
$this->author = 'Elmar Schumacher, Michael Baker, Erwann Penet';
$this->contact = '';
$this->url = 'https://github.com/mantisbt-plugins/timetracking';
}
function hooks() {
return array(
'EVENT_LAYOUT_RESOURCES' => 'resources',
'EVENT_VIEW_BUG_EXTRA' => 'ev_view_bug',
'EVENT_MENU_ISSUE' => 'timerecord_menu',
'EVENT_MENU_MAIN' => 'showreport_menu',
'EVENT_VIEW_BUGNOTE' => 'ev_view_bugnote',
'EVENT_BUGNOTE_ADD_FORM' => 'ev_bugnote_add_form',
'EVENT_BUGNOTE_DATA' => 'ev_bugnote_add_validate',
'EVENT_BUGNOTE_ADD' => 'ev_bugnote_added',
'EVENT_VIEW_BUG_DETAILS' => 'ev_view_bug_details',
'EVENT_LAYOUT_CONTENT_BEGIN' => 'ev_layout_content_begin',
'EVENT_FILTER_COLUMNS' => 'ev_register_columns',
);
}
function config() {
return array(
# old thresholds
/*
'admin_own_threshold' => DEVELOPER,
'view_others_threshold' => MANAGER,
'admin_threshold' => ADMINISTRATOR,
*/
# new thresholds
'view_threshold' => DEVELOPER,
'edit_threshold' => DEVELOPER,
'reporting_threshold' => MANAGER,
'stopwatch_enabled' => ON,
'categories' => ''
);
}
function init() {
plugin_require_api( 'core/timetracking_api.php' );
plugin_require_api( 'core/stopwatch_api.php' );
plugin_require_api( 'core/columns.php' );
plugin_require_api( 'core/reports.php' );
}
function errors() {
return array(
TimeTracking\ERROR_INVALID_TIME_FORMAT => plugin_lang_get( 'ERROR_INVALID_TIME_FORMAT' ),
TimeTracking\ERROR_ID_NOT_EXISTS => plugin_lang_get( 'ERROR_ID_NOT_EXISTS' ),
);
}
function resources() {
$res = '<link rel="stylesheet" type="text/css" href="'. plugin_file( 'timetracking.css' ) .'"/>';
$res .= '<script type="text/javascript" src="'. plugin_file( 'timetracking.js' ) .'"></script>';
$t_translation_js = plugin_page( 'javascript_translations' );
if( function_exists( 'helper_generate_cache_key' ) ) {
$t_translation_js .= '&cache_key=' . helper_generate_cache_key( array( 'lang' ) );
}
$res .= '<script type="text/javascript" src="'. $t_translation_js . '"></script>';
if( \TimeTracking\stopwatch_enabled() ) {
$res .= '<script type="text/javascript" src="'. plugin_file( 'stopwatch.js' ) .'"></script>';
}
return $res;
}
function ev_register_columns( $p_event ) {
return array(
new TimeTracking\ColumnTotalTime(),
new TimeTracking\ColumnMyTime(),
);
}
/**
* Show time tracking info within the bugnote activity area
*/
function ev_view_bugnote( $p_event, $p_bug_id, $p_note_id, $p_is_private ) {
$t_record = TimeTracking\get_record_for_bugnote( $p_note_id );
if( !$t_record ) {
return;
}
if( TimeTracking\user_can_view_record_id( $t_record['id'] ) ) {
TimeTracking\print_bugnote_label_row( $t_record, $p_is_private );
}
}
/**
* Prints the time tracking inputs within the bugnote-add form
* @param type $p_event
* @param type $p_bug_id
*/
function ev_bugnote_add_form( $p_event, $p_bug_id ) {
if( TimeTracking\user_can_edit_bug_id( $p_bug_id ) ) {
TimeTracking\print_bugnote_add_form( $p_bug_id );
}
}
/**
* Validates time tracking submitted data when adding bugnotes
*/
function ev_bugnote_add_validate( $p_event, $p_bugnote_text, $p_bug_id ) {
$t_time_imput = gpc_get_string( 'plugin_timetracking_time_input', '' );
if( !is_blank( $t_time_imput ) ) {
if( TimeTracking\user_can_edit_bug_id( $p_bug_id ) ) {
$t_parsed = TimeTracking\parse_gpc_time_record();
}
}
return $p_bugnote_text;
}
/**
* Creates a time tracking record from submitted data when adding bugnotes
*/
function ev_bugnote_added( $p_event, $p_bug_id, $p_bugnote_id, $files) {
$t_time_imput = gpc_get_string( 'plugin_timetracking_time_input', '' );
if( !is_blank( $t_time_imput ) ) {
if( TimeTracking\user_can_edit_bug_id( $p_bug_id ) ) {
$t_record = TimeTracking\parse_gpc_time_record();
$t_record['bugnote_id'] = $p_bugnote_id;
$t_record['bug_id'] = $p_bug_id;
TimeTracking\create_record( $t_record );
}
}
}
function ev_view_bug_details( $p_event, $p_bug_id ) {
if( TimeTracking\user_can_view_bug_id( $p_bug_id ) ) {
$t_records = TimeTracking\get_records_for_bug( $p_bug_id );
if( $t_records ) {
TimeTracking\print_bug_details_row( $p_bug_id );
}
}
}
function ev_layout_content_begin( $p_event ) {
if( TimeTracking\stopwatch_enabled() && TimeTracking\stopwatch_exists() ) {
TimeTracking\print_stopwatch_header_control();
}
}
function ev_view_bug( $p_event, $p_bug_id ) {
if( TimeTracking\user_can_view_bug_id( $p_bug_id ) ) {
TimeTracking\print_bug_timetracking_section( $p_bug_id );
}
}
function schema() {
$t_current_schema_version = config_get( 'plugin_TimeTracking_schema', -1 );
$t_migration_from_v2 = ( $t_current_schema_version >= 0 && $t_current_schema_version <= 9 );
# Schema 0 had a column named "user", which is a reserved word in some databases
# (eg, postgres). This column has been later renamed to "user_id" with a new plugin
# version (steps 1 to 9). However, since the installation for a new instance starts
# from step 0, the process fails.
# If this is a new installation, we can skip the original table and subsequent upgrade
# and create directly the new table structure.
if( $t_migration_from_v2 ) {
$schema[0] =
array( 'CreateTableSQL', array( plugin_table( 'data' ), "
id I NOTNULL UNSIGNED AUTOINCREMENT PRIMARY,
bug_id I DEFAULT NULL UNSIGNED,
user I DEFAULT NULL UNSIGNED,
expenditure_date T DEFAULT NULL,
hours F(15,3) DEFAULT NULL,
timestamp T DEFAULT NULL,
category C(255) DEFAULT NULL,
info C(255) DEFAULT NULL
" )
);
$schema[1] =
array(
'AddColumnSQL',
array( plugin_table( 'data' ),
" user_id I UNSIGNED,
date_created I UNSIGNED NOTNULL DEFAULT '1',
time_count I UNSIGNED NOTNULL DEFAULT '0',
time_exp_date I UNSIGNED NOTNULL DEFAULT '1',
bugnote_id I UNSIGNED"
)
);
$schema[2] = array( 'UpdateFunction', 'date_migrate', array( plugin_table( 'data' ), 'id', 'expenditure_date', 'time_exp_date' ) );
$schema[3] = array( 'UpdateFunction', 'date_migrate', array( plugin_table( 'data' ), 'id', 'timestamp', 'date_created' ) );
$schema[4] = array( 'UpdateFunction', 'timetracking_update_hours', array() );
$schema[5] = array( 'UpdateFunction', 'timetracking_update_user_id', array() );
$schema[6] = array( 'DropColumnSQL', array( plugin_table( 'data' ), 'user' ) );
$schema[7] = array( 'DropColumnSQL', array( plugin_table( 'data' ), 'expenditure_date' ) );
$schema[8] = array( 'DropColumnSQL', array( plugin_table( 'data' ), 'timestamp' ) );
$schema[9] = array( 'DropColumnSQL', array( plugin_table( 'data' ), 'hours' ) );
} else {
$schema[0] =
array( 'CreateTableSQL', array( plugin_table( 'data' ), "
id I NOTNULL UNSIGNED AUTOINCREMENT PRIMARY,
bug_id I DEFAULT NULL UNSIGNED,
user_id I UNSIGNED,
date_created I UNSIGNED NOTNULL DEFAULT '1',
time_count I UNSIGNED NOTNULL DEFAULT '0',
time_exp_date I UNSIGNED NOTNULL DEFAULT '1',
bugnote_id I UNSIGNED,
category C(255) DEFAULT NULL,
info C(255) DEFAULT NULL
" )
);
$schema[1] = null;
$schema[2] = null;
$schema[3] = null;
$schema[4] = null;
$schema[5] = null;
$schema[6] = null;
$schema[7] = null;
$schema[8] = null;
$schema[9] = null;
}
return $schema;
}
function timerecord_menu( $p_event, $p_bug_id ) {
if( TimeTracking\user_can_view_bug_id( $p_bug_id ) ) {
$t_href = '#timerecord';
return array( plugin_lang_get( 'timerecord_menu' ) => $t_href );
}
else {
return array ();
}
}
function showreport_menu() {
return array(
array(
'title' => plugin_lang_get( 'title' ),
'access_level' => plugin_config_get( 'reporting_threshold' ),
'url' => plugin_page( 'report_page' ),
'icon' => 'fa-random'
)
);
}
} # class end
function install_timetracking_update_hours() {
$t_query = new DbQuery( 'UPDATE ' . plugin_table( 'data' ) . ' SET time_count = hours*3600 WHERE time_count = 0' );
$t_query->execute();
# Return 2 because that's what ADOdb/DataDict does when things happen properly
return 2;
}
function install_timetracking_update_user_id() {
$t_query = new DbQuery( 'UPDATE ' . plugin_table( 'data' ) . ' SET user_id = user WHERE user_id IS NULL' );
$t_query->execute();
# Return 2 because that's what ADOdb/DataDict does when things happen properly
return 2;
}