Blackboard Building Blocks – Get Enrolments for User

Ok, quick post to continue from the last one – which will show how to retrieve the current user’s enrolments.

1. In the Default.aspx.cs page, add to ‘using’ statements at the top:

using Blackboard.Data.Course;
using Blackboard.Persist;

2. Now in the Page_Load method, change the existing code to:

Blackboard.Data.User.User currentUser = Blackboard.Platform.Session.GetCurrentSession().GetCurrentUser();
lblUserDetails.Text = currentUser.UserName + " is enrolled on ";
Course[] enrolments = CourseMembership.LoadEnabledCoursesByUserId(BbDatabaseContainer.GetCurrentContainer(), currentUser.Id);
foreach (Course item in enrolments)
{
lblUserDetails.Text += lblUserDetails.Text + item.CourseId + ", ";
}

 

Quick discussion of each line in turn – hopefully they’re relatively self-explanatory, but just to cover all bases:

Blackboard.Data.User.User currentUser = Blackboard.Platform.Session.GetCurrentSession().GetCurrentUser();

We’ve seen this line before, but instead of just outputting the information, we’re storing it in a Blackboard User object to use later.

lblUserDetails.Text = currentUser.UserName + " is enrolled on ";

Again, this line is just outputting the current user’s username, and then some nice text on the end to explain the other output.

Course[] enrolments = CourseMembership.LoadEnabledCoursesByUserId(BbDatabaseContainer.GetCurrentContainer(), currentUser.Id);

This line is new to us. Basically we’re creating an array of Blackboard Course objects and we populate this array with a call to CourseMembership.LoadEnabledCoursesByCourseId(). The LoadEnabledCourse…() method accepts the current database container and a user Id. (For this example, we’re using the current user Id. Maybe in a future one, we’ll look at using the user ID of another user, which could prove to be an interesting Admin Tool).

foreach (Course item in enrolments)
 {
 lblUserDetails.Text += lblUserDetails.Text + item.CourseId + ", ";
 }

These final 3 lines simply loop round the ‘enrolments’ course array, appending the courseId to the asp:Label.

Note: I’ve not tested this on our test system due to it being unavailable at time of writing. Hopefully it works – give me a shout if it doesn’t!

.NET Specific Note – We use the concept of make courses unavailable. Unfortunately the LoadEnabledCoursesByUserId() ignores both those that are disabled and unavailable – just in case your institution does the same kind of thing as us.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s