UNPKG

unserver-unify

Version:

304 lines (286 loc) 9.05 kB
'use strict'; angular.module('bamboo.course').controller('CourseAssignmentCtrl', function($scope, ApiService, CommonService, ExamService, $sce, CourseApi, Lightbox, $stateParams, printHelper, timeHelper, $rootScope, $translate) { var _self = this; this.id = $stateParams.id; //this.cid = $stateParams.cid; this.aid = $stateParams.aid; console.log($stateParams); this.assignment = {}; this.assignmentrecord = {}; this.audio; // $scope.tabs = 'doc'; this.audiourl = ApiService.SHOST + '/assignment/' + _self.aid + '/'; this.newAudioUrl = ApiService.SHOST + '/lcourse/' + _self.cid + '/'; console.log(" assignment controller "); function showAudio(version) { _self.audio = {}; if (version == 'v1') { _self.audio = $sce.trustAsResourceUrl(_self.audiourl + encodeURI(_self.assignment.audio)); } else if (version == 'v2') { _self.audio = $sce.trustAsResourceUrl(_self.newAudioUrl + encodeURI(_self.assignment.audioFile)); console.log(_self.newAudioUrl + _self.assignment.audioFile); } console.log(_self.audio); var doc = angular.element($('#audiodiv')); doc.empty(); var audioElement = document.createElement('audio'); audioElement.src = _self.audio; // audioElement.autoplay = true; audioElement.controls = 'controls'; doc.append(audioElement); } function showAudioV2() { _self.audio = $sce.trustAsResourceUrl(_self.newAudioUrl + encodeURI(_self.assignment.audioFile)); console.log(_self.audio); //console.log(_self.audiourl + _self.assignment.audio); var doc = angular.element($('#audiodiv')); doc.empty(); var audioElement = document.createElement('audio'); audioElement.src = _self.audio; // audioElement.autoplay = true; audioElement.controls = 'controls'; doc.append(audioElement); } function getInfo() { var info = { action: 'getassignment', id: _self.aid, }; CourseApi.post('/exam', info, function(result) { console.log(result); _self.assignment = result; $rootScope.assignmentName = result.title; if (result.timecontrol) { _self.assignmentValid = timeHelper.validateTime(result.start, result.stop); } else { _self.assignmentValid = true; } _self.cid = result.cid; _self.newAudioUrl = ApiService.SHOST + '/lcourse/' + _self.cid + '/'; if (_self.assignment.audio) { showAudio('v1'); } else if (!_self.assignment.audio && _self.assignment.audioFile) { showAudio('v2'); } if (_self.assignment.type && _self.assignment.type != 'text' && _self.assignment.paperId) { var _info = { id: _self.assignment.paperId, action: "getpaper", } if (_self.assignment.type == "Dynamic") { _info.action = "getdpaper"; } else if (_self.assignment.type == "Smart") { _info.action = "getspaper"; } ExamService.api(_info, function(data) { _self.paper = data; console.log(_self.paper); }); } else { var inforecord = { action: "getuserassignmentrecordv1", aid: _self.aid, cid: _self.assignment.cid, }; CourseApi.post('/exam', inforecord, function(result) { console.log(result); if(result){ _self.assignmentrecord = result; if (!_self.assignmentrecord.files) { _self.assignmentrecord.files = []; } if (_self.assignmentrecord.audio) { _self.assignmentrecord.audiourl = $sce.trustAsResourceUrl(_self.audiourl + encodeURI(_self.assignmentrecord.audio)); console.log(_self.audiourl + _self.assignmentrecord.audio); } } }); } }); }; this.deletefiles = function(index) { CommonService.showDeleteConfirm(function(){ if (index < _self.assignmentrecord.files.length) { _self.assignmentrecord.files.splice(index, 1); var object = { files: _self.assignmentrecord.files, }; updateAssignmentRecord(object); } }); } this.update = function() { console.log(_self.assignmentrecord.content); if (!_self.assignmentrecord.content) { CommonService.showNoBlockErr('The content can not be blank!'); return; } if (_self.assignmentrecord.content.length<10) { CommonService.showNoBlockErr('The content is too short!'); return; } var info = { object: { content: _self.assignmentrecord.content }, action: "updateassignmentrecord", id: _self.assignmentrecord._id, aid:_self.aid, cid: _self.assignment.cid, } CourseApi.post('/exam', info, function() { CommonService.showNoBlockInfo($translate.instant('Update Successful!')); getInfo(); }); }; var filesetting = { promise: true, s3: true, title: 'Select Attachment', maxSize: 200000, randomFlag: true, resPath: 'assignment/' + _self.aid + '/', }; function updateAssignmentRecord(object) { var info = { object: object, action: "updateassignmentrecord", id: _self.assignmentrecord._id, aid:_self.aid, cid: _self.assignment.cid, }; CourseApi.post('/exam', info, function() { CommonService.showNoBlockInfo($translate.instant('Update Successful!')); getInfo(); }); } this.publish=function() { var info = { action: "publishAssignmentRecord", id: _self.assignmentrecord._id, }; CourseApi.post('/exam', info, function() { CommonService.showNoBlockInfo($translate.instant('Update Successful!')); getInfo(); }); } this.typeclassname = { html: "fa fa-html5", ppts: "fa fa-file-powerpoint-o", mp4: 'fa fa-video-camera', youtube: 'fa fa-youtube-play', flash: 'fa fa-line-chart', test: 'fa fa-pencil-square-o', pdf: 'fa fa-file-pdf-o', docs: 'fa fa-file-word-o', docx: 'fa fa-file-word-o', ppt: 'fa fa-file-powerpoint-o', xls: 'fa fa-file-excel-o', xlsx: 'fa fa-file-excel-o', mp3: 'fa fa-music', img: 'fa fa-picture-o', }; var typelist = { pdf: "pdf", doc: "doc", docx: "doc", ppt: "ppt", pptx: "ppt", xls: "xls", xlsx: "xls", mp3: "mp3", mp4: "mp4", flv: "mp4", avi: "mp4", gif: 'img', jpg: 'img', jpeg: 'img', tiff: 'img', png: 'img', }; this.upload = function() { console.log(filesetting); CommonService.uploadFile(filesetting).then(function(data) { var filename = data.fileName; var info = { file: filename, type: 'file' } var filearray = filename.split("."); var ext = filearray[filearray.length - 1]; var type = typelist[ext]; if (type) { info.type = type; } if(!_self.assignmentrecord.files){ _self.assignmentrecord.files=[]; } _self.assignmentrecord.files.push(info); console.log(data); if (data.result) { var object = { files: _self.assignmentrecord.files, content: _self.assignmentrecord.content }; updateAssignmentRecord(object); } else { CommonService.showError("Update failed: " + data.message); } }); } this.deletefile = function() { updateAssignmentRecord({ file: null }); } var audioUploadSetting = { image: false, accept: "audio/*", audiorecord: true, s3: true, promise: true, title: "Audio", maxSize: 5120, randomFlag: true, resPath: 'assignment/' + _self.aid + '/', }; this.uploadAudio = function() { CommonService.uploadFile(audioUploadSetting).then(function(data) { if (data.result) { console.log(data); updateAssignmentRecord({ audio: data.fileName, content: _self.assignmentrecord.content }); } else { CommonService.showError("Update failed: " + data.message); } }); }; this.init = function(aid) { console.log("-- init --"); this.aid = aid; getInfo(); this.audiourl = ApiService.SHOST + '/assignment/' + _self.aid + '/'; filesetting.resPath = 'assignment/' + _self.aid + '/'; audioUploadSetting.resPath = 'assignment/' + _self.aid + '/'; } if (this.aid) { getInfo(); } $scope.openLightboxModal = function(url) { Lightbox.openModal([{ url: url }], 0); }; this.download = function() { printHelper.printPage(); } this.downloadFile = function(url) { printHelper.downloadFileByURL(url); } this.toggle = function() { console.log('----'); _self.tlg = !_self.tlg; } });