Example #1
0
use Tsugi\Util\LTI;
use Tsugi\Util\Net;
$sanity = array('re.findall' => 'You should use re.findall() to extract the numbers');
// Compute the stuff for the output
$code = $USER->id + $LINK->id + $CONTEXT->id;
$sample_url = dataUrl('regex_sum_42.txt');
$actual_url = dataUrl('regex_sum_' . $code . '.txt');
$sample_data = Net::doGet($sample_url);
$sample_count = strlen($sample_data);
$response = Net::getLastHttpResponse();
if ($response != 200) {
    die("Response={$response} url={$sample_url}");
}
$actual_data = Net::doGet($actual_url);
$actual_count = strlen($actual_data);
$response = Net::getLastHttpResponse();
if ($response != 200) {
    die("Response={$response} url={$actual_url}");
}
$actual_matches = array();
preg_match_all('/[0-9]+/', $actual_data, $actual_matches);
$actual_count = count($actual_matches[0]);
$actual_sum = 0;
foreach ($actual_matches[0] as $match) {
    $actual_sum = $actual_sum + $match;
}
$sample_matches = array();
preg_match_all('/[0-9]+/', $sample_data, $sample_matches);
$sample_count = count($sample_matches[0]);
$sample_sum = 0;
foreach ($sample_matches[0] as $match) {
Example #2
0
function getJsonOrDie($sample_url)
{
    $sample_data = Net::doGet($sample_url);
    $sample_count = strlen($sample_data);
    $response = Net::getLastHttpResponse();
    $sample_json = json_decode($sample_data);
    if ($response != 200 || $sample_json == null) {
        error_log("DIE: Sample response={$response} url={$sample_url} json_error=" . json_last_error_msg());
        die("Sample response={$response} url={$sample_url} json_error=" . json_last_error_msg());
    }
    return $sample_json;
}