<commit_before><?php
/**
 * @package Abricos
 * @subpackage recordbook
 * @copyright 2016 <NAME>
 * @license http://opensource.org/licenses/mit-license.php MIT License
 * @author <NAME> <<EMAIL>>
 */

class RecordBookQuery {

    public static function FieldList(Ab_Database $db, $widget){

    	$where = $widget === 'groupEditor' ? "WHERE remove=0" : "";
    	
    	$sql = "
			SELECT 
					fieldid as id,
					fieldcode,
					field,
					frmstudy,
					qual,
    				note,
					depart,
					remove
			FROM ".$db->prefix."rb_fieldstudy
			".$where."
			ORDER BY remove ASC
		";
		return $db->query_read($sql);
    }
    
    public static function FieldItem(Ab_Database $db, $id){
    	$sql = "
			SELECT
					fieldid as id,
					fieldcode,
					field,
					frmstudy,
					qual,
    				note,
					depart,
    				remove
			FROM ".$db->prefix."rb_fieldstudy
			WHERE fieldid = ".bkint($id)."
			LIMIT 1
		";
    	return $db->query_first($sql);
    }
    
    public static function FieldUpdate(Ab_Database $db, $id, $d){
    	
    	$sql = "
			UPDATE ".$db->prefix."rb_fieldstudy
			SET
				fieldcode='".bkstr($d->fieldcode)."',
				field='".bkstr($d->field)."',
				frmstudy='".bkstr($d->frmstudy)."',
				qual='".bkstr($d->qual)."',
				depart='".bkstr($d->depart)."',
				note='".bkstr($d->note)."'
			WHERE fieldid=".bkint($id)."
			LIMIT 1
		";
    	$db->query_write($sql);
    }
    
    public static function FieldAppend(Ab_Database $db, $d){
    	 
         $sql = "
			INSERT INTO ".$db->prefix."rb_fieldstudy (
				fieldcode,field,frmstudy,qual,depart,note
			) VALUES (
				'".bkstr($d->fieldcode)."',
				'".bkstr($d->field)."',
				'".bkstr($d->frmstudy)."',
				'".bkstr($d->qual)."',
				'".bkstr($d->depart)."',
				'".bkstr($d->note)."'
			)
		";
      	$db->query_write($sql);
    }
    
    public static function FieldRemove(Ab_Database $db, $id, $restore){
    	 
    	$sql = "
			UPDATE ".$db->prefix."rb_fieldstudy
			SET
				remove=".bkint($restore)."
			WHERE fieldid=".bkint($id)."
			LIMIT 1
		";
    	$db->query_write($sql);
    }
    
    public static function SubjectList(Ab_Database $db, $fieldid, $pageSub, $limit = 15){
    	$from = $limit * (max($pageSub, 1) - 1);
    	
    	$sql = "
			SELECT
					subjectid as id,
					namesubject,
					formcontrol,
					numcrs,
					semestr,
					numhours,
    				project,
    				remove
			FROM ".$db->prefix."rb_subject
			WHERE fieldid = ".bkint($fieldid)."
			ORDER BY id DESC
			LIMIT ".$from.",".$limit."
		";
    	
    	return $db->query_read($sql);
    }
    
    public static function SubjectAppend(Ab_Database $db, $d){
    	$proj = bkint($d->project1).','.bkint($d->project2);
    	$hours = doubleval($d->numhours1).'/'.doubleval($d->numhours2);
    	
		$sql = "
				INSERT INTO ".$db->prefix."rb_subject (
					fieldid, namesubject, formcontrol, numcrs, semestr, numhours, project
				) VALUES (
						".bkint($d->id).",
						'".bkstr($d->namesubject)."',
						'".bkstr($d->formcontrol)."',
						".bkint($d->numcrs).",
						".bkint($d->semestr).",
						'".$hours."',
						 '".$proj."'
				)
		";
		$db->query_write($sql);
    }
    
