PHP code example of as-cornell / as_courses

1. Go to this page and download the library: Download as-cornell/as_courses library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

as-cornell / as_courses example snippets


// Adjust semester generation range
$config['as_courses.settings']['semester_start_year_offset'] = -1;  // Current year - 1
$config['as_courses.settings']['semester_end_year_offset'] = 5;     // Current year + 5

// API timeout (seconds)
$config['as_courses.settings']['api_timeout'] = 10;

// Cache duration (seconds)
$config['as_courses.settings']['cache_duration'] = 3600;  // 1 hour

// Get courses by subject
$courses_api = \Drupal::service('as_courses.api');
$courses = $courses_api->getCoursesBySubject('SP26', 'PSYCH,ECON');

// Get courses by instructor
$courses = $courses_api->getCoursesByInstructor('FA25', 'abc123');

// Get semester options for forms
$semester_gen = \Drupal::service('as_courses.semester_generator');
$options = $semester_gen->getSemesterOptions();
// Returns: ['FA25' => 'Fall 2025', 'WI26' => 'Winter 2026', ...]

// Get current semester
$current = $semester_gen->getCurrentSemester();
// Returns: 'SP26' (based on current date)

// Format semester code
$name = $semester_gen->formatSemesterName('SP26');
// Returns: 'Spring 2026'

// Get custom range
$semesters = $semester_gen->getSemestersBetween(2025, 2027);

// Old (deprecated)
$courses = as_courses_get_courses_json($semester, $subjects);

// New (v2.0+)
$courses = \Drupal::service('as_courses.api')->getCoursesBySubject($semester, $subjects);
bash
# Via Composer (if published to Packagist)
composer es/custom/as_courses
# 2. Enable the module
drush en as_courses -y
bash
# From Drupal root
vendor/bin/phpunit web/modules/custom/as_courses/tests/src/Unit

# Specific test
vendor/bin/phpunit web/modules/custom/as_courses/tests/src/Unit/CoursesApiServiceTest.php