<commit_before>angular.module('doubtfire.units.partials.contexts', ['doubtfire.units.partials.modals'])

.directive('staffAdminUnitContext', ->
  replace: true
  restrict: 'E'
  templateUrl: 'units/partials/templates/staff-admin-context.tpl.html'
  controller: ($scope, $rootScope, Unit, UnitRole, alertService, groupService) ->
    temp = []
    users = []

    $scope.changeRole = (unitRole, role_id) ->
      unitRole.role_id = role_id
      unitRole.unit_id = $scope.unit.id
      UnitRole.update { id: unitRole.id, unit_role: unitRole },
        (response) -> alertService.add("success", "Role changed", 2000)
        (response) ->
          alertService.add("danger", response.data.error, 6000)

    $scope.addSelectedStaff = ->
      staff = $scope.selectedStaff
      $scope.selectedStaff = null
      $scope.unit.staff = [] unless $scope.unit.staff
      tutorRole = UnitRole.create { unit_id: $scope.unit.id, user_id: staff.id, role: 'Tutor' }
      $scope.unit.staff.push(tutorRole)

    $scope.findStaffUser = (id) ->
      for staff in $scope.staff
        return staff if staff.id == id

    # Used in the typeahead to filter staff already in unit
    $scope.filterStaff = (staff) ->
      not _.find($scope.unit.staff, (listStaff) -> staff.id == listStaff.user_id)

    $scope.removeStaff = (staff) ->
      $scope.unit.staff = _.without $scope.unit.staff, staff
      UnitRole.delete { id: staff.id }
      staffUser = $scope.findStaffUser(staff.user_id)

    $scope.groupSetName = (id) ->
      groupService.groupSetName(id, $scope.unit)

)
.directive('taskAdminUnitContext', ->
  replace: true
  restrict: 'E'
  templateUrl: 'units/partials/templates/task-admin-context.tpl.html'
  controller: ($scope, $modal, $rootScope, Task, Unit, gradeService, alertService, taskService, csvResultService) ->
    $scope.grades = gradeService.grades

    # Pagination details
    $scope.currentPage = 1
    $scope.maxSize = 7
    $scope.pageSize = 7

    # Modal Events
    $scope.editTask = (task) ->
      $modal.open
        controller: 'TaskEditModalCtrl'
        templateUrl: 'units/partials/templates/task-edit-modal.tpl.html'
        resolve: {
          task: -> task
          isNew: -> false
          unit: -> $scope.unit
        }

    guessTaskAbbreviation = () ->
      unit = $scope.unit
      if unit.task_definitions.length == 0
        "1.1P"
      else
        last_abbr = unit.task_definitions[unit.task_definitions.length-1].abbreviation
        regex = /(.*)(\d+)(\D*)/
        match = regex.exec last_abbr
        if match?
          "#{match[1]}#{parseInt(match[2])+1}#{match[3]}"
        else
          "#{last_abbr}1"


    $scope.createTask = ->
      abbr = guessTaskAbbreviation()
      task = {
        name: "Task #{abbr}",
        abbreviation: abbr,
        description: "New Description",
        target_date: new Date(),
        upload_requirements: [],
        plagiarism_checks: []
        weight: 4
        target_grade: 0
        restrict_status_updates: false
        plagiarism_warn_pct: 80
      }
      $modal.open
        controller: 'TaskEditModalCtrl'
        templateUrl: 'units/partials/templates/task-edit-modal.tpl.html'
        resolve: {
          task: -> task
          isNew: -> true
          unit: -> $scope.unit
        }

    $scope.deleteTask = (task) ->
      taskService.deleteTask(task, $scope.unit)

    $scope.taskFiles = { file: { name: 'Task PDFs', type: 'zip'  } }
    $scope.taskUploadUrl = Unit.taskUploadUrl($scope.unit)

    $scope.onTaskPDFSuccess = (response) ->
      alertService.add("success", "Files uploaded", 2000)
      $scope.filesUploaded = response

    $scope.batchFiles = { file: { name: 'CSV Data', type: 'csv'  } }
    $scope.batchTaskUrl = ->
      Task.getTaskDefinitionBatchUploadUrl($scope.unit)
    $scope.onBatchTaskSuccess = (response) ->
      csvResultService.show "Task CSV Upload Results", response
      if response.success.length > 0
        $scope.unit.refresh()

)
.directive('adminUnitContext', ->
  replace: true
  restrict: 'E'
  templateUrl: 'units/partials/templates/unit-admin-context.tpl.html'
  controller: ($scope, $state, $rootScope, Unit, alertService, unitService) ->
    $scope.format = 'yyyy-MM-dd'
    $scope.initDate = new Date('2016-04-20')
    $scope.startOpened = $scope.endOpened = $scope.opened = false
    $scope.dateOptions = {
      formatYear: 'yy',
      startingDay: 1
    }
    $scope.unitTypeAheadData = unitService.unitTypeAheadData
    $scope.studentSearch = ""

    $scope.saveUnit = ->
      if $scope.unit.convenors then delete $scope.unit.convenors

      if $scope.unit.id == -1
        Unit.create { unit: $scope.unit },
          (unit) ->
            $scope.saveSuccess(unit)
          (response) ->
            alertService.add("danger", response.data.error, 6000)
      else
        Unit.update { id: $scope.unit.id, unit: $scope.unit}, (unit) ->
          alertService.add("success", "Unit updated.", 2000)
          $state.transitionTo('admin/units#index')
)
.directive('tutorialUnitContext', ->
  replace: true
  restrict: 'E'
  templateUrl: 'units/partials/templates/tutorial-admin-context.tpl.html'
  controller: ($scope, $modal, $rootScope, Unit, UnitRole) ->
    $scope.editTutorial = (tutorial) ->
      $modal.open
        controller: 'TutorialModalCtrl'
        templateUrl: 'units/partials/templates/tutorial-modal.tpl.html'
        resolve: {
          tutorial: -> tutorial
          isNew: -> false
          tutors: -> $scope.unit.staff
          unit: -> $scope.unit
        }

    $scope.deleteTutorial = (tutorial) ->
      # TODO: No endpoint for this yet
      alert "Functionality coming soon!"
      $scope.unit.tutorials = _.without $scope.unit.tutorials, tutorial

    $scope.createTutorial = ->
      d = new Date()
      d.setHours(8)
      d.setMinutes(30)

      tutorial = { abbreviation: "LA1-??", meeting_day: "Monday", meeting_time: d, meeting_location: "ATC???" }
      $modal.open
        controller: 'TutorialModalCtrl'
        templateUrl: 'units/partials/templates/tutorial-modal.tpl.html'
        resolve: {<commit_msg>add ability to delete tutorials<commit_after>angular.module('doubtfire.units.partials.contexts', ['doubtfire.units.partials.modals'])

.directive('staffAdminUnitContext', ->
  replace: true
  restrict: 'E'
  templateUrl: 'units/partials/templates/staff-admin-context.tpl.html'
  controller: ($scope, $rootScope, Unit, UnitRole, alertService, groupService) ->
    temp = []
    users = []

    $scope.changeRole = (unitRole, role_id) ->
      unitRole.role_id = role_id
      unitRole.unit_id = $scope.unit.id
      UnitRole.update { id: unitRole.id, unit_role: unitRole },
        (response) -> alertService.add("success", "Role changed", 2000)
        (response) ->
          alertService.add("danger", response.data.error, 6000)

    $scope.addSelectedStaff = ->
      staff = $scope.selectedStaff
      $scope.selectedStaff = null
      $scope.unit.staff = [] unless $scope.unit.staff
      tutorRole = UnitRole.create { unit_id: $scope.unit.id, user_id: staff.id, role: 'Tutor' }
      $scope.unit.staff.push(tutorRole)

    $scope.findStaffUser = (id) ->
      for staff in $scope.staff
        return staff if staff.id == id

    # Used in the typeahead to filter staff already in unit
    $scope.filterStaff = (staff) ->
      not _.find($scope.unit.staff, (listStaff) -> staff.id == listStaff.user_id)

    $scope.removeStaff = (staff) ->
      $scope.unit.staff = _.without $scope.unit.staff, staff
      UnitRole.delete { id: staff.id }
      staffUser = $scope.findStaffUser(staff.user_id)

    $scope.groupSetName = (id) ->
      groupService.groupSetName(id, $scope.unit)

)
.directive('taskAdminUnitContext', ->
  replace: true
  restrict: 'E'
  templateUrl: 'units/partials/templates/task-admin-context.tpl.html'
  controller: ($scope, $modal, $rootScope, Task, Unit, gradeService, alertService, taskService, csvResultService) ->
    $scope.grades = gradeService.grades

    # Pagination details
    $scope.currentPage = 1
    $scope.maxSize = 7
    $scope.pageSize = 7

    # Modal Events
    $scope.editTask = (task) ->
      $modal.open
        controller: 'TaskEditModalCtrl'
        templateUrl: 'units/partials/templates/task-edit-modal.tpl.html'
        resolve: {
          task: -> task
          isNew: -> false
          unit: -> $scope.unit
        }

    guessTaskAbbreviation = () ->
      unit = $scope.unit
      if unit.task_definitions.length == 0
        "1.1P"
      else
        last_abbr = unit.task_definitions[unit.task_definitions.length-1].abbreviation
        regex = /(.*)(\d+)(\D*)/
        match = regex.exec last_abbr
        if match?
          "#{match[1]}#{parseInt(match[2])+1}#{match[3]}"
        else
          "#{last_abbr}1"


    $scope.createTask = ->
      abbr = guessTaskAbbreviation()
      task = {
        name: "Task #{abbr}",
        abbreviation: abbr,
        description: "New Description",
        target_date: new Date(),
        upload_requirements: [],
        plagiarism_checks: []
        weight: 4
        target_grade: 0
        restrict_status_updates: false
        plagiarism_warn_pct: 80
      }
      $modal.open
        controller: 'TaskEditModalCtrl'
        templateUrl: 'units/partials/templates/task-edit-modal.tpl.html'
        resolve: {
          task: -> task
          isNew: -> true
          unit: -> $scope.unit
        }

    $scope.deleteTask = (task) ->
      taskService.deleteTask(task, $scope.unit)

    $scope.taskFiles = { file: { name: 'Task PDFs', type: 'zip'  } }
    $scope.taskUploadUrl = Unit.taskUploadUrl($scope.unit)

    $scope.onTaskPDFSuccess = (response) ->
      alertService.add("success", "Files uploaded", 2000)
      $scope.filesUploaded = response

    $scope.batchFiles = { file: { name: 'CSV Data', type: 'csv'  } }
    $scope.batchTaskUrl = ->
      Task.getTaskDefinitionBatchUploadUrl($scope.unit)
    $scope.onBatchTaskSuccess = (response) ->
      csvResultService.show "Task CSV Upload Results", response
      if response.success.length > 0
        $scope.unit.refresh()

)
.directive('adminUnitContext', ->
  replace: true
  restrict: 'E'
  templateUrl: 'units/partials/templates/unit-admin-context.tpl.html'
  controller: ($scope, $state, $rootScope, Unit, alertService, unitService) ->
    $scope.format = 'yyyy-MM-dd'
    $scope.initDate = new Date('2016-04-20')
    $scope.startOpened = $scope.endOpened = $scope.opened = false
    $scope.dateOptions = {
      formatYear: 'yy',
      startingDay: 1
    }
    $scope.unitTypeAheadData = unitService.unitTypeAheadData
    $scope.studentSearch = ""

    $scope.saveUnit = ->
      if $scope.unit.convenors then delete $scope.unit.convenors

      if $scope.unit.id == -1
        Unit.create { unit: $scope.unit },
          (unit) ->
            $scope.saveSuccess(unit)
          (response) ->
            alertService.add("danger", response.data.error, 6000)
      else
        Unit.update { id: $scope.unit.id, unit: $scope.unit}, (unit) ->
          alertService.add("success", "Unit updated.", 2000)
          $state.transitionTo('admin/units#index')
)
.directive('tutorialUnitContext', ->
  replace: true
  restrict: 'E'
  templateUrl: 'units/partials/templates/tutorial-admin-context.tpl.html'
  controller: ($scope, $modal, $rootScope, Unit, UnitRole, Tutorial, alertService) ->
    $scope.editTutorial = (tutorial) ->
      $modal.open
        controller: 'TutorialModalCtrl'
        templateUrl: 'units/partials/templates/tutorial-modal.tpl.html'
        resolve: {
          tutorial: -> tutorial
          isNew: -> false
          tutors: -> $scope.unit.staff
          unit: -> $scope.unit
        }

    $scope.deleteTutorial = (tutorial) ->
      # TODO: No endpoint for this yet
      Tutorial.delete { id: tutorial.id },
        (response) ->
          $scope.unit.tutorials = _.without $scope.unit.tutorials, tutorial
        (response) ->
          alertService.add("danger", response.data.error)

    $scope.createTutorial = ->
      d = new Date()
      d.setHours(8)
      d.setMinutes(30)

      tutorial = { abbreviation: "LA1-??", meeting_day: "Monday", meeting_time: d, meeting_location: "ATC???" }
      $modal.open
        controller: 'TutorialModalCtrl'
        templateUrl: 'units/partials/templates/tutorial-modal.tpl.html'
        resolve: {
{'fraction_non_alphanumeric': 0.1393787413039961, 'fraction_numerical': 0.008251092056301569, 'mean_word_length': 2.5782923299565845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 89, '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': '3517839', 'n_tokens_mistral': 4074, 'n_tokens_neox': 3915, 'n_words': 745}