Browse Source

survey user form (1)

ganahrhr 2 years ago
parent
commit
fc3f9a8d73
6 changed files with 205 additions and 106 deletions
  1. 100 0
      Models/Survey.cs
  2. 4 2
      Pages/Desktop.razor
  3. 6 0
      Pages/Desktop.razor.cs
  4. 85 0
      Pages/ModalSurvey.razor
  5. 1 1
      Pages/SurveyEditor.razor
  6. 9 103
      Pages/SurveyEditor.razor.cs

+ 100 - 0
Models/Survey.cs

@@ -16,9 +16,95 @@ namespace HyperCube.Models
         public DateTime DateUpdated { get; set; }
         public string CreatorID { get; set; }
         public Dictionary<int, SurveyItem> SurveyItems { get; set; } = new();
+        public List<KeyValuePair<int, SurveyItem>> SurveyItemsSorted { get; set; } = new(); /// wrap for sorting
         public bool IsNew { get; set; } = true;
         public bool IsDeleted { get; set; } = false;
 
+        public async Task LoadByID(int id)
+        {
+            string sql = $"SELECT * FROM surveys WHERE id={id}";
+            await Load(sql);
+        }
+
+        public async Task LoadByEventID(int eventID)
+        {
+            string sql = $"SELECT * FROM surveys WHERE eventid={eventID}";
+            await Load(sql);
+        }
+
+        private async Task Load(string sql)
+        {            
+            var surveys = await MySQLConnector.Instance().SQLSelectComplex(sql);
+            if (surveys.Count > 0)
+            {
+                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($"Loading Survey, ID: {ID}, Name: {Name}");
+            }
+
+            SurveyItem item;
+            SurveyItemOption option;
+            int itemPosition = 1;
+            int optionPosition = 1;
+
+            var surveyItems = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitems WHERE surveyid={ID} AND deleted<>1 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 = itemPosition++,
+                        Required = Convert.ToBoolean(i["required"]),
+                        IsNew = false
+                    };
+                    Console.WriteLine($"Loading SurveyItem, survey: {item.SurveyID}, item id: {item.ID}, position: {item.Position}, name: {item.Text}");
+
+                    var surveyItemOption = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitemoptions WHERE itemid={item.ID} AND deleted<>1 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 = optionPosition++,
+                                Rate1 = Convert.ToInt32(o["rate1"]),
+                                Rate2 = Convert.ToInt32(o["rate2"]),
+                                Rate3 = Convert.ToInt32(o["rate3"]),
+                                Rate4 = Convert.ToInt32(o["rate4"]),
+                                IsNew = false
+                            };
+                            Console.WriteLine($"Loading SurveyItemOption, survey: {item.SurveyID}, item {item.ID}, option id: {option.ID}, position: {option.Position}, name: {option.Text}.");
+
+                            item.SurveyItemOptions.Add(option.ID, option);
+                        }
+                    }
+                    SurveyItems.Add(item.ID, item);
+                    item.SortOptions();
+                }
+                SortItems();
+            }
+        }
+
         public async Task<int> Save(string userID)
         {
             long newID;
@@ -159,6 +245,13 @@ namespace HyperCube.Models
                 }
             }
         }
+
+        public void SortItems()
+        {
+            int itemPosition = 1;
+            SurveyItemsSorted = SurveyItems.OrderBy(i => i.Value.Position).ToList();
+            foreach (var item in SurveyItemsSorted) { item.Value.Position = itemPosition++; }
+        }
     }
 
     public class SurveyItem : ICloneable
@@ -261,6 +354,13 @@ namespace HyperCube.Models
             }
         }
 
+        public void SortOptions()
+        {
+            int optionPosition = 1;
+            SurveyItemOptionsSorted = SurveyItemOptions.OrderBy(i => i.Value.Position).ToList();
+            foreach (var item in SurveyItemOptionsSorted) { item.Value.Position = optionPosition++; }
+        }
+
         public object Clone()
         {
             return MemberwiseClone();

+ 4 - 2
Pages/Desktop.razor

@@ -68,7 +68,7 @@
                 </div>
             </div>
             <div class="first-block__inforamtion__button">
-                <button class="btn_grey">Подать заявку</button>
+                <button class="btn_grey" @onclick="RequestParticipation">Подать заявку</button>
                 <button class="btn_grey">Сохранить</button>
                 <button class="btn_grey">Поделиться</button>
             </div>
@@ -244,7 +244,9 @@
         <Body>
         </Body>
     </ModalLoading>
-
+    <ModalSurvey @ref="_modalSurvey">
+        <Title></Title>
+    </ModalSurvey>
 </div>
 
 @if (_article?.ID > 0 || _article?.NounGroups?.Length > 0)

+ 6 - 0
Pages/Desktop.razor.cs

@@ -61,6 +61,7 @@ namespace HyperCube.Pages
         MemoryStream _memoryStream;
         ModalInfo _modalInfo { get; set; }
         ModalLoading _modalLoading { get; set; }
+        ModalSurvey _modalSurvey { get; set; }
 
         ArticleModel _articleClone;
         ArticleModel _article;
@@ -794,6 +795,11 @@ namespace HyperCube.Pages
             return result;
         }
 