    public static function SubjectEdit(Ab_Database $db, $d){
    	$proj = bkint($d->project1).','.bkint($d->project2);
    	$hours = doubleval($d->numhours1).'/'.doubleval($d->numhours2);
    	
    	$sql = "
			UPDATE ".$db->prefix."rb_subject
			SET 
				namesubject='".bkstr($d->namesubject)."',
				formcontrol='".bkstr($d->formcontrol)."',
				numcrs=".bkint($d->numcrs).",
				semestr=".bkint($d->semestr).",
				numhours='".$hours."',
				project='".$proj."'
			WHERE subjectid=".bkint($d->subjectid)." AND fieldid=".bkint($d->id)."
			LIMIT 1
		";
    	
    	$db->query_write($sql);
    
    }
    
    public static function SubjectRemove(Ab_Database $db, $id, $restore){
    	$sql = "
			UPDATE ".$db->prefix."rb_subject
			SET
				remove=".bkint($restore)."
			WHERE subjectid=".bkint($id)."
			LIMIT 1
		";
    	
    	$db->query_write($sql);
    }
    
    public static function GroupList(Ab_Database $db, $d, $limit = 15){
    	$from = $limit * (max($d->page, 1) - 1);
    	
    	$sql = "
			SELECT 
    				g.groupid as id,
    				g.numgroup,
    				g.numcrs,
    				g.dateline,
    				g.remove as grRemove,
    				f.field,
    				f.fieldid,
    				f.fieldcode,
    				f.frmstudy,
    				f.note,
    				f.remove
    		FROM ".$db->prefix."rb_groups g
    		INNER JOIN ".$db->prefix."rb_fieldstudy f ON f.fieldid = g.fieldid
    		WHERE f.frmstudy = '".bkstr($d->frmstudy)."'
    		ORDER BY id DESC
    		LIMIT ".$from.",".$limit."
		";
    		return $db->query_read($sql);
    }
    
    public static function CountGroup(Ab_Database $db, $frmstudy){
    
    	$sql = "
			SELECT COUNT( g.groupid ) as cnt
			FROM ".$db->prefix."rb_groups g
			INNER JOIN ".$db->prefix."rb_fieldstudy f ON f.fieldid = g.fieldid
			WHERE f.frmstudy = '".bkstr($frmstudy)."'
		";
    	$row = $db->query_first($sql);
    	return $row['cnt'];
    }
    
    public static function GroupAppend(Ab_Database $db, $group){
    	 
    	$sql = "
			INSERT INTO ".$db->prefix."rb_groups (fieldid, numgroup, numcrs, dateline)
					VALUES (
						".bkint($group->currentFieldId).",
						'".bkstr($group->numgroup)."',
							".bkint($group->numcrs).",
							".bkint($group->year)."
					)
		";
    	$db->query_write($sql);
    }
    
    public static function GroupItem(Ab_Database $db, $groupid){

    	$sql = "
			SELECT
    				g.groupid as id,
    				g.numgroup,
    				g.numcrs,
    				g.dateline,
    				f.field,
    				f.fieldid,
    				f.fieldcode,
    				f.frmstudy,
    				f.note
    		FROM ".$db->prefix."rb_groups g
    		INNER JOIN ".$db->prefix."rb_fieldstudy f ON f.fieldid = g.fieldid
    		WHERE g.groupid = ".bkint($groupid)."
    		LIMIT 1
		";
    	return $db->query_first($sql);
    }
    
    public static function GroupEdit(Ab_Database $db, $group){
    
    	$sql = "
			UPDATE ".$db->prefix."rb_groups
			SET 
				fieldid=".bkint($group->currentFieldId).",
				numgroup='".bkstr($group->numgroup)."',
				numcrs=".bkint($group->numcrs).",
				dateline=".bkint($group->year)."
			WHERE groupid=".bkint($group->groupid)."
			LIMIT 1
		";
    	
    	$db->query_write($sql);
    }
    
    public static function GroupRemove(Ab_Database $db, $d){
    
    	$sql = "
			UPDATE ".$db->prefix."rb_groups
			SET
				remove=".bkint($d->remove)."
			WHERE groupid=".bkint($d->groupid)."
			LIMIT 1
		";
    	 
    	$db->query_write($sql);
    }
    
