From f982303d6206d2ba78c6f72c0fb1a0efd53a6794 Mon Sep 17 00:00:00 2001 From: Jason Fowler Date: Fri, 28 Sep 2012 11:10:21 +0800 Subject: [PATCH] MDL-35608 - Blocks - Updating the Completion Status block to use HTML Writer --- .../block_completionstatus.php | 148 ++++++++------- blocks/completionstatus/db/upgrade.php | 5 - blocks/completionstatus/details.php | 171 +++++++++--------- .../lang/en/block_completionstatus.php | 1 - blocks/completionstatus/version.php | 4 +- theme/base/style/blocks.css | 3 + theme/upgrade.txt | 5 + 7 files changed, 184 insertions(+), 153 deletions(-) diff --git a/blocks/completionstatus/block_completionstatus.php b/blocks/completionstatus/block_completionstatus.php index b2cb80365a0..e8a8389c947 100644 --- a/blocks/completionstatus/block_completionstatus.php +++ b/blocks/completionstatus/block_completionstatus.php @@ -29,8 +29,8 @@ defined('MOODLE_INTERNAL') || die(); require_once("{$CFG->libdir}/completionlib.php"); /** - * Course completion status - * Displays overall, and individual criteria status for logged in user + * Course completion status. + * Displays overall, and individual criteria status for logged in user. */ class block_completionstatus extends block_base { @@ -38,28 +38,31 @@ class block_completionstatus extends block_base { $this->title = get_string('pluginname', 'block_completionstatus'); } - function applicable_formats() { + public function applicable_formats() { return array('all' => true, 'mod' => false, 'tag' => false, 'my' => false); } public function get_content() { global $USER; - // If content is cached - if ($this->content !== NULL) { + $rows = array(); + $srows = array(); + $prows = array(); + // If content is cached. + if ($this->content !== null) { return $this->content; } - $course = $this->page->course; + $course = $this->page->course; $context = context_course::instance($course->id); - // Create empty content + // Create empty content. $this->content = new stdClass(); // Can edit settings? $can_edit = has_capability('moodle/course:update', $context); - // Get course completion data + // Get course completion data. $info = new completion_info($course); // Don't display if completion isn't enabled! @@ -76,10 +79,10 @@ class block_completionstatus extends block_base { return $this->content; } - // Load criteria to display + // Load criteria to display. $completions = $info->get_completions($USER->id); - // Check if this course has any criteria + // Check if this course has any criteria. if (empty($completions)) { if ($can_edit) { $this->content->text = get_string('nocriteriaset', 'completion'); @@ -87,27 +90,25 @@ class block_completionstatus extends block_base { return $this->content; } - // Check this user is enroled + // Check this user is enroled. if ($info->is_tracked_user($USER->id)) { - // Generate markup for criteria statuses - $shtml = ''; + // Generate markup for criteria statuses. + $data = ''; - // For aggregating activity completion + // For aggregating activity completion. $activities = array(); $activities_complete = 0; - // For aggregating course prerequisites + // For aggregating course prerequisites. $prerequisites = array(); $prerequisites_complete = 0; - // Flag to set if current completion data is inconsistent with - // what is stored in the database + // Flag to set if current completion data is inconsistent with what is stored in the database. $pending_update = false; - // Loop through course criteria + // Loop through course criteria. foreach ($completions as $completion) { - $criteria = $completion->get_criteria(); $complete = $completion->is_complete(); @@ -115,7 +116,7 @@ class block_completionstatus extends block_base { $pending_update = true; } - // Activities are a special case, so cache them and leave them till last + // Activities are a special case, so cache them and leave them till last. if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) { $activities[$criteria->moduleinstance] = $complete; @@ -126,7 +127,7 @@ class block_completionstatus extends block_base { continue; } - // Prerequisites are also a special case, so cache them and leave them till last + // Prerequisites are also a special case, so cache them and leave them till last. if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) { $prerequisites[$criteria->courseinstance] = $complete; @@ -136,50 +137,53 @@ class block_completionstatus extends block_base { continue; } - - $shtml .= ''; - $shtml .= $criteria->get_title(); - $shtml .= ''; - $shtml .= $completion->get_status(); - $shtml .= ''; + $row = new html_table_row(); + $row->cells[0] = new html_table_cell($criteria->get_title()); + $row->cells[1] = new html_table_cell($completion->get_status()); + $row->cells[1]->style = 'text-align: right;'; + $srows[] = $row; } - // Aggregate activities + // Aggregate activities. if (!empty($activities)) { - - $shtml .= ''; - $shtml .= get_string('activitiescompleted', 'completion'); - $shtml .= ''; $a = new stdClass(); $a->first = $activities_complete; $a->second = count($activities); - $shtml .= get_string('firstofsecond', 'block_completionstatus', $a); - $shtml .= ''; + + $row = new html_table_row(); + $row->cells[0] = new html_table_cell(get_string('activitiescompleted', 'completion')); + $row->cells[1] = new html_table_cell(get_string('firstofsecond', 'block_completionstatus', $a)); + $row->cells[1]->style = 'text-align: right;'; + $srows[] = $row; } - // Aggregate prerequisites + // Aggregate prerequisites. if (!empty($prerequisites)) { - - $phtml = ''; - $phtml .= get_string('dependenciescompleted', 'completion'); - $phtml .= ''; $a = new stdClass(); $a->first = $prerequisites_complete; $a->second = count($prerequisites); - $phtml .= get_string('firstofsecond', 'block_completionstatus', $a); - $phtml .= ''; - $shtml = $phtml . $shtml; + $row = new html_table_row(); + $row->cells[0] = new html_table_cell(get_string('dependenciescompleted', 'completion')); + $row->cells[1] = new html_table_cell(get_string('firstofsecond', 'block_completionstatus', $a)); + $row->cells[1]->style = 'text-align: right;'; + $prows[] = $row; + + $srows = array_merge($prows, $srows); } - // Display completion status - $this->content->text = ''; - $this->content->text .= ''; - $this->content->text .= ''; - $this->content->text .= ''; - $this->content->text .= $shtml.'
'.get_string('status').': '; + // Display completion status. + $table = new html_table(); + $table->width = '100%'; + $table->attributes = array('style'=>'font-size: 90%;', 'class'=>''); + + $row = new html_table_row(); + $content = html_writer::tag('b', get_string('status').': '); // Is course complete? $coursecomplete = $info->is_course_complete($USER->id); - // Load course completion + // Load course completion. $params = array( 'userid' => $USER->id, 'course' => $course->id @@ -190,45 +194,61 @@ class block_completionstatus extends block_base { $criteriacomplete = $info->count_course_user_data($USER->id); if ($pending_update) { - $this->content->text .= ''.get_string('pending', 'completion').''; + $content .= html_writer::tag('i', get_string('pending', 'completion')); } else if ($coursecomplete) { - $this->content->text .= get_string('complete'); + $content .= get_string('complete'); } else if (!$criteriacomplete && !$ccompletion->timestarted) { - $this->content->text .= ''.get_string('notyetstarted', 'completion').''; + $content .= html_writer::tag('i', get_string('notyetstarted', 'completion')); } else { - $this->content->text .= ''.get_string('inprogress','completion').''; + $content .= html_writer::tag('i', get_string('inprogress', 'completion')); } - $this->content->text .= '
'; + $row->cells[0] = new html_table_cell($content); + $row->cells[0]->colspan = '2'; - // Get overall aggregation method + $rows[] = $row; + $row = new html_table_row(); + $content = ""; + // Get overall aggregation method. $overall = $info->get_aggregation_method(); - if ($overall == COMPLETION_AGGREGATION_ALL) { - $this->content->text .= get_string('criteriarequiredall', 'completion'); + $content .= get_string('criteriarequiredall', 'completion'); } else { - $this->content->text .= get_string('criteriarequiredany', 'completion'); + $content .= get_string('criteriarequiredany', 'completion'); } + $content .= ':'; + $row->cells[0] = new html_table_cell($content); + $row->cells[0]->colspan = '2'; + $rows[] = $row; - $this->content->text .= ':
'.get_string('requiredcriteria', 'completion').''.get_string('status').'
'; + $row = new html_table_row(); + $row->cells[0] = new html_table_cell(html_writer::tag('b', get_string('requiredcriteria', 'completion'))); + $row->cells[1] = new html_table_cell(html_writer::tag('b', get_string('status'))); + $row->cells[1]->style = 'text-align: right;'; + $rows[] = $row; - // Display link to detailed view + // Array merge $rows and $data here. + $rows = array_merge($rows, $srows); + + $table->data = $rows; + $this->content->text = html_writer::table($table); + + // Display link to detailed view. $details = new moodle_url('/blocks/completionstatus/details.php', array('course' => $course->id)); - $this->content->footer = '
'.get_string('moredetails', 'completion').''; + $this->content->footer = html_writer::empty_tag('br'); + $this->content->footer = html_writer::tag('a', get_string('moredetails', 'completion'), array('href'=>$details->out())); } else { - // If user is not enrolled, show error + // If user is not enrolled, show error. $this->content->text = get_string('nottracked', 'completion'); } if (has_capability('report/completion:view', $context)) { $report = new moodle_url('/report/completion/index.php', array('course' => $course->id)); - $this->content->footer .= '
'.get_string('viewcoursereport', 'completion').''; + $this->content->footer .= html_writer::empty_tag('br'); + $this->content->footer .= html_writer::tag('a', get_string('viewcoursereport', 'completion'), + array('href'=>$report->out())); } - return $this->content; } } diff --git a/blocks/completionstatus/db/upgrade.php b/blocks/completionstatus/db/upgrade.php index b5bbe8c08e0..6cf9698a4e2 100644 --- a/blocks/completionstatus/db/upgrade.php +++ b/blocks/completionstatus/db/upgrade.php @@ -1,5 +1,4 @@ . /** - * Block for displayed logged in user's course completion status + * Block for displaying logged in user's course completion status * * @package block * @subpackage completion @@ -27,43 +27,38 @@ require_once(dirname(__FILE__).'/../../config.php'); require_once("{$CFG->libdir}/completionlib.php"); - -/// -/// Load data -/// +// Load data. $id = required_param('course', PARAM_INT); $userid = optional_param('user', 0, PARAM_INT); -// Load course +// Load course. $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST); -// Load user +// Load user. if ($userid) { $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); } else { $user = $USER; } - -// Check permissions +// Check permissions. require_login(); if (!completion_can_view_data($user->id, $course)) { print_error('cannotviewreport'); } - -// Load completion data +// Load completion data. $info = new completion_info($course); $returnurl = new moodle_url('/course/view.php', array('id' => $id)); -// Don't display if completion isn't enabled! +// Don't display if completion isn't enabled. if (!$info->is_enabled()) { print_error('completionnotenabled', 'completion', $returnurl); } -// Check this user is enroled +// Check this user is enroled. if (!$info->is_tracked_user($user->id)) { if ($USER->id == $user->id) { print_error('notenroled', 'completion', $returnurl); @@ -72,13 +67,11 @@ if (!$info->is_tracked_user($user->id)) { } } +// Display page. -/// -/// Display page -/// $PAGE->set_context(context_course::instance($course->id)); -// Print header +// Print header. $page = get_string('completionprogressdetails', 'block_completionstatus'); $title = format_string($course->fullname) . ': ' . $page; @@ -90,17 +83,24 @@ $PAGE->set_heading($title); echo $OUTPUT->header(); -// Display completion status -echo ''; +// Display completion status. +echo html_writer::start_tag('table', array('class' => 'generalbox boxaligncenter')); +echo html_writer::start_tag('tbody'); -// If not display logged in user, show user name +// If not display logged in user, show user name. if ($USER->id != $user->id) { - echo ''; + echo html_writer::start_tag('tr'); + echo html_writer::start_tag('td', array('colspan' => '2')); + echo html_writer::tag('b', get_string('showinguser', 'completion')); + $url = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)); + echo html_writer::link($url, fullname($user)); + echo html_writer::end_tag('td'); + echo html_writer::end_tag('tr'); } -echo ''; +echo html_writer::end_tag('td'); +echo html_writer::end_tag('tr'); -// Load criteria to display +// Load criteria to display. $completions = $info->get_completions($user->id); -// Check if this course has any criteria +// Check if this course has any criteria. if (empty($completions)) { - echo '
'.get_string('showinguser', 'completion').': '; - echo ''.fullname($user).''; - echo '
'.get_string('status').': '; +echo html_writer::start_tag('tr'); +echo html_writer::start_tag('td', array('colspan' => '2')); +echo html_writer::tag('b', get_string('status')); // Is course complete? $coursecomplete = $info->is_course_complete($user->id); @@ -108,7 +108,7 @@ $coursecomplete = $info->is_course_complete($user->id); // Has this user completed any criteria? $criteriacomplete = $info->count_course_user_data($user->id); -// Load course completion +// Load course completion. $params = array( 'userid' => $user->id, 'course' => $course->id, @@ -118,25 +118,33 @@ $ccompletion = new completion_completion($params); if ($coursecomplete) { echo get_string('complete'); } else if (!$criteriacomplete && !$ccompletion->timestarted) { - echo ''.get_string('notyetstarted', 'completion').''; + echo html_writer::tag('i', get_string('notyetstarted', 'completion')); } else { - echo ''.get_string('inprogress','completion').''; + echo html_writer::tag('i', get_string('inprogress', 'completion')); } -echo '

'; + echo html_writer::start_tag('tr'); + echo html_writer::start_tag('td', array('colspan' => '2')); + echo html_writer::start_tag('br'); echo $OUTPUT->box(get_string('err_nocriteria', 'completion'), 'noticebox'); - echo '
'; + echo html_writer::end_tag('td'); + echo html_writer::end_tag('tr'); + echo html_writer::end_tag('tbody'); + echo html_writer::end_tag('table'); } else { - echo ''.get_string('required').': '; + echo html_writer::start_tag('tr'); + echo html_writer::start_tag('td', array('colspan' => '2')); + echo html_writer::tag('b', get_string('required')); - // Get overall aggregation method + // Get overall aggregation method. $overall = $info->get_aggregation_method(); if ($overall == COMPLETION_AGGREGATION_ALL) { @@ -145,23 +153,28 @@ if (empty($completions)) { echo get_string('criteriarequiredany', 'completion'); } - echo ''; + echo html_writer::end_tag('td'); + echo html_writer::end_tag('tr'); + echo html_writer::end_tag('tbody'); + echo html_writer::end_tag('table'); - // Generate markup for criteria statuses - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; + // Generate markup for criteria statuses. + echo html_writer::start_tag('table', + array('class' => 'generalbox logtable boxaligncenter', 'id' => 'criteriastatus', 'width' => '100%')); + echo html_writer::start_tag('tbody'); + echo html_writer::start_tag('tr', array('class' => 'ccheader')); + echo html_writer::tag('th', get_string('criteriagroup', 'block_completionstatus'), array('class' => 'c0 header', 'scope' => 'col')); + echo html_writer::tag('th', get_string('criteria', 'completion'), array('class' => 'c1 header', 'scope' => 'col')); + echo html_writer::tag('th', get_string('requirement', 'block_completionstatus'), array('class' => 'c2 header', 'scope' => 'col')); + echo html_writer::tag('th', get_string('status'), array('class' => 'c3 header', 'scope' => 'col')); + echo html_writer::tag('th', get_string('complete'), array('class' => 'c4 header', 'scope' => 'col')); + echo html_writer::tag('th', get_string('completiondate', 'report_completion'), array('class' => 'c5 header', 'scope' => 'col')); + echo html_writer::end_tag('tr'); - // Save row data + // Save row data. $rows = array(); - // Loop through course criteria + // Loop through course criteria. foreach ($completions as $completion) { $criteria = $completion->get_criteria(); @@ -175,81 +188,77 @@ if (empty($completions)) { $rows[] = $row; } - // Print table + // Print table. $last_type = ''; $agg_type = false; $oddeven = 0; foreach ($rows as $row) { - echo ''; - - // Criteria group - echo ''; + echo html_writer::end_tag('td'); - // Criteria title - echo ''; + echo html_writer::end_tag('td'); - // Requirement - echo ''; + echo html_writer::end_tag('td'); - // Status - echo ''; + echo html_writer::end_tag('td'); - // Is complete - echo ''; + echo html_writer::end_tag('td'); - // Completion data - echo ''; - echo ''; - // for row striping + echo html_writer::end_tag('td'); + echo html_writer::end_tag('tr'); + // For row striping. $oddeven = $oddeven ? 0 : 1; } - echo '
'.get_string('criteriagroup', 'block_completionstatus').''.get_string('criteria', 'completion').''.get_string('requirement', 'block_completionstatus').''.get_string('status').''.get_string('complete').''.get_string('completiondate', 'report_completion').'
'; + echo html_writer::start_tag('tr', array('class' => 'r' . $oddeven)); + // Criteria group. + echo html_writer::start_tag('td', array('class' => 'cell c0')); if ($last_type !== $row['details']['type']) { $last_type = $row['details']['type']; echo $last_type; - // Reset agg type + // Reset agg type. $agg_type = true; } else { - // Display aggregation type + // Display aggregation type. if ($agg_type) { $agg = $info->get_aggregation_method($row['type']); - - echo '('; - + echo '('. html_writer::start_tag('i'); if ($agg == COMPLETION_AGGREGATION_ALL) { echo strtolower(get_string('aggregateall', 'completion')); } else { echo strtolower(get_string('aggregateany', 'completion')); } - echo ' '.strtolower(get_string('required')).')'; + echo html_writer::end_tag('i') .strtolower(get_string('required')).')'; $agg_type = false; } } - echo ''; + // Criteria title. + echo html_writer::start_tag('td', array('class' => 'cell c1')); echo $row['details']['criteria']; - echo ''; + // Requirement. + echo html_writer::start_tag('td', array('class' => 'cell c2')); echo $row['details']['requirement']; - echo ''; + // Status. + echo html_writer::start_tag('td', array('class' => 'cell c3')); echo $row['details']['status']; - echo ''; + // Is complete. + echo html_writer::start_tag('td', array('class' => 'cell c4')); echo $row['complete'] ? get_string('yes') : get_string('no'); - echo ''; + // Completion data. + echo html_writer::start_tag('td', array('class' => 'cell c5')); if ($row['timecompleted']) { echo userdate($row['timecompleted'], get_string('strftimedate', 'langconfig')); } else { echo '-'; } - echo '
'; + echo html_writer::end_tag('tbody'); + echo html_writer::end_tag('table'); } - -echo '
'; $courseurl = new moodle_url("/course/view.php", array('id' => $course->id)); +echo html_writer::start_tag('div', array('class' => 'buttons')); echo $OUTPUT->single_button($courseurl, get_string('returntocourse', 'block_completionstatus'), 'get'); -echo '
'; - +echo html_writer::end_tag('div'); echo $OUTPUT->footer(); diff --git a/blocks/completionstatus/lang/en/block_completionstatus.php b/blocks/completionstatus/lang/en/block_completionstatus.php index e389743ef1f..01c0dccdc45 100644 --- a/blocks/completionstatus/lang/en/block_completionstatus.php +++ b/blocks/completionstatus/lang/en/block_completionstatus.php @@ -1,5 +1,4 @@ version = 2012112901; // The current plugin version (Date: YYYYMMDDXX) -$plugin->requires = 2012112900; // Requires this Moodle version +$plugin->version = 2012112901; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2012112900; // Requires this Moodle version. $plugin->component = 'block_completionstatus'; $plugin->dependencies = array('report_completion' => 2012112900); diff --git a/theme/base/style/blocks.css b/theme/base/style/blocks.css index 7d778f3675b..13fce493bde 100644 --- a/theme/base/style/blocks.css +++ b/theme/base/style/blocks.css @@ -32,6 +32,9 @@ .block.hidden .block-hider-hide {display:none;} .block.hidden .block-hider-show {display:inline;} +.block_completionstatus .generaltable { border: 0px; } +.block_completionstatus .generaltable .cell { border: 0px; } + /** Overide for RTL layout **/ .dir-rtl .block .header, .dir-rtl .block h2.header {text-align:right;} diff --git a/theme/upgrade.txt b/theme/upgrade.txt index 0ac429de407..88312d12f12 100644 --- a/theme/upgrade.txt +++ b/theme/upgrade.txt @@ -1,6 +1,11 @@ This files describes API changes in /theme/* themes, information provided here is intended especially for theme designer. +=== 2.6 === +* re-wrote the table for the course completion status block to use html_table - added some CSS classes to the table in the process. + MDL-35608 - Blocks - Updating the Completion Status block to use HTML Writer + + === 2.5 === required changes: