Example #1
0
        public static long?GetMoodleDisciplinaId
        (
            this Disciplina item,
            AppConfiguration configuration,
            Modalidade modalidade,
            GetCourseByNameClient client = null,
            HttpClient httpClient        = null
        )
        {
            string disciplinaNome     = GetNomeDisciplina(item, configuration, modalidade);
            long?  moodleDisciplineId = MoodleFromToCache.GetCachedMoodleCourse(modalidade.IdModalidade, disciplinaNome);

            if (moodleDisciplineId.HasValue)
            {
                return(moodleDisciplineId.Value);
            }

            GetCourseByNameClient getClient = client;

            if (getClient == null)
            {
                getClient = new GetCourseByNameClient();
            }

            if (httpClient != null)
            {
                getClient.AddHttpClient(httpClient);
            }

            getClient.AddBaseUrl(modalidade.MoodleUrl)
            .AddServiceUrl(modalidade.MoodleServiceUrl)
            .AddToken(modalidade.MoodleGetInfoServiceToken);

            GetByNameRequest request = new GetByNameRequest()
            {
                Name = item.DisciplinaNome
            };

            Task <CourseResponse> task = getClient.Post(request);

            task.Wait();

            CourseResponse response = task.Result;

            if (response?.Id > 0)
            {
                MoodleFromToCache.AddCourse(modalidade.IdModalidade, disciplinaNome, response.Id);
            }

            return(response?.Id);
        }
Example #2
0
        private static long?InternalGetMoodleUserId
        (
            string cpf,
            Modalidade modalidade,
            GetUserByUsernameClient client = null,
            HttpClient httpClient          = null
        )
        {
            long?moodleUserId = MoodleFromToCache.GetCachedMoodleUser(modalidade.IdModalidade, cpf);

            if (moodleUserId.HasValue)
            {
                return(moodleUserId.Value);
            }

            GetUserByUsernameClient getClient = client;

            if (getClient == null)
            {
                getClient = new GetUserByUsernameClient();
            }

            if (httpClient != null)
            {
                getClient.AddHttpClient(httpClient);
            }

            getClient.AddBaseUrl(modalidade.MoodleUrl)
            .AddServiceUrl(modalidade.MoodleServiceUrl)
            .AddToken(modalidade.MoodleGetInfoServiceToken);

            GetByUsernameRequest request = new GetByUsernameRequest()
            {
                Username = cpf.DesformatarCpf()
            };

            Task <UserResponse> task = getClient.Post(request);

            task.Wait();

            UserResponse response = task.Result;

            if (response?.Id > 0)
            {
                MoodleFromToCache.AddUser(modalidade.IdModalidade, cpf, response.Id);
            }

            return(response?.Id);
        }
