Added support for automatically emailing users with overdue loans once per day via cron.
Also fixed a bug where an empty tag was added to products without any tags Fixed a bug where an empty tag was begin added
This commit is contained in:
parent
ce40d5ddfd
commit
8aa562de83
84
cron.php
Executable file
84
cron.php
Executable file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
require_once('./include/view.php');
|
||||
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
|
||||
$cron = new Cron(time());
|
||||
$cron->run();
|
||||
|
||||
class Cron {
|
||||
private $kvs;
|
||||
private $now = 0;
|
||||
public function __construct($now) {
|
||||
$this->now = $now;
|
||||
$this->kvs = new Kvs();
|
||||
}
|
||||
|
||||
public function run() {
|
||||
$lastrun = $this->kvs->get_value('lastrun');
|
||||
$interval = 3600*24; //1 day in seconds
|
||||
|
||||
if($lastrun && $this->now - $lastrun < $interval) {
|
||||
return;
|
||||
}
|
||||
$this->kvs->set_key('lastrun', $this->now);
|
||||
|
||||
$users = get_items('user');
|
||||
foreach($users as $user) {
|
||||
$this->check_loans($user);
|
||||
}
|
||||
}
|
||||
|
||||
private function check_loans($user) {
|
||||
$overdue = $user->get_overdue_loans();
|
||||
if($overdue) {
|
||||
$this->send_reminder($user, $overdue);
|
||||
}
|
||||
}
|
||||
|
||||
private function send_reminder($user, $loans) {
|
||||
$subject_template = "DMC Helpdesk: Du har ¤count¤ försenade lån";
|
||||
$reminder_template = "¤name¤, försenad sedan ¤due¤\n";
|
||||
$message_template = <<<EOF
|
||||
Hej ¤name¤
|
||||
|
||||
Vi vill påminna dig om att ditt lån av följande artiklar har gått ut:
|
||||
|
||||
¤list¤
|
||||
|
||||
Vänligen återlämna dem till Helpdesk så snart som möjligt, alternativt kontakta
|
||||
oss för att få lånet förlängt.
|
||||
|
||||
Mvh
|
||||
DMC Helpdesk
|
||||
helpdesk@dsv.su.se
|
||||
08 - 16 16 48
|
||||
EOF;
|
||||
|
||||
$overdue_count = count($loans);
|
||||
$reminder_list = '';
|
||||
foreach($loans as $loan) {
|
||||
$replacements = array('name' => $loan->get_product()->get_name(),
|
||||
'due' => $loan->get_duration()['end']);
|
||||
$reminder_list .= replace($replacements, $reminder_template);
|
||||
}
|
||||
|
||||
$subject = replace(array('count' => $overdue_count), $subject_template);
|
||||
$message = replace(array('name' => $user->get_displayname(),
|
||||
'list' => $reminder_list), $message_template);
|
||||
|
||||
try {
|
||||
mb_send_mail($user->get_email(),
|
||||
$subject,
|
||||
$message,
|
||||
'From: noreply-boka@dsv.su.se');
|
||||
} catch(Exception $e) {
|
||||
mb_send_mail('root@dsv.su.se',
|
||||
"Kunde inte skicka påminnelse",
|
||||
"Påminnelse kunde inte skickas till ".$user->get_name());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
@ -127,19 +127,15 @@ class Product {
|
||||
$ins_prod = prepare($stmt);
|
||||
bind($ins_prod, 'sssi', $name, $invoice, $serial, $now);
|
||||
execute($ins_prod);
|
||||
$prodid = $ins_prod->insert_id;
|
||||
$ins_info = prepare('insert into `product_info`(`product`, `field`, `data`) values (?, ?, ?)');
|
||||
foreach($info as $key => $value) {
|
||||
bind($ins_info, 'iss', $prodid, $key, $value);
|
||||
execute($ins_info);
|
||||
$product = new Product($serial, 'serial');
|
||||
foreach($info as $field => $value) {
|
||||
$product->set_info($field, $value);
|
||||
}
|
||||
$ins_tag = prepare('insert into `product_tag`(`product`, `tag`) values (?, ?)');
|
||||
foreach($tags as $tag) {
|
||||
bind($ins_tag, 'is', $prodid, $tag);
|
||||
execute($ins_tag);
|
||||
$product->add_tag($tag);
|
||||
}
|
||||
commit_trans();
|
||||
return new Product($prodid);
|
||||
return $product;
|
||||
} catch(Exception $e) {
|
||||
revert_trans();
|
||||
throw $e;
|
||||
@ -364,6 +360,9 @@ class Product {
|
||||
}
|
||||
|
||||
public function add_tag($tag) {
|
||||
if(!$tag) {
|
||||
return true;
|
||||
}
|
||||
$find = prepare('select * from `product_tag` where `product`=? and `tag`=?');
|
||||
bind($find, 'is', $this->id, $tag);
|
||||
execute($find);
|
||||
@ -498,6 +497,15 @@ class User {
|
||||
}
|
||||
}
|
||||
|
||||
public function get_email() {
|
||||
global $ldap;
|
||||
try {
|
||||
return $ldap->get_user_email($this->name);
|
||||
} catch(Exception $e) {
|
||||
return 'Mailadress saknas';
|
||||
}
|
||||
}
|
||||
|
||||
public function get_id() {
|
||||
return $this->id;
|
||||
}
|
||||
@ -552,6 +560,16 @@ class User {
|
||||
}
|
||||
return $loans;
|
||||
}
|
||||
|
||||
public function get_overdue_loans() {
|
||||
$overdue = array();
|
||||
foreach($this->get_loans('active') as $loan) {
|
||||
if($loan->is_overdue()) {
|
||||
$overdue[] = $loan;
|
||||
}
|
||||
}
|
||||
return $overdue;
|
||||
}
|
||||
|
||||
public function create_loan($product, $endtime) {
|
||||
$find = prepare('select * from `loan` where `product`=? and `returntime` is null');
|
||||
@ -812,7 +830,10 @@ class Kvs {
|
||||
}
|
||||
|
||||
public function get_value($key) {
|
||||
return $this->items[$key];
|
||||
if(isset($this->items[$key])) {
|
||||
return $this->items[$key];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function set_key($key, $value) {
|
||||
|
@ -23,6 +23,14 @@ class Ldap {
|
||||
return $data[0]['cn'][0];
|
||||
}
|
||||
|
||||
public function get_user_email($uid) {
|
||||
$data = $this->search("uid=$uid", 'mail', 'uid');
|
||||
if($data['count'] !== 1) {
|
||||
throw new Exception("LDAP search for '$uid' did not return exactly one result");
|
||||
}
|
||||
return $data[0]['mail'][0];
|
||||
}
|
||||
|
||||
public function search_user($uid) {
|
||||
$data = $this->search("uid=$uid", 'cn', 'uid');
|
||||
$out = array();
|
||||
|
@ -945,15 +945,15 @@ class Ajax extends Responder {
|
||||
if(!$serial) {
|
||||
return new Failure('Artikeln måste ha ett serienummer.');
|
||||
}
|
||||
try {
|
||||
$temp = new Product($serial, 'serial');
|
||||
return new Failure('Det angivna serienumret finns redan på en annan artikel.');
|
||||
} catch(Exception $e) {}
|
||||
if(!$invoice) {
|
||||
return new Failure('Artikeln måste ha ett fakturanummer.');
|
||||
}
|
||||
$product = null;
|
||||
if(!$id) {
|
||||
try {
|
||||
$temp = new Product($serial, 'serial');
|
||||
return new Failure('Det angivna serienumret finns redan på en annan artikel.');
|
||||
} catch(Exception $e) {}
|
||||
try {
|
||||
$product = Product::create_product($name,
|
||||
$invoice,
|
||||
@ -980,7 +980,7 @@ class Ajax extends Responder {
|
||||
try {
|
||||
$product->set_serial($serial);
|
||||
} catch(Exception $e) {
|
||||
return new Failure("Serienumret upptaget. Det här meddelandet ska aldrig visas.");
|
||||
return new Failure('Det angivna serienumret finns redan på en annan artikel.');
|
||||
}
|
||||
}
|
||||
if($invoice != $product->get_invoice()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user