|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Sysadmin tool to map a course to appear as a section of another |
| 6 | + * |
| 7 | + * Should a Submitty course have multiple entries, possibly for cross |
| 8 | + * registration or undergrad/grad versions, this tool will make a DB entry |
| 9 | + * to map one course to another. The course being mapped will appear in the |
| 10 | + * course it was mapped to as another section. |
| 11 | + * |
| 12 | + * @author Peter Bailie (systems programmer, RPI dept. of computer science) |
| 13 | + */ |
| 14 | + |
| 15 | +error_reporting(E_ERROR); |
| 16 | +ini_set('display_errors', 'stderr'); |
| 17 | +define ('DB_CONFIG_PATH', '/usr/local/submitty/config/database.json'); |
| 18 | + |
| 19 | +//This script should be run as root. |
| 20 | +if (posix_getuid() !== 0) { |
| 21 | + exit(sprintf("This script must be run as root.%s", PHP_EOL)); |
| 22 | +} |
| 23 | + |
| 24 | +//This script cannot be run as a webpage. |
| 25 | +if (PHP_SAPI !== 'cli') { |
| 26 | + exit(sprintf("This script must be run from the command line.%s", PHP_EOL)); |
| 27 | +} |
| 28 | + |
| 29 | +//Print usage if there are not five CLI arguments |
| 30 | +if ($argc !== 6) { |
| 31 | + exit(sprintf("Usage: %s semester_code course section mapped_course mapped_section%s", $argv[0], PHP_EOL)); |
| 32 | +} |
| 33 | + |
| 34 | +//Get DB connection config from Submitty |
| 35 | +$json_str = file_get_contents(DB_CONFIG_PATH); |
| 36 | +$db_config = json_decode($json_str, true); |
| 37 | + |
| 38 | +//Connect to master DB. |
| 39 | +$db_conn = pg_connect("dbname=submitty host={$db_config['database_host']} user={$db_config['database_user']} password={$db_config['database_password']}"); |
| 40 | +if (pg_connection_status($db_conn) !== PGSQL_CONNECTION_OK) { |
| 41 | + exit(sprintf("ERROR: Could not establish connection to Submitty Master DB%sCheck configuration at %s%s", PHP_EOL, DB_CONFIG_PATH, PHP_EOL)); |
| 42 | +} |
| 43 | + |
| 44 | +//Register pg_close() to occur on shutdown in case script quits on error. |
| 45 | +register_shutdown_function(function() use ($db_conn) { |
| 46 | + pg_close($db_conn); |
| 47 | +}); |
| 48 | + |
| 49 | +//$argv[1]: semester_code |
| 50 | +//$argv[2]: course |
| 51 | +//$argv[3]: section |
| 52 | +//$argv[4]: mapped_course |
| 53 | +//$argc[5]: mapped_section |
| 54 | +list($semester, $course, $section, $mapped_course, $mapped_section) = array_slice($argv, 1, 5); |
| 55 | + |
| 56 | +//INSERT new (mapped) registration section |
| 57 | +$query = <<<SQL |
| 58 | +INSERT INTO courses_registration_sections |
| 59 | + VALUES ($1, $2, $3) |
| 60 | +ON CONFLICT ON CONSTRAINT courses_registration_sections_pkey |
| 61 | + DO NOTHING |
| 62 | +SQL; |
| 63 | + |
| 64 | +$res = pg_query_params($db_conn, $query, array($semester, $mapped_course, $mapped_section)); |
| 65 | +if ($res === false) { |
| 66 | + exit(sprintf("DB error when INSERTing registration section %s%s%s%s", $mapped_section, PHP_EOL, pg_last_error($db_conn), PHP_EOL)); |
| 67 | +} |
| 68 | + |
| 69 | +//INSERT new mapped course |
| 70 | +$query = <<<SQL |
| 71 | +INSERT INTO mapped_courses |
| 72 | + VALUES ($1, $2, $3, $4, $5) |
| 73 | +ON CONFLICT ON CONSTRAINT mapped_courses_pkey |
| 74 | + DO UPDATE SET mapped_course=EXCLUDED.mapped_course, mapped_section=EXCLUDED.mapped_section |
| 75 | +SQL; |
| 76 | + |
| 77 | +$res = pg_query_params($db_conn, $query, array($semester, $course, $section, $mapped_course, $mapped_section)); |
| 78 | +if ($res === false) { |
| 79 | + exit(sprintf("DB error when INSERTing mapped course %s%s%s%s", $mapped_course, PHP_EOL, pg_last_error($db_conn), PHP_EOL)); |
| 80 | +} |
| 81 | + |
| 82 | +//Complete |
| 83 | +printf("Mapped %s section %s to %s section %s.%s", $course, $section, $mapped_course, $mapped_section, PHP_EOL); |
| 84 | +exit(0); |
| 85 | + |
| 86 | +?> |
0 commit comments