<commit_before><?php
/**                    httpscripts/generate_assessment_columns.php
 */

require_once('../../scripts/http_head_options.php');

if(!isset($xmlid)){print 'Failed'; exit;}

$eid=$xmlid;
$AssDef=fetchAssessmentDefinition($eid);
$AssCount=fetchAssessmentCount($eid);
$AssDef=array_merge($AssDef,$AssCount);
$crid=$AssDef['Course']['value'];
$gena=$AssDef['GradingScheme']['value'];
$subject=$AssDef['Subject']['value'];
$compstatus=$AssDef['ComponentStatus']['value'];
$strandstatus=$AssDef['StrandStatus']['value'];
$description=$AssDef['Description']['value'];
$label=$AssDef['PrintLabel']['value'];
$total=$AssDef['Total']['value'];
$deadline=$AssDef['Deadline']['value'];
/* Any estimates or targets or whatever get assessment type = other. */
if($AssDef['ResultStatus']['value']=='R'){$asstype='yes';}
else{$asstype='other';}

$yearnow=get_curriculumyear($crid);


/* Where to apply this assessment in the MarkBook will be indicated in
 * a cohorts array with the cohort pointing to where the students are
 * now - and this could be for another course altogether.
 */
$cohorts=array();
/* Always add the cohort for the assessment year */
if($AssDef['Stage']['value']=='%'){
	$stages=(array)list_course_stages($crid);
	}
else{
	$stages[]=array('id'=>$AssDef['Stage']['value'],'name'=>$AssDef['Stage']['value']);
	}
foreach($stages as $stage){
	$cohorts[]=array('course_id'=>$crid,'stage'=>$stage['id'],'year'=>$AssDef['Year']['value']);
	}

/**
 * Is this assessment for the current year or from a previous year?
 * If for a previous year then we need to find where the students are
 * now: (1) the next stage in the same course; (2) the first stage of
 * the following course if appropriate.
 *
 */


if($AssDef['Year']['value']!=$yearnow){
	$yeardiff=$yearnow-$AssDef['Year']['value'];
	$stagegones=array();
	$stages=list_course_stages($crid);
	$cridscourses=array();
	$courses=(array)list_courses();
	foreach($courses as $course){
		$cridscourses[$course['id']]=$course;
		}
	$nextcrid=$cridscourses[$crid]['nextcourse_id'];


	if($AssDef['Stage']['value']=='%'){
		$stagegones=(array)$stages;
		$sc=0;
		}
	else{
		$stagegones[]=array('id'=>$AssDef['Stage']['value'],'name'=>$AssDef['Stage']['value']);
		$c=0;
		foreach($stages as $stagegone){
			if($stagegone['id']==$AssDef['Stage']['value']){$sc=$c;}
			$c++;
			}
		unset($c);
		}
	foreach($stagegones as $stagegone){
		$stagediff=$sc+$yeardiff;
		if($stagediff<(sizeof($stages))){
			$cohorts[]=array('course_id'=>$crid,'stage'=>$stages[$stagediff]['id'],'year'=>$yearnow);
			}
		elseif($nextcrid!='' and $nextcrid!='1000'){
			/* This will identify the first stage of the next course. */
			$nextstages=list_course_stages($nextcrid);
			if(isset($nextstages[$yeardiff-1]['id'])){
				$cohorts[]=array('course_id'=>$nextcrid,'stage'=>$nextstages[$yeardiff-1]['id'],'year'=>$yearnow);
				//trigger_error($stagegone['id'].' '.$stagediff.' -------->'.$yeardiff.' : '.$nextstages[$yeardiff-1]['id'],E_USER_WARNING);
				}
			}
		$sc++;
		}
	}



