Skip to content

Commit

Permalink
Admin: Add config allow_quick_question_description_popup
Browse files Browse the repository at this point in the history
Allows a quick question description edition with a selected
image from a popup.

BT#18814
  • Loading branch information
jmontoyaa committed May 31, 2021
1 parent d925a2a commit 0a4b4ea
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
44 changes: 43 additions & 1 deletion main/exercise/question_list_admin.inc.php
Expand Up @@ -32,6 +32,7 @@
unset($objQuestionTmp);
}
$ajax_url = api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?'.api_get_cidreq().'&exercise_id='.intval($exerciseId);
$addImageUrl = api_get_path(WEB_CODE_PATH).'inc/lib/elfinder/filemanager.php?add_image=1&'.api_get_cidreq();
?>
<div id="dialog-confirm"
title="<?php echo get_lang('ConfirmYourChoice'); ?>"
Expand All @@ -41,6 +42,33 @@
</p>
</div>
<script>
function addImageToQuestion(image, questionId) {
let url = '<?php echo $ajax_url; ?>' + '&a=save_question_description&question_id=' + questionId;
let params = {
image: image
}
$.ajax({
url: url,
data: params,
success: function (data) {
window.alert('<?php echo addslashes(get_lang('Updated')); ?>');
}
});
}

function loadEditor(button, questionId) {
event.preventDefault();
let url = '<?php echo $addImageUrl; ?>' + '&question_id=' + questionId;
let w = 850;
let h = 500;
let left = (screen.width / 2) - (w / 2);
let top = (screen.height / 2) - (h / 2);
let params = 'height=' + h + ',width=' + w + ',resizable=0,top=' + top + ',left=' + left;

setTimeout(() => window.open(url, 'editor', params), 1000);
return;
}

$(function () {
$("#dialog:ui-dialog").dialog("destroy");
$("#dialog-confirm").dialog({
Expand Down Expand Up @@ -226,6 +254,20 @@
continue;
}

$addImageLink = '';
if (api_get_configuration_value('allow_quick_question_description_popup')) {
$addImageLink = Display::url(
Display::return_icon(
'image.png',
get_lang('AddImage'),
[],
ICON_SIZE_TINY
),
'javascript:void(0);',
['class' => 'btn btn-default btn-sm ajax', 'onclick' => 'loadEditor(this, '.$id.')']
);
}

$clone_link = Display::url(
Display::return_icon(
'cd.png',
Expand Down Expand Up @@ -292,7 +334,7 @@

$btnActions = implode(
PHP_EOL,
[$edit_link, $clone_link, $delete_link]
[$edit_link, $clone_link, $addImageLink, $delete_link]
);

$title = Security::remove_XSS($objQuestionTmp->selectTitle());
Expand Down
39 changes: 39 additions & 0 deletions main/inc/ajax/exercise.ajax.php
Expand Up @@ -4,6 +4,7 @@

use Chamilo\CoreBundle\Entity\TrackEExerciseConfirmation;
use Chamilo\CoreBundle\Entity\TrackEExercises;
use Chamilo\CourseBundle\Entity\CQuizQuestion;
use ChamiloSession as Session;

require_once __DIR__.'/../global.inc.php';
Expand Down Expand Up @@ -417,6 +418,44 @@
echo Display::page_subheader(get_lang('VerificationOfAnsweredQuestions'));
echo $objExercise->getReminderTable($questionList, $statInfo, true);
break;
case 'save_question_description':
if (!api_get_configuration_value('allow_quick_question_description_popup')) {
exit;
}
if (!api_is_allowed_to_edit(null, true)) {
exit;
}

/** @var \Exercise $objExercise */
$objExercise = Session::read('objExercise');
if (empty($objExercise)) {
exit;
}

$questionId = isset($_REQUEST['question_id']) ? (int) $_REQUEST['question_id'] : null;
$image = isset($_REQUEST['image']) ? $_REQUEST['image'] : '';

$questionList = $objExercise->getQuestionList();

if (!in_array($questionId, $questionList)) {
echo '0';
exit;
}

$em = Database::getManager();
$repo = $em->getRepository(CQuizQuestion::class);
/** @var CQuizQuestion $question */
$question = $repo->find($questionId);
if (null !== $question) {
$question->setDescription('<img src="'.$image.'" />');
$em->persist($question);
$em->flush();
echo 1;
exit;
}
echo 0;
exit;
break;
case 'save_exercise_by_now':
header('Content-Type: application/json');

Expand Down
2 changes: 2 additions & 0 deletions main/inc/lib/elfinder/filemanager.php
Expand Up @@ -17,7 +17,9 @@
$includeFile = '<script type="text/javascript" src="'.api_get_path(WEB_PATH).$filePart.'"></script>';
$language = $iso;
}
$questionId = isset($_REQUEST['question_id']) ? (int) $_REQUEST['question_id'] : 0;

$template->assign('question_id', $questionId);
$template->assign('elfinder_lang', $language);
$template->assign('elfinder_translation_file', $includeFile);
$template->display('default/javascript/editor/ckeditor/elfinder.tpl');
3 changes: 3 additions & 0 deletions main/install/configuration.dist.php
Expand Up @@ -1929,6 +1929,9 @@
// Shows only users from active sessions in tracking.
//$_configuration['show_users_in_active_sessions_in_tracking'] = true;

// Allows a quick question description edition with a selected image from a popup.
//$_configuration['allow_quick_question_description_popup'] = true;

// KEEP THIS AT THE END
// -------- Custom DB changes
// Add user activation by confirmation email
Expand Down
13 changes: 11 additions & 2 deletions main/template/default/javascript/editor/elfinder_standalone.tpl
Expand Up @@ -9,7 +9,7 @@
<!-- elFinder translation (OPTIONAL) -->
{{ elfinder_translation_file }}

<script type="text/javascript" charset="utf-8">
<script charset="utf-8">
// Helper function to get parameters from the query string.
function getUrlParam(paramName) {
var reParam = new RegExp('(?:[\?&]|&amp;)' + paramName + '=([^&]+)', 'i');
Expand All @@ -22,7 +22,16 @@
var elf = $('#elfinder').elfinder({
url : '{{ _p.web_lib ~ 'elfinder/connectorAction.php?' }}{{ course_condition }}', // connector URL (REQUIRED)
getFileCallback : function(file) {
window.opener.CKEDITOR.tools.callFunction(funcNum, file.url);
if (window.opener) {
if (window.opener.CKEDITOR) {
window.opener.CKEDITOR.tools.callFunction(funcNum, file.url);
}
if (window.opener.addImageToQuestion) {
window.opener.addImageToQuestion(file.url, {{ question_id }});
}
}
window.close();
},
startPathHash: 'l2_Lw', // Sets the course driver as default
Expand Down

0 comments on commit 0a4b4ea

Please sign in to comment.