Skip to content

Instantly share code, notes, and snippets.

@dwaligora
Created September 30, 2013 23:27
Show Gist options
  • Save dwaligora/6771801 to your computer and use it in GitHub Desktop.
Save dwaligora/6771801 to your computer and use it in GitHub Desktop.
test
public function getCourses(Application $app, User $user, SchoolSemester $semester, $startDate, $endDate, $include)
{
$startDate = Carbon::createFromTimestamp($startDate);
$startDate = $startDate->startOfDay();
$endDate = Carbon::createFromTimestamp($endDate);
$endDate = $endDate->endOfDay();
$includeHomeworks = in_array('homeworks', $include);
$startOfSemesterProjection = Carbon::createFromTimestamp(
KambalaDateHelper::getLastWeekProjectionOfDate($semester->getStartAt(), $startDate, 14));
$add = $startOfSemesterProjection->getTimestamp() - $semester->getStartAt()->getTimestamp();
$courses = $user->isStudent() ?
RepositoryFactory::getCourse($app)->getCoursesByStudentId($user->getId())
: RepositoryFactory::getCourse($app)->getCoursesByTeacherId($user->getId());
$out = array();
$rounds = ceil(($endDate->getTimestamp() - $startDate->getTimestamp()) / (14 * 24 * 60 * 60));
$courseMapper = new CourseArrayMapper();
for ($i = 0; $i < $rounds; $i++) {
foreach ($courses as $course) {
$course['startAt'] = Carbon::createFromTimestamp($course['startAt']->getTimestamp());
$course['endAt'] = Carbon::createFromTimestamp($course['endAt']->getTimestamp());
if ($includeHomeworks) {
$course['homeworks'] = 0;
}
if (($course['day'] > 5 && $rounds <= 1) || $course['day'] < $startDate->dayOfWeek) {
continue;
}
$date = CoursePeriodTimeMapper::guessDay($course['day'] - 1);
$out[$date->format('Y/m/d')][] = $courseMapper->map($course, true);
}
$add += 14 * 24 * 60 * 60;
}
if ($includeHomeworks) {
$homeworks = RepositoryFactory::getHomework($app)
->getStudentHomeworksByDueDateRange($user->getId(), $startDate->getTimestamp(), $endDate->getTimestamp());
foreach ($homeworks as $homework) {
$f = $homework['scheduledAt']->format('Y/m/d');
if (isset($out[$f])) {
$count = count($out[$f]);
for ($i = 0; $i < $count; $i++) {
if ($out[$f][$i]['id'] == $homework['course']['id']) {
$out[$f][$i]['homeworks'] += 1;
$i = $count;
continue;
}
}
}
}
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment