Skip to content

Commit

Permalink
#47: Migrate courses by id XML action.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfranco committed Oct 9, 2024
1 parent c4da8cf commit bfc3eb2
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 140 deletions.
127 changes: 88 additions & 39 deletions src/Controller/Courses.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

namespace App\Controller;

use App\Helper\RecentCourses\Department as DepartmentRecentCourses;
use App\Helper\RecentCourses\RecentCoursesInterface;
use App\Service\Osid\IdMap;
use App\Service\Osid\Runtime;
use App\Service\Osid\TermHelper;
use App\Service\Osid\TopicHelper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand Down Expand Up @@ -118,7 +121,9 @@ public function view($course, $term = NULL)
public function viewxml($course, $term = NULL)
{
$data = [];
$data['courses'] = [$this->getCourseDataByIdString($course, $term)];
$courseData = $this->getCourseDataByIdString($course, $term);
$courseData['offerings'] = $this->getCourseOfferingsData($courseData['course'], $courseData['term']);
$data['courses'] = [$courseData];

$data['title'] = $data['courses'][0]['course']->getDisplayName();
$data['feedLink'] = $this->generateUrl('view_course', ['course' => $course], UrlGeneratorInterface::ABSOLUTE_URL);
Expand Down Expand Up @@ -148,7 +153,11 @@ protected function getCourseDataByIdString($idString, $termIdString = NULL)
}