Example #3
0
        private static long?InternalGetMoodleCursoId
        (
            string cursoDescricao,
            Modalidade modalidade,
            GetCategoryByNameClient client = null,
            HttpClient httpClient          = null
        )
        {
            long?moodleCategoryId = MoodleFromToCache.GetCachedMoodleCategory(modalidade.IdModalidade, cursoDescricao);

            if (moodleCategoryId.HasValue)
            {
                return(moodleCategoryId.Value);
            }

            GetCategoryByNameClient getClient = client;

            if (getClient == null)
            {
                getClient = new GetCategoryByNameClient();
            }

            if (httpClient != null)
            {
                getClient.AddHttpClient(httpClient);
            }

            getClient.AddBaseUrl(modalidade.MoodleUrl)
            .AddServiceUrl(modalidade.MoodleServiceUrl)
            .AddToken(modalidade.MoodleGetInfoServiceToken);

            GetByNameRequest request = new GetByNameRequest()
            {
                Name = cursoDescricao
            };

            Task <CategoryResponse> task = getClient.Post(request);

            task.Wait();

            CategoryResponse response = task.Result;

            if (response?.Id > 0)
            {
                MoodleFromToCache.AddCategory(modalidade.IdModalidade, cursoDescricao, response.Id);
            }

            return(response?.Id);
        }
        private InternalMoodleData VerifyIfExistsOnMoodleAndCreateIfDont(Professor professor, Disciplina item)
        {
            //category
            long?moodleCategoryId = item.GetMoodleCursoId(ModalidadeAtual, GetCategoryByNameClient, HttpClient);

            if (!moodleCategoryId.HasValue)
            {
                CategoryResponse categoryResponse = CriarCursoMoodle(item);
                moodleCategoryId = categoryResponse?.Id;
            }

            if (moodleCategoryId.HasValue)
            {
                MoodleFromToCache.AddCategory(ModalidadeAtual.IdModalidade, item.CursoDescricao, moodleCategoryId.Value);
            }
            else
            {
                throw new MoodleDataNotExistsException($"O curso [{item.CursoDescricao}] não está cadastrado no MOODLE.");
            }

            // course
            long?moodleCourseId = item.GetMoodleDisciplinaId(Configuration, ModalidadeAtual, GetCourseByNameClient, HttpClient);

            if (!moodleCourseId.HasValue)
            {
                CourseResponse courseResponse = CriarDisciplinaMoodle(item, moodleCategoryId.Value);
                moodleCourseId = courseResponse?.Id;
            }

            if (moodleCourseId.HasValue)
            {
                MoodleFromToCache.AddCourse(ModalidadeAtual.IdModalidade, item.DisciplinaNome, moodleCourseId.Value);
            }
            else
            {
                throw new MoodleDataNotExistsException($"A disciplina [{item.DisciplinaNome}] não está cadastrada no MOODLE.");
            }

            //user
            long?moodleUserId = professor.GetMoodleUserId(ModalidadeAtual, GetUserByUsernameClient, HttpClient);

            if (!moodleUserId.HasValue)
            {
                UserResponse userResponse = CriarUsuarioMoodle(professor);
                moodleUserId = userResponse?.Id;
            }

            if (moodleUserId.HasValue)
            {
                MoodleFromToCache.AddUser(ModalidadeAtual.IdModalidade, professor.ProfessorCpf, moodleUserId.Value);
            }
            else
            {
                throw new MoodleDataNotExistsException($"O professor [{professor.ProfessorCpf} | {professor.ProfessorNome}] não está cadastrado no MOODLE.");
            }

            return(new InternalMoodleData()
            {
                MoodleCategoryId = moodleCategoryId.Value,
                MoodleCourseId = moodleCourseId.Value,
                MoodleUserId = moodleUserId.Value
            });
        }
        public override SendResult <Curso, CategoryResponse> SendAll()
        {
            SendResult <Curso, CategoryResponse> result = new SendResult <Curso, CategoryResponse>();
            IEnumerable <Curso> data = GetData("");

            if (data == null)
            {
                return(result);
            }

            foreach (var modalidade in Modalidades)
            {
                var cursos = data.Where(x => x.IdModalidade == modalidade.IdModalidade).ToArray();

                this.AddMoodleBaseUrl(modalidade.MoodleUrl)
                .AddMoodleToken(modalidade.MoodleToken)
                .AddMoodleGetInfoServiceToken(modalidade.MoodleGetInfoServiceToken)
                .AddMoodleServiceUrl(modalidade.MoodleServiceUrl);

                this.AddMoodleCategoryParent(modalidade.MoodleCategoryParent)
                .AddMoodleDescriptionFormat(modalidade.MoodleDescriptionFormat);

                foreach (var item in cursos)
                {
                    try
                    {
                        long?cachedMoodleId = MoodleFromToCache.GetCachedMoodleCategory(modalidade.IdModalidade, item.CursoDescricao);

                        if (cachedMoodleId.HasValue)
                        {
                            LastUrl = "cached_value";

                            var reason = new NotImportedReason <Curso>()
                            {
                                Data   = item,
                                Url    = LastUrl,
                                Reason = $"Curso [{item.CursoDescricao}] já está adicionado ao MOODLE ({LastUrl})."
                            };

                            result.NotImported.Enqueue(reason);
                            Log(reason.Reason);
                            continue;
                        }

                        CategoryResponse exists = VerifyIfExists(item.CursoDescricao);

                        if (exists?.Id > 0)
                        {
                            MoodleFromToCache.AddCategory(modalidade.IdModalidade, item.CursoDescricao, exists.Id);

                            var reason = new NotImportedReason <Curso>()
                            {
                                Data   = item,
                                Url    = LastUrl,
                                Reason = $"Curso [{item.CursoDescricao}] já está adicionado ao MOODLE ({LastUrl})."
                            };

                            result.NotImported.Enqueue(reason);
                            Log(reason.Reason);
                            continue;
                        }

                        CategoryResponse response = SendItem(item);

                        ImportedResult <Curso, CategoryResponse> importedResult = new ImportedResult <Curso, CategoryResponse>()
                        {
                            Date   = DateTime.Now,
                            Data   = item,
                            Url    = LastUrl,
                            Result = response
                        };

                        result.ImportedSuccessfully.Enqueue(importedResult);
                        Log($"Curso [{item.CursoDescricao}] adicionado");
                    }
                    catch (Exception ex)
                    {
                        var reason = new NotImportedReason <Curso>()
                        {
                            Data      = item,
                            Url       = LastUrl,
                            Exception = ex
                        };

                        result.NotImported.Enqueue(reason);
                        Log(reason.Reason);
                    }
                }
            }

            return(result);
        }
