2020-03-04 11:07:12 +00:00
angular . module ( "AtsebaytApp" , [ "ngRoute" , "ngResource" , "ngSanitize" ] )
. config ( function ( $routeProvider , $locationProvider ) {
$routeProvider
. when ( "/auth" , {
controller : "AuthController" ,
templateUrl : "views/auth.html"
} )
2020-11-20 14:46:52 +00:00
. when ( "/grades" , {
controller : "GradesController" ,
templateUrl : "views/grades.html"
} )
2020-03-04 11:07:12 +00:00
. when ( "/surveys" , {
controller : "SurveysController" ,
templateUrl : "views/surveys.html"
} )
. when ( "/surveys/:surveyId" , {
controller : "SurveyController" ,
templateUrl : "views/survey.html"
} )
2020-03-08 00:06:44 +00:00
. when ( "/surveys/:surveyId/responses" , {
controller : "SurveyController" ,
templateUrl : "views/responses.html"
2020-03-04 11:07:12 +00:00
} )
2020-03-08 00:06:44 +00:00
. when ( "/surveys/:surveyId/responses/:questId" , {
controller : "QuestionController" ,
templateUrl : "views/correction.html"
2020-03-04 11:07:12 +00:00
} )
2020-11-20 14:46:52 +00:00
. when ( "/users" , {
controller : "UsersController" ,
templateUrl : "views/users.html"
} )
. when ( "/users/:userId" , {
controller : "UserController" ,
templateUrl : "views/user.html"
} )
2020-11-28 17:59:14 +00:00
. when ( "/users/:userId/surveys/:surveyId" , {
controller : "SurveyController" ,
templateUrl : "views/survey.html"
} )
2020-03-04 11:07:12 +00:00
. when ( "/" , {
2020-03-08 00:06:44 +00:00
controller : "SurveysController" ,
2020-03-04 11:07:12 +00:00
templateUrl : "views/home.html"
} ) ;
$locationProvider . html5Mode ( true ) ;
} ) ;
angular . module ( "AtsebaytApp" )
2020-03-08 00:06:44 +00:00
. factory ( "AllResponses" , function ( $resource ) {
return $resource ( "/api/surveys/:surveyId/questions/:questId/responses/:respId" , { surveyId : '@id' , questId : '@id' , respId : '@id' } , {
'update' : { method : 'PUT' } ,
} )
} )
2020-09-13 14:37:15 +00:00
. factory ( "CorrectionTemplate" , function ( $resource ) {
return $resource ( "/api/surveys/:surveyId/questions/:questId/corrections/:correctId" , { surveyId : '@id' , questId : '@id' , correctId : '@id' } , {
'update' : { method : 'PUT' } ,
} )
} )
2020-03-08 00:06:44 +00:00
. factory ( "MyResponse" , function ( $resource ) {
return $resource ( "/api/surveys/:surveyId/responses/:respId" , { surveyId : '@id' , respId : '@id' } )
} )
2020-11-30 09:31:38 +00:00
. factory ( "UserResponses" , function ( $resource ) {
2020-11-28 17:59:14 +00:00
return $resource ( "/api/users/:userId/surveys/:surveyId/responses/:respId" , { userId : '@id' , surveyId : '@id' , respId : '@id' } )
} )
2020-11-20 14:46:52 +00:00
. factory ( "Grades" , function ( $resource ) {
return $resource ( "/api/grades" )
} )
2020-03-04 11:07:12 +00:00
. factory ( "Survey" , function ( $resource ) {
return $resource ( "/api/surveys/:surveyId" , { surveyId : '@id' } , {
'update' : { method : 'PUT' } ,
} )
} )
2020-11-20 14:46:52 +00:00
. factory ( "SurveyGrades" , function ( $resource ) {
return $resource ( "/api/surveys/:surveyId/grades" , { surveyId : '@id' } )
} )
2020-03-08 00:06:44 +00:00
. factory ( "SurveyScore" , function ( $resource ) {
return $resource ( "/api/surveys/:surveyId/score" , { surveyId : '@id' } )
} )
2020-11-20 14:46:52 +00:00
. factory ( "SurveyUserScore" , function ( $resource ) {
return $resource ( "/api/users/:userId/surveys/:surveyId/score" , { userId : '@id' , surveyId : '@id' } )
} )
. factory ( "SurveyUserGrades" , function ( $resource ) {
return $resource ( "/api/users/:userId/surveys/:surveyId/grades" , { userId : '@id' , surveyId : '@id' } )
} )
2020-03-04 11:07:12 +00:00
. factory ( "SurveyQuest" , function ( $resource ) {
return $resource ( "/api/surveys/:surveyId/questions/:questId" , { surveyId : '@id' , questId : '@id' } , {
'update' : { method : 'PUT' } ,
} )
} )
2020-03-20 23:02:18 +00:00
. factory ( "QuestProposal" , function ( $resource ) {
2020-11-20 15:41:06 +00:00
return $resource ( "/api/surveys/:surveyId/questions/:questId/proposals/:proposalId" , { surveyId : '@id' , questId : '@id' , proposalId : '@id' } , {
2020-03-20 23:02:18 +00:00
'update' : { method : 'PUT' } ,
} )
} )
2020-03-04 11:07:12 +00:00
. factory ( "User" , function ( $resource ) {
return $resource ( "/api/users/:userId" , { userId : '@id' } , {
'update' : { method : 'PUT' } ,
} )
} ) ;
2020-03-08 00:06:44 +00:00
angular . module ( "AtsebaytApp" )
. directive ( 'integer' , function ( ) {
return {
require : 'ngModel' ,
link : function ( scope , ele , attr , ctrl ) {
ctrl . $parsers . unshift ( function ( viewValue ) {
return parseInt ( viewValue , 10 ) ;
} ) ;
}
} ;
} )
. component ( 'toast' , {
bindings : {
date : '=' ,
msg : '=' ,
title : '=' ,
variant : '=' ,
} ,
controller : function ( $element ) {
$element . children ( 0 ) . toast ( 'show' )
} ,
template : ` <div class="toast mb-2" role="alert" aria-live="assertive" aria-atomic="true" data-delay="7000">
< div class = "toast-header" >
< span ng - if = "$ctrl.variant" class = "badge badge-pill badge-{{ $ctrl.variant }}" style = "padding: .25em .66em" > & nbsp ; < / s p a n > & n b s p ;
< strong class = "mr-auto" ng - bind = "$ctrl.title" > < / s t r o n g >
< small class = "text-muted" ng - bind = "$ctrl.date" > just now < / s m a l l >
< button type = "button" class = "ml-2 mb-1 close" data - dismiss = "toast" aria - label = "Close" >
< span aria - hidden = "true" > & times ; < / s p a n >
< / b u t t o n >
< / d i v >
< div class = "toast-body" ng - bind - html = "$ctrl.msg" > < / d i v >
< / d i v > `
} )
. component ( 'surveyList' , {
bindings : {
surveys : '=' ,
islogged : '=' ,
isadmin : '=' ,
} ,
controller : function ( $location , $rootScope ) {
this . now = Date . now ( ) ;
this . show = function ( id ) {
if ( $rootScope . isLogged ) {
$location . url ( "surveys/" + id ) ;
} else {
$rootScope . addToast ( {
variant : "danger" ,
title : "Authentification requise" ,
msg : "Vous devez être connecté pour accéder aux questionnaires." ,
} ) ;
$location . url ( "auth" ) ;
}
} ;
} ,
template : ` <table class="table table-striped table-hover">
< thead >
< tr >
< th > Intitulé < / t h >
< th > État < / t h >
< th > Date < / t h >
< th ng - if = "$ctrl.islogged" > Score < / t h >
< / t r >
< / t h e a d >
< tbody style = "cursor: pointer;" >
< tr ng - repeat = "survey in $ctrl.surveys" ng - if = "survey.shown" ng - click = "$ctrl.show(survey.id)" >
< td > { { survey . title } } < / t d >
< td class = "bg-info" ng - if = "survey.start_availability > $ctrl.now" > Prévu < / t d >
< td class = "bg-warning" ng - if = "survey.start_availability <= $ctrl.now && survey.end_availability >= $ctrl.now" > En cours < / t d >
< td class = "bg-primary" ng - if = "survey.end_availability < $ctrl.now && !survey.corrected" > Terminé < / t d >
< td class = "bg-success" ng - if = "survey.end_availability < $ctrl.now && survey.corrected" > Corrigé < / t d >
2020-03-08 01:18:53 +00:00
< td ng - if = "survey.start_availability > $ctrl.now" > { { survey . start _availability | date : "medium" } } < svg class = "bi bi-arrow-bar-right" width = "1em" height = "1em" viewBox = "0 0 20 20" fill = "currentColor" xmlns = "http://www.w3.org/2000/svg" > < path fill - rule = "evenodd" d = "M12.146 6.646a.5.5 0 01.708 0l3 3a.5.5 0 010 .708l-3 3a.5.5 0 01-.708-.708L14.793 10l-2.647-2.646a.5.5 0 010-.708z" clip - rule = "evenodd" > < / p a t h > < p a t h f i l l - r u l e = " e v e n o d d " d = " M 8 1 0 a . 5 . 5 0 0 1 . 5 - . 5 H 1 5 a . 5 . 5 0 0 1 0 1 H 8 . 5 A . 5 . 5 0 0 1 8 1 0 z m - 2 . 5 6 a . 5 . 5 0 0 1 - . 5 - . 5 v - 1 1 a . 5 . 5 0 0 1 1 0 v 1 1 a . 5 . 5 0 0 1 - . 5 . 5 z " c l i p - r u l e = " e v e n o d d " > < / p a t h > < / s v g > < / t d >
< td ng - if = "survey.start_availability <= $ctrl.now" > < svg class = "bi bi-arrow-bar-left" width = "1em" height = "1em" viewBox = "0 0 20 20" fill = "currentColor" xmlns = "http://www.w3.org/2000/svg" > < path fill - rule = "evenodd" d = "M7.854 6.646a.5.5 0 00-.708 0l-3 3a.5.5 0 000 .708l3 3a.5.5 0 00.708-.708L5.207 10l2.647-2.646a.5.5 0 000-.708z" clip - rule = "evenodd" > < / p a t h > < p a t h f i l l - r u l e = " e v e n o d d " d = " M 1 2 1 0 a . 5 . 5 0 0 0 - . 5 - . 5 H 5 a . 5 . 5 0 0 0 0 1 h 6 . 5 a . 5 . 5 0 0 0 . 5 - . 5 z m 2 . 5 6 a . 5 . 5 0 0 1 - . 5 - . 5 v - 1 1 a . 5 . 5 0 0 1 1 0 v 1 1 a . 5 . 5 0 0 1 - . 5 . 5 z " c l i p - r u l e = " e v e n o d d " > < / p a t h > < / s v g > { { s u r v e y . e n d _ a v a i l a b i l i t y | d a t e : " m e d i u m " } } < / t d >
2020-09-13 14:37:15 +00:00
< td ng - if = "$ctrl.islogged && !survey.corrected" > N / A < / t d >
< td ng - if = "$ctrl.islogged && survey.corrected" ng - controller = "ScoreController" > { { score . score } } < / t d >
2020-03-08 00:06:44 +00:00
< / t r >
< / t b o d y >
< tfoot ng - if = "$ctrl.isadmin" >
< tr >
< td colspan = "4" >
< a href = "surveys/new" class = "btn btn-sm btn-primary" > Ajouter un questionnaire < / a >
< / t d >
< / t r >
< / t f o o t >
< / t a b l e > `
} )
. component ( 'surveyBadges' , {
bindings : {
survey : '=' ,
} ,
controller : function ( ) {
var now = Date . now ( )
this . test = function ( a , b ) {
return Date . parse ( a ) > now ;
}
} ,
template : ` <span class="badge badge-danger" ng-if=" $ ctrl.test( $ ctrl.survey.start_availability)">Prévu</span>
< span class = "badge badge-warning" ng - if = "!$ctrl.test($ctrl.survey.start_availability) && $ctrl.test($ctrl.survey.end_availability)" > En cours < / s p a n >
< span class = "badge badge-primary" ng - if = "!$ctrl.test($ctrl.survey.end_availability)" > Clos < / s p a n >
< span class = "badge badge-success" ng - if = "$ctrl.survey.corrected" > Corrigé < / s p a n > `
} ) ;
2020-03-04 11:07:12 +00:00
angular . module ( "AtsebaytApp" )
. run ( function ( $rootScope , $interval , $http ) {
$rootScope . checkLoginState = function ( ) {
$http ( {
method : 'GET' ,
url : "api/auth" ,
} ) . then ( function ( response ) {
$rootScope . isLogged = response . data ;
$rootScope . user = response . data ;
} , function ( response ) {
$rootScope . isLogged = false ;
} ) ;
} ;
$rootScope . checkLoginState ( ) ;
$interval ( $rootScope . checkLoginState , 20000 ) ;
$rootScope . switchAdminMode = function ( ) {
var tmp = $rootScope . user . is _admin
$rootScope . user . is _admin = $rootScope . user . was _admin || false ;
$rootScope . user . was _admin = tmp ;
}
2020-03-08 00:06:44 +00:00
$rootScope . toasts = [ ] ;
$rootScope . addToast = function ( toast ) {
$rootScope . toasts . unshift ( toast ) ;
}
2020-03-04 11:07:12 +00:00
$rootScope . disconnectCurrentUser = function ( ) {
$http ( {
method : 'POST' ,
url : "api/auth/logout"
} ) . then ( function ( response ) {
$rootScope . isLogged = false ;
$rootScope . user = null ;
} ) ;
}
} )
. controller ( "AuthController" , function ( $scope , $rootScope , $http , $location ) {
$scope . auth = {
"username" : "" ,
"password" : "" ,
} ;
$scope . logmein = function ( ) {
$scope . pleaseWait = true ;
$http ( {
method : 'POST' ,
url : "api/auth" ,
data : $scope . auth
} ) . then ( function ( response ) {
$scope . pleaseWait = false ;
$rootScope . checkLoginState ( ) ;
$location . url ( "/" ) ;
} , function ( response ) {
$scope . pleaseWait = false ;
2021-09-22 15:02:30 +00:00
if ( response . data )
$scope . addToast ( {
variant : "danger" ,
title : "Connexion impossible" ,
msg : ( response . data ? response . data . errmsg : "Impossible de contacter le serveur" ) ,
} ) ;
2020-03-04 11:07:12 +00:00
} ) ;
}
} )
2020-03-08 00:06:44 +00:00
. controller ( "SurveysController" , function ( $scope , $rootScope , Survey , $location ) {
$rootScope . qactive = $location . $$path == "/surveys" ;
2020-11-20 14:46:52 +00:00
$rootScope . uactive = $location . $$path . indexOf ( "/users" ) != - 1 ;
2020-03-04 11:07:12 +00:00
$scope . surveys = Survey . query ( ) ;
$scope . surveys . $promise . then ( function ( data ) {
data . forEach ( function ( d , k ) {
data [ k ] . start _availability = Date . parse ( data [ k ] . start _availability )
data [ k ] . end _availability = Date . parse ( data [ k ] . end _availability )
} )
} )
2020-11-28 17:59:14 +00:00
$scope . showUserSurvey = function ( ) {
$location . url ( "users/" + this . user . id + "/surveys/" + this . survey . id ) ;
}
2020-03-04 11:07:12 +00:00
} )
2021-03-01 17:00:28 +00:00
. controller ( "SurveyController" , function ( $scope , $rootScope , Survey , SurveyQuest , $routeParams , $location ) {
2020-03-08 00:06:44 +00:00
$rootScope . qactive = true ;
2020-11-20 14:46:52 +00:00
$rootScope . uactive = false ;
2020-03-04 11:07:12 +00:00
$scope . survey = Survey . get ( { surveyId : $routeParams . surveyId } ) ;
$scope . survey . $promise . then ( function ( survey ) {
2020-03-08 00:06:44 +00:00
survey . readonly = Date . now ( ) > Date . parse ( survey . end _availability )
2020-03-04 11:07:12 +00:00
} )
$scope . saveSurvey = function ( ) {
if ( this . survey . id ) {
this . survey . $update ( function ( v ) {
$scope . survey = Survey . get ( { surveyId : $routeParams . surveyId } ) ;
} ) ;
} else {
this . survey . $save ( function ( ) {
$location . url ( "surveys/" + $scope . survey . id ) ;
} ) ;
}
}
2021-03-01 17:00:28 +00:00
$scope . duplicateSurvey = function ( ) {
var bakSurveyId = this . survey . id ;
delete this . survey . id ;
this . survey . $save ( function ( ) {
// Now recopy questions
var questions = SurveyQuest . query ( { surveyId : bakSurveyId } ) ;
questions . $promise . then ( function ( questions ) {
questions . forEach ( function ( question ) {
delete question . id
question . $save ( { surveyId : $scope . survey . id } ) ;
} )
$location . url ( "surveys/" + $scope . survey . id ) ;
} )
} ) ;
}
2020-03-04 11:07:12 +00:00
$scope . editSurvey = function ( ) {
this . survey . edit = true ;
}
$scope . deleteSurvey = function ( ) {
this . survey . $remove ( function ( ) { $location . url ( "/surveys/" ) ; } ) ;
}
2020-03-08 00:06:44 +00:00
$scope . showResponses = function ( ) {
$location . url ( "surveys/" + this . survey . id + "/responses/" + this . question . id ) ;
}
} )
2020-11-20 14:46:52 +00:00
. controller ( "SurveyGradesController" , function ( $scope , SurveyGrades ) {
$scope . grades = SurveyGrades . get ( { surveyId : $scope . survey . id } )
$scope . mean = 0 ;
$scope . grades . $promise . then ( function ( grades ) {
var sum = 0
var total = 0
for ( var gid in grades ) {
if ( parseInt ( gid , 10 ) > 0 ) {
total ++
if ( grades [ gid ] ) {
sum += grades [ gid ]
}
}
}
if ( total > 0 ) {
$scope . mean = sum / total
}
} )
} )
2020-11-30 09:31:38 +00:00
. controller ( "ResponsesController" , function ( $scope , AllResponses , $rootScope , $location ) {
2020-03-08 00:06:44 +00:00
$scope . responses = AllResponses . query ( { surveyId : $scope . survey . id , questId : $scope . question . id } ) ;
2020-11-30 09:31:38 +00:00
$scope . responses . $promise . then ( function ( responses ) {
if ( ! $rootScope . usersResponses ) {
$rootScope . usersResponses = { }
}
responses . forEach ( function ( response ) {
if ( ! response . time _scored ) {
if ( $rootScope . usersResponses [ response . id _user ] === undefined ) {
$rootScope . usersResponses [ response . id _user ] = [ ]
}
$rootScope . usersResponses [ response . id _user ] . push ( response . id _question )
}
} ) ;
} )
$rootScope . showUserSurvey = function ( ) {
$location . url ( "users/" + this . id _user + "/surveys/" + this . survey . id ) ;
}
2020-03-08 00:06:44 +00:00
} )
. controller ( "ScoreController" , function ( $scope , SurveyScore ) {
2020-09-13 14:37:15 +00:00
$scope . score = SurveyScore . get ( { surveyId : $scope . survey . id } )
2020-03-08 00:06:44 +00:00
} )
2020-11-20 14:46:52 +00:00
. controller ( "UserScoreController" , function ( $scope , SurveyUserScore ) {
$scope . score = SurveyUserScore . get ( { userId : $scope . user . id , surveyId : $scope . survey . id } )
} )
. controller ( "UserGradesController" , function ( $scope , SurveyUserGrades ) {
$scope . grades = SurveyUserGrades . get ( { userId : $scope . user . id , surveyId : $scope . survey . id } )
$scope . avancement = 0 ;
$scope . grades . $promise . then ( function ( grades ) {
var answered = 0
var total = 0
for ( var gid in grades ) {
if ( parseInt ( gid , 10 ) > 0 ) {
total ++
if ( grades [ gid ] ) {
answered ++
}
}
}
if ( total > 0 ) {
$scope . avancement = answered / total
}
} )
} )
. controller ( "GradesController" , function ( $scope , $rootScope , Grades , Survey , User , $location ) {
$scope . users = User . query ( ) ;
$scope . surveys = Survey . query ( ) ;
$scope . grades = Grades . get ( ) ;
$scope . showUser = function ( ) {
$location . url ( "users/" + this . user . id ) ;
}
} )
. controller ( "UsersController" , function ( $scope , $rootScope , User , $location ) {
$rootScope . qactive = false ;
$rootScope . uactive = true ;
$scope . users = User . query ( )
$scope . showUser = function ( ) {
$location . url ( "users/" + this . user . id ) ;
}
} )
. controller ( "UserController" , function ( $scope , $routeParams , $rootScope , User ) {
$rootScope . qactive = false ;
$rootScope . uactive = true ;
2020-11-30 09:31:38 +00:00
if ( $scope . id _user ) {
$scope . user = User . get ( { userId : $scope . id _user } )
} else {
$scope . user = User . get ( { userId : $routeParams . userId } )
}
2020-11-20 14:46:52 +00:00
} )
2020-11-28 17:59:14 +00:00
. controller ( "QuestionController" , function ( $scope , Survey , SurveyQuest , AllResponses , UserResponses , CorrectionTemplate , $http , $routeParams ) {
2020-09-13 15:59:57 +00:00
$scope . notCorrected = true
2020-09-13 17:12:21 +00:00
$scope . highlight = ''
$scope . chHilight = function ( ) {
var words = $scope . highlight . split ( ',' ) . join ( '|' )
$ ( '.card-text' ) . each ( function ( k , i ) {
var e = $ ( i )
2020-09-14 08:24:29 +00:00
e . html ( e . text ( ) . replace ( new RegExp ( '(' + words + ')' , 'gi' ) , '<span class="bg-warning">$1</span>' ) )
2020-09-13 17:12:21 +00:00
} )
}
2020-03-08 00:06:44 +00:00
$scope . survey = Survey . get ( { surveyId : $routeParams . surveyId } ) ;
$scope . survey . $promise . then ( function ( survey ) {
survey . start _availability = Date . parse ( survey . start _availability )
survey . end _availability = Date . parse ( survey . end _availability )
} )
$scope . question = SurveyQuest . get ( { surveyId : $routeParams . surveyId , questId : $routeParams . questId } ) ;
2020-11-28 17:59:14 +00:00
if ( $routeParams . userId == null ) {
$scope . responses = AllResponses . query ( { surveyId : $routeParams . surveyId , questId : $routeParams . questId } ) ;
} else {
$scope . responses = UserResponses . query ( { userId : $routeParams . userId , surveyId : $routeParams . surveyId , questId : $routeParams . questId } ) ;
}
$scope . responses . $promise . then ( function ( responses ) {
2020-09-13 14:37:15 +00:00
$scope . users _corrected = { }
responses . forEach ( function ( r ) {
$http ( {
url : "/api/users/" + r . id _user + "/questions/" + $routeParams . questId
} ) . then ( function ( response ) {
$scope . users _corrected [ r . id _user ] = response . data
} )
} )
} ) ;
2020-03-08 00:06:44 +00:00
$scope . submitCorrection = function ( ) {
2020-09-13 14:37:15 +00:00
if ( $scope . users _corrected [ this . response . id _user ] ) {
if ( this . response . score === undefined )
this . response . score = $scope . users _corrected [ this . response . id _user ] . score
if ( $scope . users _corrected [ this . response . id _user ] . score _explaination && ( ! this . response . score _explaination || this . response . score _explaination . indexOf ( $scope . users _corrected [ this . response . id _user ] . score _explaination ) == - 1 ) ) {
if ( this . response . score _explaination )
this . response . score _explaination += '\n' + $scope . users _corrected [ this . response . id _user ] . score _explaination
else
this . response . score _explaination = $scope . users _corrected [ this . response . id _user ] . score _explaination
}
}
this . response . id _corrector = $scope . user . id
if ( ! this . response . time _scored )
this . response . time _scored = ( new Date ( ) ) . toISOString ( )
2020-03-08 00:06:44 +00:00
this . response . $update ( )
}
$scope . submitCorrections = function ( ) {
this . responses . forEach ( function ( response ) {
response . $update ( )
} )
}
2020-09-13 14:37:15 +00:00
$scope . templates = CorrectionTemplate . query ( { surveyId : $routeParams . surveyId , questId : $routeParams . questId } , function ( tpls ) {
$scope . template _corrected = { }
tpls . forEach ( function ( tpl ) {
$scope . template _corrected [ tpl . id ] = { }
CorrectionTemplate . query ( { surveyId : $routeParams . surveyId , questId : $routeParams . questId , correctId : tpl . id } , function ( cts ) {
cts . forEach ( function ( ct ) {
$scope . template _corrected [ tpl . id ] [ ct . id _user ] = ct
} )
} ) ;
} )
} ) ;
$scope . changeCorrection = function ( id _user ) {
var tpl = this . template
if ( $scope . template _corrected [ tpl . id ] && $scope . template _corrected [ tpl . id ] [ id _user ] ) {
$http ( {
url : "/api/users/" + id _user + "/corrections/" + $scope . template _corrected [ tpl . id ] [ id _user ] . id ,
method : "DELETE"
} ) . then ( function ( response ) {
$scope . template _corrected [ tpl . id ] [ id _user ] = false
$scope . users _corrected [ id _user ] = response . data
} )
} else {
$http ( {
url : "/api/users/" + id _user + "/corrections" ,
data : { id _template : tpl . id } ,
method : "POST"
} ) . then ( function ( response ) {
if ( $scope . template _corrected [ tpl . id ] === undefined )
$scope . template _corrected [ tpl . id ] = { }
$scope . template _corrected [ tpl . id ] [ id _user ] = { id : response . data . last _id , id _template : tpl . id }
$scope . users _corrected [ id _user ] = response . data
} )
}
}
$scope . deleteTemplate = function ( ) {
var template = new CorrectionTemplate ( {
id : $ ( '#tcid' ) . val ( ) == "" ? null : parseInt ( $ ( '#tcid' ) . val ( ) ) ,
} )
template . $remove ( { surveyId : $scope . survey . id , questId : $scope . question . id , correctId : template . id } , function ( ) {
$scope . templates = CorrectionTemplate . query ( { surveyId : $scope . survey . id , questId : $scope . question . id } ) ;
angular . element ( '#correctionTemplateModal' ) . modal ( 'hide' )
} ) ;
}
$scope . saveTemplate = function ( ) {
var template = new CorrectionTemplate ( {
id : $ ( '#tcid' ) . val ( ) == "" ? null : parseInt ( $ ( '#tcid' ) . val ( ) ) ,
id _question : parseInt ( $ ( '#tcidquestion' ) . val ( ) ) ,
label : $ ( '#tclabel' ) . val ( ) ,
score : parseInt ( $ ( '#tcscore' ) . val ( ) ) ,
score _explaination : $ ( '#tcexplaination' ) . val ( )
} )
if ( template . id ) {
template . $update ( { surveyId : $scope . survey . id , questId : $scope . question . id , correctId : template . id } , function ( ) {
$scope . templates = CorrectionTemplate . query ( { surveyId : $scope . survey . id , questId : $scope . question . id } ) ;
angular . element ( '#correctionTemplateModal' ) . modal ( 'hide' )
} ) ;
} else {
template . $save ( { surveyId : $scope . survey . id , questId : $scope . question . id } , function ( ) {
$scope . templates = CorrectionTemplate . query ( { surveyId : $scope . survey . id , questId : $scope . question . id } ) ;
angular . element ( '#correctionTemplateModal' ) . modal ( 'hide' )
} ) ;
}
}
2020-03-04 11:07:12 +00:00
} )
2020-11-30 09:31:38 +00:00
. controller ( "QuestionsController" , function ( $scope , SurveyQuest , MyResponse , UserResponses , $http , $routeParams , $location , $rootScope ) {
$rootScope . usersResponses = null ;
2020-03-04 11:07:12 +00:00
$scope . questions = SurveyQuest . query ( { surveyId : $scope . survey . id } ) ;
2020-11-30 09:31:38 +00:00
$scope . questions . $promise . then ( function ( questions ) {
$scope . showSubmit = true ;
var mapQuestions = { } ;
questions . forEach ( function ( question ) {
mapQuestions [ question . id ] = question ;
} ) ;
$scope . mapQuestions = mapQuestions ;
} , function ( response ) {
2020-03-08 01:18:32 +00:00
$scope . addToast ( {
variant : "danger" ,
title : $scope . survey . title ,
msg : "Une erreur s'est produite lors de l'accès aux questions : <strong>" + ( response . data ? response . data . errmsg : "impossible de contacter le serveur" ) + "</strong>" ,
} ) ;
$location . url ( "surveys/" )
} )
2020-11-28 17:59:14 +00:00
if ( $routeParams . userId == null ) {
$scope . myresponses = MyResponse . query ( { surveyId : $scope . survey . id } ) ;
} else {
2020-11-30 09:31:38 +00:00
$scope . myresponses = UserResponses . query ( { userId : $routeParams . userId , surveyId : $scope . survey . id } ) ;
2020-11-28 17:59:14 +00:00
}
2020-03-08 00:06:44 +00:00
$scope . myresponses . $promise . then ( function ( responses ) {
$scope . questions . $promise . then ( function ( questions ) {
var idxquestions = { }
questions . forEach ( function ( question , qid ) {
idxquestions [ question . id ] = qid ;
} ) ;
responses . forEach ( function ( response ) {
if ( ! questions [ idxquestions [ response . id _question ] ] . response ) {
if ( response . value ) {
questions [ idxquestions [ response . id _question ] ] . response = response ;
2020-11-20 17:56:32 +00:00
if ( questions [ idxquestions [ response . id _question ] ] . kind == "text" || questions [ idxquestions [ response . id _question ] ] . kind == "ucq" ) {
2020-03-20 23:02:18 +00:00
questions [ idxquestions [ response . id _question ] ] . value = response . value ;
2020-11-20 17:56:32 +00:00
} else if ( questions [ idxquestions [ response . id _question ] ] . kind == "int" ) {
questions [ idxquestions [ response . id _question ] ] . value = parseInt ( response . value ) ;
2020-03-20 23:02:18 +00:00
} else {
response . value . split ( "," ) . forEach ( function ( val ) {
questions [ idxquestions [ response . id _question ] ] [ "p" + val ] = true ;
} )
}
2020-03-08 00:06:44 +00:00
}
}
} )
} ) ;
} ) ;
2020-03-04 11:07:12 +00:00
$scope . submitAnswers = function ( ) {
$scope . submitInProgress = true ;
var res = [ ] ;
$scope . questions . forEach ( function ( q ) {
2020-03-20 23:02:18 +00:00
if ( q . kind == "text" )
res . push ( { "id_question" : q . id , "value" : q . value } )
2020-11-20 17:56:32 +00:00
else if ( q . kind == "int" )
res . push ( { "id_question" : q . id , "value" : "" + q . value } )
else if ( q . kind == "ucq" )
res . push ( { "id_question" : q . id , "value" : q . value } )
2020-03-20 23:02:18 +00:00
else {
var values = [ ] ;
Object . keys ( q ) . forEach ( function ( k ) {
if ( r = k . match ( /^p([0-9]+)$/ ) ) {
if ( q [ k ] )
values . push ( r [ 1 ] )
}
} )
res . push ( { "id_question" : q . id , "value" : values . join ( "," ) } )
}
2020-03-04 11:07:12 +00:00
} ) ;
2020-11-28 17:59:14 +00:00
var url = "/api/surveys/" + $scope . survey . id ;
if ( $routeParams . userId != null ) {
url = "/api/users/" + $routeParams . userId + "/surveys/" + $scope . survey . id ;
}
2020-03-04 11:07:12 +00:00
$http ( {
2020-11-28 17:59:14 +00:00
url : url ,
2020-03-04 11:07:12 +00:00
data : res ,
method : "POST"
} ) . then ( function ( response ) {
$scope . submitInProgress = false ;
2020-03-08 00:06:44 +00:00
$scope . addToast ( {
variant : "success" ,
title : $scope . survey . title ,
msg : "Vos réponses ont bien étés sauvegardées." ,
} ) ;
2020-03-04 11:07:12 +00:00
} , function ( response ) {
$scope . submitInProgress = false ;
2020-03-08 00:06:44 +00:00
$scope . addToast ( {
variant : "danger" ,
title : $scope . survey . title ,
2021-11-18 11:08:25 +00:00
msg : "Une erreur s'est produite durant l'envoi de vos réponses : " + ( response . data ? response . data . errmsg : "impossible de contacter le serveur" ) + "<br>Veuillez réessayer dans quelques instants." ,
2020-03-08 00:06:44 +00:00
} ) ;
2020-03-04 11:07:12 +00:00
} ) ;
}
$scope . saveQuestion = function ( ) {
this . question . edit = false ;
if ( this . question . id ) {
this . question . $update ( { surveyId : $scope . survey . id } ) ;
} else {
this . question . $save ( { surveyId : $scope . survey . id } ) ;
}
}
$scope . addQuestion = function ( ) {
$scope . questions . push ( new SurveyQuest ( { edit : true , kind : 'text' } ) )
}
$scope . editQuestion = function ( ) {
this . question . edit = true ;
}
$scope . deleteQuestion = function ( ) {
if ( this . question . id ) {
this . question . $remove ( function ( ) {
$scope . questions = SurveyQuest . query ( { surveyId : $scope . survey . id } ) ;
} ) ;
} else {
$scope . questions . splice ( $scope . questions . indexOf ( this . question ) , 1 ) ;
}
}
} )
2020-03-20 23:02:18 +00:00
. controller ( "ProposalsController" , function ( $scope , QuestProposal ) {
$scope . proposals = QuestProposal . query ( { surveyId : $scope . survey . id , questId : $scope . question . id } ) ;
$scope . saveProposal = function ( ) {
if ( this . proposal . id ) {
2020-11-20 15:41:06 +00:00
this . proposal . $update ( { surveyId : $scope . survey . id , questId : $scope . question . id , proposalId : this . proposal . id } ) ;
2020-03-20 23:02:18 +00:00
} else {
this . proposal . $save ( { surveyId : $scope . survey . id , questId : $scope . question . id } ) ;
}
}
$scope . addProposal = function ( ) {
$scope . proposals . push ( new QuestProposal ( { } ) )
}
$scope . deleteProposal = function ( ) {
if ( this . proposal . id ) {
this . proposal . $remove ( function ( ) {
$scope . proposals = QuestProposal . query ( { surveyId : $scope . survey . id , questId : $scope . question . id } ) ;
} ) ;
} else {
$scope . proposals . splice ( $scope . proposals . indexOf ( this . proposal ) , 1 ) ;
}
}
} )
2020-09-13 14:37:15 +00:00
. controller ( "CorrectionsTemplateController" , function ( $scope , CorrectionTemplate ) {
$scope . addTemplate = function ( ) {
var element = angular . element ( '#correctionTemplateModal' ) ;
var tpl = new CorrectionTemplate ( { id _question : $scope . question . id } )
if ( element . data ( 'bs.modal' ) )
element . data ( 'bs.modal' ) . _config . template = tpl
element . modal ( {
template : tpl
} ) ;
}
$scope . editTemplate = function ( ) {
var element = angular . element ( '#correctionTemplateModal' ) ;
if ( element . data ( 'bs.modal' ) )
element . data ( 'bs.modal' ) . _config . template = this . template
element . modal ( {
template : this . template
} ) ;
}
} )