protected function getCourseData(\osid_course_Course $course, \osid_course_Term|NULL $term = NULL) {
$data = [];
$data['course'] = $course;
// Optional add-on data that can be populated by other methods.
$data['offerings'] = [];
$data['terms'] = [];
// Load the topics into our view
$data = array_merge($data, $this->getTopics($course->getTopics()));

Expand Down Expand Up @@ -178,8 +187,11 @@ protected function getCourseData(\osid_course_Course $course, \osid_course_Term|
// Term
$data['term'] = $term;

// offerings.
$data['offerings'] = [];
return $data;
}

protected function getCourseOfferingsData(\osid_course_Course $course, \osid_course_Term|NULL $term = NULL) {
$data = [];
$offeringLookupSession = $this->osidRuntime->getCourseManager()->getCourseOfferingLookupSession();
$offeringLookupSession->useFederatedCourseCatalogView();
if ($term) {
Expand Down Expand Up @@ -219,7 +231,7 @@ protected function getCourseData(\osid_course_Course $course, \osid_course_Term|
}
}

$data['offerings'][] = $offering;
$data[] = $offering;
}

return $data;
Expand Down Expand Up @@ -407,75 +419,86 @@ public function topicxmlAction()
$topicLookup->useFederatedCourseCatalogView();
$topic = $topicLookup->getTopic($topicId);

$recentCourses = new Helper_RecentCourses_Department($courses);
if ($this->_getParam('cutoff')) {
$recentCourses->setRecentInterval(new DateInterval($this->_getParam('cutoff')));
$recentCourses = new DepartmentRecentCourses($this->osidIdMap, $courses);
if ($request->get('cutoff')) {
$recentCourses->setRecentInterval(new \DateInterval($request->get('cutoff')));
}
$this->outputCourseFeed($recentCourses, htmlentities('Courses in '.$topic->getDisplayName()), $searchUrl);
}

/**
* Search for courses.
*
* @return void
*
* @since 6/15/09
*/
public function byidxmlAction()
#[Route('/courses/byidxml/{catalog}', name: 'list_courses_by_ids')]
public function byidxmlAction(Request $request, $catalog)
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();

if (!$this->_getParam('catalog')) {
if (!$catalog) {
header('HTTP/1.1 400 Bad Request');
echo 'A catalog must be specified.';
exit;
}
try {
$catalogId = $this->osidIdMap->fromString($this->_getParam('catalog'));
$catalogId = $this->osidIdMap->fromString($catalog);
$lookupSession = $this->osidRuntime->getCourseManager()->getCourseLookupSessionForCatalog($catalogId);
$this->termLookupSession = $this->osidRuntime->getCourseManager()->getTermLookupSessionForCatalog($catalogId);
} catch (osid_InvalidArgumentException $e) {
} catch (\osid_InvalidArgumentException $e) {
header('HTTP/1.1 400 Bad Request');
echo 'The catalog id specified was not of the correct format.';
exit;
} catch (osid_NotFoundException $e) {
} catch (\osid_NotFoundException $e) {
header('HTTP/1.1 404 Not Found');
echo 'The catalog id specified was not found.';
exit;
}

if (!$this->_getParam('id')) {
$ids = $request->get('id', []);
if (!$ids) {
header('HTTP/1.1 400 Bad Request');
echo "'id[]' must be specified.";
exit;
}

$courseIds = [];
if (is_array($this->_getParam('id'))) {
foreach ($this->_getParam('id') as $idString) {
if (is_array($ids)) {
foreach ($ids as $idString) {
$courseIds[] = $this->osidIdMap->fromString($idString);
}
} else {
$courseIds[] = $this->osidIdMap->fromString($this->_getParam('id'));
$courseIds[] = $this->osidIdMap->fromString($ids);
}

// Use Comparative view to include any found courses, ignoring missing ids.
$lookupSession->useComparativeCourseView();

$courses = $lookupSession->getCoursesByIds(new phpkit_id_ArrayIdList($courseIds));
$courses = $lookupSession->getCoursesByIds(new \phpkit_id_ArrayIdList($courseIds));

$recentCourses = new Helper_RecentCourses_Department($courses);
if ($this->_getParam('cutoff')) {
$recentCourses->setRecentInterval(new DateInterval($this->_getParam('cutoff')));
$recentCourses = new DepartmentRecentCourses($this->osidIdMap, $courses);
if ($request->get('cutoff')) {
$recentCourses->setRecentInterval(new \DateInterval($request->get('cutoff')));
}

// Set the next and previous terms.
$currentTermId = $this->osidTermHelper->getCurrentTermId($this->termLookupSession->getCourseCatalogId());
$currentTerm = $this->termLookupSession->getTerm($currentTermId);

$data = [
'courses' => [],
];
foreach ($recentCourses->getPrimaryCourses() as $course) {
$courseData = $this->getCourseData($course);
$courseData['terms'] = $this->getRecentTermData($currentTerm, $recentCourses, $course);
$courseData['offerings'] = [];
$data['courses'][] = $courseData;
}
$data['title'] = 'Courses by Id';
$data['feedLink'] = $this->generateUrl(
'list_courses_by_ids',
[
'catalog' => $catalog,
'id' => $request->get('id'),
'cutoff' => $request->get('cutoff'),
],
UrlGeneratorInterface::ABSOLUTE_URL
);

$searchUrl = $this->_helper->pathAsAbsoluteUrl($this->_helper->url('byidxml', 'courses', null, [
'catalog' => $this->_getParam('catalog'),
'id' => $this->_getParam('id'),
'cuttoff' => $this->_getParam('cutoff'),
]));
$this->outputCourseFeed($recentCourses, 'Courses by Id', $searchUrl);
$response = new Response($this->renderView('courses/list.xml.twig', $data));
$response->headers->set('Content-Type', 'text/xml; charset=utf-8');
return $response;
}

/**
Expand Down Expand Up @@ -733,10 +756,36 @@ protected function outputCourseFeed(Helper_RecentCourses_Interface $recentCourse
exit;
}

protected function getRecentTermData(\osid_course_Term $currentTerm, RecentCoursesInterface $recentCourses, \osid_course_Course $course) {
$now = $this->DateTime_getTimestamp(new \DateTime());
$currentTermId = $currentTerm->getId();
$currentEndTime = $this->DateTime_getTimestamp($currentTerm->getEndTime());
$recentTerms = $recentCourses->getTermsForCourse($course);
$data = [];
if (count($recentTerms)) {
foreach ($recentTerms as $term) {
if ($term->getId()->isEqual($currentTermId)) {
$type = 'current';
} elseif ($currentEndTime < $this->DateTime_getTimestamp($term->getEndTime())) {
$type = 'future';
} elseif ($now > $this->DateTime_getTimestamp($term->getStartTime()) && $now < $this->DateTime_getTimestamp($term->getEndTime())) {
$type = 'current';
} else {
$type = 'past';
}
$data[] = [
'term' => $term,
'type' => $type,
];
}
}
return $data;
}

public function DateTime_getTimestamp($dt)
{
$dtz_original = $dt->getTimezone();
$dtz_utc = new DateTimeZone('UTC');
$dtz_utc = new \DateTimeZone('UTC');
$dt->setTimezone($dtz_utc);
$year = (int) $dt->format('Y');
$month = (int) $dt->format('n');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php
/**
* @since 11/16/09
*
* @copyright Copyright &copy; 2009, Middlebury College
* @copyright Copyright &copy; 2024, Middlebury College
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
*/

namespace App\Helper\RecentCourses;

use App\Service\Osid\IdMap;

/**
* A helper for accessing recent courses in a list.
*
* @since 11/16/09
*
* @copyright Copyright &copy; 2009, Middlebury College
* @copyright Copyright &copy; 2024, Middlebury College
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
*/
class Helper_RecentCourses_All extends Helper_RecentCourses_Abstract
class All extends RecentCoursesAbstract
{
private $termsCache;

Expand All @@ -25,10 +25,10 @@ class Helper_RecentCourses_All extends Helper_RecentCourses_Abstract
*
* @since 11/16/09
*/
public function __construct(osid_course_CourseList $courses)
public function __construct(IdMap $osidIdMap, \osid_course_CourseList $courses)
{
$this->termsCache = [];
parent::__construct($courses);
parent::__construct($osidIdMap, $courses);
}

/**
Expand All @@ -38,12 +38,12 @@ public function __construct(osid_course_CourseList $courses)
*
* @since 11/16/09
*/
protected function fetchCourseTerms(osid_course_Course $course)
protected function fetchCourseTerms(\osid_course_Course $course)
{
$cacheKey = Zend_Controller_Action_HelperBroker::getStaticHelper('OsidId')->toString($course->getId());
$cacheKey = $this->osidIdMap->toString($course->getId());

if (!isset($this->termsCache[$cacheKey])) {
$termsType = new phpkit_type_URNInetType('urn:inet:middlebury.edu:record:terms');
$termsType = new \phpkit_type_URNInetType('urn:inet:middlebury.edu:record:terms');
$allTerms = [];
if ($course->hasRecordType($termsType)) {
$termsRecord = $course->getCourseRecord($termsType);
Expand All @@ -52,7 +52,7 @@ protected function fetchCourseTerms(osid_course_Course $course)
while ($terms->hasNext()) {
$allTerms[] = $terms->getNextTerm();
}
} catch (osid_OperationFailedException $e) {
} catch (\osid_OperationFailedException $e) {
}
}
$this->termsCache[$cacheKey] = $allTerms;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<?php
/**
* @since 11/16/09
*
* @copyright Copyright &copy; 2009, Middlebury College
* @copyright Copyright &copy; 2024, Middlebury College
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
*/

namespace App\Helper\RecentCourses;

/**
* A helper for accessing recent courses in a list.
*
* @since 11/16/09
*
* @copyright Copyright &copy; 2009, Middlebury College
* @copyright Copyright &copy; 2024, Middlebury College
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
*/
class Helper_RecentCourses_Department extends Helper_RecentCourses_All
class Department extends All
{
/**
* Group alternate courses.
Expand All @@ -23,11 +21,11 @@ class Helper_RecentCourses_Department extends Helper_RecentCourses_All
*
* @since 11/13/09
*/
protected function groupAlternates(osid_course_CourseList $courses)
protected function groupAlternates(\osid_course_CourseList $courses)
{
while ($courses->hasNext()) {
$course = $courses->getNextCourse();
$courseIdString = Zend_Controller_Action_HelperBroker::getStaticHelper('OsidId')->toString($course->getId());
$courseIdString = $this->osidIdMap->toString($course->getId());

$groupId = $courseIdString;

Expand All @@ -50,7 +48,7 @@ protected function groupAlternates(osid_course_CourseList $courses)
$term = $this->getMostRecentTermForCourse($course);
$dates[] = $this->DateTime_getTimestamp($term->getEndTime());
$names[] = $course->getDisplayName();
} catch (osid_NotFoundException $e) {
} catch (\osid_NotFoundException $e) {
unset($group[$key]);
}
}
Expand Down
Loading

0 comments on commit bfc3eb2

Please sign in to comment.