private async Task <List <LearningTask> > ProcessTasks(LearningActivity learningActivity, ApplicationUser currentUser, bool checkUpdated = false)
        {
            //Add each task in the activity to the database
            //If a task has child tasks, add those first
            List <LearningTask> finalTasks = new List <LearningTask>();
            int orderCount = 0;

            for (int i = 0; i < learningActivity.LearningTasks.Count(); i++)
            {
                LearningTask thisTask = learningActivity.LearningTasks.ElementAt(i);
                thisTask.Order = orderCount++;

                LearningTask dbTask = await AddTaskIfNeeded(thisTask, currentUser, checkUpdated);

                List <LearningTask> finalChildTasks = new List <LearningTask>();

                if (thisTask.ChildTasks != null)
                {
                    foreach (LearningTask childTask in thisTask.ChildTasks)
                    {
                        childTask.ParentTask = dbTask;
                        finalChildTasks.Add(await AddTaskIfNeeded(childTask, currentUser, checkUpdated));
                    }
                }

                dbTask.ChildTasks = finalChildTasks;
                await db.SaveChangesAsync();

                finalTasks.Add(dbTask);
            }

            return(finalTasks);
        }
        private async Task <LearningTask> AddTaskIfNeeded(LearningTask thisTask, ApplicationUser currentUser, bool checkUpdated = false)
        {
            if (checkUpdated)
            {
                LearningTask existingTask = await db.LearningActivityTasks.FirstOrDefaultAsync(t => t.Id == thisTask.Id);

                if (existingTask != null)
                {
                    if (existingTask.Description == thisTask.Description &&
                        existingTask.ImageUrl == thisTask.ImageUrl &&
                        existingTask.JsonData == thisTask.JsonData &&
                        existingTask.Order == thisTask.Order)
                    {
                        // nothing has changed
                        return(existingTask);
                    }

                    // else something has changed - make a new task so that existing responses don't break
                    existingTask.SoftDeleted = true;
                }
            }

            LearningTask dbTask = db.LearningActivityTasks.Add(new LearningTask
            {
                Author      = currentUser,
                Description = thisTask.Description,
                JsonData    = thisTask.JsonData,
                TaskType    = await db.TaskTypes.SingleAsync(tt => tt.Id == thisTask.TaskType.Id),
                ParentTask  = thisTask.ParentTask,
                Order       = thisTask.Order
            });

            if (dbTask.TaskType.IdName == "SCAN_QR")
            {
                await db.SaveChangesAsync();

                dbTask.JsonData = await GenerateQR($"task-{dbTask.Id}", Common.ServerUtils.GetTaskQRCodeData(dbTask.Id));
            }

            await db.SaveChangesAsync();

            return(dbTask);
        }