Refactor currentPhase code to account for changes in the API (addition of "type"...
[project/lingotek.git] / lib / Drupal / lingotek / LingotekPhase.php
1 <?php
2
3 /**
4 * @file
5 * Defines LingotekPhase.
6 */
7
8 /**
9 * A class representing a Lingotek Workflow Phase.
10 */
11 class LingotekPhase {
12 /**
13 * The phase data
14 *
15 * @var int
16 */
17 protected $phase;
18
19 /**
20 * Constructor.
21 *
22 * @param object $phase
23 * Phase data as returned by a getPhase API call.
24 */
25 public function __construct($phase) {
26 $this->phase = $phase;
27 }
28
29 /**
30 * Injects reference to an API object.
31 *
32 * @param LingotekApi $api
33 * An instantiated Lingotek API object.
34 */
35 public function setApi(LingotekApi $api) {
36 $this->api = $api;
37 }
38
39 /**
40 * Factory method for getting a loaded LingotekPhase object.
41 *
42 * @param int $phase_id
43 * A phase ID.
44 *
45 * @return LingotekPhase
46 * A loaded LingotekPhase object.
47 */
48 public static function load($phase_id) {
49 $api = LingotekApi::instance();
50 $api_phase = $api->get_phase($phase_id);
51 $phase = new LingotekPhase($api_phase);
52 $phase->setApi($api);
53
54 return $phase;
55 }
56
57 /**
58 * Factory method for getting a loaded LingotekPhase object.
59 *
60 * @param object $api_phase
61 * Phase data as returned by a getPhase Lingotek API call.
62 *
63 * @return LingotekPhase
64 * A loaded LingotekPhase object.
65 */
66 public static function loadWithData($api_phase) {
67 $api = LingotekApi::instance();
68 $phase = new LingotekPhase($api_phase);
69 $phase->setApi($api);
70
71 return $phase;
72 }
73
74
75 /**
76 * Determines whether or not the current phase is eligible to be marked as complete.
77 *
78 * @return bool
79 * TRUE if the phase can be marked as complete. FALSE otherwise.
80 */
81 public function canBeMarkedComplete() {
82 $result = FALSE;
83
84 // These phase types need to be at 100% complete in order to
85 // be eligible for mark as complete.
86 $needs_100_complete_phase_types = array(
87 'TRANSLATION',
88 'REVIEW',
89 );
90
91 if (in_array($this->phase->type, $needs_100_complete_phase_types)) {
92 if ($this->phase->percentComplete == 100 && !$this->phase->isMarkedComplete) {
93 $result = TRUE;
94 }
95 }
96 elseif (!$this->phase->isMarkedComplete) {
97 // All other phase types should be able to be marked as complete regardless
98 // of completion percentage.
99 $result = TRUE;
100 }
101
102 return $result;
103 }
104
105 /**
106 * Magic get for phase property access.
107 */
108 public function __get($property) {
109 $value = NULL;
110
111 if (!empty($this->phase->$property)) {
112 $value = $this->phase->$property;
113 }
114
115 return $value;
116 }
117 }