<commit_before>require 'zip/zip'

class StudentAssignmentController < ApplicationController
  helper :wiki
  helper :student_assignment
  
  def view_team
    @student = Participant.find(params[:id])
    @teams = Team.find(:all, :conditions => ['assignment_id = ?', @student.assignment_id])
    for team in @teams
      @teamuser = TeamsUser.find(:first, :conditions => ['team_id = ? and user_id = ?', team.id, @student.user_id])
      if @teamuser != nil
        @team_id = @teamuser.team_id
      end
    end
    
    @team_members = TeamsUser.find(:all, :conditions => ['team_id = ?', @team_id])
    @send_invs = Invitation.find(:all, :conditions => ['from_id = ? and assignment_id = ?', @student.user_id, @student.assignment_id])
    @received_invs = Invitation.find(:all, :conditions => ['to_id = ? and assignment_id = ? and reply_status = "W"', @student.user_id, @student.assignment_id])
  end
  
  def list
    user_id = session[:user].id
    @user =session[:user]
    @participants = Participant.find(:all, 
                                    :conditions => ['user_id = ?', user_id],
                                    :order => "assignment_id DESC")
  end
  
  def view_publishing
    user_id = session[:user].id
    @user =session[:user]
    @participants = Participant.find(:all, 
                                    :conditions => ['user_id = ?', user_id],
                                    :order => "assignment_id DESC")
  end
  
  def view_actions
    @student = Participant.find(params[:id])
    @assignment_id = @student.assignment_id
    # assignment_id below is the ID of the assignment retrieved from the participants table (the assignment in which this student is participating)
    @due_dates = DueDate.find(:all, :conditions => ["assignment_id = ?",@assignment_id])
    @can_view_your_work, @can_view_others_work = find_viewing_permissions(@due_dates)
    @assigned_surveys = SurveyHelper::get_all_available_surveys(@assignment_id, session[:user].role_id)
  end
  
  def eula_yes
  
    @user = session[:user]
    @user.is_new_user = 0
    
    if @user.save
      flash[:notice] = 'You have accepted the license agreement'
      redirect_to :action => 'list'
    else # If something goes wrong, stay at same page
      render :action => 'list'
    end
  end
  
    def eula_no
      flash[:notice] = 'You have to accept the license agreement in order to use the system'
      redirect_to :action => 'list'
  end
  
