Ejemplo n.º 1
0
        /// <summary>
        /// Finds a Panopto folder by name (creates it if it does not already exist)
        /// </summary>
        private static SessionManagement.Folder GetFolder(string sFolderName)
        {
            //As with RemoteRecorder, pagination isn't strictly necessary as we've increased maxReceivedMessageSize="6553560" in the app.config
            //It's also unlikely that we'll return more than 25 results, since ListFolders method allows searching.
            SessionManagement.Pagination FolderPagination = new SessionManagement.Pagination()
            {
                MaxNumberResults = 1, //We only want a single folder
                PageNumber       = 0
            };
            //ListFolders requires a request object
            SessionManagement.ListFoldersRequest FolderRequest = new SessionManagement.ListFoldersRequest()
            {
                Pagination     = FolderPagination,
                ParentFolderId = Guid.Empty,                                  //Assume we're searching from the top of the folder hierarchy
                PublicOnly     = false,                                       //Assume we're searching all folders
                SortBy         = SessionManagement.FolderSortField.Relavance, //Relevance looks for the closest match folder (eg. search for foo = foo before foobar)
                SortIncreasing = true
            };

            //Create a SessionManagement Client
            SessionManagement.SessionManagementClient SMC = new SessionManagement.SessionManagementClient();

            //List folders
            SessionManagement.ListFoldersResponse FolderResponse = SMC.GetFoldersList(SessionAuthentication(), FolderRequest, sFolderName);
            //Note: in v4.6.1 we currently experience a bug where no folders are found, even though one exists YMMV

            //Note, as mentioned above, we use the search parameter to narrow down our list of folders before they are returned via the API
            //It's highly unlikely that you''ll have more than one folder with the same name, so we don't need to worry about pagination

            //Check the number of folders
            if (FolderResponse.Results.Count() == 0)
            {
                //It doesn't look like aFolderName exists in Panopto, let's create it
                //We're going to assume that this is a public folder at the top level of our hierarchy
                //Folder is public because it massively simplifies permissions. Essex actually makes it private and then gives viewer access on each recording individually.
                return(SMC.AddFolder(SessionAuthentication(), sFolderName, Guid.Empty, true));
            }
            else if (FolderResponse.Results.Count() > 1)
            {
                throw new Exception("More than one folder found: " + sFolderName);
            }
            else
            {
                return(FolderResponse.Results.FirstOrDefault());
            }
        }
        /// <summary>
        /// Get stats for all sessions user has access to.
        /// </summary>
        /// <param name="serverName">Server name</param>
        /// <param name="authUserKey">User name</param>
        /// <param name="authPassword">Password</param>
        /// <param name="errorMessage">Error message</param>
        /// <returns>String containing stats of all sessions</returns>
        public static string GetAllSessionStats(string serverName, string authUserKey, string authPassword, out string errorMessage)
        {
            string sessionStats = "Session ID, Session Name, Username, % Viewed, Last View Date, Folder Name\n";

            //List<Guid> sessionIds = new List<Guid>();
            errorMessage = "Query in progress.";

            if (!String.IsNullOrWhiteSpace(serverName) && !String.IsNullOrWhiteSpace(authUserKey) && !String.IsNullOrWhiteSpace(authPassword))
            {
                try
                {
                    // Permissions for user management
                    SessionManagement.AuthenticationInfo sessionAuth = new SessionManagement.AuthenticationInfo()
                    {
                        UserKey  = authUserKey,
                        Password = authPassword
                    };

                    SessionManagementClient smc = new SessionManagementClient(
                        new BasicHttpsBinding(), new EndpointAddress(string.Format(SessionManagementEndpointFormat, serverName)));
                    ListSessionsRequest request = new ListSessionsRequest();

                    SessionManagement.Pagination pagination = new SessionManagement.Pagination();
                    pagination.MaxNumberResults = MaxPerPage;
                    pagination.PageNumber       = 0;
                    request.Pagination          = pagination;

                    // Query newer sessions first.
                    request.SortBy         = SessionSortField.Date;
                    request.SortIncreasing = false;

                    int totalPages = int.MaxValue; // not set yet

                    while (request.Pagination.PageNumber < totalPages)
                    {
                        ListSessionsResponse sessionsResponse = smc.GetSessionsList(sessionAuth, request, null);

                        Session[] sessions = sessionsResponse.Results;
                        foreach (Session session in sessions)
                        {
                            sessionStats += GetAllStatsForSession(serverName, authUserKey, authPassword, session.Id, session.Name, session.FolderName, session.Duration);
                        }

                        // This is examined at the first call.
                        if (totalPages == int.MaxValue)
                        {
                            int maxEntries = Math.Min(sessionsResponse.TotalNumberResults, CapSessions);
                            totalPages = (maxEntries + MaxPerPage - 1) / MaxPerPage;
                        }

                        request.Pagination.PageNumber++;
                    }
                }

                catch (Exception e)
                {
                    errorMessage = e.Message;
                }
            }
            else
            {
                errorMessage = "Please enter servername, username, and password.";
            }

            return(sessionStats);
        }