+        async Task RequestParticipation()
+        {
+            await _modalSurvey.Open(_currentAccount.Name, 0);
+        }
+
         static string GetDisplayName(Enum enumValue)
         {
             return enumValue.GetType()

+ 85 - 0
Pages/ModalSurvey.razor

@@ -0,0 +1,85 @@
+@using HyperCube.Models
+
+<div class="modal" tabindex="-1" role="dialog" style="display:none" id="@elementid">
+    <div class="modal-container">
+        <div class="modal__body">
+            <div class="modal__body__header modal__body__header_noflex">
+                <div class="modal__qual__header">
+                    <p class="modal__qual__header">Квалификация</p>
+                </div>
+            </div>
+            <div class="modal__body__container modal__body__container_aligncenter">
+                <div class="modal__body__container__left modal__body__container__left_blackborder">
+                    <p class="modal__qual__info">Здравствуй, @_userName</p>
+                    <p class="modal__qual__info">Что бы участвовать в Цифровом хакатоне вам необходимо ответить на вопросы теста.</p>
+                    <p class="modal__qual__info">Всего вопросов будет @_survey.SurveyItems.Count. И твои ответы сформируют уникальную матрицу компетенций, которая и определит твою роль в Цифровом хакатоне.</p>
+                    <p class="modal__qual__info">Правила найдешь в библиотеке.</p>
+                </div>
+                <div class="modal__body__container__right modal__body__container__right_blackborder">
+                    @if (_survey.SurveyItems.Count > 0)
+                    {
+                        <div class="modal__qual__question">
+                            Вопрос №@_currentQuestionPosition<br>
+                            @_survey.SurveyItems[_currentQuestionID].Text
+
+                            @foreach (var option in _survey.SurveyItems[_currentQuestionID].SurveyItemOptionsSorted)
+                            {
+                                <div>
+                                    @(PositionToLetter(option.Value.Position)):  @(option.Value.Text)<br>
+                                </div>
+                            }
+                        </div>
+                        <div class="modal__qual__button">
+                            <div class="modal__qual__button__box">
+                                <button class="modal__qual__button__box__item" @onclick="(() => Answer(1))">А</button>
+                                <button class="modal__qual__button__box__item" @onclick="(() => Answer(2))">Б</button>
+                                <button class="modal__qual__button__box__item" @onclick="(() => Answer(3))">В</button>
+                            </div>
+                            @*<button class="modal__qual__button__item">Не знаю</button>*@
+                        </div>
+                    }
+                </div>
+            </div>
+        </div>
+        <a class="modal_close" style="cursor:pointer" @onclick="@Close">&#10006;</a>
+    </div>
+</div>
+
+@code {
+    [Inject]
+    public IJSRuntime JsRuntime { get; set; }
+
+    [Parameter]
+    public RenderFragment Title { get; set; }
+
+    string elementid = "modal_survey";
+    string _userName = "[UNKNOWN_USERNAME]";
+    Survey _survey = new();
+    int _currentQuestionPosition = 1;
+    int _currentQuestionID = 1;
+
+
+    public async Task Open(string userName, int eventID)
+    {
+        _userName = userName;
+        await _survey.LoadByEventID(eventID);
+        _currentQuestionID = _survey.SurveyItemsSorted.FirstOrDefault(i => i.Value.Position == _currentQuestionPosition).Key;
+        await JsRuntime.InvokeVoidAsync("OpenModal", elementid);
+    }
+
+    public void Close()
+    {
+        JsRuntime.InvokeVoidAsync("CloseModal", elementid);
+    }
+
+    void Answer(int position)
+    {
+
+    }
+
+    string PositionToLetter(int position)
+    {
+        char character = Char.Parse("А");
+        return ((char)(position - 1 + character)).ToString();
+    }
+}

+ 1 - 1
Pages/SurveyEditor.razor

@@ -22,7 +22,7 @@
             <InputTextArea type="text" @bind-Value="_survey.Description" placeholder="Введите описание" />
         </div>
         <div style="border:2px solid #3a3838; margin:5px"></div>
-        @foreach (var item in _sortedItems)
+        @foreach (var item in _survey.SurveyItemsSorted)
         {
             if (!item.Value.IsDeleted)
             {

+ 9 - 103
Pages/SurveyEditor.razor.cs

@@ -23,9 +23,7 @@ namespace HyperCube.Pages
         public int ID { get; set; }
 
         AccountModel _currentAccount;
-        Survey _survey = new();
-
-        List<KeyValuePair<int, SurveyItem>> _sortedItems = new(); /// wrap for items sorting        
+        Survey _survey = new();      
 
         ModalInfo _modalInfo;
 
@@ -33,86 +31,8 @@ namespace HyperCube.Pages
         {
             _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;
-                int itemPosition = 1;
-                int optionPosition = 1;
-
-                var surveyItems = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitems WHERE surveyid={_survey.ID} AND deleted<>1 ORDER BY position");
-                if (surveyItems.Count > 0)
-                {
-                    Console.WriteLine($"surveyItems.Count={surveyItems.Count}");
-
-                    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"]),
-                            Position = itemPosition++,
-                            Required = Convert.ToBoolean(i["required"]),
-                            IsNew = false
-                        };
-                        Console.WriteLine($"SurveyEditor. load survey [{item.SurveyID}] item id: {item.ID}, position: {item.Position}, name: {item.Text}");
-
-                        var surveyItemOption = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitemoptions WHERE itemid={item.ID} AND deleted<>1 ORDER BY position");
-                        if (surveyItemOption.Count > 0)
-                        {
-                            Console.WriteLine($"surveyItemOption.Count={surveyItemOption.Count}");
-
-                            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"]),
-                                    Position = optionPosition++,
-                                    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.SurveyID}] item [{item.ID}] option id: {option.ID}, position: {option.Position}, name: {option.Text}.");
-
-                                item.SurveyItemOptions.Add(option.ID, option);
-                            }                            
-                        }
-                        _survey.SurveyItems.Add(item.ID, item);
-                        SortOptions(item.ID);
-                    }
-                    SortItems();
-                }                               
-            }
+            if (ID > 0)            
+                await _survey.LoadByID(ID);
         }
 
         void NewSurvey() => NavigationManager.NavigateTo("surveyeditor", true);
