public bool PostActivityMessage(string message, int courseId, string authToken)
        {
            //validate the user
            if (!IsValidKey(authToken))
            {
                return(false);
            }

            //because "currentUser" was pulled from a previous DB context, we need
            //to repull using the current context
            int         profileId   = activeSessions[authToken].UserProfile.ID;
            UserProfile currentUser = (from u in db.UserProfiles
                                       where u.ID == profileId
                                       select u).FirstOrDefault();

            if (currentUser == null)
            {
                return(false);
            }

            //find the course
            Course course = (from c in db.Courses
                             where c.ID == courseId
                             select c).FirstOrDefault();

            if (course == null)
            {
                return(false);
            }

            CourseUser courseUser = (from cu in db.CourseUsers
                                     where cu.UserProfileID == currentUser.ID
                                     &&
                                     cu.AbstractCourseID == course.ID
                                     select cu
                                     ).FirstOrDefault();

            if (courseUser == null)
            {
                return(false);
            }

            //use the data provided to create a new dashboard post
            DashboardPost newDp = new DashboardPost();

            newDp.Content    = message;
            newDp.Posted     = DateTime.UtcNow;
            newDp.CourseUser = courseUser;

            //add & save
            db.DashboardPosts.Add(newDp);
            db.SaveChanges();

            //return success
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Removes the provided user from the active course
        /// </summary>
        /// <param name="user"></param>
        public void RemoveUserFromCourse(UserProfile user)
        {
            var cu = (from c in db.CourseUsers
                      where c.AbstractCourseID == ActiveCourseUser.AbstractCourseID &&
                      c.UserProfileID == user.ID
                      select c).FirstOrDefault();

            if (cu != null)
            {
                db.CourseUsers.Remove(cu);
            }
            db.SaveChanges();

            var teamsWithNoMembers = (from at in db.AssignmentTeams
                                      where at.Team.TeamMembers.Count == 0
                                      select at).ToList();

            foreach (var team in teamsWithNoMembers)
            {
                db.AssignmentTeams.Remove(team);
            }
            db.SaveChanges();
        }
Esempio n. 3
0
        /// <summary>
        /// Sends a document that resides on OSBLE to the Annotate server.
        /// If the document already exists on annotate's servers, this function will not resubmit
        /// unless forceUpload is set to TRUE.
        /// </summary>
        /// <param name="assignmentID">The assignment that the document belongs to</param>
        /// <param name="authorTeamID">The document's author</param>
        /// <param name="forceUpload">If set to TRUE, will force a document upload to annotate's servers</param>
        /// <returns></returns>
        public AnnotateResult UploadDocument(int assignmentID, int authorTeamID, bool forceUpload = false)
        {
            bool           needsUpload = true;
            AnnotateResult result      = new AnnotateResult();

            result.Result = ResultCode.ERROR;

            //By default, we only upload documents to annotate if they haven't been uploaded
            //already.  This can be overridden by setting forceUpload = true.
            if (forceUpload == false)
            {
                //check for existing document
                using (OSBLEContext db = new OSBLEContext())
                {
                    string docString = GetAnnotateDocumentName(assignmentID, authorTeamID);
                    AnnotateDocumentReference code = (from c in db.AnnotateDocumentReferences
                                                      where c.OsbleDocumentCode.CompareTo(docString) == 0
                                                      select c
                                                      ).FirstOrDefault();
                    if (code == null)
                    {
                        needsUpload = true;
                    }
                    else
                    {
                        needsUpload         = false;
                        result.Result       = ResultCode.OK;
                        result.DocumentDate = code.AnnotateDocumentDate;
                        result.DocumentCode = code.AnnotateDocumentCode;
                    }
                }
            }

            if (needsUpload)
            {
                long      epoch      = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
                WebClient client     = new WebClient();
                string    sendResult = "";

                //Submit document to annotate
#if DEBUG
                //string documentUrl = "http://osble.org/content/icershort.pdf";
                string documentUrl = "https://plus.osble.org/FileHandler/GetAnnotateDocument?assignmentID={0}&authorTeamID={1}&apiKey={2}";
                documentUrl = string.Format(documentUrl, assignmentID, authorTeamID, ApiKey);
#else
                string documentUrl = "https://plus.osble.org/FileHandler/GetAnnotateDocument?assignmentID={0}&authorTeamID={1}&apiKey={2}";
                documentUrl = string.Format(documentUrl, assignmentID, authorTeamID, ApiKey);
#endif

                string apiKey = GenerateAnnotateKey("uploadDocument.php", ApiUser, epoch);
                //TODO: this was changed when helplab.org went down.
                //string uploadString = "http://helplab.org/annotate/php/uploadDocument.php?" +
                string uploadString = AnnotateURL + "/annotate/php/uploadDocument.php?" +
                                      "api-user={0}" +           //Annotate admin user name (see web config)
                                      "&api-requesttime={1}" +   //UNIX timestamp
                                      "&api-annotateuser={2}" +  //the current user (reviewer)
                                      "&api-auth={3}" +          //Annotate admin auth key
                                      "&url={4}";                //URL of the document to upload
                uploadString = string.Format(uploadString,
                                             ApiUser,
                                             epoch,
                                             ApiUser,
                                             apiKey,
                                             HttpUtility.UrlEncode(documentUrl)
                                             );
                try
                {
                    sendResult = client.DownloadString(uploadString);
                    //WriteLog("UploadDocument: " + sendResult);
                }
                catch (Exception ex)
                {
                    result.RawMessage = ex.Message;
                    result.Result     = ResultCode.ERROR;
                    return(result);
                }
                string documentCode = "";
                string documentDate = "";

                result.RawMessage = sendResult;
                if (sendResult.Substring(0, 2) == "OK")
                {
                    result.Result = ResultCode.OK;
                    string[] pieces = sendResult.Split(' ');
                    documentDate        = pieces[1];
                    documentCode        = pieces[2];
                    result.DocumentCode = documentCode;
                    result.DocumentDate = documentDate;

                    //add DB entry into OSBLE so that we know something's been sent
                    using (OSBLEContext db = new OSBLEContext())
                    {
                        string docString = GetAnnotateDocumentName(assignmentID, authorTeamID);
                        AnnotateDocumentReference code = (from c in db.AnnotateDocumentReferences
                                                          where c.OsbleDocumentCode.CompareTo(docString) == 0
                                                          select c
                                                          ).FirstOrDefault();
                        if (code == null)
                        {
                            code = new AnnotateDocumentReference();
                            db.AnnotateDocumentReferences.Add(code);
                        }
                        else
                        {
                            db.Entry(code).State = EntityState.Modified;
                        }
                        code.AnnotateDocumentCode = result.DocumentCode;
                        code.AnnotateDocumentDate = result.DocumentDate;
                        code.OsbleDocumentCode    = docString;
                        db.SaveChanges();
                    }
                }
            }
            return(result);
        }