<commit_before>App.controller 'QuestionsCtrl', ['$scope', 'Question', ($scope, Question) ->
  $scope.questions = []
  $scope.selectedQuestions = []
  $scope.pages = []
  $scope.questions_per_page = <%= Settings.questions_per_page %>
  $scope.pages_to_show = <%= Settings.pages_to_show %>
  $scope.current_page = 1
  $scope.total_pages = 1
  $scope.init = (project_id, instrument_id) ->
    $scope.project_id = project_id
    $scope.current_page = 1
    $scope.instrument_id = instrument_id
    $scope.questions = Question.query({"project_id": project_id, "instrument_id": instrument_id, "page": $scope.current_page})
    $scope.instrument = Question.query({"project_id": project_id, "instrument_id": instrument_id}, -> $scope.createPages())
    
  $scope.getQuestions = () ->
    $scope.questions = Question.query({"project_id": $scope.project_id, "instrument_id": $scope.instrument_id, "page": $scope.current_page})

  $scope.sortableOptions = {
    cursor: 'move',
    handle: '.move-question',
    axis: 'y',
    stop: (e, ui) -> $scope.updateAndSaveQuestionNumbers()
  }

  $scope.editQuestion = (question) ->
    $scope.$broadcast('EDIT_QUESTION', question.id)
    $scope.edit_question = question
    $scope.edit_question.project_id = $scope.project_id
    $scope.edit_question.instrument_id = $scope.instrument_id

  $scope.newQuestionNumber = ->
    if $scope.current_page > 1
      (($scope.current_page - 1) * $scope.questions_per_page) + $scope.questions.length + 1
    else
      $scope.questions.length + 1

  $scope.newQuestion = ->
    question = new Question()
    question.project_id = $scope.project_id
    question.instrument_id = $scope.instrument_id
    question.number_in_instrument = $scope.newQuestionNumber()
    question.question_identifier = "q_#{question.project_id}_#{question.instrument_id}_#{$scope.uniqueId()}"
    $scope.questions.push(question)
    $scope.edit_question = question

  $scope.saveQuestionSuccess = (data, headers) ->
    $scope.$broadcast('SAVE_QUESTION', data.id)
    $scope.edit_question = null

  $scope.saveQuestionFailure = (result, headers) ->
    angular.forEach result.data.errors, (error, field) ->
      alert error

  $scope.saveQuestion = ->
    if $scope.edit_question.id
      $scope.edit_question.$update({},
        (data, headers) -> $scope.saveQuestionSuccess(data, headers),
        (result, headers) -> $scope.saveQuestionFailure(result, headers)
      )
    else
      $scope.edit_question.$save({},
        (data, headers) -> $scope.saveQuestionSuccess(data, headers),
        (result, headers) -> $scope.saveQuestionFailure(result, headers)
      )
    $scope.edit_question = null

  $scope.cancelQuestion = ->
    $scope.$broadcast('CANCEL_QUESTION')
    $scope.edit_question = null

  $scope.toggleSelection = (question) ->
    if $scope.questionSelected(question)
      $scope.selectedQuestions.splice($scope.selectedQuestions.indexOf(question), 1)
    else
      $scope.selectedQuestions.push question

  $scope.questionSelected = (question) ->
    question in $scope.selectedQuestions

  $scope.deleteQuestion = (question) ->
    if confirm("Are you sure you want to delete this question?")
      question.project_id = $scope.project_id
      question.$delete({},
        (data) ->
          question.id = null
        ,
        (data) ->
          alert "Failed to delete question"
        )
      $scope.questions.splice($scope.questions.indexOf(question), 1)
      $scope.updateQuestionNumbers()


  $scope.uniqueId = ->
    new Date().getTime().toString(36).split("").reverse().join("")

  $scope.questionsBefore = (question) ->
    $scope.questions.slice(0, $scope.questions.indexOf(question))

  $scope.questionsAfter = (question) ->
    $scope.questions.slice($scope.questions.indexOf(question) + 1, $scope.questions.length)

  $scope.questionTypes = ->
    <%= Settings.question_types %>

  $scope.questionTypesWithOptions = (questionType)->
    questionType in <%= Settings.question_with_options %>

  $scope.questionTypesWithImages = (questionType)->
    questionType in <%= Settings.question_with_images %>

  $scope.setPage = (new_page) ->
    new_page = 1 if new_page < 1
    $scope.current_page = new_page
    $scope.getQuestions()
    if $scope.current_page > $scope.pages_to_show and $scope.current_page % $scope.pages_to_show == 1
      if $scope.current_page > $scope.pages_to_show
        lower_bound = (($scope.current_page/$scope.pages_to_show) * $scope.pages_to_show) 
        upper_bound = (($scope.current_page/$scope.pages_to_show) * $scope.pages_to_show) + ($scope.pages_to_show - 1)
        if $scope.total_pages < upper_bound
          upper_bound = $scope.total_pages
        $scope.pages = [lower_bound..upper_bound]
    else if $scope.current_page % $scope.pages_to_show == 0
      lower_bound = (($scope.current_page/$scope.pages_to_show) * $scope.pages_to_show) - ($scope.pages_to_show - 1)
      $scope.pages =  [lower_bound..$scope.current_page]
       
  $scope.createPages = () ->
    $scope.total_pages = Math.ceil($scope.instrument[0].question_count / $scope.questions_per_page)
    if $scope.total_pages > $scope.pages_to_show
      $scope.pages = [1..$scope.pages_to_show]
    else
      $scope.pages = [1..$scope.total_pages]
      
  $scope.setEndPages = (end_page) ->
     $scope.current_page = end_page
     $scope.getQuestions()
     if end_page == 1
       $scope.createPages()
     else if $scope.total_pages > $scope.pages_to_show
       $scope.pages = [($scope.total_pages - $scope.pages_to_show)..$scope.total_pages]
     else
       $scope.pages = [1..$scope.total_pages]

  $scope.repositionQuestion = (question) ->
    question.project_id = $scope.project_id
    question.$update({},
      () -> $scope.updateQuestionNumbers()
    )
    $scope.questions = $scope.questions.filter (q) -> q isnt question
    if $scope.questionOnPage(question)
      $scope.questions.splice(question.number_in_instrument - 1, 0, question)

  $scope.updateQuestionNumbers = ->
    offset = ($scope.current_page - 1) * $scope.questions_per_page
    angular.forEach $scope.questions, (question, index) ->
      question.number_in_instrument = offset + index + 1

  $scope.updateAndSaveQuestionNumbers = ->
    offset = ($scope.current_page - 1) * $scope.questions_per_page
    angular.forEach $scope.questions, (question, index) ->
      question.number_in_instrument = offset + index + 1
      if question.id and $scope.questionOnPage(question)
        question.project_id = $scope.project_id
        question.$update()

  $scope.questionOnPage = (question) ->
    first_number = $scope.questions[0].number_in_instrument
    last_number = $scope.questions[$scope.questions.length - 1].number_in_instrument
    question.number_in_instrument >= first_number and question.number_in_instrument <= last_number
]
<commit_msg>do not create new page if at last page<commit_after>App.controller 'QuestionsCtrl', ['$scope', 'Question', ($scope, Question) ->
  $scope.questions = []
  $scope.selectedQuestions = []
  $scope.pages = []
  $scope.questions_per_page = <%= Settings.questions_per_page %>
  $scope.pages_to_show = <%= Settings.pages_to_show %>
  $scope.current_page = 1
  $scope.total_pages = 1
  $scope.init = (project_id, instrument_id) ->
    $scope.project_id = project_id
    $scope.current_page = 1
    $scope.instrument_id = instrument_id
    $scope.questions = Question.query({"project_id": project_id, "instrument_id": instrument_id, "page": $scope.current_page})
    $scope.instrument = Question.query({"project_id": project_id, "instrument_id": instrument_id}, -> $scope.createPages())
    
  $scope.getQuestions = () ->
    $scope.questions = Question.query({"project_id": $scope.project_id, "instrument_id": $scope.instrument_id, "page": $scope.current_page})

  $scope.sortableOptions = {
    cursor: 'move',
    handle: '.move-question',
    axis: 'y',
    stop: (e, ui) -> $scope.updateAndSaveQuestionNumbers()
  }

  $scope.editQuestion = (question) ->
    $scope.$broadcast('EDIT_QUESTION', question.id)
    $scope.edit_question = question
    $scope.edit_question.project_id = $scope.project_id
    $scope.edit_question.instrument_id = $scope.instrument_id

  $scope.newQuestionNumber = ->
    if $scope.current_page > 1
      (($scope.current_page - 1) * $scope.questions_per_page) + $scope.questions.length + 1
    else
      $scope.questions.length + 1

  $scope.newQuestion = ->
    question = new Question()
    question.project_id = $scope.project_id
    question.instrument_id = $scope.instrument_id
    question.number_in_instrument = $scope.newQuestionNumber()
    question.question_identifier = "q_#{question.project_id}_#{question.instrument_id}_#{$scope.uniqueId()}"
    $scope.questions.push(question)
    $scope.edit_question = question

  $scope.saveQuestionSuccess = (data, headers) ->
    $scope.$broadcast('SAVE_QUESTION', data.id)
    $scope.edit_question = null

  $scope.saveQuestionFailure = (result, headers) ->
    angular.forEach result.data.errors, (error, field) ->
      alert error

  $scope.saveQuestion = ->
    if $scope.edit_question.id
      $scope.edit_question.$update({},
        (data, headers) -> $scope.saveQuestionSuccess(data, headers),
        (result, headers) -> $scope.saveQuestionFailure(result, headers)
      )
    else
      $scope.edit_question.$save({},
        (data, headers) -> $scope.saveQuestionSuccess(data, headers),
        (result, headers) -> $scope.saveQuestionFailure(result, headers)
      )
    $scope.edit_question = null

  $scope.cancelQuestion = ->
    $scope.$broadcast('CANCEL_QUESTION')
    $scope.edit_question = null

  $scope.toggleSelection = (question) ->
    if $scope.questionSelected(question)
      $scope.selectedQuestions.splice($scope.selectedQuestions.indexOf(question), 1)
    else
      $scope.selectedQuestions.push question

  $scope.questionSelected = (question) ->
    question in $scope.selectedQuestions

  $scope.deleteQuestion = (question) ->
    if confirm("Are you sure you want to delete this question?")
      question.project_id = $scope.project_id
      question.$delete({},
        (data) ->
          question.id = null
        ,
        (data) ->
          alert "Failed to delete question"
        )
      $scope.questions.splice($scope.questions.indexOf(question), 1)
      $scope.updateQuestionNumbers()


  $scope.uniqueId = ->
    new Date().getTime().toString(36).split("").reverse().join("")

  $scope.questionsBefore = (question) ->
    $scope.questions.slice(0, $scope.questions.indexOf(question))

  $scope.questionsAfter = (question) ->
    $scope.questions.slice($scope.questions.indexOf(question) + 1, $scope.questions.length)

  $scope.questionTypes = ->
    <%= Settings.question_types %>

  $scope.questionTypesWithOptions = (questionType)->
    questionType in <%= Settings.question_with_options %>

  $scope.questionTypesWithImages = (questionType)->
    questionType in <%= Settings.question_with_images %>

  $scope.setPage = (new_page) ->
    new_page = 1 if new_page < 1
    new_page = $scope.total_pages if new_page > $scope.total_pages
    $scope.current_page = new_page
    $scope.getQuestions()
    if $scope.current_page > $scope.pages_to_show and $scope.current_page % $scope.pages_to_show == 1
      if $scope.current_page > $scope.pages_to_show
        lower_bound = (($scope.current_page/$scope.pages_to_show) * $scope.pages_to_show) 
        upper_bound = (($scope.current_page/$scope.pages_to_show) * $scope.pages_to_show) + ($scope.pages_to_show - 1)
        if $scope.total_pages < upper_bound
          upper_bound = $scope.total_pages
        $scope.pages = [lower_bound..upper_bound]
    else if $scope.current_page % $scope.pages_to_show == 0
      lower_bound = (($scope.current_page/$scope.pages_to_show) * $scope.pages_to_show) - ($scope.pages_to_show - 1)
      $scope.pages =  [lower_bound..$scope.current_page]
       
  $scope.createPages = () ->
    $scope.total_pages = Math.ceil($scope.instrument[0].question_count / $scope.questions_per_page)
    if $scope.total_pages > $scope.pages_to_show
      $scope.pages = [1..$scope.pages_to_show]
    else
      $scope.pages = [1..$scope.total_pages]
      
  $scope.setEndPages = (end_page) ->
     $scope.current_page = end_page
     $scope.getQuestions()
     if end_page == 1
       $scope.createPages()
     else if $scope.total_pages > $scope.pages_to_show
       $scope.pages = [($scope.total_pages - $scope.pages_to_show)..$scope.total_pages]
     else
       $scope.pages = [1..$scope.total_pages]

  $scope.repositionQuestion = (question) ->
    question.project_id = $scope.project_id
    question.$update({},
      () -> $scope.updateQuestionNumbers()
    )
    $scope.questions = $scope.questions.filter (q) -> q isnt question
    if $scope.questionOnPage(question)
      $scope.questions.splice(question.number_in_instrument - 1, 0, question)

  $scope.updateQuestionNumbers = ->
    offset = ($scope.current_page - 1) * $scope.questions_per_page
    angular.forEach $scope.questions, (question, index) ->
      question.number_in_instrument = offset + index + 1

  $scope.updateAndSaveQuestionNumbers = ->
    offset = ($scope.current_page - 1) * $scope.questions_per_page
    angular.forEach $scope.questions, (question, index) ->
      question.number_in_instrument = offset + index + 1
      if question.id and $scope.questionOnPage(question)
        question.project_id = $scope.project_id
        question.$update()

  $scope.questionOnPage = (question) ->
    first_number = $scope.questions[0].number_in_instrument
    last_number = $scope.questions[$scope.questions.length - 1].number_in_instrument
    question.number_in_instrument >= first_number and question.number_in_instrument <= last_number
]

{'fraction_non_alphanumeric': 0.13892929880709923, 'fraction_numerical': 0.004655222577829503, 'mean_word_length': 3.592184368737475, 'pattern_counts': {'":': 16, '<': 19, '<?xml version=': 0, '>': 104, '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': '2894070', 'n_tokens_mistral': 4502, 'n_tokens_neox': 4283, 'n_words': 703}