retrieve(1);
global $timedate;
if (!isset($timedate)) $timedate=new TimeDate();
$focus = new Course();
$event_type_info=$app_list_strings['event_types']['course'];
$schedule_obj = new Schedule();
$_REQUEST['event_type'] = 'course';
$_REQUEST['save_with_conflict'] = 'on';
$_REQUEST['top_schedule_options'] = 'off';
$_REQUEST['save_type'] = 'new';
$import_array = array();
$student_array = array();
$schedule_import_query = " SELECT * FROM smsevent_import WHERE schedule_type='monthly' ";
// 0 (single instance) or 1 (multiple instance) 2 is wrong data
$schedule_import_query .= " AND course_id='course171' "; //
//$schedule_import_query .= " LIMIT 1 ";
$result = $current_user->db->query($schedule_import_query, true);
while($row = $current_user->db->fetchByAssoc($result)) {
$import_array[$row['course_id']][$row['id']] = $row;
$import_array[$row['course_id']]['students'] = array();
$student_query = " SELECT contact_id, start_date, end_date, description FROM event_contacts WHERE event_id='{$row['course_id']}'";
$student_result = $current_user->db->query($student_query, true);
while($student_row = $current_user->db->fetchByAssoc($student_result)) {
$import_array[$row['course_id']]['students'][$student_row['contact_id']] = $student_row;
}
}
foreach($import_array as $ckey=>$cvalue) {
unset($focus); $focus = new Course(); $focus->retrieve($ckey);
unset($schedule_obj); $schedule_obj = new Schedule(); // $schedule_obj->retrieve($ckey);
$schedule_obj->json = new JSON(JSON_LOOSE_TYPE);
$index = 0;
foreach($cvalue as $key=>$value) {
if ($key=='students') continue;
$_REQUEST['record'] = $ckey;
$_REQUEST['schedule_type'] = $value['schedule_type'];
list($start_hour, $start_min, $dummy1, $dummy2, $end_hour, $end_min) = split('[:~]', $value['time_slot']);
$_REQUEST['event_start_year'] = substr($value['start_date'], 0, 4);
$_REQUEST['event_start_month'] = substr($value['start_date'], 5, 2);
$_REQUEST['event_start_day'] = substr($value['start_date'], 8, 2);
$_REQUEST['event_start_hour'] = $start_hour;
$_REQUEST['event_start_minute'] = $start_min;
$_REQUEST['event_end_year'] = substr($value['end_date'], 0, 4);
$_REQUEST['event_end_month'] = substr($value['end_date'], 5, 2);
$_REQUEST['event_end_day'] = substr($value['end_date'], 8, 2);
$_REQUEST['event_end_hour'] = $end_hour;
$_REQUEST['event_end_minute'] = $end_min;
$schedule_obj->GetSubmittedData();
switch ($value['week_day']) {
case '月曜日':
$weekday = 1; break;
case '火曜日':
$weekday = 2; break;
case '水曜日':
$weekday = 3; break;
case '木曜日':
$weekday = 4; break;
case '金曜日':
$weekday = 5; break;
case '土曜日':
$weekday = 6; break;
case '日曜日':
$weekday = 7; break;
}
$schedule_obj->recurrence_info_arr['recurrences_arr'][$index] = array (
'all_day' => NULL,
'day' => $weekday, // Friday
'from_time_hour' => $start_hour,
'from_time_minute' => $start_min,
'to_time_hour' => $end_hour,
'to_time_minute' => $end_min,
);
echo "
"; echo "course id: {$value['course_id']} "; echo "teacher id: {$value['teacher_id']} "; echo "subject id: {$value['subject']} ";
echo "room: {$value['room_id']} "; echo "start: {$value['start_date']} "; echo "end: {$value['end_date']} "; echo "time slot: {$value['time_slot']} ";
if ($value['schedule_type']=='weekly') { echo "week day: {$value['week_day']}"; }
else {
echo "monthly schedules: ";
for($i=1;$i<25;$i++) {
if ($value['monthly'.$i]!='0000-00-00') {
echo "{$value['monthly'.$i]} ";
}
else {
break;
}
}
}
$index++;
}
// first create the general course schedule (event_instance) without people and resources
if ($_REQUEST['schedule_type']=='weekly') {
$schedule_obj->new_with_id=true;
$schedule_obj->people_resources_info_arr['people'] = $people_resources_info_arr['people'];
$schedule_obj->people_resources_info_arr['resources'] = $people_resources_info_arr['resources'];
$schedule_obj->recurrence_info = $schedule_obj->json->encode($schedule_obj->recurrence_info_arr);
$schedule_obj->targets = $people_resources_info_arr['people'] + $people_resources_info_arr['resources'];
$schedule_obj->reschedule_event_mode =VarsCls::TimeslotsNewSchedule();
$schedule_obj->save();
$schedule_obj->event_name=$focus->name;
$schedule_obj->HandleRecurrenceAndTargets();
//$schedule_obj->HandleDeletes();
$schedule_obj->CheckConflicts();
$schedule_obj->SaveSchedule();
}
$index = 0;
$args_to_schedule = array(
'event_contact_status' => NULL,
'start_date' => $value['start_date'].' 00:00:00',
'end_date' => $value['end_date'].' 00:00:00',
'save_with_conflict' => true,
'existingtimes_add_target_only' => true,
'event_type' => 'course',
'editing_from_subpanel' => true, //???
);
// ANYTHING TO DESIGNATE ONE TIME SLOT AMONG MANY??!!!
$index=0;
$teacher_index=array();
$resource_index=array();
foreach($cvalue as $key=>$value) {
switch ($value['week_day']) {
case '月曜日':
$weekday = 1; break;
case '火曜日':
$weekday = 2; break;
case '水曜日':
$weekday = 3; break;
case '木曜日':
$weekday = 4; break;
case '金曜日':
$weekday = 5; break;
case '土曜日':
$weekday = 6; break;
case '日曜日':
$weekday = 7; break;
}
if ($key=='students') continue;
list($start_hour, $start_min, $dummy1, $dummy2, $end_hour, $end_min) = split('[:~]', $value['time_slot']);
$args_to_schedule_item = "{$weekday}:{$start_hour}:{$start_min}:{$end_hour}:{$end_min}";
if (!empty($value['teacher_id'])) {
$teacher_index[$value['teacher_id']]['recurrence_days'][$index] = $args_to_schedule_item;
}
if (!empty($value['subject'])) {
$resource_index[$value['subject']]['recurrence_days'][$index] = $args_to_schedule_item;
}
if (!empty($value['room_id'])) {
$resource_index[$value['room_id']]['recurrence_days'][$index] = $args_to_schedule_item;
}
$index++;
}
foreach($teacher_index as $tkey=>$tvalue) {
unset($args_to_schedule['recurrence_days']);
$args_to_schedule['recurrence_days'] = $tvalue['recurrence_days'];
$person_obj=new AppendTargetToSchedule('people', $tkey, $focus->id, $args_to_schedule);
//$person_obj->schedule_data_import_with_no_deletes = true;
$person_obj->Process();
unset($person_obj);
}
foreach($resource_index as $rkey=>$rvalue) {
unset($args_to_schedule['recurrence_days']);
$args_to_schedule['recurrence_days'] = $rvalue['recurrence_days'];
$person_obj=new AppendTargetToSchedule('resources', $rkey, $focus->id, $args_to_schedule);
//$person_obj->schedule_data_import_with_no_deletes = true;
$person_obj->Process();
unset($person_obj);
}
echo "
";
echo "students: ";
$args_to_schedule = array();
foreach($cvalue['students'] as $skey=>$svalue) {
echo "{$svalue['contact_id']} - {$svalue['description']} ";
unset($args_to_schedule);
$args_to_schedule = array(
'event_contact_status' => 0,
'start_date' => $svalue['start_date'],
'end_date' => $svalue['end_date'],
'save_with_conflict' => true,
'existingtimes_add_target_only' => true,
'event_type' => 'course',
);
$person_obj=new AppendTargetToSchedule('people', $svalue['contact_id'], $focus->id, $args_to_schedule);
$person_obj->Process();
unset($person_obj);
}
echo "
";
}
sugar_cleanup();
?>