Skip to content

Commit 0090b26

Browse files
committed
Planner API examples
1 parent 5dd7016 commit 0090b26

File tree

9 files changed

+191
-78
lines changed

9 files changed

+191
-78
lines changed

examples/Planner/GetAssignedTasksReport.php

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
*
5+
* Description:
6+
* - List all groups containing Planner plans
7+
* - Identify plans accessible to the specific group
8+
* - Extract all tasks from qualifying plans
9+
*
10+
* Permissions:
11+
* Accessing group plans requires both Group.Read.All and Tasks.Read.All permissions.
12+
*/
13+
14+
15+
use Office365\GraphServiceClient;
16+
use Office365\Planner\Plans\PlannerPlan;
17+
use Office365\Planner\Tasks\PlannerTask;
18+
19+
require_once '../vendor/autoload.php';
20+
21+
$settings = include('../../tests/Settings.php');
22+
$client = GraphServiceClient::withClientSecret($settings['TenantName'], $settings['ClientId'], $settings['ClientSecret']);
23+
24+
25+
// 1. Get the specific group
26+
$groups = $client->getGroups()
27+
->filter("displayName eq 'PlanGroup'")
28+
->get()
29+
->executeQuery();
30+
31+
// 2. Get all plans in this group
32+
$plans = $groups[0]->getPlanner()
33+
->getPlans()
34+
->get()
35+
->executeQuery();
36+
37+
$allPlansWithTasks = [];
38+
39+
/** @var PlannerPlan $plan */
40+
foreach ($plans as $plan) {
41+
try {
42+
// 3. Get all tasks for each plan
43+
$tasks = $plan->getTasks()
44+
->get()
45+
->executeQuery();
46+
47+
$planData = [
48+
'plan_id' => $plan->getId(),
49+
'plan_title' => $plan->getTitle(),
50+
//'plan_created' => $plan->getOwner()->getCreatedDateTime(),
51+
'tasks' => []
52+
];
53+
54+
/** @var PlannerTask $task */
55+
foreach ($tasks as $task) {
56+
$planData['tasks'][] = [
57+
'task_id' => $task->getId(),
58+
'task_title' => $task->getTitle(),
59+
'assignments' => $task->getAssignments(),
60+
'created_date' => $task->getCreatedDateTime(),
61+
'completed_date' => $task->getCompletedDateTime(),
62+
];
63+
}
64+
65+
$allPlansWithTasks[] = $planData;
66+
67+
} catch (\Exception $e) {
68+
echo "Error processing plan {$plan->getTitle()}: {$e->getMessage()}\n";
69+
continue;
70+
}
71+
}
72+
73+
// Output the results
74+
echo json_encode($allPlansWithTasks, JSON_PRETTY_PRINT);

src/Planner/PlannerUserIds.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,33 @@
88
use Office365\Runtime\ClientValue;
99
class PlannerUserIds extends ClientValue
1010
{
11+
12+
/**
13+
* Checks if the object is empty (has no GUIDs set)
14+
* @return bool
15+
*/
16+
public function isEmpty(): bool
17+
{
18+
foreach ($this as $value) {
19+
if ($value === true) {
20+
return false;
21+
}
22+
}
23+
return true;
24+
}
25+
26+
/**
27+
* Returns array of all User Ids
28+
* @return array
29+
*/
30+
public function getIds(): array
31+
{
32+
$guids = [];
33+
foreach ($this as $guid => $value) {
34+
if ($value === true) {
35+
$guids[] = $guid;
36+
}
37+
}
38+
return $guids;
39+
}
1140
}

src/Planner/Plans/PlannerPlanDetails.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Office365\Planner\Plans;
77

88
use Office365\Entity;
9-
use Office365\Planner\PlannerCategoryDescriptions;
109
use Office365\Planner\PlannerUserIds;
1110

