/** * Export the data. * * @param renderer_base $output * @return stdClass */ public function export_for_template(\renderer_base $output) { $frameworks = array(); $scales = array(); $planexporter = new plan_exporter($this->plan, array('template' => $this->plan->get_template())); $data = new stdClass(); $data->plan = $planexporter->export($output); $data->competencies = array(); $data->pluginbaseurl = (new moodle_url('/admin/tool/lp'))->out(false); $data->contextid = $this->plan->get_context()->id; if ($data->plan->iscompleted) { $ucproperty = 'usercompetencyplan'; $ucexporter = 'core_competency\\external\\user_competency_plan_exporter'; } else { $ucproperty = 'usercompetency'; $ucexporter = 'core_competency\\external\\user_competency_exporter'; } $pclist = api::list_plan_competencies($this->plan); $proficientcount = 0; foreach ($pclist as $pc) { $comp = $pc->competency; $usercomp = $pc->{$ucproperty}; // Get the framework. if (!isset($frameworks[$comp->get_competencyframeworkid()])) { $frameworks[$comp->get_competencyframeworkid()] = $comp->get_framework(); } $framework = $frameworks[$comp->get_competencyframeworkid()]; // Get the scale. $scaleid = $comp->get_scaleid(); if ($scaleid === null) { $scaleid = $framework->get_scaleid(); } if (!isset($scales[$framework->get_scaleid()])) { $scales[$framework->get_scaleid()] = $framework->get_scale(); } $scale = $scales[$framework->get_scaleid()]; // Prepare the data. $record = new stdClass(); $exporter = new competency_exporter($comp, array('context' => $framework->get_context())); $record->competency = $exporter->export($output); // Competency path. $exporter = new competency_path_exporter(['ancestors' => $comp->get_ancestors(), 'framework' => $framework, 'context' => $framework->get_context()]); $record->comppath = $exporter->export($output); $exporter = new $ucexporter($usercomp, array('scale' => $scale)); $record->{$ucproperty} = $exporter->export($output); $data->competencies[] = $record; if ($usercomp->get_proficiency()) { $proficientcount++; } } $data->competencycount = count($data->competencies); $data->proficientcompetencycount = $proficientcount; if ($data->competencycount) { $data->proficientcompetencypercentage = (double) $proficientcount / (double) $data->competencycount * 100.0; } else { $data->proficientcompetencypercentage = 0.0; } $data->proficientcompetencypercentageformatted = format_float($data->proficientcompetencypercentage); return $data; }
/** * Convenience method to instantiate the plan review stopped event. * * * @param plan $plan The plan. * @return self */ public static final function create_from_plan(plan $plan) { if (!$plan->get_id()) { throw new \coding_exception('The plan ID must be set.'); } $event = static::create(array('contextid' => $plan->get_context()->id, 'objectid' => $plan->get_id(), 'relateduserid' => $plan->get_userid())); $event->add_record_snapshot(plan::TABLE, $plan->to_record()); return $event; }
/** * Manually grade a user competency from the plans page. * * @param mixed $planorid * @param int $competencyid * @param int $grade * @param string $note A note to attach to the evidence * @return array of \core_competency\user_competency */ public static function grade_competency_in_plan($planorid, $competencyid, $grade, $note = null) { global $USER; static::require_enabled(); $plan = $planorid; if (!is_object($planorid)) { $plan = new plan($planorid); } $context = $plan->get_context(); if (!user_competency::can_grade_user($plan->get_userid())) { throw new required_capability_exception($context, 'moodle/competency:competencygrade', 'nopermissions', ''); } // Throws exception if competency not in plan. $competency = $plan->get_competency($competencyid); $competencycontext = $competency->get_context(); if (!has_any_capability(array('moodle/competency:competencyview', 'moodle/competency:competencymanage'), $competencycontext)) { throw new required_capability_exception($competencycontext, 'moodle/competency:competencyview', 'nopermissions', ''); } $action = evidence::ACTION_OVERRIDE; $desckey = 'evidence_manualoverrideinplan'; $result = self::add_evidence($plan->get_userid(), $competency, $context->id, $action, $desckey, 'core_competency', $plan->get_name(), false, null, $grade, $USER->id, $note); if ($result) { $uc = static::get_user_competency($plan->get_userid(), $competency->get_id()); $event = \core\event\competency_user_competency_rated_in_plan::create_from_user_competency($uc, $plan->get_id()); $event->trigger(); } return $result; }
/** * Unlink the plan from the template. * * @param int $planid The plan id * @return bool */ public static function unlink_plan_from_template($planid) { $params = self::validate_parameters(self::unlink_plan_from_template_parameters(), array('planid' => $planid)); $plan = new plan($params['planid']); self::validate_context($plan->get_context()); return api::unlink_plan_from_template($plan); }
/** * 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); } } } }