|
|
|
// 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");
|
|
|
|
const Message = require("../../model/message");
|
|
|
|
const Grade = require("../../model/grade");
|
|
|
|
const Presence = require("../../model/presence");
|
|
|
|
const app = getApp();
|
|
|
|
|
|
|
|
Page({
|
|
|
|
students: [],
|
|
|
|
presences: [],
|
|
|
|
my_presence: null,
|
|
|
|
data: {
|
|
|
|
course: null,
|
|
|
|
loading: true,
|
|
|
|
students: [],
|
|
|
|
select_stu_login:"",
|
|
|
|
message_text: "",
|
|
|
|
messages: [],
|
|
|
|
show_conversation: true
|
|
|
|
},
|
|
|
|
enter_mark_detail({currentTarget:{dataset}}){
|
|
|
|
wx.navigateTo({
|
|
|
|
url: '../mark_detail/mark_detail?course_id='+this.course_id,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
pull_messages: function(){
|
|
|
|
let query = new AV.Query("Message");
|
|
|
|
query.equalTo("edu_course_id", this.course_id);
|
|
|
|
const set_messages = this.set_messages.bind(this);
|
|
|
|
return AV.Promise.all([query.find().then(set_messages), query.subscribe()]).then(([messages, subscription]) => {
|
|
|
|
this.messageSubscription = subscription;
|
|
|
|
if (this.messageUnbind) this.messageUnbind();
|
|
|
|
this.messageUnbind = bind(subscription, messages, set_messages);
|
|
|
|
}).catch(error => console.error(error.message));
|
|
|
|
},
|
|
|
|
set_messages: function(messages){
|
|
|
|
console.log("set_messages");
|
|
|
|
messages = messages.map(message=>{
|
|
|
|
let sender_login = message.edu_account_login;
|
|
|
|
let senders_ = this.students.filter(student=>student.login==sender_login)
|
|
|
|
if(senders_.length==1){
|
|
|
|
message.set("sender_name", senders_[0]["name"]);
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
})
|
|
|
|
console.log(jsonify({ messages }));
|
|
|
|
this.setData(jsonify({messages}));
|
|
|
|
return messages;
|
|
|
|
},
|
|
|
|
send_message:function(event){
|
|
|
|
let {message_text} = this.data;
|
|
|
|
if(message_text.trim()==""){return;}
|
|
|
|
let message = new Message({
|
|
|
|
text: message_text,
|
|
|
|
edu_account_login: this.current_user.login,
|
|
|
|
edu_course_id: this.course_id,
|
|
|
|
sender_name: this.current_user.real_name||"未命名",
|
|
|
|
});
|
|
|
|
message.save().then(()=>{
|
|
|
|
this.setData({message_text: ""});
|
|
|
|
console.log("send_message")
|
|
|
|
}).catch(console.error);
|
|
|
|
},
|
|
|
|
close_conversation: function(event){
|
|
|
|
console.log("close conversation");
|
|
|
|
console.log(event);
|
|
|
|
this.setData({show_conversation: false});
|
|
|
|
this.messageSubscription.unsubscribe();
|
|
|
|
this.messageUnbind();
|
|
|
|
},
|
|
|
|
update_message:function({detail:{value}}){
|
|
|
|
this.setData({message_text: value});
|
|
|
|
},
|
|
|
|
select_stu: function(event){
|
|
|
|
let login = event.currentTarget.dataset.login;
|
|
|
|
console.log(event);
|
|
|
|
this.setData({select_stu_login: login});
|
|
|
|
console.log(this.data.select_stu_login);
|
|
|
|
},
|
|
|
|
set_isasking: function(event){
|
|
|
|
let login = event.currentTarget.dataset.login;
|
|
|
|
let status = event.currentTarget.dataset.status;
|
|
|
|
console.log("set_isasking");
|
|
|
|
console.log(this.presences);
|
|
|
|
let asking_presence = this.presences.filter(presence=>presence.get("edu_account_login")==login)[0];
|
|
|
|
console.log(asking_presence);
|
|
|
|
asking_presence.isasking = status;
|
|
|
|
wx.showLoading({
|
|
|
|
title: '请稍候',
|
|
|
|
});
|
|
|
|
asking_presence.save().then(()=>{
|
|
|
|
wx.hideLoading();
|
|
|
|
}, ()=>{
|
|
|
|
wx.hideLoading();
|
|
|
|
wx.showToast({
|
|
|
|
title: '操作失败',
|
|
|
|
icon: "none"
|
|
|
|
})
|
|
|
|
});
|
|
|
|
},
|
|
|
|
add_mark: function(event){
|
|
|
|
console.log("add_mark");
|
|
|
|
let login = event.currentTarget.dataset.login;
|
|
|
|
let mark = event.currentTarget.dataset.mark;
|
|
|
|
console.log(this.presences);
|
|
|
|
let presence = this.presences.filter(presence => presence.get("edu_account_login") == login)[0];
|
|
|
|
if(presence==null){
|
|
|
|
console.warn("没有学生Presence");
|
|
|
|
presence = new Presence({
|
|
|
|
edu_account_login: login,
|
|
|
|
edu_course_id: this.course_id,
|
|
|
|
present: false,
|
|
|
|
mark: 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
presence.increment("mark", mark);
|
|
|
|
var grade = new Grade();
|
|
|
|
console.log(grade);
|
|
|
|
grade.edu_account_login = login;
|
|
|
|
grade.edu_course_id = this.course_id;
|
|
|
|
grade.mark = mark;
|
|
|
|
presence.save().then(()=>{grade.save()}).then(()=>{
|
|
|
|
wx.showToast({
|
|
|
|
title: '操作成功',
|
|
|
|
icon:"none"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
).catch(console.error);
|
|
|
|
},
|
|
|
|
|
|
|
|
pull_presence: function(){
|
|
|
|
let query = new AV.Query("Presence");
|
|
|
|
query.equalTo("edu_course_id", this.course_id);
|
|
|
|
const set_presence = this.set_presence.bind(this);
|
|
|
|
return AV.Promise.all([query.find().then(set_presence), query.subscribe()]).then(([presences, subscription])=>{
|
|
|
|
this.presenceSubscription = subscription;
|
|
|
|
if(this.presenceUnbind) this.presenceUnbind();
|
|
|
|
console.log("show set_presence");
|
|
|
|
console.log(presences);
|
|
|
|
console.log(set_presence);
|
|
|
|
this.presenceUnbind = bind(subscription, presences, set_presence);
|
|
|
|
})
|
|
|
|
},
|
|
|
|
set_presence: function(presences){
|
|
|
|
this.presences = presences;
|
|
|
|
console.log("set_presence");
|
|
|
|
console.log(presences);
|
|
|
|
//console.log("Set_presence");
|
|
|
|
//console.log(this);
|
|
|
|
let students = this.students.concat().map(student=>{
|
|
|
|
student["mark"]=0;
|
|
|
|
student["isasking"]=0;
|
|
|
|
student["present"]=false;
|
|
|
|
return student;
|
|
|
|
});//拷贝
|
|
|
|
for(var presence of presences){
|
|
|
|
let student_login = presence.get("edu_account_login");
|
|
|
|
if(presence.edu_account_login==this.current_user.login){
|
|
|
|
this.my_presence = presence;
|
|
|
|
}
|
|
|
|
for(var i=0;i<students.length;i++){
|
|
|
|
if(students[i].login==student_login){
|
|
|
|
students[i]["present"] = presence.present;
|
|
|
|
students[i]["isasking"] = presence.isasking;
|
|
|
|
students[i]["mark"] = presence.mark;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(students);
|
|
|
|
let stus = students.filter(student => student.login == app.client.current_user.login);
|
|
|
|
if(stus.length==1){
|
|
|
|
this.setData({current_user: stus[0]});
|
|
|
|
}
|
|
|
|
this.setData({students: students});
|
|
|
|
return presences;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**new api below: */
|
|
|
|
pull_course: function(){
|
|
|
|
return app.client.get_course_info({course_id: this.course_id})
|
|
|
|
.then(res=>{
|
|
|
|
this.setData({course: res.data});
|
|
|
|
wx.setNavigationBarTitle({
|
|
|
|
title: this.data.course.name
|
|
|
|
})
|
|
|
|
}).catch(console.error);
|
|
|
|
},
|
|
|
|
pull_students: function(){
|
|
|
|
return app.client.get_course_students({course_id: this.course_id})
|
|
|
|
.then(res=>{
|
|
|
|
this.students = res.data.students;
|
|
|
|
}).catch(console.error);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生命周期函数--监听页面加载
|
|
|
|
*/
|
|
|
|
onLoad: function (options) {
|
|
|
|
this.course_id = options.id;
|
|
|
|
this.current_user = app.client.current_user;
|
|
|
|
wx.showLoading({
|
|
|
|
title: '加载中',
|
|
|
|
});
|
|
|
|
this.pull_course();
|
|
|
|
this.pull_students()
|
|
|
|
.then(()=>{
|
|
|
|
this.pull_presence()
|
|
|
|
.then(() => {
|
|
|
|
if (this.my_presence == null) {
|
|
|
|
this.my_presence = new Presence({
|
|
|
|
edu_account_login: this.current_user.login,
|
|
|
|
edu_course_id: this.course_id,
|
|
|
|
present: true
|
|
|
|
});
|
|
|
|
this.my_presence.save();
|
|
|
|
}else{
|
|
|
|
this.my_presence.enter();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
wx.hideLoading();
|
|
|
|
this.setData({ loading: false });
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
wx.hideLoading();
|
|
|
|
wx.showToast({
|
|
|
|
title: '加载失败,请刷新重试',
|
|
|
|
icon: "none"
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.pull_messages();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
|
|
*/
|
|
|
|
onReady: function () {
|
|
|
|
console.log("onReady");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生命周期函数--监听页面显示
|
|
|
|
*/
|
|
|
|
onShow: function () {
|
|
|
|
console.log("onShow");
|
|
|
|
if(this.my_presence){
|
|
|
|
console.log("enter class when onShow");
|
|
|
|
this.my_presence.enter();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生命周期函数--监听页面隐藏
|
|
|
|
*/
|
|
|
|
onHide: function () {
|
|
|
|
this.my_presence.leave();
|
|
|
|
console.log("onHide");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生命周期函数--监听页面卸载
|
|
|
|
*/
|
|
|
|
onUnload: function () {
|
|
|
|
this.my_presence.leave();
|
|
|
|
console.log("onUnload");
|
|
|
|
this.presenceSubscription.unsubscribe();
|
|
|
|
this.presenceUnbind();
|
|
|
|
this.messageSubscription.unsubscribe();
|
|
|
|
this.messageUnbind();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
|
|
*/
|
|
|
|
onPullDownRefresh: function () {
|
|
|
|
this.pull_course();
|
|
|
|
this.pull_students()
|
|
|
|
.then(()=>{
|
|
|
|
this.pull_presence().then(() => {
|
|
|
|
if (this.my_presence == null) {
|
|
|
|
this.my_presence = new Presence({
|
|
|
|
edu_account_login: this.current_user.login,
|
|
|
|
edu_course_id: this.course_id,
|
|
|
|
present: true
|
|
|
|
});
|
|
|
|
this.my_presence.save();
|
|
|
|
} else {
|
|
|
|
this.my_presence.enter();
|
|
|
|
}
|
|
|
|
}).then(()=>{
|
|
|
|
this.setData({loading: false});
|
|
|
|
}).catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
wx.showToast({
|
|
|
|
title: '加载失败,请刷新重试',
|
|
|
|
icon: "none"
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.pull_messages();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 页面上拉触底事件的处理函数
|
|
|
|
*/
|
|
|
|
onReachBottom: function () {
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户点击右上角分享
|
|
|
|
*/
|
|
|
|
onShareAppMessage: function () {
|
|
|
|
console.log("onShareAppMessage");
|
|
|
|
}
|
|
|
|
});
|