From c2bebda0c5a3c4d1b7411037de1989e0c7a1d96f Mon Sep 17 00:00:00 2001 From: Adam Franco Date: Tue, 3 Dec 2024 15:40:14 -0500 Subject: [PATCH] #47: Port admin antirequisites screen and add tests. --- .../views/scripts/admin/antirequisites.phtml | 132 ------------------ src/Controller/AdminAntirequisites.php | 119 +++++++++------- templates/admin/antirequisites.html.twig | 126 +++++++++++++++++ templates/admin/index.html.twig | 2 +- tests/Controller/AdminAntirequisitesTest.php | 89 ++++++++++++ 5 files changed, 288 insertions(+), 180 deletions(-) delete mode 100644 application/views/scripts/admin/antirequisites.phtml create mode 100644 templates/admin/antirequisites.html.twig create mode 100644 tests/Controller/AdminAntirequisitesTest.php diff --git a/application/views/scripts/admin/antirequisites.phtml b/application/views/scripts/admin/antirequisites.phtml deleted file mode 100644 index 23e2cd63..00000000 --- a/application/views/scripts/admin/antirequisites.phtml +++ /dev/null @@ -1,132 +0,0 @@ -

Manage Anti-Requisites

-

« Back to Administration

- -

Anti-requisites are course-equivalency entries that indicate that a course can't be taken if another already has been. This is in contrast to most usage of equivalency which indicates that courses are the same thing. Anti-requisites listed below will be filtered out of cross-list entries for courses.

-

Please Note: Any changes made here will affect views for authenticated users immediately, but will not affect catalog archives or anonymous views until the next nightly sync.

- - - - - - - - - - - - - - - - - - -antirequisites as $antirequisite) { - print "\n\t"; - print "\n\t\t"; - print "\n\t\t"; - print "\n\t\t"; - print "\n\t\t"; - print "\n\t\t"; - print "\n\t\t"; - print "\n\t\t"; - - print "\n\t\t"; - print "\n\t"; -} -?> - -
Subject CodeCourse NumberSubject Code EquivalentCourse Number EquivalentDate AddedAdded ByCommentsActions
".$antirequisite['subj_code']."".$antirequisite['crse_numb']."".$antirequisite['subj_code_eqiv']."".$antirequisite['crse_numb_eqiv']."".$antirequisite['added_date']."".$antirequisite['added_by']."".$antirequisite['comments'].""; - print "\n\t\t\t
"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "'; - print "\n\t\t\t
"; - print "\n\t\t
- - -

Add a new anti-requisite

- -

Search for a subject code and course number to find related equivalencies.

