Admin part: can import, export and drop themes

This commit is contained in:
Némunaire 2013-11-08 20:46:48 +01:00
parent 4d1a860a01
commit ffe0c2a7c4
8 changed files with 217 additions and 7 deletions

View File

@ -48,6 +48,22 @@ if ($n && $p[0] == SALT_ADMIN)
$page = require("admin/import_exercices.php"); $page = require("admin/import_exercices.php");
break; break;
// Theme
case "themes":
case "themes/":
$page = require("admin/list_themes.php");
break;
case "themes/import":
case "themes/import/":
$page = require("admin/import_themes.php");
break;
case "themes/export":
case "themes/export/":
$page = require("admin/export_theme.php");
break;
// Users // Users
case "teams": case "teams":
case "teams/": case "teams/":

View File

@ -0,0 +1,9 @@
<?php
if(!defined('ONYX')) exit;
header("Content-type: application/xml");
$template->assign("theme", new Theme($_GET["id"]));
return "admin/export_theme";

View File

@ -0,0 +1,75 @@
<?php
if(!defined('ONYX')) exit;
if (!empty($_FILES["inputFile"]['tmp_name']))
{
$doc = new DOMDocument();
if (!$doc->load($_FILES["inputFile"]['tmp_name']))
{
erreur("Unable to parse given file. A XML file was expected.");
return;
}
// Define thema information
$theme = new Theme();
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//theme/title");
if (!is_null($elements))
{
foreach ($elements as $element)
$theme->title = $element->nodeValue;
}
// Insert thema into BDD
if (!$theme->update())
{
erreur("Unable to add a new theme.");
return;
}
$elements = $xpath->query("//theme/exercice");
if (!is_null($elements))
{
foreach ($elements as $element)
{
$ex = new Exercice();
$ex->id = $element->getAttribute("id");
$ex->level = $element->getAttribute("level");
if ($element->hasAttribute("depends"))
$ex->require = $element->getAttribute("depends");
foreach ($element->childNodes as $child)
{
if ($child->nodeName == "title")
$ex->title = $child->nodeValue;
else if ($child->nodeName == "points")
$ex->points = intval($child->nodeValue);
else if ($child->nodeName == "statement")
$ex->statement = $child->nodeValue;
else if ($child->nodeName == "key")
$ex->add_key(
$child->hasAttribute("format")?$child->getAttribute("format"):"sha512",
$child->nodeValue);
else if ($child->nodeName == "file")
$ex->add_file(
$child->getAttribute("path"),
$child->nodeValue,
$child->getAttribute("sha1"));
}
if (!$theme->add_exercice($ex))
erreur("Unable to add '".$ex->id."' exercice.");
}
}
erreur("XML file successfully imported.", "success");
}
return "admin/import_exercices";

View File

@ -0,0 +1,43 @@
<?php
if(!defined('ONYX')) exit;
function remove_themes($id)
{
$db = new BDD();
$res = $db->query("SELECT id FROM exercices WHERE id_theme = ".$id);
foreach($res as $r)
{
$db->query("DELETE FROM exercice_files WHERE id_exercice = ".$id);
$db->query("DELETE FROM exercice_keys WHERE id_exercice = ".$id);
$db->query("DELETE FROM solved WHERE id_exercice = ".$id);
}
$db->query("DELETE FROM exercices WHERE id_theme = ".$id);
$db->query("DELETE FROM themes WHERE id = ".$id);
$db->deconnexion();
}
if (!empty($_GET["delete"]))
{
$id_team = intval($_GET["delete"]);
remove_themes($id_team);
header("Location: /".SALT_ADMIN."/themes");
exit;
}
else if (isset($_GET["drop"]))
{
foreach(Theme::get_themes() as $thm)
{
remove_themes($thm->get_id());
}
header("Location: /".SALT_ADMIN."/themes");
exit;
}
$template->assign("themes", Theme::get_themes());
return "admin/themes";

