diff --git a/admin/tool/lp/lib.php b/admin/tool/lp/lib.php index 23105619744..2350fa9fd0a 100644 --- a/admin/tool/lp/lib.php +++ b/admin/tool/lp/lib.php @@ -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. * diff --git a/competency/classes/plan.php b/competency/classes/plan.php index 9215b2248bc..4bb4c627c2e 100644 --- a/competency/classes/plan.php +++ b/competency/classes/plan.php @@ -200,7 +200,7 @@ class plan extends persistent { $comment = new comment((object) array( 'client_id' => 'plancommentarea' . $this->get_id(), 'context' => $this->get_context(), - 'component' => 'tool_lp', + 'component' => 'competency', // This cannot be named 'core_competency'. 'itemid' => $this->get_id(), 'area' => 'plan', 'showcount' => true, diff --git a/competency/classes/user_competency.php b/competency/classes/user_competency.php index 1a882b55830..18935445366 100644 --- a/competency/classes/user_competency.php +++ b/competency/classes/user_competency.php @@ -195,7 +195,7 @@ class user_competency extends persistent { $comment = new comment((object) array( 'context' => $this->get_context(), - 'component' => 'tool_lp', + 'component' => 'competency', // This cannot be named 'core_competency'. 'itemid' => $this->get_id(), 'area' => 'user_competency', 'showcount' => true, diff --git a/competency/lib.php b/competency/lib.php index 5272d79cd49..680701c5b22 100644 --- a/competency/lib.php +++ b/competency/lib.php @@ -25,8 +25,246 @@ defined('MOODLE_INTERNAL') || die(); use core_competency\api; +use core_competency\plan; +use core_competency\user_competency; use core_competency\user_evidence; +/** + * Hook when a comment is added. + * + * @param stdClass $comment The comment. + * @param stdClass $params The parameters. + * @return array + */ +function core_competency_comment_add($comment, $params) { + global $USER; + + if (!api::is_enabled()) { + return; + } + + if ($params->commentarea == 'user_competency') { + $uc = new 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() == 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. + // FIXME Use a URL resolver here. + 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 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; + // FIXME Use a URL resolver here. + $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 core_competency_comment_permissions($params) { + if (!api::is_enabled()) { + return array('post' => false, 'view' => false); + } + + if ($params->commentarea == 'user_competency') { + $uc = new 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 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 core_competency_comment_validate($params) { + if (!api::is_enabled()) { + return false; + } + + if ($params->commentarea == 'user_competency') { + if (!user_competency::record_exists($params->itemid)) { + return false; + } + return true; + } else if ($params->commentarea == 'plan') { + if (!plan::record_exists($params->itemid)) { + return false; + } + return true; + } + return false; +} /** * File serving. diff --git a/competency/tests/event_test.php b/competency/tests/event_test.php index a1248fa987c..d87a98ebaea 100644 --- a/competency/tests/event_test.php +++ b/competency/tests/event_test.php @@ -1241,7 +1241,7 @@ class core_competency_event_testcase extends advanced_testcase { $cmt->context = $context; $cmt->area = 'plan'; $cmt->itemid = $plan->get_id(); - $cmt->component = 'tool_lp'; + $cmt->component = 'competency'; $cmt->showcount = 1; $manager = new comment($cmt); $manager->set_post_permission(true); @@ -1255,7 +1255,7 @@ class core_competency_event_testcase extends advanced_testcase { $event = array_pop($events); // Checking that the event contains the expected values. - $this->assertInstanceOf('\tool_lp\event\comment_created', $event); + $this->assertInstanceOf('\core\event\competency_comment_created', $event); $this->assertEquals($context, $event->get_context()); $this->assertEquals($plan->get_id(), $event->other['itemid']); $this->assertEventContextNotUsed($event); @@ -1279,7 +1279,7 @@ class core_competency_event_testcase extends advanced_testcase { $cmt->context = $context; $cmt->area = 'plan'; $cmt->itemid = $plan->get_id(); - $cmt->component = 'tool_lp'; + $cmt->component = 'competency'; $manager = new comment($cmt); $newcomment = $manager->add("Comment to be deleted"); @@ -1291,7 +1291,7 @@ class core_competency_event_testcase extends advanced_testcase { $event = reset($events); // Checking that the event contains the expected values. - $this->assertInstanceOf('\tool_lp\event\comment_deleted', $event); + $this->assertInstanceOf('\core\event\competency_comment_deleted', $event); $this->assertEquals($context, $event->get_context()); $this->assertEquals($plan->get_id(), $event->other['itemid']); $this->assertEventContextNotUsed($event); @@ -1633,7 +1633,7 @@ class core_competency_event_testcase extends advanced_testcase { $cmt->context = $context; $cmt->area = 'user_competency'; $cmt->itemid = $uc->get_id(); - $cmt->component = 'tool_lp'; + $cmt->component = 'competency'; $cmt->showcount = 1; $manager = new comment($cmt); @@ -1646,7 +1646,7 @@ class core_competency_event_testcase extends advanced_testcase { $event = reset($events); // Checking that the event contains the expected values. - $this->assertInstanceOf('\tool_lp\event\comment_created', $event); + $this->assertInstanceOf('\core\event\competency_comment_created', $event); $this->assertEquals($context, $event->get_context()); $this->assertEquals($uc->get_id(), $event->other['itemid']); $this->assertEventContextNotUsed($event); @@ -1674,7 +1674,7 @@ class core_competency_event_testcase extends advanced_testcase { $cmt->context = $context; $cmt->area = 'user_competency'; $cmt->itemid = $uc->get_id(); - $cmt->component = 'tool_lp'; + $cmt->component = 'competency'; $manager = new comment($cmt); $newcomment = $manager->add("Comment to be deleted"); @@ -1686,7 +1686,7 @@ class core_competency_event_testcase extends advanced_testcase { $event = reset($events); // Checking that the event contains the expected values. - $this->assertInstanceOf('\tool_lp\event\comment_deleted', $event); + $this->assertInstanceOf('\core\event\competency_comment_deleted', $event); $this->assertEquals($context, $event->get_context()); $this->assertEquals($uc->get_id(), $event->other['itemid']); $this->assertEventContextNotUsed($event); diff --git a/admin/tool/lp/tests/lib_test.php b/competency/tests/lib_test.php similarity index 99% rename from admin/tool/lp/tests/lib_test.php rename to competency/tests/lib_test.php index 346f96e4a15..4726217e76f 100644 --- a/admin/tool/lp/tests/lib_test.php +++ b/competency/tests/lib_test.php @@ -17,7 +17,7 @@ /** * Lib tests. * - * @package tool_lp + * @package core_competency * @copyright 2015 Frédéric Massart - FMCorz.net * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -32,7 +32,7 @@ global $CFG; /** * Lib testcase. * - * @package tool_lp + * @package core_competency * @copyright 2015 Frédéric Massart - FMCorz.net * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ diff --git a/admin/tool/lp/classes/event/comment_created.php b/lib/classes/event/competency_comment_created.php similarity index 87% rename from admin/tool/lp/classes/event/comment_created.php rename to lib/classes/event/competency_comment_created.php index c561fe8f096..ebd6f9df633 100644 --- a/admin/tool/lp/classes/event/comment_created.php +++ b/lib/classes/event/competency_comment_created.php @@ -18,24 +18,24 @@ * Comment created event for tool_lp areas. * * - * @package tool_lp + * @package core_competency * @copyright 2016 Issam Taboubi * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -namespace tool_lp\event; +namespace core\event; defined('MOODLE_INTERNAL') || die(); /** - * Comment created event class for tool_lp areas. + * Comment created event class for core_competency areas. * * - * @package tool_lp + * @package core_competency * @since Moodle 3.1 * @copyright 2016 Issam Taboubi * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class comment_created extends \core\event\comment_created { +class competency_comment_created extends \core\event\comment_created { /** * Returns description of what happened. diff --git a/admin/tool/lp/classes/event/comment_deleted.php b/lib/classes/event/competency_comment_deleted.php similarity index 83% rename from admin/tool/lp/classes/event/comment_deleted.php rename to lib/classes/event/competency_comment_deleted.php index 4b0f4c690a7..4af2f3ecc00 100644 --- a/admin/tool/lp/classes/event/comment_deleted.php +++ b/lib/classes/event/competency_comment_deleted.php @@ -15,27 +15,27 @@ // along with Moodle. If not, see . /** - * Comment deleted event for tool_lp areas. + * Comment deleted event for core_competency areas. * * - * @package tool_lp + * @package core_competency * @copyright 2016 Issam Taboubi * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -namespace tool_lp\event; +namespace core\event; defined('MOODLE_INTERNAL') || die(); /** - * Comment deleted event class for tool_lp areas. + * Comment deleted event class for core_competency areas. * * - * @package tool_lp + * @package core_competency * @since Moodle 3.1 * @copyright 2016 Issam Taboubi * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class comment_deleted extends \core\event\comment_deleted { +class competency_comment_deleted extends \core\event\comment_deleted { /** * Returns description of what happened.