Example #1
0
        public static void Sync()
        {
            Load();

            if (_scannedCodes.Count == 0)
            {
                ResponseManager.ShowMessage("Note", "No grades to sync!");
                return;
            }

            StringBuilder stringBuilder = new StringBuilder();

            foreach (var code in _scannedCodes)
            {
                stringBuilder.AppendFormat("{0}-|-{1}-|-{2}-||-", code.Subject, code.Grade, code.Code);
            }

            var data = new NameValueCollection();

            data.Add("newgrades", string.Empty);
            data.Add("data", stringBuilder.ToString());

            string reply = WebFunctions.Request(data);

            string[] formattedReply = Regex.Split(reply, Environment.NewLine);

            foreach (string line in formattedReply)
            {
                if (line.StartsWith("Grade saved"))
                {
                    DeleteFirstCode();
                }
                else if (line.StartsWith("Code already used"))
                {
                    DeleteFirstCode();
                }
            }

            reply          = reply.Replace("Grade saved!", string.Empty);
            reply          = reply.Replace("Code already used!", string.Empty);
            formattedReply = Regex.Split(reply, Environment.NewLine);
            formattedReply = formattedReply.TrimArray();

            List <string> gradedStudents = new List <string> ();
            StringBuilder resultMessage  = new StringBuilder();

            foreach (string line in formattedReply)
            {
                if (line.StartsWith("Graded student"))
                {
                    gradedStudents.Add(line.Replace("Graded student: ", string.Empty));
                }
                else
                {
                    foreach (string line2 in formattedReply)
                    {
                        resultMessage.AppendLine(line2);
                    }

                    Functions.CurrentContext.RunOnUiThread(() => ResponseManager.ShowMessage("Success", resultMessage.ToString()));
                    return;
                }
            }

            if (gradedStudents.Count == 0)
            {
                resultMessage.AppendLine("All sent codes were already graded!");
            }
            else
            {
                resultMessage.AppendLine("Grades successfully synced to server! Results:");
                resultMessage.AppendLine();

                foreach (string student in Functions.ShuffleArray(gradedStudents.ToArray()))
                {
                    resultMessage.AppendLine(student);
                }
            }

            Functions.CurrentContext.RunOnUiThread(() => ResponseManager.ShowMessage("Success", resultMessage.ToString()));
        }