UNPKG

unserver-unify

Version:

251 lines (248 loc) 7.32 kB
angular.module('bamboo').service('CoursewareService', function($q, loginService, ApiService) { var vm = this; vm.checkresult = checkresult; vm.recordUsage = recordUsage; function checkresult(chapter, questions, _historydetail, views, _last_rid) { // console.log(questions); var correct = 0; var wrong = 0; for (var i = 0; i < questions.length; i += 1) { var q = questions[i]; checkQResult(q); if (q.judgement == 'Correct') { correct++; } else { wrong++; } } if (!_historydetail[_last_rid]) { _historydetail[_last_rid] = { len: 0, result: { correct: correct, wrong: wrong, }, count: 1, } } else { _historydetail[_last_rid].result = { correct: correct, wrong: wrong, } if (!_historydetail[_last_rid].count) { _historydetail[_last_rid].count = 1; } else { _historydetail[_last_rid].count++; } } if (!wrong) { _historydetail[_last_rid].pass = true; // recordUsage(chapter, flag, _historydetail, views); recordUsage({ prechapter: chapter, flag: false, _historydetail: _historydetail, views: views }); } //console.log(_historydetail); } //prechapter, flag, _historydetail, views, _last_rid, _lasttime function recordUsage(info) { console.log(info); var prechapter = info.prechapter; var res_id = info.prechapter._id; var flag = info.flag; var _historydetail = info._historydetail; var views = info.views; var _last_rid = info._last_rid; var _lasttime = info._lasttime; var _total = info._total; var courseid = info.courseid; var mvid = info.mvid; // var mvsubject = info.mvsubject; var chapters = info.chapters; var _now = new Date(); if (_last_rid) { var delta = (_now - _lasttime) / 1000; _total += delta; if (!_historydetail[_last_rid]) { _historydetail[_last_rid] = { len: delta, } } else { if (!_historydetail[_last_rid].len) { _historydetail[_last_rid].len = 0; } _historydetail[_last_rid].len += delta; } } if (prechapter && (prechapter.type === 'mp4' || prechapter.type === 'mp4ppt' || prechapter.type === 'mp3ppt')) { var _chapterid = prechapter._id; prechapter.mp4time = parseInt(_self.API.currentTime / 1000); if (!_historydetail[_chapterid]) { _historydetail[_chapterid] = { mp4time: _self.API.currentTime, } } else { _historydetail[_chapterid].mp4time = parseInt(_self.API.currentTime / 1000); } } _last_rid = res_id; if (res_id && views.indexOf(res_id) < 0) { views.push(res_id); } _lasttime = _now; var trigger = false; var passed = false; var passedid = {}; if (flag) { trigger = true; } else { for (var key in _historydetail) { var record = _historydetail[key]; if (record.pass) { trigger = true; passed = true; passedid[key] = true; break; } } } if (trigger && loginService.user) { var info = { action: "resourceusage", ids: views, detail: _historydetail, total: _total, cid: courseid, rid: _last_rid, // mvid: _self.id, mvid: mvid, }; if (passed && prechapter.taskFlag && chapters) { var _pass = true; for (var i = 0; i < chapters.length; i++) { var item = chapters[i]; if (item.taskFlag && !item.pass && !passedid[item.id]) { _pass = false; break; } } if (_pass) { info.passmvid = _self.id; } } return submitUsage(info); } } function checkQResult(q) { switch (q.type) { case 'single': // console.log(q) angular.forEach(q._options, function(opt, index) { // console.log(opt); // console.log(index); if (opt.correct && index == q.select) { q.judgement = 'Correct'; q.selection = opt.text; console.log('correct'); } }) if (q.judgement != 'Correct') { if (q.select || q.select == 0) { q.judgement = 'Wrong'; q.selection = q._options[q.select].text; } else { q.judgement = 'Not Attempted'; } } break; case 'multiple': var opts = q._options; console.log("--------multiple---------"); // console.log(opts); var _selected = 0; var _correct = true; for (var j = 0; j < opts.length; j += 1) { var opt = opts[j]; console.log(opt); if (opt.selection) { _selected++; } if ((opt.selection && opt.correct) || (!opt.selection && !opt.correct)) {} else { _correct = false; console.log('wrong selection'); } } console.log(_selected); console.log(_correct); if (_selected) { if (_correct) { q.judgement = 'Correct'; } else { q.judgement = 'Wrong'; } } else { q.judgement = 'Not Attempted'; } break; case 'tof': console.log("--------tof---------"); console.log(q); q.tof_answer = q._options[0] && q._options[0].correct; if (!q.selection) { q.judgement = 'Not Attempted'; } else if (q.selection == 'true' && q.tof_answer) { q.judgement = 'Correct'; } else if (q.selection == 'false' && !q.tof_answer) { q.judgement = 'Correct'; } else { q.judgement = 'Wrong'; } break; case 'fill': case 'mfill': console.log(q); var opts = q._options; console.log("---------M Fills--------"); // console.log(opts); q.judgement = 'Correct'; var _selected = 0; var _correct = true; for (var j = 0; j < opts.length; j += 1) { var opt = opts[j]; if (opt.selection) { _selected++; var _result = false; for (var n = 0; n < opt.texts.length; n++) { if (opt.selection == opt.texts[n].text) { _result = true;; break; } } if (_result) {} else { _correct = false; console.log(opt); } } else { _correct = false; } } if (_selected) { if (_correct) { q.judgement = 'Correct'; } else { q.judgement = 'Wrong'; } } else { q.judgement = 'Not Attempted'; } break; } return q; } function submitUsage(info) { return ApiService.post('/mvsubjects', info); } });