/** * Convenience method to instantiate the event. * * @param user_competency $usercompetency The user competency. * @return self */ public static function create_from_user_competency_viewed(user_competency $usercompetency) { if (!$usercompetency->get_id()) { throw new \coding_exception('The user competency ID must be set.'); } $params = array('contextid' => $usercompetency->get_context()->id, 'objectid' => $usercompetency->get_id(), 'relateduserid' => $usercompetency->get_userid(), 'other' => array('competencyid' => $usercompetency->get_competencyid())); $event = static::create($params); $event->add_record_snapshot(user_competency::TABLE, $usercompetency->to_record()); return $event; }
/** * Convenience method to instantiate the event. * * @param evidence $evidence The evidence. * @param user_competency $usercompetency The user competency object linked to the evidence. * @param bool $recommend The recommend flag. * @return evidence_created * @throws \coding_exception */ public static final function create_from_evidence(evidence $evidence, user_competency $usercompetency, $recommend) { // Make sure we have a valid evidence. if (!$evidence->get_id()) { throw new \coding_exception('The evidence ID must be set.'); } // Make sure we have a valid user competency. if (!$usercompetency->get_id()) { throw new \coding_exception('The user competency ID must be set.'); } // Make sure that the a proper user competecy is linked to the evidence. if ($evidence->get_usercompetencyid() != $usercompetency->get_id()) { throw new \coding_exception('The user competency linked with this evidence is invalid.'); } $event = static::create(['contextid' => $evidence->get_contextid(), 'objectid' => $evidence->get_id(), 'userid' => $evidence->get_actionuserid(), 'relateduserid' => $usercompetency->get_userid(), 'other' => ['usercompetencyid' => $usercompetency->get_id(), 'competencyid' => $usercompetency->get_competencyid(), 'action' => $evidence->get_action(), 'recommend' => $recommend]]); // Add record snapshot for the evidence. $event->add_record_snapshot(evidence::TABLE, $evidence->to_record()); // Add record snapshot for the user competency. $event->add_record_snapshot(user_competency::TABLE, $usercompetency->to_record()); return $event; }
/** * Apply the competency rules from a user competency. * * The user competency passed should be one that was recently marked as complete. * A user competency is considered 'complete' when it's proficiency value is true. * * This method will check if the parent of this usercompetency's competency has any * rules and if so will see if they match. When matched it will take the required * step to add evidence and trigger completion, etc... * * @param user_competency $usercompetency The user competency recently completed. * @param competency|null $competency The competency of the user competency, useful to avoid unnecessary read. * @return void */ protected static function apply_competency_rules_from_usercompetency(user_competency $usercompetency, competency $competency = null) { // Perform some basic checks. if (!$usercompetency->get_proficiency()) { throw new coding_exception('The user competency passed is not completed.'); } if ($competency === null) { $competency = $usercompetency->get_competency(); } if ($competency->get_id() != $usercompetency->get_competencyid()) { throw new coding_exception('Mismatch between user competency and competency.'); } // Fetch the parent. $parent = $competency->get_parent(); if ($parent === null) { return; } // The parent should have a rule, and a meaningful outcome. $ruleoutcome = $parent->get_ruleoutcome(); if ($ruleoutcome == competency::OUTCOME_NONE) { return; } $rule = $parent->get_rule_object(); if ($rule === null) { return; } // Fetch or create the user competency for the parent. $userid = $usercompetency->get_userid(); $parentuc = user_competency::get_record(array('userid' => $userid, 'competencyid' => $parent->get_id())); if (!$parentuc) { $parentuc = user_competency::create_relation($userid, $parent->get_id()); $parentuc->create(); } // Does the rule match? if (!$rule->matches($parentuc)) { return; } // Figuring out what to do. $recommend = false; if ($ruleoutcome == competency::OUTCOME_EVIDENCE) { $action = evidence::ACTION_LOG; } else { if ($ruleoutcome == competency::OUTCOME_RECOMMEND) { $action = evidence::ACTION_LOG; $recommend = true; } else { if ($ruleoutcome == competency::OUTCOME_COMPLETE) { $action = evidence::ACTION_COMPLETE; } else { throw new moodle_exception('Unexpected rule outcome: ' + $ruleoutcome); } } } // Finally add an evidence. static::add_evidence($userid, $parent, $parent->get_context()->id, $action, 'evidence_competencyrule', 'core_competency', null, $recommend); }
/** * 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 (!get_config('core_competency', '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. if (empty($plan)) { $urlname = get_string('userplans', 'core_competency'); $url = url::plans($uc->get_userid()); } else { $urlname = $competencyname; $url = url::user_competency_in_plan($uc->get_userid(), $uc->get_competencyid(), $plan->get_id()); } // Construct the message content. $fullmessagehtml = get_string('usercommentedonacompetencyhtml', 'core_competency', 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', 'core_competency', array('fullname' => $fullname, 'competency' => $competencyname, 'comment' => $comment->content, 'url' => $url->out(false))); } else { $format = FORMAT_HTML; $fullmessage = $fullmessagehtml; } $message = new \core\message\message(); $message->courseid = SITEID; $message->component = 'moodle'; $message->name = 'competencyusercompcomment'; $message->notification = 1; $message->userfrom = core_user::get_noreply_user(); $message->subject = get_string('usercommentedonacompetencysubject', 'core_competency', $fullname); $message->fullmessage = $fullmessage; $message->fullmessageformat = $format; $message->fullmessagehtml = $fullmessagehtml; $message->smallmessage = get_string('usercommentedonacompetencysmall', 'core_competency', 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; $url = url::plan($plan->get_id()); // Construct the message content. $fullmessagehtml = get_string('usercommentedonaplanhtml', 'core_competency', 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', 'core_competency', array('fullname' => $fullname, 'plan' => $planname, 'comment' => $comment->content, 'url' => $url->out(false))); } else { $format = FORMAT_HTML; $fullmessage = $fullmessagehtml; } $message = new \core\message\message(); $message->courseid = SITEID; $message->component = 'moodle'; $message->name = 'competencyplancomment'; $message->notification = 1; $message->userfrom = core_user::get_noreply_user(); $message->subject = get_string('usercommentedonaplansubject', 'core_competency', $fullname); $message->fullmessage = $fullmessage; $message->fullmessageformat = $format; $message->fullmessagehtml = $fullmessagehtml; $message->smallmessage = get_string('usercommentedonaplansmall', 'core_competency', 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); } } } }