Admin: Add config allow_gradebook_comments see BT#18202

Requires DB changes
pull/3732/head
Julio Montoya 5 years ago
parent 3d5f91357e
commit 1405b1c22d
  1. 53
      main/gradebook/gradebook_display_summary.php
  2. 50
      main/gradebook/lib/GradebookUtils.php
  3. 16
      main/inc/ajax/gradebook.ajax.php
  4. 5
      main/inc/lib/database.constants.inc.php
  5. 5
      main/inc/lib/pdf.lib.php
  6. 8
      main/install/configuration.dist.php
  7. 13
      main/template/default/export/table_pdf.tpl

@ -145,6 +145,47 @@ switch ($action) {
$cats = Category::load($cat_id, null, null, null, null, null, false);
GradebookUtils::generateTable($courseInfo, $userId, $cats, false, false, $userList);
break;
case 'add_comment':
if (!api_is_allowed_to_edit()) {
exit;
}
$userId = isset($_GET['user_id']) ? (int) $_GET['user_id'] : 0;
$gradeBookId = isset($_GET['gradebook_id']) ? (int) $_GET['gradebook_id'] : 0;
$comment = '';
$commentInfo = GradebookUtils::getComment($gradeBookId, $userId);
if ($commentInfo) {
$comment = $commentInfo['comment'];
}
$ajaxPath = api_get_path(WEB_AJAX_PATH).'gradebook.ajax.php?a=add_gradebook_comment';
$save = Display::return_message(get_lang('Saved'));
echo '<script>
$(function() {
$("form").on("submit", function(e) {
e.preventDefault();
$.ajax({
url: "'.$ajaxPath.'",
data: {
gradebook_id: "'.$gradeBookId.'",
user_id: "'.$userId.'",
comment: $("#comment").val()
},
success: function(data) {
$(".result").html("'.addslashes($save).'");
}
});
});
});
</script>';
$student = api_get_user_info($userId);
$form = new FormValidator('add_comment');
$form->addLabel(get_lang('User'), $student['complete_name']);
$form->addTextarea('comment', get_lang('Comment'), ['id' => 'comment']);
$form->addHtml('<div class="result"></div>');
$form->addButtonSave(get_lang('Save'));
$form->setDefaults(['comment' => $comment]);
$form->display();
exit;
break;
}
$course_code = api_get_course_id();
@ -183,6 +224,7 @@ if (count($userList) == 0) {
echo '<th>';
echo get_lang('Action');
echo '</th></tr>';
$allowComments = api_get_configuration_value('allow_gradebook_comments');
foreach ($userList as $index => $value) {
$userData = api_get_person_name($value['firstname'], $value['lastname']).' ('.$value['username'].')';
echo '<tr>
@ -205,7 +247,18 @@ if (count($userList) == 0) {
$url,
['target' => '_blank', 'class' => 'btn btn-default']
);
if ($allowComments) {
$url = api_get_self().'?'.api_get_cidreq().'&action=add_comment&user_id='.$value['user_id'].'&gradebook_id='.$cat_id;
$link .= '&nbsp;'.Display::url(
get_lang('AddGradebookComment'),
$url,
['target' => '_blank', 'class' => 'ajax btn btn-default']
);
}
echo $link;
echo '</td></tr>';
}
echo '</table></div>';

@ -1560,6 +1560,7 @@ class GradebookUtils
) {
$userInfo = api_get_user_info($userId);
$model = ExerciseLib::getCourseScoreModel();
/** @var Category $cat */
$cat = $cats[0];
$allcat = $cats[0]->get_subcategories(
$userId,
@ -1629,6 +1630,17 @@ class GradebookUtils
}
$pdf->params['student_info'] = $userInfo;
$extraRows = [];
if (api_get_configuration_value('allow_gradebook_comments')) {
$commentInfo = self::getComment($cat->get_id(), $userId);
if ($commentInfo) {
$extraRows[] = [
'label' => get_lang('Comment'),
'content' => $commentInfo['comment'],
];
}
}
$file = api_get_path(SYS_ARCHIVE_PATH).uniqid().'.html';
$settings = api_get_configuration_value('gradebook_pdf_export_settings');
@ -1647,7 +1659,8 @@ class GradebookUtils
$content,
$saveToFile,
$saveToHtmlFile,
true
true,
$extraRows
);
if ($saveToHtmlFile) {
@ -1656,4 +1669,39 @@ class GradebookUtils
return $file;
}
public static function getComment($gradeBookId, $userId)
{
$gradeBookId = (int) $gradeBookId;
$userId = (int) $userId;
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_COMMENT);
$sql = "SELECT * FROM $table
WHERE user_id = $userId AND gradebook_id = $gradeBookId";
$result = Database::query($sql);
return Database::fetch_array($result);
}
public static function saveComment($gradeBookId, $userId, $comment)
{
$commentInfo = self::getComment($gradeBookId, $userId);
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_COMMENT);
if (empty($commentInfo)) {
$params = [
'gradebook_id' => $gradeBookId,
'user_id' => $userId,
'comment' => $comment,
'created_at' => api_get_utc_datetime(),
'updated_at' => api_get_utc_datetime(),
];
Database::insert($table, $params);
} else {
$params = [
'comment' => $comment,
'updated_at' => api_get_utc_datetime(),
];
Database::update($table, $params, ['id = ?' => $commentInfo['id']]);
}
}
}

