Kaynağa Gözat

survey engine&editor part1

ganahrhr 2 yıl önce
ebeveyn
işleme
3159885532

+ 20 - 0
Models/Event.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace HyperCube.Models
+{
+    public class Event
+    {
+        public int ID { get; set; }
+        public string Name { get; set; }
+        public string Description { get; set; }
+        public DateTime DateStart { get; set; }
+        public DateTime DateEnd { get; set; }
+        public DateTime DateCreated { get; set; }
+        public DateTime DateUpdated { get; set; }
+        public string CreatorID { get; set; }
+        public bool IsActive { get; set; }
+    }
+}

+ 113 - 0
Models/Survey.cs

@@ -0,0 +1,113 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Console = HyperCube.Utils.AdvConsole;
+
+namespace HyperCube.Models
+{
+    public class Survey
+    {        
+        public int ID { get; set; }
+        public int EvenID { get; set; }
+        public string Name { get; set; }
+        public string Description { get; set; }
+        public DateTime DateCreated { get; set; }
+        public DateTime DateUpdated { get; set; }
+        public string CreatorID { get; set; }
+        public Dictionary<int, SurveyItem> SurveyItems { get; set; } = new();
+        public bool IsNew { get; set; } = true;
+
+        public async Task<int> Save(string userID)
+        {
+            long newID;
+            string stringSQL;
+
+            if (IsNew)
+            {
+                ///tmp
+                EvenID = 1;
+
+                stringSQL = $"INSERT INTO surveys (eventid, name, description, creatorid) " +
+                    $"VALUES ({EvenID}, '{Name}', '{Description}', '{userID}')";
+            }
+            else
+            {
+                stringSQL = $"UPDATE surveys " +
+                    $"SET eventid={EvenID}, name='{Name}', description='{Description}' " + // creatorid='{userID}'
+                    $"WHERE id={ID}";
+            }
+
+            newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
+            IsNew = false;
+            return ID = (int)newID;
+        }
+
+        public void AddNewItem(string userID)
+        {
+            int surveyID = ID;
+            Console.WriteLine($"Survey AddNewItem. survey id={surveyID}.");
+            int newTempKey = (SurveyItems.Count > 0) ? SurveyItems.Keys.Max() + 1 : 1;
+            int position = (SurveyItems.Count > 0) ? SurveyItems.Select(o => o.Value.Position).Max() + 1 : 1;
+            Console.WriteLine($"Survey AddNewItem. item newTempKey={newTempKey}.");
+            SurveyItem item = new()
+            {
+                ID = newTempKey,
+                SurveyID = surveyID,
+                Position = position,
+                CreatorID = userID,
+                DateCreated = DateTime.Now
+            };
+            SurveyItems.Add(newTempKey, item);
+        }
+    }
+
+    public class SurveyItem
+    {
+        public int ID { get; set; }
+        public int SurveyID { get; set; }
+        public string Text { get; set; }
+        public int Position { get; set; }
+        public string CreatorID { get; set; }
+        public DateTime DateCreated { get; set; }
+        public DateTime DateUpdated { get; set; }
+        public bool Required { get; set; } = true;
+        public Dictionary<int, SurveyItemOption> SurveyItemOptions { get; set; } = new();
+        public int AnswerID { get; set; } /// SurveyItemOption selected ID
+        public bool IsNew { get; set; } = true;
+
+        public void AddNewOption(string userID)
+        {
+            int itemID = ID;
+            Console.WriteLine($"SurveyItem AddNewOption. item id={itemID}.");
+            int newTempKey = (SurveyItemOptions.Count > 0) ? SurveyItemOptions.Keys.Max() + 1 : 1;
+            int position = (SurveyItemOptions.Count > 0) ? SurveyItemOptions.Select(o => o.Value.Position).Max() + 1 : 1;
+            Console.WriteLine($"SurveyItem AddNewOption. option newTempKey={newTempKey}.");
+            SurveyItemOption option = new()
+            {
+                ID = newTempKey,
+                ItemID = itemID,
+                Position = position,
+                CreatorID = userID,
+                DateCreated = DateTime.Now
+            };
+            SurveyItemOptions.Add(newTempKey, option);
+        }
+    }
+
+    public class SurveyItemOption
+    {
+        public int ID { get; set; }
+        public int ItemID { get; set; }
+        public string Text { get; set; }
+        public int Rate1 { get; set; }
+        public int Rate2 { get; set; }
+        public int Rate3 { get; set; }
+        public int Rate4 { get; set; }
+        public int Position { get; set; }
+        public string CreatorID { get; set; }
+        public DateTime DateCreated { get; set; }
+        public DateTime DateUpdated { get; set; }
+        public bool IsNew { get; set; } = true;
+    }
+}

