cats, string theParent)
- {
- string myList = string.Empty;
- string selTxt;
- foreach (var item in cats)
- {
- if (item.CParentId == theParent)
- {
- string subList = MakeCSubList(asForm, indent + indentString, curSelect, ref cats, item.CId);
- switch (asForm)
- {
- case "radioBtn":
- if (item.CId == curSelect)
- selTxt = " checked";
- else
- selTxt = "";
- myList += "\n" +
- " \n" +
- " \n" +
- "
\n";
- if (subList != string.Empty)
- myList += subList;
- break;
- case "DropDown":
- case "DropDownIndented":
- if (item.CId == curSelect)
- selTxt = " selected";
- else
- selTxt = "";
- myList = myList + "";
- if (subList != string.Empty)
- myList += subList;
- break;
- default:
- break;
- }
-
- }
- }
- return myList;
- }
- public string MakeCList(string uid, string asForm, string curSelect)
- {
- DbOps qManager = new();
- List cats = qManager.GetCats(uid);
- switch (asForm)
- {
- case "radioBtn":
- //return "" + MakeCSubList(asForm, "  ", curSelect, ref cats, "/") + "
";
- return "" + MakeCSubList(asForm, "", curSelect, ref cats, "/") + "
";
- case "DropDown":
- return "";
- case "DropDownIndented":
- return "";
- default:
- return "MakeCList form error: " + asForm + "
";
- }
- }
- public Cat SetCat(string uid, string parentTagID, string newTagName)
- {
- Cat myCat = new();
- myCat.CParentId = parentTagID;
- myCat.UID = uid;
- myCat.Txt = newTagName;
- myCat.CId = GetGuid();
- // doesn't map TId fild // myDbConnection.Insert(myTag);
- using var myDbConnection = new MySqlConnection(GetConnectionString());
- try
- {
- myDbConnection.Open();
- int rowsAffected = myDbConnection.Execute("INSERT INTO Cats (CId, UID, CParentID, Txt) VALUES(@CId, @UID, @CParentID, @Txt)", myCat);
- return myCat;
- }
- catch (MySqlException ex)
- {
- Console.WriteLine(ex.Message);
- }
- return null;
- }
-
- #endregion General
-
-
- #region Question
-
- public List GetQuestions()
- {
- using var myDbConnection = new MySqlConnection(GetConnectionString());
- try
- {
- myDbConnection.Open();
- return myDbConnection.GetAll().AsList();
- }
- catch (MySqlException ex)
- {
- Console.WriteLine(ex.Message);
- }
- return null;
- }
-
- public Question GetQuestion(string questionID)
- {
- using var myDbConnection = new MySqlConnection(GetConnectionString());
- try
- {
- myDbConnection.Open();
- return myDbConnection.QueryFirstOrDefault("SELECT * FROM Questions WHERE QuestionID = @questionID", new
- {
- questionID
- });
- }
- catch (MySqlException ex)
- {
- Console.WriteLine(ex.Message);
- }
- return null;
- }
-
- public List GetQuestions(string category)
- {
- using var myDbConnection = new MySqlConnection(GetConnectionString());
- try
- {
- myDbConnection.Open();
- return myDbConnection.Query("SELECT * FROM Questions WHERE Category = @theCat ORDER BY QuestionText ASC", new
- {
- theCat = category
- }).AsList();
- }
- catch (MySqlException ex)
- {
- Console.WriteLine(ex.Message);
- }
- return null;
- }
-
public void DeleteQuestion(string questionID)
{
using var myDbConnection = new MySqlConnection(GetConnectionString());
@@ -391,6 +301,99 @@ namespace Quizzer.Persistens
#region Category
+ private string indentString = " ";
+
+ public string MakeCSubList(string asForm, string indent, string curSelect, ref List cats, string theParent)
+ {
+ string myList = string.Empty;
+ string selTxt;
+ foreach (var item in cats)
+ {
+ if (item.CParentId == theParent)
+ {
+ string subList = MakeCSubList(asForm, indent + indentString, curSelect, ref cats, item.CId);
+ switch (asForm)
+ {
+ case "radioBtn":
+ if (item.CId == curSelect)
+ selTxt = " checked";
+ else
+ selTxt = "";
+ myList += "\n" +
+ " \n" +
+ " \n" +
+ "
\n";
+ if (subList != string.Empty)
+ myList += subList;
+ break;
+ case "DropDown":
+ case "DropDownIndented":
+ if (item.CId == curSelect)
+ selTxt = " selected";
+ else
+ selTxt = "";
+ myList = myList + "";
+ if (subList != string.Empty)
+ myList += subList;
+ break;
+ default:
+ break;
+ }
+
+ }
+ }
+ return myList;
+ }
+ public string MakeCList(string uid, string asForm, string curSelect)
+ {
+ DbOps qManager = new();
+ List cats = qManager.GetCats(uid);
+ switch (asForm)
+ {
+ case "radioBtn":
+ //return "" + MakeCSubList(asForm, "  ", curSelect, ref cats, "/") + "
";
+ return "" + MakeCSubList(asForm, "", curSelect, ref cats, "/") + "
";
+ case "DropDown":
+ return "";
+ case "DropDownIndented":
+ return "";
+ default:
+ return "MakeCList form error: " + asForm + "
";
+ }
+ }
+
+ public Cat SetCat(string uid, string parentTagID, string newTagName)
+ {
+ Cat myCat = new();
+ myCat.CParentId = parentTagID;
+ myCat.UID = uid;
+ myCat.Txt = newTagName;
+ myCat.CId = GetGuid();
+ // doesn't map TId fild // myDbConnection.Insert(myTag);
+ using var myDbConnection = new MySqlConnection(GetConnectionString());
+ try
+ {
+ myDbConnection.Open();
+ int rowsAffected = myDbConnection.Execute("INSERT INTO Cats (CId, UID, CParentID, Txt) VALUES(@CId, @UID, @CParentID, @Txt)", myCat);
+ return myCat;
+ }
+ catch (MySqlException ex)
+ {
+ Console.WriteLine(ex.Message);
+ }
+ return null;
+ }
+
public List GetCats(string uid)
{
using var myDbConnection = new MySqlConnection(GetConnectionString());
@@ -659,6 +662,12 @@ namespace Quizzer.Persistens
#region Journal
+ ///
+ /// Get the journal for a specific quiz and a specific user. Returns null if no journal exists for that quiz and user.
+ ///
+ ///
+ ///
+ ///
public Journal GetJournal(string thePID, string theQuizzID)
{
string sql = "SELECT * FROM Journals WHERE PID = '" + thePID + "' AND QuizzID = '" + theQuizzID + "'";
@@ -680,6 +689,11 @@ namespace Quizzer.Persistens
}
}
+ ///
+ /// Get all journals for a specific user. Returns null if no journal exists for that user.
+ ///
+ ///
+ ///
public List GetJournal(string thePID)
{
string sql = "SELECT * FROM Journals WHERE PID = '" + thePID + "'";
@@ -697,6 +711,10 @@ namespace Quizzer.Persistens
}
}
+ ///
+ /// Update journal. Journal is identified by PID and QuizzID. If no journal exists for that PID and QuizzID, nothing happens.
+ ///
+ ///
public void UpdateJournal(Journal theJournal)
{
using var myDbConnection = new MySqlConnection(GetConnectionString());
@@ -720,6 +738,10 @@ namespace Quizzer.Persistens
}
}
+ ///
+ /// Insert journal. If a journal already exists for that PID and QuizzID, nothing happens.
+ ///
+ ///
public void PostJournal(Journal theJournal)
{
using var myDbConnection = new MySqlConnection(GetConnectionString());
@@ -745,6 +767,11 @@ namespace Quizzer.Persistens
}
}
+ ///
+ /// Get all journals for a specific quiz. Returns null if no journal exists for that quiz.
+ ///
+ ///
+ ///
public List GetJournals(string quizzID)
{
using var myDbConnection = new MySqlConnection(GetConnectionString());
@@ -760,6 +787,10 @@ namespace Quizzer.Persistens
}
}
+ ///
+ /// Deletes the journal entry associated with the specified quiz identifier.
+ ///
+ /// The unique identifier of the quiz whose journal entry is to be deleted.
public void DeleteJournalByQuizz(string quizzID)
{
using var myDbConnection = new MySqlConnection(GetConnectionString());
@@ -773,6 +804,12 @@ namespace Quizzer.Persistens
Console.WriteLine(ex.Message);
}
}
+
+ ///
+ /// Deletes the journal entry associated with the specified participant identifier and quiz identifier.
+ ///
+ ///
+ ///
public void DeleteJournal(string pID, string quizzID)
{
using var myDbConnection = new MySqlConnection(GetConnectionString());
@@ -787,6 +824,171 @@ namespace Quizzer.Persistens
#endregion Journal
+ #region Journal2
+
+ // JOURNAL2 is an alternative journal table with same structure as Journal, but with different name. This is used for testing purposes, to avoid conflicts with the original Journal table. It is not used in production.
+
+ ///
+ /// Get the journal for a specific quiz and a specific user. Returns null if no journal exists for that quiz and user.
+ ///
+ ///
+ ///
+ ///
+ public Journal2 GetJournal2(string thePID, string theQuizzID)
+ {
+ string sqlJ = $"SELECT * FROM Journal2s WHERE PID = '{thePID}' AND QuizzID = '{theQuizzID}'";
+ string sqlI = $"SELECT * FROM Journal2Items WHERE PID = '{thePID}' AND QuizzID = '{theQuizzID}'";
+ using var myDbConnection = new MySqlConnection(GetConnectionString());
+ try
+ {
+ myDbConnection.Open();
+ // Get the journal entry, general part.
+ var theJournal = myDbConnection.QueryFirstOrDefault(sqlJ);
+ if (theJournal == null) return null;
+ // Get the journal items.
+ theJournal.Items = myDbConnection.Query(sqlI).AsList();
+ //theJournal.Items.Sort((x, y) => x.QuestionID.CompareTo(y.QuestionID));
+ if (theJournal.Items != null)
+ {
+ foreach (var item in theJournal.Items)
+ {
+ item.ReplyMC = JsonConvert.DeserializeObject>(item.ReplyMCJson);
+ }
+ }
+ return theJournal;
+ }
+ catch
+ {
+ myDbConnection.Close();
+ CreateIfNotExist(myDbConnection); // must be in cosed state
+ return null;
+ }
+ }
+
+ ///
+ /// Get all journals for a specific user. Returns null if no journal exists for that user.
+ ///
+ ///
+ ///
+ public List GetJournal2(string thePID)
+ {
+ string sqlJ = $"SELECT * FROM Journal2s WHERE PID = '{thePID}'";
+ using var myDbConnection = new MySqlConnection(GetConnectionString());
+ try
+ {
+ myDbConnection.Open();
+ List result = myDbConnection.Query(sqlJ).AsList();
+ return result;
+ }
+ catch (MySqlException ex)
+ {
+ Console.WriteLine(ex.Message);
+ return null;
+ }
+ }
+
+ ///
+ /// Update journal. Journal is identified by PID and QuizzID. If no journal exists for that PID and QuizzID, nothing happens.
+ ///
+ ///
+ public void UpdateJournal2(Journal2 theJournal)
+ {
+ using var myDbConnection = new MySqlConnection(GetConnectionString());
+ try
+ {
+ myDbConnection.Open();
+ myDbConnection.Execute("UPDATE Journal2s SET IsOpen = @IsOpen, Score = @Score WHERE PID=@PID AND QuizzID = @QuizzID", theJournal);
+ foreach (var item in theJournal.Items)
+ {
+ item.ReplyMCJson = JsonConvert.SerializeObject(item.ReplyMC);
+ myDbConnection.Execute("UPDATE Journal2Items SET IsOpen = @IsOpen, Score = @Score, MaxScore = @MaxScore, ReplyMCJson = @ReplyMCJson WHERE PID = @PID AND QuizzID = @QuizzID AND QuestionID = @QuestionID", item);
+ }
+ }
+ catch (MySqlException ex)
+ {
+ Console.WriteLine(ex.Message);
+ return;
+ }
+ }
+
+ ///
+ /// Insert journal. If a journal already exists for that PID and QuizzID, nothing happens.
+ ///
+ ///
+ public void PostJournal2(Journal2 theJournal)
+ {
+ using var myDbConnection = new MySqlConnection(GetConnectionString());
+ try
+ {
+ myDbConnection.Open();
+ myDbConnection.Execute("INSERT INTO Journal2s (PID, QuizzID, Category, Anonymous, IsOpen, Score, ScoreAcc) VALUES(@PID,@QuizzID,@Category,@Anonymous,@IsOpen,@Score,@ScoreAcc)", new
+ {
+ PID = theJournal.PID,
+ QuizzID = theJournal.QuizzID,
+ Category = theJournal.Category,
+ Anonymous = theJournal.Anonymous,
+ IsOpen = theJournal.IsOpen,
+ Score = theJournal.Score,
+ ScoreAcc = theJournal.ScoreAcc,
+ });
+
+ // Must populate the Journal2Items table with the items of the journal
+ if (theJournal.Items != null)
+ {
+ foreach (var item in theJournal.Items)
+ {
+ item.PID = theJournal.PID;
+ item.QuizzID = theJournal.QuizzID;
+ item.ReplyMCJson = JsonConvert.SerializeObject(item.ReplyMC);
+ myDbConnection.Execute("INSERT INTO Journal2Items (PID, QuizzID, QuestionID, IsOpen, Score, MaxScore, ReplyMCJson) VALUES(@PID, @QuizzID, @QuestionID, @IsOpen, @Score, @MaxScore, @ReplyMCJson)", item);
+ }
+ }
+ }
+ catch (MySqlException ex)
+ {
+ Console.WriteLine(ex.Message);
+ return;
+ }
+ }
+
+ ///
+ /// Deletes the journal entry associated with the specified quiz identifier.
+ ///
+ /// The unique identifier of the quiz whose journal entry is to be deleted.
+ public void DeleteJournalByQuizz2(string quizzID)
+ {
+ using var myDbConnection = new MySqlConnection(GetConnectionString());
+ try
+ {
+ myDbConnection.Open();
+ myDbConnection.Delete(quizzID);
+ }
+ catch (MySqlException ex)
+ {
+ Console.WriteLine(ex.Message);
+ }
+ }
+
+ ///
+ /// Deletes the journal entry associated with the specified participant identifier and quiz identifier.
+ ///
+ ///
+ ///
+ public void DeleteJournal2(string pID, string quizzID)
+ {
+ using var myDbConnection = new MySqlConnection(GetConnectionString());
+ try { myDbConnection.Open(); }
+ catch (MySqlException ex)
+ {
+ Console.WriteLine(ex.Message);
+ return;
+ }
+ myDbConnection.Execute("DELETE FROM Journal2s WHERE PID = @PID AND QuizzID = @QuizzID", new { PID = pID, QuizzID = quizzID });
+ myDbConnection.Execute("DELETE FROM Journal2Items WHERE PID = @PID AND QuizzID = @QuizzID", new { PID = pID, QuizzID = quizzID });
+ }
+
+ #endregion Journal2
+
#region Statistic