Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control which interface can see timers and include closed box timer #38

Merged
merged 6 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 86 additions & 27 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,36 @@ static function getTypeName($nb = 0) {

function showForm() {

$rand = mt_rand();

$this->getFromDB(1);
$this->showFormHeader();

echo "<input type='hidden' name='id' value='1'>";

$values = [
0 => __('In Standard interface only (default)', 'actualtime'),
1 => __('Both in Standard and Helpdesk interfaces', 'actualtime'),
];
echo "<tr class='tab_bg_1'>";
echo "<td>" . __("Enable timer on tasks", "actualtime") . "</td><td>";
Dropdown::showYesNo('enable', $this->isEnabled(), -1,
['on_change' => 'show_hide_options(this.value);']);
Dropdown::showFromArray(
'displayinfofor',
$values,
[
'value' => $this->fields['displayinfofor']
]
);
echo "</td>";
echo "</tr>";

echo Html::scriptBlock("
function show_hide_options(val) {
var display = (val == 0) ? 'none' : '';
$('tr[name=\"optional$rand\"').css( 'display', display );
}");

$style = ($this->isEnabled()) ? "" : "style='display: none '";

// Include lines with other settings

echo "<tr class='tab_bg_1' name='optional$rand' $style>";
echo "<tr class='tab_bg_1'>";
echo "<td>" . __("Display pop-up window with current running timer", "actualtime") . "</td><td>";
Dropdown::showYesNo('showtimerpopup', $this->showTimerPopup(), -1);
echo "</td>";
echo "</tr>";

echo "<tr class='tab_bg_1'>";
echo "<td>" . __("Display actual time in closed task box ('Processing ticket' list)", "actualtime") . "</td><td>";
Dropdown::showYesNo('showtimerinbox', $this->showTimerInBox(), -1);
echo "<tr class='tab_bg_1' name='optional$rand' $style>";
echo "<td>" . __("Automatically open new created tasks", "actualtime") . "</td><td>";
Dropdown::showYesNo('autoopennew', $this->autoOpenNew(), -1);
Expand Down Expand Up @@ -124,21 +124,30 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtem
}

/**
* Plugin is enabled in plugin settings?
* Is displaying timer pop-up on every page enabled in plugin settings?
*
* @return boolean
*/
function isEnabled() {
return ($this->fields['enable'] ? true : false);
function showTimerPopup() {
return ($this->fields['showtimerpopup'] ? true : false);
}

/**
* Timer pop-up display on every page enabled in plugin settings?
* Is actual time information (timers) shown also in Helpdesk interface?
*
* @return boolean
*/
function showTimerPopup() {
return ($this->fields['showtimerpopup'] ? true : false);
function showInHelpdesk() {
return ($this->fields['displayinfofor'] == 1);
}

/**
* Is timer shown in closed task box at 'Actions historical' page?
*
* @return boolean
*/
function showTimerInBox() {
return ($this->fields['showtimerinbox'] ? true : false);
}

/**
Expand All @@ -165,10 +174,14 @@ static function install(Migration $migration) {

$table = self::getTable();
if (! $DB->tableExists($table)) {

$migration->displayMessage("Installing $table");

$query = "CREATE TABLE IF NOT EXISTS $table (
`id` int(11) NOT NULL auto_increment,
`displayinfofor` smallint NOT NULL DEFAULT 0,
`showtimerpopup` boolean NOT NULL DEFAULT true,
`showtimerinbox` boolean NOT NULL DEFAULT true,
`enable` boolean NOT NULL DEFAULT true,
`showtimerpopup` boolean NOT NULL DEFAULT true,
`autoopennew` boolean NOT NULL DEFAULT false,
Expand All @@ -177,19 +190,48 @@ static function install(Migration $migration) {
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->query($query) or die($DB->error());
}
} else {

$fields = $DB->list_fields($table, false);

if ($DB->tableExists($table)) {
if (! $DB->fieldExists($table, 'showtimerpopup')) {
// Add new field showtimerpopup
if (! isset($fields['displayinfofor'])) {
// For whom the actualtime timers are displayed?
// 0 - Only in standard/central interface (default)
// 1 - Both in standard and helpdesk interfaces
$migration->addField(
$table,
'displayinfofor',
'smallint',
[
'update' => 0,
'value' => 0,
'after' => 'enable'
]
);
}

if (! isset($fields['showtimerpopup'])) {
$migration->addField(
$table,
'showtimerpopup',
'boolean',
[
'update' => true,
'value' => true,
'after' => 'enable'
'after' => 'displayinfofor'
]
);
}

if (! isset($fields['showtimerinbox'])) {
$migration->addField(
$table,
'showtimerinbox',
'boolean',
[
'update' => true,
'value' => true,
'after' => 'showtimerpopup'
]
);
}
Expand All @@ -202,7 +244,7 @@ static function install(Migration $migration) {
[
'update' => false,
'value' => false,
'after' => 'showtimerpopup',
'after' => 'showtimerinbox',
]
);
}
Expand All @@ -228,7 +270,24 @@ static function install(Migration $migration) {
]
);
}
// Old not used field in version 1.1.1
if (isset($fields['enable'])) {
$migration->dropField(
$table,
'enable'
);
}

}

// Create default record (if it does not exist)
$reg = $DB->request($table);
if (! count($reg)) {
$DB->insert(
$table, [
'displayinfofor' => 0
]
);
}

}
Expand Down
105 changes: 75 additions & 30 deletions inc/task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ static public function postForm($params) {
case 'TicketTask':
if ($item->getID()) {

$config = new PluginActualtimeConfig;

$task_id = $item->getID();
$rand = mt_rand();
$buttons = (self::checkTech($task_id) && $item->can($task_id, UPDATE));
$time = self::totalEndTime($task_id);
$text_restart = __('Restart', 'actualtime');
$text_pause = __('Pause', 'actualtime');
$html = '';
$script = <<<JAVASCRIPT
$(document).ready(function() {

JAVASCRIPT;

// Only task user
if ($buttons) {

$value1 = __('Start');
Expand Down Expand Up @@ -72,27 +80,18 @@ static public function postForm($params) {

}

$html="<tr class='tab_bg_2'>";
$html.="<td colspan='2'></td>";
$html.="<td colspan='2'>";
$html = "<tr class='tab_bg_2'>";
$html .= "<td colspan='2'></td>";
$html .= "<td colspan='2'>";
// Objects of the same task have the same id beginning
// as they all should be changed on actions in case multiple
// windows of the same task is opened (list of tasks + modal)
$html.="<div><input type='button' id='actualtime_button_{$task_id}_1_{$rand}' action='$action1' value='$value1' class='x-button x-button-main' style='background-color:$color1;color:white' $disabled1></div>";
$html.="<div><input type='button' id='actualtime_button_{$task_id}_2_{$rand}' action='$action2' value='".__('End')."' class='x-button x-button-main' style='background-color:$color2;color:white' $disabled2></div>";
$html.="</td></tr>";
$html.="<tr class='tab_bg_2'>";
$html.="<td class='center'>".__("Start date")."</td><td class='center'>".__("Partial actual duration", 'actualtime')."</td>";
$html.="<td>".__('Actual Duration', 'actualtime')." </td><td id='actualtime_timer_{$task_id}_{$rand}' style='color:{$timercolor}'>".HTML::timestampToString($time)."</td>";
$html.="</tr>";
$html.="<tr id='actualtime_segment_{$task_id}_{$rand}'>";
$html.=self::getSegment($item->getID());
$html.="</tr>";
echo $html;

$script=<<<JAVASCRIPT
$(document).ready(function() {
$html .= "<div><input type='button' id='actualtime_button_{$task_id}_1_{$rand}' action='$action1' value='$value1' class='x-button x-button-main' style='background-color:$color1;color:white' $disabled1></div>";
$html .= "<div><input type='button' id='actualtime_button_{$task_id}_2_{$rand}' action='$action2' value='".__('End')."' class='x-button x-button-main' style='background-color:$color2;color:white' $disabled2></div>";
$html .= "</td></tr>";

// Only task user have buttons
$script .= <<<JAVASCRIPT
$("#actualtime_button_{$task_id}_1_{$rand}").click(function(event) {
actualtime_pressedButton($task_id, $(this).attr('action'));
});
Expand All @@ -101,20 +100,35 @@ static public function postForm($params) {
actualtime_pressedButton($task_id, $(this).attr('action'));
});

JAVASCRIPT;

}

// Task user (always) or Standard interface (always)
// or Helpdesk inteface (only if config allows)
if ($buttons
|| (Session::getCurrentInterface() == "central")
|| $config->showInHelpdesk()) {

$html .= "<tr class='tab_bg_2'>";
$html .= "<td class='center'>" . __("Start date") . "</td><td class='center'>" . __("Partial actual duration", 'actualtime') . "</td>";
$html .= "<td>" . __('Actual Duration', 'actualtime') . " </td><td id='actualtime_timer_{$task_id}_{$rand}' style='color:{$timercolor}'></td>";
$html .= "</tr>";
$html .= "<tr id='actualtime_segment_{$task_id}_{$rand}'>";
$html .= self::getSegment($item->getID());
$html .= "</tr>";

echo $html;

// Finally, fill the actual total time in all timers
$script .= <<<JAVASCRIPT

actualtime_fillCurrentTime($task_id, $time);

});
JAVASCRIPT;
echo Html::scriptBlock($script);
} else {
$time=self::totalEndTime($item->getID());

$html="<tr class='tab_bg_2'>";
$html.="<td class='center'>".__("Start date")."</td><td class='center'>".__("Partial actual duration", 'actualtime')."</td>";
$html.="<td>".__('Actual Duration', 'actualtime')." </td><td id='actualtime_timer_{$task_id}_{$rand}'>".HTML::timestampToString($time)."</td>";
$html.="</div></td></tr>";
$html.="<tr id='actualtime_segment_{$task_id}_{$rand}'>";
$html.=self::getSegment($item->getID());
$html.="</tr>";
echo $html;

}
}
break;
Expand Down Expand Up @@ -297,7 +311,10 @@ static function getActualBegin($task_id) {
static public function showStats(Ticket $ticket) {
global $DB;

if (Session::getCurrentInterface() == "central") {
$config = new PluginActualtimeConfig;
if ((Session::getCurrentInterface() == "central")
|| $config->showInHelpdesk()) {

$total_time=$ticket->getField('actiontime');
$ticket_id=$ticket->getID();
$actual_totaltime=0;
Expand Down Expand Up @@ -509,6 +526,35 @@ static function postShowItem($params) {
switch ($item->getType()) {
case 'TicketTask':

$task_id = $item->getID();
$rand = mt_rand();

$config = new PluginActualtimeConfig;
// Standard interface (always)
// or Helpdesk inteface (only if config allows)
if ((Session::getCurrentInterface() == "central")
|| $config->showInHelpdesk()) {

$time = self::totalEndTime($task_id);
$fa_icon = ($time > 0 ? ' fa-clock-o' : '');
$timercolor = (self::checkTimerActive($task_id) ? 'red' : 'black');
// Anchor to find correct span, even when user has no update
// right on status checkbox
echo "<div id='actualtime_anchor_$rand'></div>";
$script = <<<JAVASCRIPT
$(document).ready(function() {
if ($("[id^='actualtime_faclock_{$task_id}_']").length == 0) {
$("#actualtime_anchor_{$rand}").prev().find("span.state")
.after("<i id='actualtime_faclock_{$task_id}_{$rand}' class='fa{$fa_icon}' style='color:{$timercolor}; padding:3px; vertical-align:middle;'></i><span id='actualtime_timer_{$task_id}_box_{$rand}' style='color:{$timercolor}; vertical-align:middle;'></span>");
if ($time > 0) {
actualtime_fillCurrentTime($task_id, $time);
}
}
});
JAVASCRIPT;
echo Html::scriptBlock($script);
}
break;
$config = new PluginActualtimeConfig;
$task_id = $item->getID();
// Auto open needs to use correct item randomic number
Expand Down Expand Up @@ -576,7 +622,6 @@ function waitForFormLoad(i){
}
}
break;

}
}

Expand Down
7 changes: 7 additions & 0 deletions js/actualtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ function actualtime_endCount(){
clearInterval(timer);
}

function actualtime_fillCurrentTime(task, time) {
var timestr = actualtime_timeToText(time, 1);
$("[id^='actualtime_timer_" + task + "_']").text(timestr);
}

function actualtime_pressedButton(task, val) {
jQuery.ajax({
type: "POST",
Expand All @@ -142,12 +147,14 @@ function actualtime_pressedButton(task, val) {
$("[id^='actualtime_button_" + task + "_1_']").attr('value', text_pause).attr('action', 'pause').css('background-color', 'orange').prop('disabled', false);
$("[id^='actualtime_button_" + task + "_2_']").attr('action', 'end').css('background-color', 'red').prop('disabled', false);
actualtime_showTimerPopup(result['ticket_id']);
$("[id^='actualtime_faclock_" + task + "_']").addClass('fa-clock-o').css('color', 'red');
return;
} else if ((val == 'end') || (val == 'pause')) {
actualtime_endCount();
$("#actualtime_popup").remove();
// Update all forms of this task (normal and modal)
$("[id^='actualtime_timer_" + task + "_']").css('color', 'black');
$("[id^='actualtime_faclock_" + task + "_']").css('color', 'black');
var timestr = actualtime_timeToText(result['time'], 1);
$("[id^='actualtime_timer_" + task + "_']").text(timestr);
$("[id^='actualtime_segment_" + task + "_']").html(result['segment']);
Expand Down
Loading