Skip to content

Commit

Permalink
Refactored and fixed notification creation (haven't tested sending)
Browse files Browse the repository at this point in the history
  • Loading branch information
allomanta committed Sep 19, 2024
1 parent c06eccf commit 4710fdd
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 116 deletions.
13 changes: 13 additions & 0 deletions IguideME.Web/Services/Constants/DatabaseQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,19 @@ INNER JOIN `assignments`
USING (`assignment_id`)
WHERE `assignments`.`course_id`=@courseID
AND `submissions`.`user_id`=@userID
;";

public const string QUERY_TILE_SUBMISSIONS_FOR_STUDENT =
@"SELECT `submissions`.`submission_id`,
`submissions`.`assignment_id`,
`submissions`.`user_id`,
`submissions`.`Grade`,
`submissions`.`date`
FROM `submissions`
INNER JOIN `tile_entries`
ON `submissions`.`assignment_id` = `tile_entries`.`content_id`
WHERE `tile_entries`.`tile_id`=@tileID
AND `submissions`.`user_id`=@userID
;";

public const string QUERY_ALL_USER_SUBMISSIONS_FOR_TILE =
Expand Down
18 changes: 6 additions & 12 deletions IguideME.Web/Services/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ long syncID
// When it is done by the user, we get the last available syncID, to replace the settings instead of register them
long activeSync = syncID == 0 ? this.GetCurrentSyncID(courseID) : syncID;
User tempUser = new User(userID, courseID, -1, "", "-1", (int)UserRoles.student);
_logger.LogInformation("Updating settings for user {} for course {}: consent {} notifications () goal {} total {}", userID, courseID, consent, notifications, goalGrade, totalGrade);
// _logger.LogInformation("Updating settings for user {} for course {}: consent {} notifications {} goal {} total {}", userID, courseID, consent, notifications, goalGrade, totalGrade);

using (
SQLiteDataReader r = Query(
Expand Down Expand Up @@ -1242,7 +1242,7 @@ long syncID
}
}

_logger.LogInformation("Actually updating user Notifications to {}", tempUser.Settings.Notifications);
// _logger.LogInformation("Actually updating user Notifications to {}", tempUser.Settings.Notifications);

NonQuery(
DatabaseQueries.REGISTER_STUDENT_SETTINGS,
Expand Down Expand Up @@ -1876,9 +1876,8 @@ public List<PredictedGrade> GetPredictedGrades(int courseID, string userID)
return predictions;
}

// TODO: probably switch to using grades version of submissions instead of Grade.
public List<AssignmentSubmission> GetTileSubmissionsForUser(
int courseID,
int tileID,
string userID,
long syncID = 0
)
Expand All @@ -1887,9 +1886,9 @@ public List<AssignmentSubmission> GetTileSubmissionsForUser(

using (
SQLiteDataReader r1 = Query(
DatabaseQueries.QUERY_COURSE_SUBMISSIONS_FOR_STUDENT,
DatabaseQueries.QUERY_TILE_SUBMISSIONS_FOR_STUDENT,
new SQLiteParameter("userID", userID),
new SQLiteParameter("courseID", courseID)
new SQLiteParameter("tileID", tileID)
)
)
{
Expand All @@ -1900,18 +1899,13 @@ public List<AssignmentSubmission> GetTileSubmissionsForUser(
r1.GetInt32(0),
r1.GetInt32(1),
r1.GetValue(2).ToString(),
r1.GetInt32(3),
r1.GetDouble(3),
r1.GetInt32(4)
);
submissions.Add(submission);
}
}

foreach (AssignmentSubmission sub in submissions)
{
sub.Meta = GetEntryMeta(sub.ID);
}

return submissions;
}

Expand Down
209 changes: 105 additions & 104 deletions IguideME.Web/Services/Workers/PeerGroupWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,119 +378,120 @@ void CreatePeerGroups()

CalculateGrades(peerGroup, tiles);
StorePeerStatistics(goalGrade, peerGroup);
CreateNotifications(groupedUsers[goalGrade], peerEntryGradesMap);
CreateNotifications(groupedUsers[goalGrade]);
}
}

void CreateNotifications(List<string> users, Dictionary<int, List<double>> grades)
void CreateNotifications(List<string> users)
{
this._logger.LogInformation("Creating Notifications for Peer Group");

foreach (string user in users)
{
_logger.LogInformation("Creating notification for user {}", user);
CreateNotificationsForUser(user);
}
}

