Skip to content

Exercise: Fix Excel quiz import and exercise creation issues - refs BT#22106 #5857

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
Oct 15, 2024
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
8 changes: 4 additions & 4 deletions public/main/exercise/TestCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public static function get_stats_table_by_attempt($exercise, $category_list = []
true
)
);
$tempResult[$category_id] = round($category_item['score'] / $category_item['total'] * 10);
$tempResult[$category_id] = $category_item['total'] != 0 ? round($category_item['score'] / $category_item['total'] * 10) : 0;
$row++;
}

Expand Down Expand Up @@ -1039,7 +1039,7 @@ public static function get_category_id_for_title($title, $courseId = 0)
$res = Database::query($sql);
if (Database::num_rows($res) > 0) {
$data = Database::fetch_array($res);
$out_res = $data['id'];
$out_res = $data['iid'];
}

return $out_res;
Expand All @@ -1063,8 +1063,8 @@ public static function addCategoryToQuestion($categoryId, $questionId, $courseId
$questionId > 0 &&
$courseId > 0
) {
$sql = "INSERT INTO $table (c_id, question_id, category_id)
VALUES (".intval($courseId).", ".intval($questionId).", ".intval($categoryId).")";
$sql = "INSERT INTO $table (question_id, category_id)
VALUES (".intval($questionId).", ".intval($categoryId).")";
Database::query($sql);
$id = Database::insert_id();

Expand Down
2 changes: 1 addition & 1 deletion public/main/exercise/answer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ public function save()
->setAnswer($answer)
->setCorrect($correct)
->setComment($comment)
->setPonderation($weighting)
->setPonderation(!is_null($weighting) ? $weighting : 0.0)
->setPosition($position)
->setHotspotCoordinates($hotspot_coordinates)
->setHotspotType($hotspot_type)
Expand Down
15 changes: 6 additions & 9 deletions public/main/exercise/question.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,6 @@ public function create_question(
$type = 1,
$level = 1
) {
$course_id = api_get_course_int_id();
$tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
$tbl_quiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);

Expand All @@ -1785,36 +1784,34 @@ public function create_question(
FROM $tbl_quiz_question q
INNER JOIN $tbl_quiz_rel_question r
ON
q.id = r.question_id AND
quiz_id = $quiz_id AND
q.c_id = $course_id AND
r.c_id = $course_id";
q.iid = r.question_id AND
quiz_id = $quiz_id";
$rs_max = Database::query($sql);
$row_max = Database::fetch_object($rs_max);
$max_position = $row_max->max_position + 1;

$params = [
'c_id' => $course_id,
'question' => $question_name,
'description' => $question_description,
'ponderation' => $max_score,
'position' => $max_position,
'type' => $type,
'level' => $level,
'mandatory' => 0,
];
$question_id = Database::insert($tbl_quiz_question, $params);

if ($question_id) {
// Get the max question_order
$sql = "SELECT max(question_order) as max_order
FROM $tbl_quiz_rel_question
WHERE c_id = $course_id AND quiz_id = $quiz_id ";
WHERE quiz_id = $quiz_id ";
$rs_max_order = Database::query($sql);
$row_max_order = Database::fetch_object($rs_max_order);
$max_order = $row_max_order->max_order + 1;
// Attach questions to quiz
$sql = "INSERT INTO $tbl_quiz_rel_question (c_id, question_id, quiz_id, question_order)
VALUES($course_id, $question_id, $quiz_id, $max_order)";
$sql = "INSERT INTO $tbl_quiz_rel_question (question_id, quiz_id, question_order)
VALUES($question_id, $quiz_id, $max_order)";
Database::query($sql);
}

Expand Down
41 changes: 25 additions & 16 deletions public/main/exercise/upload_exercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ChamiloSession as Session;
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
use PhpOffice\PhpSpreadsheet\IOFactory;

/**
* Upload quiz: This script shows the upload quiz feature.
Expand Down Expand Up @@ -147,7 +148,15 @@ function lp_upload_quiz_action_handling()
$answerList = [];
$quizTitle = '';

$objPHPExcel = PHPExcel_IOFactory::load($_FILES['user_upload_quiz']['tmp_name']);
if (isset($_FILES['user_upload_quiz'])) {
try {
$objPHPExcel = IOFactory::load($_FILES['user_upload_quiz']['tmp_name']);
} catch (\Exception $e) {
return;
}
} else {
return;
}

$objPHPExcel->setActiveSheetIndex(0);
$worksheet = $objPHPExcel->getActiveSheet();
Expand All @@ -159,9 +168,9 @@ function lp_upload_quiz_action_handling()
$useCustomScore = isset($_POST['user_custom_score']) ? true : false;

for ($row = 1; $row <= $highestRow; $row++) {
$cellTitleInfo = $worksheet->getCellByColumnAndRow(0, $row);
$cellDataInfo = $worksheet->getCellByColumnAndRow(1, $row);
$cellScoreInfo = $worksheet->getCellByColumnAndRow(2, $row);
$cellTitleInfo = $worksheet->getCell("A$row");
$cellDataInfo = $worksheet->getCell("B$row");
$cellScoreInfo = $worksheet->getCell("C$row");
$title = $cellTitleInfo->getValue();

switch ($title) {
Expand All @@ -177,13 +186,13 @@ function lp_upload_quiz_action_handling()
$answerIndex = 0;
while ($continue) {
$answerRow++;
$answerInfoTitle = $worksheet->getCellByColumnAndRow(0, $answerRow);
$answerInfoData = $worksheet->getCellByColumnAndRow(1, $answerRow);
$answerInfoExtra = $worksheet->getCellByColumnAndRow(2, $answerRow);
$answerInfoTitle = $answerInfoTitle->getValue();
$answerInfoTitle = $worksheet->getCell("A$answerRow")->getValue();
$answerInfoData = $worksheet->getCell("B$answerRow")->getValue();
$answerInfoExtra = $worksheet->getCell("C$answerRow")->getValue();

if (false !== strpos($answerInfoTitle, 'Answer')) {
$answerList[$numberQuestions][$answerIndex]['data'] = $answerInfoData->getValue();
$answerList[$numberQuestions][$answerIndex]['extra'] = $answerInfoExtra->getValue();
$answerList[$numberQuestions][$answerIndex]['data'] = $answerInfoData;
$answerList[$numberQuestions][$answerIndex]['extra'] = $answerInfoExtra;
} else {
$continue = false;
}
Expand All @@ -201,14 +210,14 @@ function lp_upload_quiz_action_handling()
$questionTypeIndex = 0;
while ($continue) {
$answerRow++;
$questionTypeTitle = $worksheet->getCellByColumnAndRow(0, $answerRow);
$questionTypeExtra = $worksheet->getCellByColumnAndRow(2, $answerRow);
$title = $questionTypeTitle->getValue();
if ('QuestionType' == $title) {
$questionTypeList[$numberQuestions] = $questionTypeExtra->getValue();
$questionTypeTitle = $worksheet->getCell("A$answerRow")->getValue();
$questionTypeExtra = $worksheet->getCell("C$answerRow")->getValue();

if ('QuestionType' == $questionTypeTitle) {
$questionTypeList[$numberQuestions] = $questionTypeExtra;
$continue = false;
}
if ('Question' == $title) {
if ('Question' == $questionTypeTitle) {
$continue = false;
}
// To avoid loops
Expand Down
2 changes: 1 addition & 1 deletion src/CourseBundle/Entity/CQuizQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function getLevel()
return $this->level;
}

public function setExtra(string $extra): self
public function setExtra(?string $extra): self
{
$this->extra = $extra;

Expand Down
Loading