Skip to content

Commit

Permalink
#47: Add single-section XML view.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfranco committed Oct 3, 2024
1 parent df1de2c commit 4cb5999
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 237 deletions.
220 changes: 0 additions & 220 deletions application/views/scripts/offerings/searchxml.phtml

This file was deleted.

4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ services:
tags:
- { name: 'twig.extension' }

App\Twig\DateRangeFormatter:
tags:
- { name: 'twig.extension' }

# Service aliases.
osid.runtime:
class: App\Service\Osid\Runtime
Expand Down
39 changes: 22 additions & 17 deletions src/Controller/Offerings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* A controller for working with courses.
Expand Down Expand Up @@ -697,13 +697,22 @@ protected function viewBase($idString)

$data['title'] = $offering->getDisplayName();

$data['previousTerm'] = NULL;
$data['term'] = $offering->getTerm();
$data['nextTerm'] = NULL;
$data['terms'] = NULL;

$data['location'] = NULL;
if ($offering->hasLocation()) {
$data['location'] = $offering->getLocation();
}

$data['weekly_schedule'] = NULL;
$weeklyScheduleType = new \phpkit_type_URNInetType('urn:inet:middlebury.edu:record:weekly_schedule');
if ($offering->hasRecordType($weeklyScheduleType)) {
$data['weekly_schedule'] = $offering->getCourseOfferingRecord($weeklyScheduleType);
}

// Instructors
$data['instructors'] = NULL;
$instructorsType = new \phpkit_type_URNInetType('urn:inet:middlebury.edu:record:instructors');
Expand All @@ -717,9 +726,11 @@ protected function viewBase($idString)
}

// Alternates.
$data['is_primary'] = TRUE;
$data['alternates'] = NULL;
if ($offering->hasRecordType($this->getAlternateType())) {
$record = $offering->getCourseOfferingRecord($this->getAlternateType());
$data['is_primary'] = $record->isPrimary();
if ($record->hasAlternates()) {
$data['alternates'] = [];
$alternates = $record->getAlternates();
Expand Down Expand Up @@ -758,25 +769,19 @@ protected function viewBase($idString)
return $data;
}

/**
* Answer search results as an xml feed.
*
* @return void
*
* @since 10/21/09
*/
public function viewxmlAction()
#[Route('/offerings/viewxml/{id}', name: 'view_offering_xml')]
public function viewxmlAction($id)
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setRender('searchxml');
$this->getResponse()->setHeader('Content-Type', 'text/xml');
$data = $this->viewBase($id);

$this->viewBase();
$data['feedLink'] = $this->generateUrl('view_offering', ['id' => $id], UrlGeneratorInterface::ABSOLUTE_URL);

$this->view->feedTitle = $this->view->title;
$this->view->feedLink = $this->_helper->pathAsAbsoluteUrl('/offerings/view/'.$catalog.'/offering/'.$this->_getParam('offering'));
$this->view->sections = new \phpkit_course_ArrayCourseOfferingList([$this->view->offering]);
$this->postDispatch();

$data['sections'] = [$data['offering']];

$response = new Response($this->renderView('offerings/search.xml.twig', $data));
$response->headers->set('Content-Type', 'text/xml; charset=utf-8');
return $response;
}

protected function getAlternateType() {
Expand Down
40 changes: 40 additions & 0 deletions src/Service/Osid/IdMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,44 @@ public function toString(\osid_id_Id $id)
return \phpkit_id_URNInetId::getInetURNString($id);
}
}

/**
* Get and OSID Type object from a string.
*
* @param string $typeString
*
* @return osid_type_Type
*
* @since 4/21/09
*/
public function typeFromString($typeString)
{
try {
return new \phpkit_type_URNInetType($typeString);
} catch (\osid_InvalidArgumentException $e) {
if ($this->getIdAuthorityToShorten()) {
return new \phpkit_type_Type($this->getIdAuthorityToShorten(), 'urn', $typeString);
} else {
throw $e;
}
}
}

/**
* Answer a string representation of an OSID type object.
*
* @return string
*
* @since 4/21/09
*/
public function typeToString(\osid_type_Type $type)
{
if ($this->getIdAuthorityToShorten()
&& 'urn' == strtolower($type->getIdentifierNamespace())
&& strtolower($type->getAuthority()) == $this->getIdAuthorityToShorten()) {
return $type->getIdentifier();
} else {
return \phpkit_type_URNInetType::getInetURNString($type);
}
}
}
24 changes: 24 additions & 0 deletions src/Twig/DateRangeFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

// src/Twig/AppExtension.php
namespace App\Twig;

use App\Service\Osid\IdMap;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class DateRangeFormatter extends AbstractExtension
{

public function getFunctions(): array
{
return [
new TwigFunction('dateRangeToWeeks', [$this, 'dateRangeToWeeks']),
];
}

public function dateRangeToWeeks(\Date|\DateTime $start, \Date|\DateTime $end): int
{
return ceil(abs($end->format('U') - $start->format('U'))/60/60/24/7);
}
}
6 changes: 6 additions & 0 deletions src/Twig/OsidIdExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ public function getFunctions(): array
{
return [
new TwigFunction('osidIdToString', [$this, 'osidIdToString']),
new TwigFunction('osidTypeToString', [$this, 'osidTypeToString']),
];
}

public function osidIdToString(\osid_id_Id $id): string
{
return $this->idMap->toString($id);
}

public function osidTypeToString(\osid_type_Type $type): string
{
return $this->idMap->typeToString($type);
}
}
Loading

0 comments on commit 4cb5999

Please sign in to comment.