Commit 3be2b123 authored by Guillermo Gomez's avatar Guillermo Gomez Committed by Safat Shahin
Browse files

MDL-71642 qbank_comment: Add question comment plugin to core



This implementation will introduce a qbank plugin
"comment" which will allow users with the capability
to comment in a question. It also implements a
callback for the question preview page to view
and add comments from the preview page. Comment
plugin is implemented using the existing comments
API from the core.
Co-Authored-By: default avatarGuillermo Gomez Arias <guillermogomez@catalyst-au.net>
Co-Authored-By: default avatarSafat Shahin <safatshahin@catalyst-au.net>
Co-Authored-By: default avatarMatt Porritt <mattp@catalyst-au.net>
parent 70073fdc
......@@ -36,6 +36,7 @@ require_once($CFG->dirroot . '/backup/moodle2/backup_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_default_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_xml_transformer.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qbank_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_extrafields_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_gradingform_plugin.class.php');
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines backup_qbank_plugin class.
*
* @package core_backup
* @subpackage moodle2
* @category backup
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Base class for qbank backup plugins.
*
* @package core_backup
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class backup_qbank_plugin extends backup_plugin {
// Use default parent behaviour.
}
......@@ -787,34 +787,28 @@ class backup_filters_structure_step extends backup_structure_step {
}
/**
* structure step in charge of constructing the comments.xml file for all the comments found
* in a given context
* Structure step in charge of constructing the comments.xml file for all the comments found in a given context.
*/
class backup_comments_structure_step extends backup_structure_step {
protected function define_structure() {
// Define each element separated
// Define each element separated.
$comments = new backup_nested_element('comments');
$comment = new backup_nested_element('comment', array('id'), array(
'commentarea', 'itemid', 'content', 'format',
'component', 'commentarea', 'itemid', 'content', 'format',
'userid', 'timecreated'));
// Build the tree
// Build the tree.
$comments->add_child($comment);
// Define sources
// Define sources.
$comment->set_source_table('comments', array('contextid' => backup::VAR_CONTEXTID));
// Define id annotations
// Define id annotations.
$comment->annotate_ids('user', 'userid');
// Return the root element (comments)
// Return the root element (comments).
return $comments;
}
}
......@@ -2356,6 +2350,9 @@ class backup_questions_structure_step extends backup_structure_step {
// attach qtype plugin structure to $question element, only one allowed
$this->add_plugin_structure('qtype', $question, false);
// Attach qbank plugin stucture to $question element, multiple allowed.
$this->add_plugin_structure('qbank', $question, true);
// attach local plugin stucture to $question element, multiple allowed
$this->add_plugin_structure('local', $question, true);
......
......@@ -35,6 +35,7 @@ require_once($CFG->dirroot . '/backup/moodle2/restore_final_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_qbank_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_extrafields_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_format_plugin.class.php');
......@@ -46,6 +47,7 @@ require_once($CFG->dirroot . '/backup/moodle2/restore_plagiarism_plugin.class.ph
require_once($CFG->dirroot . '/backup/moodle2/restore_gradingform_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_enrol_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qbank_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_extrafields_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines restore_qbank_plugin class.
*
* @package core_backup
* @subpackage moodle2
* @category backup
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Base class for qbank backup plugins.
*
* @package core_backup
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class restore_qbank_plugin extends restore_plugin {
// Use default parent behaviour.
}
......@@ -4750,10 +4750,13 @@ class restore_create_categories_and_questions extends restore_structure_step {
// Apply for 'qtype' plugins optional paths at question level
$this->add_plugin_structure('qtype', $question);
// Apply for 'qbank' plugins optional paths at question level.
$this->add_plugin_structure('qbank', $question);
// Apply for 'local' plugins optional paths at question level
$this->add_plugin_structure('local', $question);
return array($category, $question, $hint, $tag);
return [$category, $question, $hint, $tag];
}
protected function process_question_category($data) {
......
......@@ -365,6 +365,8 @@ $string['question:viewall'] = 'View all questions';
$string['question:viewmine'] = 'View your own questions';
$string['question:tagall'] = 'Tag all questions';
$string['question:tagmine'] = 'Tag your own questions';
$string['question:commentall'] = 'Comment all questions';
$string['question:commentmine'] = 'Comment your own questions';
$string['rating:rate'] = 'Add ratings to items';
$string['rating:view'] = 'View the total rating you received';
$string['rating:viewany'] = 'View total ratings that anyone received';
......
......@@ -1944,6 +1944,7 @@ class core_plugin_manager {
),
'qbank' => [
'comment',
'deletequestion',
'editquestion',
'exporttoxml',
......
......@@ -387,6 +387,9 @@ function question_delete_question($questionid) {
}
}
// Delete question comments.
$DB->delete_records('comments', ['itemid' => $questionid, 'component' => 'qbank_comment',
'commentarea' => 'question']);
// Finally delete the question record itself
$DB->delete_records('question', array('id' => $questionid));
question_bank::notify_question_edited($questionid);
......@@ -1569,7 +1572,7 @@ function question_has_capability_on($questionorid, $cap, $notused = -1) {
// These are existing questions capabilities that are set per category.
// Each of these has a 'mine' and 'all' version that is appended to the capability name.
$capabilitieswithallandmine = ['edit' => 1, 'view' => 1, 'use' => 1, 'move' => 1, 'tag' => 1];
$capabilitieswithallandmine = ['edit' => 1, 'view' => 1, 'use' => 1, 'move' => 1, 'tag' => 1, 'comment' => 1];
if (!isset($capabilitieswithallandmine[$cap])) {
return has_capability('moodle/question:' . $cap, $context);
......@@ -1742,6 +1745,8 @@ function question_get_question_capabilities() {
'moodle/question:moveall',
'moodle/question:tagmine',
'moodle/question:tagall',
'moodle/question:commentmine',
'moodle/question:commentall',
);
}
......
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Column selector js.
*
* @package qbank_comment
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import Fragment from 'core/fragment';
import * as Str from 'core/str';
import ModalEvents from 'core/modal_events';
import ModalFactory from 'core/modal_factory';
import Notification from 'core/notification';
/**
* Event listeners for the module.
*
* @method clickEvent
* @param {int} questionId
* @param {int} courseID
* @param {int} contextId
*/
const commentEvent = (questionId, courseID, contextId) => {
let args = {
questionid: questionId,
courseid: courseID
};
let commentFragment = Fragment.loadFragment('qbank_comment', 'question_comment', contextId, args);
ModalFactory.create({
type: ModalFactory.types.SAVE_CANCEL,
title: Str.get_string('commentheader', 'qbank_comment'),
body: commentFragment,
large: true,
}).then((modal) => {
let root = modal.getRoot();
// Don't display the default add comment link in the modal.
root.on(ModalEvents.bodyRendered, function() {
const submitlink = document.querySelectorAll("div.comment-area a")[0];
submitlink.style.display = 'none';
});
// Get the required strings and updated the modal button text labels.
Str.get_strings([
{key: 'addcomment', component: 'qbank_comment'},
{key: 'close', component: 'qbank_comment'},
]).then((strings) => {
modal.setButtonText('save', strings[0]);
modal.setButtonText('cancel', strings[1]);
return;
}).fail(Notification.exception);
root.on(ModalEvents.cancel, function() {
location.reload();
modal.hide();
});
// Handle adding the comment when the button in the modal is clicked.
root.on(ModalEvents.save, function(e) {
e.preventDefault();
const submitlink = document.querySelectorAll("div.comment-area a")[0];
const textarea = document.querySelectorAll("div.comment-area textarea")[0];
// Check there is a valid comment to add, and trigger adding if there is.
if (textarea.value != textarea.getAttribute('aria-label') && textarea.value != '') {
submitlink.click();
}
});
root.on('click', 'button[data-action="hide"]', () => {
location.reload();
modal.hide();
});
modal.show();
return modal;
}).fail(Notification.exception);
};
/**
* Entrypoint of the js.
*
* @method init
* @param {string} questionSelector the question comment identifier.
*/
export const init = (questionSelector) => {
let target = document.querySelector(questionSelector);
let contextId = 1;
let questionId = target.getAttribute('data-questionid'),
courseID = target.getAttribute('data-courseid');
target.addEventListener('click', () => {
// Call for the event listener to listed for clicks in any comment count row.
commentEvent(questionId, courseID, contextId);
});
};
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Provides the information to backup question comments.
*
* @package qbank_comment
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_qbank_comment_plugin extends \backup_qbank_plugin {
/**
* Returns the comment information to attach to question element.
*/
protected function define_question_plugin_structure() {
// Define the virtual plugin element with the condition to fulfill.
$plugin = $this->get_plugin_element();
// Create one standard named plugin element (the visible container).
$pluginwrapper = new backup_nested_element($this->get_recommended_name());
// Connect the visible container ASAP.
$plugin->add_child($pluginwrapper);
$comments = new backup_nested_element('comments');
$comment = new backup_nested_element('comment', ['id'], ['component', 'commentarea', 'itemid', 'contextid',
'content', 'format', 'userid', 'timecreated']);
$pluginwrapper->add_child($comments);
$comments->add_child($comment);
$comment->set_source_sql("SELECT c.*
FROM {comments} c
WHERE c.commentarea = 'question'
AND c.component = 'qbank_comment'
AND c.itemid = ?", [backup::VAR_PARENTID]);
$comment->annotate_ids('user', 'userid');
return $plugin;
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Restore plugin class that provides the necessary information needed to restore comments for questions.
*
* @package qbank_comment
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_qbank_comment_plugin extends restore_qbank_plugin {
/**
* Returns the paths to be handled by the plugin at question level.
*/
protected function define_question_plugin_structure() {
return [
new restore_path_element('comment', $this->get_pathfor('/comments/comment'))
];
}
/**
* Process the question comments element.
*
* @param array $data The comment data to restore.
*/
public function process_comment($data) {
global $DB, $CFG;
$data = (object)$data;
$newquestionid = $this->get_new_parentid('question');
$questioncreated = (bool) $this->get_mappingid('question_created', $this->get_old_parentid('question'));
if (!$questioncreated) {
// This question already exists in the question bank. Nothing for us to do.
return;
}
if ($CFG->usecomments) {
$data->itemid = $newquestionid;
$DB->insert_record('comments', $data);
}
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qbank_comment;
use core_question\local\bank\column_base;
use question_bank;
/**
* A column to show the number of comments.
*
* @package qbank_comment
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class comment_count_column extends column_base {
/**
* Get the name of the column, used internally.
*
* @return string
*/
public function get_name(): string {
return 'commentcount';
}
/**
* Get the title of the column that will be displayed.
*
* @return string
*/
protected function get_title(): string {
return get_string('commentplural', 'qbank_comment');
}
/**
* Generate the content to be displayed.
*
* @param object $question The question object.
* @param string $rowclasses Classes that can be added.
*/
protected function display_content($question, $rowclasses): void {
global $DB, $PAGE;
$args = [
'component' => 'qbank_comment',
'commentarea' => 'question',
'itemid' => $question->id,
'contextid' => 1
];
$commentcount = $DB->count_records('comments', $args);
$attributes = [];
if (question_has_capability_on($question, 'comment')) {
$target = 'questioncommentpreview_' . $question->id;
$datatarget = '[data-target="' . $target . '"]';
$PAGE->requires->js_call_amd('qbank_comment/comment', 'init', [$datatarget]);
$attributes = [
'data-target' => $target,
'data-questionid' => $question->id,
'data-courseid' => $this->qbank->course->id,
'class' => 'link-primary comment-pointer'
];
}
echo \html_writer::tag('a', $commentcount, $attributes);
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace qbank_comment\event;
/**
* qbank_comment comment created event.
*
* @package qbank_comment
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class comment_created extends \core\event\comment_created {
/**
* Get URL related to the action, null in this case.
*
* @return null
*/
public function get_url() {
return null;
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
$a = new \stdClass();
$a->userid = $this->userid;
$a->objectid = $this->objectid;
$a->component = $this->component;
$a->itemid = $this->other['itemid'];
return get_string('comment_added', 'qbank_comment', $a);
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.</