Beispiel #1
0
function main() {
	$fetcher = new OWFetcher();
	echo "\n\n=== presistence test ===\n\n";
	$loadex = loadExercise( 4 );
	$loadex->setFetcher( $fetcher );
	dumpExercise( $loadex, 10 );

}
Beispiel #2
0
	/** retrieve the exercice we're using for this session */
	public function getExercise( $username ) {

		# we currently assume one exercise per user, and no master...
		# this will change and this needs refactoring
		# for now we can cheat
		$exercises = $this->getExercisesForUser( $username );
		if ( count( $exercises ) < 1 )
			return null;

		# the cheat
		$exercise_id = $exercises[0];

		# carrying on, whisteling innocently
		$exercise = loadExercise( $exercise_id );
		$exercise->setFetcher( new OWFetcher() );
		return $exercise;
	}
Beispiel #3
0
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 );

}
 public function copyExercise($id)
 {
     if (!userOwnsSeries(Auth::id())) {
         flash()->error('You must own series in order to add exercises.')->important();
         return redirect('exercises/' . $id);
     }
     $myseries = loadMySeries();
     $series = [];
     foreach ($myseries as $serie) {
         $series = $series + [$serie->id => $serie->title];
     }
     $exercise = loadExercise($id)[0];
     $exercise->expected_result = null;
     return view('exercises.copy', compact('series', 'exercise'));
 }
 public function storeAnswer($id, CreateAnswerRequest $request)
 {
     $input = $request->all();
     // Get time between exercise load and store answer.
     $endTime = microtime(true);
     $diffTime = $endTime - $input['start_time'];
     $exercise = loadExercise($id)[0];
     //must check for empty answers & stuff like that...
     //must also find a way to avoid duplicate answers since 'text' types can't be used as key
     $ans = new Answer();
     $ans->given_code = $input['given_code'];
     $ans->time = $diffTime;
     $result = preg_replace('/[^A-Za-z0-9\\-\\ ,\\.;:\\[\\]\\?\\!@#$%&\\*\\(\\)\\-=\\+\\.^\\P{C}\\n]/', '', $input['result']);
     // dd($result);
     // dd(preg_match("/^[hH]ello, [wW]orld$/", substr_replace($result, "", -1)));
     // dd(preg_match("/^Hello, world$/", $result));
     if ($exercise->expected_result == '*') {
         $ans->success = true;
     } else {
         $rule = "/" . $exercise->expected_result . "/";
         // dd($rule);
         if (preg_match($rule, $result)) {
             $ans->success = true;
         } elseif (compare(bin2hex($result), bin2hex($exercise->expected_result . chr(0xd) . chr(0xa)))) {
             $ans->success = true;
         } else {
             $ans->success = false;
         }
     }
     $ans->uId = Auth::id();
     $ans->eId = $id;
     storeAnswer($ans);
     if ($exercise->expected_result != '*') {
         if ($ans->success) {
             flash()->success("You solved the exercise in " . $diffTime . " seconds.");
             \Session::flash('correctAnswer', 'blabla');
         } else {
             flash()->error("Too bad, the answer was wrong.");
         }
     }
     // $result = $input['result'];
     $answer = $input['given_code'];
     $sId = \Session::get('currentSerie');
     $challenges = loadChallengesByUserExercise(\Auth::id(), $id);
     // Only update challenge if the given answer is correct.
     if ($ans->success) {
         foreach ($challenges as $c) {
             if ($c->winner != \Auth::id()) {
                 if ($c->userA == \Auth::id()) {
                     if (!empty(loadCorrectAnswers($c->userB, $id)) && $diffTime < loadCorrectAnswers($c->userB, $id)[0]->time) {
                         $newScore = loadUser(\Auth::id())[0]->score;
                         $newScore += 1;
                         setUserScore(\Auth::id(), $newScore);
                         setWinner($c->id, \Auth::id());
                         storeNotification($c->userB, "challenge beaten", \Auth::id(), $c->id);
                     }
                 } else {
                     if (!empty(loadCorrectAnswers($c->userA, $id)) && $diffTime < loadCorrectAnswers($c->userA, $id)[0]->time) {
                         $newScore = loadUser(\Auth::id())[0]->score;
                         $newScore += 1;
                         setUserScore(\Auth::id(), $newScore);
                         setWinner($c->id, \Auth::id());
                         storeNotification($c->userA, "challenge beaten", \Auth::id(), $c->id);
                     }
                 }
             }
         }
     }
     return redirect('exercises/' . $id)->with(['result' => $result, 'answer' => $answer]);
 }