Adding a second parameter to read() in order to speed up the question list

skala
Julio Montoya 12 years ago
parent 45218bb7d1
commit 2ac36752b0
  1. 75
      main/exercice/exercise.class.php
  2. 41
      main/exercice/testcategory.class.php
  3. 4
      main/inc/ajax/model.ajax.php

@ -72,6 +72,7 @@ class Exercise
public $mediaList;
public $loadQuestionAJAX = false;
public $emailNotificationTemplate = null;
public $countQuestions = 0;
/**
* Constructor of the class
@ -120,7 +121,7 @@ class Exercise
* @param int $id - exercise ID
* @return boolean - true if exercise exists, otherwise false
*/
public function read($id)
public function read($id, $parseQuestionList = true)
{
if (empty($this->course_id)) {
return false;
@ -189,10 +190,13 @@ class Exercise
// Control time
$this->expired_time = $object->expired_time;
// Checking if question_order is correctly set
$this->questionList = $this->selectQuestionList(true);
if ($parseQuestionList) {
$this->setMediaList();
// Checking if question_order is correctly set
$this->questionList = $this->selectQuestionList(true);
$this->setMediaList();
}
//overload questions list with recorded questions list
//load questions only for exercises of type 'one question per page'
@ -496,23 +500,25 @@ class Exercise
{
if (!empty($this->id)) {
$category_list = Testcategory::getListOfCategoriesNameForTest($this->id, false);
//$category_list = Testcategory::getListOfCategoriesIDForTestObject($this);
$TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
$sql = "SELECT q.iid
FROM $TBL_EXERCICE_QUESTION e INNER JOIN $TBL_QUESTIONS q
ON (e.question_id = q.iid AND e.c_id = ".$this->course_id.")
ON (e.question_id = q.iid AND e.c_id = ".$this->course_id." )
WHERE e.exercice_id = '".Database::escape_string($this->id)."'
ORDER BY question_order";
$limitCondition = null;
if (!empty($start) && !empty($limit)) {
if (isset($start) && isset($limit)) {
$start = intval($start);
$limit = intval($limit);
$limitCondition = " LIMIT $start, $limit";
}
$sql .= $limitCondition;
$result = Database::query($sql);
$questions = array();
if (Database::num_rows($result)) {
@ -550,6 +556,28 @@ class Exercise
}
}
/**
* @return int
*/
public function getQuestionCount()
{
$TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
$sql = "SELECT count(e.iid) as count
FROM $TBL_EXERCICE_QUESTION e INNER JOIN $TBL_QUESTIONS q
ON (e.question_id = q.iid)
WHERE e.c_id = {$this->course_id} AND e.exercice_id = ".Database::escape_string($this->id);
$result = Database::query($sql);
$count = 0;
if (Database::num_rows($result)) {
$row = Database::fetch_array($result);
$count = $row['count'];
}
return $count;
}
/**
* Gets the question list ordered by the question_order setting (drag and drop)
* @return array
@ -563,18 +591,19 @@ class Exercise
// Getting question_order to verify that the question list is correct and all question_order's were set
$sql = "SELECT DISTINCT e.question_order
FROM $TBL_EXERCICE_QUESTION e INNER JOIN $TBL_QUESTIONS q
FROM $TBL_EXERCICE_QUESTION e INNER JOIN $TBL_QUESTIONS q
ON (e.question_id = q.iid)
WHERE e.exercice_id = ".Database::escape_string($this->id);
WHERE e.c_id = {$this->course_id} AND e.exercice_id = ".Database::escape_string($this->id);
$result = Database::query($sql);
$count_question_orders = Database::num_rows($result);
// Getting question list from the order (question list drag n drop interface ).
$sql = "SELECT e.question_id, e.question_order
FROM $TBL_EXERCICE_QUESTION e INNER JOIN $TBL_QUESTIONS q
FROM $TBL_EXERCICE_QUESTION e INNER JOIN $TBL_QUESTIONS q
ON (e.question_id= q.iid)
WHERE e.exercice_id = '".Database::escape_string($this->id)."'
WHERE e.c_id = {$this->course_id} AND e.exercice_id = '".Database::escape_string($this->id)."'
ORDER BY question_order";
$result = Database::query($sql);
@ -6102,4 +6131,28 @@ class Exercise
return $ribbon;
}
public function getQuestionWithCategories()
{
$categoryTable = Database::get_course_table(TABLE_QUIZ_CATEGORY);
$categoryRelTable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
$sql = "SELECT DISTINCT cat.*
FROM $TBL_EXERCICE_QUESTION e INNER JOIN $TBL_QUESTIONS q
ON (e.question_id = q.iid and e.c_id = {$this->course_id})
INNER JOIN $categoryRelTable catRel
ON (catRel.question_id = e.question_id)
INNER JOIN $categoryTable cat
ON (cat.iid = catRel.category_id)
WHERE e.c_id = {$this->course_id} AND e.exercice_id = ".Database::escape_string($this->id);
$result = Database::query($sql);
$categoriesInExercise = array();
if (Database::num_rows($result)) {
$categoriesInExercise = Database::store_result($result, 'ASSOC');
}
return $categoriesInExercise;
}
}

@ -70,6 +70,10 @@ class Testcategory
*/
public function getCategory($id)
{
if (empty($id)) {
return false;
}
$t_cattable = Database::get_course_table(TABLE_QUIZ_CATEGORY);
$id = Database::escape_string($id);
$sql = "SELECT * FROM $t_cattable WHERE iid = $id ";
@ -460,6 +464,7 @@ class Testcategory
return $result;
}
/**
* return the list of different categories ID for a test
* @param int exercise id
@ -472,9 +477,18 @@ class Testcategory
// parcourir les questions d'un test, recup les categories uniques dans un tableau
$categories_in_exercise = array();
$exercise = new Exercise();
$exercise->setCategoriesGrouping($grouped_by_category);
$exercise->read($exercise_id);
$question_list = $exercise->selectQuestionList();
$exercise->read($exercise_id, false);
$categories_in_exercise = $exercise->getQuestionWithCategories();
$categories = array();
if (!empty($categories_in_exercise)) {
foreach($categories_in_exercise as $category) {
$category['id'] = $category['iid'];
$categories[$category['iid']] = $category;
}
}
return $categories;
/*
// the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ???
foreach ($question_list as $question_id) {
$category_list = Testcategory::getCategoryForQuestion($question_id);
@ -485,7 +499,7 @@ class Testcategory
if (!empty($categories_in_exercise)) {
$categories_in_exercise = array_unique(array_filter($categories_in_exercise));
}
return $categories_in_exercise;
return $categories_in_exercise;*/
}
public static function getListOfCategoriesIDForTestObject($exercise_obj)
@ -517,16 +531,19 @@ class Testcategory
*
* @author function rewrote by jmontoya
*/
public static function getListOfCategoriesNameForTest($exercise_id, $grouped_by_category = true) {
public static function getListOfCategoriesNameForTest($exercise_id, $grouped_by_category = true)
{
$result = array();
$categories = self::getListOfCategoriesIDForTest($exercise_id, $grouped_by_category);
foreach ($categories as $cat_id) {
$cat = new Testcategory($cat_id);
if (!empty($cat->id)) {
$result[$cat->id] = array(
'title' => $cat->name,
'parent_id' => $cat->parent_id,
'c_id' => $cat->c_id
foreach ($categories as $catInfo) {
$categoryId = $catInfo['iid'];
///$cat = new Testcategory($cat_id);
if (!empty($categoryId)) {
$result[$categoryId] = array(
'title' => $catInfo['title'],
'parent_id' => $catInfo['parent_id'],
'c_id' => $catInfo['c_id']
);
}
}

@ -176,8 +176,8 @@ switch ($action) {
require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
$exerciseId = isset($_REQUEST['exerciseId']) ? $_REQUEST['exerciseId'] : null;
$exercise = new Exercise(api_get_course_int_id());
$exercise->read($exerciseId);
$count = $exercise->selectNbrQuestions();
$exercise->read($exerciseId, false);
$count = $exercise->getQuestionCount();
break;
case 'get_group_reporting':
$course_id = isset($_REQUEST['course_id']) ? $_REQUEST['course_id'] : null;

Loading…
Cancel
Save