Change TestCategory class use getCategory instead of constructor.

pull/2487/head
jmontoyaa 9 years ago
parent deef939d37
commit 10de35b2cd
  1. 110
      main/exercise/TestCategory.php
  2. 4
      main/exercise/exercise.class.php
  3. 29
      main/exercise/tests_category.php
  4. 3
      main/exercise/upload_exercise.php
  5. 2
      main/inc/lib/online.inc.php

@ -17,28 +17,9 @@ class TestCategory
/**
* Constructor of the class Category
* If you give an in_id and no in_name, you get info concerning the category of id=in_id
* otherwise, you've got an category objet avec your in_id, in_name, in_descr
*
* @param int $id
* @param string $name
* @param string $description
*
* @author - Hubert Borderiou
*/
public function __construct($id = 0, $name = '', $description = '')
public function __construct()
{
if ($id != 0 && $name == '') {
$obj = new TestCategory();
$obj->getCategory($id);
$this->id = $obj->id;
$this->name = $obj->name;
$this->description = $obj->description;
} else {
$this->id = $id;
$this->name = $name;
$this->description = $description;
}
}
/**
@ -57,10 +38,15 @@ class TestCategory
if (Database::num_rows($res)) {
$row = Database::fetch_array($res);
$this->id = $row['id'];
$this->name = $row['title'];
$this->description = $row['description'];
return $this;
}
return false;
}
/**
@ -69,11 +55,11 @@ class TestCategory
public function addCategoryInBDD()
{
$table = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$v_name = Database::escape_string($this->name);
$v_description = Database::escape_string($this->description);
$name = Database::escape_string($this->name);
$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();
WHERE title = '$name' AND c_id=".api_get_course_int_id();
$result = Database::query($sql);
$data_verif = Database::fetch_array($result);
// lets add in BDD if not the same name
@ -81,8 +67,8 @@ class TestCategory
$c_id = api_get_course_int_id();
$params = [
'c_id' => $c_id,
'title' => $v_name,
'description' => $v_description
'title' => $name,
'description' => $description
];
$new_id = Database::insert($table, $params);
@ -114,22 +100,23 @@ class TestCategory
* Removes the category from the database
* if there were question in this category, the link between question and category is removed
*/
public function removeCategory()
public function removeCategory($id)
{
$table = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$tbl_question_rel_cat = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$v_id = intval($this->id);
$id = intval($id);
$course_id = api_get_course_int_id();
$sql = "DELETE FROM $table
WHERE id= $v_id AND c_id=".$course_id;
$result = Database::query($sql);
if (Database::affected_rows($result) <= 0) {
return false;
} else {
$category = $this->getCategory($id);
if ($category) {
$sql = "DELETE FROM $table
WHERE id= $id AND c_id=".$course_id;
Database::query($sql);
// remove link between question and category
$sql = "DELETE FROM $tbl_question_rel_cat
WHERE category_id = $v_id AND c_id=".$course_id;
WHERE category_id = $id AND c_id=".$course_id;
Database::query($sql);
// item_property update
$course_info = api_get_course_info_by_id($course_id);
@ -143,6 +130,8 @@ class TestCategory
return true;
}
return false;
}
/**
@ -151,17 +140,19 @@ class TestCategory
public function modifyCategory()
{
$table = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$v_id = intval($this->id);
$v_name = Database::escape_string($this->name);
$v_description = Database::escape_string($this->description);
$sql = "UPDATE $table SET
title = '$v_name',
description = '$v_description'
WHERE id = $v_id AND c_id=".api_get_course_int_id();
$result = Database::query($sql);
if (Database::affected_rows($result) <= 0) {
return false;
} else {
$id = intval($this->id);
$name = Database::escape_string($this->name);
$description = Database::escape_string($this->description);
$cat = $this->getCategory($id);
if ($cat) {
$sql = "UPDATE $table SET
title = '$name',
description = '$description'
WHERE id = $id AND c_id=".api_get_course_int_id();
Database::query($sql);
// item_property update
$course_id = api_get_course_int_id();
$course_info = api_get_course_info_by_id($course_id);
@ -172,9 +163,10 @@ class TestCategory
'TestCategoryModified',
api_get_user_id()
);
return true;
}
return false;
}
/**
@ -212,16 +204,12 @@ class TestCategory
$in_field = Database::escape_string($in_field);
$categories = array();
if ($in_field == '') {
$sql = "SELECT * FROM $table
$sql = "SELECT id FROM $table
WHERE c_id=$courseId ORDER BY title ASC";
$res = Database::query($sql);
while ($row = Database::fetch_array($res)) {
$tmpcat = new TestCategory(
$row['id'],
$row['title'],
$row['description']
);
$categories[] = $tmpcat;
$category = new TestCategory();
$categories[] = $category->getCategory($row['id']);
}
} else {
$sql = "SELECT $in_field FROM $table
@ -401,8 +389,8 @@ class TestCategory
$result = array();
$categories = self::getListOfCategoriesIDForTestObject($exercise_obj);
foreach ($categories as $cat_id) {
$cat = new TestCategory($cat_id);
$cat = (array)$cat;
$cat = new TestCategory();
$cat = (array)$cat->getCategory($cat_id);
$cat['iid'] = $cat['id'];
$cat['title'] = $cat['name'];
$result[$cat['id']] = $cat;
@ -658,8 +646,9 @@ class TestCategory
$tabResult = array();
$tabCatName = array(); // tab of category name
while (list($cat_id, $tabquestion) = each($in_tab)) {
$catTitle = new TestCategory($cat_id);
$tabCatName[$cat_id] = $catTitle->name;
$category = new TestCategory();
$category = $category->getCategory($cat_id);
$tabCatName[$cat_id] = $category->name;
}
reset($in_tab);
// sort table by value, keeping keys as they are
@ -668,6 +657,7 @@ class TestCategory
while (list($key, $val) = each($tabCatName)) {
$tabResult[$key] = $in_tab[$key];
}
return $tabResult;
}
@ -886,7 +876,8 @@ class TestCategory
$form->addElement('select', 'visibility', get_lang('Visibility'), $options);
$script = null;
if (!empty($this->parent_id)) {
$parent_cat = new TestCategory($this->parent_id);
$parent_cat = new TestCategory();
$parent_cat = $parent_cat->getCategory($this->parent_id);
$category_parent_list = array($parent_cat->id => $parent_cat->name);
$script .= '<script>$(function() { $("#parent_id").trigger("addItem",[{"title": "'.$parent_cat->name.'", "value": "'.$parent_cat->id.'"}]); });</script>';
}
@ -1100,7 +1091,8 @@ class TestCategory
$html = '';
foreach ($categories as $category) {
$tmpobj = new TestCategory($category['id']);
$tmpobj = new TestCategory();
$tmpobj = $tmpobj->getCategory($category['id']);
$nb_question = $tmpobj->getCategoryQuestionsNumber();
$rowname = self::protectJSDialogQuote($category['title']);
$nb_question_label = $nb_question == 1 ? $nb_question . ' ' . get_lang('Question') : $nb_question . ' ' . get_lang('Questions');

@ -1019,7 +1019,9 @@ class Exercise
$newCategoryList = array();
foreach ($questions_by_category as $categoryId => $questionList) {
$cat = new TestCategory($categoryId);
$cat = new TestCategory();
$cat = $cat->getCategory($categoryId);
$cat = (array)$cat;
$cat['iid'] = $cat['id'];
//*$cat['name'] = $cat['name'];

@ -72,7 +72,9 @@ switch ($action) {
$categories = Import::csv_reader($_FILES['file']['tmp_name']);
if (!empty($categories)) {
foreach ($categories as $item) {
$cat = new TestCategory(0, $item['title'], $item['description']);
$cat = new TestCategory();
$cat->name = $item['title'];
$cat->description = $item['description'];
$cat->addCategoryInBDD();
}
Display::addFlash(Display::return_message(get_lang('Imported')));
@ -111,8 +113,8 @@ function edit_category_form($action)
$action = Security::remove_XSS($action);
if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
$category_id = intval($_GET['category_id']);
$objcat = new TestCategory($category_id);
$objcat = new TestCategory();
$objcat = $objcat->getCategory($category_id);
$form = new FormValidator(
'note',
'post',
@ -147,12 +149,13 @@ function edit_category_form($action)
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
$category = new TestCategory(
$values['category_id'],
$values['category_name'],
$values['category_description']
);
if ($category->modifyCategory()) {
$category = new TestCategory();
$category = $category->getCategory($values['category_id']);
if ($category) {
$category->name = $values['category_name'];
$category->description = $values['category_description'];
$category->modifyCategory();
Display::addFlash(Display::return_message(get_lang('Updated')));
} else {
Display::addFlash(Display::return_message(get_lang('ModifyCategoryError'), 'error'));
@ -177,8 +180,8 @@ function edit_category_form($action)
function delete_category_form()
{
if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
$category = new TestCategory($_GET['category_id']);
if ($category->removeCategory()) {
$category = new TestCategory();
if ($category->removeCategory($_GET['category_id'])) {
Display::addFlash(Display::return_message(get_lang('DeleteCategoryDone')));
} else {
Display::addFlash(Display::return_message(get_lang('CannotDeleteCategoryError'), 'error'));
@ -216,7 +219,9 @@ function add_category_form($action)
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
$category = new TestCategory(0, $values['category_name'], $values['category_description']);
$category = new TestCategory();
$category->name = $values['category_name'];
$category->description = $values['category_description'];
if ($category->addCategoryInBDD()) {
Display::addFlash(Display::return_message(get_lang('AddCategoryDone')));
} else {

@ -353,7 +353,8 @@ function lp_upload_quiz_action_handling()
$categoryName = $categoryList[$i];
$categoryId = TestCategory::get_category_id_for_title($categoryName, $courseId);
if (empty($categoryId)) {
$category = new TestCategory(null, $categoryName, '');
$category = new TestCategory();
$category->name = $categoryName;
$categoryId = $category->addCategoryInBDD();
}
}

@ -117,7 +117,7 @@ function online_logout($user_id = null, $logout_redirect = false)
LIMIT 0,1";
$q_last_connection = Database::query($sql);
if (Database::num_rows($q_last_connection)>0) {
$i_id_last_connection = Database::result($q_last_connection,0,"login_id");
$i_id_last_connection = Database::result($q_last_connection, 0, "login_id");
}
if (!isset($_SESSION['login_as'])) {

Loading…
Cancel
Save