Skip to content
Permalink
Browse files

MDL-40750 course: Add JS category expander (backport of MDL-38661)

This includes the following commits:

 * dd834cb MDL-38661 JavaScript: Add missing lazy loading on collapseall
 * 4dab1c3 MDL-38661 JavaScript: Ensure that ARIA tags are set when expanding/collapsing content
 * 1574e65 MDL-38661: Add keyboard support for expansion
 * 3df632f MDL-38661 Add pointer cursor to clickable collapse/expand icons
 * 53c1b93 MDL-38661 Course: Add JS category expander.
  • Loading branch information...
FMCorz committed Mar 12, 2013
1 parent 8e92813 commit 46ea75aca35512ae8474f9cf1d3dcc171aa76a5d
@@ -0,0 +1,39 @@
<?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/>.
/**
* Helps moodle-course-categoryexpander to serve AJAX requests
*
* @see core_course_renderer::coursecat_include_js()
* @see core_course_renderer::coursecat_ajax()
*
* @package core
* @copyright 2013 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require_once(dirname(__dir__) . '/config.php');
if ($CFG->forcelogin) {
require_login();
}
$PAGE->set_context(context_system::instance());
$courserenderer = $PAGE->get_renderer('core', 'course');
echo json_encode($courserenderer->coursecat_ajax());
@@ -40,6 +40,9 @@ class core_course_renderer extends plugin_renderer_base {
const COURSECAT_SHOW_COURSES_EXPANDED = 20;
const COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT = 30;
const COURSECAT_TYPE_CATEGORY = 0;
const COURSECAT_TYPE_COURSE = 1;
/**
* A cache of strings
* @var stdClass
@@ -1080,7 +1083,13 @@ protected function coursecat_coursebox(coursecat_helper $chelper, $course, $addi
$classes .= ' collapsed';
$nametag = 'div';
}
$content .= html_writer::start_tag('div', array('class' => $classes)); // .coursebox
// .coursebox
$content .= html_writer::start_tag('div', array(
'class' => $classes,
'data-courseid' => $course->id,
'data-type' => self::COURSECAT_TYPE_COURSE,
));
$content .= html_writer::start_tag('div', array('class' => 'info'));
@@ -1098,6 +1107,8 @@ protected function coursecat_coursebox(coursecat_helper $chelper, $course, $addi
$image = html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/info'),
'alt' => $this->strings->summary));
$content .= html_writer::link($url, $image, array('title' => $this->strings->summary));
// Make sure JS file to expand course content is included.
$this->coursecat_include_js();
}
}
$content .= html_writer::end_tag('div'); // .moreinfo
@@ -1365,6 +1376,20 @@ protected function coursecat_subcategories(coursecat_helper $chelper, $coursecat
return $content;
}
/**
* Make sure that javascript file for AJAX expanding of courses and categories content is included
*/
protected function coursecat_include_js() {
global $CFG;
static $jsloaded = false;
if (!$jsloaded && $CFG->enableajax) {
// We must only load this module once.
$this->page->requires->yui_module('moodle-course-categoryexpander',
'Y.Moodle.course.categoryexpander.init');
$jsloaded = true;
}
}
/**
* Returns HTML to display the subcategories and courses in the given category
*
@@ -1437,6 +1462,8 @@ protected function coursecat_category(coursecat_helper $chelper, $coursecat, $de
$classes[] = 'with_children';
$classes[] = 'collapsed';
}
// Make sure JS file to expand category content is included.
$this->coursecat_include_js();
} else {
// load category content
$categorycontent = $this->coursecat_category_content($chelper, $coursecat, $depth);
@@ -1445,9 +1472,13 @@ protected function coursecat_category(coursecat_helper $chelper, $coursecat, $de
$classes[] = 'with_children';
}
}
$content = html_writer::start_tag('div', array('class' => join(' ', $classes),
$content = html_writer::start_tag('div', array(
'class' => join(' ', $classes),
'data-categoryid' => $coursecat->id,
'data-depth' => $depth));
'data-depth' => $depth,
'data-showcourses' => $chelper->get_show_courses(),
'data-type' => self::COURSECAT_TYPE_CATEGORY,
));
// category name
$categoryname = $coursecat->get_formatted_name();
@@ -1485,29 +1516,29 @@ protected function coursecat_tree(coursecat_helper $chelper, $coursecat) {
return '';
}
// Generate an id and the required JS call to make this a nice widget
$id = html_writer::random_id('course_category_tree');
$this->page->requires->js_init_call('M.util.init_toggle_class_on_click',
array($id, '.category.with_children.loaded > .info .name', 'collapsed', '.category.with_children.loaded'));
// Start content generation
$content = '';
$attributes = $chelper->get_and_erase_attributes('course_category_tree clearfix');
$content .= html_writer::start_tag('div',
array('id' => $id, 'data-showcourses' => $chelper->get_show_courses()) + $attributes);
$content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
if ($coursecat->get_children_count() && $chelper->get_subcat_depth() != 1) {
// We don't need to display "Expand all"/"Collapse all" buttons if there are no
// subcategories or there is only one level of subcategories loaded
// TODO if subcategories are loaded by AJAX this might still be needed!
$content .= html_writer::start_tag('div', array('class' => 'controls'));
$content .= html_writer::tag('div', get_string('collapseall'), array('class' => 'addtoall expandall'));
$content .= html_writer::tag('div', get_string('expandall'), array('class' => 'removefromall collapseall'));
$content .= html_writer::start_tag('div', $attributes);
if ($coursecat->get_children_count()) {
$classes = array(
'collapseexpand',
'collapse-all',
);
if ($chelper->get_subcat_depth() == 1) {
$classes[] = 'disabled';
}
// Only show the collapse/expand if there are children to expand.
$content .= html_writer::start_tag('div', array('class' => 'collapsible-actions'));
$content .= html_writer::link('#', get_string('collapseall'),
array('class' => implode(' ', $classes)));
$content .= html_writer::end_tag('div');
$this->page->requires->strings_for_js(array('collapseall', 'expandall'), 'moodle');
}
$content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
$content .= html_writer::end_tag('div'); // .course_category_tree
return $content;
@@ -1632,6 +1663,58 @@ public function course_category($category) {
return $output;
}
/**
* Serves requests to /course/category.ajax.php
*
* In this renderer implementation it may expand the category content or
* course content.
*
* @return string
* @throws coding_exception
*/
public function coursecat_ajax() {
global $DB, $CFG;
require_once($CFG->libdir. '/coursecatlib.php');
$type = required_param('type', PARAM_INT);
if ($type === self::COURSECAT_TYPE_CATEGORY) {
// This is a request for a category list of some kind.
$categoryid = required_param('categoryid', PARAM_INT);
$showcourses = required_param('showcourses', PARAM_INT);
$depth = required_param('depth', PARAM_INT);
$category = coursecat::get($categoryid);
$chelper = new coursecat_helper();
$baseurl = new moodle_url('/course/index.php', array('categoryid' => $categoryid));
$coursedisplayoptions = array(
'limit' => $CFG->coursesperpage,
'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1))
);
$catdisplayoptions = array(
'limit' => $CFG->coursesperpage,
'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1))
);
$chelper->set_show_courses($showcourses)->
set_courses_display_options($coursedisplayoptions)->
set_categories_display_options($catdisplayoptions);
return $this->coursecat_category_content($chelper, $category, $depth);
} else if ($type === self::COURSECAT_TYPE_COURSE) {
// This is a request for the course information.
$courseid = required_param('courseid', PARAM_INT);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$chelper = new coursecat_helper();
$chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
return $this->coursecat_coursebox_content($chelper, $course);
} else {
throw new coding_exception('Invalid request type');
}
}
/**
* Renders html to display search result page
*

0 comments on commit 46ea75a

Please sign in to comment.
You can’t perform that action at this time.