#  def set_publish_permission_yes
#    @participant = Participant.find(:first, :conditions => ["id = ?",params[:id]])
#    @participant.permission_granted = 1;
#    @participant_id = params[:id]
#    if @participant.save
#      flash[:notice] = 'Your work will now be published'
#      redirect_to :action => 'submit', :id => @participant_id
#    else # If something goes wrong, stay at same page
#      render :action => 'submit', :id => @participant_id
#    end
#  end
  
  def set_publish_permission_yes
    @participant = Participant.find(:first, :conditions => ["id = ?",params[:participant]])
    @participant.permission_granted = 1;
    @participant_id = params[:id]
    if @participant.save
      flash[:notice] = 'Your work will now be published'
      redirect_to :action => 'view_publishing'
    else # If something goes wrong, stay at same page
      render :action => 'view_publishing'
    end
  end
  
  def set_publish_permission_no
    @participant = Participant.find(:first, :conditions => ["id = ?",params[:participant]])
    @participant.permission_granted = 0;
    @participant_id = params[:id]
    if @participant.save
      flash[:notice] = 'Your work will now be published'
      redirect_to :action => 'view_publishing'
    else # If something goes wrong, stay at same page
      render :action => 'view_publishing'
    end
  end
  
  def set_all_publish_permission_yes
    @participants = Participant.find(:all, :conditions => ["user_id = ?",params[:user]])
    for participant in @participants
      participant.permission_granted = 1;
      if !participant.save
        render :action => 'view_publishing'
      end
    end
    redirect_to :action => 'view_publishing'
  end
  
  def set_all_publish_permission_no
    @participants = Participant.find(:all, :conditions => ["user_id = ?",params[:user]])
    for participant in @participants
      participant.permission_granted = 0;
      if !participant.save
        render :action => 'view_publishing'
      end
    end
    redirect_to :action => 'view_publishing'
  end
  
  def set_future_publish_permission_yes
    @user = session[:user]
    @user.master_permission_granted = 1;
    if @user.save
      redirect_to :action => 'view_publishing'
    else # If something goes wrong, stay at same page
      render :action => 'view_publishing'
    end
  end
  
  def set_future_publish_permission_no
    @user = session[:user]
    @user.master_permission_granted = 0;
    if @user.save
      redirect_to :action => 'view_publishing'
    else # If something goes wrong, stay at same page
      render :action => 'view_publishing'
    end
  end
  
  def view_scores
    @author_id = session[:user].id
    @assignment_id = Participant.find(params[:id]).assignment_id
    @assignment = Assignment.find(@assignment_id)
    if @assignment.team_assignment 
      @team_id = TeamsUser.find(:first,:conditions => ["user_id=? and team_id in (select id from teams where assignment_id=?)", @author_id, @assignment_id]).team_id
      @author_first_user_id = TeamsUser.find(:first,:conditions => ["team_id=?", @team_id]).user_id
      @student = Participant.find(:first,:conditions => ["user_id = ? AND assignment_id = ?", @author_first_user_id, @assignment_id])
      @user_name= session[:user].name
      #@user_name = User.find(@author_first_user_id).name
      @review_mapping = ReviewMapping.find(:all,:conditions => ["team_id = ? and assignment_id = ?", @team_id, @assignment_id])
    elsif !@assignment.team_assignment
      @student = Participant.find(params[:id])
      @user_name= session[:user].name
      @user_name = User.find(@student.user_id).name
      @review_mapping = ReviewMapping.find(:all,:conditions => ["author_id = ? and assignment_id = ?", @author_id, @assignment_id])
    end
    @late_policy = LatePolicy.find(Assignment.find(@assignment_id).due_dates[0].late_policy_id)
    @penalty_units = @student.penalty_accumulated/@late_policy.penalty_period_in_minutes
    
    #the code below finds the sum of the maximum scores of all questions in the questionnaire
    @sum_of_max = 0
    for question in Questionnaire.find(Assignment.find(@assignment_id).review_questionnaire_id).questions
      @sum_of_max += Questionnaire.find(Assignment.find(@assignment_id).review_questionnaire_id).max_question_score
    end
    
    if @student.penalty_accumulated/@late_policy.penalty_period_in_minutes*@late_policy.penalty_per_unit < @late_policy.max_penalty
      @final_penalty = @penalty_units*@late_policy.penalty_per_unit
    elsif @penalty_units ==0
      @final_penalty = 0
    else
      @final_penalty = @late_policy.max_penalty
    end
        
    @review_of_review_mappings = Array.new
    
    @review_mappings_for_author = ReviewMapping.find(:all, :conditions => ["author_id = ?",(session[:user].id)])
    for review_mapping_for_author in @review_mappings_for_author
      if(ReviewOfReviewMapping.find(:first, :conditions => ["review_mapping_id = ?",review_mapping_for_author.id])!= nil)
        puts ReviewOfReviewMapping.find(:first, :conditions => ["review_mapping_id = ?",review_mapping_for_author.id])
        @review_of_review_mappings << ReviewOfReviewMapping.find(:first, :conditions => ["review_mapping_id = ?",review_mapping_for_author.id])
      end
    end
    
  end
  
  def set_feedback
    @participant_id = params[:participant_id]
    @review_id = params[:review_id]
    @assignment_id = params[:assignment_id]
    update_author_feedback(@review_id,@assignment_id,params['author']['text'])
    redirect_to :action => 'view_scores', :id => @participant_id
  end
  
  def update_author_feedback(review_id,assignment_id,text)
    if(ReviewFeedback.find(:first,:conditions =>["review_id = ? and assignment_id = ?", review_id, assignment_id]))
      @review_feedback = ReviewFeedback.find(:first,:conditions =>["review_id = ? and assignment_id = ?", review_id, assignment_id])
      @review_feedback.additional_comments = text
      @review_feedback.update
    else
      @review_feedback = ReviewFeedback.new
      @review_feedback.review_id = review_id
      @review_feedback.assignment_id = assignment_id
      @review_feedback.additional_comments = text<commit_msg>ROR scores displayed<commit_after>require 'zip/zip'

