/// <summary> /// Attempts to detect if there is a SharePoint v3 site at the URL provided by user /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCheckSharepointV3Web_Click(object sender, EventArgs e) { lblSLKSiteInitStatus.Text = String.Empty; btnCheckSharepointV3Web.Enabled = false; SharePointV3 site = new SharePointV3(); string result; if (site.TestSharePointV3Site(tbSLKWeb.Text, out result)) { lblSLKSiteInitStatus.Text = TextResources.WebConnectionSuccessful; } else { lblSLKSiteInitStatus.Text = TextResources.WebConnectionError + result; } btnCheckSharepointV3Web.Enabled = true; }
/// <summary> /// accesses the document library at the path provided, /// loops through the learning packages /// located the library and reads the package Id from package manifest /// </summary> /// <param name="siteURL">web site url</param> /// <param name="documentLibraryName">document library name</param> /// <returns></returns> public Hashtable GetAllLearningResources(string siteURL, string documentLibraryName) { Hashtable resources = new Hashtable(); SharePointV3 sharepoint = new SharePointV3(); SPWeb documentLibWeb = sharepoint.OpenWeb(siteURL); SPDocumentLibrary docLibrary = sharepoint.GetDocumentLibrary(documentLibraryName, documentLibWeb); Microsoft.SharePointLearningKit.SlkStore store = SlkStore.GetStore(documentLibWeb); foreach (SPListItem item in docLibrary.Items) { //if this list item is a file if (item.File != null) { SharePointFileLocation fileLocation = new SharePointFileLocation(documentLibWeb, item.UniqueId, item.File.UIVersion); string location = fileLocation.ToString(); try { PackageInformation info = store.GetPackageInformation(location); XPathNavigator manifestNavigator = info.ManifestReader.CreateNavigator(); Guid packageIdentifier = ManifestParser.ParseIndexXml(manifestNavigator); if (packageIdentifier == Guid.Empty) { packageIdentifier = ManifestParser.ParseImsManifestXml(manifestNavigator); } if (packageIdentifier != Guid.Empty) { resources.Add(packageIdentifier.ToString(), location); } } catch { //not a valid learning package, do nothing } } } return(resources); }
/// <summary> /// attempts to verify validity of the SharePoint V3 library path. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCheckSLKDocLib_Click(object sender, EventArgs e) { lblSLKDocLibStatus.Text = String.Empty; btnCheckSLKDocLib.Enabled = false; SharePointV3 site = new SharePointV3(); string result; if (site.TestSharePointV3Site(tbDocLibWeb.Text, out result)) { if (site.TestSharePointDocLibrary(tbDocLibWeb.Text, tbDocLibName.Text, out result)) { lblSLKDocLibStatus.Text += TextResources.WebConnectionSuccessful; } else { lblSLKDocLibStatus.Text += TextResources.DocLibraryTestError + result; } } else { lblSLKDocLibStatus.Text += TextResources.WebConnectionError + result; } btnCheckSLKDocLib.Enabled = true; }
/// <summary> /// Does the actual work of creating an SLK site with groups and users. /// </summary> /// <param name="classData">Class to be created</param> /// <param name="processingLog">Method returns log of actions performed</param> private void ProcessClass(CS4Class classData, ref string processingLog) { string log = System.String.Empty; SharePointV3 SLKweb = new SharePointV3(); CS4UserCollection classUsers = new CS4UserCollection(); CS4UserCollection groupUsers = new CS4UserCollection(); bool classAdded = false; string classURL = String.Empty; string classCreateResult = String.Empty; CS4User groupsOwner = null; log += System.Environment.NewLine + TextResources.ProcessingClass + classData.ClassName + System.Environment.NewLine; if (classData.Transfer) { classAdded = SLKweb.CreateSite( SiteBuilder.Default.SLKSchoolWeb, classData.ClassWeb, classData.ClassName, String.Empty, classData.ClassLCID, classData.Overwrite, ref classURL, ref classCreateResult); log += classCreateResult; if (classAdded) { //adding site users for (int classUserIndex = 0; classUserIndex < classData.Users.Count; classUserIndex++) { CS4User user = classData.Users.Item(classUserIndex); if (user.Transfer) { user.UserRoles = this.DefineUserRoles(user.IsTeacher, classURL); classUsers.Add(user); //if the user is teacher, set it as group owner //we only take first teacher to be groups owner as we only need one if ((groupsOwner == null) && (user.IsTeacher)) { groupsOwner = user; } } else { log += string.Format(TextResources.UserNotForTransfer, user.UserLoginWithDomain) + System.Environment.NewLine; } } string addUsersLog = String.Empty; SLKweb.AssignUsersToSite(classURL, classUsers, ref addUsersLog); log += addUsersLog; //adding groups for (int groupIndex = 0; groupIndex < classData.Groups.Count; groupIndex++) { log += string.Format(TextResources.ProcessingGroup, classData.Groups.Item(groupIndex).WebName) + System.Environment.NewLine; if (classData.Groups.Item(groupIndex).Transfer) { //processing group users groupUsers = new CS4UserCollection(); for (int groupUserIndex = 0; groupUserIndex < classData.Groups.Item(groupIndex).GroupUsers.Count; groupUserIndex++) { CS4User groupUser = classData.Groups.Item(groupIndex).GroupUsers.Item(groupUserIndex); if (groupUser.Transfer) { groupUser.UserRoles = this.DefineUserRoles(groupUser.IsTeacher, classURL); groupUsers.Add(groupUser); } else { log += string.Format(TextResources.GroupUserNotForTransfer, groupUser.UserLoginWithDomain) + System.Environment.NewLine; } } //adding group //only if we have a group owner and at least one group user if ((groupUsers.Count > 0) && (groupsOwner != null)) { //taking first user as default user bool groupAdded = false; string groupCreateResult = string.Empty; groupAdded = SLKweb.CreateUsersGroup( classURL, classData.Groups.Item(groupIndex).WebName, groupsOwner, classData.Groups.Item(groupIndex).GroupUsers.Item(0), classData.Groups.Item(groupIndex).Overwrite, ref groupCreateResult); //adding group users log += groupCreateResult; if (groupAdded) { string addGroupUsersLog = String.Empty; SLKweb.AssignUsersToGroup(classURL, classData.Groups.Item(groupIndex).WebName, groupUsers, ref addGroupUsersLog); log += addGroupUsersLog; } } else { //not transferring this group log += TextResources.GroupOwnerOrDefaultUserProblem + System.Environment.NewLine; } } else { log += TextResources.GroupNotForTransfer + System.Environment.NewLine; } } } } else { log += TextResources.ClassNotForTransfer + System.Environment.NewLine; } log += String.Format(TextResources.FinishedProcessingClass, classData.ClassName) + System.Environment.NewLine; processingLog = log; }
/// <summary> /// For the class requested gets the assignments data from Class Server 4 database /// and transfers assignments and grading points and teacher comments using SLK API /// </summary> /// <param name="classData">class information from Class Server 4 classes config file</param> /// <param name="logText">returns log of operations performed</param> /// <param name="learningPackages">information about learning packages available on SLK school site</param> private void MoveAssignments(CS4Class classData, ref string logText, Hashtable learningPackages) { SharePointV3 assignmentSite = new SharePointV3(); string assignmentSiteUrl = assignmentSite.BuildSubSiteUrl(SiteBuilder.Default.SLKSchoolWeb, classData.ClassWeb); //for the getmemberships operation to succeed the current user has to be an SLK instructor on the web site //looping through all the class users to see if any of them are instructors //and trying to open the class web with and instructor's token SPWeb assignmentWeb = null; SPWeb assignmentWebUnderCurrentUser = assignmentSite.OpenWeb(assignmentSiteUrl); SPRoleDefinition instructorsRole = assignmentWebUnderCurrentUser.RoleDefinitions[SiteBuilder.Default.SLKInstructorSharePointRole]; bool foundInstructor = false; foreach (CS4User user in classData.Users) { if ((user.IsTeacher) && (user.Transfer)) { try { SPUser spUser = assignmentWebUnderCurrentUser.SiteUsers[user.UserLoginWithDomain]; SPUserToken token = spUser.UserToken; SPSite site = new SPSite(assignmentSiteUrl, token); assignmentWeb = site.OpenWeb(); if (assignmentWeb.AllRolesForCurrentUser.Contains(instructorsRole)) { foundInstructor = true; break; } } catch { //doing nothing, will try the next instructor } } } if (!foundInstructor) { logText += TextResources.AssignmentsTransferErrorNoClassInstructors + Environment.NewLine; return; } //open the Class SLK store //note we are using SPWeb opened with an instructor's SPUserToken Microsoft.SharePointLearningKit.SlkStore slkStore = SlkStore.GetStore(assignmentWeb); //get all learners and instructors for the class SlkMemberships memberships = slkStore.GetMemberships(assignmentWeb, null, null); Dictionary <string, SlkUser> allLearners = new Dictionary <string, SlkUser>( StringComparer.OrdinalIgnoreCase); foreach (SlkUser learner in memberships.Learners) { allLearners.Add(learner.SPUser.LoginName, learner); } Dictionary <string, SlkUser> allInstructors = new Dictionary <string, SlkUser>( StringComparer.OrdinalIgnoreCase); foreach (SlkUser instructor in memberships.Instructors) { allInstructors.Add(instructor.SPUser.LoginName, instructor); } //instructors list will always be the same for all assignments //because there is no link between assignments and teachers in CS4 SlkUserCollection classInstructors = new SlkUserCollection(); foreach (CS4User user in classData.Users) { if ((user.IsTeacher) && (user.Transfer)) { SlkUser slkUser; if (allInstructors.TryGetValue(user.UserLoginWithDomain, out slkUser)) { classInstructors.Add(slkUser); } else { //instructor not found on slk site, log logText += String.Format(TextResources.InstructorNotRegisteredWithSLKSite, user.UserLoginWithDomain, assignmentSiteUrl) + Environment.NewLine; } } } //get assignments for this class from the CS4 data base CS4Database database = new CS4Database(SiteBuilder.Default.ClassServerDBConnectionString); DataTable assignmentItems; DataTable userAssignments; int numAssignments = database.GetAssignments(classData.ClassId, out assignmentItems, out userAssignments); //loop through assignments list for (int assignmentIndex = 0; assignmentIndex < numAssignments; assignmentIndex++) { try { string packageIdent = (assignmentItems.Rows[assignmentIndex]["PackageIdentifier"] != System.DBNull.Value ? assignmentItems.Rows[assignmentIndex]["PackageIdentifier"].ToString() : String.Empty); int assignmentId = (assignmentItems.Rows[assignmentIndex]["AssignmentID"] != System.DBNull.Value ? System.Convert.ToInt32(assignmentItems.Rows[assignmentIndex]["AssignmentID"]) : 0); logText += String.Format(TextResources.TransferringAssignment, assignmentId.ToString()) + Environment.NewLine; //get assignment's package identifier string packageLocation = GetPackageLocation(packageIdent, learningPackages); if (packageLocation.Length == 0) { if (packageIdent.Length == 0) { //log: not importing assignment as the package cannot be identified logText += String.Format(TextResources.CantTransferAssignmentUnknownLearningResource, assignmentId) + Environment.NewLine; } else { //log - assignment cannot be imported as the package is not imported into slk logText += String.Format(TextResources.CantTransferAssignmentNoLearningResource, assignmentId) + Environment.NewLine; } //move on to the next assignment break; } //set assignment properties AssignmentProperties properties = ReadAssignmentPropertiesFromDataRow(assignmentItems.Rows[assignmentIndex]); Hashtable gradingPoints = new Hashtable(); Hashtable gradingComments = new Hashtable(); //set instructors list foreach (SlkUser classInstructor in classInstructors) { properties.Instructors.Add(classInstructor); } //set learners list for (int userAssignmentIndex = 0; userAssignmentIndex < userAssignments.Rows.Count; userAssignmentIndex++) { DataRow assignmentRow = userAssignments.Rows[userAssignmentIndex]; int userAssignmentTableID = (assignmentRow["AssignmentID"] == System.DBNull.Value) ? 0 : System.Convert.ToInt32(assignmentRow["AssignmentID"]); int userId = (assignmentRow["StudentID"] == System.DBNull.Value) ? 0 : System.Convert.ToInt32(assignmentRow["StudentID"]); bool isAssignmentGraded = (assignmentRow["HasTeacherGraded"].ToString().ToLower() == "true" ? true : false); float points = (assignmentRow["Points"] == System.DBNull.Value) ? 0 : System.Convert.ToSingle(assignmentRow["Points"]); string instructorComments = assignmentRow["TeacherComments"].ToString(); //to minimize sql queries the UserAssignments table contains all assignments for the class //so we need to check if this row is for the assignment currently being processed if (assignmentId == userAssignmentTableID) { //find this user in list of users from classes.xml CS4User user = classData.Users.GetByUserId(userId); if (user != null) { //see if this user is for transfer in classes.xml if (user.Transfer) { //see if this user is a learner member on SLK site SlkUser slkUser; if (allLearners.TryGetValue(user.UserLoginWithDomain, out slkUser)) { properties.Learners.Add(slkUser); //save grading info for this learner to be used later if (isAssignmentGraded) { gradingPoints.Add(slkUser.UserId, points); gradingComments.Add(slkUser.UserId, instructorComments); } } else { //user not found on slk site, log logText += String.Format(TextResources.UserNotRegisteredWithSLKSite, user.UserLoginWithDomain, assignmentSiteUrl, assignmentId) + Environment.NewLine; } } else { //user assignments will not be transferred as user is marked "not for transfer" logText += String.Format(TextResources.UserNotForTransfer, user.UserLoginWithDomain) + Environment.NewLine; } } else { //user is not found in xml file, log logText += String.Format(TextResources.UserNotFoundInXMLFile, userId, assignmentSiteUrl, SiteBuilder.Default.ClassStructureXML, assignmentId) + Environment.NewLine; } } //create the assignment AssignmentItemIdentifier assignmentIdSLK = slkStore.CreateAssignment(assignmentWeb, packageLocation, 0, SlkRole.Instructor, properties); //transfer the grading results for the assignments AssignmentProperties basicAssignmentProperties; ReadOnlyCollection <GradingProperties> gradingPropertiesList = slkStore.GetGradingProperties(assignmentIdSLK, out basicAssignmentProperties); for (int learnerIndex = 0; learnerIndex < gradingPropertiesList.Count; learnerIndex++) { // set <gradingProperties> to information about this learner assignment GradingProperties gradingProperties = gradingPropertiesList[learnerIndex]; if (gradingPoints.ContainsKey(gradingProperties.LearnerId)) { //assignment has been graded, transfer grade and comment to SLK gradingProperties.Status = LearnerAssignmentState.Final; gradingProperties.FinalPoints = (float)gradingPoints[gradingProperties.LearnerId]; gradingProperties.InstructorComments = gradingComments[gradingProperties.LearnerId].ToString(); } } //this call will not save the grade, but it will set the correct state and //put the teacher's comment. logText += slkStore.SetGradingProperties(assignmentIdSLK, gradingPropertiesList) + Environment.NewLine; //calling the second time to save grades for (int learnerIndex = 0; learnerIndex < gradingPropertiesList.Count; learnerIndex++) { gradingPropertiesList[learnerIndex].Status = null; } logText += slkStore.SetGradingProperties(assignmentIdSLK, gradingPropertiesList) + Environment.NewLine; logText += String.Format(TextResources.TransferredAssignment, assignmentId.ToString(), assignmentIdSLK.GetKey().ToString()) + Environment.NewLine; } } catch (System.Exception ex) { //exception when transferring an assignment logText += TextResources.AnError + ex.Message + Environment.NewLine; } } }