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

@ -19,7 +19,7 @@ $htmlHeadXtra[] = '
} }
</script>'; </script>';
$nameTools = ""; $nameTools = '';
require_once '../inc/global.inc.php'; require_once '../inc/global.inc.php';
@ -94,10 +94,8 @@ Display::display_footer();
function importCategoryForm() function importCategoryForm()
{ {
$form = new FormValidator('import', 'post', api_get_self().'?action=import_category&'.api_get_cidreq()); $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->addElement('file', 'file', get_lang('ImportCSVFileLocation'));
$form->addRule('file', get_lang('ThisFieldIsRequired'), 'required'); $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')); $form->addButtonImport(get_lang('Import'));
return $form; return $form;
@ -149,19 +147,19 @@ function edit_category_form($action)
$check = Security::check_token('post'); $check = Security::check_token('post');
if ($check) { if ($check) {
$values = $form->exportValues(); $values = $form->exportValues();
$v_id = Security::remove_XSS($values['category_id']); $category = new TestCategory(
$v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER); $values['category_id'],
$v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER); $values['category_name'],
$objcat = new TestCategory($v_id, $v_name, $v_description); $values['category_description']
if ($objcat->modifyCategory()) { );
Display::addFlash(Display::return_message(get_lang('MofidfyCategoryDone'))); if ($category->modifyCategory()) {
Display::addFlash(Display::return_message(get_lang('Updated')));
} else { } else {
Display::addFlash(Display::return_message(get_lang('ModifyCategoryError'))); Display::addFlash(Display::return_message(get_lang('ModifyCategoryError'), 'error'));
} }
} }
Security::clear_token(); Security::clear_token();
} else { } else {
display_goback();
$token = Security::get_token(); $token = Security::get_token();
$form->addElement('hidden', 'sec_token'); $form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token)); $form->setConstants(array('sec_token' => $token));
@ -179,15 +177,14 @@ function edit_category_form($action)
function delete_category_form($action) function delete_category_form($action)
{ {
if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) { if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
$category_id = Security::remove_XSS($_GET['category_id']); $category = new TestCategory($_GET['category_id']);
$catobject = new TestCategory($category_id); if ($category->removeCategory()) {
if ($catobject->removeCategory()) { Display::addFlash(Display::return_message(get_lang('DeleteCategoryDone')));
Display::display_confirmation_message(get_lang('DeleteCategoryDone'));
} else { } else {
Display::display_error_message(get_lang('CannotDeleteCategoryError')); Display::addFlash(Display::return_message(get_lang('CannotDeleteCategoryError'), 'error'));
} }
} else { } 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'); $check = Security::check_token('post');
if ($check) { if ($check) {
$values = $form->exportValues(); $values = $form->exportValues();
$v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER); $category = new TestCategory(0, $values['category_name'], $values['category_description']);
$v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER); if ($category->addCategoryInBDD()) {
$objcat = new TestCategory(0, $v_name, $v_description); Display::addFlash(Display::return_message(get_lang('AddCategoryDone')));
if ($objcat->addCategoryInBDD()) {
Display::display_confirmation_message(get_lang('AddCategoryDone'));
} else { } else {
Display::display_confirmation_message(get_lang('AddCategoryNameAlreadyExists')); Display::addFlash(Display::return_message(get_lang('AddCategoryNameAlreadyExists'), 'warning'));
} }
} }
Security::clear_token(); Security::clear_token();
} else { } else {
//display_goback();
$token = Security::get_token(); $token = Security::get_token();
$form->addElement('hidden', 'sec_token'); $form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token)); $form->setConstants(array('sec_token' => $token));
@ -263,12 +257,3 @@ function displayActionBar()
echo "<br/>"; echo "<br/>";
echo "<fieldset><legend>" . get_lang('QuestionCategory') . "</legend></fieldset>"; 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