@ -8,7 +8,7 @@ class Testcategory {
public $id;
public $id;
public $name;
public $name;
public $description;
public $description;
/**
/**
* Constructor of the class Category
* Constructor of the class Category
* @author - Hubert Borderiou
* @author - Hubert Borderiou
@ -29,7 +29,7 @@ class Testcategory {
$this->description = $in_description;
$this->description = $in_description;
}
}
}
}
/** return the Testcategory object with id=in_id
/** return the Testcategory object with id=in_id
*/
*/
function getCategory($in_id) {
function getCategory($in_id) {
@ -45,7 +45,7 @@ class Testcategory {
$this->description = $row['description'];
$this->description = $row['description'];
}
}
}
}
/** add Testcategory in the database if name doesn't already exists
/** add Testcategory in the database if name doesn't already exists
*/
*/
function addCategoryInBDD() {
function addCategoryInBDD() {
@ -67,13 +67,13 @@ class Testcategory {
}
}
else {
else {
return false;
return false;
}
}
}
}
/**
/**
* Removes the category with id=in_id from the database if no question use this category
* Removes the category with id=in_id from the database if no question use this category
* @todo I'm removing the $in_id parameter because it seems that you're using $this->id instead of $in_id after confirmation delete this
* @todo I'm removing the $in_id parameter because it seems that you're using $this->id instead of $in_id after confirmation delete this
* jmontoya
* jmontoya
*/
*/
//function removeCategory($in_id) {
//function removeCategory($in_id) {
function removeCategory() {
function removeCategory() {
@ -85,11 +85,11 @@ class Testcategory {
return false;
return false;
} else {
} else {
return true;
return true;
}
}
}
}
/** modify category name or description of category with id=in_id
/** modify category name or description of category with id=in_id
*/
*/
//function modifyCategory($in_id, $in_name, $in_description) {
//function modifyCategory($in_id, $in_name, $in_description) {
function modifyCategory() {
function modifyCategory() {
@ -106,8 +106,8 @@ class Testcategory {
return true;
return true;
}
}
}
}
/**
/**
* Gets the number of question of category id=in_id
* Gets the number of question of category id=in_id
* @todo I'm removing the $in_id parameter because it seems that you're using $this->id instead of $in_id after confirmation delete this
* @todo I'm removing the $in_id parameter because it seems that you're using $this->id instead of $in_id after confirmation delete this
* jmontoya
* jmontoya
@ -121,15 +121,15 @@ class Testcategory {
$row = Database::fetch_array($res);
$row = Database::fetch_array($res);
return $row['nb'];
return $row['nb'];
}
}
function display($in_color="#E0EBF5") {
function display($in_color="#E0EBF5") {
echo "< textarea style = 'background-color:$in_color; width:60%; height:100px;' > ";
echo "< textarea style = 'background-color:$in_color; width:60%; height:100px;' > ";
print_r($this);
print_r($this);
echo "< / textarea > ";
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 in the database (in_field = id or name or description)
Otherwise, return an array of all in_field value in the database (in_field = id or name or description)
@ -137,8 +137,8 @@ class Testcategory {
public static function getCategoryListInfo($in_field="", $in_courseid="") {
public static function getCategoryListInfo($in_field="", $in_courseid="") {
if (empty($in_courseid) || $in_courseid=="") {
if (empty($in_courseid) || $in_courseid=="") {
$in_courseid = api_get_course_int_id();
$in_courseid = api_get_course_int_id();
}
}
$t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$in_field = Database::escape_string($in_field);
$in_field = Database::escape_string($in_field);
$tabres = array();
$tabres = array();
if ($in_field=="") {
if ($in_field=="") {
@ -158,7 +158,7 @@ class Testcategory {
}
}
return $tabres;
return $tabres;
}
}
/**
/**
Return the testcategory id for question with question_id = $in_questionid
Return the testcategory id for question with question_id = $in_questionid
@ -170,43 +170,43 @@ class Testcategory {
if (empty($in_courseid) || $in_courseid=="") {
if (empty($in_courseid) || $in_courseid=="") {
$in_courseid = api_get_course_int_id();
$in_courseid = api_get_course_int_id();
}
}
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$question_id = Database::escape_string($in_questionid);
$question_id = Database::escape_string($in_questionid);
$sql = "SELECT category_id FROM $t_cattable WHERE question_id = '$question_id' AND c_id = $in_courseid";
$sql = "SELECT category_id FROM $t_cattable WHERE question_id = '$question_id' AND c_id = $in_courseid";
$res = Database::query($sql);
$res = Database::query($sql);
if (Database::num_rows($res) > 0) {
if (Database::num_rows($res) > 0) {
while ($row = Database::fetch_array($res)) {
while ($row = Database::fetch_array($res)) {
$result[] = $row['category_id'];
$result[] = $row['category_id'];
}
}
}
}
return $result;
return $result;
}
}
public static function getCategoryNamesForQuestion($in_questionid, $in_courseid = null, $display_into_labels = true) {
public static function getCategoryNamesForQuestion($in_questionid, $in_courseid = null, $display_into_labels = true) {
$result = array(); // result
$result = array(); // result
if (empty($in_courseid) || $in_courseid=="") {
if (empty($in_courseid) || $in_courseid=="") {
$in_courseid = api_get_course_int_id();
$in_courseid = api_get_course_int_id();
}
}
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$table_category = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$table_category = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$question_id = Database::escape_string($in_questionid);
$question_id = Database::escape_string($in_questionid);
$sql = "SELECT c.title FROM $t_cattable qc INNER JOIN $table_category c
$sql = "SELECT c.title FROM $t_cattable qc INNER JOIN $table_category c
ON (qc.category_id = c.id AND qc.c_id = $in_courseid AND c.c_id = $in_courseid)
ON (qc.category_id = c.id AND qc.c_id = $in_courseid AND c.c_id = $in_courseid)
WHERE question_id = '$question_id' ";
WHERE question_id = '$question_id' ";
$res = Database::query($sql);
$res = Database::query($sql);
if (Database::num_rows($res) > 0) {
if (Database::num_rows($res) > 0) {
while ($row = Database::fetch_array($res)) {
while ($row = Database::fetch_array($res)) {
$result[] = $row['title'];
$result[] = $row['title'];
}
}
}
}
if ($display_into_labels) {
if ($display_into_labels) {
$html = self::draw_category_label($result, 'header');
$html = self::draw_category_label($result, 'header');
return $html;
return $html;
}
}
return $result;
return $result;
}
}
/**
/**
* true if question id has a category
* true if question id has a category
*/
*/
@ -217,8 +217,8 @@ class Testcategory {
}
}
return false;
return false;
}
}
/**
/**
* @todo fix this
* @todo fix this
Return the category name for question with question_id = $in_questionid
Return the category name for question with question_id = $in_questionid
@ -230,11 +230,11 @@ class Testcategory {
$course_id = api_get_course_int_id();
$course_id = api_get_course_int_id();
}
}
$course_id = intval($course_id);
$course_id = intval($course_id);
$category_list = Testcategory::getCategoryForQuestion($question_id, $course_id);
$category_list = Testcategory::getCategoryForQuestion($question_id, $course_id);
$result = array(); // result
$result = array(); // result
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$catid = Database::escape_string($catid);
$catid = Database::escape_string($catid);
$sql = "SELECT title FROM $t_cattable WHERE id='$catid' AND c_id = $course_id";
$sql = "SELECT title FROM $t_cattable WHERE id='$catid' AND c_id = $course_id";
$res = Database::query($sql);
$res = Database::query($sql);
@ -244,7 +244,7 @@ class Testcategory {
}
}
return $result;
return $result;
}
}
/**
/**
* return the list of differents categories ID for a test
* return the list of differents categories ID for a test
* input : test_id
* input : test_id
@ -257,19 +257,19 @@ class Testcategory {
$quiz = new Exercise();
$quiz = new Exercise();
$quiz->read($exercise_id);
$quiz->read($exercise_id);
$question_list = $quiz->selectQuestionList();
$question_list = $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($question_list); $i++) {
for ($i=1; $i < = count($question_list); $i++) {
$category_list = Testcategory::getCategoryForQuestion($question_list[$i]);
$category_list = Testcategory::getCategoryForQuestion($question_list[$i]);
if (!empty($category_list)) {
if (!empty($category_list)) {
$categories_in_exercise = array_merge($categories_in_exercise, $category_list);
$categories_in_exercise = array_merge($categories_in_exercise, $category_list);
}
}
}
}
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;
}
}
/**
/**
* return the list of differents categories NAME for a test
* return the list of differents categories NAME for a test
* input : test_id
* input : test_id
@ -286,7 +286,7 @@ class Testcategory {
}
}
return $tabcatName;
return $tabcatName;
}
}
/**
/**
* return the number of differents categories for a test
* return the number of differents categories for a test
* input : test_id
* input : test_id
@ -295,8 +295,8 @@ class Testcategory {
*/
*/
public static function getNumberOfCategoriesForTest($in_testid) {
public static function getNumberOfCategoriesForTest($in_testid) {
return count(Testcategory::getListOfCategoriesIDForTest($in_testid));
return count(Testcategory::getListOfCategoriesIDForTest($in_testid));
}
}
/**
/**
* return the number of question of a category id in a test
* return the number of question of a category id in a test
* input : test_id, category_id
* input : test_id, category_id
@ -310,14 +310,14 @@ class Testcategory {
$question_list = $exercise->selectQuestionList();
$question_list = $exercise->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($question_list); $i++) {
for ($i=1; $i < = count($question_list); $i++) {
$category_in_question = Testcategory::getCategoryForQuestion($question_list[$i]);
$category_in_question = Testcategory::getCategoryForQuestion($question_list[$i]);
if (in_array($category_id, $category_in_question)) {
if (in_array($category_id, $category_in_question)) {
$number_questions_in_category++;
$number_questions_in_category++;
}
}
}
}
return $number_questions_in_category;
return $number_questions_in_category;
}
}
/**
/**
* return the number of question for a test using random by category
* return the number of question for a test using random by category
* input : test_id, number of random question (min 1)
* input : test_id, number of random question (min 1)
@ -327,8 +327,8 @@ class Testcategory {
public static function getNumberOfQuestionRandomByCategory($exercise_id, $in_nbrandom) {
public static function getNumberOfQuestionRandomByCategory($exercise_id, $in_nbrandom) {
$nbquestionresult = 0;
$nbquestionresult = 0;
$list_categories = Testcategory::getListOfCategoriesIDForTest($exercise_id);
$list_categories = Testcategory::getListOfCategoriesIDForTest($exercise_id);
if (!empty($list_categories)) {
if (!empty($list_categories)) {
for ($i=0; $i < count ( $ list_categories ) ; $ i + + ) {
for ($i=0; $i < count ( $ list_categories ) ; $ i + + ) {
if ($list_categories[$i] > 0) { // 0 = no category for this question
if ($list_categories[$i] > 0) { // 0 = no category for this question
$nbQuestionInThisCat = Testcategory::getNumberOfQuestionsInCategoryForTest($exercise_id, $list_categories[$i]);
$nbQuestionInThisCat = Testcategory::getNumberOfQuestionsInCategoryForTest($exercise_id, $list_categories[$i]);
@ -343,8 +343,8 @@ class Testcategory {
}
}
return $nbquestionresult;
return $nbquestionresult;
}
}
/**
/**
* Return an array (id=>name)
* Return an array (id=>name)
* tabresult[0] = get_lang('NoCategory');
* tabresult[0] = get_lang('NoCategory');
@ -353,7 +353,7 @@ class Testcategory {
static function getCategoriesIdAndName($in_courseid = "") {
static function getCategoriesIdAndName($in_courseid = "") {
if (empty($in_courseid) || $in_courseid=="") {
if (empty($in_courseid) || $in_courseid=="") {
$in_courseid = api_get_course_int_id();
$in_courseid = api_get_course_int_id();
}
}
$tabcatobject = Testcategory::getCategoryListInfo("", $in_courseid);
$tabcatobject = Testcategory::getCategoryListInfo("", $in_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 ( $ tabcatobject ) ; $ i + + ) {
@ -361,7 +361,7 @@ class Testcategory {
}
}
return $tabresult;
return $tabresult;
}
}
/**
/**
* return an array of question_id for each category
* return an array of question_id for each category
* tabres[0] = array of question id with category id = 0 (i.e. no category)
* tabres[0] = array of question id with category id = 0 (i.e. no category)
@ -369,11 +369,11 @@ class Testcategory {
* In this version, a question has 0 or 1 category
* In this version, a question has 0 or 1 category
*/
*/
static function getQuestionsByCat($in_exerciceId) {
static function getQuestionsByCat($in_exerciceId) {
$tabres = array();
$tabres = array();
$TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$in_exerciceId = intval($in_exerciceId);
$in_exerciceId = intval($in_exerciceId);
$sql = "SELECT qrc.question_id, qrc.category_id FROM $TBL_QUESTION_REL_CATEGORY qrc, $TBL_EXERCICE_QUESTION eq
$sql = "SELECT qrc.question_id, qrc.category_id FROM $TBL_QUESTION_REL_CATEGORY qrc, $TBL_EXERCICE_QUESTION eq
WHERE exercice_id=$in_exerciceId AND eq.question_id=qrc.question_id AND eq.c_id=".api_get_course_int_id()." AND eq.c_id=qrc.c_id ORDER BY category_id, question_id";
WHERE exercice_id=$in_exerciceId AND eq.question_id=qrc.question_id AND eq.c_id=".api_get_course_int_id()." AND eq.c_id=qrc.c_id ORDER BY category_id, question_id";
$res = Database::query($sql);
$res = Database::query($sql);
while ($data = Database::fetch_array($res)) {
while ($data = Database::fetch_array($res)) {
@ -384,8 +384,8 @@ class Testcategory {
}
}
return $tabres;
return $tabres;
}
}
/**
/**
* return a tab of $in_number random elements of $in_tab
* return a tab of $in_number random elements of $in_tab
*/
*/
@ -397,7 +397,7 @@ class Testcategory {
}
}
return $tabres;
return $tabres;
}
}
/**
/**
* Display signs [+] and/or (>0) after question title if question has options
* Display signs [+] and/or (>0) after question title if question has options
* scoreAlwaysPositive and/or uncheckedMayScore
* scoreAlwaysPositive and/or uncheckedMayScore
@ -410,7 +410,7 @@ class Testcategory {
echo "< span style = 'font-size:75%' > [+]< / span > ";
echo "< span style = 'font-size:75%' > [+]< / span > ";
}
}
}
}
/**
/**
* sortTabByBracketLabel ($tabCategoryQuestions)
* sortTabByBracketLabel ($tabCategoryQuestions)
* key of $tabCategoryQuestions are the categopy id (0 for not in a category)
* key of $tabCategoryQuestions are the categopy id (0 for not in a category)
@ -433,7 +433,7 @@ class Testcategory {
}
}
return $tabResult;
return $tabResult;
}
}
/**
/**
* return total score for test exe_id for all question in the category $in_cat_id for user
* return total score for test exe_id for all question in the category $in_cat_id for user
* If no question for this category, return ""
* If no question for this category, return ""
@ -444,8 +444,8 @@ class Testcategory {
$in_cat_id = intval($in_cat_id);
$in_cat_id = intval($in_cat_id);
$in_exe_id = intval($in_exe_id);
$in_exe_id = intval($in_exe_id);
$in_user_id = intval($in_user_id);
$in_user_id = intval($in_user_id);
$query = "SELECT DISTINCT marks, exe_id, user_id, ta.question_id, category_id FROM $tbl_track_attempt ta , $tbl_question_rel_category qrc
$query = "SELECT DISTINCT marks, exe_id, user_id, ta.question_id, category_id FROM $tbl_track_attempt ta , $tbl_question_rel_category qrc
WHERE ta.question_id=qrc.question_id AND qrc.category_id=$in_cat_id AND exe_id=$in_exe_id AND user_id=$in_user_id";
WHERE ta.question_id=qrc.question_id AND qrc.category_id=$in_cat_id AND exe_id=$in_exe_id AND user_id=$in_user_id";
$res = Database::query($query);
$res = Database::query($query);
$totalcatscore = "";
$totalcatscore = "";
@ -454,7 +454,7 @@ class Testcategory {
}
}
return $totalcatscore;
return $totalcatscore;
}
}
/**
/**
* return the number max of question in a category
* return the number max of question in a category
@ -475,7 +475,7 @@ class Testcategory {
}
}
return $res_num_max;
return $res_num_max;
}
}
public static function getCategoryListName($course_id = null) {
public static function getCategoryListName($course_id = null) {
$category_list = self::getCategoryListInfo(null, $course_id);
$category_list = self::getCategoryListInfo(null, $course_id);
$category_name_list = array();
$category_name_list = array();
@ -483,26 +483,26 @@ class Testcategory {
foreach($category_list as $category) {
foreach($category_list as $category) {
$category_name_list[$category->id] = $category->name;
$category_name_list[$category->id] = $category->name;
}
}
}
}
return $category_name_list;
return $category_name_list;
}
}
public static function return_category_labels($category_list, $all_categories) {
public static function return_category_labels($category_list, $all_categories) {
$category_list_to_render = array();
$category_list_to_render = array();
foreach ($category_list as $category_id) {
foreach ($category_list as $category_id) {
$category_name = null;
$category_name = null;
if (!isset($all_categories[$category_id])) {
if (!isset($all_categories[$category_id])) {
$category_name = get_lang('Untitled');
$category_name = get_lang('Untitled');
} else {
} else {
$category_name = cut($all_categories[$category_id], 15);
$category_name = cut($all_categories[$category_id], 15);
}
}
$category_list_to_render[] = $category_name;
$category_list_to_render[] = $category_name;
}
}
$html = self::draw_category_label($category_list_to_render, 'label');
$html = self::draw_category_label($category_list_to_render, 'label');
return $html;
return $html;
}
}
static function draw_category_label($category_list, $type = 'label') {
static function draw_category_label($category_list, $type = 'label') {
$new_category_list = array();
$new_category_list = array();
foreach ($category_list as $category_name) {
foreach ($category_list as $category_name) {
switch ($type) {
switch ($type) {
@ -514,7 +514,7 @@ class Testcategory {
break;
break;
}
}
}
}
$html = null;
$html = null;
if (!empty($new_category_list)) {
if (!empty($new_category_list)) {
switch ($type) {
switch ($type) {
@ -524,11 +524,11 @@ class Testcategory {
case 'header':
case 'header':
$html = Display::page_subheader3(get_lang('Category').': '.implode(', ', $new_category_list));
$html = Display::page_subheader3(get_lang('Category').': '.implode(', ', $new_category_list));
}
}
}
}
return $html;
return $html;
}
}
/**
/**
* Returns a category summary report
* Returns a category summary report
* @params int exercise id
* @params int exercise id
* @params array prefilled array with the category_id, score, and weight example: array(1 => array('score' => '10', 'total' => 20));
* @params array prefilled array with the category_id, score, and weight example: array(1 => array('score' => '10', 'total' => 20));
@ -536,37 +536,37 @@ class Testcategory {
public static function get_stats_table_by_attempt($exercise_id, $category_list = array()) {
public static function get_stats_table_by_attempt($exercise_id, $category_list = array()) {
if (empty($category_list)) {
if (empty($category_list)) {
return null;
return null;
}
}
$category_name_list = Testcategory::getListOfCategoriesNameForTest($exercise_id);
$category_name_list = Testcategory::getListOfCategoriesNameForTest($exercise_id);
$table = new HTML_Table(array('class' => 'data_table'));
$table = new HTML_Table(array('class' => 'data_table'));
$table->setHeaderContents(0, 0, get_lang('Categories'));
$table->setHeaderContents(0, 0, get_lang('Categories'));
$table->setHeaderContents(0, 1, get_lang('AbsoluteScore'));
$table->setHeaderContents(0, 1, get_lang('AbsoluteScore'));
$table->setHeaderContents(0, 2, get_lang('RelativeScore'));
$table->setHeaderContents(0, 2, get_lang('RelativeScore'));
$row = 1;
$row = 1;
$none_category = array();
$none_category = array();
if (isset($category_list['none'])) {
if (isset($category_list['none'])) {
$none_category = $category_list['none'];
$none_category = $category_list['none'];
unset($category_list['none']);
unset($category_list['none']);
}
}
$total = array();
$total = array();
if (isset($category_list['total'])) {
if (isset($category_list['total'])) {
$total = $category_list['total'];
$total = $category_list['total'];
unset($category_list['total']);
unset($category_list['total']);
}
}
if (count($category_list) > 1) {
if (count($category_list) > 1) {
foreach ($category_list as $category_id => $category_item) {
foreach ($category_list as $category_id => $category_item) {
$table->setCellContents($row, 0, $category_name_list[$category_id]);
$table->setCellContents($row, 0, $category_name_list[$category_id]);
$table->setCellContents($row, 1, show_score($category_item['score'], $category_item['total'], false));
$table->setCellContents($row, 1, show_score($category_item['score'], $category_item['total'], false));
$table->setCellContents($row, 2, show_score($category_item['score'], $category_item['total'], true, false, true));
$table->setCellContents($row, 2, show_score($category_item['score'], $category_item['total'], true, false, true));
$row++;
$row++;
}
}
if (!empty($none_category)) {
if (!empty($none_category)) {
$table->setCellContents($row, 0, get_lang('None'));
$table->setCellContents($row, 0, get_lang('None'));
$table->setCellContents($row, 1, show_score($none_category['score'], $none_category['total'], false));
$table->setCellContents($row, 1, show_score($none_category['score'], $none_category['total'], false));
$table->setCellContents($row, 2, show_score($none_category['score'], $none_category['total'], true, false, true));
$table->setCellContents($row, 2, show_score($none_category['score'], $none_category['total'], true, false, true));
$row++;
$row++;
}
}
@ -579,8 +579,8 @@ class Testcategory {
}
}
return null;
return null;
}
}
}
}
endif;
endif;