Newer
Older
<?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/>.
/**
* Course overview block for UW Moodle
*
* This block improves on the default course_summary block by filtering courses by term.
Matt Petro
committed
* The block is dependent on the UW enrollment plugins and is largely coped from the existing course_overview block.
* I've left copyright statements for the original authors on most files.
* @author 2013 Matt Petro
John Hoopes
committed
* @author 2014 John Hoopes
defined('MOODLE_INTERNAL') || die;
Matt Petro
committed
require_once($CFG->dirroot.'/blocks/course_overview_uwmoodle/locallib.php');
require_once($CFG->dirroot.'/user/profile/lib.php');
John Hoopes
committed
require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
class block_course_overview_uwmoodle extends block_base {
/**
* block initializations
*/
public function init() {
Matt Petro
committed
global $CFG, $PAGE;
$this->title = get_string('pluginname', 'block_course_overview_uwmoodle');
if($CFG->version < '2013051400'){
$PAGE->requires->css('/blocks/course_overview_uwmoodle/js/jquery-ui.min.css');
}
/**
* Require jQuery and jQuery ui plugins for rendering the block
*/
public function get_required_javascript() {
parent::get_required_javascript();
if($CFG->version >= '2013051400') {
$this->page->requires->jquery();
$this->page->requires->jquery_plugin('ui');
$this->page->requires->jquery_plugin('ui-css');
$this->page->requires->jquery_plugin('block_course_overview_uwmoodle-animate-enhanced', 'block_course_overview_uwmoodle');
}else{
$this->page->requires->js('/blocks/course_overview_uwmoodle/js/jquery-ui.min.js');
}
$this->page->requires->js('/blocks/course_overview_uwmoodle/js/course_overview_uwmoodle.js');
}
Matt Petro
committed
const TERM_OTHER = 0; // our "term_code" for non-timetable classes
/**
* block contents
*
* @return object
*/
public function get_content() {
Matt Petro
committed
global $USER, $CFG, $DB;
if($this->content !== NULL) {
return $this->content;
}
Matt Petro
committed
$config = get_config('block_course_overview_uwmoodle');
Matt Petro
committed
$this->content = new stdClass();
$this->content->text = '';
Matt Petro
committed
profile_load_custom_fields($USER);
John Hoopes
committed
// start rendering the welcome section and then determine if this is AJAX load or php load
Matt Petro
committed
$renderer = $this->page->get_renderer('block_course_overview_uwmoodle');
if (!empty($config->showwelcomearea)) {
require_once($CFG->dirroot.'/message/lib.php');
$msgcount = message_count_unread_messages();
$this->content->text = $renderer->welcome_area($msgcount);
John Hoopes
committed
// next render the news section if there is any content
if(!empty($config->news)){
$this->content->text .= $renderer->render_news($config->news);
}
John Hoopes
committed
$usephp = optional_param('usephp', '', PARAM_ALPHA);
if(empty($usephp)){ // set up course blocks, but then let ajax load courses
// the next 2 renders happen for both, but to get the order to display correctly they have to be put in twice
// render the error container so that js/php can add errors
$this->content->text .= $renderer->render_error_container($usephp);
// render the legend
$this->content->text .= $renderer->render_create_course_and_legend();
John Hoopes
committed
$this->content->text .= $renderer->use_ajax(true, $this->config); // adds in a javascript variable for using ajax
$this->content->text .= $renderer->ajax_course_block();
$this->content->text .= $renderer->ajax_other_course_block();
}else{ // load courses without using ajax to load them on the client side
$this->content->text .= $renderer->use_ajax(false); // adds in javascript variable to not use ajax
// Get user's courses and sort by term
list($courses, $errors) = block_course_overview_uwmoodle_get_sorted_courses();
John Hoopes
committed
$terms = block_course_overview_uwmoodle_group_courses_by_term($courses);
$currentterm = block_course_overview_uwmoodle_get_current_term();
$selectedterm = optional_param('term', $currentterm, PARAM_INT);
John Hoopes
committed
if (!isset($terms[$currentterm])) {
$terms[$currentterm] = array();
John Hoopes
committed
// If selectedterm is not valid, select currentterm
if (!isset($terms[$selectedterm])) {
$selectedterm = $currentterm;
}
// Get course overviews
//$overviews = block_course_overview_uwmoodle_get_overviews($courses);
$overviews = array(); // for now course overviews are disabled
// Sort the terms with newest first
block_course_overview_uwmoodle_sort_term_array($terms);
John Hoopes
committed
$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];
}
}
/// the next 2 renders happen for both, but to get the order to display correctly they have to be put in twice
// render the error container so that js/php can add errors
$this->content->text .= $renderer->render_error_container($usephp, $errors);
// render the legend
$this->content->text .= $renderer->render_create_course_and_legend();
John Hoopes
committed
// Render the block
$this->content->text .= $renderer->course_block($displayedterms, $overviews, $selectedterm);
John Hoopes
committed
if (!empty($othercourses)) {
$this->content->text .= $renderer->other_course_block($othercourses, $overviews);
}
John Hoopes
committed
$this->content->footer = $renderer->footer();
return $this->content;
}
/**
* Get the current term from the datastore, and update user show/hide preferences when it changes.
*
* @return boolean true on success
*/
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) {
Matt Petro
committed
// Suppress errors to the log. This can get noisy quickly when CHUB is down.
//mtrace("Error fetching current term: ".$e->getMessage());
return false;
}
if ($currentterm === false) {
mtrace("No current term found");
return false;
}
if ($currentterm && $currentterm != $oldcurrentterm) {
// store new term
mtrace('new current term: '.$currentterm);
set_config('currentterm', $currentterm, 'block_course_overview_uwmoodle');
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
* Fetch current term from CHUB. Throw exception on CHUB error.
*
* @return string|false term_code or false if none found.
*/
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;
}
}
if (!empty($futureterms)) {
$currentterm = min($futureterms);
} else {
$currentterm = false;
}
return $currentterm;
}
/**
* allow the block to have a configuration page
*
* @return boolean
*/
public function has_config() {
Matt Petro
committed
return true;
* locations where block can be displayed
*
* @return array
*/
public function applicable_formats() {
return array('my-index'=>true);
}
/**
* Block cron to update currentterm
*
* @return boolean true on success
*/
public function cron() {
return $this->update_current_term();
}
Matt Petro
committed
/**
* Sets block header to be hidden or visible
*
* @return bool if true then header will be visible.
*/
public function hide_header() {
// Hide header if welcome area is show.
$config = get_config('block_course_overview_uwmoodle');
return !empty($config->showwelcomearea);
}