/** * Test the course prepend rule. */ public function test_course_prepend() { global $DB; $before = $DB->count_records('course_sections', array('course' => $this->course->id)); $this->assertEquals(9, $before); // Apply a rule to delete the section. $rule = \tool_cat\rule::from_record(array('id' => \tool_cat\rule::FAKE_RULE_ID, 'order' => 1, 'rule' => 'prepend_to', 'target' => 'course', 'targetid' => null, 'datatype' => 'standard', 'data' => serialize($this->generate_sectiondata()))); $rule->apply(array($this->course)); // Ensure the section has been created. $this->assertEquals($before + 1, $DB->count_records('course_sections', array('course' => $this->course->id))); }
/** * Test the block delete rule. */ public function test_block_delete() { global $DB; $generator = $this->getDataGenerator(); $context = context_course::instance($this->course->id); $before = $DB->count_records('block_instances', array('parentcontextid' => $context->id)); // Add a block. $block = $generator->create_block('online_users', array('parentcontextid' => $context->id)); // Ensure the block has been created. $this->assertEquals($before + 1, $DB->count_records('block_instances', array('parentcontextid' => $context->id))); // Apply a rule to delete the block. $rule = \tool_cat\rule::from_record(array('id' => \tool_cat\rule::FAKE_RULE_ID, 'order' => 1, 'rule' => 'delete', 'target' => 'block', 'targetid' => 'online_users', 'datatype' => '', 'data' => serialize(''))); $rule->apply(array($this->course)); // Ensure the block has been deleted. $this->assertEquals($before, $DB->count_records('block_instances', array('parentcontextid' => $context->id))); }
/** * Test the region empty rule. */ public function test_block_region_add() { global $DB; $generator = $this->getDataGenerator(); $context = context_course::instance($this->course->id); $before = $DB->count_records('block_instances', array('parentcontextid' => $context->id, 'defaultregion' => \BLOCK_POS_RIGHT, 'pagetypepattern' => 'course-view-*')); // Apply a rule to append a block. $rule = \tool_cat\rule::from_record(array('id' => \tool_cat\rule::FAKE_RULE_ID, 'order' => 1, 'rule' => 'append_to', 'target' => 'block_region', 'targetid' => \BLOCK_POS_RIGHT, 'datatype' => 'block', 'data' => serialize(array('block' => 'feedback')))); $rule->apply(array($this->course)); // Apply a rule to prepend a block. $rule = \tool_cat\rule::from_record(array('id' => \tool_cat\rule::FAKE_RULE_ID, 'order' => 1, 'rule' => 'prepend_to', 'target' => 'block_region', 'targetid' => \BLOCK_POS_RIGHT, 'datatype' => 'block', 'data' => serialize(array('block' => 'html')))); $rule->apply(array($this->course)); // Ensure the html block has a lower weight than the feedback block. $feedbackweight = (int) $DB->get_field('block_instances', 'defaultweight', array('parentcontextid' => $context->id, 'defaultregion' => \BLOCK_POS_RIGHT, 'pagetypepattern' => 'course-view-*', 'blockname' => 'feedback')); $htmlweight = (int) $DB->get_field('block_instances', 'defaultweight', array('parentcontextid' => $context->id, 'defaultregion' => \BLOCK_POS_RIGHT, 'pagetypepattern' => 'course-view-*', 'blockname' => 'html')); $this->assertLessThan($feedbackweight, $htmlweight); // Ensure the block has been created. $this->assertEquals($before + 2, $DB->count_records('block_instances', array('parentcontextid' => $context->id, 'defaultregion' => \BLOCK_POS_RIGHT, 'pagetypepattern' => 'course-view-*'))); }
/** * Returns a list of valid targets for a given rule. * * @param $rule * @return array [string] * @throws \invalid_parameter_exception */ public static function get_targets($rule) { $params = self::validate_parameters(self::get_targets_parameters(), array('rule' => $rule)); $obj = \tool_cat\rule::create_rule($params['rule']); $keys = $obj->get_supported_targets(); // Prettify. $values = array_map(function ($str) { $str = str_replace('_', ' ', $str); return ucwords($str); }, $keys); return array_combine($keys, $values); }
/** * Returns all known category rules to this category. * This includes parent categories. */ public function get_rules() { global $DB; $coursecat = \coursecat::get($this->id, \IGNORE_MISSING); if (!$coursecat) { return array(); } $parents = $coursecat->get_parents(); $categories = array_merge($parents, array($this->id)); list($sql, $params) = $DB->get_in_or_equal($categories); $rules = $DB->get_records_select('tool_cat_rules', 'categoryid ' . $sql, $params); // Sort. // Parent categories first, then by seq. $ret = array(); foreach ($categories as $category) { $buffer = array(); foreach ($rules as $rule) { if ($rule->categoryid != $category) { continue; } $buffer[$rule->seq] = $rule; } // Add the buffer in. ksort($buffer); foreach ($buffer as $rule) { $ret[] = \tool_cat\rule::from_record($rule); } } return $ret; }
<td><p><em>Day, time</em></p><p><em>Day, time</em></p></td> </tr> <tr> <td><strong>Lecturer</strong></td> <td>************</td> <td><a href="mailto:***@kent.ac.uk">***@kent.ac.uk</a></td> <td>01227 82****</td> <td><p><em>Day, time</em></p><p><em>Day, time</em></p></td></tr></tbody></table><em> </em> HTML5; $rule = \tool_cat\rule::from_record(array('id' => \tool_cat\rule::FAKE_RULE_ID, 'order' => 1, 'rule' => 'prepend_to', 'target' => 'section', 'targetid' => 0, 'datatype' => 'text', 'data' => serialize(array('text' => $summary)))); $rule->apply($courses); // Apply first section. foreach ($courses as $course) { // We can't use a rule for this. $DB->set_field('course_sections', 'name', get_course_display_name_for_list($course), array('course' => $course->id, 'section' => 0)); // Prepend an announcements forum. $rule = \tool_cat\rule::from_record(array('id' => \tool_cat\rule::FAKE_RULE_ID, 'order' => 1, 'rule' => 'append_to', 'target' => 'course', 'targetid' => null, 'datatype' => 'news', 'data' => serialize(array('intro' => 'Announcements relating to ' . $course->shortname . ' will be posted here.', 'showdescription' => 1)))); $rule->apply(array($course)); // Append a discussion forum. $rule = \tool_cat\rule::from_record(array('id' => \tool_cat\rule::FAKE_RULE_ID, 'order' => 1, 'rule' => 'append_to', 'target' => 'section', 'targetid' => '0', 'datatype' => 'activity', 'data' => serialize(array('activity' => 'forum', 'name' => 'Discussion Forum ', 'intro' => '<p>(optional) Use this Discussion Forum to discuss the material of ' . $course->shortname . ' with other students and/or staff.</p><p>Please note that posts seeking (or providing) direct solutions to coursework, either assessed or unassessed, are not permitted.</p>', 'showdescription' => 1)))); $rule->apply(array($course)); } // Apply a rule to prepend to the section. $rule = \tool_cat\rule::from_record(array('id' => \tool_cat\rule::FAKE_RULE_ID, 'order' => 1, 'rule' => 'prepend_to', 'target' => 'section', 'targetid' => '0', 'datatype' => 'activity', 'data' => serialize(array('activity' => 'aspirelists', 'name' => 'Reading list')))); $rule->apply($courses); // Append a URL mod to the 7th section. $rule = \tool_cat\rule::from_record(array('id' => \tool_cat\rule::FAKE_RULE_ID, 'order' => 1, 'rule' => 'append_to', 'target' => 'section', 'targetid' => '6', 'datatype' => 'activity', 'data' => serialize(array('activity' => 'url', 'name' => 'Past exam papers Link', 'url' => '')))); $rule->apply($courses); // So we can work something out. print_r(array_keys($courses));
/** * Test the section delete rule. */ public function test_section_delete() { global $DB; $before = $DB->count_records('course_sections', array('course' => $this->course->id)); $this->assertEquals(10, $before); // Apply a rule to delete the section. $rule = \tool_cat\rule::from_record(array('id' => \tool_cat\rule::FAKE_RULE_ID, 'order' => 1, 'rule' => 'delete', 'target' => 'section', 'targetid' => '3', 'datatype' => '', 'data' => serialize(''))); $rule->apply(array($this->course)); // Ensure the section has been deleted. $this->assertEquals($before - 1, $DB->count_records('course_sections', array('course' => $this->course->id))); }