/** * Constructor * * @param string $elementName Element name * @param mixed $elementLabel Label(s) for an element * @param array $options Options to control the element's display * @param mixed $attributes Either a typical HTML attribute string or an associative array. */ public function __construct($elementName = null, $elementLabel = null, $options = array(), $attributes = null) { global $OUTPUT; if ($elementName == null) { // This is broken quickforms messing with the constructors. return; } if (!isset($options['courseid'])) { throw new coding_exception('Course id is required for the course_competencies form element'); } $courseid = $options['courseid']; if (!empty($options['cmid'])) { $current = \core_competency\api::list_course_module_competencies_in_course_module($options['cmid']); $ids = array(); foreach ($current as $coursemodulecompetency) { array_push($ids, $coursemodulecompetency->get_competencyid()); } $this->setValue($ids); } $competencies = api::list_course_competencies($courseid); $validoptions = array(); $context = context_course::instance($courseid); foreach ($competencies as $competency) { // We don't need to show the description as part of the options, so just set this to null. $competency['competency']->set_description(null); $exporter = new competency_exporter($competency['competency'], array('context' => $context)); $templatecontext = array('competency' => $exporter->export($OUTPUT)); $id = $competency['competency']->get_id(); $validoptions[$id] = $OUTPUT->render_from_template('tool_lp/competency_summary', $templatecontext); } $attributes['tags'] = false; $attributes['multiple'] = 'multiple'; parent::__construct($elementName, $elementLabel, $validoptions, $attributes); }
/** * Constructor * * @param string $elementName Element name * @param mixed $elementLabel Label(s) for an element * @param array $options Options to control the element's display * @param mixed $attributes Either a typical HTML attribute string or an associative array. */ public function __construct($elementName = null, $elementLabel = null, $options = array(), $attributes = null) { if ($elementName == null) { // This is broken quickforms messing with the constructors. return; } if (!empty($options['cmid'])) { $cmid = $options['cmid']; $current = \core_competency\api::list_course_module_competencies_in_course_module($cmid); // Note: We just pick the outcome set on the first course_module_competency - because in our UI are are // forcing them to be all the same for each activity. if (!empty($current)) { $one = array_pop($current); $this->setValue($one->get_ruleoutcome()); } } $validoptions = course_module_competency::get_ruleoutcome_list(); parent::__construct($elementName, $elementLabel, $validoptions, $attributes); }
/** * Hook the add/edit of the course module. * * @param stdClass $data Data from the form submission. * @param stdClass $course The course. */ function tool_lp_coursemodule_edit_post_actions($data, $course) { if (!get_config('core_competency', 'enabled')) { return $data; } // It seems like the form did not contain any of the form fields, we can return. if (!isset($data->competency_rule) && !isset($data->competencies)) { return $data; } // We bypass the API here and go direct to the persistent layer - because we don't want to do permission // checks here - we need to load the real list of existing course module competencies. $existing = \core_competency\course_module_competency::list_course_module_competencies($data->coursemodule); $existingids = array(); foreach ($existing as $cmc) { array_push($existingids, $cmc->get_competencyid()); } $newids = isset($data->competencies) ? $data->competencies : array(); $removed = array_diff($existingids, $newids); $added = array_diff($newids, $existingids); foreach ($removed as $removedid) { \core_competency\api::remove_competency_from_course_module($data->coursemodule, $removedid); } foreach ($added as $addedid) { \core_competency\api::add_competency_to_course_module($data->coursemodule, $addedid); } if (isset($data->competency_rule)) { // Now update the rules for each course_module_competency. $current = \core_competency\api::list_course_module_competencies_in_course_module($data->coursemodule); foreach ($current as $coursemodulecompetency) { \core_competency\api::set_course_module_competency_ruleoutcome($coursemodulecompetency, $data->competency_rule); } } return $data; }
public function test_list_course_modules_using_competency() { global $SITE; $this->resetAfterTest(true); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $u1 = $dg->create_user(); $u2 = $dg->create_user(); $course = $dg->create_course(); $course2 = $dg->create_course(); $this->setAdminUser(); $f = $lpg->create_framework(); $c = $lpg->create_competency(array('competencyframeworkid' => $f->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $f->get_id())); $cc = api::add_competency_to_course($course->id, $c->get_id()); $cc2 = api::add_competency_to_course($course->id, $c2->get_id()); // First check we get an empty list when there are no links. $expected = array(); $result = api::list_course_modules_using_competency($c->get_id(), $course->id); $this->assertEquals($expected, $result); $pagegenerator = $this->getDataGenerator()->get_plugin_generator('mod_page'); $page = $pagegenerator->create_instance(array('course' => $course->id)); $cm = get_coursemodule_from_instance('page', $page->id); // Add a link and list again. $ccm = api::add_competency_to_course_module($cm, $c->get_id()); $expected = array($cm->id); $result = api::list_course_modules_using_competency($c->get_id(), $course->id); $this->assertEquals($expected, $result); // Check a different course. $expected = array(); $result = api::list_course_modules_using_competency($c->get_id(), $course2->id); $this->assertEquals($expected, $result); // Remove the link and check again. $result = api::remove_competency_from_course_module($cm, $c->get_id()); $expected = true; $this->assertEquals($expected, $result); $expected = array(); $result = api::list_course_modules_using_competency($c->get_id(), $course->id); $this->assertEquals($expected, $result); // Now add 2 links. api::add_competency_to_course_module($cm, $c->get_id()); api::add_competency_to_course_module($cm, $c2->get_id()); $result = api::list_course_module_competencies_in_course_module($cm->id); $this->assertEquals($result[0]->get_competencyid(), $c->get_id()); $this->assertEquals($result[1]->get_competencyid(), $c2->get_id()); // Now re-order. api::reorder_course_module_competency($cm, $c->get_id(), $c2->get_id()); $result = api::list_course_module_competencies_in_course_module($cm->id); $this->assertEquals($result[0]->get_competencyid(), $c2->get_id()); $this->assertEquals($result[1]->get_competencyid(), $c->get_id()); // And re-order again. api::reorder_course_module_competency($cm, $c->get_id(), $c2->get_id()); $result = api::list_course_module_competencies_in_course_module($cm->id); $this->assertEquals($result[0]->get_competencyid(), $c->get_id()); $this->assertEquals($result[1]->get_competencyid(), $c2->get_id()); }