<commit_before>angular.module('doubtfire.units.partials.modals', [])
.controller('TutorialModalCtrl', ($scope, $modalInstance,  tutorial, isNew, tutors, unit, Tutorial, alertService) ->
  $scope.tutorial = tutorial
  $scope.isNew = isNew
  $scope.tutors = tutors

  $scope.saveTutorial = ->
    save_data = _.omit(tutorial, 'tutor', 'tutor_name', 'meeting_time', 'data')
    save_data.tutor_id = tutorial.tutor.user_id

    if tutorial.meeting_time.getHours
      save_data.meeting_time = tutorial.meeting_time.getHours() + ":" + tutorial.meeting_time.getMinutes()

    if isNew
      save_data.unit_id = unit.id
      Tutorial.create( tutorial: save_data ).$promise.then (
        (response) ->
          $modalInstance.close(response)
          unit.tutorials.push(response)
          alertService.add("success", "Tutorial Added", 5000)
      ),
      (
        (response) ->
          if response.data.error?
            alertService.add("danger", "Error: " + response.data.error, 5000)
      )
    else

      Tutorial.update( { id: tutorial.id, tutorial: save_data } ).$promise.then (
        (response) ->
          $modalInstance.close(response)
          tutorial.tutor = response.tutor
          tutorial.tutor_name = response.tutor_name
          alertService.add("success", "Tutorial Updated", 5000)
      ),
      (
        (response) ->
          if response.data.error?
            alertService.add("danger", "Error: " + response.data.error, 5000)
      )
)
.controller('UnitModalCtrl', ($scope, $modalInstance, Unit, convenors, unit) ->
  $scope.unit = unit
  $scope.modalState = {}
  $scope.availableConvenors = angular.copy(convenors)

  $scope.addSelectedConvenor = ->
    # Rip out the convenor to add and clear the input
    convenor = $scope.modalState.selectedConvenor
    $scope.modalState.selectedConvenor = null

    # Add the convenor to the list and remove it
    # from the list of available convenors
    $scope.unit.convenors.push(convenor)
    $scope.availableConvenors = _.without $scope.availableConvenors, convenor

  $scope.removeConvenor = (convenor) ->
    $scope.unit.convenors = _.without $scope.unit.convenors, convenor
    $scope.availableConvenors.push(convenor)

  $scope.saveUnit = ->
    Unit.create { unit: $scope.unit }
)
.controller('EnrolStudentModalCtrl', ($scope, $modalInstance, Project, unit, projects) ->
  $scope.unit = unit

  $scope.enrolStudent = (student_id, tutorial) ->
    # get tutorial_id from tutorial_name
    Project.create {unit_id: unit.id, student_num: student_id, tutorial_id: if tutorial then tutorial.id else null }, (project) ->
      projects.push project
      $modalInstance.close()
      #TODO: success and error alerts
)
.controller('TaskEditModalCtrl', ($scope, $modalInstance, TaskDefinition, task, unit, alertService, isNew) ->
  $scope.unit = unit
  $scope.task = task
  $scope.isNew = isNew
  
  # Datepicker opener
  $scope.open = ($event) ->
    $event.preventDefault()
    $event.stopPropagation()
    $scope.opened = true
    
  $scope.addUpReq = () ->
    newLength = $scope.task.upload_requirements.length + 1
    newUpReq = { key: "file#{newLength-1}", name: "", type: "code" }
    $scope.task.upload_requirements.push newUpReq
  
  $scope.removeUpReq = (upReq) ->
    $scope.task.upload_requirements = $scope.task.upload_requirements.filter (anUpReq) -> anUpReq.key isnt upReq.key
    
  $scope.saveTask = () ->
    # Map the task to upload to the appropriate fields
    task = $scope.task
    task.abbreviation = $scope.task.abbr
    task.weighting = $scope.task.weight
    task.unit_id = $scope.unit.id
    task.upload_requirements = JSON.stringify $scope.task.upload_requirements
    task.description = $scope.task.desc
    
    if $scope.isNew
      TaskDefinition.create( { task_def: task } ).$promise.then (
        (response) ->
          $modalInstance.close(response)
          $scope.unit.task_definitions.push(response)
          alertService.add("success", "#{response.name} Added", 5000)
      ),
      (
        (response) ->
          if response.data.error?
            alertService.add("danger", "Error: " + response.data.error, 5000)
      )
    else
      TaskDefinition.update( { id: task.id, task_def: task } ).$promise.then (
        (response) ->
          $modalInstance.close(response)
          $scope.unit.task_definitions.push(response)
          alertService.add("success", "#{response.name} Updated", 5000)
      ),
      (
        (response) ->
          if response.data.error?
            alertService.add("danger", "Error: " + response.data.error, 5000)
      )

)<commit_msg>fix task dates, task duplications in front end on update<commit_after>angular.module('doubtfire.units.partials.modals', [])
.controller('TutorialModalCtrl', ($scope, $modalInstance,  tutorial, isNew, tutors, unit, Tutorial, alertService) ->
  $scope.tutorial = tutorial
  $scope.isNew = isNew
  $scope.tutors = tutors

  $scope.saveTutorial = ->
    save_data = _.omit(tutorial, 'tutor', 'tutor_name', 'meeting_time', 'data')
    save_data.tutor_id = tutorial.tutor.user_id

    if tutorial.meeting_time.getHours
      save_data.meeting_time = tutorial.meeting_time.getHours() + ":" + tutorial.meeting_time.getMinutes()

    if isNew
      save_data.unit_id = unit.id
      Tutorial.create( tutorial: save_data ).$promise.then (
        (response) ->
          $modalInstance.close(response)
          unit.tutorials.push(response)
          alertService.add("success", "Tutorial Added", 5000)
      ),
      (
        (response) ->
          if response.data.error?
            alertService.add("danger", "Error: " + response.data.error, 5000)
      )
    else
      Tutorial.update( { id: tutorial.id, tutorial: save_data } ).$promise.then (
        (response) ->
          $modalInstance.close(response)
          tutorial.tutor = response.tutor
          tutorial.tutor_name = response.tutor_name
          alertService.add("success", "Tutorial Updated", 5000)
      ),
      (
        (response) ->
          if response.data.error?
            alertService.add("danger", "Error: " + response.data.error, 5000)
      )
)
.controller('UnitModalCtrl', ($scope, $modalInstance, Unit, convenors, unit) ->
  $scope.unit = unit
  $scope.modalState = {}
  $scope.availableConvenors = angular.copy(convenors)

  $scope.addSelectedConvenor = ->
    # Rip out the convenor to add and clear the input
    convenor = $scope.modalState.selectedConvenor
    $scope.modalState.selectedConvenor = null

    # Add the convenor to the list and remove it
    # from the list of available convenors
    $scope.unit.convenors.push(convenor)
    $scope.availableConvenors = _.without $scope.availableConvenors, convenor

  $scope.removeConvenor = (convenor) ->
    $scope.unit.convenors = _.without $scope.unit.convenors, convenor
    $scope.availableConvenors.push(convenor)

  $scope.saveUnit = ->
    Unit.create { unit: $scope.unit }
)
.controller('EnrolStudentModalCtrl', ($scope, $modalInstance, Project, unit, projects) ->
  $scope.unit = unit

  $scope.enrolStudent = (student_id, tutorial) ->
    # get tutorial_id from tutorial_name
    Project.create {unit_id: unit.id, student_num: student_id, tutorial_id: if tutorial then tutorial.id else null }, (project) ->
      projects.push project
      $modalInstance.close()
      #TODO: success and error alerts
)
.controller('TaskEditModalCtrl', ($scope, $modalInstance, TaskDefinition, task, unit, alertService, isNew) ->
  $scope.unit = unit
  $scope.task = task
  $scope.isNew = isNew
  
  # Datepicker opener
  $scope.open = ($event) ->
    $event.preventDefault()
    $event.stopPropagation()
    $scope.opened = true
    
  $scope.addUpReq = () ->
    newLength = $scope.task.upload_requirements.length + 1
    newUpReq = { key: "file#{newLength-1}", name: "", type: "code" }
    $scope.task.upload_requirements.push newUpReq
  
  $scope.removeUpReq = (upReq) ->
    $scope.task.upload_requirements = $scope.task.upload_requirements.filter (anUpReq) -> anUpReq.key isnt upReq.key
  
  populate_task = (oldTask, newTask) ->
    _.extend(oldTask, newTask)
    if newTask.abbreviation
      oldTask.abbr = newTask.abbreviation
    else
      oldTask.abbr = newTask.abbr
    if newTask.weighting
      oldTask.weight = newTask.weighting
    else
      oldTask.weight = newTask.weight
    oldTask.name = newTask.name
    oldTask.upload_requirements = newTask.upload_requirements
    oldTask.target_date = newTask.target_date
    oldTask.required = newTask.required

  $scope.saveTask = () ->
    # Map the task to upload to the appropriate fields
    task = $scope.task
    task.abbreviation = $scope.task.abbr
    task.weighting = $scope.task.weight
    task.unit_id = $scope.unit.id
    task.upload_requirements = JSON.stringify $scope.task.upload_requirements
    # task.upload_requirements = $scope.task.upload_requirements
    if task.target_date && task.target_date.getMonth
      tgt = task.target_date
      task.target_date = "#{tgt.getFullYear()}-#{tgt.getMonth() + 1}-#{tgt.getDate()}"
    
    task.description = $scope.task.desc
    
    if $scope.isNew
      TaskDefinition.create( { task_def: task } ).$promise.then (
        (response) ->
          $modalInstance.close(response)
          $scope.unit.task_definitions.push(response)
          alertService.add("success", "#{response.name} Added", 5000)
      ),
      (
        (response) ->
          if response.data.error?
            alertService.add("danger", "Error: " + response.data.error, 5000)
      )
    else
      TaskDefinition.update( { id: task.id, task_def: task } ).$promise.then (
        (response) ->
          $modalInstance.close(response)
          alertService.add("success", "#{response.name} Updated", 5000)
          populate_task($scope.task, response)
      ),
      (
        (response) ->
          if response.data.error?
            alertService.add("danger", "Error: " + response.data.error, 5000)
      )

)
{'fraction_non_alphanumeric': 0.1295961422543701, 'fraction_numerical': 0.006931886678722122, 'mean_word_length': 2.9162077104642012, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6099128', 'n_tokens_mistral': 3259, 'n_tokens_neox': 3132, 'n_words': 597}