Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbutv3/word-app/util/

db_connect.php
dict.xml
practice.php
user.php
util.php

practice.php

124 lines ASCII Windows (CRLF)
<?php


// scentences

function new_scentence($translationId$scentence$gramaticlyCorrect false$gramaticlySound false$verified false)
{
    
quick_statment("INSERT INTO scentence (translation_id, scentence, gramaticly_correct, gramaticly_sound, verified) VALUES(?, ?, ?, ?, ?)""isiii"$translationId$scentence$gramaticlyCorrect$gramaticlySound$verified);
}

function 
verify_scentence($scentenceId$gramaticlyCorrect$gramaticlySound)
{
    
quick_statment("UPDATE scentence SET gramaticly_correct=? gramaticly_sound=? verified=true WHERE id=?""iiii"$gramaticlyCorrect$gramaticlySound$scentenceId);
}

// translation functions

function get_users_available_translations($userId)
{

    return 
quick_statment("SELECT * FROM translation WHERE user_id=?""i"$userId)->fetch_all(MYSQLI_ASSOC);
}


// try_record functions 
function next_checkpoint(&$tries// IMPORTANT - WILL SORT THE ORIGINAL ARRAY
{
    if (
count($tries) < 5)
        return 
0;

    
usort($tries, function ($a$b) {
        return 
$b["time"] - $a["time"];
    });
    
// sort

    // figure out next 
    
$streak 0;
    for (
$j 0$j count($tries); $j++) {
        
$try $tries[$j];
        if (
$try["success"] && $streak != -1)
            
$streak++;
        else
            
$streak = -1;

        if (
$streak 3)
            break;
    }

    if (
$streak 4) {
        return 
0;
    }
    return 
$tries[0]["time"] - $tries[$streak 1]["time"];
}

function 
to_recent($try)
{
    
$tryCooldownTime 30// 30 seconds
    
return time() - $try["time"] < $tryCooldownTime;
}

function 
get_practice_problem()
{
    global 
$db;
    
$userId user_id();
    
$availableTranslations get_users_available_translations($userId);
    
$problem null;
    
$backupProblem null;
    
$leftToLearn 0;
    
// check for checkpoints
    
foreach ($availableTranslations as $translation) {
        
$tries quick_statment("SELECT * FROM try_record WHERE translation_id=?""i"$translation["id"])->fetch_all(MYSQLI_ASSOC);
        
$nextCheckpoint next_checkpoint($tries);

        
//echo $nextCheckpoint;
        
if (time() > $nextCheckpoint) {
            if (
$nextCheckpoint != 0) {
                if (
time() - $tries[0]["time"] < 60 && $problem == null) {
                    
$problem $translation;
                    continue;
                } else {
                    
$problem $translation;
                    break;
                }

            }
            if (
time() - $tries[0]["time"] < 60) {
                if (
$backupProblem == null) {
                    
$backupProblem $translation;
                }
            } else {
                
$backupProblem $translation;
            }

        }
    }
    if (
$backupProblem == null && $problem == null)
        die(
"???");

    if (
$problem == null)
        
$problem $backupProblem;

    
$wordObj quick_statment("SELECT * FROM word WHERE id=?""i"$problem["word_id"])->fetch_assoc();
    
$translations = [];

    foreach (
$availableTranslations as $translation) {
        if (
$translation["word_id"] == $wordObj["id"])
            
$translations[] = $translation;
    }

    return
        [
            
"word" => $wordObj,
            
"translations" => $translations
        
];
}

function 
record_practice(int $wordIdbool $success)
{
    global 
$db;
    
$stmt $db->prepare("INSERT INTO try_record (word_id, success) VALUES (?, ?)");
    
$stmt->bind_param("ib"$wordId$success);
    
$stmt->execute();
}