MDL-26854 COMMENT
1. added pluginname_comment_validate callback to comments api
2. change permission callback defaults to false
3. Tidied up serveal areas of comments and implemented callback to
ignore system permissions for view (credits to Sam Hemelryk)
AMOS BEGIN
MOV [modulerejectcomment,error],[callbackrejectcomment,error]
AMOS END
This commit is contained in:
@@ -3098,3 +3098,108 @@ function data_presets_export($course, $cm, $data, $tostorage=false) {
|
||||
// Return the full path to the exported preset file:
|
||||
return $exportfile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Running addtional permission check on plugin, for example, plugins
|
||||
* may have switch to turn on/off comments option, this callback will
|
||||
* affect UI display, not like pluginname_comment_validate only throw
|
||||
* exceptions.
|
||||
* Capability check has been done in comment->check_permissions(), we
|
||||
* don't need to do it again here.
|
||||
*
|
||||
* @param stdClass $comment_param {
|
||||
* context => context the context object
|
||||
* courseid => int course id
|
||||
* cm => stdClass course module object
|
||||
* commentarea => string comment area
|
||||
* itemid => int itemid
|
||||
* }
|
||||
* @return array
|
||||
*/
|
||||
function data_comment_permissions($comment_param) {
|
||||
global $CFG, $DB;
|
||||
if (!$record = $DB->get_record('data_records', array('id'=>$comment_param->itemid))) {
|
||||
throw new comment_exception('invalidcommentitemid');
|
||||
}
|
||||
if (!$data = $DB->get_record('data', array('id'=>$record->dataid))) {
|
||||
throw new comment_exception('invalidid', 'data');
|
||||
}
|
||||
if ($data->comments) {
|
||||
return array('post'=>true, 'view'=>true);
|
||||
} else {
|
||||
return array('post'=>false, 'view'=>false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate comment parameter before perform other comments actions
|
||||
*
|
||||
* @param stdClass $comment_param {
|
||||
* context => context the context object
|
||||
* courseid => int course id
|
||||
* cm => stdClass course module object
|
||||
* commentarea => string comment area
|
||||
* itemid => int itemid
|
||||
* }
|
||||
* @return boolean
|
||||
*/
|
||||
function data_comment_validate($comment_param) {
|
||||
global $DB;
|
||||
// validate comment area
|
||||
if ($comment_param->commentarea != 'database_entry') {
|
||||
throw new comment_exception('invalidcommentarea');
|
||||
}
|
||||
// validate itemid
|
||||
if (!$record = $DB->get_record('data_records', array('id'=>$comment_param->itemid))) {
|
||||
throw new comment_exception('invalidcommentitemid');
|
||||
}
|
||||
if (!$data = $DB->get_record('data', array('id'=>$record->dataid))) {
|
||||
throw new comment_exception('invalidid', 'data');
|
||||
}
|
||||
if (!$course = $DB->get_record('course', array('id'=>$data->course))) {
|
||||
throw new comment_exception('coursemisconf');
|
||||
}
|
||||
if (!$cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
|
||||
throw new comment_exception('invalidcoursemodule');
|
||||
}
|
||||
if (!$data->comments) {
|
||||
throw new comment_exception('commentsoff', 'data');
|
||||
}
|
||||
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
|
||||
|
||||
//check if approved
|
||||
if ($data->approval and !$record->approved and !data_isowner($record) and !has_capability('mod/data:approve', $context)) {
|
||||
throw new comment_exception('notapproved', 'data');
|
||||
}
|
||||
|
||||
// group access
|
||||
if ($record->groupid) {
|
||||
$groupmode = groups_get_activity_groupmode($cm, $course);
|
||||
if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
|
||||
if (!groups_is_member($record->groupid)) {
|
||||
throw new comment_exception('notmemberofgroup');
|
||||
}
|
||||
}
|
||||
}
|
||||
// validate context id
|
||||
if ($context->id != $comment_param->context->id) {
|
||||
throw new comment_exception('invalidcontext');
|
||||
}
|
||||
// validation for comment deletion
|
||||
if (!empty($comment_param->commentid)) {
|
||||
if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
|
||||
if ($comment->commentarea != 'database_entry') {
|
||||
throw new comment_exception('invalidcommentarea');
|
||||
}
|
||||
if ($comment->contextid != $comment_param->context->id) {
|
||||
throw new comment_exception('invalidcontext');
|
||||
}
|
||||
if ($comment->itemid != $comment_param->itemid) {
|
||||
throw new comment_exception('invalidcommentitemid');
|
||||
}
|
||||
} else {
|
||||
throw new comment_exception('invalidcommentid');
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user