-
- - - -
- -searchResults)): ?> -
- - - - - - - - - - - - - - - searchResults); - foreach ($this->searchResults as $eqiv) { - print "\n\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t\t"; - print "\n\t\t"; - } - ?> - -
Add?Subject CodeCourse NumberSubject Code EquivalentCourse Number EquivalentDate AddedAdded ByComments
"; - if ($eqiv->antirequisite) { - print ""; - } else { - print ""; - } - print "\n\t\t\t"; - print $eqiv->SCREQIV_SUBJ_CODE; - print "\n\t\t\t"; - print $eqiv->SCREQIV_CRSE_NUMB; - print "\n\t\t\t"; - print $eqiv->SCREQIV_SUBJ_CODE_EQIV; - print "\n\t\t\t"; - print $eqiv->SCREQIV_CRSE_NUMB_EQIV; - print "\n\t\t\t"; - if ($eqiv->antirequisite) { - print $eqiv->added_date; - } - print "\n\t\t\t"; - if ($eqiv->antirequisite) { - print $eqiv->added_by; - } - print "\n\t\t\t"; - if ($eqiv->antirequisite) { - print $this->escape($eqiv->comments); - } else { - print ""; - } - print "\n\t\t\t
- - - - -
- diff --git a/src/Controller/AdminAntirequisites.php b/src/Controller/AdminAntirequisites.php index 64e9f9c9..e6d426ba 100644 --- a/src/Controller/AdminAntirequisites.php +++ b/src/Controller/AdminAntirequisites.php @@ -4,7 +4,9 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Security\Core\Exception\AccessDeniedException; class AdminAntirequisites extends AbstractController { @@ -16,52 +18,21 @@ public function __construct( /** * Manage antirequisites. */ - #[Route('/admin/antirequisites', name: 'admin_antirequisites')] - public function antirequisitesAction() + #[Route('/admin/antirequisites', name: 'list_antirequisites', methods: ['GET'])] + public function antirequisitesAction(Request $request) { - $db = Zend_Registry::get('db'); - - // Delete any requested item. - if ($this->_getParam('delete')) { - // Verify our CSRF key - if (!$this->_getParam('csrf_key') == $this->_helper->csrfKey()) { - throw new PermissionDeniedException('Invalid CSRF Key. Please log in again.'); - } - - // Verify that this is a valid term. - $deleteStmt = $db->prepare('DELETE FROM antirequisites WHERE subj_code = ? AND crse_numb = ? AND subj_code_eqiv = ? AND crse_numb_eqiv = ?'); - $deleteStmt->execute([ - $this->_getParam('subj_code'), - $this->_getParam('crse_numb'), - $this->_getParam('subj_code_eqiv'), - $this->_getParam('crse_numb_eqiv'), - ]); - } - - // Add any chosen items. - if ($this->_getParam('add')) { - // Verify our CSRF key - if (!$this->_getParam('csrf_key') == $this->_helper->csrfKey()) { - throw new PermissionDeniedException('Invalid CSRF Key. Please log in again.'); - } - - // Verify that this is a valid term. - $insertStmt = $db->prepare('INSERT INTO antirequisites (subj_code, crse_numb, subj_code_eqiv, crse_numb_eqiv, added_by, comments) VALUES (?, ?, ?, ?, ?, ?)'); - foreach ($this->_getParam('equivalents_to_add') as $toAdd) { - $params = explode('/', $toAdd); - $params[] = $this->view->getUserDisplayName(); - $params[] = $this->_getParam($toAdd.'-comments'); - $insertStmt->execute($params); - } - } + $data = [ + 'searchResults' => [], + ]; + $db = $this->entityManager->getConnection(); // Select our already-created antirequisites $data['antirequisites'] = $db->query('SELECT * FROM antirequisites ORDER BY subj_code, crse_numb, subj_code_eqiv, crse_numb_eqiv')->fetchAllAssociative(); // Supply search results. - $this->view->search_subj_code = $this->_getParam('search_subj_code'); - $this->view->search_crse_numb = $this->_getParam('search_crse_numb'); - if ($this->_getParam('search_subj_code') && $this->_getParam('search_crse_numb')) { + $data['search_subj_code'] = $request->get('search_subj_code'); + $data['search_crse_numb'] = $request->get('search_crse_numb'); + if ($request->get('search_subj_code') && $request->get('search_crse_numb')) { $searchStmt = $db->prepare( 'SELECT *, @@ -79,13 +50,67 @@ public function antirequisitesAction() ORDER BY SCREQIV_SUBJ_CODE, SCREQIV_CRSE_NUMB, SCREQIV_SUBJ_CODE_EQIV, SCREQIV_CRSE_NUMB_EQIV '); - $searchStmt->execute([ - $this->_getParam('search_subj_code'), - $this->_getParam('search_crse_numb'), - $this->_getParam('search_subj_code'), - $this->_getParam('search_crse_numb'), - ]); - $this->view->searchResults = $searchStmt->fetchAll(PDO::FETCH_OBJ); + $searchStmt->bindValue(1, $request->get('search_subj_code')); + $searchStmt->bindValue(2, $request->get('search_crse_numb')); + $searchStmt->bindValue(3, $request->get('search_subj_code')); + $searchStmt->bindValue(4, $request->get('search_crse_numb')); + $result = $searchStmt->executeQuery(); + $data['searchResults'] = $result->fetchAllAssociative(); + } + + return $this->render('admin/antirequisites.html.twig', $data); + } + + /** + * Manage antirequisites. + */ + #[Route('/admin/antirequisites/delete', name: 'delete_antirequisite', methods: ['POST'])] + public function deleteAntirequisiteAction(Request $request) + { + $db = $this->entityManager->getConnection(); + + // Verify our CSRF key + if (!$this->isCsrfTokenValid('admin-antirequisites-delete', $request->get('csrf_key'))) { + throw new AccessDeniedException('Invalid CSRF key.'); + } + + // Delete any requested item. + $deleteStmt = $db->prepare('DELETE FROM antirequisites WHERE subj_code = ? AND crse_numb = ? AND subj_code_eqiv = ? AND crse_numb_eqiv = ?'); + $deleteStmt->bindValue(1, $request->get('subj_code')); + $deleteStmt->bindValue(2, $request->get('crse_numb')); + $deleteStmt->bindValue(3, $request->get('subj_code_eqiv')); + $deleteStmt->bindValue(4, $request->get('crse_numb_eqiv')); + $deleteStmt->executeQuery(); + + return $this->redirect($this->generateUrl('list_antirequisites')); + } + + /** + * Manage antirequisites. + */ + #[Route('/admin/antirequisites', name: 'add_antirequisites', methods: ['POST'])] + public function addAntirequisitesAction(Request $request) + { + $db = $this->entityManager->getConnection(); + + // Verify our CSRF key + if (!$this->isCsrfTokenValid('admin-antirequisites-add', $request->get('csrf_key'))) { + throw new AccessDeniedException('Invalid CSRF key.'); } + + // Add any chosen items. + $insertStmt = $db->prepare('INSERT INTO antirequisites (subj_code, crse_numb, subj_code_eqiv, crse_numb_eqiv, added_by, comments) VALUES (?, ?, ?, ?, ?, ?)'); + foreach ($request->get('equivalents_to_add') as $toAdd) { + $params = explode('/', $toAdd); + $insertStmt->bindValue(1, $params[0]); + $insertStmt->bindValue(2, $params[1]); + $insertStmt->bindValue(3, $params[2]); + $insertStmt->bindValue(4, $params[3]); + $insertStmt->bindValue(5, $this->getUser()->getName()); + $insertStmt->bindValue(6, $request->get($toAdd.'-comments')); + $insertStmt->executeQuery(); + } + + return $this->redirect($this->generateUrl('list_antirequisites')); } } diff --git a/templates/admin/antirequisites.html.twig b/templates/admin/antirequisites.html.twig new file mode 100644 index 00000000..cbae78c1 --- /dev/null +++ b/templates/admin/antirequisites.html.twig @@ -0,0 +1,126 @@ +{% extends 'base.html.twig' %} + +{% block body %} +

Manage Anti-Requisites

+

« Back to Administration

+ +

Anti-requisites are course-equivalency entries that indicate that a course can't be taken if another already has been. This is in contrast to most usage of equivalency which indicates that courses are the same thing. Anti-requisites listed below will be filtered out of cross-list entries for courses.

+

Please Note: Any changes made here will affect views for authenticated users immediately, but will not affect catalog archives or anonymous views until the next nightly sync.

+ + + + + + + + + + + + + + + + + + +{% for antirequisite in antirequisites %} + + + + + + + + + + + +{% endfor %} + +
Subject CodeCourse NumberSubject Code EquivalentCourse Number EquivalentDate AddedAdded ByCommentsActions
{{ antirequisite.subj_code }}{{ antirequisite.crse_numb }}{{ antirequisite.subj_code_eqiv }}{{ antirequisite.crse_numb_eqiv }}{{ antirequisite.added_date }}{{ antirequisite.added_by }}{{ antirequisite.comments }} +
+ + + + + + + + + +
+
+ + +

Add a new anti-requisite

+ +

Search for a subject code and course number to find related equivalencies.

+
+ + + +
+ +{% if searchResults is not empty %} +
+ + + + + + + + + + + + + + +{% for eqiv in searchResults %} + + + + + + + + + + +{% endfor %} + +
Add?Subject CodeCourse NumberSubject Code EquivalentCourse Number EquivalentDate AddedAdded ByComments
+{% if eqiv.antirequisite %} + +{% else %} + +{% endif %} + + {{ eqiv.SCREQIV_SUBJ_CODE }} + + {{ eqiv.SCREQIV_CRSE_NUMB }} + + {{ eqiv.SCREQIV_SUBJ_CODE_EQIV }} + + {{ eqiv.SCREQIV_CRSE_NUMB_EQIV }} + + {{ eqiv.antirequisite ? eqiv.added_date }} + + {{ eqiv.antirequisite ? eqiv.added_by }} + +{% if eqiv.antirequisite %} + {{ eqiv.comments }} +{% else %} + +{% endif %} +
+ + + + +
+{% endif %} +{% endblock %} diff --git a/templates/admin/index.html.twig b/templates/admin/index.html.twig index 4a237d1b..b1f32946 100755 --- a/templates/admin/index.html.twig +++ b/templates/admin/index.html.twig @@ -4,7 +4,7 @@

Course Catalog Administration