/* Check user has permission to configure */
$perm=getCoursePerm($crid,$respons);
$neededperm='x';
if($perm["$neededperm"]=1 and $AssDef['MarkCount']['value']==0){

	/*find the appropriate markdef_name*/
	if($gena!='' and $gena!=' '){
		$grading_grades=$AssDef['GradingScheme']['grades'];
		$d_m=mysql_query("SELECT name FROM markdef WHERE
						grading_name='$gena' AND scoretype='grade' 
						AND (course_id='%' OR course_id='$crid');");
		if(mysql_num_rows($d_m)==0){
			/* If none exists then create keeping the name field unique for this crid.  */
			$markdef_name=$crid.' '.$gena;
			mysql_query("INSERT INTO markdef SET
						name='$markdef_name', scoretype='grade', grading_name='$gena',
						comment='$description', outoftotal='$total', author='ClaSS', 
						course_id='$crid', subject_id='$subject';");
			}
		else{
			$markdef_name=mysql_result($d_m,0);
			}
		$markdef_scoretype='grade';
		}
	else{
		$d_m=mysql_query("SELECT name FROM markdef WHERE
						scoretype='value' AND (course_id='%' OR course_id='$crid');");
		$markdef_name=mysql_result($d_m,0);
		$markdef_scoretype='value';
		}
	mysql_free_result($d_m);

	if($deadline!='0000-00-00'){$entrydate=$deadline;}
	else{$entrydate=date('Y').'-'.date('n').'-'.date('j');}

	/*make a list of subjects that will need distinct new marks*/
	$subjects=array();
	if($subject!='%'){$subjects[]['id']=$subject;}
	else{
		$subjects=list_course_subjects($crid);
		}
		
	/* Generate a mark for each bid/pid combination */
	foreach($subjects as $subject){
		$bid=$subject['id'];
		$components=(array)list_subject_components($bid,$crid,$compstatus);
		/* If there is no component for this subject or
		   components are not requested then $pid is blank*/
		if(sizeof($components)==0){$components[0]['id']='';}
		foreach($components as $component){
			$strands=(array)list_subject_components($component['id'],$crid,$strandstatus);
			if(sizeof($strands)==0){$strands[0]['id']=$component['id'];}
			foreach($strands as $strand){
				$pid=$strand['id'];

				if($bid==' ' or $bid=='G'){$bid='%';}
				$cidno=0;
				foreach($cohorts as $cohort){
					$cohid=update_cohort($cohort);
					$cridnow=$cohort['course_id'];
					$stagenow=$cohort['stage'];
					$bidnow='';
					$pidnow='';
					if($cridnow!=$crid){
						/* If carrying forward to another course need
						   to accomodate changes in currilucum
						   structure ie. hunt for equivalent subject/component/strand combination */
						$d_s=mysql_query("SELECT DISTINCT subject_id FROM component 
									WHERE course_id='$cridnow' AND id='$pid' AND subject_id='$bid' AND status!='U';");
						if(mysql_num_rows($d_s)>0){$bidnow=$bid;$pidnow=$pid;}
						else{
							if($pid!=''){
								$d_s=mysql_query("SELECT DISTINCT subject_id FROM component 
											WHERE course_id='$cridnow' AND id='' AND subject_id='$pid' AND status!='U';");
								if(mysql_num_rows($d_s)>0){$bidnow=$pid;$pidnow='';}
								else{
									if($pid!=''){
										$d_s=mysql_query("SELECT DISTINCT subject_id FROM component 
															WHERE course_id='$cridnow' AND id='$pid' AND status!='U';");
										if(mysql_num_rows($d_s)>0){$bidnow=mysql_result($d_s,0);$pidnow=$pid;}
										if($bidnow!=''){
											$d_s=mysql_query("SELECT DISTINCT subject_id FROM component 
														   WHERE course_id='$cridnow' AND id='$bidnow' AND status!='U';");
											if(mysql_num_rows($d_s)>0){$bidnow=mysql_result($d_s,0);$pidnow=$pid;}
											}
										}
									}
								}
							}
						}
					else{
						$bidnow=$bid;
						$pidnow=$pid;
						}

					/* Can only carry forward to next course if their is a correpsonding subject */
					if($bidnow!=''){
						//trigger_error($stage.' '.$crid.': '.$bid.' : '.$pid.' -------->'.$cridnow.' : '.$bidnow.' : '.$pidnow.' : '.$stagenow,E_USER_WARNING);
	
						mysql_query("INSERT INTO mark (entrydate, marktype, topic, comment, author,
						 def_name, assessment, component_id) VALUES ('$entrydate', 'score', '$description', 
						 '', 'ClaSS', '$markdef_name', '$asstype', '$pidnow');");
						$mid=mysql_insert_id();

						/* Make entry in eidmid for this new mark. */
						mysql_query("INSERT INTO eidmid (assessment_id,mark_id) VALUES ('$eid', '$mid');");

						$d_class=mysql_query("SELECT id FROM class 
												WHERE cohort_id='$cohid' AND subject_id LIKE '$bidnow';");
						/* Make entries in midcid for the new mark */
						while($d_cid=mysql_fetch_array($d_class,MYSQL_NUM)){
							$cid=$d_cid[0];
							mysql_query("INSERT INTO midcid (mark_id,class_id) VALUES ('$mid', '$cid');");
							$cidno++;
							}
						mysql_free_result($d_class);
						//trigger_error($stagenow.' '.$cridnow.': '.$bidnow.' : '.$pidnow. ' '.$cidno,E_USER_WARNING);
						}

					if($cidno>0){
						$d_eidsids=mysql_query("SELECT eidsid.student_id, eidsid.result, eidsid.value, comments.detail 
		   					FROM eidsid LEFT JOIN comments ON comments.eidsid_id=eidsid.id WHERE
							eidsid.subject_id LIKE '$bid' AND eidsid.component_id='$pid' AND eidsid.assessment_id='$eid';");<commit_msg>improved the generate_assessment_columns script to handle curriculum structure changes from year to year<commit_after><?php
/**                    httpscripts/generate_assessment_columns.php
 */

require_once('../../scripts/http_head_options.php');

if(!isset($xmlid)){print 'Failed'; exit;}

$eid=$xmlid;
$AssDef=fetchAssessmentDefinition($eid);
$AssCount=fetchAssessmentCount($eid);
$AssDef=array_merge($AssDef,$AssCount);
$crid=$AssDef['Course']['value'];
$gena=$AssDef['GradingScheme']['value'];
$subject=$AssDef['Subject']['value'];
$compstatus=$AssDef['ComponentStatus']['value'];
$strandstatus=$AssDef['StrandStatus']['value'];
$description=$AssDef['Description']['value'];
$label=$AssDef['PrintLabel']['value'];
$total=$AssDef['Total']['value'];
$deadline=$AssDef['Deadline']['value'];
/* Any estimates or targets or whatever get assessment type = other. */
if($AssDef['ResultStatus']['value']=='R'){$asstype='yes';}
else{$asstype='other';}

$yearnow=get_curriculumyear($crid);


/* Where to apply this assessment in the MarkBook will be indicated in
 * a cohorts array with the cohort pointing to where the students are
 * now - and this could be for another course altogether.
 */
$cohorts=array();
/* Always add the cohort for the assessment year */
if($AssDef['Stage']['value']=='%'){
	$stages=(array)list_course_stages($crid);
	}
else{
	$stages[]=array('id'=>$AssDef['Stage']['value'],'name'=>$AssDef['Stage']['value']);
	}
foreach($stages as $stage){
	$cohorts[]=array('course_id'=>$crid,'stage'=>$stage['id'],'year'=>$AssDef['Year']['value']);
	}

/**
 * Is this assessment for the current year or from a previous year?
 * If for a previous year then we need to find where the students are
 * now: (1) the next stage in the same course; (2) the first stage of
 * the following course if appropriate.
 *
 */


if($AssDef['Year']['value']!=$yearnow){
	$yeardiff=$yearnow-$AssDef['Year']['value'];
	$stagegones=array();
	$stages=list_course_stages($crid);
	$cridscourses=array();
	$courses=(array)list_courses();
	foreach($courses as $course){
		$cridscourses[$course['id']]=$course;
		}
	$nextcrid=$cridscourses[$crid]['nextcourse_id'];


	if($AssDef['Stage']['value']=='%'){
		$stagegones=(array)$stages;
		$sc=0;
		}
	else{
		$stagegones[]=array('id'=>$AssDef['Stage']['value'],'name'=>$AssDef['Stage']['value']);
		$c=0;
		foreach($stages as $stagegone){
			if($stagegone['id']==$AssDef['Stage']['value']){$sc=$c;}
			$c++;
			}
		unset($c);
		}
	foreach($stagegones as $stagegone){
		$stagediff=$sc+$yeardiff;
		if($stagediff<(sizeof($stages))){
			$cohorts[]=array('course_id'=>$crid,'stage'=>$stages[$stagediff]['id'],'year'=>$yearnow);
			}
		elseif($nextcrid!='' and $nextcrid!='1000'){
			/* This will identify the first stage of the next course. */
			$nextstages=list_course_stages($nextcrid);
			if(isset($nextstages[$yeardiff-1]['id'])){
				$cohorts[]=array('course_id'=>$nextcrid,'stage'=>$nextstages[$yeardiff-1]['id'],'year'=>$yearnow);
				//trigger_error($stagegone['id'].' '.$stagediff.' -------->'.$yeardiff.' : '.$nextstages[$yeardiff-1]['id'],E_USER_WARNING);
				}
			}
		$sc++;
		}
	}



/* Check user has permission to configure */
$perm=getCoursePerm($crid,$respons);
$neededperm='x';
if($perm["$neededperm"]=1 and $AssDef['MarkCount']['value']==0){

	/*find the appropriate markdef_name*/
	if($gena!='' and $gena!=' '){
		$grading_grades=$AssDef['GradingScheme']['grades'];
		$d_m=mysql_query("SELECT name FROM markdef WHERE
						grading_name='$gena' AND scoretype='grade' 
						AND (course_id='%' OR course_id='$crid');");
		if(mysql_num_rows($d_m)==0){
			/* If none exists then create keeping the name field unique for this crid.  */
			$markdef_name=$crid.' '.$gena;
			mysql_query("INSERT INTO markdef SET
						name='$markdef_name', scoretype='grade', grading_name='$gena',
						comment='$description', outoftotal='$total', author='ClaSS', 
						course_id='$crid', subject_id='$subject';");
			}
		else{
			$markdef_name=mysql_result($d_m,0);
			}
		$markdef_scoretype='grade';
		}
	else{
		$d_m=mysql_query("SELECT name FROM markdef WHERE
						scoretype='value' AND (course_id='%' OR course_id='$crid');");
		$markdef_name=mysql_result($d_m,0);
		$markdef_scoretype='value';
		}
	mysql_free_result($d_m);

	if($deadline!='0000-00-00'){$entrydate=$deadline;}
	else{$entrydate=date('Y').'-'.date('n').'-'.date('j');}

	/*make a list of subjects that will need distinct new marks*/
	$subjects=array();
	if($subject!='%'){$subjects[]['id']=$subject;}
	else{
		$subjects=list_course_subjects($crid);
		}
		
	/* Generate a mark for each bid/pid combination */
	foreach($subjects as $subject){
		$bid=$subject['id'];
		$components=(array)list_subject_components($bid,$crid,$compstatus);
		/* If there is no component for this subject or
		   components are not requested then $pid is blank*/
		if(sizeof($components)==0){$components[0]['id']='';}
		foreach($components as $component){
			$strands=(array)list_subject_components($component['id'],$crid,$strandstatus);
			if(sizeof($strands)==0){$strands[0]['id']=$component['id'];}
			foreach($strands as $strand){
				$pid=$strand['id'];

				if($bid==' ' or $bid=='G'){$bid='%';}
				$cidno=0;
				foreach($cohorts as $cohort){
					$cohid=update_cohort($cohort);
					$cridnow=$cohort['course_id'];
					$stagenow=$cohort['stage'];
					$bidnow='';
					$pidnow='';
					if($cridnow!=$crid){
						/* If carrying forward to another course need
						   to accomodate changes in currilucum
						   structure ie. hunt for equivalent subject/component/strand combination */
						$d_s=mysql_query("SELECT DISTINCT subject_id FROM component 
									WHERE course_id='$cridnow' AND id='$pid' AND subject_id='$bid' AND status!='U';");
						if(mysql_num_rows($d_s)>0){$bidnow=$bid;$pidnow=$pid;}
						else{
							if($pid!=''){
								$d_s=mysql_query("SELECT DISTINCT subject_id FROM component 
											WHERE course_id='$cridnow' AND id='' AND subject_id='$pid' AND status!='U';");
								if(mysql_num_rows($d_s)>0){$bidnow=$pid;$pidnow='';}
								else{
									if($pid!=''){
										$d_s=mysql_query("SELECT DISTINCT subject_id FROM component 
															WHERE course_id='$cridnow' AND id='$pid' AND status!='U';");
										if(mysql_num_rows($d_s)>0){$bidnow=mysql_result($d_s,0);$pidnow=$pid;}
										if($bidnow!=''){
											$d_s=mysql_query("SELECT DISTINCT subject_id FROM component 
														   WHERE course_id='$cridnow' AND id='$bidnow' AND status!='U';");
											if(mysql_num_rows($d_s)>0){$bidnow=mysql_result($d_s,0);$pidnow=$pid;}
											}
										}
									}
								}
							}
						}
					else{
						$bidnow=$bid;
						$pidnow=$pid;
						}

					/* Can only carry forward to next course if their is a correpsonding subject */
					if($bidnow!=''){
						//trigger_error($stage.' '.$crid.': '.$bid.' : '.$pid.' -------->'.$cridnow.' : '.$bidnow.' : '.$pidnow.' : '.$stagenow,E_USER_WARNING);
	
						mysql_query("INSERT INTO mark (entrydate, marktype, topic, comment, author,
						 def_name, assessment, component_id) VALUES ('$entrydate', 'score', '$description', 
						 '', 'ClaSS', '$markdef_name', '$asstype', '$pidnow');");
						$mid=mysql_insert_id();

						/* Make entry in eidmid for this new mark. */
						mysql_query("INSERT INTO eidmid (assessment_id,mark_id) VALUES ('$eid', '$mid');");

						$d_class=mysql_query("SELECT id FROM class 
												WHERE cohort_id='$cohid' AND subject_id LIKE '$bidnow';");
						/* Make entries in midcid for the new mark */
						while($d_cid=mysql_fetch_array($d_class,MYSQL_NUM)){
							$cid=$d_cid[0];
							mysql_query("INSERT INTO midcid (mark_id,class_id) VALUES ('$mid', '$cid');");
							$cidno++;
							}
						mysql_free_result($d_class);
						//trigger_error($stagenow.' '.$cridnow.': '.$bidnow.' : '.$pidnow. ' '.$cidno,E_USER_WARNING);
						}

					if($cidno>0){

						$d_eidsids=mysql_query("SELECT eidsid.student_id, eidsid.result, eidsid.value, comments.detail 
		   					FROM eidsid LEFT JOIN comments ON comments.eidsid_id=eidsid.id WHERE
							eidsid.subject_id LIKE '$bid' AND eidsid.component_id='$pid' AND eidsid.assessment_id='$eid';");

						if(mysql_num_rows($d_eidsids)==0){
							//								$AssDef['Year']['value'];
							$assyear=$yearnow-$yeardiff;
							$d_s=mysql_query("SELECT DISTINCT subject_id FROM component 
											WHERE course_id='$crid' AND id='$bid' AND status!='U' AND year='$assyear';");
							trigger_error($yearnow.'BID   '.$bid.'/'.$pid,E_USER_WARNING);
							if(mysql_num_rows($d_s)>0){
								$bidnow=mysql_result($d_s,0);
								$pidnow=$bid;
								trigger_error($yearnow.'NEWBID   '.$bid.'/'.$pid.' : '.$bidnow.'/'.$pidnow,E_USER_WARNING);
								$d_eidsids=mysql_query("SELECT eidsid.student_id, eidsid.result, eidsid.value, comments.detail 
											FROM eidsid LEFT JOIN comments ON comments.eidsid_id=eidsid.id WHERE
											eidsid.subject_id LIKE '$bidnow' AND eidsid.component_id='$pidnow' AND eidsid.assessment_id='$eid';");
								}
							}

{'fraction_non_alphanumeric': 0.2018348623853211, 'fraction_numerical': 0.004499503301583592, 'mean_word_length': 4.511755233494364, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3431127', 'n_tokens_mistral': 7211, 'n_tokens_neox': 5983, 'n_words': 1081}