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

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

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

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

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

Loading…
Cancel
Save