import React,{ Component } from "react"; import {Checkbox,Radio, Input} from "antd"; import '../css/members.css' import '../css/busyWork.css' import './pollStyle.css' import moment from 'moment' import { WordsBtn } from 'educoder' import Modals from '../../modals/Modals' import CoursesListType from '../coursesPublic/CoursesListType'; import axios from 'axios'; const map={1:"单选题",2:"多选题",3:"主观题"} const statudmap={1:"未发布",2:"提交中",3:"已截止"} class PollInfo extends Component{ constructor(props){ super(props); this.state={ courseName:undefined, userName:undefined, poll:undefined, question_answered:undefined, question_types:undefined, questions:undefined, inputArray:[], modalsType:false, modalsTopval:undefined, modalsBottomval:undefined, loadtype:true, modalSave:undefined, questionPanelFixed:false, pollAnswerList:[] } } componentDidUpdate (prevProps) { // 需要等get_user_info执行完才能getInfo if (!prevProps.current_user && this.props.current_user) { this.getInfo() } } componentDidMount(){ if(this.props.current_user){ this.getInfo(); } //window.addEventListener('scroll', this.handleScroll); } // 滚动定位 handleScroll=()=>{ if(parseInt(window.scrollY)>230){ this.setState({ questionPanelFixed:true }) }else{ this.setState({ questionPanelFixed:false }) } } scrollToAnchor=(index)=>{ let name="Anchor_"+index; if (index) { let anchorElement = document.getElementById(name); if(anchorElement) { anchorElement.scrollIntoView(); } } } getInfo=()=>{ this.setState({ courseName:this.props.current_user.course_name, userName:this.props.current_user.username }) let pollId=this.props.match.params.pollId; let user_id=this.props.match.params.login; let url=`/polls/${pollId}/start_answer.json?login=${user_id}` axios.get(url).then((result)=>{ if(result.status==200){ this.setState({ poll:result.data.poll, question_answered:result.data.question_answered, question_types:result.data.question_types, questions:result.data.questions }) let list=[]; for(var i=0;i{ console.log(error); }) } //选中选择题(单选题)选项保存 ChangeOptionSingle=(item)=>{ let arr=item.target.name let txt=Object.assign({}, this.state.inputArray[parseInt(arr[1])]); let ids=item.target.value; let list=txt.answersList.filter(element => element.answer_id == ids); this.postAnswer(arr[0],ids,list[0].answer_text=="其他"?txt.text:"",arr[1]); } //选中选择题(多选题)选项保存 ChangeOptionMuntil=(a_id,q_id,key)=>{ let ind=Object.assign({}, this.state.inputArray[parseInt(key)]); let count=0; a_id.forEach(element => { let list=ind.answersList.filter(item=>item.answer_id==element); if(list[0].answer_text=="其他"){ count++; } }); this.postAnswer(q_id,a_id,count > 0 ? ind.text : "",key); } postAnswer=(q_id,a_id,text,key)=>{ let url=`/poll_questions/${q_id}/poll_votes.json`; console.log(text); axios.post(url,{ poll_answer_id:a_id, vote_text:text }).then((result)=>{ if(result){ const answer=Object.assign({}, this.state.question_answered[parseInt(key)]); answer.ques_status=result.data.poll_vote.question_status; const question_answered=this.state.question_answered; question_answered[parseInt(key)]=answer const inputs=Object.assign({}, this.state.inputArray[parseInt(key)]); inputs.answers=a_id; const inputArray=this.state.inputArray; inputArray[parseInt(key)]=inputs; this.setState({ question_answered, inputArray }) } }).catch((error)=>{ console.log(error); }) } //输入其它--内容 blurTxt=(e)=>{ let index=parseInt(e.target.name) const list = Object.assign({}, this.state.inputArray[index]) list.text=e.target.value; const inputArray=this.state.inputArray; inputArray[index]=list this.setState({ inputArray }) } //输入其它--内容后重新保存 saveInputAndAnswer=(e,a_id,q_id,key,type)=>{ const inputs=Object.assign({}, this.state.inputArray[parseInt(key)]); let answers=inputs.answers; let flag=true; if(answers.length){ flag=answers.indexOf(a_id) > -1?true:false; answers= type == 1 ? answers[0] : answers; }else{ flag = answers==a_id?true:false; } if(answers && (answers.length>0 || answers !="")){ this.postAnswer(q_id,answers,flag ? inputs.text : "",key); } } //提交主观题 commitText=(key,q_id)=>{ const text = Object.assign({}, this.state.inputArray[key]).text; let url='/poll_questions/'+q_id+'/poll_votes.json?'; const ans=Object.assign({}, this.state.question_answered[key]); ans.ques_status=text ? 1 : 0 ; const questionAnswered=this.state.question_answered; questionAnswered[key]=ans this.setState({ question_answered:questionAnswered }) if(text!=undefined){ axios.post((url),{ vote_text:text }).then((result)=>{ if(result.status==200){ const answer=Object.assign({}, this.state.question_answered[key]); answer.ques_status=result.data.poll_vote.question_status; const question_answered=this.state.question_answered; question_answered[key]=answer this.setState({ question_answered }) } }).catch((error)=>{ console.log(error); }) } } // 提交 submitPoll=()=>{ let {inputArray,question_answered}=this.state; let must=0; let may=0; for(var j=0;j0){ this.setState({ modalsType:true, modalsTopval:"有 "+must+" 题必答题未答,无法提交 ", modalsBottomval:"请完成全部必答题后再提交", loadtype:true, modalSave:this.cancelSubmit }) return; } if(may >= 0){ this.setState({ modalsType:true, modalsTopval: may>0 ? "有 "+may+" 题未答":"", modalsBottomval:"提交后无法再修改答题,是否确认提交?", loadtype:false, modalSave:this.sureSubmit }) } } cancelSubmit=()=>{ this.setState({ modalsType:false, modalsTopval:"", modalsBottomval:"" }) } sureSubmit=()=>{ let pollId=this.props.match.params.pollId; let url=`/polls/${pollId}/commit_poll.json` axios.post(url).then((result)=>{ if(result.status == 200){ this.props.showNotification(result.data.message) this.setState({ modalsType:false, modalsTopval:"", modalsBottomval:"" }) window.location.href=`/courses/${this.props.match.params.coursesId}/polls/${pollId}/detail` } }).catch((error)=>{ console.log(error); }) } render(){ let coursesId=this.props.match.params.coursesId; let{poll,question_answered,question_types,questions,courseName,userName, inputArray, modalsType, modalsTopval, loadtype, modalsBottomval, modalSave, questionPanelFixed }=this.state; const { current_user } = this.props let isAdmin=this.props.isAdmin(); let isStudent=this.props.isStudent(); return(
{/*

*/}

{courseName} > 问卷 > 问卷详情> {question_types&&question_types.user_name}

{poll && poll.polls_name} { isAdmin || (poll && poll.user_poll_status == 1) ? 返回 :'' }

{ poll && poll.polls_description &&

{poll.polls_description}

}

{ question_types && question_types.q_counts > 0 && 合计{question_types.q_counts}题: } { question_types && question_types.q_singles > 0 && 单选题{question_types.q_singles}题 } { question_types && question_types.q_doubles > 0 && 多选题{ question_types && question_types.q_doubles }题 } { question_types && question_types.q_mains > 0 && 主观题{question_types &&question_types.q_mains}题 }

已答 未答 截止时间 {poll && moment(poll.end_time).format("YYYY-MM-DD HH:mm")}

{ questions && questions.map((item,key)=>{ return(
{item.question.question_number}、{map[item.question.question_type]} { item.question.is_necessary==1 ? 必答:选答 } { item.question.question_type == 2 && item.question.min_choices && item.question.max_choices ? { item.question.min_choices == item.question.max_choices ? "可选"+item.question.max_choices+"项" : "可选"+item.question.min_choices+"-"+item.question.max_choices+"项" } :"" }
  • {item.question.question_title}
  • { //单选 item.question.question_type==1 && { item.question.answers && item.question.answers.map((i,k)=>{ return(
  • {i.answer_text} { i.answer_text=="其他" ? { inputArray && inputArray.map((j,k)=>{ return( { j.id == item.question.id ? this.saveInputAndAnswer(e,i.answer_id,item.question.id,key,item.question.question_type)} /> : "" } ) }) } : "" }
  • ) }) }
    } { //多选 item.question.question_type==2 && this.ChangeOptionMuntil(value,item.question.id,key)} disabled={isAdmin || (isStudent && poll && poll.user_poll_status == 1)?true:false} defaultValue={item.question.poll_answer_ids} className="answerList" name={key}> { item.question.answers && item.question.answers.map((i,k)=>{ return(
  • {i.answer_text} { i.answer_text=="其他"? { inputArray && inputArray.map((j,k)=>{ return( { j.id == item.question.id ? this.saveInputAndAnswer(e,i.answer_id,item.question.id,key,item.question.question_type)} /> : "" } ) }) } :"" }
  • ) }) }
    } { //主观题 item.question.question_type==3 &&
    { inputArray && inputArray.map((j,k)=>{ return( { j.id == item.question.id ? : "" } ) }) }
    }
    ) }) }
    { isStudent && poll && poll.user_poll_status == 0 ?
    提交
    :"" }
    ) } } export default PollInfo;