    public static function GroupFind(Ab_Database $db, $d){
    	
    	if(!$d->value){
    		return false;
    	}
    
    	$sql = "
			SELECT
    				g.groupid as id,
    				g.numgroup,
    				g.numcrs,
    				g.dateline,
    				f.field,
    				f.fieldid,
    				f.fieldcode,
    				f.frmstudy,
    				f.note
    		FROM ".$db->prefix."rb_groups g
    		INNER JOIN ".$db->prefix."rb_fieldstudy f ON f.fieldid = g.fieldid
    		WHERE g.numgroup LIKE '".bkstr($d->value)."%' 
    				AND f.frmstudy = '".bkstr($d->frmstudy)."'
    						AND g.remove = 0
		";
    	return $db->query_read($sql);
    }
    
    public static function StudList(Ab_Database $db, $groupid){
    	 
    	$sql = "
			SELECT
					id as id,
    				groupid,
    				numbook,
    				fio,
    				datebirth,
    				preveducation,
    				listgroup
    		FROM ".$db->prefix."rb_students
    		WHERE groupid=".bkint($groupid)." 
    					AND transferal=0
    		ORDER BY listgroup ASC, fio ASC
		";
    	return $db->query_read($sql);
    }
    
    public static function StudAppend(Ab_Database $db, $stud){
    
    	$sql = "
			INSERT INTO ".$db->prefix."rb_students (groupid, numbook, fio, datebirth, preveducation)
			VALUES (
						".bkint($stud->gid).",
						'".bkstr($stud->numbook)."',
						'".bkstr($stud->fio)."',
						".bkint($stud->datebirth).",
						'".bkstr($stud->preveducation)."'
			)
		";
    	$db->query_write($sql);
    }
    
    public static function StudEdit(Ab_Database $db, $stud){
    
    	$sql = "
			UPDATE ".$db->prefix."rb_students
			SET
				groupid=".bkint($stud->gid).",
				numbook='".bkstr($stud->numbook)."',
				fio='".bkstr($stud->fio)."',
				datebirth=".bkint($stud->datebirth).",
				preveducation='".bkstr($stud->preveducation)."'
			WHERE id=".bkint($stud->sid)."
			LIMIT 1
		";
    	$db->query_write($sql);
    }
    
    public static function StudRemove(Ab_Database $db, $d){
    	$sql = "
				UPDATE ".$db->prefix."rb_students
				SET
					transferal = ".bkint($d->remove)."
    			WHERE id=".bkint($d->studid)."
    			LIMIT 1
		";
    	$db->query_write($sql);
    }
    
    public static function SubjectListSheet(Ab_Database $db, $d){
    	 
    	$where = "fieldid = ".bkint($d->fieldid)."
						AND numcrs = ".bkint($d->numcrs)."
								AND semestr = ".bkint($d->semestr)."
										AND remove = 0";
    	
    	switch($d->type){
    		case 1:
    		case 2:
    			$where .= " AND formcontrol <> '-'";
    				break;
    		case 3:
    		case 4:
    			$where .= " AND project LIKE '%1%'";
    				break;
    	}
    	
    	$sql = "
				SELECT
						subjectid as id,
						namesubject,
						formcontrol,
						numcrs,
						semestr,
						numhours,
    					project
				FROM ".$db->prefix."rb_subject
				WHERE ".$where."
			";
    	return $db->query_read($sql);
    }
    
    public static function SheetList(Ab_Database $db, $d){
    
    	$sql = "
			SELECT
					s.sheetid as id,
					s.subjectid,
    				sj.namesubject,
    				sj.formcontrol,
					s.firstattproc,
					s.secondattproc,
					s.thirdattproc,
					s.date,
    				s.type,
    				s.fioteacher,
    				sj.remove,
    				sj.project
			FROM ".$db->prefix."rb_sheet s INNER JOIN ".$db->prefix."rb_subject sj ON sj.subjectid = s.subjectid
			WHERE s.groupid = ".bkint($d->groupid)."
						AND sj.semestr = ".bkint($d->currentSemestr)." 
								AND sj.numcrs = ".bkint($d->numcrs)."
									 AND sj.fieldid = ".bkint($d->fieldid)."
			ORDER BY id DESC
		";
    	return $db->query_read($sql);
    }
    
