<commit_before><?php defined('BASEPATH') OR exit('No direct script access allowed');


class Grade_model extends CI_Model
{

	var $kTeach;
	var $kStudent;
	var $kAssignment;
	var $points;
	var $average;
	var $status;
	var $footnote;

	function __construct()
	{
		parent::__construct();
	}

	function prepare_variables()
	{
		$variables = array("kTeach","kStudent","kAssignment","points","status","footnote");
		for($i = 0; $i < count($variables); $i++){
			$myVariable = $variables[$i];
			if($this->input->post($myVariable)){
				$this->$myVariable = $this->input->post($myVariable);
			}
		}
	}


	function get($kStudent,$kAssignment)
	{
		$this->db->where("kStudent",$kStudent);
		$this->db->where("kAssignment",$kAssignment);
		$this->db->from("grade");
		$output = $this->db->get()->row();
		return $output;
	}


	/**
	 * has_grade
	 * @param int $kStudent
	 * @param int $kAssignment
	 * determines if a student has a grade for a given assignment.
	 */
	function has_grade($kStudent,$kAssignment)
	{
		$this->db->where("kAssignment",$kAssignment);
		$this->db->where("kStudent",$kStudent);
		$this->db->from("grade");
		$result = $this->db->get()->num_rows();
		return $result;
	}

	/**
	 * batch_insert
	 * @param int $kAssignment
	 * @param int $kTeach
	 * @param varchar $term
	 * @param in $year
	 * Finds all the students with a given assignment of the same term, teacher and year and creates new records for the student.
	 */
	function batch_insert($kAssignment,$kTeach,$term,$year,$grade_start, $grade_end)
	{
		$this->db->select("distinct(`grade`.`kStudent`)");
		$this->db->from("assignment");
		$this->db->join("grade","grade.kAssignment = assignment.kAssignment","LEFT");
		$this->db->join("student", "grade.kStudent = student.kStudent");
		$this->db->where("(student.stuGrade BETWEEN $grade_start AND $grade_end)");
		$this->db->where("assignment.kTeach",$kTeach);
		$this->db->where("term",$term);
		$this->db->where("year",$year);
		$this->db->where("grade.kStudent IS NOT NULL");
		$this->db->where_not("grade.kAssignment",$kAssignment);
		$students = $this->db->get()->result();
		foreach($students as $student){
			$data = array("kAssignment"=>$kAssignment, "kStudent"=>$student->kStudent,"points"=>"0");
			$this->db->insert("grade",$data);
		}
		return $students;
	}


	function update($kStudent, $kAssignment,$kTeach, $points,$total, $status,$footnote,$category)
	{
		$output = FALSE;
		//this variable is not declared in $_POST or $_GET. It must be calculated.
		$weight = $this->calculate_weight($kTeach,$category);
		$this->average = $points/$total*$weight/100;
		//if the status is either "Exc" or "Abs" or anything else for that matter,
		// then the grade is counted at full value
		if($status == "Exc" || $status== "Abs"){
			$this->average = 1;

		}
		$data = array("points" => $points,"status"=>$status,"footnote"=>$footnote,"average"=>$this->average);
		if($this->has_grade($kStudent, $kAssignment) > 0){
			$this->db->where("kAssignment",$kAssignment);
			$this->db->where("kStudent",$kStudent);
			$this->db->update("grade", $data);
			$output = TRUE;
		}else{
			$data["kStudent"] = $kStudent;
			$data["kAssignment"] = $kAssignment;
			$this->db->insert("grade", $data);
			$output = $this->db->insert_id();
			$output = $this->db->last_query();
		}
		return $this->average;

	}

	function calculate_weight($kTeach, $category)<commit_msg>Update to batch_insert to prevent duplicates.<commit_after><?php defined('BASEPATH') OR exit('No direct script access allowed');


class Grade_model extends CI_Model
{

	var $kTeach;
	var $kStudent;
	var $kAssignment;
	var $points;
	var $average;
	var $status;
	var $footnote;

