/// <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> /// 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; } } }