Example #6
0
        protected override void ProcessItem(Professor item, AbstractMoodleServiceClient createClient, AbstractMoodleServiceClient verifyClient)
        {
            try
            {
                long?cachedMoodleId = MoodleFromToCache.GetCachedMoodleUser(ModalidadeAtual.IdModalidade, item.ProfessorCpf);

                if (cachedMoodleId.HasValue)
                {
                    LastUrl = "cached_value";

                    var reason = new NotImportedReason <Professor>()
                    {
                        Data   = item,
                        Url    = LastUrl,
                        Reason = $"Professor [{item.ProfessorCpf} | {item.ProfessorNome}] já está adicionado ao MOODLE ({LastUrl})."
                    };

                    Result.NotImported.Enqueue(reason);
                    Log(reason.Reason);
                    return;
                }

                UserResponse exists = VerifyIfExists(verifyClient, item.ProfessorCpf);

                if (exists?.Id > 0)
                {
                    MoodleFromToCache.AddUser(ModalidadeAtual.IdModalidade, item.ProfessorCpf, exists.Id);

                    var reason = new NotImportedReason <Professor>()
                    {
                        Data   = item,
                        Url    = LastUrl,
                        Reason = $"Professor [{item.ProfessorCpf} | {item.ProfessorNome}] já está adicionado ao MOODLE ({LastUrl})."
                    };

                    Result.NotImported.Enqueue(reason);
                    Log(reason.Reason);
                    return;
                }

                if (item.AtivoProfessor)
                {
                    UserResponse response = SendItem(createClient, item);

                    ImportedResult <Professor, UserResponse> importedResult = new ImportedResult <Professor, UserResponse>()
                    {
                        Date   = DateTime.Now,
                        Data   = item,
                        Url    = LastUrl,
                        Result = response,
                        Active = true,
                    };

                    Result.ImportedSuccessfully.Enqueue(importedResult);
                    Log($"Professor [{item.ProfessorCpf} | {item.ProfessorNome}] adicionado.");
                    MoodleFromToCache.AddUser(ModalidadeAtual.IdModalidade, item.ProfessorCpf, response.Id);
                }
                else
                {
                    SuspendedUserResult suspendedUserResult = this.SuspendItem(item, ModalidadeAtual, createClient.GetUnderlyingHttpClient());

                    if (!suspendedUserResult.MoodleId.HasValue)
                    {
                        throw new MoodleDataNotExistsException($"Tentativa de suspender usuário falhou. O professor [{item.ProfessorCpf} | {item.ProfessorNome}] não está cadastrado no MOODLE");
                    }

                    var nome      = item.ProfessorNome;
                    var matricula = item.ProfessorMatricula.FormatarMatricula();

                    if (item.ProfessorEmail == null)
                    {
                        item.ProfessorEmail = "";
                    }

                    UserResponse response = new UserResponse()
                    {
                        Email    = item.ProfessorEmail.TratarEmail(item.ProfessorMatricula),
                        Id       = suspendedUserResult.MoodleId.Value,
                        Fullname = nome,
                        Username = item.ProfessorCpf.DesformatarCpf()
                    };

                    ImportedResult <Professor, UserResponse> importedResult = new ImportedResult <Professor, UserResponse>()
                    {
                        Date   = DateTime.Now,
                        Data   = item,
                        Url    = suspendedUserResult.LastUrl,
                        Result = response,
                        Active = false
                    };

                    Result.ImportedSuccessfully.Enqueue(importedResult);
                    Log($"Professor [{item.ProfessorCpf} | {item.ProfessorNome}] SUSPENSO.");
                    MoodleFromToCache.AddUser(ModalidadeAtual.IdModalidade, item.ProfessorCpf, suspendedUserResult.MoodleId.Value);
                }
            }
            catch (MoodleDataNotExistsException mex)
            {
                var reason = new NotImportedReason <Professor>()
                {
                    Data   = item,
                    Url    = LastUrl,
                    Reason = mex.Message
                };

                Result.NotImported.Enqueue(reason);
                Log(reason.Reason);
            }
            catch (AggregateException agex)
            {
                var exception = agex.InnerExceptions[0];

                var reason = new NotImportedReason <Professor>()
                {
                    Data      = item,
                    Url       = LastUrl,
                    Reason    = exception.Message,
                    Exception = exception
                };

                Result.NotImported.Enqueue(reason);
                Log(reason.Reason);
            }
            catch (Exception ex)
            {
                var reason = new NotImportedReason <Professor>()
                {
                    Data      = item,
                    Url       = LastUrl,
                    Exception = ex,
                    Reason    = ex.Message
                };

                Result.NotImported.Enqueue(reason);
                Log(reason.Reason);
            }
        }
