Commit 36bbfaf7 authored by GROSS-AMBLARD David's avatar GROSS-AMBLARD David
Browse files

Moves all actions in dedicated file in /pages

Issues with tinymce, because the site must be in apache root
parent 378c856a
......@@ -3,7 +3,7 @@
session_start();
require_once "HWlib.php";
require_once "TaskViewerModel.php";
require_once "pages/taskViewer/TaskViewerModel.php";
$manager = new AjaxManager();
......
2019-03-23:HIGH:parters logo on platform
2018-12-03:LOW;add min/max time for tasks
2018-12-03:LOW;add captcha tasks
2018-12-03:LOW;add available sensors for profile
2018-03-07:HIGH:encode skill tree crowdsourcing
2018-03-07:HIGH:Add an admin log
2018-03-07:MEDIUM:separate users from artifacts
2018-03-06:LOW:Obtain an offline GUI (even if external css are not available)
......@@ -11,123 +11,6 @@ function cleanDb($conn)
//trace des preferences si besoin
}
/**
* initialize
*
* Initialize all artifacts is the projects folder, except the disabled one, and update the artfactID in startme table
*
* @param $conn : A valid database PDO connection
*/
function initialize($conn)
{
// run each init-workflow.sql in each project, except in the disabled directory
$projectPath="projects";
$projectlist=scandir($projectPath);
$projectlist=array_diff($projectlist,array(".",".."));
foreach($projectlist as $projectdir)
if(is_dir("$projectPath/$projectdir"))
if($projectdir!="disabled"){
debug("Loading project ".$projectdir);
executeSqlFile("$projectPath/$projectdir/init-skills.sql", $conn);
executeSqlFile("$projectPath/$projectdir/init-workflow.sql", $conn);
// load each .tmpl template file of each project
$filenames=scandir("$projectPath/$projectdir");
$filenames=array_diff($filenames,array(".",".."));
foreach($filenames as $filename){
debug("looking into $filename");
if(is_file("$projectPath/$projectdir/$filename")){
debug("checking $filename, with ".pathinfo($filename)['extension']);
if (pathinfo($filename,PATHINFO_EXTENSION)=="tmpl"){
$shortfilename=pathinfo($filename,PATHINFO_FILENAME);
$query="insert into Template(id,body) values ('$projectdir-$shortfilename',".$conn->quote((file_get_contents("$projectPath/$projectdir/$filename"))).")";
$conn->query($query);
}
}
}
}
// update all artifacts in the startme table
$query="select ID from Artifact";
$table=$conn->query($query);
foreach ($table as $artifactid){
debug("launching artifact ".$artifactid['ID']);
update($conn, $artifactid['ID']);
}
}
/**
* restart
*
* Initialize the db
*
* @param $conn : A valid database PDO connection
*/
function restart($conn)
{
executeSqlFile('init-db-schema.sql', $conn);
}
/**
* execute the content of an SQL file
*
* @param $file_name : the name of a file containing SQL queries
* param $conn : A valid database PDO connection
*/
function executeSqlFile($file_name, $conn){
$file_content = file($file_name);
$query = "";
foreach ($file_content as $sql_line) {
if (trim($sql_line) != "" && strpos($sql_line, "--") === false) {
$query .= $sql_line;
if (substr(rtrim($query), -1) == ';') {
debug("query from init-workflow");
debug($query);
$conn->query($query);
$query = "";
}
}
}
}
/**
* createBots
* create an armada of bots as users
* @param $conn: a valid PDO connection
* @param $nbbots: the number of bots to create
*/
function createBots($conn,$nbbots){
$namePrefix="Allgaier";
$conn->query("delete from Skills where iduser in (select id from Users where name like '$namePrefix%')");
$conn->query("delete from UserProfile where id in (select id from Users where name like '$namePrefix%')");
$conn->query("delete from Users where name like '$namePrefix%'");
for($i=1;$i<=NBBOTS;$i++){
$botname="$namePrefix$i";
$pwd="bot";
$stmt = $conn->prepare("insert into Users(name,hashed_password) values(:name,:pwd)");
$stmt->bindParam(':name', $botname, PDO::PARAM_STR);
$stmt->bindParam(':pwd', $pwd, PDO::PARAM_STR);
$pwd=password_hash($pwd, PASSWORD_DEFAULT);
$stmt->execute();
$stmt = $conn->prepare("select * from Users where name=:username");
$stmt->bindParam(':username', $botname, PDO::PARAM_STR);
$stmt->execute();
$result =$stmt->setFetchMode(PDO::FETCH_ASSOC);
$line=$stmt->fetch();
$id=$line['id'];
// Any user has the "common knowledge" skill
$conn->query("insert into Skills values ($id,1,100)");
$conn->query("insert into UserProfile values ($id,'I am a bot and proud of it.',365,0)");
}
}
/**
* nextTaskFromArtifact
......@@ -156,303 +39,5 @@ function nextTaskFromArtifact($conn,$user,$artifact){
// TODO document bots
// TODO document presentation.html
/**
* insertJsonAnswer
*
* Insert in the db a user answer & call update
*
* @param $conn : A valid database PDO connection
* @param $id : The id of the task
* @param $activity_id : The id of the artifact of the task
* @param $answer : The user answer(s)
* @param $mass: the mass of the answer for uncertain ones
*/
function insertJsonAnswer($conn, $id, $activity_id,$answer,$mass){
$typeanswer=0;
$ajax = (isset($_POST['ajax']) and $_POST['ajax'] == true) ? true : false;
$help = (isset($_POST['help']) and $_POST['help'] == true) ? true : false;
if (isset($_POST['name']) && isset($_POST['MyRadio'])) {
$radioVal = $_POST["MyRadio"];
if ($radioVal == "q1") {
$_POST['answer'] = $_POST["select_".$radioVal];
$typeanswer = 1;
} elseif ($radioVal == "q2") {
$_POST['answer'] = $_POST["select_".$radioVal];
$typeanswer = 2;
} elseif ($radioVal == "qPrecis") {
$_POST['answer'] = $_POST["select_".$radioVal];
$typeanswer = 3;
}
$_POST['answer'] = $_POST["select_".$radioVal];
}
if (isset($_POST["level"]) && $_POST["level"]) {
$_POST['answer'] = $_POST["level"];
$typeanswer = 0;
}
insertAnswer($conn, $id, $activity_id,$answer, $mass,$ajax, $help, $typeanswer);
}
/**
* insertAnswer
*
* Insert in the db a user answer & call update
*
* @param $conn : A valid database PDO connection
* @param $id : The id of the task
* @param $artifactid : The id of the artifact of the task
* @param $value : The user answer(s)
* @param $mass: the mass of the answer for uncertain ones
* @param boolean $ajax true if you want to make the user go directly to the next task of the artifact
*/
function insertAnswer($conn, $id, $artifactid, $value, $mass, $isAjax=false, $help=false, $typeanswer=0)
{
debug("called");
$user=$_SESSION['id'];
$stmt = $conn->query("select * from Task where id=$id and artifact=$artifactid");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$checker = $result['checker'];
$type = $result['type'];
$artifact=$result['artifact'];
$realMass=1; // by default, answers are certain (crisp)
// checking if this is an uncertain answer
if ($result['modality']=='belief')
$realMass=$mass;
// Checking if whether or not the answer is a JSON object
$isJson = false;
if ($type >= 5 and $type <= 7) {
json_decode($value);
$isJson = json_last_error() == JSON_ERROR_NONE;
}
if (!empty($checker) && !$isJson) {
debug("<p>$checker on</p>");
$stmt = $conn->query("select '$value' $checker as result from dual");
$checked=$stmt->fetch()['result'];
debug("<p> res $checked</p>");
} else {
debug("no checker");
$checked=true;
}
// Checking if the answer is valid depending of its type
if ((($checked and $type != 3) or ($checked and $type == 3 and strlen(preg_replace("/\r|\n/", "", $value)) > 0)) and $value !== null) {
//TODO : gérer la partie ressource "help"
$query = "insert into Answer(idtask, artifact, user, mass, value".($help ? ", help" : "").") values ";
if ($type == 3 || (($type == 5 || $type == 6 || $type == 7) && $isJson)) { // Multi-line answer or JSON answer
$values = "";
if ($type == 3) {
foreach (explode(PHP_EOL, $value) as $val) {
$val = html_entity_decode($val, ENT_QUOTES); // We need to decode in order to avoid double encoding issues
$values .= "(:id, :artifact, :user, $realMass, " . $conn->quote($val) . "),";
}
} else {
foreach (json_decode($value) as $jsonObject) {
$val = html_entity_decode($jsonObject->value, ENT_QUOTES); // We need to decode in order to avoid double encoding issues
$values .= "(" . $jsonObject->idtask . ",:artifact, :user, $realMass," . $conn->quote($val) . ($help ? ", ".(strlen($jsonObject->help->link) > 0 ? $conn->quote(json_encode($jsonObject->help)) : "null") : "") . "),";
}
}
$values = substr($values, 0, strlen($values) - 1);
$query .= $values;
} else {
$query .= "(:id, :artifact,:user, $realMass, :value)";
}
debug("Current query ".$query);
$stmt = $conn->prepare($query);
if (!$isJson) {
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
if ($type != 3) {
$stmt->bindParam(':value', $value, PDO::PARAM_STR);
}
}
$stmt->bindParam(':user', $user, PDO::PARAM_INT);
$stmt->bindParam(':artifact', $artifact, PDO::PARAM_STR);
$stmt->execute(); // or die(mysql_error());
// indicated that the task has been answered
$conn->query("insert into Answered(id,artifact) values ($id,$artifact)"); // or die();
update($conn, $artifact);
if (!$isAjax) {
$nextTaskId=nextTaskFromArtifact($conn, $user, $artifact);
if($nextTaskId)
answer($conn, $nextTaskId);
else
showTasks($conn,true);
} else { // If the task has Ajax enabled, we check if a new task has been created after the insertion
/*$stmtAjax = $conn->prepare("SELECT t.id
FROM Task t, profile p, Skills s
WHERE s.iduser = :user
AND s.idskill = p.idskill
AND t.id = p.idtask
AND t.artifact = :artifact
ORDER BY t.id DESC LIMIT 1");
$stmtAjax->bindParam(':user', $user, PDO::PARAM_INT);
$stmtAjax->bindParam(':artifact', $_REQUEST['choice'], PDO::PARAM_INT);
$stmtAjax->execute();
$taskId = $stmtAjax->fetchColumn();*/
$taskId = nextTaskFromArtifact($conn, $user, $_REQUEST['choice']);
// We only send the new task html if its id is different than the previous one (Ajax takes care of the 'else' part)
if ($taskId != $id) {
answer($conn, $taskId);
}
}
} else {
answer($conn, $id, true);
}
}
/**
* Store the user feedback in database
* @param PDO $conn a valid PDO connection to database
* @param string $page the path of the affected page
* @param string $feedbackText the feedback given by the user
*/
function submitFeedback($conn, $page, $feedbackText)
{
$user=$_SESSION['id'];
debug("feedback : $user $page $feedbackText");
$stmt = $conn->prepare("INSERT INTO feedback (iduser, page, text) VALUES (:iduser, :page, :text)");
$stmt->execute(array(
":iduser" => $user,
":page" => $page,
":text" => $feedbackText
)); // or die(mysql_error());
}
/**
* Store the user question/task feedback in a specific table in database
* @param PDO $conn a valid PDO connection to database
* @param int $idtask the task id targeted by the feedback
* @param string $feedbackText the feedback
* @param string $targetTable the name of the database table which contains the fedback
*/
function insertTaskFeedback($conn, $idtask, $feedbackText, $targetTable)
{
$user=$_SESSION['id'];
$stmt = $conn->prepare("INSERT INTO `$targetTable` (idtask, feedback, iduser) VALUES (:idtask, :text, :iduser)");
$stmt->execute(array(
"idtask" => $idtask,
"text" => $feedbackText,
"iduser" => $user
));
}
/**
* Store the user post in database
* @param int $topicId the topic id in which the post will be added
* @param string $content the content of the post
*/
function insertPostForum($topicId, $content)
{
require_once "ForumController.php";
$forum = new ForumController();
$forum->addPostToTopic($topicId, $content);
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
header('Location: http://'.$host.$uri.'/index.php?mode=showForum&topic='.$topicId);
}
/**
* Create a new topic
* @param string $title the topic title
* @param int $category the category's id of the topic
* @param int $task the task's id (only used if the category is 'task')
* @param string $content
*/
function insertNewTopic($title, $category, $task, $content)
{
require_once "ForumController.php";
$forum = new ForumController();
$topicId = $forum->createNewTopic($title, $category, $task, $content);
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
header('Location: http://'.$host.$uri.'/index.php?mode=showForum&topic='.$topicId);
}
function DEPRECATEDchooseArtifactH($conn, $id, $artifact)
{
debug("called");
$user=$_SESSION['id'];
$sql_classid = " select CLASSID from ARTIFACT where ID = $id";
$stmt_classid = $conn->query($sql_classid);
$classid=$stmt_classid->fetch()['CLASSID'];
$_SESSION['artifactclass_active'] = $classid;
$_SESSION['artifact_active'] = $id;
if ($id == "") {
$sql = " select description, definition from ARTIFACTCLASS where ID = 51";
} else {
$sql_desc = " select description from ARTIFACTCLASS where ID = $classid";
$sql_def = " select definition from ARTIFACTCLASS where ID = $classid";
$sql_tablename = " select TABLENAME from ARTIFACTCLASS where ID = $classid";
$sql_project = " select PROJECT from ARTIFACTCLASS where ID = $classid";
}
$stmt_desc = $conn->query($sql_desc);
$stmt_def = $conn->query($sql_def);
$stmt_tablename = $conn->query($sql_tablename);
$stmt_project = $conn->query($sql_project);
$artifact_def=$stmt_def->fetch()['definition'];
$artifact_desc=$stmt_desc->fetch()['description'];
$artifact_project=$stmt_project->fetch()['PROJECT'];
$_SESSION['project'] = $artifact_project;
$artifact=load_artifact($artifact_def);
$conn->query("delete from CURRENTARTIFACT where USER=".$_SESSION['id']." and NAME='".$_SESSION['tablename']."'");
$artifact_tablename=$stmt_tablename->fetch()['TABLENAME'];
$_SESSION['tablename'] = $artifact_tablename;
$stmt = $conn->prepare("insert into CURRENTARTIFACT(ID,NAME,USER) values (:id,:name,:user)");
$stmt->bindParam(':id', $id, PDO::PARAM_STR);
$stmt->bindParam(':name', $_SESSION['tablename'], PDO::PARAM_STR);
$stmt->bindParam(':user', $_SESSION['id'], PDO::PARAM_INT);
$stmt->execute(); // or die(mysql_error());
$_SESSION['artifact_active'] = $id;
update($conn, $artifact);
debug("update date");
showAllTasks($conn);
}
/**
* Add a new artifact class to the MyOwnProject projectegory is 'task')
* @param string $content
*/
function addArtifact($conn,$json){
global $VIEW;
$VIEW['MAIN'].="Did it<pre>$json</pre>";
$file=fopen("projects/MyOwnProject/myownproject.sca", "w");
fwrite($file,$json);
fclose($file);
$query="insert into ArtifactClass(id,description,definition,project,autostart) values (10000,'MyOwnProject','MyOwnProject/myownproject.sca', 'MyOwnProject',true)";
$conn->query($query);
}
<?php
/**
* addUser
*
* Add a new user in the db
*
* @param $conn : A valid database PDO connection
*/
function addUser($conn)
{
$stmt = $conn->prepare("select id from Users where name=:username");
debug("addUser:username:".$_POST["username"]." !");
$stmt->bindParam(':username', $_POST["username"], PDO::PARAM_STR);
$stmt->execute();
if ($stmt->rowCount()>0 && $_POST["username"]!="anonymous") {
//RESTART SWITCH
showregister($conn, true);
} else {
$stmt = $conn->prepare("insert into Users(name,hashed_password) values(:name,:pwd)");
$stmt->bindParam(':name', $_POST["username"], PDO::PARAM_STR);
$stmt->bindParam(':pwd', $pwd, PDO::PARAM_STR);
$pwd=password_hash($_POST["password"], PASSWORD_DEFAULT);
$stmt->execute() or die(mysql_error());
$stmt = $conn->prepare("select * from Users where name=:username AND hashed_password=:pwd");
$stmt->bindParam(':username', $_POST["username"], PDO::PARAM_STR);
$stmt->bindParam(':pwd', $pwd, PDO::PARAM_STR);
$stmt->execute();
$result =$stmt->setFetchMode(PDO::FETCH_ASSOC);
$line=$stmt->fetch();
$_SESSION["login"]=true;
$_SESSION["id"]=$line['id'];
// Any user has the "common knowledge" skill
$conn->query("insert into Skills values (".$_SESSION["id"].",1,100)");
$_SESSION['username']=$line['name'];
if ($_POST["username"]!="anonymous") {
showProfile($conn);
} else {
showTasks($conn);
}
}
//update($conn,$artifact);
}
/**
* logout
*
* Close and unset the session, redirection to the log in page
*
* @param $conn : A valid database PDO connection
*/
function logout($conn)
{
if ($_SESSION['username']=="anonymous") {
cleanDb($conn);
}
unset($_SESSION);
session_destroy();
// Suppressing automatic connection cookies
setcookie('login', '');
setcookie('pass_hache', '');
loginForm($conn);
}
function maintenance()
{
if ($_SESSION['username']=="anonymous") {
cleanDb($conn);
}
unset($_SESSION);
session_destroy();
showMaintenance();
}
......@@ -21,21 +21,30 @@ else {
}
require("lib/HWlib.php");
require("lib/HTML.php");
require("lib/artifacts.php");
require("profile.php");
require("pages/skills/skills.php");
require("tasks.php");
require("actions.php");
require("connection.php");
require("show-pages.php");
require("show-artifact.php");
require("drawArtifact.php");
require("pages/workflowdesign/workflowdesign.php");
require("pages/showCredit/showCredit.php");
require("pages/showState/showState.php");
require("pages/loginForm/loginForm.php");
require_once("lib/HWlib.php");
require_once("lib/HTML.php");
require_once("lib/artifacts.php");
require_once("pages/skills/skills.php");
require_once("tasks.php");
require_once("actions.php");
require_once("connection.php");
require_once("show-pages.php");
require_once("pages/workflowdesign/workflowdesign.php");
require_once("pages/showCredit/showCredit.php");
require_once("pages/showState/showState.php");
require_once("pages/loginForm/loginForm.php");
require_once("pages/register/register.php");
require_once("pages/addUser/addUser.php");
require_once("pages/logout/logout.php");
require_once("pages/profile/profile.php");
require_once("pages/showTasks/showTasks.php");
require_once("pages/startup/startup.php");
require_once("pages/drawArtifact/drawArtifact.php");
require_once("pages/maintenance/maintenance.php");
require_once("pages/feedback/feedback.php");
require_once("pages/forum/forum.php");
require_once("pages/taskViewer/TaskViewer.php");
session_start();
......@@ -66,21 +75,26 @@ debug("mode is : $mode");
switch ($mode) {
case "login":
loginForm($conn);
// moved to /Pages
// moved to /pages
break;
case "register":
showregister($conn, false);
register($conn, false);
// moved to /pages
break;
case "registered":
case "addUser":
// moved to /pages
addUser($conn);
break;
case "logout":
// moved to /pages
logout($conn);
break;
case "profile":
showProfile($conn);
// moved to /pages
profile($conn);
break;
case "saveProfile":
// moved to /pages
saveProfile($conn);
break;
case "skills":
......@@ -88,27 +102,27 @@ switch ($mode) {
skills($conn);
break;
case "addSkill":
// moved into /pages
addSkill($conn);
break;
case "saveSkill":
// moved into /pages
saveSkill($conn);
break;
case "showTasks":
// moved into /pages
showTasks($conn);
break;
case "showAllTasks":
showAllTasks($conn);
break;
case "generateJson":
generateJson($conn);
break;