Test Failed
Push — master ( dddd22...de0d5b )
by
unknown
21:40
created

FreeBusyModule::getFreeBusyInfo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 29
rs 9.6333
1
<?php
2
3
/**
4
 * FreeBusyModule Module.
5
 */
6
class FreeBusyModule extends Module {
7
	public function __construct($id, $data) {
8
		parent::__construct($id, $data);
9
	}
10
11
	public function execute() {
12
		foreach ($this->data as $actionType => $selUser) {
13
			if (isset($actionType)) {
14
				try {
15
					switch ($actionType) {
16
						case "list":
17
							$this->addUserData($selUser);
18
							break;
19
20
						default:
21
							$this->handleUnknownActionType($actionType);
22
					}
23
				}
24
				catch (MAPIException $e) {
25
					$this->processException($e, $actionType);
26
				}
27
			}
28
		}
29
	}
30
31
	/**
32
	 * This function will get user info from address book and add freebusy data of the user to
33
	 * response.
34
	 *
35
	 * @param {Array} $selUser User that should be resolved
0 ignored issues
show
Documentation Bug introduced by
The doc comment {Array} at position 0 could not be parsed: Unknown type name '{' at position 0 in {Array}.
Loading history...
36
	 */
37
	public function addUserData($selUser) {
38
		$data = [];
39
		$data["users"] = [];
40
41
		foreach ($selUser["users"] as $fbUser) {
42
			$user = [];
43
44
			// Copy the identifier of the user.
45
			$user["userid"] = $fbUser["userid"];
46
			$user["entryid"] = $fbUser["entryid"];
47
48
			// Obtain the Freebusy data for this user
49
			$busyArray = $this->getFreeBusyInfo($fbUser["entryid"], $selUser["start"], $selUser["end"]);
50
51
			if ($busyArray) {
52
				// We have freebusy information, go over the data
53
				// and insert the blocks into the user object.
54
				foreach ($busyArray as $busyItem) {
55
					$busy = [];
56
					$busy["status"] = $busyItem["status"];
57
					$busy["start"] = $busyItem["start"];
58
					$busy["isRestrictedRange"] = isset($busyItem["isRestrictedRange"]) ? $busyItem["isRestrictedRange"] : false;
59
					$busy["end"] = $busyItem["end"];
60
					$user["items"][] = $busy;
61
				}
62
			}
63
			else {
64
				// No freebusy data available, create a single empty block
65
				$busy["status"] = -1;
66
				$busy["start"] = $selUser["start"];
67
				$busy["end"] = $selUser["end"];
68
				$user["items"][] = $busy;
69
			}
70
71
			$data["users"][] = $user;
72
		}
73
74
		$this->addActionData("list", $data);
75
		$GLOBALS["bus"]->addData($this->getResponseData());
76
	}
77
78
	/**
79
	 * This function will get freebusy data for user based on the timeframe passed in arguments.
80
	 *
81
	 * @param {String} $entryID Entryid of the user for which we need to get freebusy data
0 ignored issues
show
Documentation Bug introduced by
The doc comment {String} at position 0 could not be parsed: Unknown type name '{' at position 0 in {String}.
Loading history...
82
	 * @param {Number} $start start offset for freebusy publish range
83
	 * @param {Number} $end end offset for freebusy publish range
84
	 *
85
	 * @return {Array} freebusy blocks for passed publish range
0 ignored issues
show
Documentation Bug introduced by
The doc comment {Array} at position 0 could not be parsed: Unknown type name '{' at position 0 in {Array}.
Loading history...
86
	 */
87
	public function getFreeBusyInfo($entryID, $start, $end) {
88
		$result = [];
89
90
		$fbdata = mapi_getuserfreebusy($GLOBALS['mapisession']->getSession(), hex2bin($entryID), $start, $end);
91
92
		if (empty($fbdata['fbevents'])) {
93
			return $result;
94
		}
95
		$last_end = $start;
96
		foreach ($fbdata['fbevents'] as $event) {
97
			$blockItem = [];
98
			$blockItem['start'] = $event['start'];
99
			$blockItem['end'] = $event['end'];
100
			$blockItem['status'] = $event['busystatus'];
101
			$last_end = $event['end'];
102
			$result[] = $blockItem;
103
		}
104
105
		// Add restricted Free/Busy range information for recipient.
106
		$noInfoItem = [];
107
		$noInfoItem["status"] = -1;
108
		$noInfoItem["isRestrictedRange"] = true;
109
		// Last day of visible Free/Busy range was first day of restricted Free/Busy range.
110
		$noInfoItem["start"] = $last_end;
111
		// Scheduler's last day.
112
		$noInfoItem["end"] = $end;
113
		$result[] = $noInfoItem;
114
115
		return $result;
116
	}
117
}
118