예제 #1
0
        static String[,] getCourseList(String courseuser)
        {
            // Create a two dimensional array to house the course and membership data
            String[,] courseList = new String[10000, 4];

            // Add headers as the first element of the array
            courseList[0, 0] = "COURSE ID";
            courseList[0, 1] = "COURSE NAME";
            courseList[0, 2] = "MEMBERSHIP ID";
            courseList[0, 3] = "COURSE ROLE";

            if (DEBUG)
            {
                Console.WriteLine(courseList[0, 0] + "," + courseList[0, 1] + "," + courseList[0, 2] + "," + courseList[0, 3]);
            }

            // Get the list of courseIds associated with the user requested.
            CourseIdVO [] courses = ctx.getMemberships(courseuser);

            if (courses == null)
            {
                return(courseList);
            }

            // Initialize a user filter, set to get user by username and availability.
            UserFilter uf = new UserFilter();

            uf.filterType          = 6;
            uf.filterTypeSpecified = true;
            uf.name = new string[] { courseuser };

            // Get the user object
            UserVO [] user = usr.getUser(uf);

            if (user == null)
            {
                return(courseList);
            }

            int i = 1;

            // Iterate through the course list returned from contextWS.getMemberships()
            foreach (CourseIdVO courseIdVO in courses)
            {
                try
                {
                    // Grab the course Id
                    String courseId = courseIdVO.externalId;

                    // Initialize a new Course Filter and Membership Filter
                    CourseFilter     cf = new CourseFilter();
                    MembershipFilter mf = new MembershipFilter();

                    // Set course filter to search for courses by user Id. Yes, this is redundant, however
                    // ContextWS.getMemberships returns both courses and organizations. This only retrieves courses
                    cf.filterType          = 3;
                    cf.filterTypeSpecified = true;
                    cf.ids = new string[] { courseId };

                    // Set the membership filter to get memberships by course Id and UserId.
                    // Getting by user Id only (filter type 5) always fails. The course Id will be added
                    // during the foreach loop below.
                    mf.filterType          = 6;
                    mf.filterTypeSpecified = true;
                    mf.userIds             = new string[] { user[0].id };

                    // Load courses according to the course filter above
                    CourseVO[] course = crs.loadCourses(cf);

                    // if course is null, either the call failed, or the courseId actually refers to an organization.
                    // Check for course == null so we don't throw an exception when attempting to access the array.
                    if (course == null || course[0] == null)
                    {
                        continue;
                    }
                    else
                    {
                        // Add the current course Id to the membership filter
                        mf.courseIds = new string[] { courseId };

                        // Get Course memberships for the given user and course, as specified in the membership filter
                        CourseMembershipVO[] memberships = crm.loadCourseMembership(courseId, mf);

                        // If memberships were returned...
                        if (memberships != null)
                        {
                            // add the course id, the course name, the membership pk1, and the role id
                            courseList[i, 0] = course[0].courseId;
                            courseList[i, 1] = course[0].name;
                            courseList[i, 2] = memberships[0].id;
                            courseList[i, 3] = memberships[0].roleId;
                            if (DEBUG)
                            {
                                Console.WriteLine(i + ": " + courseList[i, 0] + "," + courseList[i, 1] + "," + courseList[i, 2] + "," + courseList[i, 3]);
                            }
                            i++;
                        }
                    }
                }
                catch (Exception e)
                {
                    // This error handling is not very useful, but essentially it is likely failing because of a
                    // NullReferenceException, so the current courselist is returned.
                    return(courseList);
                }
            }
            // Finished processing, return courseLIst.
            return(courseList);
        }