def query(self, query): self.connect() self.cur = self.dbh.cursor() CommonFunc.debug("Executing query: %s" % (query)) self.res = self.cur.execute(query) #CommonFunc.debug( "Returned rows: %d" % (int(self.numrows())) ) return self.res
def connect(self): #CommonFunc.debug("Creating (or reusing) DB connection") #CommonFunc.debug("--- DB ---") CommonFunc.debug("Connectiong to DB") if self.dbh == None: CommonFunc.debug("Creating DB hablder DB handler") self.dbh = MySQLdb.connect(host=self.db_host, user=self.db_user, passwd=self.db_pass, db=self.db_name) CommonFunc.debug("Using DB hablder: %s" % (self.dbh)) else: CommonFunc.debug("Reusing DB handler: %s" % (self.dbh))
def DB_get_user_data(self): import appmysqldb, CommonFunc user_id = "None" course_id = "None" user_name = "None" user_email = "None" db = appmysqldb.mysql('localhost', 3306, 'edxapp', 'root', '') q = "SELECT id, user_id, course_id FROM student_anonymoususerid WHERE anonymous_user_id='" + self.n_user_id + "'" CommonFunc.debug("QUERY: %s" %(q)) db.query(q) res = db.fetchall() for row in res: user_id = row[1] course_id = row[2] q = "SELECT name FROM auth_userprofile WHERE user_id='%s' " % (user_id) CommonFunc.debug("QUERY: %s" %(q)) db.query(q) res = db.fetchall() for row in res: user_name = row[0] q = "SELECT username FROM auth_user WHERE id='%s' " % (user_id) CommonFunc.debug("QUERY: %s" %(q)) db.query(q) res = db.fetchall() for row in res: user_email = row[0] results = [user_id,course_id,user_name,user_email] return results
def DB_get_user_data(self): import appmysqldb, CommonFunc user_id = "None" course_id = "None" user_name = "None" user_email = "None" user_score = "0" #ids : user and course db = appmysqldb.mysql('localhost', 3306, 'edxapp', 'root', '') q = "SELECT id, user_id, course_id FROM student_anonymoususerid WHERE anonymous_user_id='" + self.n_user_id + "'" CommonFunc.debug("QUERY: %s" %(q)) db.query(q) res = db.fetchall() for row in res: user_id = row[1] course_id = row[2] #username q = "SELECT name FROM auth_userprofile WHERE user_id='%s' " % (user_id) CommonFunc.debug("QUERY: %s" %(q)) db.query(q) res = db.fetchall() for row in res: user_name = row[0] #email q = "SELECT email FROM auth_user WHERE id='%s' " % (user_id) CommonFunc.debug("QUERY: %s" %(q)) db.query(q) res = db.fetchall() for row in res: user_email = row[0] """ getting course data from mongodb """ # Mongo DB Connect from pymongo import Connection xmoduledb = "edxapp" connection = Connection() db_mongo = connection[xmoduledb] mongo_modulestore = db_mongo['modulestore'] badge_list_problems = edxappCourseData.getListProblemsFromBadgeId(mongo_modulestore,self.bg_id,course_id, self.xblock_name_field) badge_problems_score = edxappCourseData.getScoreFromBadgeId(mongo_modulestore,self.bg_id,course_id, self.xblock_name_field) """ """ #calculate badge_score user_score = 0 partial_user_score = [] badge_partial_user_score = 0 badge_percent_user_score = 0 #calculate user partials if badge_problems_score>0: if len(badge_list_problems)>0: for problem in badge_list_problems: if 'problem_score' in problem: problem_score = problem['problem_score'] problem_id = problem['problem_id'] #getting partial values if int(problem_score)>0: q = "SELECT ((%s/max_grade)*grade) FROM courseware_studentmodule WHERE course_id='%s' AND student_id='%s' AND module_id='%s'" % (problem_score,course_id,user_id,problem_id) CommonFunc.debug("QUERY: %s" %(q)) db.query(q) res = db.fetchall() for row in res: if row[0]>0: partial_user_score.append( float(row[0]) ) badge_partial_user_score = sum(partial_user_score) #calculate total percent if round(badge_partial_user_score,2)>0 and int(badge_problems_score)>0: badge_percent_user_score = ( badge_partial_user_score * 100.0 ) / badge_problems_score badge_percent_user_score = round(badge_percent_user_score,2) if int(badge_percent_user_score)>0: user_score = badge_percent_user_score #show results results = [user_id,course_id,user_name,user_email,user_score,badge_list_problems,badge_problems_score,badge_partial_user_score,badge_percent_user_score,badge_problems_score] return results
def disconnect(self): CommonFunc.debug("Closing DB connection") self.dbh.close() CommonFunc.debug("Disconnected")
def showConfig(self): CommonFunc.debug( "db_host : %s" % (self.db_host) ) CommonFunc.debug( "db_name : %s" % (self.db_name) ) CommonFunc.debug( "db_user : %s" % (self.db_user) ) CommonFunc.debug( "db_pass : %s" % (self.db_pass) )
def fetchall(self): CommonFunc.debug("Fetching all results") return self.cur.fetchall()