Skip to content

Exercise: Show question usage across courses/sessions in edition view - refs BT#22575 #6266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions public/main/exercise/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,7 @@ function ($acc, $questionId) {
echo '</div>';
} else {
require 'question_admin.inc.php';
// @todo
//ExerciseLib::showTestsWhereQuestionIsUsed($objQuestion->iid, $objExercise->getId());
ExerciseLib::showTestsWhereQuestionIsUsed($objQuestion->iid, $objExercise->getId());
}
}
}
Expand Down
44 changes: 39 additions & 5 deletions public/main/exercise/question_pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Chamilo\CourseBundle\Entity\CQuizQuestion;
use Chamilo\CourseBundle\Entity\CQuizRelQuestion;
use ChamiloSession as Session;
use Doctrine\ORM\NoResultException;
use Knp\Component\Pager\Paginator;
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
use Chamilo\CoreBundle\Component\Utils\ObjectIcon;
Expand Down Expand Up @@ -46,7 +47,7 @@

// by default when we go to the page for the first time, we select the current course
if (!isset($_GET['selected_course']) && !isset($_GET['exerciseId'])) {
$selected_course = api_get_course_int_id();
$selected_course = -1;
}

$_course = api_get_course_info();
Expand Down Expand Up @@ -619,6 +620,40 @@ function getQuestions(
->setParameter('exerciseId', $exerciseId);
} elseif ($exerciseId == -1) {
$qb->andWhere($qb->expr()->isNull('qr.quiz'));
} elseif ($selectedCourse > 0) {
$exerciseList = ExerciseLib::get_all_exercises_for_course_id(
$selectedCourse,
$sessionId ?? 0,
true
);

if (!empty($exerciseList)) {
$exerciseIds = array_column($exerciseList, 'iid');
$qb->andWhere($qb->expr()->in('qr.quiz', ':exerciseIds'))
->setParameter('exerciseIds', $exerciseIds);
} else {
return $getCount ? 0 : [];
}
} elseif ($selectedCourse == -1 && $sessionId > 0) {
$sessionCourses = SessionManager::get_course_list_by_session_id($sessionId);
$courseIds = array_keys($sessionCourses);

if (!empty($courseIds)) {
$exerciseIds = [];
foreach ($courseIds as $courseId) {
$exercises = ExerciseLib::get_all_exercises_for_course_id($courseId, $sessionId, true);
$exerciseIds = array_merge($exerciseIds, array_column($exercises, 'iid'));
}

if (!empty($exerciseIds)) {
$qb->andWhere($qb->expr()->in('qr.quiz', ':exerciseIds'))
->setParameter('exerciseIds', $exerciseIds);
} else {
return $getCount ? 0 : [];
}
} else {
return $getCount ? 0 : [];
}
}