+ 3 - 3
Pages/Desktop.razor.cs

@@ -576,15 +576,15 @@ namespace HyperCube.Pages
 
             stringSQL = $"UPDATE articles " +
                 $"SET filename='{_article.Filename}', article_name='{_article.Name}', authors='{_article.Authors}', " +
-                    $"date_publish='{_article.PublishDate:yyyy-MM-dd}', annotation='{_article.Annotation}', " +
-                    $"keywords='{_article.Keywords}', rating={rating}, file_hash='{_article.HashSum}' " +
+                $"date_publish='{_article.PublishDate:yyyy-MM-dd}', annotation='{_article.Annotation}', " +
+                $"keywords='{_article.Keywords}', rating={rating}, file_hash='{_article.HashSum}' " +
                 $"WHERE id={_article.ID}";
             await dbCon.SQLInsert(stringSQL);
 
             ///todo обновлять док в массиве AppData.Articles
 
             stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id) " +
-                    $"VALUES ('{_article.ID}', '{(int)articleStatus}', '{_currentAccount.UUID}')";
+                 $"VALUES ('{_article.ID}', '{(int)articleStatus}', '{_currentAccount.UUID}')";
             await dbCon.SQLInsert(stringSQL);
 
             ///если нужно фиксировать изменение статуса в поле (табл articles_edit_log)

+ 60 - 0
Pages/SurveyEditor.razor

@@ -0,0 +1,60 @@
+@page "/surveyeditor"
+@page "/surveyeditor/{ID:int}"
+
+<div style="margin-left:20px">
+    <h3>Редактор опросов (тестовая квалификаци)</h3>
+
+    <div style="margin:10px; margin-left:0px">
+        <a href="surveys">Вернуться к списку опросов</a>
+        ||
+        <a style="cursor:pointer" @onclick="SaveSurvey">Сохранить опрос</a>
+        ||
+        <a style="cursor:pointer" @onclick="NewSurvey">Создать новый опрос</a>
+    </div>
+
+    @*<p>id: @ID</p>*@
+    <EditForm Model="@_survey">
+        <div>Наименование опроса:</div>
+        <div>
+            <InputText type="text" @bind-Value="_survey.Name" placeholder="Введите наименование" />
+        </div>
+        <div>Описание опроса:</div>
+        <div>
+            <InputTextArea type="text" @bind-Value="_survey.Description" placeholder="Введите описание" />
+        </div>
+        <div>Вопросы:</div>
+        @foreach (var item in _survey.SurveyItems)
+        {
+            @*<div>id: @item.Key</div>*@
+            <div>
+                Вопрос №@item.Value.Position
+                <a style="cursor:pointer" @onclick="ItemMoveUp">&#11014;</a>
+                <a style="cursor:pointer" @onclick="ItemMoveDown">&#11015;</a>
+                <a style="cursor:pointer" @onclick="ItemDelete">&#10006;</a>
+            </div>
+            <div>
+                <InputTextArea type="text" @bind-Value="item.Value.Text" placeholder="Введете текст вопроса" />                
+            </div>
+            @foreach (var option in item.Value.SurveyItemOptions)
+            {
+                <div>
+                    Ответ №@option.Value.Position
+                    <a style="cursor:pointer" @onclick="OptionMoveUp">&#11014;</a>
+                    <a style="cursor:pointer" @onclick="OptionMoveDown">&#11015;</a>
+                    <a style="cursor:pointer" @onclick="OptionDelete">&#10006;</a>
+                </div>
+                <div>
+                    <InputText type="text" @bind-Value="option.Value.Text" placeholder="Введите текст ответа" />
+                </div>
+                <div>
+                    П - <InputNumber @bind-Value="option.Value.Rate1" style="width:40px; text-align:center" />
+                    М - <InputNumber @bind-Value="option.Value.Rate2" style="width:40px; text-align:center" />
+                    Л - <InputNumber @bind-Value="option.Value.Rate3" style="width:40px; text-align:center" />
+                    С - <InputNumber @bind-Value="option.Value.Rate4" style="width:40px; text-align:center" />
+                </div>
+            }
+            <div style="margin-left:20px; cursor:pointer" @onclick="(() => AddNewOption(item.Key))">+ Добавить вариант ответа</div>
+        }
+        <div style="cursor:pointer" @onclick="(() => AddNewItem())">+ Добавить вопрос</div>
+    </EditForm>
+</div>

