Skip to content
Snippets Groups Projects
Commit 300fd4d3 authored by John Hoopes's avatar John Hoopes
Browse files

[PHARMCE-501] updating CE branch of this block to work with categories

parent 32c6b7fa
Branches master
No related tags found
No related merge requests found
......@@ -28,7 +28,7 @@
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/blocks/course_overview_uwmoodle/locallib.php');
//require_once($CFG->dirroot.'/blocks/course_overview_uwmoodle/locallib.php');
require_once($CFG->dirroot.'/user/profile/lib.php');
class block_course_overview_uwmoodle extends block_base {
......@@ -60,110 +60,133 @@ class block_course_overview_uwmoodle extends block_base {
profile_load_custom_fields($USER);
// Get user's courses and sort by term
$courses = block_course_overview_uwmoodle_get_sorted_courses();
$terms = block_course_overview_uwmoodle_group_courses_by_term($courses);
$courses = enrol_get_users_courses($USER->id, true);
$currentterm = block_course_overview_uwmoodle_get_current_term();
$selectedterm = optional_param('term', $currentterm, PARAM_INT);
if (!isset($terms[$currentterm])) {
$terms[$currentterm] = array();
}
// If selectedterm is not valid, select currentterm
if (!isset($terms[$selectedterm])) {
$selectedterm = $currentterm;
foreach ($courses as $c) {
if (isset($USER->lastcourseaccess[$c->id])) {
$courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
} else {
$courses[$c->id]->lastaccess = 0;
}
}
// Get course overviews
$overviews = block_course_overview_uwmoodle_get_overviews($courses);
$renderer = $this->page->get_renderer('block_course_overview_uwmoodle');
// Show welcome area if configuration is set to show
if (!empty($config->showwelcomearea)) {
require_once($CFG->dirroot.'/message/lib.php');
$msgcount = message_count_unread_messages();
$msgcount = $this->message_count_unread_messages();
$this->content->text = $renderer->welcome_area($msgcount);
}
// Sort the terms with newest first
block_course_overview_uwmoodle_sort_term_array($terms);
if(!empty($courses)){
$displayedterms = $terms;
$othercourses = false;
if (empty($this->config->combineongoing)) {
unset($displayedterms[self::TERM_OTHER]);
if (!empty($terms[self::TERM_OTHER])) {
$othercourses = $terms[self::TERM_OTHER];
}
}
list($tree, $categoryids, $selectedcourse, $selectedcategory) = $this->block_categories_setup($courses,
$this->instance->parentcontextid);
//Get the categories from the courses
list($insql, $params) = $DB->get_in_or_equal($categoryids);
$sql = 'SELECT * FROM {course_categories} WHERE id ' . $insql;
$categories = $DB->get_records_sql($sql, $params);
// Get course overviews
$overviews = $this->block_course_overview_uwmoodle_get_overviews($courses);
// Render the block
$this->content->text .= $renderer->course_block($categories, $overviews, $tree, $selectedcategory);
// Render the block
$this->content->text .= $renderer->course_block($displayedterms, $overviews, $selectedterm);
if (!empty($othercourses)) {
$this->content->text .= $renderer->other_course_block($othercourses, $overviews);
}
$this->content->footer = $renderer->footer();
return $this->content;
}
/**
* Get the current term from the datastore, and update user show/hide preferences when it changes.
* Builds course category tree from the courses array, which creates
* a categoryid keyed array.
*
* @return boolean true on success
* This also returns an array of categoryids
*
* @param array $courses
* @param int $blockinstancecontext The parent block instance context
*
* @return array of vars to send back
*/
public function update_current_term() {
global $CFG, $DB;
$oldcurrentterm = get_config('block_course_overview_uwmoodle', 'currentterm');
try {
$currentterm = $this->get_current_term();
} catch (Exception $e) {
mtrace("Error fetching current term: ".$e->getMessage());
return false;
}
if ($currentterm === false) {
mtrace("No current term found");
return false;
}
protected function block_categories_setup($courses, $blockinstancecontext){
$tree = array();
$categoryids = array();
$selectedcourse = '';
$selectedcategory = '';
foreach($courses as $course){
//get categoryids to get the category records from the category table later
if(!in_array($course->category, $categoryids)){
$categoryids[] = $course->category;
}
//build the tree out keyed by category and then keyed by course id
if(isset($tree[$course->category])){
$tree[$course->category][$course->id] = $course;
}else{
$tree[$course->category] = array();
$tree[$course->category][$course->id] = $course;
}
if(context_course::instance($course->id)->id == $blockinstancecontext){
$selectedcourse = $course->id;
$selectedcategory = $course->category;
}
if ($currentterm && $currentterm != $oldcurrentterm) {
// store new term
mtrace('new current term: '.$currentterm);
set_config('currentterm', $currentterm, 'block_course_overview_uwmoodle');
}
return true;
return array($tree, $categoryids, $selectedcourse, $selectedcategory);
}
/**
* Fetch current term from CHUB. Throw exception on CHUB error.
* Display overview for courses
*
* @return string|false term_code or false if none found.
* @param array $courses courses for which overview needs to be shown
* @return array html overview
*/
public function get_current_term() {
global $CFG;
require_once($CFG->dirroot.'/enrol/wisc/lib/datastore.php');
$datastore = wisc_timetable_datastore::get_timetable_datastore();
$terms = $datastore->getAvailableTerms();
$now = time();
$futureterms = array();
foreach ($terms as $term) {
// check that term hasn't ended and doesn't have an odd termCode
// odd term codes (e.g. Winter) are never considered the current term
if ($term->endDate > $now && ($term->termCode % 2 != 1)) {
$futureterms[] = $term->termCode;
protected function block_course_overview_uwmoodle_get_overviews($courses) {
$htmlarray = array();
if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
foreach ($modules as $fname) {
try {
$fname($courses,$htmlarray);
} catch (Exception $e) {
// Log the error message, and otherwise ignore the error
error_log("course_overview_uwmoodle: Error in getting overviews. ". $e->getMessage());
}
}
}
if (!empty($futureterms)) {
$currentterm = min($futureterms);
return $htmlarray;
}
/**
* Returns the count of unread messages for user. Either from a specific user or from all users.
*
* @param object $user1 the first user. Defaults to $USER
* @param object $user2 the second user. If null this function will count all of user 1's unread messages.
* @return int the count of $user1's unread messages
*/
function message_count_unread_messages($user1=null, $user2=null) {
global $USER, $DB;
if (empty($user1)) {
$user1 = $USER;
}
if (!empty($user2)) {
return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?",
array($user1->id, $user2->id), "COUNT('id')");
} else {
$currentterm = false;
return $DB->count_records_select('message', "useridto = ?",
array($user1->id), "COUNT('id')");
}
return $currentterm;
}
/**
* allow the block to have a configuration page
*
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Helper functions for course_overview block
*
* @package block_course_overview_uwmoodle
* @copyright 2012 Adam Olley <adam.olley@netspot.com.au>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Adapted for course_overview_uwmoodle
*
* @author 2013 Matt Petro
*/
defined('MOODLE_INTERNAL') || die;
/**
* Display overview for courses
*
* @param array $courses courses for which overview needs to be shown
* @return array html overview
*/
function block_course_overview_uwmoodle_get_overviews($courses) {
$htmlarray = array();
if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
foreach ($modules as $fname) {
try {
$fname($courses,$htmlarray);
} catch (Exception $e) {
// Log the error message, and otherwise ignore the error
error_log("course_overview_uwmoodle: Error in getting overviews. ". $e->getMessage());
}
}
}
return $htmlarray;
}
/**
* Returns shortname of activities in course
*
* @param int $courseid id of course for which activity shortname is needed
* @return string|bool list of child shortname
*/
function block_course_overview_uwmoodle_get_child_shortnames($courseid) {
global $DB;
$ctxselect = context_helper::get_preload_record_columns_sql('ctx');
$sql = "SELECT c.id, c.shortname, $ctxselect
FROM {enrol} e
JOIN {course} c ON (c.id = e.customint1)
JOIN {context} ctx ON (ctx.instanceid = e.customint1)
WHERE e.courseid = :courseid AND e.enrol = :method AND ctx.contextlevel = :contextlevel ORDER BY e.sortorder";
$params = array('method' => 'meta', 'courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE);
if ($results = $DB->get_records_sql($sql, $params)) {
$shortnames = array();
// Preload the context we will need it to format the category name shortly.
foreach ($results as $res) {
context_helper::preload_from_record($res);
$context = context_course::instance($res->id);
$shortnames[] = format_string($res->shortname, true, $context);
}
$total = count($shortnames);
$suffix = '';
if ($total > 10) {
$shortnames = array_slice($shortnames, 0, 10);
$diff = $total - count($shortnames);
if ($diff > 1) {
$suffix = get_string('shortnamesufixprural', 'block_course_overview_uwmoodle', $diff);
} else {
$suffix = get_string('shortnamesufixsingular', 'block_course_overview_uwmoodle', $diff);
}
}
$shortnames = get_string('shortnameprefix', 'block_course_overview_uwmoodle', implode('; ', $shortnames));
$shortnames .= $suffix;
}
return isset($shortnames) ? $shortnames : false;
}
/**
* Return sorted list of user courses
*
* @return array list of sorted courses grouped by term.
*/
function block_course_overview_uwmoodle_get_sorted_courses() {
global $USER;
$courses = enrol_get_my_courses('id, shortname, fullname, modinfo, sectioncache', 'fullname', 0);
$site = get_site();
if (array_key_exists($site->id,$courses)) {
unset($courses[$site->id]);
}
foreach ($courses as $c) {
if (isset($USER->lastcourseaccess[$c->id])) {
$courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
} else {
$courses[$c->id]->lastaccess = 0;
}
}
return $courses;
}
/**
* Organize courses into terms, maintaining existing sorting inside each term
*
* @param array $courses user courses
* @return array
*/
function block_course_overview_uwmoodle_group_courses_by_term($courses) {
global $DB;
// Find the termcodes for each course
$courseterms = array();
if (!empty($courses)) {
$courseids = array();
foreach ($courses as $course) {
$courseids[] = $course->id;
}
list ($insql, $inparams) = $DB->get_in_or_equal($courseids);
$select = "enrol='wisc' AND courseid $insql";
$enrols = $DB->get_records_select('enrol', $select, $inparams, '', 'id,courseid,customchar1');
foreach ($enrols as $enrol) {
$courseterms[$enrol->courseid][$enrol->customchar1] = true;
}
}
// Sort the courses by termcode. Note that one course can be in multiple terms.
$terms = array();
foreach ($courses as $course) {
if (!empty($courseterms[$course->id])) {
foreach ($courseterms[$course->id] as $termcode => $unused) {
$terms[$termcode][$course->id] = $course;
}
} else {
$terms[block_course_overview_uwmoodle::TERM_OTHER][$course->id] = $course;
}
}
return $terms;
}
/**
* Sort term array by ascending date
*
* @param array $terms keys are term_codes
* @return boolean
*/
function block_course_overview_uwmoodle_sort_term_array(&$terms) {
// Sort the terms with ongoing (code = 0) first, then in decreasing order
$cmp = function ($a, $b) {
if ($a == 0 || $b == 0) {
return $a-$b;
} else {
return $b-$a;
}
};
return uksort($terms, $cmp);
}
/**
* Get the current term from our settings
*
* @return string termcode
*/
function block_course_overview_uwmoodle_get_current_term() {
$currentterm = get_config('block_course_overview_uwmoodle', 'currentterm');
if (!$currentterm) {
$currentterm = 1136; // Just set it to something in the past, until cron runs and updates the term.
}
return $currentterm;
}
/**
* Return a string representing the term (e.g. "Fall 2010")
* This function doesn't make any remote calls.
*
* @param string $termCode
* @return string $termName
*/
function block_course_overview_uwmoodle_get_term_name($termCode) {
$termCode = (string)$termCode;
$c = substr($termCode,0,1);
$yy = substr($termCode,1,2);
$year = 1900+100*$c+$yy;
$semester = substr($termCode,3,1);
switch($semester) {
case 2:
$name = sprintf("<span class='semester'>Fall</span>&nbsp;<span class='year'>%d</span>", $year-1);
break;
case 3:
$name = sprintf("<span class='semester'>Winter</span>&nbsp;<span class='year'>%d</span>", $year);
break;
case 4:
$name = sprintf("<span class='semester'>Spring</span>&nbsp;<span class='year'>%d</span>", $year);
break;
case 6:
$name = sprintf("<span class='semester'>Summer</span>&nbsp;<span class='year'>%d</span>", $year);
break;
default:
$name = "Ongoing";
}
return $name;
}
\ No newline at end of file
......@@ -38,43 +38,40 @@ defined('MOODLE_INTERNAL') || die;
*/
class block_course_overview_uwmoodle_renderer extends plugin_renderer_base {
/** @var array $categories category information to get category names */
protected $categories;
/**
* Generate course_block containing term courses
*
* @param array $terms array of the form $termcode => array of courses
* @param array $categories category information to get category names
* @param array $overviews list of course overviews
* @param string $selectedterm active termcode
* @param array $tree array of courses array(categoryid => array(courseid => course))
* @param string $selectedcategory active categoryid
* @return string
*/
public function course_block($terms, $overviews, $selectedterm) {
public function course_block($categories, $overviews, $tree, $selectedcategory) {
$this->categories = $categories;
$html = '';
$html .= $this->begin_course_block(get_string('courses', 'block_course_overview_uwmoodle'), 'uwmm_mycourses_block');
$termcodes = array_keys($terms);
$categoryids = array_keys($tree);
// If there was no default selected category, default to the first
if(empty($selectedcategory)){
$selectedcategory = $categoryids[0];
}
$html .= html_writer::start_tag('div', array('class' => 'lhs'));
$html .= $this->term_list($termcodes, $selectedterm);
$html .= $this->term_list($categoryids, $selectedcategory);
$html .= html_writer::end_tag('div');
$html .= html_writer::start_tag('div', array('class' => 'rhs'));
$html .= $this->course_overview_allterms($terms, $overviews, $selectedterm);
$html .= $this->course_overview_allterms($tree, $overviews, $selectedcategory);
$html .= html_writer::end_tag('div');
$html .= $this->end_course_block();
return $html;
}
/**
* Generate course_block containing non-term courses
*
* @param array $terms array of the form $termcode => array of courses
* @param array $overviews list of course overviews
* @return string
*/
public function other_course_block($courses, $overviews) {
$html = '';
$html .= $this->begin_course_block(get_string('ongoingcourses', 'block_course_overview_uwmoodle'), 'uwmm_othercourses_block', 'ongoingcourses');
$html .= $this->course_overview($courses, $overviews, block_course_overview_uwmoodle::TERM_OTHER);
$html .= $this->end_course_block();
return $html;
}
/**
* Generate list of all courses in all terms
*
......@@ -83,25 +80,25 @@ class block_course_overview_uwmoodle_renderer extends plugin_renderer_base {
* @param string $selectedterm active termcode
* @return string
*/
protected function course_overview_allterms($terms, $overviews, $selectedterm) {
protected function course_overview_allterms($tree, $overviews, $selectedcategory) {
$html = '';
$html .= html_writer::start_tag('div', array('class' => 'courselistcontainer', 'id' => 'uwmm_terms_content'));
foreach ($terms as $termcode => $courses) {
if ($termcode != $selectedterm) {
foreach ($tree as $categoryid => $courses) {
if ($categoryid != $selectedcategory) {
$hidden = 'hidden';
} else {
$hidden = '';
}
$html .= html_writer::start_tag('div', array('class' => 'courselist '.$hidden, 'id' => 'uwmm_term_'.$termcode.'_courses'));
$html .= html_writer::start_tag('div', array('class' => 'courselist '.$hidden, 'id' => 'uwmm_term_'.$categoryid.'_courses'));
$termstr = block_course_overview_uwmoodle_get_term_name($termcode);
$termstr = $this->categories[$categoryid]->name;
$title = get_string('mycoursesinterm', 'block_course_overview_uwmoodle', $termstr);
$html .= html_writer::start_tag('div', array('class' => 'title'));
$html .= $this->output->heading($title, 2, 'termname');
$html .= html_writer::end_tag('div');
$html .= $this->course_overview($courses, $overviews, $termcode);
$html .= $this->course_overview($courses, $overviews, $categoryid);
$html .= html_writer::end_tag('div');
}
$html .= html_writer::end_tag('div');
......@@ -118,7 +115,7 @@ class block_course_overview_uwmoodle_renderer extends plugin_renderer_base {
* @param string $termcode termcode for courses
* @return string
*/
protected function course_overview($courses, $overviews, $termcode) {
protected function course_overview($courses, $overviews, $categoryid) {
$html = '';
$html .= html_writer::start_tag('div', array('class' => 'courselistcontainer'));
if (empty($courses)) {
......@@ -143,7 +140,7 @@ class block_course_overview_uwmoodle_renderer extends plugin_renderer_base {
$html .= html_writer::end_tag('div');
if (isset($overviews[$course->id])) {
$html .= $this->activity_display($course->id, $overviews[$course->id], $termcode);
$html .= $this->activity_display($course->id, $overviews[$course->id], $categoryid);
}
//$html .= $this->output->box('', 'flush');
......@@ -157,23 +154,23 @@ class block_course_overview_uwmoodle_renderer extends plugin_renderer_base {
/**
* Generate terms menu
*
* @param array $termcodes array of termcodes
* @param string $selectedterm active termcode
* @param array $categoryids array of category ids
* @param string $selectedcategory active category id
* @return string
*/
protected function term_list($termcodes, $selectedterm) {
protected function term_list($categoryids, $selectedcategory) {
$thisurl = $this->page->url;
$html = '';
$html .= html_writer::start_tag('div', array('class' => 'termscontainer'));
$html .= html_writer::start_tag('ul', array('class' => 'terms', 'id' => 'uwmm_terms'));
foreach ($termcodes as $termcode) {
$termname = block_course_overview_uwmoodle_get_term_name($termcode);
$attributes = array('id'=>'uwmm_term_'.$termcode);
if ($termcode == $selectedterm) {
foreach ($categoryids as $categoryid) {
$termname = $this->categories[$categoryid]->name;
$attributes = array('id'=>'uwmm_term_'.$categoryid);
if ($categoryid == $selectedcategory) {
$attributes['class'] = 'active';
}
$html .= html_writer::start_tag('li', $attributes);
$html .= html_writer::tag('a', $termname, array('href' => $thisurl->out(false, array('term'=>$termcode))));
$html .= html_writer::tag('a', $termname, array('href' => $thisurl->out(false, array('term'=>$categoryid))));
$html .= html_writer::end_tag('li');
}
$html .= html_writer::end_tag('ul');
......
......@@ -93,16 +93,18 @@
.block_course_overview_uwmoodle .terms li a {
font: bold 12px Verdana, Arial, Helvetica, sans-serif;
display: block;
background: #dddddd url([[pix:block_course_overview_uwmoodle|menu]]) 100% 0;
height: 22px; /*Set to height of bg image- padding within link (ie: 32px - 4px - 4px)*/
/*background: #dddddd url([[pix:block_course_overview_uwmoodle|menu]]) 100% 0;*/
/*height: 22px; */ /*Set to height of bg image- padding within link (ie: 32px - 4px - 4px)*/
padding: 4px 0 4px 10px;
line-height: 24px; /*Set line-height of bg image- padding within link (ie: 32px - 4px - 4px)*/
text-decoration: none;
color: #000000;
padding-right: 2em;
border-bottom: 3px solid;
background: #dddddd;
}
.block_course_overview_uwmoodle .terms li.active a {
.block_course_overview_uwmoodle .terms li.active a{
color: #ffffff;
background-color: #A00000;
background-position: 100% -60px;
......
......@@ -18,13 +18,9 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'block_course_overview_uwmoodle';
$plugin->version = 2013071801;
$plugin->version = 2013112100;
$plugin->release = '1.1';
$plugin->requires = 2012062500;
$plugin->maturity = MATURITY_RC;
$plugin->cron = 43200; // Set min time between cron executions to 12 hours
$plugin->dependencies = array(
'enrol_wisc' => 2012112900,
);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment