Example #1
0
 /**
  * @desc Saves the webwork question code and default seed setting into question_webwork. Will recreate all corresponding derived questions.
  * @param $question object The question object holding new data.
  * @return boolean to indicate success of failure.
  */
 function save_question_options($question)
 {
     //determing update or insert
     $oldrecord = get_record('question_webwork', 'question', $question->id);
     if (!$oldrecord) {
         $isupdate = false;
     } else {
         $isupdate = true;
     }
     //set new variables for DB entry
     $record = new stdClass();
     $record->question = $question->id;
     $record->codecheck = $question->codecheck;
     $record->code = base64_encode(stripslashes($question->code));
     $record->seed = $question->seed;
     $record->trials = $question->trials;
     $results = webwork_qtype::_derivations();
     if (count($results) > 0) {
         $record->grading = $results[0]['grading'];
     }
     //insert or update question in DB
     if ($isupdate) {
         //update
         $record->id = $oldrecord->id;
         $errresult = update_record("question_webwork", $record);
         if (!$errresult) {
             $errresult->error = "Could not update question_webwork record! (id={$record->id})";
             return $errresult;
         }
     } else {
         //insert
         $errresult = insert_record("question_webwork", $record);
         if (!$errresult) {
             $errresult->error = "Could not insert question_webwork record!";
             return $errresult;
         }
         //set the new record id
         $record->id = $errresult;
     }
     $wwquestionid = $record->id;
     //copy the tmp directory to the question one
     if ($isupdate == false) {
         rename(webwork_get_tmp_path_full(), webwork_get_wwquestion_path_full($wwquestionid));
     }
     //update the derivations
     $this->_update_derivations($wwquestionid);
     return true;
 }
 /**
  * @desc Test creation of a wwquestion directory
  */
 function test_create_wwquestion_directory()
 {
     webwork_make_wwquestion_dir('SIMPLETEST');
     $path = webwork_get_wwquestion_path_full('SIMPLETEST');
     $this->assertEqual(is_dir($path), true, 'Webwork Question Directory Creation Test.');
     webwork_delete_wwquestion_dir('SIMPLETEST');
 }
