Skip to content

Commit

Permalink
lesson: MDL-19890 Fix block-printing code in lesson (I hope)
Browse files Browse the repository at this point in the history
This got broken by one of the commits for MDL-19077 & MDL-19010
  • Loading branch information
tjhunt committed Jul 23, 2009
1 parent d18675a commit 4262a2f
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 255 deletions.
21 changes: 18 additions & 3 deletions lib/ajax/ajaxlib.php
Expand Up @@ -616,12 +616,27 @@ public function get_html() {
* is responsible for outputting this HTML promptly.
*/
public function asap() {
if ($this->is_done()) {
return;
}
if (!$this->manager->is_head_done()) {
$this->in_head();
return '';
} else {
return $this->now();
}
}

/**
* Return the required JavaScript immediately, so it can be included in some
* HTML that is being built.
*
* This is not really recommeneded. But is necessary in some legacy code that
* includes a .js files that does document.write.
*
* @return string The HTML for the script tag. The caller
* is responsible for making sure it is output.
*/
public function now() {
if ($this->is_done()) {
return '';
}
$output = $this->get_html();
$this->mark_done();
Expand Down
40 changes: 0 additions & 40 deletions mod/lesson/action/continue.html
Expand Up @@ -7,26 +7,6 @@
**/
?>

<!-- Primary layout table -->
<table id="layout-table" cellpadding="0" cellspacing="0">
<tr>
<!-- First Column -->
<?php if (lesson_blocks_have_content($lesson, $pageblocks, BLOCK_POS_LEFT)) { ?>
<td id="left-column" style="width: <?php echo $leftcolumnwidth; ?>px;">
<?php
lesson_print_menu_block($cm->id, $lesson);

if (!empty($CFG->showblocksonmodpages)) {
if ((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
}
}
?>
</td>
<?php } ?>
<!-- Start Primary column -->
<td id="middle-column">

<?php if ($lesson->displayleft) { // Skip navigation anchor ?>

<a name="maincontent" id="maincontent" title="<?php print_string('anchortitle', 'lesson') ?>"></a>
Expand Down Expand Up @@ -97,23 +77,3 @@
<?php } ?>

</form>
</td>
<!-- End primary column -->
<?php if (lesson_blocks_have_content($lesson, $pageblocks, BLOCK_POS_RIGHT)) { ?>
<td id="right-column" style="width: <?php echo $rightcolumnwidth; ?>px;">
<!-- Third column -->
<?php
lesson_print_clock_block($cm->id, $lesson, $timer);
lesson_print_mediafile_block($cm->id, $lesson);

if (!empty($CFG->showblocksonmodpages)) {
if ((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) {
blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
}
}
?>
</td>
<?php } ?>
</tr>
</table>
<!-- End primary layout table -->
7 changes: 2 additions & 5 deletions mod/lesson/action/continue.php
Expand Up @@ -682,7 +682,7 @@
$options = new stdClass;
$options->noclean = true;
$options->para = true;
$feedback = print_simple_box(format_text($page->contents, FORMAT_MOODLE, $options), 'center', '', '', 5, 'generalbox', '', true);
$feedback = print_box(format_text($page->contents, FORMAT_MOODLE, $options), 'generalbox boxaligncenter', '', true);
$feedback .= '<em>'.get_string("youranswer", "lesson").'</em> : '.format_text($studentanswer, FORMAT_MOODLE, $options).
"<div class=\"$class\">".format_text($response, FORMAT_MOODLE, $options).'</div>';
}
Expand Down Expand Up @@ -783,12 +783,9 @@

$PAGE->set_url('mod/lesson/view.php', array('id' => $cm->id, 'pageid' => $page->id));
$PAGE->set_subpage($page->id);
$pageblocks = blocks_setup($PAGE);

$leftcolumnwidth = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
$rightcolumnwidth = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]), 210);

/// Print the header, heading and tabs
lesson_add_pretend_blocks($PAGE, $cm, $lesson, $timer);
lesson_print_header($cm, $course, $lesson, 'view', true, $page->id);