foreach (KeyValuePair<int, List<double>> entry in grades)
{
// Get only tiles with notifications
if (_databaseManager.GetTileNotificationState(entry.Key))
{
_logger.LogInformation("Handling tile {}", entry.Key);

List<AssignmentSubmission> userTileSubmissions =
_databaseManager.GetTileSubmissionsForUser(
entry.Key,
user,
this._syncID
);

// Find the submission with the highest ID, as it is the most recent
int lastSubmissionID = -1;
foreach (AssignmentSubmission submission in userTileSubmissions)
if (submission.ID > lastSubmissionID)
lastSubmissionID = submission.ID;

// Create one list with all the submission grades and one more without the most recent submission
List<double> currentSubmissionGrades = new();
List<double> lastSubmissionGrades = new();
foreach (AssignmentSubmission submission in userTileSubmissions)
{
currentSubmissionGrades.Add(submission.Grade);
if (submission.ID != lastSubmissionID)
lastSubmissionGrades.Add(submission.Grade);
}

double currentAverage = -1;
double lastAverage = -1;
double peerAverage = -1;
// Store the three important Averages in variables
if (currentSubmissionGrades.Count != 0)
currentAverage = currentSubmissionGrades.Average();
if (lastSubmissionGrades.Count != 0)
lastAverage = lastSubmissionGrades.Average();
if (entry.Value != null)
peerAverage = entry.Value.Average();

if (currentAverage != -1 && peerAverage != 0)
{
if (currentAverage >= peerAverage) // +1)
{
// outperform
_databaseManager.RegisterNotification(
this._courseID,
user,
entry.Key,
(int)Notification_Types.outperforming,
this._syncID
);
}
// else if (currentAverage >= peerAverage)
// {
// // do nothing
// }
else if (currentAverage - lastAverage > 0)
{
// closing the gap
_databaseManager.RegisterNotification(
this._courseID,
user,
entry.Key,
(int)Notification_Types.closing_gap,
this._syncID
);
}
else if (
(currentAverage - lastAverage <= 0)
&& (peerAverage - currentAverage <= 1)
)
{
// falling behind
_databaseManager.RegisterNotification(
this._courseID,
user,
entry.Key,
(int)Notification_Types.falling_behind,
this._syncID
);
}
else if (
(currentAverage - lastAverage <= 0)
&& (peerAverage - currentAverage > 1)
)
{
// put more effort
_databaseManager.RegisterNotification(
this._courseID,
user,
entry.Key,
(int)Notification_Types.more_effort,
this._syncID
);
}
}
}
}
void CreateNotificationsForUser(string user)
{
_logger.LogInformation("Creating notification for user {}", user);
foreach ((int tileID, List<double> peerGrades) in this.peerTileGradesMap)
{
_logger.LogInformation("test {} {}", _databaseManager.GetTileNotificationState(tileID), tileID);

// Get only tiles with notifications
if (_databaseManager.GetTileNotificationState(tileID))
{
CreateTileNotifications(user, tileID, peerGrades);
}
}
}

void CreateTileNotifications(string user, int tileID, List<double> peerGrades)
{
_logger.LogInformation("Handling tile {}", tileID);

if (!peerGrades.Any())
{
_logger.LogInformation("No peer grades found");
return;
}

List<AssignmentSubmission> submissions = _databaseManager.GetTileSubmissionsForUser(
tileID,
user,
this._syncID
);

if (!submissions.Any())
{
_logger.LogInformation("No submissions found.");
return;
}

// Sort the submissions by their submission id. Since these are autoincrement this means that the most
// recent addition will be last.
submissions.Sort((A, B) => A.ID.CompareTo(B.ID));


List<double> subGrades = submissions.Select(sub => sub.Grade).ToList();
IEnumerable<double> oldSubmissions = subGrades.Take(submissions.Count - 1);

// Average means tile grade.
double currentAverage = subGrades.Average();
double lastAverage = oldSubmissions.Any() ? oldSubmissions.Average() : -1;
double peerAverage = peerGrades.Any() ? peerGrades.Average() : -1;

// Outperforming
if (currentAverage >= peerAverage)
{
_databaseManager.RegisterNotification(
this._courseID,
user,
tileID,
(int)Notification_Types.outperforming,
this._syncID
);
}
// Closing the gap
else if (currentAverage - lastAverage > 0)
{
_databaseManager.RegisterNotification(
this._courseID,
user,
tileID,
(int)Notification_Types.closing_gap,
this._syncID
);
}
// Falling behind
else if (
(currentAverage - lastAverage <= 0)
&& (peerAverage - currentAverage <= 1)
)
{
_databaseManager.RegisterNotification(
this._courseID,
user,
tileID,
(int)Notification_Types.falling_behind,
this._syncID
);
}
// Have to put more effort
else if (
(currentAverage - lastAverage <= 0)
&& (peerAverage - currentAverage > 1)
)
{
_databaseManager.RegisterNotification(
this._courseID,
user,
tileID,
(int)Notification_Types.more_effort,
this._syncID
);
}

}
}
}

0 comments on commit 4710fdd

Please sign in to comment.