From ff17665cb0caa0a23e2fdf4fdedcc70c686aa9b5 Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 7 Jun 2023 15:00:53 +0200 Subject: [PATCH] Some completion stuff --- src/reportmonster/classes/vessel.py | 29 +++++++++++++++++++++++++---- src/reportmonster/const.py | 4 +++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/reportmonster/classes/vessel.py b/src/reportmonster/classes/vessel.py index 092e351..977c7f2 100644 --- a/src/reportmonster/classes/vessel.py +++ b/src/reportmonster/classes/vessel.py @@ -1,13 +1,15 @@ -from .database import Database -from ..const import * +from classes.database import Database from configparser import SectionProxy from typing import Optional, Union from datetime import datetime from MySQLdb.cursors import DictCursor +from MySQLdb._exceptions import IntegrityError from bcrypt import hashpw, gensalt +from const import * + class Vessel: """Class describing a Vessel @@ -77,6 +79,9 @@ class Vessel: def connect(self): return Database(self) + def reconnect(self): + self.db = self.connect() + @staticmethod def getTimestamp() -> int: return int(datetime.now().timestamp()) @@ -195,7 +200,7 @@ class Vessel: def setPassword(self, username: str, password: str): hashed = hashpw(password.encode(), gensalt(prefix=b"2b")) query = QUERY_USER_SET_PASSWORD - self.db._execute(query, (password, username)) + self.db._execute(query, (hashed, username)) def getEnrols(self, enrol: Optional[str] = None): results = list(self.db._execute(QUERY_ENROL, ctype=DictCursor)) @@ -247,6 +252,9 @@ class Vessel: email = email or f"{self.getUsers(id=userid)[userid]['username']}@pin.seachefsacademy.com" self.db._execute(QUERY_USER_SET_EMAIL, (email, userid)) + def setName(self, userid: int, first: str, last: str): + self.db._execute(QUERY_USER_SET_NAME, (first, last, userid)) + def getCustomCourseFields(self): results = list(self.db._execute(QUERY_COURSE_FIELDS, ctype=DictCursor)) return results @@ -264,5 +272,18 @@ class Vessel: results = list(self.db._execute(QUERY_MODULE_COMPLETION, (moduleid,), ctype=DictCursor)) return results + def setCourseModuleCompletion(self, moduleid: int, userid: int): + try: + self.db._execute(QUERY_INSERT_MODULE_COMPLETION, (moduleid, userid, Vessel.getTimestamp())) + except IntegrityError: + pass # Module completion record already exists + self.db._execute(QUERY_UPDATE_MODULE_COMPLETION, (moduleid, userid)) + + def setCourseCompletion(self, courseid: int, userid: int): + modules = self.getCourseModules(courseid) + + for module in modules: + self.setCourseModuleCompletion(module["id"], userid) + def writeLog(self, event, data): - self.db._execute(QUERY_LOG_INSERT, (event, data)) \ No newline at end of file + self.db._execute(QUERY_LOG_INSERT, (event, data)) diff --git a/src/reportmonster/const.py b/src/reportmonster/const.py index c60de82..cb00f7a 100644 --- a/src/reportmonster/const.py +++ b/src/reportmonster/const.py @@ -9,11 +9,11 @@ QUERY_USER_INFO_FIELD = "SELECT * FROM mdl_user_info_field" QUERY_USER_INFO_DATA = "SELECT * FROM mdl_user_info_data" QUERY_USER_SET_PASSWORD = "UPDATE mdl_user SET password = %s WHERE username = %s" QUERY_USER_SET_EMAIL = "UPDATE mdl_user SET email = %s WHERE id = %s" +QUERY_USER_SET_NAME = "UPDATE mdl_user SET firstname = %s, lastname = %s WHERE id = %s" QUERY_USER_CREATE = "INSERT INTO mdl_user(username, email, firstname, lastname, timecreated, timemodified, mnethostid, confirmed) VALUES (%s, %s, %s, %s, %s, %s, 1, 1)" QUERY_ASSIGN_ROLE = "INSERT INTO mdl_role_assignments(roleid, contextid, userid, timemodified) VALUES (%s, %s, %s, %s)" QUERY_GET_ROLE = "SELECT * FROM mdl_role_assignments WHERE contextid = %s AND userid = %s" QUERY_GET_USERID = "SELECT * FROM mdl_user WHERE username = %s" -QUERY_USER_GET_PASSWORD = "SELECT password FROM mdl_user WHERE id = %s" QUERY_COURSE = "SELECT * FROM mdl_course" QUERY_ENROL = "SELECT * FROM mdl_enrol" @@ -25,6 +25,8 @@ QUERY_COURSE_CONTEXT_REVERSE = "SELECT * FROM mdl_context WHERE id = %s" QUERY_COURSE_FIELDS = "SELECT * FROM mdl_customfield_category cat JOIN mdl_customfield_field fld ON fld.categoryid = cat.id JOIN mdl_customfield_data dat ON dat.fieldid = fld.id" QUERY_COURSE_MODULES = "SELECT * FROM mdl_course_modules WHERE course = %s" QUERY_MODULE_COMPLETION = "SELECT * FROM mdl_course_modules_completion WHERE coursemoduleid = %s" +QUERY_INSERT_MODULE_COMPLETION = "INSERT INTO mdl_course_modules_completion(coursemoduleid, userid, completionstate, timemodified) VALUES (%s, %s, 1, %s)" +QUERY_UPDATE_MODULE_COMPLETION = "UPDATE mdl_course_modules_completion SET completionstate = 1 WHERE coursemoduleid = %s AND userid = %s" QUERY_LOG_INSERT = "INSERT INTO mdl_logstore_standard_log(eventname, other, origin) VALUES (%s, %s, 'monster')"