<commit_before><?php
/**								   message.php
 *
 */

$action='message_action.php';
$choice='message.php';

/* Normally handled by the host page but this has to work differently depending on the sequence. */
if(isset($_POST['groupsearch']) and $_POST['groupsearch']=='yes'){
	$nolist='yes';
	include('group_search_action.php');
	}
elseif(isset($_POST['messageto'])  or isset($_POST['messageop'])){
	$sids=(array)$_SESSION['infosids'];
	}
elseif(isset($_POST['sids'])){
	$sids=(array)$_POST['sids'];
	}
else{
	$sids=array();
	}
/**/

if(isset($_POST['messageop'])){$messageop=$_POST['messageop'];}else{$messageop='email';}
if(isset($_POST['messageto'])){$messageto=$_POST['messageto'];}else{$messageto='family';}
if(isset($_POST['share0'])){$share=$_POST['share0'];}else{$share='yes';}
$_SESSION[$book.'recipients']=array();
$_SESSION[$book.'tutors']=array();

/* Locked down for teachers to only email and only to students. */
if($_SESSION['role']=='teacher'){
	$messageop='email';
	$messageto='student';
	}

include('scripts/sub_action.php');

if(sizeof($sids)==0){
	if(isset($_POST['groupsearch']) and $_POST['groupsearch']=='yes'){
		$result[]='Please choose a group of students.';
		include('scripts/results.php');
		}
	$action='group_search.php';
	include('scripts/redirect.php');
	exit;
	}


$from_user=get_user($tid);
$recipients=array();
$email_blank_sids=array();
$email_blank_gids=array();


if($messageto=='student'){

	foreach($sids as $sid){
			$Student=fetchStudent_short($sid);		
			$recipient=array();
			if($messageop=='email'){
				$field=fetchStudent_singlefield($sid,'EmailAddress');
				$email=$field['EmailAddress']['value'];
				if($email!='' and $email!=' '){
					$recipient['name']=$Student['DisplayFullName']['value'];
					$recipient['explanation']='<p>'.$CFG->schoolname.'</p>';
					$recipient['email']=$email;
					$recipients[]=$recipient;
					$sid_recipient_no++;
					}
				else{
					$email_blank_sids[]=$Student['id_db'];
					}
				}
			elseif($messageop=='sms'){
				$field=fetchStudent_singlefield($sid,'MobilePhone');
				$mobile=$field['MobilePhone']['value'];
				if($mobile!='' and $mobile!=' '){
					$recipient['name']=$Student['DisplayFullName']['value'];
					$recipient['explanation']='';
					$recipient['mobile']=$mobile;
					$recipients[]=$recipient;
					$sid_recipient_no++;
					}
				else{
					$email_blank_sids[]=$Student['id_db'];
					}
				}
			}

		}
	elseif($messageto=='contacts' or $messageto=='family'){
		
		$tutors=array();
		foreach($sids as $sid){
			$Student=fetchStudent_short($sid);
			foreach($Student['RegistrationTutor'] as $Tutor){
				if($Tutor['email']!=''){
					$tutors[$Tutor['email']]=array('email'=>$Tutor['email'],
												   'explanation'=>$CFG->schoolname.': message sent to parents of '.$Student['RegistrationGroup']['value']);
					}
				}
			$Contacts=(array)fetchContacts($sid);
			$sid_recipient_no=0;
			foreach($Contacts as $Contact){
				$recipient=array();
				if($messageop=='email'){
					if($Contact['ReceivesMailing']['value']=='1'){
						/* Only contacts who have an email address and are 
						 * flagged to receive all mailings 
						 */
						$email=strtolower($Contact['EmailAddress']['value']);
						if(!empty($email)){
							$recipient['name']=$Contact['DisplayFullName']['value'];
							//$recipient['explanation']=$CFG->schoolname;
							$recipient['explanation']='';
							if(($messageto=='contacts' or $messageto=='family')){
								$recipient['explanation'].=get_string(displayEnum($Contact['Relationship']['value'],'relationship'),'infobook'). ' of '. $Student['DisplayFullName']['value'];
								}

							$recipient['email']=$email;
							$recipient['sid']=$sid;
							$recipient['Student']=$Student;
							$recipient['Siblings']=(array)fetchDependents($Contact['id_db']);

							if($messageto=='family'){
								$recipients[$email]=$recipient;
								}
							else{
								$recipients[]=$recipient;
								}
							$sid_recipient_no++;
							}
						elseif($Contact['EmailAddress']['value']==''){
							$email_blank_gids[]=$Contact['id_db'];
							}
						}
					}
				elseif($messageop=='sms'){
					if($Contact['ReceivesMailing']['value']=='1'){
						$mobile='';
						$Phones=(array)$Contact['Phones'];
						foreach($Phones as $Phone){
							if($Phone['PhoneType']['value']=='M'){
								$mobile=$Phone['PhoneNo']['value'];
								}
							}
						if($mobile!='' and $mobile!=' '){
							$recipient['name']=$Contact['DisplayFullName']['value'];
							$recipient['explanation']='';
							$recipient['mobile']=$mobile;
							$recipient['email']=strtolower($Contact['EmailAddress']['value']);
							if($messageto=='family'){$recipients[$mobile]=$recipient;}
							else{$recipients[]=$recipient;}
							$sid_recipient_no++;
							}
						elseif($mobile==''){
							$email_blank_gids[]=$Contact['id_db'];
							}
						}
					}
				}
			/* Collect a list of sids who won't have any contacts receving this message */
			if($sid_recipient_no==0){
				$email_blank_sids[]=$sid;
				}
			}

		}


