Skip to content

Commit

Permalink
#47: Add single-course XML view.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfranco committed Oct 8, 2024
1 parent 6a202bd commit d1c7415
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 175 deletions.
93 changes: 0 additions & 93 deletions application/views/scripts/courses/viewxml.phtml

This file was deleted.

64 changes: 0 additions & 64 deletions application/views/scripts/offeringsxml.phtml

This file was deleted.

42 changes: 26 additions & 16 deletions src/Controller/Courses.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +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 @@ -113,6 +114,20 @@ public function view($course, $term = NULL)

}

#[Route('/courses/viewxml/{course}/{term}', name: 'view_course_xml')]
public function viewxml($course, $term = NULL)
{
$data = [];
$data['courses'] = [$this->getCourseDataByIdString($course, $term)];

$data['title'] = $data['courses'][0]['course']->getDisplayName();
$data['feedLink'] = $this->generateUrl('view_course', ['course' => $course], UrlGeneratorInterface::ABSOLUTE_URL);

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

protected function getCourseDataByIdString($idString, $termIdString = NULL)
{
$id = $this->osidIdMap->fromString($idString);
Expand Down Expand Up @@ -182,16 +197,24 @@ protected function getCourseData(\osid_course_Course $course, \osid_course_Term|
$instructorsRecord = $offering->getCourseOfferingRecord($this->instructorsType);
$instructors = $instructorsRecord->getInstructors();
$offering->instructors = [];
$offering->instructorNames = [];
if ($instructors->hasNext()) {
$instNames = array();
while ($instructors->hasNext()) {
$instructor = $instructors->getNextResource();
$instructorData = [
'resource' => $instructor,
'givename' => NULL,
'surname' => NULL,
];
if ($instructor->hasRecordType($this->namesType)) {
$namesRecord = $instructor->getResourceRecord($this->namesType);
$offering->instructors[] = $namesRecord->getSurname();
$instructorData['givename'] = $namesRecord->getGivenName();
$instructorData['surname'] = $namesRecord->getSurname();
$offering->instructorNames[] = $namesRecord->getSurname();
} else {
$offering->instructors[] = $instructor->getDisplayName();
$offering->instructorNames[] = $instructor->getDisplayName();
}
$offering->instructors[] = $instructorData;
}
}
}
Expand All @@ -202,19 +225,6 @@ protected function getCourseData(\osid_course_Course $course, \osid_course_Term|
return $data;
}

/**
* Get an XML view of a course.
*
* @return void
*/
public function viewxmlAction()
{
$this->_helper->layout->disableLayout();
$this->getResponse()->setHeader('Content-Type', 'text.xml');

$this->viewAction();
}

/**
* Search for courses.
*
Expand Down
15 changes: 15 additions & 0 deletions templates/courses/list.xml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:catalog="http://www.middlebury.edu/course_catalog">
<channel>
<title>{{ title }}</title>
<link>{{ feedLink }}</link>
<description></description>
<lastBuildDate>{{ 'now' | date('r') }}</lastBuildDate>
<generator>Course Catalog</generator>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
{% for course_data in courses %}
{{ include('partials/course.xml.twig', course_data, with_context = FALSE) }}
{% endfor %}

</channel>
</rss>
39 changes: 39 additions & 0 deletions templates/partials/course.xml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<item>
<title>{{ course.displayname }}</title>
<link>{{ url('view_course', {'course': osidIdToString(course.id)}) }}</link>
<description><![CDATA[{{ course.description|raw }}]]></description>
<catalog:title>{{ course.title }}</catalog:title>
<catalog:id>{{ osidIdToString(course.id) }}</catalog:id>
{%~ if is_primary is not null %}
<catalog:enrollable>{{ is_primary ? 'true':'false' }}</catalog:enrollable>
{% endif ~%}
{%- if alternates is not empty %}
{%- for alternate in alternates %}
<catalog:alternate id="{{ osidIdToString(alternate.id) }}" href="{{ url('view_course', {course: osidIdToString(alternate.id)}) }}" is_primary="{{ alternate.is_primary ? 'true':'false' }}">{{ alternate.displayname }}</catalog:alternate>
{% endfor ~%}
{% endif %}
<catalog:genustype type="{{ osidTypeToString(course.genustype) }}" id="{{ osidTypeToString(course.genustype) }}">{{ course.genustype.displayname }}</catalog:genustype>
{% for topic in subjectTopics %}
<catalog:topic type="{{ osidTypeToString(topic.genustype) }}" id="{{ osidIdToString(topic.id) }}" href='{{ url('view_topic', {topic: osidIdToString(topic.id)}) }}'>{{ topic.displayname }}</catalog:topic>
{% endfor %}
{%- for topic in departmentTopics %}
<catalog:topic type="{{ osidTypeToString(topic.genustype) }}" id="{{ osidIdToString(topic.id) }}" href='{{ url('view_topic', {topic: osidIdToString(topic.id)}) }}'>{{ topic.displayname }}</catalog:topic>
{% endfor %}
{%- for topic in divisionTopics %}
<catalog:topic type="{{ osidTypeToString(topic.genustype) }}" id="{{ osidIdToString(topic.id) }}" href='{{ url('view_topic', {topic: osidIdToString(topic.id)}) }}'>{{ topic.displayname }}</catalog:topic>
{% endfor %}
{%- for topic in requirementTopics %}
<catalog:topic type="{{ osidTypeToString(topic.genustype) }}" id="{{ osidIdToString(topic.id) }}" href='{{ url('view_topic', {topic: osidIdToString(topic.id)}) }}'>{{ topic.displayname }}</catalog:topic>
{% endfor %}
{%- if blockTopics is not empty %}
{%- for topic in blockTopics %}
<catalog:topic type="{{ osidTypeToString(topic.genustype) }}" id="{{ osidIdToString(topic.id) }}" href='{{ url('view_topic', {topic: osidIdToString(topic.id)}) }}'>{{ topic.displayname }}</catalog:topic>
{% endfor %}
{% endif %}
{%- if levelTopics is not empty %}
{%- for topic in levelTopics %}
<catalog:topic type="{{ osidTypeToString(topic.genustype) }}" id="{{ osidIdToString(topic.id) }}" href='{{ url('view_topic', {topic: osidIdToString(topic.id)}) }}'>{{ topic.displayname }}</catalog:topic>
{% endfor ~%}
{% endif %}
{{ include('partials/offering_list.xml.twig', {offerings: offerings}, with_context = false) }}
</item>
29 changes: 29 additions & 0 deletions templates/partials/offering_list.xml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<catalog:offering_list>
{% set currentTerm = null %}
{% for offering in offerings %}
{% if currentTerm is null or currentTerm.id != offering.termid %}
{% if currentTerm is not null %}
</catalog:offering_term>
{% endif %}
{% set currentTerm = offering.term %}
<catalog:offering_term id="{{ osidIdToString(currentTerm.id) }}" name="{{ currentTerm.displayname }}">
{% endif %}
<catalog:offering id="{{ osidIdToString(offering.id) }}" href="{{ url('view_offering', {id: osidIdToString(offering.id)}) }}">
<catalog:display_name>{{ offering.displayname }}</catalog:display_name>
<catalog:title>{{ offering.title }}</catalog:title>
<catalog:description><![CDATA[{{ offering.description|raw }}]]></catalog:description>
<catalog:type id="{{ osidTypeToString(offering.genustype) }}">{{ offering.genustype.displayname }}</catalog:type>
<catalog:term id="{{ osidIdToString(offering.term.id) }}">{{ offering.term.displayname }}</catalog:term>
<catalog:location id="{{ osidIdToString(offering.locationid) }}">{{ offering.locationInfo }}</catalog:location>
<catalog:schedule>{{ offering.scheduleinfo }}</catalog:schedule>
{% for instructor in offering.instructors %}
<catalog:instructor id="{{ osidIdToString(instructor.resource.id) }}">
<catalog:given_name>{{ instructor.givename }}</catalog:given_name>
<catalog:surname>{{ instructor.surname }}</catalog:surname>
<catalog:display_name>{{ instructor.resource.displayname }}</catalog:display_name>
</catalog:instructor>
{% endfor %}
</catalog:offering>
{% endfor %}
</catalog:offering_term>
</catalog:offering_list>
4 changes: 2 additions & 2 deletions templates/partials/offerings.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
{% endif %}
<a class='offering_link' href="{{ url('view_offering', {id: osidIdToString(offering.id)}) }}">{{offering.displayname}}</a>
<span class='type'>{{ offering.genustype.displayname }}</span>
{% if offering.instructors %}
<span class='instructor'>({{ offering.instructors|join(', ') }})</span>
{% if offering.instructorNames %}
<span class='instructor'>({{ offering.instructorNames|join(', ') }})</span>
{% endif %}
</div>
{% endfor %}

0 comments on commit d1c7415

Please sign in to comment.