Fix variables, add AddFlash functions

pull/2487/head
jmontoyaa 9 years ago
parent 96dfd2eadc
commit 3ae340930e
  1. 122
      main/exercise/TestCategory.php
  2. 51
      main/exercise/tests_category.php

@ -69,22 +69,20 @@ class TestCategory
public function addCategoryInBDD()
{
$table = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$v_name = $this->name;
$v_name = Database::escape_string($v_name);
$v_description = $this->description;
$v_description = Database::escape_string($v_description);
$v_name = Database::escape_string($this->name);
$v_description = Database::escape_string($this->description);
// check if name already exists
$sql = "SELECT count(*) AS nb FROM $table
WHERE title = '$v_name' AND c_id=".api_get_course_int_id();
$result_verif = Database::query($sql);
$data_verif = Database::fetch_array($result_verif);
$result = Database::query($sql);
$data_verif = Database::fetch_array($result);
// lets add in BDD if not the same name
if ($data_verif['nb'] <= 0) {
$c_id = api_get_course_int_id();
$params = [
'c_id' => $c_id,
'title' => $v_name,
'description' => $v_description,
'description' => $v_description
];
$new_id = Database::insert($table, $params);
@ -130,9 +128,9 @@ class TestCategory
return false;
} else {
// remove link between question and category
$sql2 = "DELETE FROM $tbl_question_rel_cat
$sql = "DELETE FROM $tbl_question_rel_cat
WHERE category_id = $v_id AND c_id=".$course_id;
Database::query($sql2);
Database::query($sql);
// item_property update
$course_info = api_get_course_info_by_id($course_id);
api_item_property_update(
@ -195,31 +193,25 @@ class TestCategory
return $row['nb'];
}
/**
* @param string $in_color
*/
public function display($in_color="#E0EBF5")
{
echo "<textarea style='background-color:$in_color; width:60%; height:100px;'>";
print_r($this);
echo "</textarea>";
}
/**
* Return an array of all Category objects in the database
* If in_field=="" Return an array of all category objects in the database
* Otherwise, return an array of all in_field value
* in the database (in_field = id or name or description)
*
* @param string $in_field
* @param int $courseId
* @return array
*/
public static function getCategoryListInfo($in_field = "", $courseId = "")
public static function getCategoryListInfo($in_field = '', $courseId = 0)
{
if (empty($courseId) || $courseId=="") {
if (empty($courseId)) {
$courseId = api_get_course_int_id();
}
$table = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$in_field = Database::escape_string($in_field);
$tabres = array();
if ($in_field == "") {
$categories = array();
if ($in_field == '') {
$sql = "SELECT * FROM $table
WHERE c_id=$courseId ORDER BY title ASC";
$res = Database::query($sql);
@ -229,7 +221,7 @@ class TestCategory
$row['title'],
$row['description']
);
$tabres[] = $tmpcat;
$categories[] = $tmpcat;
}
} else {
$sql = "SELECT $in_field FROM $table
@ -237,11 +229,11 @@ class TestCategory
ORDER BY $in_field ASC";
$res = Database::query($sql);
while ($row = Database::fetch_array($res)) {
$tabres[] = $row[$in_field];
$categories[] = $row[$in_field];
}
}
return $tabres;
return $categories;
}
/**
@ -253,10 +245,10 @@ class TestCategory
*
* @return int
*/
public static function getCategoryForQuestion($questionId, $courseId ="")
public static function getCategoryForQuestion($questionId, $courseId = 0)
{
$result = 0;
if (empty($courseId) || $courseId == "") {
if (empty($courseId)) {
$courseId = api_get_course_int_id();
}
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
@ -275,12 +267,17 @@ class TestCategory
/**
* true if question id has a category
*
* @param int $questionId
* @return bool
*/
public static function isQuestionHasCategory($questionId)
{
if (TestCategory::getCategoryForQuestion($questionId) > 0) {
return true;
}
return false;
}
@ -291,13 +288,13 @@ class TestCategory
*/
public static function getCategoryNameForQuestion(
$questionId,
$courseId = ""
$courseId = 0
) {
if (empty($courseId) || $courseId=="") {
if (empty($courseId)) {
$courseId = api_get_course_int_id();
}
$catid = TestCategory::getCategoryForQuestion($questionId, $courseId);
$result = ""; // result
$result = ''; // result
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$catid = intval($catid);
$sql = "SELECT title FROM $table
@ -362,6 +359,7 @@ class TestCategory
if (!empty($categories_in_exercise)) {
$categories_in_exercise = array_unique(array_filter($categories_in_exercise));
}
return $categories_in_exercise;
}
@ -436,10 +434,10 @@ class TestCategory
$nbCatResult = 0;
$quiz = new Exercise();
$quiz->read($exerciseId);
$tabQuestionList = $quiz->selectQuestionList();
$questionList = $quiz->selectQuestionList();
// the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ? ? ?
for ($i=1; $i <= count($tabQuestionList); $i++) {
if (TestCategory::getCategoryForQuestion($tabQuestionList[$i]) == $categoryId) {
for ($i=1; $i <= count($questionList); $i++) {
if (TestCategory::getCategoryForQuestion($questionList[$i]) == $categoryId) {
$nbCatResult++;
}
}
@ -456,9 +454,9 @@ class TestCategory
public static function getNumberOfQuestionRandomByCategory($exerciseId, $in_nbrandom)
{
$nbquestionresult = 0;
$tabcatid = TestCategory::getListOfCategoriesIDForTest($exerciseId);
$categories = TestCategory::getListOfCategoriesIDForTest($exerciseId);
foreach ($tabcatid as $category) {
foreach ($categories as $category) {
if (empty($category['id'])) {
continue;
}
@ -484,16 +482,17 @@ class TestCategory
* @return array
*
*/
public static function getCategoriesIdAndName($courseId = "")
public static function getCategoriesIdAndName($courseId = 0)
{
if (empty($courseId)) {
$courseId = api_get_course_int_id();
}
$tabcatobject = TestCategory::getCategoryListInfo("", $courseId);
$tabresult = array("0"=>get_lang('NoCategorySelected'));
for ($i=0; $i < count($tabcatobject); $i++) {
$tabresult[$tabcatobject[$i]->id] = $tabcatobject[$i]->name;
$categories = TestCategory::getCategoryListInfo('', $courseId);
$tabresult = array('0' => get_lang('NoCategorySelected'));
for ($i=0; $i < count($categories); $i++) {
$tabresult[$categories[$i]->id] = $categories[$i]->name;
}
return $tabresult;
}
@ -530,7 +529,7 @@ class TestCategory
ON (q.id = qrc.question_id )
WHERE
exercice_id = $exerciseId AND
qrc.c_id = ".$courseId."
qrc.c_id = $courseId
";
$res = Database::query($sql);
@ -592,16 +591,18 @@ class TestCategory
*/
public static function getNElementsFromArray($in_tab, $in_number)
{
$tabres = $in_tab;
shuffle($tabres);
if ($in_number < count($tabres)) {
$tabres = array_slice($tabres, 0, $in_number);
$list = $in_tab;
shuffle($list);
if ($in_number < count($list)) {
$list = array_slice($list, 0, $in_number);
}
return $tabres;
return $list;
}
/**
* display the category
* @param int $questionId
* @param int $in_display_category_name
*/
public static function displayCategoryAndTitle($questionId, $in_display_category_name = 1)
{
@ -705,9 +706,9 @@ class TestCategory
{
$res_num_max = 0;
// foreach question
$tabcatid = TestCategory::getListOfCategoriesIDForTest($exerciseId);
$categories = TestCategory::getListOfCategoriesIDForTest($exerciseId);
foreach ($tabcatid as $category) {
foreach ($categories as $category) {
if (empty($category['id'])) {
continue;
}
@ -718,6 +719,7 @@ class TestCategory
$res_num_max = $nbQuestionInThisCat;
}
}
return $res_num_max;
}
@ -779,14 +781,16 @@ class TestCategory
/**
* @return array
*/
function get_all_categories()
public static function get_all_categories()
{
$table = Database::get_course_table(TABLE_QUIZ_CATEGORY);
$sql = "SELECT * FROM $table ORDER BY title ASC";
$res = Database::query($sql);
$array = [];
while ($row = Database::fetch_array($res,'ASSOC')) {
$array[] = $row;
}
return $array;
}
@ -849,6 +853,10 @@ class TestCategory
return $categories;
}
/**
* @param $form
* @param string $action
*/
public function getForm(& $form, $action = 'new')
{
switch($action) {
@ -974,18 +982,20 @@ class TestCategory
/**
* Return true if a category already exists with the same name
* @param string $in_name
* @param string $name
*
* @return bool
*/
public static function category_exists_with_title($in_name)
public static function category_exists_with_title($name)
{
$tab_test_category = TestCategory::getCategoryListInfo("title");
foreach ($tab_test_category as $title) {
if ($title == $in_name) {
$categories = TestCategory::getCategoryListInfo('title');
foreach ($categories as $title) {
if ($title == $name) {
return true;
}
}
return false;
}

@ -19,7 +19,7 @@ $htmlHeadXtra[] = '
}
</script>';
$nameTools = "";
$nameTools = '';
require_once '../inc/global.inc.php';
@ -94,10 +94,8 @@ Display::display_footer();
function importCategoryForm()
{
$form = new FormValidator('import', 'post', api_get_self().'?action=import_category&'.api_get_cidreq());
//$form->addElement('header', get_lang('ImportGroups'));
$form->addElement('file', 'file', get_lang('ImportCSVFileLocation'));
$form->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
//$form->addElement('label', null, Display::url(get_lang('ExampleCSVFile'), api_get_path(WEB_CODE_PATH).'group/example.csv'));
$form->addButtonImport(get_lang('Import'));
return $form;
@ -149,19 +147,19 @@ function edit_category_form($action)
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
$v_id = Security::remove_XSS($values['category_id']);
$v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
$v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
$objcat = new TestCategory($v_id, $v_name, $v_description);
if ($objcat->modifyCategory()) {
Display::addFlash(Display::return_message(get_lang('MofidfyCategoryDone')));
$category = new TestCategory(
$values['category_id'],
$values['category_name'],
$values['category_description']
);
if ($category->modifyCategory()) {
Display::addFlash(Display::return_message(get_lang('Updated')));
} else {
Display::addFlash(Display::return_message(get_lang('ModifyCategoryError')));
Display::addFlash(Display::return_message(get_lang('ModifyCategoryError'), 'error'));
}
}
Security::clear_token();
} else {
display_goback();
$token = Security::get_token();
$form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token));
@ -179,15 +177,14 @@ function edit_category_form($action)
function delete_category_form($action)
{
if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
$category_id = Security::remove_XSS($_GET['category_id']);
$catobject = new TestCategory($category_id);
if ($catobject->removeCategory()) {
Display::display_confirmation_message(get_lang('DeleteCategoryDone'));
$category = new TestCategory($_GET['category_id']);
if ($category->removeCategory()) {
Display::addFlash(Display::return_message(get_lang('DeleteCategoryDone')));
} else {
Display::display_error_message(get_lang('CannotDeleteCategoryError'));
Display::addFlash(Display::return_message(get_lang('CannotDeleteCategoryError'), 'error'));
}
} else {
Display::display_error_message(get_lang('CannotDeleteCategoryError'));
Display::addFlash(Display::return_message(get_lang('CannotDeleteCategoryError'), 'error'));
}
}
@ -219,18 +216,15 @@ function add_category_form($action)
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
$v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
$v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
$objcat = new TestCategory(0, $v_name, $v_description);
if ($objcat->addCategoryInBDD()) {
Display::display_confirmation_message(get_lang('AddCategoryDone'));
$category = new TestCategory(0, $values['category_name'], $values['category_description']);
if ($category->addCategoryInBDD()) {
Display::addFlash(Display::return_message(get_lang('AddCategoryDone')));
} else {
Display::display_confirmation_message(get_lang('AddCategoryNameAlreadyExists'));
Display::addFlash(Display::return_message(get_lang('AddCategoryNameAlreadyExists'), 'warning'));
}
}
Security::clear_token();
} else {
//display_goback();
$token = Security::get_token();
$form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token));
@ -263,12 +257,3 @@ function displayActionBar()
echo "<br/>";
echo "<fieldset><legend>" . get_lang('QuestionCategory') . "</legend></fieldset>";
}
// display goback to category list page link
function display_goback()
{
echo '<div class="actions">';
echo '<a href="' . api_get_self() . '?'.api_get_cidreq().'">' .
Display::return_icon('back.png', get_lang('BackToCategoryList'), array(), 32) . '</a>';
echo '</div>';
}

Loading…
Cancel
Save