Implemented tracking of who initiated an event.

This commit is contained in:
Erik Thuning 2025-03-14 14:12:28 +01:00
parent ee082de50a
commit 63ccad38c8
11 changed files with 76 additions and 12 deletions

@ -642,6 +642,9 @@
<th> <th>
End date End date
</th> </th>
<th>
Started by
</th>
<th> <th>
Misc Misc
</th> </th>
@ -668,6 +671,9 @@
<td> <td>
¤end_date¤ ¤end_date¤
</td> </td>
<td>
¤initiator¤
</td>
<td> <td>
¤note¤ ¤note¤
</td> </td>
@ -688,6 +694,9 @@
<th> <th>
End date End date
</th> </th>
<th>
Started by
</th>
<th> <th>
Misc Misc
</th> </th>
@ -711,6 +720,9 @@
<td> <td>
¤end_date¤ ¤end_date¤
</td> </td>
<td>
¤initiator¤
</td>
<td> <td>
¤note¤ ¤note¤
</td> </td>

@ -642,6 +642,9 @@
<th> <th>
Slutdatum Slutdatum
</th> </th>
<th>
Startat av
</th>
<th> <th>
Övrigt Övrigt
</th> </th>
@ -668,6 +671,9 @@
<td> <td>
¤end_date¤ ¤end_date¤
</td> </td>
<td>
¤initiator¤
</td>
<td> <td>
¤note¤ ¤note¤
</td> </td>
@ -688,6 +694,9 @@
<th> <th>
Slutdatum Slutdatum
</th> </th>
<th>
Startat av
</th>
<th> <th>
Övrigt Övrigt
</th> </th>
@ -711,6 +720,9 @@
<td> <td>
¤end_date¤ ¤end_date¤
</td> </td>
<td>
¤initiator¤
</td>
<td> <td>
¤note¤ ¤note¤
</td> </td>

