MDL-53700 competency: Migrating comment related APIs to core

This commit is contained in:
Frederic Massart 2016-04-06 17:52:41 +08:00
parent 7160fb19d7
commit 4aa6acb22a
8 changed files with 261 additions and 258 deletions

View file

@ -1,57 +0,0 @@
<?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/>.
/**
* Comment created event for tool_lp areas.
*
*
* @package tool_lp
* @copyright 2016 Issam Taboubi <issam.taboubi@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\event;
defined('MOODLE_INTERNAL') || die();
/**
* Comment created event class for tool_lp areas.
*
*
* @package tool_lp
* @since Moodle 3.1
* @copyright 2016 Issam Taboubi <issam.taboubi@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class comment_created extends \core\event\comment_created {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' added the comment with id '$this->objectid' to the '$this->component'";
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return null;
}
}

View file

@ -1,48 +0,0 @@
<?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/>.
/**
* Comment deleted event for tool_lp areas.
*
*
* @package tool_lp
* @copyright 2016 Issam Taboubi <issam.taboubi@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\event;
defined('MOODLE_INTERNAL') || die();
/**
* Comment deleted event class for tool_lp areas.
*
*
* @package tool_lp
* @since Moodle 3.1
* @copyright 2016 Issam Taboubi <issam.taboubi@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class comment_deleted extends \core\event\comment_deleted {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' deleted the comment with id '$this->objectid' from '$this->component'";
}
}

View file

@ -152,241 +152,6 @@ function tool_lp_extend_navigation_category_settings($navigation, $coursecategor
}
}
/**
* Hook when a comment is added.
*
* @param stdClass $comment The comment.
* @param stdClass $params The parameters.
* @return array
*/
function tool_lp_comment_add($comment, $params) {
global $USER;
if (!\core_competency\api::is_enabled()) {
return;
}
if ($params->commentarea == 'user_competency') {
$uc = new \core_competency\user_competency($params->itemid);
// Message both the user and the reviewer, except when they are the author of the message.
$recipients = array($uc->get_userid());
if ($uc->get_reviewerid()) {
$recipients[] = $uc->get_reviewerid();
}
$recipients = array_diff($recipients, array($comment->userid));
if (empty($recipients)) {
return;
}
// Get the sender.
$user = $USER;
if ($USER->id != $comment->userid) {
$user = core_user::get_user($comment->userid);
}
$fullname = fullname($user);
// Get the competency.
$competency = $uc->get_competency();
$competencyname = format_string($competency->get_shortname(), true, array('context' => $competency->get_context()));
// We want to send a message for one plan, trying to find an active one first, or the last modified one.
$plan = null;
$plans = $uc->get_plans();
foreach ($plans as $candidate) {
if ($candidate->get_status() == \core_competency\plan::STATUS_ACTIVE) {
$plan = $candidate;
break;
} else if (!empty($plan) && $plan->get_timemodified() < $candidate->get_timemodified()) {
$plan = $candidate;
} else if (empty($plan)) {
$plan = $candidate;
}
}
// Urls.
// TODO MDL-52749 Replace the link to the plan with the user competency page.
if (empty($plan)) {
$urlname = get_string('userplans', 'tool_lp');
$url = new moodle_url('/admin/tool/lp/plans.php', array('userid' => $uc->get_userid()));
} else {
$urlname = $competencyname;
$url = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
'userid' => $uc->get_userid(),
'competencyid' => $uc->get_competencyid(),
'planid' => $plan->get_id()
));
}
// Construct the message content.
$fullmessagehtml = get_string('usercommentedonacompetencyhtml', 'tool_lp', array(
'fullname' => $fullname,
'competency' => $competencyname,
'comment' => format_text($comment->content, $comment->format, array('context' => $params->context->id)),
'url' => $url->out(true),
'urlname' => $urlname,
));
if ($comment->format == FORMAT_PLAIN || $comment->format == FORMAT_MOODLE) {
$format = FORMAT_MOODLE;
$fullmessage = get_string('usercommentedonacompetency', 'tool_lp', array(
'fullname' => $fullname,
'competency' => $competencyname,
'comment' => $comment->content,
'url' => $url->out(false),
));
} else {
$format = FORMAT_HTML;
$fullmessage = $fullmessagehtml;
}
$message = new \core\message\message();
$message->component = 'moodle';
$message->name = 'competencyusercompcomment';
$message->notification = 1;
$message->userfrom = core_user::get_noreply_user();
$message->subject = get_string('usercommentedonacompetencysubject', 'tool_lp', $fullname);
$message->fullmessage = $fullmessage;
$message->fullmessageformat = $format;
$message->fullmessagehtml = $fullmessagehtml;
$message->smallmessage = get_string('usercommentedonacompetencysmall', 'tool_lp', array(
'fullname' => $fullname,
'competency' => $competencyname,
));
$message->contexturl = $url->out(false);
$message->contexturlname = $urlname;
// Message each recipient.
foreach ($recipients as $recipient) {
$msgcopy = clone($message);
$msgcopy->userto = $recipient;
message_send($msgcopy);
}
} else if ($params->commentarea == 'plan') {
$plan = new \core_competency\plan($params->itemid);
// Message both the user and the reviewer, except when they are the author of the message.
$recipients = array($plan->get_userid());
if ($plan->get_reviewerid()) {
$recipients[] = $plan->get_reviewerid();
}
$recipients = array_diff($recipients, array($comment->userid));
if (empty($recipients)) {
return;
}
// Get the sender.
$user = $USER;
if ($USER->id != $comment->userid) {
$user = core_user::get_user($comment->userid);
}
$fullname = fullname($user);
$planname = format_string($plan->get_name(), true, array('context' => $plan->get_context()));
$urlname = $planname;
$url = new moodle_url('/admin/tool/lp/plan.php', array(
'id' => $plan->get_id()
));
// Construct the message content.
$fullmessagehtml = get_string('usercommentedonaplanhtml', 'tool_lp', array(
'fullname' => $fullname,
'plan' => $planname,
'comment' => format_text($comment->content, $comment->format, array('context' => $params->context->id)),
'url' => $url->out(true),
'urlname' => $urlname,
));
if ($comment->format == FORMAT_PLAIN || $comment->format == FORMAT_MOODLE) {
$format = FORMAT_MOODLE;
$fullmessage = get_string('usercommentedonaplan', 'tool_lp', array(
'fullname' => $fullname,
'plan' => $planname,
'comment' => $comment->content,
'url' => $url->out(false),
));
} else {
$format = FORMAT_HTML;
$fullmessage = $fullmessagehtml;
}
$message = new \core\message\message();
$message->component = 'moodle';
$message->name = 'competencyplancomment';
$message->notification = 1;
$message->userfrom = core_user::get_noreply_user();
$message->subject = get_string('usercommentedonaplansubject', 'tool_lp', $fullname);
$message->fullmessage = $fullmessage;
$message->fullmessageformat = $format;
$message->fullmessagehtml = $fullmessagehtml;
$message->smallmessage = get_string('usercommentedonaplansmall', 'tool_lp', array(
'fullname' => $fullname,
'plan' => $planname,
));
$message->contexturl = $url->out(false);
$message->contexturlname = $urlname;
// Message each recipient.
foreach ($recipients as $recipient) {
$msgcopy = clone($message);
$msgcopy->userto = $recipient;
message_send($msgcopy);
}
}
}
/**
* Return the permissions of for the comments.
*
* @param stdClass $params The parameters.
* @return array
*/
function tool_lp_comment_permissions($params) {
if (!\core_competency\api::is_enabled()) {
return array('post' => false, 'view' => false);
}
if ($params->commentarea == 'user_competency') {
$uc = new \core_competency\user_competency($params->itemid);
if ($uc->can_read()) {
return array('post' => $uc->can_comment(), 'view' => $uc->can_read_comments());
}
} else if ($params->commentarea == 'plan') {
$plan = new \core_competency\plan($params->itemid);
if ($plan->can_read()) {
return array('post' => $plan->can_comment(), 'view' => $plan->can_read_comments());
}
}
return array('post' => false, 'view' => false);
}
/**
* Validates comments.
*
* @param stdClass $params The parameters.
* @return bool
*/
function tool_lp_comment_validate($params) {
if (!\core_competency\api::is_enabled()) {
return false;
}
if ($params->commentarea == 'user_competency') {
if (!\core_competency\user_competency::record_exists($params->itemid)) {
return false;
}
return true;
} else if ($params->commentarea == 'plan') {
if (!\core_competency\plan::record_exists($params->itemid)) {
return false;
}
return true;
}
return false;
}
/**
* Inject the competencies elements into all moodle module settings forms.
*

View file

@ -1,321 +0,0 @@
<?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/>.
/**
* Lib tests.
*
* @package tool_lp
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core_competency\plan;
use core_competency\user_competency;
global $CFG;
/**
* Lib testcase.
*
* @package tool_lp
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_competency_lib_testcase extends advanced_testcase {
public function test_comment_add_user_competency() {
global $DB;
$this->resetAfterTest();
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('core_competency');
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$u3 = $dg->create_user();
$reviewerroleid = $dg->create_role();
assign_capability('moodle/competency:planview', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
assign_capability('moodle/competency:usercompetencycomment', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
$dg->role_assign($reviewerroleid, $u2->id, context_user::instance($u1->id));
$dg->role_assign($reviewerroleid, $u3->id, context_user::instance($u1->id));
accesslib_clear_all_caches_for_unit_testing();
$f1 = $lpg->create_framework();
$c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // In 1 plan.
$c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // In 2 plans.
$c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // Orphan.
$p1 = $lpg->create_plan(array('userid' => $u1->id));
$lpg->create_plan_competency(array('planid' => $p1->get_id(), 'competencyid' => $c1->get_id()));
$lpg->create_plan_competency(array('planid' => $p1->get_id(), 'competencyid' => $c2->get_id()));
$p2 = $lpg->create_plan(array('userid' => $u1->id));
$lpg->create_plan_competency(array('planid' => $p2->get_id(), 'competencyid' => $c2->get_id()));
$DB->set_field(plan::TABLE, 'timemodified', 1, array('id' => $p1->get_id())); // Make plan 1 appear as old.
$p1->read();
$uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c1->get_id(),
'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
$uc2 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c2->get_id(),
'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
$uc3 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c3->get_id(),
'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
// Post a comment for the user competency being in one plan. The reviewer is messaged.
$this->setUser($u1);
$comment = $uc1->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('Hello world!');
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(1, $messages);
$message = array_pop($messages);
$expectedurlname = $c1->get_shortname();
$expectedurl = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
'userid' => $u1->id,
'competencyid' => $c1->get_id(),
'planid' => $p1->get_id()
));
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u2->id, $message->useridto);
$this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
$this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
$this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
$this->assertEquals($expectedurl->out(false), $message->contexturl);
$this->assertEquals($expectedurlname, $message->contexturlname);
// Reviewer posts a comment for the user competency being in two plans. Owner is messaged.
$this->setUser($u2);
$comment = $uc2->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('Hello world!');
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(1, $messages);
$message = array_pop($messages);
$expectedurlname = $c2->get_shortname();
$expectedurl = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
'userid' => $u1->id,
'competencyid' => $c2->get_id(),
'planid' => $p2->get_id()
));
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u1->id, $message->useridto);
$this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
$this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
$this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
$this->assertEquals($expectedurl->out(false), $message->contexturl);
$this->assertEquals($expectedurlname, $message->contexturlname);
// Reviewer posts a comment for the user competency being in no plans. User is messaged.
$this->setUser($u2);
$comment = $uc3->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('Hello world!');
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(1, $messages);
$message = array_pop($messages);
$expectedurlname = get_string('userplans', 'tool_lp');
$expectedurl = new moodle_url('/admin/tool/lp/plans.php', array(
'userid' => $u1->id,
));
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u1->id, $message->useridto);
$this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
$this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
$this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
$this->assertEquals($expectedurl->out(false), $message->contexturl);
$this->assertEquals($expectedurlname, $message->contexturlname);
// A comment is posted by another user, reviewer and owner are messaged.
$this->setUser($u3);
$comment = $uc3->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('Hello world!');
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(2, $messages);
$message1 = array_shift($messages);
$message2 = array_shift($messages);
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u1->id, $message1->useridto);
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u2->id, $message2->useridto);
// A comment is posted in HTML.
$this->setUser($u2);
$comment = $uc3->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('<em>Hello world!</em>', FORMAT_HTML);
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(1, $messages);
$message = array_pop($messages);
$expectedurlname = get_string('userplans', 'tool_lp');
$expectedurl = new moodle_url('/admin/tool/lp/plans.php', array(
'userid' => $u1->id,
));
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u1->id, $message->useridto);
$this->assertTrue(strpos($message->fullmessage, '<em>Hello world!</em>') !== false);
$this->assertTrue(strpos($message->fullmessagehtml, '<em>Hello world!</em>') !== false);
$this->assertEquals(FORMAT_HTML, $message->fullmessageformat);
$this->assertEquals($expectedurl->out(false), $message->contexturl);
$this->assertEquals($expectedurlname, $message->contexturlname);
}
/**
* Commenting on a plan.
*/
public function test_comment_add_plan() {
$this->resetAfterTest();
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('core_competency');
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$u3 = $dg->create_user();
$userroleid = $dg->create_role();
$reviewerroleid = $dg->create_role();
assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $userroleid, context_system::instance()->id, true);
assign_capability('moodle/competency:planviewown', CAP_ALLOW, $userroleid, context_system::instance()->id, true);
assign_capability('moodle/competency:planviewdraft', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
assign_capability('moodle/competency:planmanage', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
assign_capability('moodle/competency:plancomment', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
$dg->role_assign($userroleid, $u1->id, context_user::instance($u1->id));
$dg->role_assign($reviewerroleid, $u2->id, context_user::instance($u1->id));
$dg->role_assign($reviewerroleid, $u3->id, context_system::instance());
accesslib_clear_all_caches_for_unit_testing();
$p1 = $lpg->create_plan(array('userid' => $u1->id));
// Post a comment in own plan, no reviewer. Nobody is messaged.
$this->setUser($u1);
$comment = $p1->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('Hello world!');
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(0, $messages);
// Post a comment in plan as someone else, no reviewer. The owner is messages.
$this->setUser($u3);
$comment = $p1->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('Hello world!');
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(1, $messages);
$message = array_pop($messages);
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u1->id, $message->useridto);
// Post a comment in a plan with reviewer. The reviewer is messaged.
$p1->set_reviewerid($u2->id);
$p1->update();
$this->setUser($u1);
$comment = $p1->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('Hello world!');
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(1, $messages);
$message = array_pop($messages);
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u2->id, $message->useridto);
// Post a comment as reviewer in a plan being reviewed. The owner is messaged.
$p1->set_reviewerid($u2->id);
$p1->update();
$this->setUser($u2);
$comment = $p1->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('Hello world!');
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(1, $messages);
$message = array_pop($messages);
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u1->id, $message->useridto);
// Post a comment as someone else in a plan being reviewed. The owner and reviewer are messaged.
$p1->set_reviewerid($u2->id);
$p1->update();
$this->setUser($u3);
$comment = $p1->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('Hello world!');
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(2, $messages);
$message1 = array_shift($messages);
$message2 = array_shift($messages);
$this->assertEquals(core_user::get_noreply_user()->id, $message1->useridfrom);
$this->assertEquals($u1->id, $message1->useridto);
$this->assertEquals(core_user::get_noreply_user()->id, $message2->useridfrom);
$this->assertEquals($u2->id, $message2->useridto);
$p1->set_reviewerid(null);
$p1->update();
// Test message content.
$this->setUser($u3);
$comment = $p1->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('Hello world!');
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(1, $messages);
$message = array_pop($messages);
$expectedurlname = $p1->get_name();
$expectedurl = new moodle_url('/admin/tool/lp/plan.php', array(
'id' => $p1->get_id()
));
$this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
$this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
$this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
$this->assertEquals($expectedurl->out(false), $message->contexturl);
$this->assertEquals($expectedurlname, $message->contexturlname);
// Test message content as HTML.
$this->setUser($u3);
$comment = $p1->get_comment_object();
$sink = $this->redirectMessages();
$comment->add('<em>Hello world!</em>', FORMAT_HTML);
$messages = $sink->get_messages();
$sink->close();
$this->assertCount(1, $messages);
$message = array_pop($messages);
$expectedurlname = $p1->get_name();
$expectedurl = new moodle_url('/admin/tool/lp/plan.php', array(
'id' => $p1->get_id()
));
$this->assertTrue(strpos($message->fullmessage, '<em>Hello world!</em>') !== false);
$this->assertTrue(strpos($message->fullmessagehtml, '<em>Hello world!</em>') !== false);
$this->assertEquals(FORMAT_HTML, $message->fullmessageformat);
$this->assertEquals($expectedurl->out(false), $message->contexturl);
$this->assertEquals($expectedurlname, $message->contexturlname);
}
}