def doGet(request): mode = request.GET.get('mode', None) course = request.GET.get('course', None) to_render = {} islogin = cookiegetter.isLogined(request) haveThisCourse = False if islogin is True: userEmail = cookiegetter.getEmail(request) userID = cookiegetter.getUserIDByEmail(userEmail) with connections['ResultDB'].cursor() as cursor: pass haveThisCourse = True if haveThisCourse is False: print('noHave') to_render['IsLogin'] = 2 return to_render to_render['IsLogin'] = 1 print('teacher') return to_render else: print('not login') to_render['IsLogin'] = 2 return to_render
def forum_data_view(request): request.encoding = 'utf-8' if request.method == "GET": to_render = {} islogin = cookiegetter.isLogined(request) list_ = [] if islogin is True: userEmail = cookiegetter.getEmail(request) userID = cookiegetter.getUserIDByEmail(userEmail) if cookiegetter.isTeacher(userID): teachersCourse = cookiegetter.get_Teacher_Courses(userID) with connections['ResultDB'].cursor() as cursor: # 取得最新統計日期 # 原Java版式搜尋course_total_data,但因為field name有問題因此改用course_total_data_v2 # 兩者間域名域名有些不同,如id -> course_id, 其餘包含.之域名全改為_ cursor.execute("SELECT 統計日期 " "FROM edxresult.course_total_data_v2") result = namedtuplefetchall(cursor) finalUpdate = result[-1].統計日期 # 取得老師擁有的課程資料 cursor.execute("SELECT * " "FROM edxresult.course_total_data_v2 " "WHERE 統計日期 = %s order by course_id", [finalUpdate]) result = namedtuplefetchall(cursor) temp_site = 0 data = [] for rs in result: for i in range(len(teachersCourse)): if rs.course_id == teachersCourse[i]: data.clear() data.append(rs.課程代碼) data.append(rs.course_id) data.append(rs.course_name) data.append(rs.討論區討論次數) data.append(rs.討論區參與度) list_.append(data.copy()) # 回傳要呈現的資料 to_render['result'] = list_ # 預設日期範圍的下拉式選單,預設值為1:-請選擇- to_render['select'] = 1 to_render['finalUpdate'] = "最後資料更新時間 : " + finalUpdate to_render['IsLogin'] = 1 print('teacher') return render(request, '3_ForumData.html', to_render) else: print("student") to_render['IsLogin'] = 2 return render(request, '3_ForumData.html', to_render) else: print('not login') to_render['IsLogin'] = 2 return render(request, '3_ForumData.html', to_render) if request.method == 'POST': to_render = {} list_ = [] islogin = cookiegetter.isLogined(request) if islogin is True: userEmail = cookiegetter.getEmail(request) userID = cookiegetter.getUserIDByEmail(userEmail) if cookiegetter.isTeacher(userID): teachersCourse = cookiegetter.get_Teacher_Courses(userID) choose = request.POST.get('choose', None) # 使用者選擇範圍 S_startDate = request.POST.get('startDate', None) # 使用者選擇日期 D_userChooseStartDate = getChooseDate(choose, S_startDate) if choose is None: to_render['select'] = 1 to_render['selectStartDate'] = S_startDate else: to_render['select'] = choose to_render['selectStartDate'] = '年/月/日' with connections['ResultDB'].cursor() as cursor: # 取得統計日期,方式是取得最後一筆資料,此筆資料的統計日期就是最新日期 # 原Java版式搜尋course_total_data,但因為field name有問題因此改用course_total_data_v2 # 兩者間域名域名有些不同,如id -> course_id, 其餘包含.之域名全改為_ cursor.execute("select 統計日期 from course_total_data_v2") result = namedtuplefetchall(cursor) finalUpdate = result[-1].統計日期 # 依據使用者選擇的日期,取得資料 cursor.execute( "SELECT * " "FROM course_total_data_v2 " "WHERE 統計日期 = %s AND start_date > %s", [finalUpdate, D_userChooseStartDate] ) result = namedtuplefetchall(cursor) data = [] for rs in result: for i in range(len(teachersCourse)): if rs.course_id == teachersCourse[i]: data.clear() data.append(rs.課程代碼) data.append(rs.course_id) data.append(rs.course_name) data.append(rs.討論區討論次數) data.append(rs.討論區參與度) list_.append(data.copy()) to_render['result'] = list_ to_render['finalUpdate'] = '最後資料更新時間 : ' + finalUpdate to_render['IsLogin'] = 1 print('teacher') return render(request, '3_ForumData.html', to_render) else: to_render['IsLogin'] = 2 print('student') return render(request, '3_ForumData.html', to_render) else: to_render['IsLogin'] = 2 print('not login') return render(request, '3_ForumData.html', to_render)
def doGet(request): mode = request.GET.get('mode', False) course = request.GET.get('course', False) jsonArray_temp = [] jsonArray_RegisteredPersons = [] jsonArray_temp_1 = [] jsonArray_active = [] jsonArray_temp_2 = [] jsonArray_answerRatio = [] to_render = {} islogin = cookiegetter.isLogined(request) haveThisCourse = False if islogin: userEmail = cookiegetter.getEmail(request) userID = cookiegetter.getUserIDByEmail(userEmail) if cookiegetter.isTeacher(userID): teachersCourse = cookiegetter.get_Teacher_Courses(userID) for j in range(len(teachersCourse)): if getCourseIDByCourseCode(course) == teachersCourse[j]: # 由於course_total_data裡面的欄位無法執行,於是以下的資料全改成從course_total_data_v2取出 with connections['ResultDB'].cursor() as cursor: cursor.execute( "select * from course_total_data_v2 where 課程代碼 = %s", [course]) result = namedtuplefetchall(cursor) count = 0 courseCode = '' courseId = '' courseName = '' totalRegisteredPersons = '' age_17 = '' age_18_25 = '' age_26_ = '' withDrew = '' for rs in result: courseCode = str(rs.課程代碼) courseId = str( rs.course_id) # 在course_total_data裡為 'id' courseName = str( rs.course_name ) # 在這之下所有欄位名稱內有'_'的項目,在course_total_data裡全是'.' totalRegisteredPersons = str(rs.註冊人數) age_17 = str(rs.age_17) age_18_25 = str(rs.age_18_25) age_26_ = str(rs.age_26_) withDrew = str(rs.退選人數) count += 1 cursor.execute( "SELECT count(*) as dataCount " "FROM course_total_data_v2 " "WHERE 課程代碼 = %s and 統計日期>=start_date and end_date>=統計日期 order by 統計日期 asc", [course]) result = namedtuplefetchall(cursor) count = result[-1].dataCount RegisteredPersons = [0 for i in range(count)] updateDate = ['' for i in range(count)] cursor.execute( "select 註冊人數 as RegisteredPersons,練習題作答率_活躍 as ActiveAnswerRatio,統計日期 as updateDate,活躍 as active,非活躍 as nonActive,練習題作答率 as answerRatio " "from course_total_data_v2 " "where 課程代碼 = %s and 統計日期>=start_date and end_date>=統計日期 order by 統計日期 asc", [course]) result = namedtuplefetchall(cursor) jsonArray_temp.clear() jsonArray_temp.append('日期') jsonArray_temp.append('註冊人數') jsonArray_temp_1.clear() jsonArray_temp_1.append('日期') jsonArray_temp_1.append('活躍人數') jsonArray_temp_1.append('非活躍人數') jsonArray_temp_2.append('日期') jsonArray_temp_2.append('練習題作答率') jsonArray_temp_2.append('練習題作答率_活躍') jsonArray_RegisteredPersons.append( jsonArray_temp.copy()) jsonArray_active.append(jsonArray_temp_1.copy()) jsonArray_answerRatio.append(jsonArray_temp_2.copy()) i = 0 for rs in result: RegisteredPersons[i] = int(rs.RegisteredPersons) answerRatio = float(rs.answerRatio) ActiveAnswerRatio = float(rs.ActiveAnswerRatio) updateDate[i] = str(rs.updateDate) active = int(rs.active) nonActive = int(rs.nonActive) jsonArray_temp.clear() jsonArray_temp.append(updateDate[i]) jsonArray_temp.append(RegisteredPersons[i]) jsonArray_RegisteredPersons.append( jsonArray_temp.copy()) jsonArray_temp_1.clear() jsonArray_temp_1.append(updateDate[i]) jsonArray_temp_1.append(active) jsonArray_temp_1.append(nonActive) jsonArray_active.append(jsonArray_temp_1.copy()) jsonArray_temp_2.clear() jsonArray_temp_2.append(updateDate[i]) jsonArray_temp_2.append(answerRatio) jsonArray_temp_2.append(ActiveAnswerRatio) jsonArray_answerRatio.append( jsonArray_temp_2.copy()) i = i + 1 weekChageRegisteredPersons = RegisteredPersons[ count - 1] - RegisteredPersons[count - 2] registeredPersons_Taiwan = None registeredPersons_Foreign = None education_master = None education_Bachelo = None education_Associate = None education_senior = None education_junior = None education_primary = None male = None female = None to_render['courseCode'] = courseCode to_render['courseId'] = courseId to_render['courseName'] = courseName to_render[ 'registeredPersons_Taiwan'] = registeredPersons_Taiwan to_render[ 'registeredPersons_Foreign'] = registeredPersons_Foreign to_render['age_17'] = age_17 to_render['age_18_25'] = age_18_25 to_render['age_26_'] = age_26_ to_render['RegisteredPersons'] = RegisteredPersons to_render['updateDate'] = updateDate to_render['jsonArray_RegisteredPersons'] = json.dumps( jsonArray_RegisteredPersons) to_render['jsonArray_active'] = json.dumps( jsonArray_active) to_render['jsonArray_answerRatio'] = json.dumps( jsonArray_answerRatio) to_render[ 'weekChageRegisteredPersons'] = weekChageRegisteredPersons to_render['withDrew'] = withDrew to_render['education_master'] = education_master to_render['education_Bachelo'] = education_Bachelo to_render['education_Associate'] = education_Associate to_render['education_senior'] = education_senior to_render['education_junior'] = education_junior to_render['education_primary'] = education_primary to_render[ 'totalRegisteredPersons'] = totalRegisteredPersons to_render['mode'] = mode to_render['male'] = male to_render['female'] = female with connections['SurveyDB'].cursor() as cursor: checkBefore = False checkAfter = False to_render['IsLogin'] = 1 print('teacher') haveThisCourse = True break if haveThisCourse is False: print('not have') to_render['IsLogin'] = 2 else: print('student') to_render['IsLogin'] = 2 else: print('not login') to_render['IsLogin'] = 2 return to_render
def before_survey_view(request): request.encoding = 'utf-8' if request.method == 'GET': to_render = {} islogin = cookiegetter.isLogined(request) if islogin is True: userEmail = cookiegetter.getEmail(request) userID = cookiegetter.getUserIDByEmail(userEmail) if cookiegetter.isTeacher(userID): teachersCourse = cookiegetter.get_Teacher_Courses(userID) with connections['SurveyDB'].cursor() as cursor: # 找出所有課程的id(不重複) cursor.execute( "SELECT DISTINCT course_id FROM survey.before_class_survey order by course_id asc" ) result = namedtuplefetchall(cursor) courseNumber = 0 for rs in result: for i in range(len(teachersCourse)): if rs.course_id == teachersCourse[i]: courseNumber += 1 # 宣告陣列長度 data = [['' for i in range(11)] for i in range(courseNumber)] allCourseID = [] for rs in result: for i in range(len(teachersCourse)): if rs.course_id == teachersCourse[i]: allCourseID.append(rs.course_id) count = 0 i = 0 surveyNumber = 0 while i < courseNumber: cursor.execute( "select count(*) as surveyNumber " "from before_class_survey " "where course_id = %s", [allCourseID[i]]) result = namedtuplefetchall(cursor) surveyNumber = result[-1].surveyNumber # 找出所有該課程的問卷資料 cursor.execute( "select course_name,course_id,selection1,selection2,selection3 " "from before_class_survey " "where course_id = %s", [allCourseID[i]]) result = namedtuplefetchall(cursor) data[count][6] = '0' data[count][7] = '0' data[count][8] = '0' data[count][9] = '0' data[count][10] = '0' RegisteredPersonsAndCourseCode = [] for rs in result: RegisteredPersonsAndCourseCode = getRegisteredPersonsAndCourseCodeByCourseID( rs.course_id) data[count][0] = RegisteredPersonsAndCourseCode[1] data[count][3] = RegisteredPersonsAndCourseCode[0] break for rs in result: data[count][1] = rs.course_name data[count][2] = rs.course_id data[count][4] = str(surveyNumber) data[count][5] = '{:.1f}'.format( surveyNumber / int(data[count][3]) * 100) + '%' if rs.selection1 == 0: data[count][6] = str(int(data[count][6]) + 1) elif rs.selection1 == 1: data[count][7] = str(int(data[count][7]) + 1) elif rs.selection1 == 2: data[count][8] = str(int(data[count][8]) + 1) data[count][9] = str( int(data[count][9]) + (int(rs.selection2) - 5) * (-1)) data[count][10] = str( int(data[count][10]) + (int(rs.selection3) - 5) * (-1)) data[count][6] = '{:.1f}'.format( float(data[count][6]) / surveyNumber * 100) + '%' data[count][7] = '{:.1f}'.format( float(data[count][7]) / surveyNumber * 100) + '%' data[count][8] = '{:.1f}'.format( float(data[count][8]) / surveyNumber * 100) + '%' data[count][9] = '{:.1f}'.format( float(data[count][9]) / surveyNumber) data[count][10] = '{:.1f}'.format( float(data[count][10]) / surveyNumber) count = count + 1 i = i + 1 to_render['data'] = data to_render['IsLogin'] = 1 print('teacher') return render(request, 'beforeSurvey.html', to_render) else: to_render['IsLogin'] = 2 print('student') return render(request, 'beforeSurvey.html', to_render) else: to_render['IsLogin'] = 2 print('not login') return render(request, 'beforeSurvey.html', to_render)
def basic_course_data_view(request): request.encoding = 'utf-8' if request.method == 'GET': to_render = {} coursedata = [] courseId = [] studentsCourse = [] islogin = cookiegetter.isLogined(request) if islogin: userEmail = cookiegetter.getEmail(request) userID = cookiegetter.getUserIDByEmail(userEmail) with connections['ResultDB'].cursor() as cursor: # 取得統計日期,方式是取得最後一筆資料,此筆資料的統計日期就是最新日期 cursor.execute( "SELECT max(run_date) as day FROM student_total_data0912") result = namedtuplefetchall(cursor) finalUpdate_Student = result[-1].day cursor.execute( "SELECT max(統計日期) as day FROM course_total_data_v2") result = namedtuplefetchall(cursor) finalUpdate_course = result[-1].day # 取得學生擁有的課程的資料 cursor.execute( "SELECT course_id,name " "FROM student_total_data0912 " "WHERE run_date = %s and user_id = %s", [finalUpdate_Student, userID]) result = namedtuplefetchall(cursor) studentName = None for rs in result: courseId.append(rs.course_id) studentName = rs.name cursor.execute( "select * from course_total_data_v2 where 統計日期 = %s", [finalUpdate_course]) result = namedtuplefetchall(cursor) data = [] for rs in result: for i in range(len(courseId)): if rs.course_id == courseId[i]: data.clear() data.append(rs.課程代碼) data.append(rs.course_id) data.append(rs.course_name) data.append(rs.註冊人數) data.append(rs.start_date) data.append(rs.end_date) coursedata.append(data.copy()) to_render['result'] = coursedata # 設定日期範圍下拉式選單,預設值為1 to_render['select'] = 1 to_render['finalUpdate'] = '最後資料更新時間 : ' + finalUpdate_Student to_render['studentName'] = studentName to_render['IsLogin'] = 1 print('teacher') return render(request, '1_BasicCourseData.html', to_render) else: print('not login') to_render['IsLogin'] = 2 return render(request, 'noLogin.html', to_render) if request.method == 'POST': to_render = {} # 要傳給html的資料 coursedata = [] courseId = [] # 學生擁有的課程 studentsCourse = [] islogin = cookiegetter.isLogined(request) if islogin: userEmail = cookiegetter.getEmail(request) userID = cookiegetter.getUserIDByEmail(userEmail) choose = request.GET.get('choose', False) S_startDate = request.GET.get('startDate', False) D_userChooseStartDate = getChooseDate(choose, S_startDate) if choose is None: to_render['select'] = 1 to_render['selectStartDate'] = S_startDate else: to_render['select'] = choose to_render['selectStartDate'] = '年/月/日' with connections['ResultDB'].cursor() as cursor: # 取得統計日期,方式是取得最後一筆資料,此筆資料的統計日期就是最新日期 cursor.execute("select * from student_total_data0912") result = namedtuplefetchall(cursor) finalUpdate_Student = result[-1].run_date cursor.execute("select * from course_total_data_v2") result = namedtuplefetchall(cursor) finalUpdate_course = result[-1].統計日期 # 依據使用者選擇的日期,取得資料 cursor.execute( "SELECT course_id,name " "FROM student_total_data0912 " "WHERE run_date = %s and user_id = %s", [finalUpdate_Student, userID]) result = namedtuplefetchall(cursor) studentName = None for rs in result: courseId.append(rs.course_id) studentName = rs.name cursor.execute( "SELECT * " "FROM course_total_data_v2 " "WHERE 統計日期 = %s AND start_date > %s", [finalUpdate_course, D_userChooseStartDate]) result = namedtuplefetchall(cursor) data = [] for rs in result: for i in range(len(courseId)): if rs.course_id == courseId[i]: data.clear() data.append(rs.課程代碼) data.append(rs.course_id) data.append(rs.course_name) data.append(rs.註冊人數) data.append(rs.start_date) data.append(rs.end_date) coursedata.append(data.copy()) to_render['result'] = coursedata to_render['select'] = 1 to_render['finalUpdate'] = '最後資料更新時間 : ' + finalUpdate_Student to_render['studentName'] = studentName to_render['IsLogin'] = 1 return render(request, '1_BasicCourseData.html', to_render) else: to_render['IsLogin'] = 2 return render(request, 'noLogin.html', to_render)
def index_view(request): request.encoding = 'utf-8' if request.method == "GET": to_render = {} islogin = cookiegetter.isLogined(request) if islogin is True: userEmail = cookiegetter.getEmail(request) userID = cookiegetter.getUserIDByEmail(userEmail) with connections['ResultDB'].cursor() as cursor: # 取得學生統計日期 cursor.execute("SELECT max(run_date) as max_run " "FROM student_total_data0912 " "WHERE user_id = %s", [userID]) result = namedtuplefetchall(cursor) StudentfinalUpdate = result[-1].max_run to_render['finalUpdate'] = '最後資料更新時間 : ' + StudentfinalUpdate # 取得統計日期,方式是取得最後一筆資料,此筆資料的統計日期就是最新日期 cursor.execute("select max(統計日期) as max_date from course_total_data_v2") result = namedtuplefetchall(cursor) CoursefinalUpdate = result[-1].max_date to_render['CoursefinalUpdate'] = CoursefinalUpdate courseData = [] cursor.execute( "SELECT (影片觀看人次_台灣 + 影片觀看人次_非台灣 + login_count)/(註冊人數+退選人數) as a,start_date,註冊人數,`course_name`,course_id,課程代碼,end_date " "FROM course_total_data_v2 " "WHERE course_id in (select course_id from student_total_data0912 where run_date ='" + StudentfinalUpdate + "'" + " and user_id = '" + userID + "') and 統計日期='" + CoursefinalUpdate + "' order by course_id" ) result = namedtuplefetchall(cursor) Data = ['' for i in range(9)] duration_week = '' for rs in result: Data[0] = rs.course_name Data[1] = rs.start_date Data[2] = rs.註冊人數 Data[3] = rs.課程代碼 # 計算課程剩餘幾天 if rs.end_date != 'NA': today = datetime.today() day = (datetime.strptime(rs.end_date, '%Y-%m-%d') - today).days duration_week = str(day) Data[4] = duration_week # 判斷課程是否已結束 if rs.end_date != 'NA' and datetime.strptime(rs.end_date, '%Y-%m-%d') < datetime.today(): # 結束 Data[5] = rs.course_name else: # 開課中 Data[5] = '0' Data[6] = rs.end_date Data[7] = rs.course_id if rs.a is not None: Data[8] = rs.a else: Data[8] = '0' courseData.append(Data.copy()) cursor.execute( "SELECT watch_count,login_count,login_count,certificate,last_login,course_id as id,name " "FROM student_total_data0912 " "WHERE run_date = %s and user_id = %s order by id", [StudentfinalUpdate, userID] ) result = namedtuplefetchall(cursor) countAllCourse = 0 countExistCourse = 0 basiccourse = [] selfstudycourse = [] registerdate = getStudentLastlogin(userID) registerdate = datetime.strptime(registerdate, '%Y-%m-%d %H:%M:%S.%f') to_render['date_joined'] = datetime.strftime(registerdate, '%Y-%m-%d') finalCourseData = ['' for i in range(12)] i = 0 readyendexist = False endexist = False startexist = False selfstudyexist = False noData = 0 studentName = '' for rs in result: studentWatchTemp = 0 studentLoginTemp = 0 studentName = rs.name if rs.id == courseData[i][7]: finalCourseData[0] = courseData[i][0] finalCourseData[1] = courseData[i][1] finalCourseData[2] = courseData[i][2] finalCourseData[3] = courseData[i][3] finalCourseData[4] = courseData[i][4] finalCourseData[5] = courseData[i][5] finalCourseData[6] = courseData[i][6] finalCourseData[7] = courseData[i][7] # 若此課程為開課中,此欄為零 if finalCourseData[5] == '0': countExistCourse += 1 if courseData[i][8] is not None: avgcourse = float(courseData[i][8]) else: avgcourse = 0 studentWatchTemp = rs.watch_count studentLoginTemp = rs.login_count if studentWatchTemp + studentLoginTemp < avgcourse: finalCourseData[11] = '待加強' finalCourseData[8] = rs.login_count finalCourseData[9] = rs.last_login if finalCourseData[9] is None: finalCourseData[9] = '無登入紀錄' if rs.certificate == 1: finalCourseData[10] = '是' else: finalCourseData[10] = '否' if finalCourseData[6] != 'NA': basiccourse.append(finalCourseData.copy()) i += 1 if finalCourseData[4] != '0': readyendexist = True if finalCourseData[5] != '0': endexist = True if finalCourseData[5] == '0': startexist = True else: selfstudycourse.append(finalCourseData.copy()) i += 1 selfstudyexist = True else: # 資料庫內無此課程資料 noData += 1 countAllCourse += 1 to_render['studentName'] = studentName to_render['basiccourse'] = basiccourse to_render['selfstudycourse'] = selfstudycourse to_render['selfstudyexist'] = selfstudyexist to_render['startexist'] = startexist to_render['endexist'] = endexist to_render['readyendexist'] = readyendexist courseLogin = [['' for i in range(4)] for i in range(10)] k = 0 cursor.execute( "SELECT course_name,course_id,`課程代碼`,login_count_month " "FROM course_total_data_v2 " "WHERE 統計日期 = %s order by login_count_month desc", [CoursefinalUpdate] ) result = namedtuplefetchall(cursor) for rs in result: if k < 10: courseLogin[k][0] = rs.course_name courseLogin[k][1] = rs.course_id courseLogin[k][2] = '{:,.2f}'.format(rs.login_count_month) courseLogin[k][3] = rs.課程代碼 k += 1 else: break to_render['courseLogin'] = courseLogin totalcourse = countAllCourse - noData endcourse = totalcourse - countExistCourse startcourse = countExistCourse to_render['totalcourse'] = totalcourse to_render['startcourse'] = startcourse to_render['endcourse'] = endcourse with connections['OpenEduDB'].cursor() as cursor: now = datetime.strftime(datetime.today(), '%Y-%m-%d') cursor.execute( "SELECT count(*) as number FROM edxapp.auth_user where last_login > %s", [datetime.strftime] ) result = namedtuplefetchall(cursor) todayLogin = result[-1].number to_render['todayLogin'] = todayLogin to_render['IsLogin'] = 1 print('student') return render(request, 'index.html', to_render) else: to_render['IsLogin'] = 2 print('not login') return render(request, 'noLogin.html', to_render)