@ -92,7 +92,10 @@ class Ajax extends Responder {
return new Failure(i18n('Invalid serial number.')); return new Failure(i18n('Invalid serial number.'));
} }
try { try {
Loan::create_loan($user, $product, $_POST['end']); Loan::create_loan($user,
$product,
$_POST['end'],
$this->logged_in_user);
return new Success(i18n('{product} checked out.', $product)); return new Success(i18n('{product} checked out.', $product));
} catch(Exception $e) { } catch(Exception $e) {
return new Failure(i18n('The product is already on loan.')); return new Failure(i18n('The product is already on loan.'));
@ -385,7 +388,7 @@ class Ajax extends Responder {
private function toggle_service() { private function toggle_service() {
$product = new Product($_POST['id']); $product = new Product($_POST['id']);
try { try {
$product->toggle_service(); $product->toggle_service($this->logged_in_user);
return new Success(i18n('Product service status updated.')); return new Success(i18n('Product service status updated.'));
} catch(Exception $e) { } catch(Exception $e) {
return new Failure(i18n('Cannot register service.')); return new Failure(i18n('Cannot register service.'));

@ -5,7 +5,7 @@ class Event {
protected $starttime = 0; protected $starttime = 0;
protected $returntime = null; protected $returntime = null;
protected static function create_event($product, $type) { protected static function create_event($product, $type, $initiator) {
$status = $product->get_status(); $status = $product->get_status();
if($status != 'available') { if($status != 'available') {
$emsg = ''; $emsg = '';
@ -37,9 +37,10 @@ class Event {
} }
$now = time(); $now = time();
$insert = prepare('insert into `event` $insert = prepare('insert into `event`
(`product`, `type`, `starttime`) (`product`, `type`, `starttime`, `initiator`)
values (?, ?, ?)'); values (?, ?, ?, ?)');
bind($insert, 'isi', $product->get_id(), $type, $now); bind($insert, 'isis',
$product->get_id(), $type, $now, $initiator->get_id());
execute($insert); execute($insert);
$event_id = $insert->insert_id; $event_id = $insert->insert_id;
return new Event($event_id); return new Event($event_id);
@ -66,6 +67,7 @@ class Event {
$this->product = $result['product']; $this->product = $result['product'];
$this->starttime = $result['starttime']; $this->starttime = $result['starttime'];
$this->returntime = $result['returntime']; $this->returntime = $result['returntime'];
$this->initiator = $result['initiator'];
} }
public function get_id() { public function get_id() {
@ -76,6 +78,14 @@ class Event {
return new Product($this->product); return new Product($this->product);
} }
public function get_initiator() {
$initiator = $this->initiator;
if($initiator === null) {
return null;
}
return new User($this->initiator);
}
public function get_starttime() { public function get_starttime() {
return $this->starttime; return $this->starttime;
} }

@ -3,9 +3,9 @@ class Loan extends Event {
private $user = 0; private $user = 0;
private $endtime = 0; private $endtime = 0;
public static function create_loan($user, $product, $endtime) { public static function create_loan($user, $product, $endtime, $initiator) {
begin_trans(); begin_trans();
$event = parent::create_event($product, 'loan'); $event = parent::create_event($product, 'loan', $initiator);
$event_id = $event->get_id(); $event_id = $event->get_id();
$insert = prepare('insert into `loan`(`event`, `user`, `endtime`) $insert = prepare('insert into `loan`(`event`, `user`, `endtime`)
values (?, ?, ?)'); values (?, ?, ?)');

@ -224,6 +224,15 @@ abstract class Page extends Responder {
'page' => 'products'), 'page' => 'products'),
$this->fragments['item_link']); $this->fragments['item_link']);
$status = $loan->get_status(); $status = $loan->get_status();
$initiator = $loan->get_initiator();
$initiator_name = i18n('Unknown');
if($initiator) {
$initiator_name = replace(
array('id' => $initiator->get_id(),
'name' => $initiator->get_name(),
'page' => 'users'),
$this->fragments['item_link']);
}
$note = ''; $note = '';
if($status !== 'inactive_loan') { if($status !== 'inactive_loan') {
$extend = format_date($loan->get_endtime()); $extend = format_date($loan->get_endtime());
@ -241,6 +250,7 @@ abstract class Page extends Responder {
'serial' => $product->get_serial(), 'serial' => $product->get_serial(),
'start_date' => format_date($start), 'start_date' => format_date($start),
'end_date' => format_date($end), 'end_date' => format_date($end),
'initiator' => $initiator_name,
'note' => $note), 'note' => $note),
$this->fragments['user_loan_table_row']); $this->fragments['user_loan_table_row']);
} }

@ -344,14 +344,14 @@ EOF;
} }
} }
public function toggle_service() { public function toggle_service($initiator) {
$status = $this->get_status(); $status = $this->get_status();
$now = time(); $now = time();
$update = ''; $update = '';
if($status == 'service') { if($status == 'service') {
return $this->get_active_service()->end(); return $this->get_active_service()->end();
} else if($status == 'available') { } else if($status == 'available') {
Service::create_service($this); Service::create_service($this, $initiator);
return true; return true;
} }
$id = $this->get_id(); $id = $this->get_id();

@ -115,6 +115,15 @@ class ProductPage extends Page {
$itemlink = 'Service'; $itemlink = 'Service';
$start = $event->get_starttime(); $start = $event->get_starttime();
$end = $event->get_returntime(); $end = $event->get_returntime();
$initiator = $event->get_initiator();
$initiator_name = i18n('Unknown');
if($initiator) {
$initiator_name = replace(
array('id' => $initiator->get_id(),
'name' => $initiator->get_name(),
'page' => 'users'),
$this->fragments['item_link']);
}
$note = ''; $note = '';
if($event instanceof Loan) { if($event instanceof Loan) {
$user = $event->get_user(); $user = $event->get_user();
@ -135,6 +144,7 @@ class ProductPage extends Page {
'name' => $itemlink, 'name' => $itemlink,
'start_date' => format_date($start), 'start_date' => format_date($start),
'end_date' => format_date($end), 'end_date' => format_date($end),
'initiator' => $initiator_name,
'note' => $note), 'note' => $note),
$this->fragments['product_loan_table_row']); $this->fragments['product_loan_table_row']);
} }

@ -2,6 +2,7 @@
abstract class Responder { abstract class Responder {
protected $fragments = array(); protected $fragments = array();
protected $ldap = null; protected $ldap = null;
protected $logged_in_user = null;
public function __construct() { public function __construct() {
global $language, $required_entitlements; global $language, $required_entitlements;
@ -14,6 +15,8 @@ abstract class Responder {
} }
} }
$remote_user = explode('@', $_SERVER['REMOTE_USER'])[0];
$this->logged_in_user = $this->user_init($remote_user, null);
$this->fragments = get_fragments("./html/$language/fragments.html"); $this->fragments = get_fragments("./html/$language/fragments.html");
$this->ldap = new Ldap(); $this->ldap = new Ldap();
} }

@ -1,8 +1,8 @@
<?php <?php
class Service extends Event { class Service extends Event {
public static function create_service($product) { public static function create_service($product, $initiator) {
begin_trans(); begin_trans();
$event = parent::create_event($product, 'service'); $event = parent::create_event($product, 'service', $initiator);
$event_id = $event->get_id(); $event_id = $event->get_id();
$insert = prepare('insert into `service`(`event`) values (?)'); $insert = prepare('insert into `service`(`event`) values (?)');
bind($insert, 'i', $event_id); bind($insert, 'i', $event_id);

@ -73,6 +73,10 @@ $i18n = array(
"en" => function() { return "years"; }, "en" => function() { return "years"; },
"sv" => function() { return "år"; }, "sv" => function() { return "år"; },
), ),
"Unknown" => array(
"en" => function() { return "Unknown"; },
"sv" => function() { return "Okänd"; },
),
"{count} products" => array( "{count} products" => array(
"en" => function($count) { return "$count products"; }, "en" => function($count) { return "$count products"; },
"sv" => function($count) { return "$count artiklar"; }, "sv" => function($count) { return "$count artiklar"; },