-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_log.module
More file actions
397 lines (356 loc) · 12 KB
/
time_log.module
File metadata and controls
397 lines (356 loc) · 12 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* Time Log
* Adaptation of Support Timer module
**/
/**
* @file time_log.module
*/
/**
* Implementation of hook_help()
**/
function time_log_help($path, $arg) {
switch ($path) {
case 'admin/help#time_log':
return '<p>' . t('Time log is a content type with a timer attached. You can create node relationships to other content types, such as tasks. You can also use the time field in views.') . '</p>';
break;
}
}
/**
* Implementation of hook_menu()
**/
function time_log_menu() {
$items = array();
$items['admin/settings/time-log'] = array(
'title' => 'Time Log',
'description' => 'Configuration settings for the time log.',
'page callback' => 'drupal_get_form',
'page arguments' => array('time_log_admin'),
'access arguments' => array('administer time log'),
);
return $items;
}
/**
* Implementation of hook_node_info().
*/
function time_log_node_info() {
return array(
'time_log' => array(
'name' => t('Time Log'),
'module' => 'time_log',
'description' => "This is a time log with a time field.",
)
);
}
/**
* Implementation of hook_perm().
*/
function time_log_perm() {
return array('view time spent', 'edit time spent', 'track time spent', 'administer time log', 'create time log','edit any time log','edit own time log','delete any time log','delete own time log');
}
/**
* Implementation of hook_access().
*/
function time_log_access($op, $node, $account) {
if ($op == 'create') {
// Only users with permission to do so may create this node type.
return user_access('create time log', $account);
}
// Users who create a node may edit, assuming they have the
// necessary permissions.
if ($op == 'update') {
if (user_access('edit own time log', $account) && ($account->uid == $node->uid)) {
return TRUE;
}
elseif (user_access('edit any time log', $account)) {
return TRUE;
}
}
// Users with permissions may delete the node
if ($op == 'delete') {
if (user_access('delete own time log',$account) && ($account->uid == $node->uid)) {
return TRUE;
}
elseif (user_access('delete any time log',$account)) {
return TRUE;
}
}
}
/**
* Implementation of hook_form()
**/
function time_log_form(&$node, $form_state) {
$type = node_get_types('type', $node);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);
}
if ($type->has_body) {
$form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
}
return $form;
}
/**
* Implementation of hook_admin()
*
* Make it possible to disable javascript that displays a warning if a user
* navigates away from a page without saving changes.
**/
function time_log_admin() {
$form = array();
$form['time_log_unload_warning'] = array(
'#type' => 'checkbox',
'#title' => t('Warn user if navigating away from task without saving timer information'),
'#default_value' => variable_get('time_log_unload_warning', TRUE),
);
$form['time_log_start'] = array(
'#type' => 'textfield',
'#title' => t('Starting Time'),
'#description' => t('The starting time for the timer. Must be in this form: hh:mm:ss. E.g.: Enter 00:15:00 to add 15 minutes before starting any clock.'),
'#size' => 10,
'#maxlength' => 8,
'#default_value' => variable_get('time_log_start','00:00:00'),
'#element_validate' => array('time_log_time_element_validator'),
);
return system_settings_form($form);
}
/**
* Implementation of hook_form_alter().
**/
function time_log_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'time_log_node_form') {
if (isset($form['nid']) && is_array($form['nid']) &&
isset($form['nid']['#value'])) {
$form = array_merge(time_log_display_timer(array('nid' => $form['nid']['#value'])), $form);
}
else {
$reference = array();
$form = array_merge(time_log_add_timer($form_state, $form), $form);
}
}
}
/**
* Implementation of hook_nodeapi().
**/
function time_log_nodeapi(&$node, $op, $teaser, $page) {
if ($node->type == 'time_log') {
switch ($op) {
case 'view':
if (user_access('view time spent')) {
// Display time spent on ticket.
$timer = db_fetch_object(db_query('SELECT time FROM {time_log} WHERE nid = %d', $node->nid));
if (!empty($timer) && $timer->time) {
$weight = content_extra_field_weight($node->type,'timer');
$node->content['timer'] = array(
'#value' => "<div class='task-timer'><div class='time'>Time: $timer->time</div></div>",
'#weight' => !empty($weight) ? $weight : 100,
);
}
}
break;
case 'load':
return db_fetch_array(db_query('SELECT time FROM {time_log} WHERE nid = %d', $node->nid));
break;
case 'validate':
if (user_access('track time spent') || user_access('administer time log')) {
}
case 'insert':
case 'update':
$date = strtotime($node->timer_date);
db_query("UPDATE {time_log} SET time = '%s' WHERE nid = %d", $node->elapsed, $node->nid);
if (!db_affected_rows()) {
db_query("INSERT INTO {time_log} (nid, time) VALUES(%d, '%s')", $node->nid, $node->elapsed);
}
break;
case 'delete':
db_query('DELETE FROM {time_log} WHERE nid = %d', $node->nid);
break;
}
}
}
/**
* Display form for tracking time elapsed.
**/
function time_log_add_timer(&$form_state, $edit) {
$form = array();
$start = variable_get('time_log_start','00:00:00');
$start_time = explode(':', $start);
$start_hours = ltrim($start_time[0],'00');
$start_mins = ltrim($start_time[1],'00');
$start_secs = ltrim($start_time[2],'00');
if (user_access('track time spent') || user_access('administer time log')) {
if (isset($form_state['values']['elapsed'])) {
$elapsed = $form_state['values']['elapsed'];
}
else if (isset($form_state['post']['elapsed'])) {
$elapsed = $form_state['post']['elapsed'];
}
else {
$elapsed = $start;
}
drupal_add_js(array('time_log' => array('unload_warning' => variable_get('time_log_unload_warning', TRUE), 'elapsed' => $elapsed, 'start_hours' => $start_hours, 'start_mins' => $start_mins, 'start_secs' => $start_secs, 'start' => $start)), 'setting');
drupal_add_js(drupal_get_path('module', 'time_log'). '/time_log.js');
$form['timer'] = array(
'#type' => 'fieldset',
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
'#weight' => content_extra_field_weight($node->type,'timer'),
);
if (!user_access('edit time spent') && !user_access('administer time log')) {
$attributes = array('disabled' => 'disabled');
}
else {
$attributes = array();
}
$visible = user_access('view time spent');
$form['timer']['elapsed'] = array(
'#type' => $visible ? 'textfield' : 'hidden',
'#title' => t('Time spent'),
'#description' => t('(hh:mm:ss)'),
'#size' => '8',
'#maxlength' => '10',
'#default_value' => $elapsed,
'#attributes' => $attributes,
'#element_validate' => array('time_log_time_element_validator'),
);
$form['timer']['pause'] = array(
'#type' => $visible ? 'submit' : 'hidden',
'#value' => t('Start'),
'#prefix' => ' ',
'#attributes' => array('onclick' => 'pause_timer(); return false;'),
);
$form['timer']['reset'] = array(
'#type' => $visible ? 'submit' : 'hidden',
'#value' => t('Reset'),
'#attributes' => array('onclick' => 'reset_timer(); return false;'),
);
}
return $form;
}
/**
* Validation of the time element.
**/
function time_log_time_element_validator($form, &$form_state) {
$elapsed_pattern_structure = '/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/';
$elapsed_pattern_limit = '/^[0-9]{2}:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/';
if(!preg_match($elapsed_pattern_structure,$form['#value'])) {
form_set_error('text', t('The time must be in this form: hh:mm:ss. E.g. 01:25:00 for 1 hour and 25 minutes.'));
}
if(!preg_match($elapsed_pattern_limit,$form['#value'])) {
form_set_error('text', t('The hours cannot be greater than 99; the minutes cannot be greater than 59 and the seconds cannot be greater than 59.'));
}
}
/**
* Display time form on node edit.
*/
function time_log_display_timer($ids) {
$form = array();
$details = db_fetch_object(db_query('SELECT time FROM {time_log} WHERE nid = %d', $ids['nid']));
$elapsed = $details->time;
$start = variable_get('time_log_start','00:00:00');
$start_time = explode(':', $start);
$start_hours = ltrim($start_time[0],'00');
$start_mins = ltrim($start_time[1],'00');
$start_secs = ltrim($start_time[2],'00');
if (user_access('view time spent') || user_access('edit time spent') ||
user_access('administer time log')) {
drupal_add_js(array('time_log' => array('unload_warning' => variable_get('time_log_unload_warning', TRUE), 'elapsed' => $elapsed, 'start_hours' => $start_hours, 'start_mins' => $start_mins, 'start_secs' => $start_secs, 'start' => $start)), 'setting');
drupal_add_js(drupal_get_path('module', 'time_log'). '/time_log.js');
$form['timer'] = array(
'#type' => 'fieldset',
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
'#weight' => content_extra_field_weight($node->type,'timer'),
);
if (!user_access('edit time spent') && !user_access('administer time log')) {
$attributes = array('disabled' => 'disabled');
}
else {
$attributes = array();
}
$visible = user_access('view time spent');
$form['timer']['elapsed'] = array(
'#type' => $visible ? 'textfield' : 'hidden',
'#title' => t('Time spent'),
'#description' => t('(hh:mm:ss)'),
'#size' => '8',
'#maxlength' => '10',
'#default_value' => $elapsed,
'#attributes' => $attributes,
'#element_validate' => array('time_log_time_element_validator'),
);
$form['timer']['pause'] = array(
'#type' => $visible ? 'submit' : 'hidden',
'#value' => t('Start'),
'#prefix' => ' ',
'#attributes' => array('onclick' => 'pause_timer(); return false;'),
);
$form['timer']['reset'] = array(
'#type' => $visible ? 'submit' : 'hidden',
'#value' => t('Reset'),
'#attributes' => array('onclick' => 'reset_timer(); return false;'),
);
}
return $form;
}
/*
* Implementation of hook_views_api()
*
* Expose time element to Views
*/
function time_log_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'time_log'),
);
}
/**
* Implementation of hook_content_extra_fields()
*/
function time_log_content_extra_fields($type_name) {
if ($type_name == 'time_log') {
$extras['timer'] = array(
'label' => t('Timer'),
'description' => t('Timer for time log content type.'),
'weight' => 100,
);
return $extras;
}
}
/**
* Need way to convert back and forth from time
http://drupal.org/node/131031
//Stick this into the views footer, phptemplate, or make a proper module
$my_view = views_get_current_view();
$total = 0;
foreach($my_view->result as $row) {
$total = $total + convert_seconds($row->time_log_time);
$total_timelog = convert_timelog($total);
$total_hours = round($total/3600,2);
}
print '<table>';
print '<tr><td><strong>';
print 'Total Time</strong></td><td>' . $total_hours . 'h</td><td>' . $total_timelog . '</td></tr>';
print '</table>';
//convert hh:mm:ss to #seconds
function convert_seconds($timelog) {
$time = explode(':', $timelog);
$time = $time[0] * 3600 + $time[1] * 60 + $time[2];
return $time;
}
//convert seconds to hh:mm:ss
function convert_timelog($seconds) {
$hh = 00;
$mm = 00;
if ($seconds > 3600) { $hh = floor($seconds/3600); }
if ($seconds > 60) {$mm = floor($seconds/60) - $hh*60; }
$ss = $seconds - ($hh*3600 + $mm*60);
$timelog = $hh . ':' . $mm . ':' . $ss;
return $timelog;
}
**/