%PDF- %PDF-
| Direktori : /data/www_bck/varak.net_bck/mpr.varak.net/app/model/ |
| Current File : //data/www_bck/varak.net_bck/mpr.varak.net/app/model/Committee.php |
<?php
/**
* Created by PhpStorm.
* User: E581905
* Date: 29.4.14
* Time: 20:34
*/
class Committee extends Base
{
/** Gets list of committees
* @return mixed List of committees
*/
public function getCommittees()
{
return $this->db->query("SELECT [C].[id], [C].[title], [C].[date], [C].[type], [C].[qualification], [U].[name] [chairman] FROM [committees] [C] join [committee_members] [CM] on [CM].[committee]=[C].[id] join [users] [U] on [U].[id]=[CM].[user] where [CM].[isowner]=1");
}
/** Gets details for one committee
* @param int $id ID of committee
* @return mixed Record for selected committee
*/
public function getCommittee($id)
{
return $this->db->query("SELECT [C].[id], [C].[title], [C].[date], [C].[type], [C].[qualification], C.minimum, [U].[name] [chairman] FROM [committees] [C] join [committee_members] [CM] on [CM].[committee]=[C].[id] join [users] [U] on [U].[id]=[CM].[user] where [CM].[isowner]=1 AND [C].[id]=%i", $id)->fetch();
}
/** Gets members for committee
* @param int $id ID of committee
* @return mixed List of selected committee's members
*/
public function getCommitteeMembers($id)
{
return $this->db->query("SELECT [U].[id], [U].[name], [CM].[isowner]
from [committee_members] [CM]
join [users] [U] on [CM].[user]=[U].[id]
WHERE [CM].[committee] = %i
order by [CM].[isowner] desc, [U].[name]",$id);
}
/** Signups user to committee
* @param int $committeeId ID of committee
* @param int $userId ID of user
*/
public function signupUserToCommittee($committeeId, $userId)
{
$res = $this->db->query("select count(*) from [committee_signups] where [committee]=%i and [user]=%i", $committeeId, $userId)->fetchSingle();
if($res > 0) return;
$data = array(
'committee' => $committeeId,
'user' => $userId
);
$this->db->query("insert into [committee_signups]", $data);
$this->db->query("insert into [test_parts_score]([user], [score], [part]) select %i, 0, [id] from [test_parts] where [committee]=%i", $userId, $committeeId);
}
/** Gets list of users that signed up for selected committee
* @param $committeeId ID of committee
* @return mixed List of users signed up for selected committee
*/
public function getSignedUsers($committeeId)
{
return $this->db->query("select [U].[username], [U].[name], U.id AS user_id, [CS].[id], [CS].[paid], [CS].[score]
from [committee_signups] [CS]
join [users] [U] on [CS].[user] = [U].[id]
where [CS].[committee]=%i", $committeeId);
}
/** Sets if user paid or not
* @param int $signupId ID of user signup
* @param bool $paid Flag if user paid or not
* @return mixed ID of committee that user signed up for
*/
public function setPaid($signupId, $paid)
{
$this->db->query("update [committee_signups] set [paid]=%i where [id]=%i", $paid, $signupId);
return $this->db->query("select [committee] from [committee_signups] where [id]=%i", $signupId)->fetchSingle();
}
/** Creates new committee
* @param mixed $values Values to create new committee with
* @return int ID of newly created committee
*/
public function insertCommittee($values)
{
$chairman = $values->chairman;
$values->date .= ' '.$values->time;
unset($values->submit);
unset($values->chairman);
unset($values->time);
$this->db->query('INSERT INTO [committees] ',$values);
$id = $this->db->insertId();
$this->insertCommitteeMember(['committee' => $id, 'user' => $chairman, 'isowner' => 1]);
return $id;
}
/**
* @param mixed $values Values for new committee member
*/
public function insertCommitteeMember($values)
{
$this->db->query('INSERT INTO [committee_members] ',$values);
}
public function removeCommitteeMember($user,$committee)
{
$this->db->query('DELETE FROM [committee_members] WHERE user = %i AND committee = %i',$user,$committee);
}
public function getPotencialMembersForCommittee($id)
{
if(!$id) return $this->db->query('SELECT * FROM [users]');
else
{
$com = $this->getCommittee($id);
return $this->db->query('SELECT * FROM [users] WHERE ',$com->qualification,' = 1');
}
}
public function gradeUser($values)
{
$this->db->query('UPDATE [committee_signups] SET score = %i WHERE committee = %i AND user = %i',$values->score,$values->committee,$values->user);
}
public function isUserSignedUp($committeeId, $userId)
{
return $this->db->query("select count(*) from [committee_signups] where [committee]=%i and [user]=%i", $committeeId, $userId)->fetchSingle();
}
public function getUsersResults($committee,$user)
{
return $this->db->query('SELECT [C].[minimum], [C].[id], [CS].[score], [C].[title], [C].[date], [U].[name]
FROM [committees] C
LEFT JOIN [committee_signups] CS ON C.id = CS.committee JOIN [users] [U] on [U].[id]=[CS].[user]
WHERE C.id = %i AND CS.user = %i AND CS.committee = %i',$committee,$user,$committee);
}
public function getTestParts($committeeId)
{
return $this->db->query("select [id], [title], [minimum], [type] from [test_parts] where [committee]=%i", $committeeId);
}
public function insertTestPart($values)
{
$this->db->query("insert into [test_parts]", $values);
}
public function getTestPartsScore($committeeId, $userId)
{
return $this->db->query("select [TP].[title], [TP].[minimum], [TPS].[id], [TPS].[score] from [test_parts] [TP] join [test_parts_score] [TPS] on [TP].[id] = [TPS].[part] where [TPS].[user]=%i and [TP].[committee]=%i", $userId, $committeeId);
}
public function gradePart($values)
{
$this->db->query("update [test_parts_score] set [score]=%i where [id]=%i", $values->score, $values->partid);
// Update entire score
$res = $this->db->query("select [TP].[minimum], [TPS].[score] from [test_parts] [TP] join [test_parts_score] [TPS] on [TP].[id] = [TPS].[part] where [TPS].[user]=%i and [TP].[committee]=%i", $values->user_id, $values->committee_id);
$total = 0;
$cnt = 0;
foreach($res as $r)
{
if($r->score >= $r->minimum)
{
$total += $r->score;
$cnt++;
}
else
{
$this->db->query("update [committee_signups] set [score]=0 where [user]=%i and [committee]=%i", $values->user_id, $values->committee_id);
return;
}
}
$this->db->query("update [committee_signups] set [score]=%i where [user]=%i and [committee]=%i", ceil($total / $cnt), $values->user_id, $values->committee_id);
}
public function getAppealData($signupId)
{
return $this->db->query("select [C].[date], [C].[minimum], [CS].[score], [U].[name], [U].[address] from [committee_signups] [CS] join [committees] [C] on [CS].[committee]=[C].[id] join [users] [U] on [U].[id]=[CS].[user] where [C].[id]=%i", $signupId)->fetch();
}
}