+ 170 - 0
Pages/SurveyEditor.razor.cs

@@ -0,0 +1,170 @@
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Authorization;
+using Microsoft.AspNetCore.Identity;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using HyperCube.Models;
+using Console = HyperCube.Utils.AdvConsole;
+
+namespace HyperCube.Pages
+{
+    public partial class SurveyEditor : ComponentBase
+    {
+        [Inject]
+        NavigationManager NavigationManager { get; set; }
+        [Inject]
+        AuthenticationStateProvider AuthenticationStateProvider { get; set; }
+        [Inject]
+        UserManager<IdentityUser> UserManager { get; set; }
+
+        [Parameter]
+        public int ID { get; set; }
+
+        AccountModel _currentAccount;
+        Survey _survey = new();
+
+        protected override async Task OnInitializedAsync()
+        {
+            _currentAccount = await GetCurrentAcc();
+
+            if (ID > 0)
+            {
+                var surveys = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveys WHERE id={ID}");
+                if (surveys.Count > 0)
+                {
+                    _survey = new()
+                    {
+                        EvenID = Convert.ToInt32(surveys[0]["eventid"]),
+                        ID = Convert.ToInt32(surveys[0]["id"]),
+                        Name = Convert.ToString(surveys[0]["name"]),
+                        Description = Convert.ToString(surveys[0]["description"]),
+                        DateCreated = Convert.ToDateTime(surveys[0]["date_created"]),
+                        DateUpdated = Convert.ToDateTime(surveys[0]["date_updated"]),
+                        CreatorID = Convert.ToString(surveys[0]["creatorid"]),
+                        IsNew = false
+                    };
+                    Console.WriteLine($"SurveyEditor. load survey id: {_survey.ID}, name: {_survey.Name}");
+                }
+
+                SurveyItem item;
+                SurveyItemOption option;                
+
+                var surveyItems = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitems WHERE surveyid={_survey.ID} ORDER BY position");
+                if (surveyItems.Count > 0)
+                {
+                    foreach (var i in surveyItems)
+                    {
+                        item = new()
+                        {
+                            ID = Convert.ToInt32(i["id"]),
+                            SurveyID = Convert.ToInt32(i["surveyid"]),
+                            DateCreated = Convert.ToDateTime(i["date_created"]),
+                            DateUpdated = Convert.ToDateTime(i["date_updated"]),
+                            CreatorID = Convert.ToString(i["creatorid"]),
+                            Text = Convert.ToString(i["itemtext"]),
+                            Position = Convert.ToInt32(i["position"]),
+                            Required = Convert.ToBoolean(i["required"]),
+                            IsNew = false
+                        };
+                        Console.WriteLine($"SurveyEditor. load survey items id: {item.ID}, position: {item.Position}, name: {item.Text}");
+
+                        var surveyItemOption = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitemoptions WHERE itemid={item.ID} ORDER BY position");
+                        if (surveyItemOption.Count > 0)
+                        {
+                            foreach (var o in surveyItemOption)
+                            {
+                                option = new()
+                                {
+                                    ID = Convert.ToInt32(o["id"]),
+                                    ItemID = Convert.ToInt32(o["itemid"]),
+                                    DateCreated = Convert.ToDateTime(o["date_created"]),
+                                    DateUpdated = Convert.ToDateTime(o["date_updated"]),
+                                    CreatorID = Convert.ToString(o["creatorid"]),
+                                    Text = Convert.ToString(o["optiontext"]),
+                                    Position = Convert.ToInt32(o["position"]),
+                                    Rate1 = Convert.ToInt32(o["rate1"]),
+                                    Rate2 = Convert.ToInt32(o["rate2"]),
+                                    Rate3 = Convert.ToInt32(o["rate3"]),
+                                    Rate4 = Convert.ToInt32(o["rate4"]),
+                                    IsNew = false
+                                };                                
+                                Console.WriteLine($"SurveyEditor. load survey item option id: {option.ID}, position: {option.Position}, name: {option.Text}.");
+
+                                item.SurveyItemOptions.Add(option.ID, option);
+                            }
+                        }
+                        _survey.SurveyItems.Add(item.ID, item);
+                    }
+                }
+            }
+        }
+
+        void AddNewItem() => _survey.AddNewItem(_currentAccount.UUID);
+
+        void AddNewOption(int ItemID) => _survey.SurveyItems[ItemID].AddNewOption(_currentAccount.UUID);
+
+        void NewSurvey()
+        {
+            NavigationManager.NavigateTo("surveyeditor", true);
+        }
+
+        void ItemMoveUp()
+        {
+        }
+
+        void ItemMoveDown()
+        {
+        }
+
+        void ItemDelete()
+        {
+        }
+
+        void OptionMoveUp()
+        {
+        }
+
+        void OptionMoveDown()
+        {
+        }
+
+        void OptionDelete()
+        {
+        }
+
+        async Task SaveSurvey()
+        {
+            await _survey.Save(_currentAccount.UUID);
+        }
+
+        async Task<AccountModel> GetCurrentAcc()
+        {
+            Console.WriteLine($"SurveyEditor GetCurrentAcc");
+
+            if (_currentAccount == null)
+            {
+                var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
+                var user = authState.User;
+
+                if (user.Identity.IsAuthenticated)
+                {
+                    var currentUser = await UserManager.GetUserAsync(user);
+                    var acc = AccountModel.Find(currentUser.Id);
+                    if (acc != null)
+                    {
+                        _currentAccount = AccountModel.Loaded[currentUser.Id];
+                    }
+                    else
+                    {
+                        Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
+                        if (accounts.ContainsKey(currentUser.Id))
+                            _currentAccount = accounts[currentUser.Id];
+                    }
+                }
+            }
+            return _currentAccount;
+        }
+    }
+}