@@ -120,37 +40,37 @@ namespace HyperCube.Pages
         void AddNewItem()
         {
             _survey.AddNewItem(_currentAccount.UUID);
-            SortItems();
+            _survey.SortItems();
         }
 
         void ItemMove(int itemID, int step)
         {
             _survey.MoveItem(itemID, step);
-            SortItems();
+            _survey.SortItems();
         }
 
         void ItemDelete(int itemID)
         {
             _survey.DeleteItem(itemID);
-            SortItems();
+            _survey.SortItems();
         }
 
         void AddNewOption(int itemID)
         {
             _survey.SurveyItems[itemID].AddNewOption(_currentAccount.UUID);
-            SortOptions(itemID);
+            _survey.SurveyItems[itemID].SortOptions();
         } 
 
         void OptionMove(int itemID, int optionID, int step)
         {
             _survey.SurveyItems[itemID].MoveItem(optionID, step);
-            SortOptions(itemID);
+            _survey.SurveyItems[itemID].SortOptions();
         }
 
         void OptionDelete(int itemID, int optionID)
         {
             _survey.SurveyItems[itemID].DeleteOption(optionID);
-            SortOptions(itemID);
+            _survey.SurveyItems[itemID].SortOptions();
         }
 
         async Task SaveSurvey()
@@ -159,20 +79,6 @@ namespace HyperCube.Pages
             _modalInfo.Open("Опрос сохранен.");
         }
 
-        void SortItems()
-        {
-            int itemPosition = 1;
-            _sortedItems = _survey.SurveyItems.OrderBy(i => i.Value.Position).ToList();
-            foreach (var item in _sortedItems) { item.Value.Position = itemPosition++; }
-        }
-
-        void SortOptions(int itemID)
-        {
-            int optionPosition = 1;
-            _survey.SurveyItems[itemID].SurveyItemOptionsSorted = _survey.SurveyItems[itemID].SurveyItemOptions.OrderBy(i => i.Value.Position).ToList();
-            foreach (var item in _survey.SurveyItems[itemID].SurveyItemOptionsSorted) { item.Value.Position = optionPosition++; }
-        }
-
         async Task<AccountModel> GetCurrentAcc()
         {
             Console.WriteLine($"SurveyEditor GetCurrentAcc");