forked from AppStateESS/InternshipInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTermFactory.php
111 lines (84 loc) · 3.34 KB
/
TermFactory.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
103
104
105
106
107
108
109
110
111
<?php
namespace Intern;
class TermFactory
{
/**
* Get a term object using its term code.
*
* @param string $termCode
* @return Mixed<Term|bool> Returns the requested Term object, or fasle if it doesn't exist in the intern_term table.
*/
public static function getTermByTermCode(string $termCode) {
$db = PdoFactory::getPdoInstance();
$stmt = $db->prepare('SELECT * from intern_term where term = :termCode');
$stmt->execute(array('termCode' => $termCode));
$stmt->setFetchMode(\PDO::FETCH_CLASS, '\Intern\TermRestored');
return $stmt->fetch();
}
/**
* Get an associative array of every term
* in the database. Looks like: { 201840 => 'Fall 2018' }
* @return Array Associative array of term codes and their descriptions
*/
public static function getTermsAssoc(): array
{
$db = PdoFactory::getPdoInstance();
$stmt = $db->prepare('SELECT * from intern_term ORDER BY term DESC');
$stmt->execute();
$stmt->setFetchMode(\PDO::FETCH_CLASS, '\Intern\TermRestored');
$results = $stmt->fetchAll();
$terms = array();
foreach ($results as $term) {
$terms[$term->getTermCode()] = $term->getDescription();
}
return $terms;
}
public static function getNextTerm(Term $term) {
$db = PdoFactory::getPdoInstance();
$stmt = $db->prepare('SELECT * FROM intern_term WHERE start_timestamp > :currentTermStart ORDER BY start_timestamp ASC LIMIT 1');
$stmt->execute(array('currentTermStart' => $term->getStartTimestamp()));
$stmt->setFetchMode(\PDO::FETCH_CLASS, '\Intern\TermRestored');
$result = $stmt->fetch();
if($result === false){
return null;
}
return $result;
}
/**
* Get an associative array of terms > current term
* in the database. Looks like: { raw_term => readable_string }
*/
public static function getFutureTermsAssoc(): array
{
$db = PdoFactory::getPdoInstance();
$stmt = $db->prepare('SELECT * from intern_term where census_date_timestamp > :currentTimestamp ORDER BY term asc');
$stmt->execute(array('currentTimestamp'=>time()));
$stmt->setFetchMode(\PDO::FETCH_CLASS, '\Intern\TermRestored');
$terms = $stmt->fetchAll();
return $terms;
}
public static function getAvailableTerms(): array
{
$db = PdoFactory::getPdoInstance();
$stmt = $db->prepare('SELECT * from intern_term
where
extract(epoch from now())::int >= available_on_timestamp AND
extract(epoch from now())::int < census_date_timestamp');
$stmt->execute();
$stmt->setFetchMode(\PDO::FETCH_CLASS, '\Intern\TermRestored');
$results = $stmt->fetchAll();
return $results;
}
/**
* Determine if a term exists in the database.
* Useful for deciding if a future term is "ready" yet
*
* @param $targetTerm Term to decide if exists or not
* @return bool True if the given term eixsts in the database, false otherwise
*/
public static function termExists(string $targetTermCode): bool
{
$terms = self::getTermsAssoc();
return in_array($targetTerm, array_keys($terms));
}
}