if ($courseCategoryId > 0) {
Expand Down Expand Up @@ -661,7 +696,7 @@ function getQuestions(
$qb->select('COUNT(qq.iid)');
try {
return (int) $qb->getQuery()->getSingleScalarResult();
} catch (\Doctrine\ORM\NoResultException $e) {
} catch (NoResultException $e) {
return 0;
}
} else {
Expand All @@ -673,14 +708,13 @@ function getQuestions(

$questions = [];
foreach ($results as $result) {
$question = [
$questions[] = [
'iid' => $result['id'],
'question' => $result['question'],
'type' => $result['type'],
'level' => $result['level'],
'exerciseId' => $result['exerciseId'],
];
$questions[] = $question;
}

return $questions;
Expand Down Expand Up @@ -803,7 +837,7 @@ function getQuestions(
// IN A TEST - NOT IN THE COURSE
$actionLabel = get_lang('Re-use a copy inside the current test');
$actionIcon1 = 'clone';
$actionIcon2 = '';
$actionIcon2 = 'add';
$questionTagA = 0;

if ($selected_course == api_get_course_int_id()) {
Expand Down
108 changes: 45 additions & 63 deletions public/main/inc/lib/exercise.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1600,78 +1600,60 @@ class="window window_left_question window{$questionId}_question">
}

/**
* Get an HTML string with the list of exercises where the given question
* is being used.
*
* @param int $questionId The iid of the question being observed
* @param int $excludeTestId If defined, exclude this (current) test from the list of results
*
* @return string An HTML string containing a div and a table
* Displays a table listing the quizzes where a question is used.
*/
public static function showTestsWhereQuestionIsUsed(int $questionId, int $excludeTestId = 0)
public static function showTestsWhereQuestionIsUsed(int $questionId, int $excludeTestId = 0): void
{
$questionId = (int) $questionId;
$excludeTestId = (int) $excludeTestId;

$sql = "SELECT qz.title quiz_title,
c.title course_title,
s.title session_name,
qz.iid as quiz_id,
qz.c_id,
qz.session_id
FROM c_quiz qz,
c_quiz_rel_question qq,
course c,
session s
WHERE qz.c_id = c.id AND
(qz.session_id = s.id OR qz.session_id = 0) AND
qq.quiz_id = qz.iid AND ";
if (!empty($excludeTestId)) {
$sql .= " qz.iid != $excludeTestId AND ";
}
$sql .= " qq.question_id = $questionId
GROUP BY qq.iid";
$em = Database::getManager();
$quizRepo = $em->getRepository(CQuiz::class);
$quizzes = $quizRepo->findQuizzesUsingQuestion($questionId, $excludeTestId);

$result = [];
$html = "";

$sqlResult = Database::query($sql);

if (0 != Database::num_rows($sqlResult)) {
while ($row = Database::fetch_assoc($sqlResult)) {
$tmp = [];
$tmp[0] = $row['course_title'];
$tmp[1] = $row['session_name'];
$tmp[2] = $row['quiz_title'];
// Send do other test with r=1 to reset current test session variables
$urlToQuiz = api_get_path(WEB_CODE_PATH).'exercise/admin.php?'.api_get_cidreq().'&exerciseId='.$row['quiz_id'].'&r=1';
$tmp[3] = '<a href="'.$urlToQuiz.'">'.Display::getMdiIcon('order-bool-ascending-variant', 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Edit')).'</a>';
if (0 == (int) $row['session_id']) {
$tmp[1] = '-';
}
if (empty($quizzes)) {
echo '';
return;
}

$result[] = $tmp;
}
$result = [];

$headers = [
get_lang('Course'),
get_lang('Session'),
get_lang('Quiz'),
get_lang('LinkToTestEdition'),
foreach ($quizzes as $quiz) {
$link = $quiz->getFirstResourceLink();
$course = $link?->getCourse();
$session = $link?->getSession();
$courseId = $course?->getId() ?? 0;
$sessionId = $session?->getId() ?? 0;

$url = api_get_path(WEB_CODE_PATH).'exercise/admin.php?'.
'cid='.$courseId.'&sid='.$sessionId.'&gid=0&gradebook=0&origin='.
'&exerciseId='.$quiz->getIid().'&r=1';


$result[] = [
$course?->getTitle() ?? '-',
$session?->getTitle() ?? '-',
$quiz->getTitle(),
'<a href="'.$url.'">'.Display::getMdiIcon(
'order-bool-ascending-variant',
'ch-tool-icon',
null,
ICON_SIZE_SMALL,
get_lang('Edit')
).'</a>',
];
}

$title = Display::div(
get_lang('QuestionAlsoUsedInTheFollowingTests'),
[
'class' => 'section-title',
'style' => 'margin-top: 25px; border-bottom: none',
]
);
$headers = [
get_lang('Course'),
get_lang('Session'),
get_lang('Quiz'),
get_lang('Link to test edition'),
];

$html = $title.Display::table($headers, $result);
}
$title = Display::div(
get_lang('Question also used in the following tests'),
['class' => 'section-title', 'style' => 'margin-top: 25px; border-bottom: none']
);

echo $html;
echo $title.Display::table($headers, $result);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/CourseBundle/Repository/CQuizRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,30 @@ public function findAutoLaunchableQuizByCourseAndSession(Course $course, ?Sessio

return $result ? $result['iid'] : null;
}

/**
* Finds quizzes that are using a given question, optionally excluding one quiz.
*/
public function findQuizzesUsingQuestion(int $questionId, int $excludeQuizId = 0): array
{
$qb = $this->getEntityManager()->createQueryBuilder();

$qb->select('quiz', 'rn', 'rl', 'course', 'session')
->from(CQuiz::class, 'quiz')
->innerJoin('quiz.questions', 'rel')
->innerJoin('quiz.resourceNode', 'rn')
->leftJoin('rn.resourceLinks', 'rl')
->leftJoin('rl.course', 'course')
->leftJoin('rl.session', 'session')
->where('rel.question = :questionId')
->setParameter('questionId', $questionId)
->groupBy('quiz.iid');

if ($excludeQuizId > 0) {
$qb->andWhere('quiz.iid != :excludeQuizId')
->setParameter('excludeQuizId', $excludeQuizId);
}

return $qb->getQuery()->getResult();
}
}
Loading