Example #7
0
        protected override void ProcessItem(Disciplina item, AbstractMoodleServiceClient createClient, AbstractMoodleServiceClient verifyClient)
        {
            try
            {
                item.DisciplinaNome = item.GetNomeDisciplina(Configuration, ModalidadeAtual);
                item.ShortName      = item.GetShortNameDisciplina(Configuration, ModalidadeAtual);

                long?cachedMoodleId = MoodleFromToCache.GetCachedMoodleCourse(ModalidadeAtual.IdModalidade, item.DisciplinaNome);

                if (cachedMoodleId.HasValue)
                {
                    LastUrl = "cached_value";

                    var reason = new NotImportedReason <Disciplina>()
                    {
                        Data   = item,
                        Url    = LastUrl,
                        Reason = $"Disciplina [{item.DisciplinaNome}] já está adicionada ao MOODLE ({LastUrl})."
                    };

                    Result.NotImported.Enqueue(reason);
                    Log(reason.Reason);
                    return;
                }

                CourseResponse exists = VerifyIfExists(verifyClient, item.DisciplinaNome);

                if (exists?.Id > 0)
                {
                    MoodleFromToCache.AddCourse(ModalidadeAtual.IdModalidade, item.DisciplinaNome, exists.Id);

                    var reason = new NotImportedReason <Disciplina>()
                    {
                        Data   = item,
                        Url    = LastUrl,
                        Reason = $"Disciplina [{item.DisciplinaNome}] já está adicionada ao MOODLE ({LastUrl})."
                    };

                    Result.NotImported.Enqueue(reason);
                    Log(reason.Reason);
                    return;
                }

                CourseResponse response = SendItem(createClient, item);

                ImportedResult <Disciplina, CourseResponse> importedResult = new ImportedResult <Disciplina, CourseResponse>()
                {
                    Date   = DateTime.Now,
                    Data   = item,
                    Url    = LastUrl,
                    Result = response,
                    Active = true,
                };

                Result.ImportedSuccessfully.Enqueue(importedResult);
                MoodleFromToCache.AddCourse(ModalidadeAtual.IdModalidade, item.DisciplinaNome, response.Id);

                Log($"Disciplina {item.DisciplinaNome} adicionado.");
            }
            catch (MoodleDataNotExistsException mex)
            {
                var reason = new NotImportedReason <Disciplina>()
                {
                    Data   = item,
                    Url    = LastUrl,
                    Reason = mex.Message
                };

                Result.NotImported.Enqueue(reason);
                Log(reason.Reason);
            }
            catch (AggregateException agex)
            {
                var exception = agex.InnerExceptions[0];

                var reason = new NotImportedReason <Disciplina>()
                {
                    Data      = item,
                    Url       = LastUrl,
                    Reason    = exception.Message,
                    Exception = exception
                };

                Result.NotImported.Enqueue(reason);
                Log(reason.Reason);
            }
            catch (Exception ex)
            {
                var reason = new NotImportedReason <Disciplina>()
                {
                    Data      = item,
                    Url       = LastUrl,
                    Exception = ex,
                    Reason    = ex.Message
                };

                Result.NotImported.Enqueue(reason);
                Log(reason.Reason);
            }
        }