|
<?php defined('SYSPATH') or die('No direct script access.');
|
|
/*
|
|
* This file is part of open source system FreeNetIS
|
|
* and it is release under GPLv3 licence.
|
|
*
|
|
* More info about licence can be found:
|
|
* http://www.gnu.org/licenses/gpl-3.0.html
|
|
*
|
|
* More info about project can be found:
|
|
* http://www.freenetis.org/
|
|
*
|
|
*/
|
|
|
|
/**
|
|
*
|
|
* @author Michal Kliment
|
|
* @package Model
|
|
*
|
|
* @property integer $priority
|
|
* @property integer $approval_template_id
|
|
* @property Approval_template_Model $approval_template
|
|
* @property integer $approval_type_id
|
|
* @property Approval_type_Model $approval_type
|
|
*/
|
|
class Approval_template_item_Model extends ORM
|
|
{
|
|
protected $belongs_to = array('approval_template', 'approval_type');
|
|
|
|
/**
|
|
* Function to return all approval template items belongs to approval template
|
|
*
|
|
* @author Michal Kliment
|
|
* @param number $template_id
|
|
* @return Mysql_Result object
|
|
*/
|
|
public function get_all_items_by_template_id($template_id)
|
|
{
|
|
return $this->db->query("
|
|
SELECT i.id as item_id, i.priority, i.approval_template_id, t.id,
|
|
t.name, ag.id AS group_id, ag.name AS group_name, t.type,
|
|
t.interval, t.min_suggest_amount
|
|
FROM approval_template_items i
|
|
LEFT JOIN approval_types t ON t.id = i.approval_type_id
|
|
LEFT JOIN aro_groups ag ON t.aro_group_id = ag.id
|
|
WHERE i.approval_template_id = ?
|
|
ORDER BY i.priority
|
|
", array($template_id));
|
|
}
|
|
|
|
/**
|
|
* Function to return the lowest priority of approval template
|
|
*
|
|
* @author Michal Kliment
|
|
* @param number $template_id
|
|
* @return number
|
|
*/
|
|
public function get_lowest_priority_of_template($template_id)
|
|
{
|
|
$result = $this->db->query("
|
|
SELECT min(priority) AS min
|
|
FROM approval_template_items
|
|
GROUP BY approval_template_id
|
|
HAVING approval_template_id = ?
|
|
", array($template_id));
|
|
|
|
if (!$result || $result->count() != 1)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
return $result->current()->min;
|
|
}
|
|
|
|
/**
|
|
* Function to return the highest priority of approval template
|
|
*
|
|
* @author Michal Kliment
|
|
* @param number $template_id
|
|
* @return number
|
|
*/
|
|
public function get_highest_priority_of_template($template_id)
|
|
{
|
|
$result = $this->db->query("
|
|
SELECT max(priority) AS max
|
|
FROM approval_template_items
|
|
GROUP BY approval_template_id
|
|
HAVING approval_template_id = ?
|
|
", array($template_id));
|
|
|
|
if (!$result || $result->count() != 1)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
return $result->current()->max;
|
|
}
|
|
|
|
/**
|
|
* Function to check if user have rights to vote
|
|
*
|
|
* @author Michal Kliment
|
|
* @param number $template_id
|
|
* @param number $user_id
|
|
* @return bool
|
|
*/
|
|
public function check_user_vote_rights(
|
|
$template_id, $user_id, $suggest_amount = 0)
|
|
{
|
|
return (bool) $this->get_aro_group_by_approval_template_id_and_user_id(
|
|
$template_id, $user_id, $suggest_amount
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Returns all group assigned to approval
|
|
*
|
|
* @author Michal Kliment
|
|
* @param number $template_id
|
|
* @param number $suggest_amount
|
|
* @param number $user_id
|
|
* @return Mysql Result object
|
|
*/
|
|
public function get_aro_groups_by_approval_template_id(
|
|
$template_id, $suggest_amount = 0, $user_id = 0)
|
|
{
|
|
$cond = '';
|
|
|
|
if ($suggest_amount)
|
|
{
|
|
$cond = ' AND t.min_suggest_amount <= '.doubleval($suggest_amount);
|
|
}
|
|
|
|
return $this->db->query("
|
|
SELECT a.id, a.name FROM approval_template_items i
|
|
LEFT JOIN approval_types t ON i.approval_type_id = t.id
|
|
LEFT JOIN aro_groups a ON t.aro_group_id = a.id
|
|
WHERE i.approval_template_id = ? $cond
|
|
ORDER BY i.priority
|
|
", array($template_id));
|
|
}
|
|
|
|
/**
|
|
* Returns aro group assigned to user and approval
|
|
*
|
|
* @author Michal Kliment
|
|
* @param number $template_id
|
|
* @param number $user_id
|
|
* @param number $suggest_amount
|
|
* @return Mysql Result object
|
|
*/
|
|
public function get_aro_group_by_approval_template_id_and_user_id(
|
|
$template_id, $user_id, $suggest_amount = 0)
|
|
{
|
|
$cond = '';
|
|
|
|
if ($suggest_amount)
|
|
{
|
|
$cond = ' AND t.min_suggest_amount <= '.doubleval($suggest_amount);
|
|
}
|
|
|
|
$result = $this->db->query("
|
|
SELECT a.id, a.name FROM approval_template_items i
|
|
LEFT JOIN approval_types t ON i.approval_type_id = t.id
|
|
LEFT JOIN aro_groups a ON t.aro_group_id = a.id
|
|
LEFT JOIN groups_aro_map m ON a.id = m.group_id
|
|
WHERE i.approval_template_id = ? AND m.aro_id = ? $cond
|
|
ORDER BY i.priority
|
|
", array($template_id, $user_id));
|
|
|
|
// no record, returns null
|
|
if (!$result || $result->count() != 1)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
// returns first record
|
|
return $result->current();
|
|
}
|
|
|
|
/**
|
|
* Returns all aro ids assigned to approval
|
|
*
|
|
* @author Michal Kliment
|
|
* @param number $template_id
|
|
* @param number $suggest_amount
|
|
* @return Mysql Result object
|
|
*/
|
|
public function get_aro_ids_by_approval_template_id(
|
|
$template_id, $suggest_amount = 0)
|
|
{
|
|
$cond = '';
|
|
|
|
if ($suggest_amount)
|
|
{
|
|
$cond = ' AND t.min_suggest_amount <= '.doubleval($suggest_amount);
|
|
}
|
|
|
|
return $this->db->query("
|
|
SELECT g.aro_id AS id FROM approval_template_items i
|
|
LEFT JOIN approval_types t ON i.approval_type_id = t.id
|
|
LEFT JOIN groups_aro_map g ON t.aro_group_id = g.group_id
|
|
WHERE i.approval_template_id = ? $cond
|
|
GROUP BY g.aro_id
|
|
", array($template_id));
|
|
}
|
|
|
|
}
|