$_SESSION[$book.'recipients']=$recipients;

three_buttonmenu();
?>

  <div id="heading">
	<label><?php print_string('message',$book);?></label>
<?php print_string($messageop,$book);?>
  </div>

  <div id="viewcontent" class="content">

		<div class="divgroup left">
	   	<table class="listmenu">
		  <tr><commit_msg>include yeargroup resposibles on bcc<commit_after><?php
/**								   message.php
 *
 */

$action='message_action.php';
$choice='message.php';

/* Normally handled by the host page but this has to work differently depending on the sequence. */
if(isset($_POST['groupsearch']) and $_POST['groupsearch']=='yes'){
	$nolist='yes';
	include('group_search_action.php');
	}
elseif(isset($_POST['messageto'])  or isset($_POST['messageop'])){
	$sids=(array)$_SESSION['infosids'];
	}
elseif(isset($_POST['sids'])){
	$sids=(array)$_POST['sids'];
	}
else{
	$sids=array();
	}
/**/

if(isset($_POST['messageop'])){$messageop=$_POST['messageop'];}else{$messageop='email';}
if(isset($_POST['messageto'])){$messageto=$_POST['messageto'];}else{$messageto='family';}
if(isset($_POST['share0'])){$share=$_POST['share0'];}else{$share='yes';}
$_SESSION[$book.'recipients']=array();
$_SESSION[$book.'tutors']=array();

/* Locked down for teachers to only email and only to students. */
if($_SESSION['role']=='teacher'){
	$messageop='email';
	$messageto='student';
	}

include('scripts/sub_action.php');

if(sizeof($sids)==0){
	if(isset($_POST['groupsearch']) and $_POST['groupsearch']=='yes'){
		$result[]='Please choose a group of students.';
		include('scripts/results.php');
		}
	$action='group_search.php';
	include('scripts/redirect.php');
	exit;
	}


$from_user=get_user($tid);
$recipients=array();
$email_blank_sids=array();
$email_blank_gids=array();