    public static function SheetAppend(Ab_Database $db, $d){
    
    	$a = array(30, 30, 40);
    	$typeSheet = $d->typeSheet;
   		if($typeSheet === 3 || $typeSheet === 4 || $d->isPractic){
   			$a[0] = 0;
   			$a[1] = 0;
   			$a[2] = 100;
   		}
   		
    	$sql = "
			INSERT INTO ".$db->prefix."rb_sheet (subjectid, groupid, firstattproc, secondattproc, thirdattproc, date, type, fioteacher)
			VALUES (
					".bkint($d->idSubject).",
					".bkint($d->groupid).",
						".$a[0].",
						".$a[1].",
						".$a[2].",		
					".bkint($d->date).",
					".bkint($typeSheet).",
					'".bkstr($d->fioteacher)."'
			)
		";
    	$db->query_write($sql);
    	
    		$idSheet = mysql_insert_id();
    		$arrStud = array();
    		$valIns = "";
    		
    		if($d->typeSheet === 2 || $d->typeSheet === 4){
    			$arrStud = $d->arrStudId;
    			$arrCount = count($arrStud);
    			
    		} else {
    			$sql = "
    				SELECT id
    				FROM ".$db->prefix."rb_students
    				WHERE groupid=".bkint($d->groupid)." AND transferal = 0
    			";
    			$rows = $db->query_read($sql);
    			
    			while ($d = $db->fetch_row($rows)){
					array_push($arrStud, $d[0]);
    			}
    			$arrCount = count($arrStud);
    		}
    		
    		if($arrCount === 0) return false;
    		
		    		foreach($arrStud as $key => $val){
		    			$valIns .= "(".$idSheet.",".$val.")";
		    			if($key < $arrCount - 1){
		    				$valIns .= ",";
		    			}
		    		}
		    		$sql = "<commit_msg>bugfix: insert_id()<commit_after><?php
/**
 * @package Abricos
 * @subpackage recordbook
 * @copyright 2016 <NAME>
 * @license http://opensource.org/licenses/mit-license.php MIT License
 * @author <NAME> <<EMAIL>>
 */

class RecordBookQuery {

    public static function FieldList(Ab_Database $db, $widget){

    	$where = $widget === 'groupEditor' ? "WHERE remove=0" : "";
    	
    	$sql = "
			SELECT 
					fieldid as id,
					fieldcode,
					field,
					frmstudy,
					qual,
    				note,
					depart,
					remove
			FROM ".$db->prefix."rb_fieldstudy
			".$where."
			ORDER BY remove ASC
		";
		return $db->query_read($sql);
    }
    
    public static function FieldItem(Ab_Database $db, $id){
    	$sql = "
			SELECT
					fieldid as id,
					fieldcode,
					field,
					frmstudy,
					qual,
    				note,
					depart,
    				remove
			FROM ".$db->prefix."rb_fieldstudy
			WHERE fieldid = ".bkint($id)."
			LIMIT 1
		";
    	return $db->query_first($sql);
    }
    
    public static function FieldUpdate(Ab_Database $db, $id, $d){
    	
    	$sql = "
			UPDATE ".$db->prefix."rb_fieldstudy
			SET
				fieldcode='".bkstr($d->fieldcode)."',
				field='".bkstr($d->field)."',
				frmstudy='".bkstr($d->frmstudy)."',
				qual='".bkstr($d->qual)."',
				depart='".bkstr($d->depart)."',
				note='".bkstr($d->note)."'
			WHERE fieldid=".bkint($id)."
			LIMIT 1
		";
    	$db->query_write($sql);
    }
    
    public static function FieldAppend(Ab_Database $db, $d){
    	 
         $sql = "
			INSERT INTO ".$db->prefix."rb_fieldstudy (
				fieldcode,field,frmstudy,qual,depart,note
			) VALUES (
				'".bkstr($d->fieldcode)."',
				'".bkstr($d->field)."',
				'".bkstr($d->frmstudy)."',
				'".bkstr($d->qual)."',
				'".bkstr($d->depart)."',
				'".bkstr($d->note)."'
			)
		";
      	$db->query_write($sql);
    }
    
    public static function FieldRemove(Ab_Database $db, $id, $restore){
    	 
    	$sql = "
			UPDATE ".$db->prefix."rb_fieldstudy
			SET
				remove=".bkint($restore)."
			WHERE fieldid=".bkint($id)."
			LIMIT 1
		";
    	$db->query_write($sql);
    }
    
    public static function SubjectList(Ab_Database $db, $fieldid, $pageSub, $limit = 15){
    	$from = $limit * (max($pageSub, 1) - 1);
    	
    	$sql = "
			SELECT
					subjectid as id,
					namesubject,
					formcontrol,
					numcrs,
					semestr,
					numhours,
    				project,
    				remove
			FROM ".$db->prefix."rb_subject
			WHERE fieldid = ".bkint($fieldid)."
			ORDER BY id DESC
			LIMIT ".$from.",".$limit."
		";
    	
    	return $db->query_read($sql);
    }
    
    public static function SubjectAppend(Ab_Database $db, $d){
    	$proj = bkint($d->project1).','.bkint($d->project2);
    	$hours = doubleval($d->numhours1).'/'.doubleval($d->numhours2);
    	
		$sql = "
				INSERT INTO ".$db->prefix."rb_subject (
					fieldid, namesubject, formcontrol, numcrs, semestr, numhours, project
				) VALUES (
						".bkint($d->id).",
						'".bkstr($d->namesubject)."',
						'".bkstr($d->formcontrol)."',
						".bkint($d->numcrs).",
						".bkint($d->semestr).",
						'".$hours."',
						 '".$proj."'
				)
		";
		$db->query_write($sql);
    }
    
    public static function SubjectEdit(Ab_Database $db, $d){
    	$proj = bkint($d->project1).','.bkint($d->project2);
    	$hours = doubleval($d->numhours1).'/'.doubleval($d->numhours2);
    	
    	$sql = "
			UPDATE ".$db->prefix."rb_subject
			SET 
				namesubject='".bkstr($d->namesubject)."',
				formcontrol='".bkstr($d->formcontrol)."',
				numcrs=".bkint($d->numcrs).",
				semestr=".bkint($d->semestr).",
				numhours='".$hours."',
				project='".$proj."'
			WHERE subjectid=".bkint($d->subjectid)." AND fieldid=".bkint($d->id)."
			LIMIT 1
		";
    	
    	$db->query_write($sql);
    
    }
    
    public static function SubjectRemove(Ab_Database $db, $id, $restore){
    	$sql = "
			UPDATE ".$db->prefix."rb_subject
			SET
				remove=".bkint($restore)."
			WHERE subjectid=".bkint($id)."
			LIMIT 1
		";
    	
    	$db->query_write($sql);
    }
    
    public static function GroupList(Ab_Database $db, $d, $limit = 15){
    	$from = $limit * (max($d->page, 1) - 1);
    	
    	$sql = "
			SELECT 
    				g.groupid as id,
    				g.numgroup,
    				g.numcrs,
    				g.dateline,
    				g.remove as grRemove,
    				f.field,
    				f.fieldid,
    				f.fieldcode,
    				f.frmstudy,
    				f.note,
    				f.remove
    		FROM ".$db->prefix."rb_groups g
    		INNER JOIN ".$db->prefix."rb_fieldstudy f ON f.fieldid = g.fieldid
    		WHERE f.frmstudy = '".bkstr($d->frmstudy)."'
    		ORDER BY id DESC
    		LIMIT ".$from.",".$limit."
		";
    		return $db->query_read($sql);
    }
    
    public static function CountGroup(Ab_Database $db, $frmstudy){
    
    	$sql = "
			SELECT COUNT( g.groupid ) as cnt
			FROM ".$db->prefix."rb_groups g
			INNER JOIN ".$db->prefix."rb_fieldstudy f ON f.fieldid = g.fieldid
			WHERE f.frmstudy = '".bkstr($frmstudy)."'
		";
    	$row = $db->query_first($sql);
    	return $row['cnt'];
    }
    
    public static function GroupAppend(Ab_Database $db, $group){
    	 
    	$sql = "
			INSERT INTO ".$db->prefix."rb_groups (fieldid, numgroup, numcrs, dateline)
					VALUES (
						".bkint($group->currentFieldId).",
						'".bkstr($group->numgroup)."',
							".bkint($group->numcrs).",
							".bkint($group->year)."
					)
		";
    	$db->query_write($sql);
    }
    
    public static function GroupItem(Ab_Database $db, $groupid){

    	$sql = "
			SELECT
    				g.groupid as id,
    				g.numgroup,
    				g.numcrs,
    				g.dateline,
    				f.field,
    				f.fieldid,
    				f.fieldcode,
    				f.frmstudy,
    				f.note
    		FROM ".$db->prefix."rb_groups g
    		INNER JOIN ".$db->prefix."rb_fieldstudy f ON f.fieldid = g.fieldid
    		WHERE g.groupid = ".bkint($groupid)."
    		LIMIT 1
		";
    	return $db->query_first($sql);
    }
    
    public static function GroupEdit(Ab_Database $db, $group){
    
    	$sql = "
			UPDATE ".$db->prefix."rb_groups
			SET 
				fieldid=".bkint($group->currentFieldId).",
				numgroup='".bkstr($group->numgroup)."',
				numcrs=".bkint($group->numcrs).",
				dateline=".bkint($group->year)."
			WHERE groupid=".bkint($group->groupid)."
			LIMIT 1
		";
    	
    	$db->query_write($sql);
    }
    
    public static function GroupRemove(Ab_Database $db, $d){
    
    	$sql = "
			UPDATE ".$db->prefix."rb_groups
			SET
				remove=".bkint($d->remove)."
			WHERE groupid=".bkint($d->groupid)."
			LIMIT 1
		";
    	 
    	$db->query_write($sql);
    }
    
    public static function GroupFind(Ab_Database $db, $d){
    	
    	if(!$d->value){
    		return false;
    	}
    
    	$sql = "
			SELECT
    				g.groupid as id,
    				g.numgroup,
    				g.numcrs,
    				g.dateline,
    				f.field,
    				f.fieldid,
    				f.fieldcode,
    				f.frmstudy,
    				f.note
    		FROM ".$db->prefix."rb_groups g
    		INNER JOIN ".$db->prefix."rb_fieldstudy f ON f.fieldid = g.fieldid
    		WHERE g.numgroup LIKE '".bkstr($d->value)."%' 
    				AND f.frmstudy = '".bkstr($d->frmstudy)."'
    						AND g.remove = 0
		";
    	return $db->query_read($sql);
    }
    
    public static function StudList(Ab_Database $db, $groupid){
    	 
    	$sql = "
			SELECT
					id as id,
    				groupid,
    				numbook,
    				fio,
    				datebirth,
    				preveducation,
    				listgroup
    		FROM ".$db->prefix."rb_students
    		WHERE groupid=".bkint($groupid)." 
    					AND transferal=0
    		ORDER BY listgroup ASC, fio ASC
		";
    	return $db->query_read($sql);
    }
    
    public static function StudAppend(Ab_Database $db, $stud){
    
    	$sql = "
			INSERT INTO ".$db->prefix."rb_students (groupid, numbook, fio, datebirth, preveducation)
			VALUES (
						".bkint($stud->gid).",
						'".bkstr($stud->numbook)."',
						'".bkstr($stud->fio)."',
						".bkint($stud->datebirth).",
						'".bkstr($stud->preveducation)."'
			)
		";
    	$db->query_write($sql);
    }
    
    public static function StudEdit(Ab_Database $db, $stud){
    
    	$sql = "
			UPDATE ".$db->prefix."rb_students
			SET
				groupid=".bkint($stud->gid).",
				numbook='".bkstr($stud->numbook)."',
				fio='".bkstr($stud->fio)."',
				datebirth=".bkint($stud->datebirth).",
				preveducation='".bkstr($stud->preveducation)."'
			WHERE id=".bkint($stud->sid)."
			LIMIT 1
		";
    	$db->query_write($sql);
    }
    
    public static function StudRemove(Ab_Database $db, $d){
    	$sql = "
				UPDATE ".$db->prefix."rb_students
				SET
					transferal = ".bkint($d->remove)."
    			WHERE id=".bkint($d->studid)."
    			LIMIT 1
		";
    	$db->query_write($sql);
    }
    
    public static function SubjectListSheet(Ab_Database $db, $d){
    	 
    	$where = "fieldid = ".bkint($d->fieldid)."
						AND numcrs = ".bkint($d->numcrs)."
								AND semestr = ".bkint($d->semestr)."
										AND remove = 0";
    	
    	switch($d->type){
    		case 1:
    		case 2:
    			$where .= " AND formcontrol <> '-'";
    				break;
    		case 3:
    		case 4:
    			$where .= " AND project LIKE '%1%'";
    				break;
    	}
    	
    	$sql = "
				SELECT
						subjectid as id,
						namesubject,
						formcontrol,
						numcrs,
						semestr,
						numhours,
    					project
				FROM ".$db->prefix."rb_subject
				WHERE ".$where."
			";
    	return $db->query_read($sql);
    }
    
    public static function SheetList(Ab_Database $db, $d){
    
    	$sql = "
			SELECT
					s.sheetid as id,
					s.subjectid,
    				sj.namesubject,
    				sj.formcontrol,
					s.firstattproc,
					s.secondattproc,
					s.thirdattproc,
					s.date,
    				s.type,
    				s.fioteacher,
    				sj.remove,
    				sj.project
			FROM ".$db->prefix."rb_sheet s INNER JOIN ".$db->prefix."rb_subject sj ON sj.subjectid = s.subjectid
			WHERE s.groupid = ".bkint($d->groupid)."
						AND sj.semestr = ".bkint($d->currentSemestr)." 
								AND sj.numcrs = ".bkint($d->numcrs)."
									 AND sj.fieldid = ".bkint($d->fieldid)."
			ORDER BY id DESC
		";
    	return $db->query_read($sql);
    }
    
    public static function SheetAppend(Ab_Database $db, $d){
    
    	$a = array(30, 30, 40);
    	$typeSheet = $d->typeSheet;
   		if($typeSheet === 3 || $typeSheet === 4 || $d->isPractic){
   			$a[0] = 0;
   			$a[1] = 0;
   			$a[2] = 100;
   		}
   		
    	$sql = "
			INSERT INTO ".$db->prefix."rb_sheet (subjectid, groupid, firstattproc, secondattproc, thirdattproc, date, type, fioteacher)
			VALUES (
					".bkint($d->idSubject).",
					".bkint($d->groupid).",
						".$a[0].",
						".$a[1].",
						".$a[2].",		
					".bkint($d->date).",
					".bkint($typeSheet).",
					'".bkstr($d->fioteacher)."'
			)
		";
    	$db->query_write($sql);
    	
    		$idSheet = $db->insert_id();
    		$arrStud = array();
    		$valIns = "";
    		
    		if($d->typeSheet === 2 || $d->typeSheet === 4){
    			$arrStud = $d->arrStudId;
    			$arrCount = count($arrStud);
    			
    		} else {
    			$sql = "
    				SELECT id
    				FROM ".$db->prefix."rb_students
    				WHERE groupid=".bkint($d->groupid)." AND transferal = 0
    			";
    			$rows = $db->query_read($sql);
    			
    			while ($d = $db->fetch_row($rows)){
					array_push($arrStud, $d[0]);
    			}
    			$arrCount = count($arrStud);
    		}
    		
    		if($arrCount === 0) return false;
    		
		    		foreach($arrStud as $key => $val){
		    			$valIns .= "(".$idSheet.",".$val.")";
		    			if($key < $arrCount - 1){
		    				$valIns .= ",";
		    			}
		    		}
		    		$sql = "
{'fraction_non_alphanumeric': 0.18343427301642629, 'fraction_numerical': 0.005577099037078995, 'mean_word_length': 2.031567824593845, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 280, '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': '4087467', 'n_tokens_mistral': 10761, 'n_tokens_neox': 8726, 'n_words': 1499}