-
Notifications
You must be signed in to change notification settings - Fork 3
/
ModuleWorkflow.cs
127 lines (111 loc) · 6.26 KB
/
ModuleWorkflow.cs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Extensions.Configuration;
using Microsoft.Graph.Beta.Models;
using MicrosoftEduGraphSamples.Utilities;
using MicrosoftGraphSDK;
namespace MicrosoftEduGraphSamples.Workflows
{
/// <summary>
/// A code sample related to modules include creating a draft, publishing, setting up resources, adding link resources,
/// Word document resources, channel resources, assignment resources, patching, and deleting as a teacher.
/// </summary>
internal class ModuleWorkflow
{
private readonly IConfiguration _config;
public ModuleWorkflow(IConfiguration configuration)
{
this._config = configuration;
GlobalMethods.ValidateConfiguration(_config);
}
/// <summary>
/// A code sample to create and publish the module
/// </summary>
/// <param name="appOnly">True value authenticates the graph client with application permissions only, otherwise it will be created with delegated permissions.</param>
public async Task ClassworkAsync(bool appOnly = false)
{
try
{
// Get a Graph client based on the appOnly parameter
var graphClient = appOnly ? GraphClient.GetApplicationClient(_config["tenantId"], _config["appId"], _config["secret"]) : GraphClient.GetDelegateClient(_config["tenantId"], _config["appId"], _config["teacherAccount"], _config["password"]);
// Create a draft module
var module = await Module.CreateSampleAsync(graphClient, _config["classId"], "Sample Module " + DateTime.Now.ToString("dd/MM/yyyy HHmm"), "This Classwork module was created with Microsoft Graph SDK.");
Console.WriteLine($"New module has been created: {module.Id} - {module.DisplayName} - {module.Status}");
// Set up a resources folder
await Module.SetupResourcesFolderAsync(graphClient, _config["classId"], module.Id);
// Add a link resource
EducationModuleResource requestBody = new EducationModuleResource
{
Resource = new EducationLinkResource
{
OdataType = "#microsoft.graph.educationLinkResource",
DisplayName = "Bing site",
Link = "https://www.bing.com",
},
};
var newResource = await Module.PostResourceAsync(graphClient, _config["classId"], module.Id.ToString(), requestBody);
// Add a Word document resource
requestBody = new EducationModuleResource
{
Resource = new EducationWordResource
{
OdataType = "#microsoft.graph.educationWordResource",
DisplayName = "test_word_file.docx",
},
};
newResource = await Module.PostResourceAsync(graphClient, _config["classId"], module.Id.ToString(), requestBody);
// Add a channel resource
var channels = await MicrosoftGraphSDK.Team.GetChannelsAsync(graphClient, _config["classId"]);
requestBody = new EducationModuleResource
{
Resource = new EducationChannelResource
{
OdataType = "#microsoft.graph.educationChannelResource",
Url = "https://graph.microsoft.com/v1.0/teams/" + _config["classId"] + "/channels/" + channels.Value[0].Id,
DisplayName = "General",
},
};
newResource = await Module.PostResourceAsync(graphClient, _config["classId"], module.Id.ToString(), requestBody);
// Add a Assignment resource
var assignment = await Assignment.CreateSampleAssignmentAsync(graphClient, _config["classId"]);
requestBody = new EducationModuleResource
{
Resource = new EducationLinkedAssignmentResource
{
OdataType = "#microsoft.graph.educationLinkedAssignmentResource",
Url = "https://graph.microsoft.com/v1.0/education/classes/" + _config["classId"] + "/assignments/" + assignment.Id,
},
};
newResource = await Module.PostResourceAsync(graphClient, _config["classId"], module.Id.ToString(), requestBody);
// Patch the module
var updateBody = new EducationModule
{
DisplayName = module.DisplayName + " Updated",
Description = module.Description + " updated",
};
module = await Module.PatchAsync(graphClient, _config["classId"], module.Id.ToString(), updateBody);
Console.WriteLine($"Module has been Patched: {module.DisplayName}");
// Publish the module
module = await Module.PublishAsync(graphClient, _config["classId"], module.Id.ToString());
// Switch to student account
graphClient = GraphClient.GetDelegateClient(_config["tenantId"], _config["appId"], _config["studentAccount"], _config["password"]);
// As Student, get module resources
var resources = await Module.GetModuleResourcesAsync(graphClient, _config["classId"], module.Id.ToString());
foreach ( var resource in resources.Value )
{
Console.WriteLine($"Resource: {resource.Resource.DisplayName}");
}
// Switch to Teacher account
graphClient = GraphClient.GetDelegateClient(_config["tenantId"], _config["appId"], _config["teacherAccount"], _config["password"]);
// As Teacher, delete the module
await Module.DeleteAsync(graphClient, _config["classId"], module.Id.ToString());
Console.WriteLine($"Module has been Deleted: {module.DisplayName}");
}
catch (Exception ex)
{
Console.WriteLine($"ClassworkAsync: {ex.ToString()}");
return;
}
}
}
}