Example #3
0
function webwork_codecheck($data, $wwquestionid, $questioncopy)
{
    //codechecklevel
    $codechecklevel = $data['codecheck'];
    //here we construct a temp question object
    $question = new stdClass();
    $question->code = base64_encode(stripslashes($data['code']));
    $question->seed = $data['seed'];
    $question->trials = $data['trials'];
    //handle the new question from old functionality
    if ($questioncopy) {
        webwork_make_tmp_dir();
        $path = webwork_get_wwquestion_path_full($wwquestionid);
        $filelist = list_directories_and_files($path);
        //copy everthing but derivations folder into tmp folder
        foreach ($filelist as $file) {
            if ($file != 'derivations') {
                webwork_copy_file("{$path}/{$file}", webwork_get_tmp_path_full() . '/' . $file);
            }
        }
    }
    //should we look in tmp or in wwquestion dir to find stuff
    if (!isset($wwquestionid)) {
        $path = webwork_get_tmp_path_full();
        $urlpath = webwork_get_filehandler_path() . '/' . webwork_get_tmp_path() . '/';
    } else {
        $path = webwork_get_wwquestion_path_full($this->question->webworkid);
        $urlpath = webwork_get_filehandler_path() . '/' . webwork_get_wwquestion_path($this->question->webworkid) . '/';
    }
    $filelist = list_directories_and_files($path);
    $filearray = array();
    //files that need to be pushed
    foreach ($filelist as $file) {
        if (!is_dir($path . '/' . $file)) {
            $encode = base64_encode($urlpath . '/' . $file);
            array_push($filearray, $encode);
        }
    }
    $question->files = $filearray;
    //one call to the server will return response for this code and keep it static in the function
    $results = webwork_get_derivations($question);
    //filter errors and warnings
    $errorresults = array();
    $noerrorresults = array();
    $warningresults = array();
    $goodresults = array();
    foreach ($results as $record) {
        if (isset($record['errors']) && $record['errors'] != '' && $record['errors'] != null) {
            array_push($errorresults, $record);
        } else {
            array_push($noerrorresults, $record);
        }
    }
    foreach ($noerrorresults as $record) {
        if (isset($record['warnings']) && $record['warnings'] != '' && $record['warnings'] != null) {
            array_push($warningresults, $record);
        } else {
            array_push($goodresults, $record);
        }
    }
    switch ($codechecklevel) {
        //No code check
        case 0:
            webwork_qtype::_derivations($results);
            return true;
            break;
            //reject seeds with errors
        //reject seeds with errors
        case 1:
            if (count($noerrorresults) > 0) {
                webwork_qtype::_derivations($noerrorresults);
                return true;
            }
            break;
            //reject if errors
        //reject if errors
        case 2:
            if (count($noerrorresults) == count($results)) {
                webwork_qtype::_derivations($results);
                return true;
            }
            break;
            //reject seeds with errors or warnings
        //reject seeds with errors or warnings
        case 3:
            if (count($goodresults) > 0) {
                webwork_qtype::_derivations($goodresults);
                return true;
            }
            break;
            //reject if errors or warnings
        //reject if errors or warnings
        case 4:
            if (count($goodresults) == count($results)) {
                webwork_qtype::_derivations($results);
                return true;
            }
            break;
    }
    $errormsgs = array();
    $warningmsgs = array();
    //at this point we are going to be invalid
    //this correlates seeds with certain error messages for better output
    //ERRORS
    foreach ($errorresults as $record) {
        $found = 0;
        $candidate = $record['errors'] . "<br>";
        $candidateseed = $record['seed'];
        for ($i = 0; $i < count($errormsgs); $i++) {
            if ($candidate == $errormsgs[$i]['errors']) {
                $found = 1;
                $errormsgs[$i]['seeds'][] = $candidateseed;
            }
        }
        if ($found == 0) {
            //new error message
            $msg = array();
            $msg['errors'] = $candidate;
            $msg['seeds'] = array();
            $msg['seeds'][] = $candidateseed;
            $errormsgs[] = $msg;
        }
    }
    //WARNINGS
    foreach ($warningresults as $record) {
        $found = 0;
        $candidate = $record['warnings'] . "<br>";
        $candidateseed = $record['seed'];
        for ($i = 0; $i < count($warningmsgs); $i++) {
            if ($candidate == $warningmsgs[$i]['errors']) {
                $found = 1;
                $warningmsgs[$i]['seeds'][] = $candidateseed;
            }
        }
        if ($found == 0) {
            //new error message
            $msg = array();
            $msg['warnings'] = $candidate;
            $msg['seeds'] = array();
            $msg['seeds'][] = $candidateseed;
            $warningmsgs[] = $msg;
        }
    }
    $output = "Errors in PG Code on: " . count($errorresults) . " out of " . count($results) . " seeds tried:<br>";
    //construct error statement
    $counter = 1;
    foreach ($errormsgs as $msg) {
        $output .= "{$counter}) ";
        $output .= "Seeds (";
        foreach ($msg['seeds'] as $seed) {
            $output .= $seed . " ";
        }
        $output .= ") gave Errors:" . $msg['errors'] . "<br><br>";
        $counter++;
    }
    $output .= "Warnings in PG Code on: " . count($warningresults) . " out of " . count($results) . " seeds tried:<br>";
    $counter = 1;
    foreach ($warningmsgs as $msg) {
        $output .= "{$counter}) ";
        $output .= "Seeds (";
        foreach ($msg['seeds'] as $seed) {
            $output .= $seed . " ";
        }
        $output .= ") gave Warnings:" . $msg['warnings'] . "<br><br>";
        $counter++;
    }
    $returner = array();
    $returner['code'] = $output;
    return $returner;
}