/** hack to Just Get A Question from current dmid, using Excercise class * to do our factorying for us. * Obviously the factory code needs to be refactored to ElseWhere, but not on Sunday * :-P * (Sun 15 Jun 2008) */ function getQuestion( $dmid, $questionLanguages, $answerLanguages ) { if ( !$questionLanguages ) throw new Exception( "Vocview: no question (original) languages provided!" ); if ( !$answerLanguages ) throw new Exception( "Vocview: no answer (translation) languages provided!" ); /* 3 men walked into a bar^wExercise * fullset, fetcher, and subset */ /* our regular fetcher is provided by functions.php * I suppose if I were tidier, I could use fetchers for * persistence */ $fetcher = new OWFetcher(); /* fullset is a domdocument containing a <collection> of * empty <defined-meanings/> (just their defined-meaning-id * attribute is set) * This format makes sense on some days, it's just * massive nukular overkill today, specially since * we only have 1 element :-P * Still, if Exercise wants this as a dom, we can oblige. */ $xmlString = " <collection> <defined-meaning defined-meaning-id=\"$dmid\" /> </collection> "; $xml = simplexml_load_string( $xmlString ); $fullset = dom_import_simplexml( $xml )->ownerDocument; # et voila. /* subset is a selection of stuff we are actually * interested in, from the above, as an array of * dmid's .... Oh look! We have just one! */ $subset = array( $dmid ); # :-P # (ok, to be honest, it does also get auto-generated # from the fullset, if we say nothing... but # then where would the joke be? ) # and we already had the fetcher. # So the fullset said to the fetcher: let's do this! $exercise = new Exercise( $fetcher, $fullset, $subset ); $exercise->setQuestionLanguages( $questionLanguages ); $exercise->setAnswerLanguages( $answerLanguages ); # Ok, now let's see. which question did we need? # (and setting selfcheck to false) $question = $exercise->getQuestion( $dmid, false ); # Oh REALLY! And we needed to go through all that? # well, let's return it, before people start asking # more difficult questions. return $question; }
function loadExercise( $exercise_id ) { if ( !is_int( $exercise_id ) ) throw new Exception( "persist; loadExercise exercise_id is not an integer" ); global $mysql_info; DBTools::connect( $mysql_info ); $exercise_id = mysql_real_escape_string( $exercise_id ); $row = DBTools::doQuery( "select * from exercises where id=\"$exercise_id\"" ); $exercise = new Exercise(); $exercise->setId( $exercise_id ); $exercise->loadXML( $row["exercise"] ); $exercise->setQuestionLanguages( explode( ",", $row["questionLanguages"] ) ); $exercise->setAnswerLanguages( explode( ",", $row["answerLanguages"] ) ); return $exercise; }
function main() { $collection_id = 376317; # olpc dictionary.... WAY too big $fetcher = new OWFetcher(); echo "fullset...\n"; $fullSetXML = $fetcher->getFullSetXML_asString( $collection_id ); $fullSet = new DOMDocument(); $success = $fullSet->loadXML( $fullSetXML ); if ( !$success ) { throw new Exception( "Failed to load category XML from server" ); } $maxSubSet = dom2set( $fullSet ); # sort($maxSubSet); foreach ($maxSubSet as $dmid) {print "$dmid,";} # var_dump($fullSet->saveXML()); $exercise = new Exercise( $fetcher, $fullSet, $maxSubSet ); # pwease, not the max! # $exercise->setLanguages(array("eng","fra","deu")); $exercise->setQuestionLanguages( array( "deu" ) ); $exercise->setAnswerLanguages( array( "eng" ) ); # $question_dmid=$maxSubSet[array_rand($maxSubSet)]; echo "question...\n"; # $questionNode=$exercise->getQuestionNode($question_dmid); # dumpNode($questionNode); $runex = $exercise->randSubExercise( 10 ); dumpExercise( $runex, 5 ); echo "\n\n=== presistence test ===\n\n"; saveExercise( $runex ); $exid = mysql_insert_id(); $loadex = loadExercise( $exid ); $loadex->setFetcher( $fetcher ); dumpExercise( $loadex, 10 ); }
/** create a new Exercise from scratch. # */ public function createExercise( $userName, $size, $collection_id, $questionLanguages, $answerLanguages, $hide ) { # this can be simplified for now... # first get a master exercise... $fetcher = new OWFetcher(); $fullSetXML = $fetcher->getFullSetXML_asString( $collection_id, array_merge( $questionLanguages, $answerLanguages ) ); $fullSet = new DOMDocument(); $success = $fullSet->loadXML( $fullSetXML ); if ( !$success ) { throw new Exception( "Failed to load category XML from server" ); } $exercise = new Exercise( $fetcher, $fullSet ); $exercise->setQuestionLanguages( $questionLanguages ); $exercise->setAnswerLanguages( $answerLanguages ); $exercise->setHide( $hide ); # This is the master exercise... which we should now store and # worship. That's for mark II though. # Today we toss it in the trash and just snarf a # subset instead. Mean huh? $subExercise = $exercise->randSubExercise( $size ); $this->saveExercise( $subExercise, $userName ); return $subExercise; }
/** On the basis of the subset array provided, create new exercise object */ public function getSubExercize( $subset ) { $dom = new DOMDocument(); $collection = $dom->createElement( "collection" ); $dom->appendChild( $collection ); foreach ( $subset as $dmid ) { # Grab each node from the current full set, $_depth must always start at 0 # and ($fetch=false) we just want the current state, # we can fetch things later at our leisure. $questionNode = $this->_getQuestionNode( $dmid, $this->fullSet, 0, false ); $questionNode = $dom->importNode( $questionNode, true ); $collection->appendChild( $questionNode ); } $newExercise = new Exercise( $this->fetcher, $dom, $subset ); $newExercise->setQuestionLanguages( $this->getQuestionLanguages() ); $newExercise->setAnswerLanguages( $this->getAnswerLanguages() ); $newExercise->setHide( $this->getHide() ); return $newExercise; }