	function __construct()
	{
		parent::__construct();
	}

	function prepare_variables()
	{
		$variables = array("kTeach","kStudent","kAssignment","points","status","footnote");
		for($i = 0; $i < count($variables); $i++){
			$myVariable = $variables[$i];
			if($this->input->post($myVariable)){
				$this->$myVariable = $this->input->post($myVariable);
			}
		}
	}


	function get($kStudent,$kAssignment)
	{
		$this->db->where("kStudent",$kStudent);
		$this->db->where("kAssignment",$kAssignment);
		$this->db->from("grade");
		$output = $this->db->get()->row();
		return $output;
	}


	/**
	 * has_grade
	 * @param int $kStudent
	 * @param int $kAssignment
	 * determines if a student has a grade for a given assignment.
	 */
	function has_grade($kStudent,$kAssignment)
	{
		$this->db->where("kAssignment",$kAssignment);
		$this->db->where("kStudent",$kStudent);
		$this->db->from("grade");
		$result = $this->db->get()->num_rows();
		return $result;
	}

	/**
	 * batch_insert
	 * @param int $kAssignment
	 * @param int $kTeach
	 * @param varchar $term
	 * @param in $year
	 * Finds all the students with a given assignment of the same term, teacher and year and creates new records for the student.
	 */
	function batch_insert($kAssignment,$kTeach,$term,$year,$grade_start, $grade_end)
	{
		$this->db->select("distinct(`grade`.`kStudent`)");
		$this->db->from("assignment");
		$this->db->join("grade","grade.kAssignment = assignment.kAssignment","LEFT");
		$this->db->join("student", "grade.kStudent = student.kStudent");
		$this->db->where("(student.stuGrade BETWEEN $grade_start AND $grade_end)");
		$this->db->where("assignment.kTeach",$kTeach);
		$this->db->where("term",$term);
		$this->db->where("year",$year);
		$this->db->where("grade.kStudent IS NOT NULL");
		$this->db->where("grade.kAssignment !=$kAssignment");
		$students = $this->db->get()->result();
		print $this->db->last_query();
		foreach($students as $student){
			//$data = array("kAssignment"=>$kAssignment, "kStudent"=>$student->kStudent,"points"=>"0");
			//$this->db->insert("grade",$data);
			$this->update($student->kStudent, $kAssignment, $kTeach, 0, NULL, NULL, NULL, NULL);
		}
		return $students;
	}


	function update($kStudent, $kAssignment,$kTeach, $points,$total, $status,$footnote,$category)
	{
		$output = FALSE;
		//this variable is not declared in $_POST or $_GET. It must be calculated.
		$weight = 1;
		if(!$total){
			$total = 100;
		}
		if($category){
		$weight = $this->calculate_weight($kTeach,$category);
		}
		$this->average = $points/$total*$weight/100;
		//if the status is either "Exc" or "Abs" or anything else for that matter,
		// then the grade is counted at full value
		if($status == "Exc" || $status== "Abs"){
			$this->average = 1;

		}
		$data = array("points" => $points,"status"=>$status,"footnote"=>$footnote,"average"=>$this->average);
		if($this->has_grade($kStudent, $kAssignment) > 0){
			$this->db->where("kAssignment",$kAssignment);
			$this->db->where("kStudent",$kStudent);
			$this->db->update("grade", $data);
			$output = TRUE;
		}else{
			$data["kStudent"] = $kStudent;
			$data["kAssignment"] = $kAssignment;
			$this->db->insert("grade", $data);
			$output = $this->db->insert_id();
			$output = $this->db->last_query();
		}
		return $this->average;

	}

	function calculate_weight($kTeach, $category)
{'fraction_non_alphanumeric': 0.20607188887297723, 'fraction_numerical': 0.0027208935987397967, 'mean_word_length': 4.8006644518272426, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 157, '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': '2471575', 'n_tokens_mistral': 2619, 'n_tokens_neox': 2429, 'n_words': 477}