diff --git a/html/en/fragments.html b/html/en/fragments.html index 517397a..fa03881 100644 --- a/html/en/fragments.html +++ b/html/en/fragments.html @@ -642,6 +642,9 @@ <th> End date </th> + <th> + Started by + </th> <th> Misc </th> @@ -668,6 +671,9 @@ <td> ¤end_date¤ </td> + <td> + ¤initiator¤ + </td> <td> ¤note¤ </td> @@ -688,6 +694,9 @@ <th> End date </th> + <th> + Started by + </th> <th> Misc </th> @@ -711,6 +720,9 @@ <td> ¤end_date¤ </td> + <td> + ¤initiator¤ + </td> <td> ¤note¤ </td> diff --git a/html/sv/fragments.html b/html/sv/fragments.html index edd1016..4a8b586 100644 --- a/html/sv/fragments.html +++ b/html/sv/fragments.html @@ -642,6 +642,9 @@ <th> Slutdatum </th> + <th> + Startat av + </th> <th> Övrigt </th> @@ -668,6 +671,9 @@ <td> ¤end_date¤ </td> + <td> + ¤initiator¤ + </td> <td> ¤note¤ </td> @@ -688,6 +694,9 @@ <th> Slutdatum </th> + <th> + Startat av + </th> <th> Övrigt </th> @@ -711,6 +720,9 @@ <td> ¤end_date¤ </td> + <td> + ¤initiator¤ + </td> <td> ¤note¤ </td> diff --git a/include/Ajax.php b/include/Ajax.php index 49adff1..6455536 100644 --- a/include/Ajax.php +++ b/include/Ajax.php @@ -92,7 +92,10 @@ class Ajax extends Responder { return new Failure(i18n('Invalid serial number.')); } 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)); } catch(Exception $e) { return new Failure(i18n('The product is already on loan.')); @@ -385,7 +388,7 @@ class Ajax extends Responder { private function toggle_service() { $product = new Product($_POST['id']); try { - $product->toggle_service(); + $product->toggle_service($this->logged_in_user); return new Success(i18n('Product service status updated.')); } catch(Exception $e) { return new Failure(i18n('Cannot register service.')); diff --git a/include/Event.php b/include/Event.php index 711bb5b..3586b03 100644 --- a/include/Event.php +++ b/include/Event.php @@ -5,7 +5,7 @@ class Event { protected $starttime = 0; protected $returntime = null; - protected static function create_event($product, $type) { + protected static function create_event($product, $type, $initiator) { $status = $product->get_status(); if($status != 'available') { $emsg = ''; @@ -37,9 +37,10 @@ class Event { } $now = time(); $insert = prepare('insert into `event` - (`product`, `type`, `starttime`) - values (?, ?, ?)'); - bind($insert, 'isi', $product->get_id(), $type, $now); + (`product`, `type`, `starttime`, `initiator`) + values (?, ?, ?, ?)'); + bind($insert, 'isis', + $product->get_id(), $type, $now, $initiator->get_id()); execute($insert); $event_id = $insert->insert_id; return new Event($event_id); @@ -66,6 +67,7 @@ class Event { $this->product = $result['product']; $this->starttime = $result['starttime']; $this->returntime = $result['returntime']; + $this->initiator = $result['initiator']; } public function get_id() { @@ -76,6 +78,14 @@ class Event { 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() { return $this->starttime; } diff --git a/include/Loan.php b/include/Loan.php index 26254ed..10eba37 100644 --- a/include/Loan.php +++ b/include/Loan.php @@ -3,9 +3,9 @@ class Loan extends Event { private $user = 0; private $endtime = 0; - public static function create_loan($user, $product, $endtime) { + public static function create_loan($user, $product, $endtime, $initiator) { begin_trans(); - $event = parent::create_event($product, 'loan'); + $event = parent::create_event($product, 'loan', $initiator); $event_id = $event->get_id(); $insert = prepare('insert into `loan`(`event`, `user`, `endtime`) values (?, ?, ?)'); diff --git a/include/Page.php b/include/Page.php index a23b4bc..d3aa795 100644 --- a/include/Page.php +++ b/include/Page.php @@ -224,6 +224,15 @@ abstract class Page extends Responder { 'page' => 'products'), $this->fragments['item_link']); $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 = ''; if($status !== 'inactive_loan') { $extend = format_date($loan->get_endtime()); @@ -241,6 +250,7 @@ abstract class Page extends Responder { 'serial' => $product->get_serial(), 'start_date' => format_date($start), 'end_date' => format_date($end), + 'initiator' => $initiator_name, 'note' => $note), $this->fragments['user_loan_table_row']); } diff --git a/include/Product.php b/include/Product.php index 4160156..0af2b8c 100644 --- a/include/Product.php +++ b/include/Product.php @@ -344,14 +344,14 @@ EOF; } } - public function toggle_service() { + public function toggle_service($initiator) { $status = $this->get_status(); $now = time(); $update = ''; if($status == 'service') { return $this->get_active_service()->end(); } else if($status == 'available') { - Service::create_service($this); + Service::create_service($this, $initiator); return true; } $id = $this->get_id(); diff --git a/include/ProductPage.php b/include/ProductPage.php index 9e06728..1838349 100644 --- a/include/ProductPage.php +++ b/include/ProductPage.php @@ -115,6 +115,15 @@ class ProductPage extends Page { $itemlink = 'Service'; $start = $event->get_starttime(); $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 = ''; if($event instanceof Loan) { $user = $event->get_user(); @@ -135,6 +144,7 @@ class ProductPage extends Page { 'name' => $itemlink, 'start_date' => format_date($start), 'end_date' => format_date($end), + 'initiator' => $initiator_name, 'note' => $note), $this->fragments['product_loan_table_row']); } diff --git a/include/Responder.php b/include/Responder.php index 2787279..157e6f8 100644 --- a/include/Responder.php +++ b/include/Responder.php @@ -2,6 +2,7 @@ abstract class Responder { protected $fragments = array(); protected $ldap = null; + protected $logged_in_user = null; public function __construct() { 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->ldap = new Ldap(); } diff --git a/include/Service.php b/include/Service.php index 526e241..4fe027d 100644 --- a/include/Service.php +++ b/include/Service.php @@ -1,8 +1,8 @@ <?php class Service extends Event { - public static function create_service($product) { + public static function create_service($product, $initiator) { begin_trans(); - $event = parent::create_event($product, 'service'); + $event = parent::create_event($product, 'service', $initiator); $event_id = $event->get_id(); $insert = prepare('insert into `service`(`event`) values (?)'); bind($insert, 'i', $event_id); diff --git a/include/translations.php b/include/translations.php index 12a84da..5f0f991 100644 --- a/include/translations.php +++ b/include/translations.php @@ -73,6 +73,10 @@ $i18n = array( "en" => function() { return "years"; }, "sv" => function() { return "år"; }, ), + "Unknown" => array( + "en" => function() { return "Unknown"; }, + "sv" => function() { return "Okänd"; }, + ), "{count} products" => array( "en" => function($count) { return "$count products"; }, "sv" => function($count) { return "$count artiklar"; },