View File

@ -130,9 +130,9 @@ class Theme
$db->deconnexion(); $db->deconnexion();
$array = array(); $array = array();
foreach ($ids as $id) { if ($ids)
$array[] = new Theme($id['id']); foreach ($ids as $id)
} $array[] = new Theme($id['id']);
return $array; return $array;
} }

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<theme>
<title>{$theme->get_name()}</title>
{if $theme->get_exercicesOrdered()}
{foreach from=$theme->get_exercicesOrdered() item=e}
<exercice id="{$e->id}" level="{$e->level}"{if $e->require} depends="{$e->require}"{/if}>
<title>{$e->get_name()}</title>
<points>{$e->points}</points>
<statement>{$e->statement}</statement>
{if $e->keys}
{foreach from=$e->keys item=k}
<key format="{$k.format}">{$k.value}</key>
{/foreach}
{/if}
{if $e->files}
{foreach from=$e->files item=f}
<file path="{$f.path}" sha1="{$f.sha1}">{$f.name}</file>
{/foreach}
{/if}
</exercice>
{/foreach}
{/if}
</theme>

View File

@ -1,4 +1,4 @@
{extends file="layout-nav.tpl"} {extends file="admin/layout.tpl"}
{block name=head} {block name=head}
<link href="/css/common.css" rel="stylesheet"> <link href="/css/common.css" rel="stylesheet">
{/block} {/block}
@ -6,11 +6,11 @@
<h1> <h1>
Import d'exercices Import d'exercices
</h1> </h1>
<form action="/exercices/import" method="post" enctype="multipart/form-data"> <form action="/{$SALT_ADMIN}/themes/import" method="post" enctype="multipart/form-data">
<div class="form-group"> <div class="form-group">
<label for="inputFile">File to import:</label> <label for="inputFile">Fichier XML à importer :</label>
<input type="file" id="inputFile" name="inputFile"> <input type="file" id="inputFile" name="inputFile">
</div> </div>
<button type="submit" class="btn btn-default">Submit</button> <button type="submit" class="btn btn-primary">Importer</button>
</form> </form>
{/block} {/block}

View File

@ -0,0 +1,44 @@
{extends file="admin/layout.tpl"}
{block name=content}
<table class="table">
<thead>
<tr>
<th>id</th>
<th>Nom</th>
<th>Exercices</th>
<th>Résolus</th>
</tr>
</thead>
{if $themes}
<tbody>
{foreach from=$themes item=t}
<tr>
<td>
{$t->id}<br>
<a href="?delete={$t->id}"><span class="glyphicon glyphicon-trash"></span></a>
<a href="/{$SALT_ADMIN}/themes/export?id={$t->id}" role="button"><span class="glyphicon glyphicon-floppy-save"></span> Exporter</a>
</td>
<td><a href="/{$SALT_ADMIN}/{$t->id}-{$t->name}">{$t->name}</a></td>
<td><ul>
{foreach from=$t->get_exercicesOrdered() item=e}
<li><a href="/{$SALT_ADMIN}/{$t->id}-{$t->name}/{$e->id}">{$e->get_name()}</a></li>
{/foreach}
</ul></td>
<td>FIXME</td>
</tr>
{/foreach}
</tbody>
{/if}
<tfoot>
<tr>
<td colspan="6">
<a class="btn btn-success" href="/{$SALT_ADMIN}/themes/import" role="button"><span class="glyphicon glyphicon-cloud-upload"></span> Importer</a>
{if $themes}
<a class="btn btn-danger" href="/{$SALT_ADMIN}/themes/?drop" onclick="return confirm('Êtes-vous sûr de vouloir vider la table des thèmes ainsi que les exercices associés ?')" role="button"><span class="glyphicon glyphicon-floppy-trash"></span> Vider</a>
{/if}
</td>
</tr>
</tfoot>
</table>
{/block}