forked from AppStateESS/InternshipInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDepartmentFactory.php
102 lines (85 loc) · 3.15 KB
/
DepartmentFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace Intern;
use Intern\Department;
/**
* Factory for loading Department objects.
* @author jbooker
* @package intern
*/
class DepartmentFactory {
/**
* Returns a Department object based on the given department ID.
* @param unknown $id
* @return Department
*/
public static function getDepartmentById($id)
{
// Sanity checking
if (!isset($id) || $id === '') {
throw new \InvalidArgumentException('Missing department ID.');
}
// Query
$query = "SELECT * FROM intern_department WHERE id = $id";
$result = \PHPWS_DB::getRow($query);
if (\PHPWS_Error::isError($result)) {
throw new DatabaseException($result->toString());
}
if (sizeof($result) == 0) {
return null;
}
// Create the object and set member variables
$department = new DepartmentDB();
$department->setId($result['id']);
$department->setName($result['name']);
$department->setHidden($result['hidden']);
$department->setCorequisite($result['corequisite']);
return $department;
}
/**
* Return an associative array {id => dept. name} for all the
* departments in database.
* @param $except - Always show the department with this ID. Used for internships
* with a hidden department. We still want to see it in the select box.
*/
public static function getDepartmentsAssoc($except=null)
{
$db = new \PHPWS_DB('intern_department');
$db->addOrder('name');
$db->addColumn('id');
$db->addColumn('name');
$db->addWhere('hidden', 0, '=', 'OR');
if(!is_null($except)) {
$db->addWhere('id', $except, '=', 'OR');
}
$db->setIndexBy('id');
return $db->select('col');
}
/**
* Return an associative array {id => dept. name} for all the departments
* that the user with $username is allowed to see.
* @param $includeHiddenDept - Include the department with this ID, even if it's hidden. Used for internships
* with a hidden department. We still want to see it in the select box.
*/
public static function getDepartmentsAssocForUsername($username, $includeHiddenDept = null)
{
$db = new \PHPWS_DB('intern_department');
$db->addOrder('name');
$db->addColumn('id');
$db->addColumn('name');
$db->addWhere('hidden', 0, '=', 'OR', 'grp');
if(!is_null($includeHiddenDept)){
$db->addWhere('id', $includeHiddenDept, '=', 'OR', 'grp');
}
// If the user doesn't have the 'all_departments' permission,
// then add a join to limit to specific departments
if(!\Current_User::allow('intern', 'all_departments') && !\Current_User::isDeity()){
$db->addJoin('LEFT', 'intern_department', 'intern_admin', 'id', 'department_id');
$db->addWhere('intern_admin.username', $username);
}
$db->setIndexBy('id');
$depts = array();
$depts[-1] = 'Select Department';
$depts += $db->select('col');
return $depts;
}
}