class StudentAssignmentController < ApplicationController
  helper :wiki
  helper :student_assignment
  
  def view_team
    @student = Participant.find(params[:id])
    @teams = Team.find(:all, :conditions => ['assignment_id = ?', @student.assignment_id])
    for team in @teams
      @teamuser = TeamsUser.find(:first, :conditions => ['team_id = ? and user_id = ?', team.id, @student.user_id])
      if @teamuser != nil
        @team_id = @teamuser.team_id
      end
    end
    
    @team_members = TeamsUser.find(:all, :conditions => ['team_id = ?', @team_id])
    @send_invs = Invitation.find(:all, :conditions => ['from_id = ? and assignment_id = ?', @student.user_id, @student.assignment_id])
    @received_invs = Invitation.find(:all, :conditions => ['to_id = ? and assignment_id = ? and reply_status = "W"', @student.user_id, @student.assignment_id])
  end
  
  def list
    user_id = session[:user].id
    @user =session[:user]
    @participants = Participant.find(:all, 
                                    :conditions => ['user_id = ?', user_id],
                                    :order => "assignment_id DESC")
  end
  
  def view_publishing
    user_id = session[:user].id
    @user =session[:user]
    @participants = Participant.find(:all, 
                                    :conditions => ['user_id = ?', user_id],
                                    :order => "assignment_id DESC")
  end
  
  def view_actions
    @student = Participant.find(params[:id])
    @assignment_id = @student.assignment_id
    # assignment_id below is the ID of the assignment retrieved from the participants table (the assignment in which this student is participating)
    @due_dates = DueDate.find(:all, :conditions => ["assignment_id = ?",@assignment_id])
    @can_view_your_work, @can_view_others_work = find_viewing_permissions(@due_dates)
    @assigned_surveys = SurveyHelper::get_all_available_surveys(@assignment_id, session[:user].role_id)
  end
  
  def eula_yes
  
    @user = session[:user]
    @user.is_new_user = 0
    
    if @user.save
      flash[:notice] = 'You have accepted the license agreement'
      redirect_to :action => 'list'
    else # If something goes wrong, stay at same page
      render :action => 'list'
    end
  end
  
    def eula_no
      flash[:notice] = 'You have to accept the license agreement in order to use the system'
      redirect_to :action => 'list'
  end
  
