Some completion stuff

This commit is contained in:
Kumi 2023-06-07 15:00:53 +02:00
parent 7990cfa700
commit ff17665cb0
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 28 additions and 5 deletions

View file

@ -1,13 +1,15 @@
from .database import Database from classes.database import Database
from ..const import *
from configparser import SectionProxy from configparser import SectionProxy
from typing import Optional, Union from typing import Optional, Union
from datetime import datetime from datetime import datetime
from MySQLdb.cursors import DictCursor from MySQLdb.cursors import DictCursor
from MySQLdb._exceptions import IntegrityError
from bcrypt import hashpw, gensalt from bcrypt import hashpw, gensalt
from const import *
class Vessel: class Vessel:
"""Class describing a Vessel """Class describing a Vessel
@ -77,6 +79,9 @@ class Vessel:
def connect(self): def connect(self):
return Database(self) return Database(self)
def reconnect(self):
self.db = self.connect()
@staticmethod @staticmethod
def getTimestamp() -> int: def getTimestamp() -> int:
return int(datetime.now().timestamp()) return int(datetime.now().timestamp())
@ -195,7 +200,7 @@ class Vessel:
def setPassword(self, username: str, password: str): def setPassword(self, username: str, password: str):
hashed = hashpw(password.encode(), gensalt(prefix=b"2b")) hashed = hashpw(password.encode(), gensalt(prefix=b"2b"))
query = QUERY_USER_SET_PASSWORD query = QUERY_USER_SET_PASSWORD
self.db._execute(query, (password, username)) self.db._execute(query, (hashed, username))
def getEnrols(self, enrol: Optional[str] = None): def getEnrols(self, enrol: Optional[str] = None):
results = list(self.db._execute(QUERY_ENROL, ctype=DictCursor)) 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" email = email or f"{self.getUsers(id=userid)[userid]['username']}@pin.seachefsacademy.com"
self.db._execute(QUERY_USER_SET_EMAIL, (email, userid)) 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): def getCustomCourseFields(self):
results = list(self.db._execute(QUERY_COURSE_FIELDS, ctype=DictCursor)) results = list(self.db._execute(QUERY_COURSE_FIELDS, ctype=DictCursor))
return results return results
@ -264,5 +272,18 @@ class Vessel:
results = list(self.db._execute(QUERY_MODULE_COMPLETION, (moduleid,), ctype=DictCursor)) results = list(self.db._execute(QUERY_MODULE_COMPLETION, (moduleid,), ctype=DictCursor))
return results 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): def writeLog(self, event, data):
self.db._execute(QUERY_LOG_INSERT, (event, data)) self.db._execute(QUERY_LOG_INSERT, (event, data))

View file

@ -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_INFO_DATA = "SELECT * FROM mdl_user_info_data"
QUERY_USER_SET_PASSWORD = "UPDATE mdl_user SET password = %s WHERE username = %s" 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_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_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_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_ROLE = "SELECT * FROM mdl_role_assignments WHERE contextid = %s AND userid = %s"
QUERY_GET_USERID = "SELECT * FROM mdl_user WHERE username = %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_COURSE = "SELECT * FROM mdl_course"
QUERY_ENROL = "SELECT * FROM mdl_enrol" 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_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_COURSE_MODULES = "SELECT * FROM mdl_course_modules WHERE course = %s"
QUERY_MODULE_COMPLETION = "SELECT * FROM mdl_course_modules_completion WHERE coursemoduleid = %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')" QUERY_LOG_INSERT = "INSERT INTO mdl_logstore_standard_log(eventname, other, origin) VALUES (%s, %s, 'monster')"