protected function get_other_values(renderer_base $output) { $other = array(); if (!empty($this->related['actionuser'])) { $exporter = new user_summary_exporter($this->related['actionuser']); $actionuser = $exporter->export($output); $other['actionuser'] = $actionuser; } $other['description'] = $this->persistent->get_description(); $other['userdate'] = userdate($this->persistent->get_timecreated()); if ($this->persistent->get_grade() === null) { $gradename = '-'; } else { $gradename = $this->related['scale']->scale_items[$this->persistent->get_grade() - 1]; } $other['gradename'] = $gradename; // Try to guess the user from the user competency. $userid = null; if ($this->related['usercompetency']) { $userid = $this->related['usercompetency']->get_userid(); } else { if ($this->related['usercompetencyplan']) { $userid = $this->related['usercompetencyplan']->get_userid(); } else { $uc = user_competency::get_record(['id' => $this->persistent->get_usercompetencyid()]); $userid = $uc->get_userid(); } } $other['candelete'] = evidence::can_delete_user($userid); return $other; }
protected function get_other_values(renderer_base $output) { // Arrays are copy on assign. $related = $this->related; $result = new stdClass(); // Remove course from related as it is not wanted by the user_competency_summary_exporter. unset($related['course']); $related['usercompetencyplan'] = null; $related['usercompetency'] = null; $exporter = new user_competency_summary_exporter(null, $related); $result->usercompetencysummary = $exporter->export($output); $result->usercompetencysummary->cangrade = user_competency::can_grade_user_in_course($this->related['user']->id, $this->related['course']->id); $context = context_course::instance($this->related['course']->id); $exporter = new course_summary_exporter($this->related['course'], array('context' => $context)); $result->course = $exporter->export($output); $coursemodules = api::list_course_modules_using_competency($this->related['competency']->get_id(), $this->related['course']->id); $fastmodinfo = get_fast_modinfo($this->related['course']->id); $exportedmodules = array(); foreach ($coursemodules as $cm) { $cminfo = $fastmodinfo->cms[$cm]; $cmexporter = new course_module_summary_exporter(null, array('cm' => $cminfo)); $exportedmodules[] = $cmexporter->export($output); } $result->coursemodules = $exportedmodules; return (array) $result; }
/** * Convenience method to instantiate the event. * * @param user_competency $usercompetency The user competency. * @param int $planid The plan ID * @return self */ public static function create_from_user_competency(user_competency $usercompetency, $planid) { 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(), 'grade' => $usercompetency->get_grade(), 'planid' => $planid)); $event = static::create($params); $event->add_record_snapshot(user_competency::TABLE, $usercompetency->to_record()); return $event; }
/** * Whether or not the rule is matched. * * @param user_competency $usercompetency The user competency. * @return bool */ public function matches(user_competency $usercompetency) { global $DB; // TODO Improve performance here, perhaps the caller could already provide records. $children = competency::get_records(array('parentid' => $this->competency->get_id())); if (empty($children)) { // Leaves are not compatible with this rule. return false; } $ids = array(); foreach ($children as $child) { $ids[] = $child->get_id(); } list($insql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED); $sql = "userid = :userid\n AND proficiency = :proficiency\n AND competencyid {$insql}"; $params['userid'] = $usercompetency->get_userid(); $params['proficiency'] = 1; // Is the user is marked as proficient in all children? return user_competency::count_records_select($sql, $params) === count($ids); }
/** * Whether or not the rule is matched. * * @param user_competency $usercompetency The user competency. * @return bool */ public function matches(user_competency $usercompetency) { global $DB; $config = $this->get_config(); $pointsrequired = $config->base->points; // Index by competency ID and extract required. $compsrules = array(); $requiredids = array(); foreach ($config->competencies as $comp) { $compsrules[$comp->id] = $comp; if ($comp->required) { $requiredids[$comp->id] = $comp->id; } } // Find all the user competency records. list($insql, $params) = $DB->get_in_or_equal(array_keys($compsrules), SQL_PARAMS_NAMED); $sql = "userid = :userid\n AND proficiency = :proficiency\n AND competencyid {$insql}"; $params['userid'] = $usercompetency->get_userid(); $params['proficiency'] = 1; $ucs = user_competency::get_records_select($sql, $params, '', 'competencyid'); // Check that all the required are found. if (!empty($requiredids)) { $unmetrequired = array_diff_key($requiredids, $ucs); if (!empty($unmetrequired)) { return false; } } // Check that we have enough points. $points = 0; foreach ($compsrules as $compid => $comp) { if (array_key_exists($compid, $ucs)) { $points += $comp->points; } } return $points >= $pointsrequired; }
/** * 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; }
protected function get_other_values(renderer_base $output) { $result = new stdClass(); if ($this->persistent->get_grade() === null) { $gradename = '-'; } else { $gradename = $this->related['scale']->scale_items[$this->persistent->get_grade() - 1]; } $result->gradename = $gradename; if ($this->persistent->get_proficiency() === null) { $proficiencyname = get_string('no'); } else { $proficiencyname = get_string($this->persistent->get_proficiency() ? 'yes' : 'no'); } $result->proficiencyname = $proficiencyname; $statusname = '-'; if ($this->persistent->get_status() != user_competency::STATUS_IDLE) { $statusname = (string) user_competency::get_status_name($this->persistent->get_status()); } $result->statusname = $statusname; $result->canrequestreview = $this->persistent->can_request_review(); $result->canreview = $this->persistent->can_review(); $result->isstatusidle = $this->persistent->get_status() == user_competency::STATUS_IDLE; $result->isstatusinreview = $this->persistent->get_status() == user_competency::STATUS_IN_REVIEW; $result->isstatuswaitingforreview = $this->persistent->get_status() == user_competency::STATUS_WAITING_FOR_REVIEW; $result->isrequestreviewallowed = $result->canrequestreview && $result->isstatusidle; $result->iscancelreviewrequestallowed = $result->canrequestreview && $result->isstatuswaitingforreview; $result->isstartreviewallowed = $result->canreview && $result->isstatuswaitingforreview; $result->isstopreviewallowed = $result->canreview && $result->isstatusinreview; if (!empty($result->isstatusinreview)) { // TODO Make this more efficient. $userexporter = new user_summary_exporter(core_user::get_user($this->persistent->get_reviewerid(), '*', MUST_EXIST)); $result->reviewer = $userexporter->export($output); } $result->url = url::user_competency($this->persistent->get_id())->out(false); return (array) $result; }
/** * Test evidence_created event by linking an invalid user competency to an evidence. * * @expectedException coding_exception * @expectedExceptionMessage The user competency linked with this evidence is invalid. */ public function test_evidence_created_with_invalid_user_competency() { $this->resetAfterTest(true); $dg = $this->getDataGenerator(); $syscontext = context_system::instance(); // Create students. $student = $dg->create_user(); $student2 = $dg->create_user(); // Create a competency for the course. $lpg = $dg->get_plugin_generator('core_competency'); $framework = $lpg->create_framework(); $comp = $lpg->create_competency(['competencyframeworkid' => $framework->get_id()]); // Create a different user competency. $otheruc = \core_competency\user_competency::create_relation($student2->id, $comp->get_id()); $otheruc->create(); // Add evidence. $recommend = false; $evidence = api::add_evidence($student->id, $comp, $syscontext, \core_competency\evidence::ACTION_OVERRIDE, 'commentincontext', 'core', null, $recommend, null, 1); // We expect this to fail and throw a coding exception. \core\event\competency_evidence_created::create_from_evidence($evidence, $otheruc, $recommend)->trigger(); }
/** * Assert that a competency was graded in a course. * * @param int $courseid The course ID. * @param int $userid The user ID. * @param int $compid The competency ID. * @param int $grade The grade. */ protected function assertSuccessWithGradeCompetencyInCourse($courseid, $userid, $compid, $grade = 1) { $beforecount = evidence::count_records(); api::grade_competency_in_course($courseid, $userid, $compid, $grade); $this->assertEquals($beforecount + 1, evidence::count_records()); $uc = user_competency::get_record(array('userid' => $userid, 'competencyid' => $compid)); $records = evidence::get_records(array(), 'id', 'DESC', 0, 1); $evidence = array_pop($records); $this->assertEquals($uc->get_id(), $evidence->get_usercompetencyid()); }
/** * Manually grade a user course competency from the course page. * * This may push the rating to the user competency * if the course is configured this way. * * @param mixed $courseorid * @param int $userid * @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_course($courseorid, $userid, $competencyid, $grade, $note = null) { global $USER, $DB; static::require_enabled(); $course = $courseorid; if (!is_object($courseorid)) { $course = $DB->get_record('course', array('id' => $courseorid)); } $context = context_course::instance($course->id); // Check that we can view the user competency details in the course. if (!user_competency::can_read_user_in_course($userid, $course->id)) { throw new required_capability_exception($context, 'moodle/competency:usercompetencyview', 'nopermissions', ''); } // Validate the permission to grade. if (!user_competency::can_grade_user_in_course($userid, $course->id)) { throw new required_capability_exception($context, 'moodle/competency:competencygrade', 'nopermissions', ''); } // Check that competency is in course and visible to the current user. $competency = course_competency::get_competency($course->id, $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', ''); } // Check that the user is enrolled in the course, and is "gradable". if (!is_enrolled($context, $userid, 'moodle/competency:coursecompetencygradable')) { throw new coding_exception('The competency may not be rated at this time.'); } $action = evidence::ACTION_OVERRIDE; $desckey = 'evidence_manualoverrideincourse'; $result = self::add_evidence($userid, $competency, $context->id, $action, $desckey, 'core_competency', $context->get_context_name(), false, null, $grade, $USER->id, $note); if ($result) { $all = user_competency_course::get_multiple($userid, $course->id, array($competency->get_id())); $uc = reset($all); $event = \core\event\competency_user_competency_rated_in_course::create_from_user_competency_course($uc); $event->trigger(); } return $result; }
/** * Create a new user competency. * * @param array|stdClass $record * @return user_competency */ public function create_user_competency($record = null) { $record = (object) $record; if (!isset($record->userid)) { throw new coding_exception('The userid value is required.'); } if (!isset($record->competencyid)) { throw new coding_exception('The competencyid value is required.'); } $usercompetency = new user_competency(0, $record); $usercompetency->create(); return $usercompetency; }
/** * Returns true when some competencies of the framework have user competencies. * * This is useful to determine if the framework, or part of it, should be locked down. * * @return boolean */ public function has_user_competencies() { return user_competency::has_records_for_framework($this->get_id()) || user_competency_plan::has_records_for_framework($this->get_id()); }
public function test_who_can_change_settings() { global $CFG, $DB; $this->resetAfterTest(true); $syscontext = context_system::instance(); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $role = create_role('Settings changer role', 'settingschanger', 'Someone who can change course competency settings'); assign_capability('moodle/competency:coursecompetencyconfigure', CAP_ALLOW, $role, $syscontext->id); assign_capability('moodle/competency:competencygrade', CAP_ALLOW, $role, $syscontext->id); assign_capability('moodle/competency:coursecompetencyview', CAP_ALLOW, $role, $syscontext->id); assign_capability('moodle/competency:planview', CAP_ALLOW, $role, $syscontext->id); $gradedrole = create_role('Graded role', 'graded', 'Someone who can be graded'); assign_capability('moodle/competency:coursecompetencygradable', CAP_ALLOW, $gradedrole, $syscontext->id); $c1 = $dg->create_course(); $u1 = $dg->create_user(); $u2 = $dg->create_user(); $u3 = $dg->create_user(); $framework = $lpg->create_framework(); $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $lpg->create_course_competency(array('competencyid' => $comp1->get_id(), 'courseid' => $c1->id)); $lpg->create_course_competency(array('competencyid' => $comp2->get_id(), 'courseid' => $c1->id)); // Enrol the user. $dg->enrol_user($u1->id, $c1->id); role_assign($gradedrole, $u1->id, $syscontext->id); // Assign roles. role_assign($role, $u2->id, $syscontext->id); $this->setUser($u2); set_config('pushcourseratingstouserplans', true, 'core_competency'); $coursesettings = course_competency_settings::get_by_courseid($c1->id); $this->assertTrue((bool) $coursesettings->get_pushratingstouserplans()); set_config('pushcourseratingstouserplans', false, 'core_competency'); $coursesettings = course_competency_settings::get_by_courseid($c1->id); $this->assertFalse((bool) $coursesettings->get_pushratingstouserplans()); api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true)); $coursesettings = course_competency_settings::get_by_courseid($c1->id); $this->assertTrue((bool) $coursesettings->get_pushratingstouserplans()); set_config('pushcourseratingstouserplans', true, 'core_competency'); api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => false)); $coursesettings = course_competency_settings::get_by_courseid($c1->id); $this->assertFalse((bool) $coursesettings->get_pushratingstouserplans()); // Right now the setting is false. api::grade_competency_in_course($c1->id, $u1->id, $comp1->get_id(), 1, 'Note'); $filterparams = array('userid' => $u1->id, 'competencyid' => $comp1->get_id()); $usercompcourse = \core_competency\user_competency_course::get_record($filterparams); $usercomp = \core_competency\user_competency::get_record($filterparams); // No grade in plan - only a grade in the course. $this->assertEmpty($usercomp->get_grade()); $this->assertEquals(1, $usercompcourse->get_grade()); api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true)); api::grade_competency_in_course($c1->id, $u1->id, $comp1->get_id(), 2, 'Note 2'); $filterparams = array('userid' => $u1->id, 'competencyid' => $comp1->get_id()); $usercompcourse = \core_competency\user_competency_course::get_record($filterparams); $usercomp = \core_competency\user_competency::get_record($filterparams); // Updated grade in plan - updated grade in the course. $this->assertEquals(2, $usercomp->get_grade()); $this->assertEquals(2, $usercompcourse->get_grade()); $this->setUser($u3); $this->setExpectedException('required_capability_exception'); api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => false)); }
/** * Validate the user competency. * * @param int $value * @return true|lang_string */ protected function validate_usercompetencyid($value) { if (!user_competency::record_exists($value)) { return new lang_string('invaliddata', 'error'); } return true; }
/** * Validates comments. * * @param stdClass $params The parameters. * @return bool */ function core_competency_comment_validate($params) { if (!get_config('core_competency', '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; }
protected function get_other_values(renderer_base $output) { global $DB; $result = new stdClass(); $result->showrelatedcompetencies = true; $competency = $this->related['competency']; $exporter = new competency_summary_exporter(null, array('competency' => $competency, 'context' => $competency->get_context(), 'framework' => $competency->get_framework(), 'linkedcourses' => array(), 'relatedcompetencies' => $this->related['relatedcompetencies'])); $result->competency = $exporter->export($output); $result->cangrade = user_competency::can_grade_user($this->related['user']->id); if ($this->related['user']) { $exporter = new user_summary_exporter($this->related['user']); $result->user = $exporter->export($output); } $related = array('scale' => $competency->get_scale()); if ($this->related['usercompetency']) { $exporter = new user_competency_exporter($this->related['usercompetency'], $related); $result->usercompetency = $exporter->export($output); } if ($this->related['usercompetencyplan']) { $exporter = new user_competency_plan_exporter($this->related['usercompetencyplan'], $related); $result->usercompetencyplan = $exporter->export($output); } if ($this->related['usercompetencycourse']) { $exporter = new user_competency_course_exporter($this->related['usercompetencycourse'], $related); $result->usercompetencycourse = $exporter->export($output); } $allevidence = array(); $usercache = array(); $scale = $competency->get_scale(); $result->evidence = array(); if (count($this->related['evidence'])) { foreach ($this->related['evidence'] as $evidence) { $actionuserid = $evidence->get_actionuserid(); if (!empty($actionuserid)) { $usercache[$evidence->get_actionuserid()] = true; } } $users = array(); if (!empty($usercache)) { list($sql, $params) = $DB->get_in_or_equal(array_keys($usercache)); $users = $DB->get_records_select('user', 'id ' . $sql, $params); } foreach ($users as $user) { $usercache[$user->id] = $user; } foreach ($this->related['evidence'] as $evidence) { $actionuserid = $evidence->get_actionuserid(); $related = array('scale' => $scale, 'usercompetency' => $this->related['usercompetency'] ? $this->related['usercompetency'] : null, 'usercompetencyplan' => $this->related['usercompetencyplan'] ? $this->related['usercompetencyplan'] : null); $related['actionuser'] = !empty($actionuserid) ? $usercache[$actionuserid] : null; $exporter = new evidence_exporter($evidence, $related); $allevidence[] = $exporter->export($output); } $result->evidence = $allevidence; } $usercompetency = !empty($this->related['usercompetency']) ? $this->related['usercompetency'] : null; if (!empty($usercompetency) && $usercompetency->can_read_comments()) { $commentareaexporter = new comment_area_exporter($usercompetency->get_comment_object()); $result->commentarea = $commentareaexporter->export($output); } return (array) $result; }
/** * Check if we can delete competencies safely. * * This moethod does not check any capablities. * Check if competency is used in a plan and user competency. * Check if competency is used in a template. * Check if competency is linked to a course. * * @param array $ids Array of competencies ids. * @return bool True if we can delete the competencies. */ public static function can_all_be_deleted($ids) { if (empty($ids)) { return true; } // Check if competency is used in template. if (template_competency::has_records_for_competencies($ids)) { return false; } // Check if competency is used in plan. if (plan_competency::has_records_for_competencies($ids)) { return false; } // Check if competency is used in course. if (course_competency::has_records_for_competencies($ids)) { return false; } // Check if competency is used in user_competency. if (user_competency::has_records_for_competencies($ids)) { return false; } // Check if competency is used in user_competency_plan. if (user_competency_plan::has_records_for_competencies($ids)) { return false; } return true; }
public function test_create_user_competency() { $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $framework = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $this->assertEquals(0, user_competency::count_records()); $rc = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id())); $rc = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id())); $this->assertEquals(2, user_competency::count_records()); $this->assertInstanceOf('\\core_competency\\user_competency', $rc); }