#  def set_publish_permission_yes
#    @participant = Participant.find(:first, :conditions => ["id = ?",params[:id]])
#    @participant.permission_granted = 1;
#    @participant_id = params[:id]
#    if @participant.save
#      flash[:notice] = 'Your work will now be published'
#      redirect_to :action => 'submit', :id => @participant_id
#    else # If something goes wrong, stay at same page
#      render :action => 'submit', :id => @participant_id
#    end
#  end
  
  def set_publish_permission_yes
    @participant = Participant.find(:first, :conditions => ["id = ?",params[:participant]])
    @participant.permission_granted = 1;
    @participant_id = params[:id]
    if @participant.save
      flash[:notice] = 'Your work will now be published'
      redirect_to :action => 'view_publishing'
    else # If something goes wrong, stay at same page
      render :action => 'view_publishing'
    end
  end
  
  def set_publish_permission_no
    @participant = Participant.find(:first, :conditions => ["id = ?",params[:participant]])
    @participant.permission_granted = 0;
    @participant_id = params[:id]
    if @participant.save
      flash[:notice] = 'Your work will now be published'
      redirect_to :action => 'view_publishing'
    else # If something goes wrong, stay at same page
      render :action => 'view_publishing'
    end
  end
  
  def set_all_publish_permission_yes
    @participants = Participant.find(:all, :conditions => ["user_id = ?",params[:user]])
    for participant in @participants
      participant.permission_granted = 1;
      if !participant.save
        render :action => 'view_publishing'
      end
    end
    redirect_to :action => 'view_publishing'
  end
  
  def set_all_publish_permission_no
    @participants = Participant.find(:all, :conditions => ["user_id = ?",params[:user]])
    for participant in @participants
      participant.permission_granted = 0;
      if !participant.save
        render :action => 'view_publishing'
      end
    end
    redirect_to :action => 'view_publishing'
  end
  
  def set_future_publish_permission_yes
    @user = session[:user]
    @user.master_permission_granted = 1;
    if @user.save
      redirect_to :action => 'view_publishing'
    else # If something goes wrong, stay at same page
      render :action => 'view_publishing'
    end
  end
  
  def set_future_publish_permission_no
    @user = session[:user]
    @user.master_permission_granted = 0;
    if @user.save
      redirect_to :action => 'view_publishing'
    else # If something goes wrong, stay at same page
      render :action => 'view_publishing'
    end
  end
  
  def view_scores
    @author_id = session[:user].id
    @assignment_id = Participant.find(params[:id]).assignment_id
    @assignment = Assignment.find(@assignment_id)
    if @assignment.team_assignment 
      @team_id = TeamsUser.find(:first,:conditions => ["user_id=? and team_id in (select id from teams where assignment_id=?)", @author_id, @assignment_id]).team_id
      @author_first_user_id = TeamsUser.find(:first,:conditions => ["team_id=?", @team_id]).user_id
      @student = Participant.find(:first,:conditions => ["user_id = ? AND assignment_id = ?", @author_first_user_id, @assignment_id])
      @user_name= session[:user].name
      #@user_name = User.find(@author_first_user_id).name
      @review_mapping = ReviewMapping.find(:all,:conditions => ["team_id = ? and assignment_id = ?", @team_id, @assignment_id])
    elsif !@assignment.team_assignment
      @student = Participant.find(params[:id])
      @user_name= session[:user].name
      @user_name = User.find(@student.user_id).name
      @review_mapping = ReviewMapping.find(:all,:conditions => ["author_id = ? and assignment_id = ?", @author_id, @assignment_id])
    end
    @late_policy = LatePolicy.find(Assignment.find(@assignment_id).due_dates[0].late_policy_id)
    @penalty_units = @student.penalty_accumulated/@late_policy.penalty_period_in_minutes
    
    #the code below finds the sum of the maximum scores of all questions in the questionnaire
    @sum_of_max = 0
    for question in Questionnaire.find(Assignment.find(@assignment_id).review_questionnaire_id).questions
      @sum_of_max += Questionnaire.find(Assignment.find(@assignment_id).review_questionnaire_id).max_question_score
    end
    
    if @student.penalty_accumulated/@late_policy.penalty_period_in_minutes*@late_policy.penalty_per_unit < @late_policy.max_penalty
      @final_penalty = @penalty_units*@late_policy.penalty_per_unit
    elsif @penalty_units ==0
      @final_penalty = 0
    else
      @final_penalty = @late_policy.max_penalty
    end
    @review_of_review_mappings = Array.new
    for review_mapping_for_author in @review_mappings
      if(ReviewOfReviewMapping.find(:first, :conditions => ["review_mapping_id = ?",review_mapping_for_author.id])!= nil)
        @review_of_review_mappings << ReviewOfReviewMapping.find(:first, :conditions => ["review_mapping_id = ?",review_mapping_for_author.id])
      end
    end
  end
  
  def set_feedback
    @participant_id = params[:participant_id]
    @review_id = params[:review_id]
    @assignment_id = params[:assignment_id]
    update_author_feedback(@review_id,@assignment_id,params['author']['text'])
    redirect_to :action => 'view_scores', :id => @participant_id
  end
  
  def update_author_feedback(review_id,assignment_id,text)
    if(ReviewFeedback.find(:first,:conditions =>["review_id = ? and assignment_id = ?", review_id, assignment_id]))
      @review_feedback = ReviewFeedback.find(:first,:conditions =>["review_id = ? and assignment_id = ?", review_id, assignment_id])
      @review_feedback.additional_comments = text
      @review_feedback.update
    else
      @review_feedback = ReviewFeedback.new
      @review_feedback.review_id = review_id
      @review_feedback.assignment_id = assignment_id
      @review_feedback.additional_comments = text
{'fraction_non_alphanumeric': 0.11584898992873549, 'fraction_numerical': 0.0014135108074680488, 'mean_word_length': 3.6609936865220973, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3513687', 'n_tokens_mistral': 5447, 'n_tokens_neox': 5290, 'n_words': 1135}