@ -1,5 +1,7 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Responses to AJAX calls.
*/
@ -10,6 +12,20 @@ api_protect_course_script(true);
$action = $_REQUEST['a'];
switch ($action) {
case 'add_gradebook_comment':
if (true !== api_get_configuration_value('allow_gradebook_comments')) {
exit;
}
if (api_is_allowed_to_edit(null, true)) {
$userId = $_REQUEST['user_id'] ?? 0;
$gradeBookId = $_REQUEST['gradebook_id']?? 0;
$comment = $_REQUEST['comment'] ?? '';
GradebookUtils::saveComment($gradeBookId, $userId, $comment);
echo 1;
exit;
}
echo 0;
break;
case 'get_gradebook_weight':
if (api_is_allowed_to_edit(null, true)) {
$cat_id = $_GET['cat_id'];

@ -43,7 +43,7 @@ define('TABLE_MAIN_SYSTEM_TEMPLATE', 'system_template');
define('TABLE_MAIN_OPENID_ASSOCIATION', 'openid_association');
define('TABLE_MAIN_COURSE_REQUEST', 'course_request');
// Gradebook
// Gradebook.
define('TABLE_MAIN_GRADEBOOK_CATEGORY', 'gradebook_category');
define('TABLE_MAIN_GRADEBOOK_EVALUATION', 'gradebook_evaluation');
define('TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG', 'gradebook_linkeval_log');
@ -54,8 +54,9 @@ define('TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY', 'gradebook_score_display');
define('TABLE_MAIN_GRADEBOOK_CERTIFICATE', 'gradebook_certificate');
define('TABLE_MAIN_GRADEBOOK_SCORE_LOG', 'gradebook_score_log');
define('TABLE_MAIN_GRADEBOOK_RESULT_ATTEMPT', 'gradebook_result_attempt');
define('TABLE_MAIN_GRADEBOOK_COMMENT', 'gradebook_comment');
// Profiling
// Extra fields.
define('TABLE_EXTRA_FIELD', 'extra_field');
define('TABLE_EXTRA_FIELD_OPTIONS', 'extra_field_options');
define('TABLE_EXTRA_FIELD_VALUES', 'extra_field_values');

@ -89,6 +89,7 @@ class PDF
* @param bool|false $saveToFile
* @param bool|false $returnHtml
* @param bool $addDefaultCss (bootstrap/default/base.css)
* @param array
*
* @return string
*/
@ -96,7 +97,8 @@ class PDF
$content,
$saveToFile = false,
$returnHtml = false,
$addDefaultCss = false
$addDefaultCss = false,
$extraRows = []
) {
if (empty($this->template)) {
$tpl = new Template('', false, false, false, false, true, false);
@ -141,6 +143,7 @@ class PDF
$tpl->assign('pdf_student_info', $this->params['student_info']);
$tpl->assign('show_grade_generated_date', $this->params['show_grade_generated_date']);
$tpl->assign('add_signatures', $this->params['add_signatures']);
$tpl->assign('extra_rows', $extraRows);
// Getting template
$tableTemplate = $tpl->get_template('export/table_pdf.tpl');

@ -1799,6 +1799,14 @@ $_configuration['auth_password_links'] = [
// Survey duplicate: Order survey questions by student name
// $_configuration['survey_duplicate_order_by_name'] = true;
// Allow gradebook_comment
/*
CREATE TABLE gradebook_comment (id BIGINT AUTO_INCREMENT NOT NULL, user_id INT DEFAULT NULL, gradebook_id INT DEFAULT NULL, comment LONGTEXT NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX IDX_C3B70763A76ED395 (user_id), INDEX IDX_C3B70763AD3ED51C (gradebook_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;
ALTER TABLE gradebook_comment ADD CONSTRAINT FK_C3B70763A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE;
ALTER TABLE gradebook_comment ADD CONSTRAINT FK_C3B70763AD3ED51C FOREIGN KEY (gradebook_id) REFERENCES gradebook_category (id) ON DELETE CASCADE;
*/
// $_configuration['allow_gradebook_comments'] = true;
// KEEP THIS AT THE END
// -------- Custom DB changes
// Add user activation by confirmation email

@ -81,6 +81,19 @@
</td>
</tr>
{% endif %}
{% if extra_rows %}
{% for row in extra_rows %}
<tr>
<td style="background-color: #E5E5E5; text-align: left; width:130px;">
<strong>{{ row.label }}:</strong>
</td>
<td>
{{ row.content }}
</td>
</tr>
{% endfor %}
{% endif %}
</table>
<br />

Loading…
Cancel
Save