Skip to content
Snippets Groups Projects
block_course_overview_uwmoodle.php 7.26 KiB
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.
 * 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.
defined('MOODLE_INTERNAL') || die;

//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 {
    /**
     * block initializations
     */
    public function init() {
        $this->title   = get_string('pluginname', 'block_course_overview_uwmoodle');
    }

    const TERM_OTHER = 0;  // our "term_code" for non-timetable classes

    /**
     * block contents
     *
     * @return object
     */
    public function get_content() {
        if($this->content !== NULL) {
            return $this->content;
        }

        $config = get_config('block_course_overview_uwmoodle');
        $this->content = new stdClass();
        $this->content->text = '';
        $courses = enrol_get_users_courses($USER->id, true);
        foreach ($courses as $c) {
            if (isset($USER->lastcourseaccess[$c->id])) {
                $courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
            } else {
                $courses[$c->id]->lastaccess = 0;
            }
        $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 = $this->message_count_unread_messages();
            $this->content->text = $renderer->welcome_area($msgcount);
            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);
        return $this->content;
    }

     * Builds course category tree from the courses array, which creates
     * a categoryid keyed array.
     * 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
    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;
            }

        return array($tree, $categoryids, $selectedcourse, $selectedcategory);
     * @param array $courses courses for which overview needs to be shown
     * @return array html overview
    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());
                }
        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')");
            return $DB->count_records_select('message', "useridto = ?",
                array($user1->id), "COUNT('id')");
    /**
     * allow the block to have a configuration page
     *
     * @return boolean
     */
    public function has_config() {
     * 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();
    }

    /**
     * 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);
    }