+ 66 - 0
Pages/Surveys.razor

@@ -0,0 +1,66 @@
+@page "/surveys"
+@using HyperCube.Models
+
+@attribute [Authorize]
+
+<h3>Surveys</h3>
+
+<div style="margin:10px">
+    <a href="surveyeditor">Создать новый опрос</a>
+</div>
+
+<div>
+    <table border="1" width="100%">
+        <tr>
+            <th>EvenID</th>
+            <th>ID</th>
+            <th>Name</th>
+            <th>Description</th>
+            <th>DateCreated</th>
+            <th>DateUpdated</th>
+            <th>CreatorID</th>
+        </tr>
+        @foreach (var survey in _surveys)
+        {
+            <tr>
+                <td>@survey.Value.EvenID</td>
+                <td>@survey.Value.ID</td>
+                <td><a href="@($"/surveyeditor/{survey.Key}")">@survey.Value.Name</a></td>
+                <td>@survey.Value.Description</td>
+                <td>@survey.Value.DateCreated</td>
+                <td>@survey.Value.DateUpdated</td>
+                <td>@survey.Value.CreatorID</td>
+            </tr>
+        }
+    </table>
+</div>
+
+@code {
+    Dictionary<int, Survey> _surveys = new();
+
+    protected override async Task OnInitializedAsync()
+    {
+        Survey survey;
+
+        var surveys = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveys;");
+        if (surveys.Count > 0)
+        {
+            foreach (var s in surveys)
+            {
+                survey = new()
+                {
+                    EvenID = Convert.ToInt32(s["eventid"]),
+                    ID = Convert.ToInt32(s["id"]),
+                    Name = Convert.ToString(s["name"]),
+                    Description = Convert.ToString(s["description"]),
+                    DateCreated = Convert.ToDateTime(s["date_created"]),
+                    DateUpdated = Convert.ToDateTime(s["date_updated"]),
+                    CreatorID = Convert.ToString(s["creatorid"])
+                };
+
+                Console.WriteLine($"add survey. id: {survey.ID}, name: {survey.Name}");
+                _surveys.Add(survey.ID, survey);
+            }
+        }
+    }
+}