Skip to content

[Feature:System] Local CSV File #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions student_auto_feed/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,23 @@
//Allows "\r" EOL encoding. This is rare but exists (e.g. Excel for Macintosh).
ini_set('auto_detect_line_endings', true);

/* DATA SOURCING --------------------------------------------------------------
* The Student Autofeed provides helper scripts to retrieve the CSV file for
* processing. Shell script ssaf.sh is used to invoke one of the helper
* scripts and then execute the autofeed. Current options are csv_local.php,
* imap_remote.php, and json_remote.php
* ------------------------------------------------------------------------- */

//Local CSV
//This is used by csv_local.php to reference where the CSV file is provided.
define('LOCAL_SOURCE_CSV', '/path/to/csv');

//Remote IMAP
//This is used by imap_remote.php to login and retrieve a student enrollment
//datasheet, should datasheets be provided via an IMAP email box. This also
//works with exchange servers with IMAP enabled.
//works with exchange servers (local network and cloud) with IMAP and basic
//authentication enabled.
//Note that this does NOT work should exchange require OAuth2.
//IMAP_FOLDER is the folder where the data sheets can be found.
//IMAP_OPTIONS: q.v. "Optional flags for names" at https://www.php.net/manual/en/function.imap-open.php
//IMAP_FROM is for validation. Make sure it matches the identity of who sends the data sheets
Expand Down Expand Up @@ -163,7 +176,8 @@
define('JSON_REMOTE_PASSWORD', 'json_password'); //DO NOT USE IN PRODUCTION
define('JSON_REMOTE_PATH', '/path/to/files/');

// Add/Drop Reporting
/* ADD/DROP REPORTING ------------------------------------------------------ */

// Where to email reports. Set to null to disable sending email.
// Sendmail (or equivalent) needs to be installed on the server and configured
// in php.ini. Reports are sent "unauthenticated".
Expand Down
91 changes: 91 additions & 0 deletions student_auto_feed/csv_local.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env php
<?php
require __DIR__ . "/config.php";

new csv_local();
exit(0);

/**
* Validate and copy CSV data file via filesystem.
*
* This data source option has the CSV file provided to any filesystem the
* autofeed's server can access (local or mounted). LOCAL_SOURCE_CSV must
* be defined in config.php, referencing the location of the CSV file upload.
*
* @author Peter Bailie, Rensselaer Polytechnic Institute
*/
class csv_local {
/** @static @property string */
private static $source_file = LOCAL_SOURCE_CSV;

/** @static @property string */
private static $dest_file = CSV_FILE;

/** @static @property string */
private static $err = "";

public function __construct() {
// Main process
switch(false) {
case $this->validate_csv():
case $this->copy_csv():
// If we wind up here, something went wrong in the main process.
fprintf(STDERR, "%s", self::$err);
exit(1);
}
}

/**
* Validate CSV file before copy.
*
* Check's for the file's existence and tries to check that the file was
* provided/refreshed on the same day as the autofeed was run. The day
* check is to help prevent the auto feed from blindly running the same CSV
* multiple days in a row and alert the sysadmin that an expected file
* refresh did not happen. $this->err is set with an error message when
* validation fails.
*
* @return boolean true when CSV is validated, false otherwise.
*/
private function validate_csv() {
clearstatcache();

if (!file_exists(self::$source_file)) {
self::$err = sprintf("CSV upload missing: %s\n", self::$source_file);
return false;
}

$file_modified = filemtime(self::$source_file);
$today = time();
// There are 86400 seconds in a day.
if (intdiv($today, 86400) !== intdiv($file_modified, 86400)) {
$today = date("m-d-Y", $today);
$file_modified = date("m-d-Y", $file_modified);
$hash = md5(file_get_contents(self::$source_file));
self::$err = sprintf("CSV upload modified time mismatch.\nToday: %s\nUploaded File: %s\nUploaded File Hash: %s\n", $today, $file_modified, $hash);
return false;
}

return true;
}

/**
* Copy CSV file.
*
* $this->err is set with an error message when file copy fails.
*
* @return boolean true when copy is successful, false otherwise.
*/
private function copy_csv() {
if (file_exists(self::$dest_file)) {
unlink(self::$dest_file);
}

if (!copy(self::$source_file, self::$dest_file)) {
self::$err = sprintf("Failed to copy file.\nSource: %s\nDest: %s\n", self::$source_file, self::$dest_file);
return false;
}

return true;
}
}
41 changes: 41 additions & 0 deletions student_auto_feed/ssaf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

# Use this bash script to run a data sourcing script before
# submitty_student_auto_feed.php. This is intended to be used with cron.
#
# Author: Peter Bailie, Rensselaer Polytechnic Institute

display_usage() {
cat << EOM
usage: ssaf.sh (data_source) (term) [DB_auth]

data_source: csv_local|imap_remote|json_remote
Which data sourcing script to run first: csv_local.php,
imap_remote.php, or json_remote.php (required)
term: Term code to pass to submitty_student_auto_feed.php (required)
DB_auth: DB auth string for submitty_student_auto_feed.php [optional]
EOM
exit 1
}

if [ $# -ne 2 ] && [ $# -ne 3 ]; then
display_usage
fi

CWD=$(dirname "$0")
if [ "$1" = "csv_local" ] || [ "$1" = "imap_remote" ] || [ "$1" = "json_remote" ]; then
SOURCE="${CWD}/${1}.php"
else
display_usage
fi

if $SOURCE; then
if [ "$3" != "" ]; then
DASH_A="-a$3"
fi

DASH_T="-t$2"
"$CWD"/submitty_student_auto_feed.php "$DASH_T" "$DASH_A"
else
echo "${1}.php exited $?. Auto feed not run."
fi
39 changes: 0 additions & 39 deletions student_auto_feed/ssaf_remote.sh

This file was deleted.