1211
/**

src/Planner/Tasks/PlannerTask.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616
*/
1717
class PlannerTask extends Entity
1818
{
19+
20+
/**
21+
* @return string
22+
*/
23+
public function getCreatedDateTime()
24+
{
25+
return $this->getProperty("CreatedDateTime");
26+
}
27+
28+
/**
29+
* @return string
30+
*/
31+
public function getCompletedDateTime()
32+
{
33+
return $this->getProperty("CompletedDateTime");
34+
}
35+
1936
/**
2037
* Identity of the user that created the task.
2138
* @return IdentitySet

src/Planner/Tasks/PlannerTaskCollection.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Office365\Entity;
66
use Office365\EntityCollection;
77
use Office365\Planner\Plans\PlannerPlan;
8+
use Office365\Runtime\Actions\CreateEntityQuery;
89
use Office365\Runtime\ClientRuntimeContext;
910
use Office365\Runtime\ResourcePath;
1011

@@ -36,23 +37,28 @@ public function create($title, $planId=null, $bucketId=null, $assignments=[]){
3637
throw new \Exception("planId is mandatory when creating a task without a parent");
3738
}
3839

39-
$returnType = $this->getContext()->getPlanner()->getTasks()->add();
40+
$returnType = new PlannerTask($this->getContext());
41+
$this->getContext()->getPlanner()->getTasks()->addChild($returnType);
42+
4043
$returnType->setTitle($title);
44+
$returnType->setProperty("bucketId", $bucketId);
45+
if (!empty($assignments)) {
46+
$returnType->setProperty("assignments", $assignments);
47+
}
48+
49+
$qry = new CreateEntityQuery($returnType);
4150

4251
if ($this->parent instanceof PlannerPlan) {
43-
$this->parent->ensureProperty("Id", function () use ($returnType) {
52+
$this->parent->ensureProperty("Id", function () use ($qry, $returnType) {
4453
$returnType->setProperty("planId", $this->parent->getId());
54+
$this->getContext()->addQueryAndResultObject($qry,$returnType);
4555
});
4656
}
4757
else {
48-
$returnType->setProperty("planId", null);
58+
$returnType->setProperty("planId", $planId);
59+
$this->getContext()->addQueryAndResultObject($qry,$returnType);
4960
}
5061

51-
52-
$returnType->setProperty("bucketId", null);
53-
if (!empty($assignments)) {
54-
$returnType->setProperty("assignments", $assignments);
55-
}
5662
return $returnType;
5763
}
5864

tests/planner/PlannerPlansTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Office365;
4+
5+
6+
use Office365\Directory\Groups\Group;
7+
use Office365\GraphTestCase;
8+
use Office365\Planner\Plans\PlannerPlan;
9+
use Office365\Runtime\Http\RequestException;
10+
11+
class PlannerPlansTest extends GraphTestCase
12+
{
13+
14+
15+
16+
public function testCreateUserPlan ()
17+
{
18+
$title = "TestUserPlan_" . rand(1, 100000);
19+
$user = self::$graphClient->getUsers()->getByUserPrincipalName(self::$settings['UserName']);
20+
$result = $user->getPlanner()->getPlans()->create($title)->executeQuery();
21+
self::assertNotNull($result->getResourcePath());
22+
return $result;
23+
}
24+
25+
public function testListUserPlans()
26+
{
27+
$user = self::$graphClient->getUsers()->getByUserPrincipalName(self::$settings['UserName']);
28+
$result = $user->getPlanner()->getPlans()->get()->executeQuery();
29+
self::assertNotNull($result->getResourcePath());
30+
}
31+
32+
/**
33+
* @depends testCreateUserPlan
34+
* @param PlannerPlan $plan
35+
*/
36+
public function testGetUserPlan(PlannerPlan $plan)
37+
{
38+
$result = $plan->getDetails()->get()->executeQuery();
39+
self::assertNotNull($result->getResourcePath());
40+
}
41+
42+
/**
43+
* @depends testCreateUserPlan
44+
* @param PlannerPlan $plan
45+
*/
46+
public function testDeleteUserPlan(PlannerPlan $plan)
47+
{
48+
$plan->deleteObject()->executeQuery();
49+
50+
$this->expectException(RequestException::class);
51+
$this->expectExceptionCode(404);
52+
$plan->get()->executeQuery();
53+
}
54+
55+
}

tests/planner/PlannerTasksTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public static function setUpBeforeClass(): void
2020
{
2121
$title = "TestPlan_" . rand(1, 100000);
2222
parent::setUpBeforeClass();
23-
$currentUser = self::$graphClient->getUsers()->getByUserPrincipalName(self::$settings['UserName']);
24-
self::$targetPlan = $currentUser->getPlanner()->getPlans()->create($title)->executeQuery();
23+
$user = self::$graphClient->getUsers()->getByUserPrincipalName(self::$settings['UserName']);
24+
self::$targetPlan = $user->getPlanner()->getPlans()->create($title)->executeQuery();
2525
}
2626

2727
public static function tearDownAfterClass(): void

tests/planner/PlannerTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ public static function tearDownAfterClass(): void
3232
parent::tearDownAfterClass();
3333
}
3434

35-
public function testGetMyPlans()
36-
{
37-
$result = self::$graphClient->getMe()->getPlanner()->getPlans()->get()->executeQuery();
38-
self::assertNotNull($result->getResourcePath());
39-
}
40-
4135
public function testCreateGroupPlan()
4236
{
4337
$planTitle = "TestPlan_" . rand(1, 100000);

0 commit comments

Comments
 (0)