You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.5 KiB

5 years ago
// pages/classroom/classroom.js
const AV = require("../../lib/av-live-query-weapp-min")
const { jsonify } = require('../../utils/leancloudutils');
const bind = require("../../lib/live-query-binding")
Page({
/**
页面的初始数据
students: 该课程所有的学生
元素student
student.objectId 该学生的主键唯一
student.username 学生的用户名是唯一就像微信号一样
student.name 学生的姓名
student.present 是否出席
student.isasking :0:没有提问,1:提问,2:请回答
*/
students: [],
presences: [],
my_presence: null,
is_askiny: null,
5 years ago
data: {
//以下数据的同步由该代码文件实现数据的呈现由classroom.wxml实现
students: [], // 该课程所有的学生, 元素类型见上文中的student
current_user: null, //目前登陆的用户,类型为对象属性有objectIdusername, name
teacher: null,
select_stu_id: '',
5 years ago
//该课程教员,类型为对象
//请在进入界面时判断用户是否为教员以给与权限或更改界面
//属性如下objectId, username, name
class_name: "", //课程名称
class_id: "", //该课程的主键(编号)
},
enter_detail: function(event){
wx.navigateTo({
url: '../classdetail/classdetail?class_id='+this.data.class_id,
})
},
select_stu: function(event){
id = event.currentTarget.dataset.id;
if(id!='none'){
console.log(id);
}else{console.log("none");id=""}
this.setData({select_stu_id: id});
5 years ago
},
set_isasking: function(event){
userid = event.currentTarget.dataset.id;
status = event.currentTarget.dataset.status;
console.log("set_isasking");
console.log(status);
console.log(typeof status);
this.asking_present = this.presences.filter(presence=>presence.get("user").id==userid)[0];
this.asking_present.isasking = status;
wx.showLoading({
title: '请稍候',
})
this.asking_present.save().then(()=>{
wx.hideLoading();
}, ()=>{
wx.hideLoading();
wx.showToast({
title: '操作失败',
icon: "none"
})
});
},
add_mark: function(event){
id = event.currentTarget.dataset.id;
mark = event.currentTarget.dataset.mark;
console.log("add_mark");
console.log(mark);
console.log(typeof mark);
presence = this.presences.filter(presence => presence.get("user").id == id)[0];
if(presence==null){
console.warn("没有选择学生");
}
presence.increment("mark", mark).save().then(()=>{
wx.showToast({
title: '操作成功',
icon:"none"
})
}).catch(error=>console.error(error.message));
},
pull_presence: function(){
5 years ago
var query = new AV.Query("Presence");
//console.log(this.class);
query.equalTo("class", this.class);
query.include("user");
set_presence = this.set_presence.bind(this);
return AV.Promise.all([query.find().then(set_presence), query.subscribe()]).then(([presents, subscription])=>{
5 years ago
this.PresentSubscription = subscription;
if(this.presentUnbind) this.presentUnbind();
this.presentUnbind = bind(subscription, presents, set_presence);
5 years ago
}).catch(error=> console.error(error.message));
},
set_presence: function(presences){
console.log("set_presence");
//console.log(presences);
this.presences = presences;
console.log(this.presences);
students = this.presences.map((presence)=>{
student = presence.get("user");
if(student.get("username")==null){
students = this.students.filter(stu=>stu.id==student.id)
if(students.length==1){
student = students[0];
5 years ago
}else{
student.fetch();
5 years ago
}
}
student.set("present", presence.get("present"));
student.set("isasking",presence.get("isasking"));
student.set("mark", presence.get("mark"));
if(student.id==AV.User.current().id){
this.my_presence = presence;
this.setData({current_user:student.toJSON()});
}
return student;
});
this.students = students;
console.log(students);
this.setData(jsonify({students}));
return presences;
5 years ago
},
fetch_class: function(class_id){
query = new AV.Query("Class_");
query.equalTo("objectId", class_id);
query.include("teacher");
return query.find().then((classes) => {
5 years ago
//console.log(this.class);
this.class = classes[0];
teacher = this.class.get("teacher");
console.log("fetch_class");
console.log(teacher);
teacher = teacher.toJSON();
// if(typeof teacher.toJson == "function"){
// teacher = teacher.toJson();
// console.log("toJson");
// }
5 years ago
this.setData({
class_name: this.class.get("name"),
class_id: this.class.get("objectId"),
teacher: teacher
5 years ago
});
console.log(teacher);
//this.setData(teacher);
5 years ago
}).catch(error=>console.error(error.message))
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.showLoading({
title: '加载中'
})
this.setData({current_user: AV.User.current().toJSON()});
5 years ago
this.class_id = options.class_id;
this.fetch_class.bind(this)(this.class_id)
.then(()=>{
console.log("pull_presence");
this.pull_presence.bind(this)().then((presences) => { this.my_presence.enter(); this.presences = presences;});
wx.hideLoading();
console.log("hideLoading");
});
5 years ago
console.log("onLoad");
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
console.log("onReady");
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
console.log("onShow");
console.log("enter class when onShow");
console.log(AV.User.current());
if(this.my_presence){
this.my_presence.enter();
}
5 years ago
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
this.my_presence.leave();
5 years ago
console.log("onHide");
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
this.my_presence.leave();
5 years ago
console.log("onUnload");
this.PresentSubscription.unsubscribe();
this.presentUnbind();
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
this.pull_presence.bind(this)();
5 years ago
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
console.log("onShareAppMessage");
}
})