if($messageto=='student'){

	foreach($sids as $sid){
			$Student=fetchStudent_short($sid);		
			$recipient=array();
			if($messageop=='email'){
				$field=fetchStudent_singlefield($sid,'EmailAddress');
				$email=$field['EmailAddress']['value'];
				if($email!='' and $email!=' '){
					$recipient['name']=$Student['DisplayFullName']['value'];
					$recipient['explanation']='<p>'.$CFG->schoolname.'</p>';
					$recipient['email']=$email;
					$recipients[]=$recipient;
					$sid_recipient_no++;
					}
				else{
					$email_blank_sids[]=$Student['id_db'];
					}
				}
			elseif($messageop=='sms'){
				$field=fetchStudent_singlefield($sid,'MobilePhone');
				$mobile=$field['MobilePhone']['value'];
				if($mobile!='' and $mobile!=' '){
					$recipient['name']=$Student['DisplayFullName']['value'];
					$recipient['explanation']='';
					$recipient['mobile']=$mobile;
					$recipients[]=$recipient;
					$sid_recipient_no++;
					}
				else{
					$email_blank_sids[]=$Student['id_db'];
					}
				}
			}

		}
	elseif($messageto=='contacts' or $messageto=='family'){
		
		$tutors=array();
		$yids=array();
		foreach($sids as $sid){
			$Student=fetchStudent_short($sid);
			foreach($Student['RegistrationTutor'] as $Tutor){
				if($Tutor['email']!=''){
					$tutors[$Tutor['email']]=array('email'=>$Tutor['email'],
												   'explanation'=>$CFG->schoolname.': message sent to parents of '.$Student['RegistrationGroup']['value']);
					}
				$yids[$Student['YearGroup']['value']]=$Student['YearGroup']['value'];
				}
			$Contacts=(array)fetchContacts($sid);
			$sid_recipient_no=0;
			foreach($Contacts as $Contact){
				$recipient=array();
				if($messageop=='email'){
					if($Contact['ReceivesMailing']['value']=='1'){
						/* Only contacts who have an email address and are 
						 * flagged to receive all mailings 
						 */
						$email=strtolower($Contact['EmailAddress']['value']);
						if(!empty($email)){
							$recipient['name']=$Contact['DisplayFullName']['value'];
							//$recipient['explanation']=$CFG->schoolname;
							$recipient['explanation']='';
							if(($messageto=='contacts' or $messageto=='family')){
								$recipient['explanation'].=get_string(displayEnum($Contact['Relationship']['value'],'relationship'),'infobook'). ' of '. $Student['DisplayFullName']['value'];
								}

							$recipient['email']=$email;
							$recipient['sid']=$sid;
							$recipient['Student']=$Student;
							$recipient['Siblings']=(array)fetchDependents($Contact['id_db']);

							if($messageto=='family'){
								$recipients[$email]=$recipient;
								}
							else{
								$recipients[]=$recipient;
								}
							$sid_recipient_no++;
							}
						elseif($Contact['EmailAddress']['value']==''){
							$email_blank_gids[]=$Contact['id_db'];
							}
						}
					}
				elseif($messageop=='sms'){
					if($Contact['ReceivesMailing']['value']=='1'){
						$mobile='';
						$Phones=(array)$Contact['Phones'];
						foreach($Phones as $Phone){
							if($Phone['PhoneType']['value']=='M'){
								$mobile=$Phone['PhoneNo']['value'];
								}
							}
						if($mobile!='' and $mobile!=' '){
							$recipient['name']=$Contact['DisplayFullName']['value'];
							$recipient['explanation']='';
							$recipient['mobile']=$mobile;
							$recipient['email']=strtolower($Contact['EmailAddress']['value']);
							if($messageto=='family'){$recipients[$mobile]=$recipient;}
							else{$recipients[]=$recipient;}
							$sid_recipient_no++;
							}
						elseif($mobile==''){
							$email_blank_gids[]=$Contact['id_db'];
							}
						}
					}
				}
			/* Collect a list of sids who won't have any contacts receving this message */
			if($sid_recipient_no==0){
				$email_blank_sids[]=$sid;
				}
			}

		foreach($yids as $yid){
			$yeartutors=(array)list_pastoral_users($yid,array('r'=>1,'w'=>1,'x'=>1));
			foreach($yeartutors as $tutor){
				if($tutor['email']!=''){
					$tutors[$tutor['email']]=array('email'=>$tutor['email'],
												   'explanation'=>$CFG->schoolname.': message sent to parents of Year '.get_yeargroupname($yid));
					}
				}
			}

		}


$_SESSION[$book.'recipients']=$recipients;

three_buttonmenu();
?>

  <div id="heading">
	<label><?php print_string('message',$book);?></label>
<?php print_string($messageop,$book);?>
  </div>

  <div id="viewcontent" class="content">

		<div class="divgroup left">
	   	<table class="listmenu">
		  <tr>
{'fraction_non_alphanumeric': 0.22540617746830924, 'fraction_numerical': 0.001517586145331191, 'mean_word_length': 4.53781512605042, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 45, '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': '6233110', 'n_tokens_mistral': 4910, 'n_tokens_neox': 3729, 'n_words': 464}