/**
  * @desc Test creation of a derivation directory
  */
 function test_create_derivation_directory()
 {
     webwork_make_derivation_dir('SIMPLETEST', 'SIMPLETEST');
     $path = webwork_get_derivation_path_full('SIMPLETEST', 'SIMPLETEST');
     $this->assertEqual(is_dir($path), true, 'Webwork Derivation Directory Test.');
     webwork_delete_derivation_dir('SIMPLETEST', 'SIMPLETEST');
 }
Example #2
0
/**
* @desc Parses a derivation record. This changes all paths of external entities. Copies all external entities to local data folder.
* @param object $derivation The derivation object.
* @return bool true. 
*/
function webwork_parse_change_derivation(&$derivation)
{
    //webwork question id
    $wwquestionid = $derivation->question_webwork;
    //assign paths
    $wwquestioncopyto = webwork_get_wwquestion_path_full($wwquestionid);
    $wwquestionreplacer = webwork_get_wwquestion_url($wwquestionid);
    $derivationcopyto = webwork_get_derivation_path_full($wwquestionid, $derivation->id);
    $derivationreplacer = webwork_get_derivation_url($wwquestionid, $derivation->id);
    $parsedhtml = "";
    $html = base64_decode($derivation->html);
    $parser = new HtmlParser($html);
    while ($parser->parse()) {
        if ($parser->iNodeType == NODE_TYPE_ELEMENT) {
            $nodename = $parser->iNodeName;
            if (strcasecmp($nodename, 'img') == 0) {
                //image, path fixing
                if (isset($parser->iNodeAttributes['src'])) {
                    $src = $parser->iNodeAttributes['src'];
                    if (!(strstr($src, '/tmp/') == false)) {
                        //image produced by the server
                        $srccapture = strrchr($src, '/');
                        webwork_copy_file($src, $derivationcopyto . $srccapture);
                        $parser->iNodeAttributes['src'] = $derivationreplacer . $srccapture;
                    }
                }
            } else {
                if (strcasecmp($nodename, 'a') == 0) {
                    //hyperlink, path fixing
                    if (isset($parser->iNodeAttributes['href'])) {
                        $href = $parser->iNodeAttributes['href'];
                        $hrefcapture = strrchr($href, '/');
                        webwork_copy_file($href, $derivationcopyto . $hrefcapture);
                        $parser->iNodeAttributes['href'] = $derivationreplacer . $hrefcapture;
                    }
                }
            }
        }
        $parsedhtml .= $parser->printTag();
    }
    $derivation->html = base64_encode($parsedhtml);
    return true;
}