include(dirname(__FILE__).'/continue.html');
Expand Down
209 changes: 99 additions & 110 deletions mod/lesson/locallib.php
Expand Up @@ -230,19 +230,16 @@
function lesson_print_header($cm, $course, $lesson, $currenttab = '', $extraeditbuttons = false, $lessonpageid = NULL) {
global $CFG, $PAGE;

// Note: MDL-19010 there will be further changes to printing header and blocks.
// The code will be much nicer than this eventually.
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$activityname = format_string($lesson->name, true, $course->id);
$strlesson = get_string('modulename', 'lesson');

if (empty($title)) {
$title = "{$course->shortname}: $activityname";
}

/// Build the buttons
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
if (has_capability('mod/lesson:edit', $context)) {
$buttons = update_module_button($cm->id, $course->id, $strlesson);
$buttons = update_module_button($cm->id, $course->id, get_string('modulename', 'lesson'));
if ($extraeditbuttons) {
if ($lessonpageid === NULL) {
print_error('invalidpageid', 'lesson');
Expand All @@ -255,20 +252,6 @@ function lesson_print_header($cm, $course, $lesson, $currenttab = '', $extraedit
'<input type="hidden" name="pageid" value="'.$lessonpageid.'" />'.
'<input type="submit" value="'.get_string('editpagecontent', 'lesson').'" />'.
'</form>';

if (!empty($CFG->showblocksonmodpages) and $PAGE->user_allowed_editing()) {
if ($PAGE->user_is_editing()) {
$onoff = 'off';
} else {
$onoff = 'on';
}
$buttons .= '<form '.$CFG->frametarget.' method="get" action="'.$CFG->wwwroot.'/mod/lesson/view.php">'.
'<input type="hidden" name="id" value="'.$cm->id.'" />'.
'<input type="hidden" name="pageid" value="'.$lessonpageid.'" />'.
'<input type="hidden" name="edit" value="'.$onoff.'" />'.
'<input type="submit" value="'.get_string("blocksedit$onoff").'" />
</form>';
}
}
$buttons = '<span class="edit_buttons">' . $buttons .'</span>';
}
Expand Down Expand Up @@ -1734,54 +1717,93 @@ function lesson_displayleftif($lesson) {
return $lesson->displayleft;
}

/**
*
* @param $cm
* @param $lesson
* @param $page
* @return unknown_type
*/
function lesson_add_pretend_blocks($page, $cm, $lesson, $timer = null) {
$bc = lesson_menu_block_contents($cm->id, $lesson);
if (!empty($bc)) {
$regions = $page->blocks->get_regions();
$firstregion = reset($regions);
$page->blocks->add_pretend_block($bc, $firstregion);
}

$bc = lesson_mediafile_block_contents($cm->id, $lesson);
if (!empty($bc)) {
$page->blocks->add_pretend_block($bc, $page->blocks->get_default_region());
}

if (!empty($timer)) {
$bc = lesson_clock_block_contents($cm->id, $lesson, $timer, $page);
if (!empty($bc)) {
$page->blocks->add_pretend_block($bc, $page->blocks->get_default_region());
}
}
}

/**
* If there is a media file associated with this
* lesson, then print it in a block.
* lesson, return a block_contents that displays it.
*
* @param int $cmid Course Module ID for this lesson
* @param object $lesson Full lesson record object
* @return void
* @return block_contents
**/
function lesson_print_mediafile_block($cmid, $lesson) {
if (!empty($lesson->mediafile)) {
$url = '/mod/lesson/mediafile.php?id='.$cmid;
$options = 'menubar=0,location=0,left=5,top=5,scrollbars,resizable,width='. $lesson->mediawidth .',height='. $lesson->mediaheight;
$name = 'lessonmediafile';

$content = link_to_popup_window ($url, $name, get_string('mediafilepopup', 'lesson'), '', '', get_string('mediafilepopup', 'lesson'), $options, true);
$content .= helpbutton("mediafilestudent", get_string("mediafile", "lesson"), "lesson", true, false, '', true);

print_side_block(get_string('linkedmedia', 'lesson'), $content, NULL, NULL, '', array('class' => 'mediafile'), get_string('linkedmedia', 'lesson'));
function lesson_mediafile_block_contents($cmid, $lesson) {
if (empty($lesson->mediafile)) {
return null;
}

$url = '/mod/lesson/mediafile.php?id='.$cmid;
$options = 'menubar=0,location=0,left=5,top=5,scrollbars,resizable,width='. $lesson->mediawidth .',height='. $lesson->mediaheight;
$name = 'lessonmediafile';

$content = link_to_popup_window ($url, $name, get_string('mediafilepopup', 'lesson'), '', '', get_string('mediafilepopup', 'lesson'), $options, true);
$content .= helpbutton("mediafilestudent", get_string("mediafile", "lesson"), "lesson", true, false, '', true);

$bc = new block_contents();
$bc->title = get_string('linkedmedia', 'lesson');
$bc->set_classes('mediafile');
$bc->content = $content;

return $bc;
}

/**
* If a timed lesson and not a teacher, then
* print the clock
* return a block_contents containing the clock.
*
* @param int $cmid Course Module ID for this lesson
* @param object $lesson Full lesson record object
* @param object $timer Full timer record object
* @return void
* @return block_contents
**/
function lesson_print_clock_block($cmid, $lesson, $timer) {
global $CFG, $PAGE;

function lesson_clock_block_contents($cmid, $lesson, $timer, $page) {
// Display for timed lessons and for students only
$context = get_context_instance(CONTEXT_MODULE, $cmid);
if(!$lesson->timed || has_capability('mod/lesson:manage', $context)) {
return null;
}

// Display for timed lessons and for students only
if($lesson->timed and !has_capability('mod/lesson:manage', $context) and !empty($timer)) {
$content = '<div class="jshidewhenenabled">';
$content .= lesson_print_time_remaining($timer->starttime, $lesson->maxtime, true)."\n";
$content .= '</div>';

$clocksettings = Array('starttime'=>$timer->starttime, 'servertime'=>time(),'testlength'=>($lesson->maxtime * 60));
$content = $PAGE->requires->data_for_js('clocksettings', $clocksettings)->asap();
$content .= $PAGE->requires->js('mod/lesson/timer.js')->asap();
$content .= $PAGE->requires->js_function_call('show_clock')->asap();
$content .= '<div class="jshidewhenenabled">';
$content .= lesson_print_time_remaining($timer->starttime, $lesson->maxtime, true)."\n";
$content .= '</div>';
$clocksettings = array('starttime'=>$timer->starttime, 'servertime'=>time(),'testlength'=>($lesson->maxtime * 60));
$content .= $page->requires->data_for_js('clocksettings', $clocksettings)->now();
$content .= $page->requires->js('mod/lesson/timer.js')->now();
$content .= $page->requires->js_function_call('show_clock')->now();

print_side_block(get_string('timeremaining', 'lesson'), $content, NULL, NULL, '', array('class' => 'clock'), get_string('timeremaining', 'lesson'));
}
$bc = new block_contents();
$bc->title = get_string('timeremaining', 'lesson');
$bc->set_classes('clock');
$bc->content = $content;

return $bc;
}

/**
Expand All @@ -1792,77 +1814,44 @@ function lesson_print_clock_block($cmid, $lesson, $timer) {
* @param object $lesson Full lesson record object
* @return void
**/
function lesson_print_menu_block($cmid, $lesson) {
function lesson_menu_block_contents($cmid, $lesson) {
global $CFG, $DB;

if ($lesson->displayleft) {
$pageid = $DB->get_field('lesson_pages', 'id', array('lessonid' => $lesson->id, 'prevpageid' => 0));
$params = array ("lessonid" => $lesson->id);
$pages = $DB->get_records_select('lesson_pages', "lessonid = :lessonid", $params);
$currentpageid = optional_param('pageid', $pageid, PARAM_INT);

if ($pageid and $pages) {
$content = '<a href="#maincontent" class="skip">'.get_string('skip', 'lesson')."</a>\n<div class=\"menuwrapper\">\n<ul>\n";
if (!$lesson->displayleft) {
return null;
}

while ($pageid != 0) {
$page = $pages[$pageid];
$pageid = $DB->get_field('lesson_pages', 'id', array('lessonid' => $lesson->id, 'prevpageid' => 0));
$params = array ("lessonid" => $lesson->id);
$pages = $DB->get_records_select('lesson_pages', "lessonid = :lessonid", $params);
$currentpageid = optional_param('pageid', $pageid, PARAM_INT);

// Only process branch tables with display turned on
if ($page->qtype == LESSON_BRANCHTABLE and $page->display) {
if ($page->id == $currentpageid) {
$content .= '<li class="selected">'.format_string($page->title,true)."</a></li>\n";
} else {
$content .= "<li class=\"notselected\"><a href=\"$CFG->wwwroot/mod/lesson/view.php?id=$cmid&amp;pageid=$page->id\">".format_string($page->title,true)."</a></li>\n";
}

}
$pageid = $page->nextpageid;
}
$content .= "</ul>\n</div>\n";
print_side_block(get_string('lessonmenu', 'lesson'), $content, NULL, NULL, '', array('class' => 'menu'), get_string('lessonmenu', 'lesson'));
}
if (!$pageid || !$pages) {
return null;
}
}

/**
* This is not ideal, but checks to see if a
* column has "block" content.
*
* In the future, it would be nice if the lesson
* media file, timer and navigation were blocks
* then this would be unnecessary.
*
* @uses $CFG
* @uses $PAGE
* @param object $lesson Full lesson record object
* @param array $pageblocks An array of block instances organized by left and right columns
* @param string $column Pass either BLOCK_POS_RIGHT or BLOCK_POS_LEFT constants
* @return boolean
**/
function lesson_blocks_have_content($lesson, $pageblocks, $column) {
global $CFG, $PAGE;
$content = '<a href="#maincontent" class="skip">'.get_string('skip', 'lesson')."</a>\n<div class=\"menuwrapper\">\n<ul>\n";

// First check lesson conditions
if ($column == BLOCK_POS_RIGHT) {
$managecap = false;
if ($cm = get_coursemodule_from_instance('lesson', $lesson->id, $lesson->course)) {
$managecap = has_capability('mod/lesson:manage', get_context_instance(CONTEXT_MODULE, $cm->id));
}
if (($lesson->timed and !$managecap) or !empty($lesson->mediafile)) {
return true;
}
} else if ($column == BLOCK_POS_LEFT) {
if ($lesson->displayleft) {
return true;
}
}
if (!empty($CFG->showblocksonmodpages)) {
if ((blocks_have_content($pageblocks, $column) || $PAGE->user_is_editing())) {
return true;
while ($pageid != 0) {
$page = $pages[$pageid];

// Only process branch tables with display turned on
if ($page->qtype == LESSON_BRANCHTABLE and $page->display) {
if ($page->id == $currentpageid) {
$content .= '<li class="selected">'.format_string($page->title,true)."</li>\n";
} else {
$content .= "<li class=\"notselected\"><a href=\"$CFG->wwwroot/mod/lesson/view.php?id=$cmid&amp;pageid=$page->id\">".format_string($page->title,true)."</a></li>\n";
}

}
$pageid = $page->nextpageid;
}
$content .= "</ul>\n</div>\n";

return false;
}
$bc = new block_contents();
$bc->title = get_string('lessonmenu', 'lesson');
$bc->set_classes('menu');
$bc->content = $content;

?>
return $bc;
}

0 comments on commit 4262a2f

Please sign in to comment.