Skip to content

Commit

Permalink
Merged in LT-1841_indent (pull request moodle#38)
Browse files Browse the repository at this point in the history
Lt 1841_indent
  • Loading branch information
Russell Smith committed Aug 4, 2015
2 parents edcc222 + 848450d commit a17fdcd
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 22 deletions.
24 changes: 22 additions & 2 deletions blocks/oua_forum_recent_posts/block_oua_forum_recent_posts.php
Expand Up @@ -67,8 +67,10 @@ public function get_content() {
// $courseforums = get_fast_modinfo($course)->get_instances_of('forum');
$courseforums = forum_get_readable_forums($USER->id, $course->id);
$firsthiddenforum = null;
foreach($courseforums as $cmforum) {
if($cmforum->cm->section == $parentsectionid && $cmforum->cm->indent >= 2) {

foreach ($courseforums as $cmforum) {
if ($cmforum->cm->section == $parentsectionid && $cmforum->cm->uservisible
&& $this->module_is_hidden($cmforum->cm)) {
$firsthiddenforum = $cmforum;
break;
}
Expand All @@ -86,6 +88,24 @@ public function get_content() {
return $this->content;
}

/**
* Determine if this forum is on that should be included in the list of invisible forums.
*
* Unless the course format used supports the module_is_visible function, then all forums
* will be visible and included in this block.
*/
protected function module_is_hidden($cm) {
// The course format needs to tell us if it's hidden or not.
$courseformat = course_get_format($this->page->course);
$cancall = is_callable(array($courseformat, 'module_is_hidden_from_view'));

if (!$cancall) {
return false;
}

return $courseformat->module_is_hidden_from_view($cm);
}

/**
* Given a course and a date, prints a summary of all the new
* messages posted in the course since that date
Expand Down
Expand Up @@ -29,6 +29,19 @@ protected function setUp() {
global $CFG;
}

/**
* Indent a created module. Moodle's generator function doesn't handle indent correctly.
* @param $cm stdClass The course module to indent.
*/
private function indent_module($cm) {
global $DB;
// Moodle 2.9 ignores indent as an option when creating plugins, we need to update the database.
$coursemoduledata = $DB->get_record('course_modules', array('id' => $cm->cmid));
$coursemoduledata->indent = 5;
$DB->update_record('course_modules', $coursemoduledata);
rebuild_course_cache($cm->course);
}

static function setAdminUser() {
global $USER;
parent::setAdminUser();
Expand All @@ -46,8 +59,6 @@ static function setAdminUser() {
* @test
*/
public function test_forum_recent_post_block_no_errors() {
$this->markTestIncomplete('Test development has been deferred until we have completed functionality.');
return;
global $PAGE;

self::setAdminUser();
Expand All @@ -61,17 +72,90 @@ public function test_forum_recent_post_block_no_errors() {

$html = $block->get_content()->text;

$expected = <<<BLOCK
<div>
$this->assertEmpty($html, "The html output of the message should be in the format expected");
}

public function test_forum_recent_post_block_shows_no_forum_posts_when_not_hidden() {
global $PAGE;

$this->resetAfterTest();
self::setAdminUser();

</div>
BLOCK;
$course = $this->getDataGenerator()->create_course(array('format' => 'invisible'));

$courseformat = course_get_format($course);

$record = array('course' => $course->id);

$forum = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_instance($record);
$PAGE->set_cm(get_coursemodule_from_id('forum', $forum->cmid));

$record = new stdClass();
$record->course = $course->id;
$record->userid = 2;
$record->forum = $forum->id;
$discussion1 = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
$discussion2 = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

$block = $this->getDataGenerator()->create_block('oua_forum_recent_posts');
$block = block_instance('oua_forum_recent_posts', $block);

$html = $block->get_content()->text;

$this->assertEquals($expected, $html, "The html output of the message should be in the format expected");
$this->assertEmpty($html, 'No posts should be returned as the item is not hidden.');
}

public function test_forum_recent_post_block_shows_recent_forum_posts() {
$this->markTestIncomplete('Test development has been deferred until we have completed functionality.');
global $PAGE;

$this->resetAfterTest();

self::setAdminUser();

$course = $this->getDataGenerator()->create_course(array('format' => 'invisible'));

$courseformat = course_get_format($course);

$record = array('course' => $course->id);

$forum = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_instance($record);
$this->indent_module($forum);
$PAGE->set_cm(get_coursemodule_from_id('forum', $forum->cmid));

$record = new stdClass();
$record->course = $course->id;
$record->userid = 2;
$record->forum = $forum->id;
$discussion1 = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
sleep(1);
$discussion2 = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

$block = $this->getDataGenerator()->create_block('oua_forum_recent_posts');
$block = block_instance('oua_forum_recent_posts', $block);

$html = $block->get_content()->text;

$doc = new DOMDocument();
$doc->loadHTML($html);
$selector = new DOMXPath($doc);
$result = $selector->query('//div[@class="head"]');
$this->assertEquals(2, $result->length, 'There must be 2 visible posts.');

sleep(1);
$discussion3 = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
sleep(1);
$discussion4 = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
$block->refresh_content();
$html = $block->get_content()->text;

$doc->loadHTML($html);
$selector = new DOMXPath($doc);
$result = $selector->query('//div[@class="head"]');
$this->assertEquals(3, $result->length, 'There must be 3 visible posts.');
$this->assertNotContains($discussion1->name, $html);
$this->assertContains($discussion2->name, $html);
$this->assertContains($discussion3->name, $html);
$this->assertContains($discussion4->name, $html);
}

public function test_forum_recent_post_block_contains_only_posts_from_this_section() {
Expand Down
28 changes: 25 additions & 3 deletions course/format/invisible/lib.php
Expand Up @@ -332,16 +332,20 @@ public function module_to_display($modinfo, $currentcmid = false, $providebestat
return $this->module_return($type, null, null);
}

/**
* Determine if a course module should be displayed to the user based on rules for this course format.
*
* @param $cm cm_info A course module to test.
* @return bool true is to be displayed, false otherwise.
*/
private function module_is_visible($cm) {
// If the item is not user visible, they will never be able to get there so ignore these modules
// in all circumstances.
if (!$cm->uservisible) {
return false;
}

// Exclude modules that are indented so we can easily exclude items from the next/previous links.
// sytles.css has visual indicators for the selectio made here, review that CSS if you are making changes.
if ($cm->indent > 1) {
if ($this->module_is_hidden_from_view($cm)) {
return false;
}

Expand All @@ -352,6 +356,24 @@ private function module_is_visible($cm) {
return true;
}

/**
* Exclude modules that are indented so we can easily exclude items from the next/previous links.
* They are also excluded from other locations based on this hidden criteria.
* eg; recent forum posts block and oua navigation block.
*
* sytles.css has visual indicators for the selection made here, review that CSS if you are making changes.
*
* @param $cm cm_info The module to determine if it's hidden from view.
* @return bool true for hidden, false otherwise.
*/
public function module_is_hidden_from_view($cm) {
if ($cm->indent >= 5) {
return true;
} else {
return false;
}
}

private function module_return($type, $previousactivity, $nextactivity) {
// Determine whether we have enough information to return to the user.
if ($type == 'next') {
Expand Down
5 changes: 2 additions & 3 deletions course/format/invisible/styles.css
Expand Up @@ -8,9 +8,8 @@
.course-content ul.topics li.section .right img.icon { padding: 0 0 4px 0;}
.course-content ul.topics li.section .left .section-handle img.icon { padding:0; vertical-align: baseline; }
/* Indented items in this format don't appear to the user, so display that fact to them. */
.mod-indent-2, .mod-indent-3, .mod-indent-4, .mod-indent-5, .mod-indent-6, .mod-indent-7,
.mod-indent-8, .mod-indent-9, .mod-indent-10, .mod-indent-11, .mod-indent-12, .mod-indent-13,
.mod-indent-14, .mod-indent-15, .mod-indent-huge {
.mod-indent-5, .mod-indent-6, .mod-indent-7, .mod-indent-8, .mod-indent-9, .mod-indent-10,
.mod-indent-11, .mod-indent-12, .mod-indent-13, .mod-indent-14, .mod-indent-15, .mod-indent-huge {
background: url('[[pix:i/show]]') no-repeat center;
}
.jumpmenu {text-align:center;}
28 changes: 23 additions & 5 deletions course/format/invisible/tests/format_invisible_test.php
Expand Up @@ -309,13 +309,11 @@ public function test_module_to_display_progression() {
$moduledatainvisible['visible'] = 0;

$record = array('course' => $course->id);
$recordindented = $record;
$recordindented['indent'] = 2;

$cmfirstlook = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_instance($record, $moduledata);
$cminvisible = $this->getDataGenerator()->get_plugin_generator('mod_page')->create_instance($record, $moduledatainvisible);
$cm1 = $this->getDataGenerator()->get_plugin_generator('mod_page')->create_instance($record, $moduledata);
$cmexcluded = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_instance($recordindented, $moduledata);
$cmexcluded = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_instance($record, $moduledata);
$this->indent_module($cmexcluded);
$cm2 = $this->getDataGenerator()->get_plugin_generator('mod_page')->create_instance($record, $moduledata);

Expand Down Expand Up @@ -413,17 +411,37 @@ public function test_module_to_display_progression() {

/**
* Indent a created module. Moodle's generator function doesn't handle indent correctly.
* @param $cm The course module to indent.
* @param $cm stdClass The course module to indent.
*/
private function indent_module($cm) {
global $DB;
// Moodle 2.9 ignores indent as an option when creating plugins, we need to update the database.
$coursemoduledata = $DB->get_record('course_modules', array('id' => $cm->cmid));
$coursemoduledata->indent = 2;
$coursemoduledata->indent = 5;
$DB->update_record('course_modules', $coursemoduledata);
rebuild_course_cache($cm->course);
}

public function test_module_is_hidden_when_indented_5_times() {
$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course(array('format' => 'invisible'));

$courseformat = course_get_format($course);

$record = array('course' => $course->id);

$cm = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_instance($record);

$modinfo = get_fast_modinfo($course);

$this->assertFalse($courseformat->module_is_hidden_from_view($modinfo->get_cm($cm->cmid)));

$this->indent_module($cm);
$modinfo = get_fast_modinfo($course);
$this->assertTrue($courseformat->module_is_hidden_from_view($modinfo->get_cm($cm->cmid)));
}

/**
* Ensure we limit the output of next and previous to section
* boundaries when calling module_to_display. get_view_url
Expand Down
2 changes: 1 addition & 1 deletion course/format/invisible/tests/view_url_test.php
Expand Up @@ -188,7 +188,7 @@ private function indent_module($cm) {
global $DB;
// Moodle 2.9 ignores indent as an option when creating plugins, we need to update the database.
$coursemoduledata = $DB->get_record('course_modules', array('id' => $cm->cmid));
$coursemoduledata->indent = 2;
$coursemoduledata->indent = 5;
$DB->update_record('course_modules', $coursemoduledata);
rebuild_course_cache($cm->course);
}
Expand Down

0 comments on commit a17fdcd

Please sign in to comment.