Skip to content

Commit

Permalink
MDL-37009 Display courses and categories tree on /course/index.php us…
Browse files Browse the repository at this point in the history
…ing new renderer

Also /course/category.php
  • Loading branch information
marinaglancy committed Apr 1, 2013
1 parent 9176cdb commit 8e57a6d
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 200 deletions.
159 changes: 3 additions & 156 deletions course/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,164 +67,11 @@
$site = get_site();
$PAGE->set_title("$site->shortname: $category->name");
$PAGE->set_heading($site->fullname);
$PAGE->set_button($courserenderer->course_search_form('', 'navbar'));
echo $OUTPUT->header();

/// Print the category selector
$displaylist = coursecat::make_categories_list();

echo '<div class="categorypicker">';
$select = new single_select(new moodle_url('/course/category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
$select->set_label(get_string('categories').':');
echo $OUTPUT->render($select);
echo '</div>';

/// Print current category description
echo $OUTPUT->box_start();
$options = new stdClass;
$options->noclean = true;
$options->para = false;
$options->overflowdiv = true;
if (!isset($category->descriptionformat)) {
$category->descriptionformat = FORMAT_MOODLE;
}
$text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
echo format_text($text, $category->descriptionformat, $options);
echo $OUTPUT->box_end();

// Print out all the sub-categories
// In order to view hidden subcategories the user must have the viewhiddencategories
// capability in the current category.
if (has_capability('moodle/category:viewhiddencategories', $context)) {
$categorywhere = '';
} else {
$categorywhere = 'AND cc.visible = 1';
}
// We're going to preload the context for the subcategory as we know that we
// need it later on for formatting.

$ctxselect = context_helper::get_preload_record_columns_sql('ctx');
$sql = "SELECT cc.*, $ctxselect
FROM {course_categories} cc
JOIN {context} ctx ON cc.id = ctx.instanceid
WHERE cc.parent = :parentid AND
ctx.contextlevel = :contextlevel
$categorywhere
ORDER BY cc.sortorder ASC";
$subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id, 'contextlevel' => CONTEXT_COURSECAT));
// Prepare a table to display the sub categories.
$table = new html_table;
$table->attributes = array('border' => '0', 'cellspacing' => '2', 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter category_subcategories');
$table->head = array(new lang_string('subcategories'));
$table->data = array();
$baseurl = new moodle_url('/course/category.php');
foreach ($subcategories as $subcategory) {
// Preload the context we will need it to format the category name shortly.
context_helper::preload_from_record($subcategory);
$context = context_coursecat::instance($subcategory->id);
// Prepare the things we need to create a link to the subcategory
$attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
$text = format_string($subcategory->name, true, array('context' => $context));
// Add the subcategory to the table
$baseurl->param('id', $subcategory->id);
$table->data[] = array(html_writer::link($baseurl, $text, $attributes));
}

$subcategorieswereshown = (count($table->data) > 0);
if ($subcategorieswereshown) {
echo html_writer::table($table);
}

// Print out all the courses.
$courses = get_courses_page($category->id, 'c.sortorder ASC',
'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
$totalcount, $page*$perpage, $perpage);
$numcourses = count($courses);

// We can consider that we are using pagination when the total count of courses is different than the one returned.
$pagingmode = $totalcount != $numcourses;

if (!$courses) {
// There is no course to display.
if (empty($subcategorieswereshown)) {
echo $OUTPUT->heading(get_string("nocoursesyet"));
}
} else if ($numcourses <= $CFG->courseswithsummarieslimit and !$pagingmode) {
// We display courses with their summaries as we have not reached the limit, also we are not
// in paging mode and not allowed to edit either.
echo $OUTPUT->box_start('courseboxes');
print_courses($category);
echo $OUTPUT->box_end();
} else {
// The conditions above have failed, we display a basic list of courses with paging/editing options.
echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");

echo '<form id="movecourses" action="category.php" method="post"><div>';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<table border="0" cellspacing="2" cellpadding="4" class="generaltable boxaligncenter"><tr>';
echo '<th class="header" scope="col">'.get_string('courses').'</th>';
echo '<th class="header" scope="col">&nbsp;</th>';
echo '</tr>';

$count = 0;

// Checking if we are at the first or at the last page, to allow courses to
// be moved up and down beyond the paging border
if ($totalcount > $perpage) {
$atfirstpage = ($page == 0);
if ($perpage > 0) {
$atlastpage = (($page + 1) == ceil($totalcount / $perpage));
} else {
$atlastpage = true;
}
} else {
$atfirstpage = true;
$atlastpage = true;
}

$baseurl = new moodle_url('/course/category.php', $urlparams + array('sesskey' => sesskey()));
foreach ($courses as $acourse) {
$coursecontext = context_course::instance($acourse->id);

$count++;
$up = ($count > 1 || !$atfirstpage);
$down = ($count < $numcourses || !$atlastpage);

$linkcss = $acourse->visible ? '' : ' class="dimmed" ';
echo '<tr>';
$coursename = get_course_display_name_for_list($acourse);
echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
echo '<td align="right">';
// print enrol info
if ($icons = enrol_get_course_info_icons($acourse)) {
foreach ($icons as $pix_icon) {
echo $OUTPUT->render($pix_icon);
}
}
if (!empty($acourse->summary)) {
$url = new moodle_url("/course/info.php?id=$acourse->id");
echo $OUTPUT->action_link($url, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
new popup_action('click', $url, 'courseinfo'), array('title'=>get_string('summary')));
}
echo "</td>";
echo "</tr>";
}

echo '</table>';
echo '</div></form>';
echo '<br />';
}
$content = $courserenderer->course_category($id);
echo $OUTPUT->header();

echo '<div class="buttons">';
if (has_capability('moodle/course:create', $context)) {
// Print button to create a new course
$url = new moodle_url('/course/edit.php', array('category' => $category->id, 'returnto' => 'category'));
echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
}

if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
print_course_request_buttons(context_system::instance());
}
echo '</div>';
echo $content;

echo $OUTPUT->footer();
44 changes: 6 additions & 38 deletions course/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,45 +43,13 @@
if (can_edit_in_category()) {
$managebutton = $OUTPUT->single_button(new moodle_url('/course/manage.php'),
get_string('managecourses'), 'get');
$PAGE->set_button($managebutton);
}
$PAGE->set_heading($COURSE->fullname);
$content = $courserenderer->course_category(0);

$showaddcoursebutton = true;
if ($countcategories > 1 || ($countcategories == 1 && $DB->count_records('course') > 200)) {
$strcategories = get_string('categories');
echo $OUTPUT->header();
echo $OUTPUT->skip_link_target();
echo $content;

$PAGE->set_title("$site->shortname: $strcategories");
$PAGE->set_heading($COURSE->fullname);
if (isset($managebutton)) {
$PAGE->set_button($managebutton);
}
echo $OUTPUT->header();
echo $OUTPUT->heading($strcategories);
echo $OUTPUT->skip_link_target();
echo $OUTPUT->box_start('categorybox');
print_whole_category_list();
echo $OUTPUT->box_end();
echo $courserenderer->course_search_form();
} else {
$strfulllistofcourses = get_string('fulllistofcourses');

$PAGE->set_title("$site->shortname: $strfulllistofcourses");
$PAGE->set_heading($COURSE->fullname);
if (isset($managebutton)) {
$PAGE->set_button($managebutton);
}
echo $OUTPUT->header();
echo $OUTPUT->skip_link_target();
echo $OUTPUT->box_start('courseboxes');
$showaddcoursebutton = print_courses(0);
echo $OUTPUT->box_end();
}

echo $OUTPUT->container_start('buttons');
if (has_capability('moodle/course:create', $systemcontext) && $showaddcoursebutton) {
// Print link to create a new course, for the 1st available category.
$options = array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat');
echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
}
print_course_request_buttons($systemcontext);
echo $OUTPUT->container_end();
echo $OUTPUT->footer();
12 changes: 6 additions & 6 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1508,21 +1508,21 @@ function print_category_info($category, $depth = 0, $showcourses = false, array
/**
* Print the buttons relating to course requests.
*
* @param object $systemcontext the system context.
* @param object $context current page context.
*/
function print_course_request_buttons($systemcontext) {
function print_course_request_buttons($context) {
global $CFG, $DB, $OUTPUT;
if (empty($CFG->enablecourserequests)) {
return;
}
if (!has_capability('moodle/course:create', $systemcontext) && has_capability('moodle/course:request', $systemcontext)) {
if (!has_capability('moodle/course:create', $context) && has_capability('moodle/course:request', $context)) {
/// Print a button to request a new course
echo $OUTPUT->single_button('request.php', get_string('requestcourse'), 'get');
echo $OUTPUT->single_button(new moodle_url('/course/request.php'), get_string('requestcourse'), 'get');
}
/// Print a button to manage pending requests
if (has_capability('moodle/site:approvecourse', $systemcontext)) {
if ($context->contextlevel == CONTEXT_SYSTEM && has_capability('moodle/site:approvecourse', $context)) {
$disabled = !$DB->record_exists('course_request', array());
echo $OUTPUT->single_button('pending.php', get_string('coursespending'), 'get', array('disabled'=>$disabled));
echo $OUTPUT->single_button(new moodle_url('/course/pending.php'), get_string('coursespending'), 'get', array('disabled' => $disabled));
}
}

Expand Down
111 changes: 111 additions & 0 deletions course/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,117 @@ protected function coursecat_tree(coursecat_helper $chelper, $coursecat) {

return $content;
}

/**
* Renders HTML to display particular course category - list of it's subcategories and courses
*
* Invoked from /course/index.php
*
* @param int|stdClass|coursecat $category
*/
public function course_category($category) {
global $CFG;
require_once($CFG->libdir. '/coursecatlib.php');
$coursecat = coursecat::get(is_object($category) ? $category->id : $category);
$site = get_site();
$output = '';

if (!$coursecat->id && coursecat::count_all() == 1) {
// There exists only one category in the system, do not display link to it
$coursecat = coursecat::get_default();
$strfulllistofcourses = get_string('fulllistofcourses');
$this->page->set_title("$site->shortname: $strfulllistofcourses");
} else if (!$coursecat->id) {
$strcategories = get_string('categories');
$this->page->set_title("$site->shortname: $strcategories");
} else {
$this->page->set_title("$site->shortname: ". $coursecat->get_formatted_name());
$this->page->set_button($this->course_search_form('', 'navbar'));

// Print the category selector
$output .= html_writer::start_tag('div', array('class' => 'categorypicker'));
$select = new single_select(new moodle_url('/course/category.php'), 'id',
coursecat::make_categories_list(), $coursecat->id, null, 'switchcategory');
$select->set_label(get_string('categories').':');
$output .= $this->render($select);
$output .= html_writer::end_tag('div'); // .categorypicker
}

// Print current category description
$chelper = new coursecat_helper();
if ($description = $chelper->get_category_formatted_description($coursecat)) {
$output .= $this->box($description, array('class' => 'generalbox info'));
}

// Prepare parameters for courses and categories lists in the tree
$chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO)
->set_attributes(array('class' => 'category-browse category-browse-'.$coursecat->id));

$coursedisplayoptions = array();
$catdisplayoptions = array();
$browse = optional_param('browse', null, PARAM_ALPHA);
$perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
$page = optional_param('page', 0, PARAM_INT);
if ($coursecat->id) {
$baseurl = new moodle_url('/course/category.php', array('id' => $coursecat->id));
} else {
$baseurl = new moodle_url('/course/index.php');
}
if ($browse === 'courses' || !$coursecat->has_children()) {
$coursedisplayoptions['limit'] = $perpage;
$coursedisplayoptions['offset'] = $page * $perpage;
$coursedisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'perpage' => $perpage));
$catdisplayoptions['nodisplay'] = true;
$catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 0));
$catdisplayoptions['viewmoretext'] = new lang_string('viewallsubcategores');
} else if ($browse === 'categories' || !$coursecat->has_courses()) {
$coursedisplayoptions['nodisplay'] = true;
$catdisplayoptions['limit'] = $perpage;
$catdisplayoptions['offset'] = $page * $perpage;
$catdisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'perpage' => $perpage));
$coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 0));
$coursedisplayoptions['viewmoretext'] = new lang_string('viewallcourses');
} else {
// we have a category that has both subcategories and courses, display pagination separately
$coursedisplayoptions['limit'] = $CFG->coursesperpage;
$catdisplayoptions['limit'] = $CFG->coursesperpage;
$coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1));
$catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1));
}
$chelper->set_courses_display_options($coursedisplayoptions)->set_categories_display_options($catdisplayoptions);

// Display course category tree
$output .= $this->coursecat_tree($chelper, $coursecat);

// Add course search form (if we are inside category it was already added to the navbar)
if (!$coursecat->id) {
$output .= $this->course_search_form();
}

// Add action buttons
$output .= $this->container_start('buttons');
$context = get_category_or_system_context($coursecat->id);
if (has_capability('moodle/course:create', $context)) {
// Print link to create a new course, for the 1st available category.
if ($coursecat->id) {
$url = new moodle_url('/course/edit.php', array('category' => $coursecat->id, 'returnto' => 'category'));
} else {
$url = new moodle_url('/course/edit.php', array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat'));
}
$output .= $this->single_button($url, get_string('addnewcourse'), 'get');
}
ob_start();
if (coursecat::count_all() == 1) {
print_course_request_buttons(context_system::instance());
} else {
print_course_request_buttons($context);
}
$output .= ob_get_contents();
ob_end_clean();
$output .= $this->container_end();

return $output;
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,7 @@
$string['viewallcourses'] = 'View all courses';
$string['viewallcoursescategories'] = 'View all courses and categories';
$string['viewmore'] = 'View more';
$string['viewallsubcategores'] = 'View all subcategories';
$string['viewfileinpopup'] = 'View file in a popup window';
$string['viewprofile'] = 'View profile';
$string['views'] = 'Views';
Expand Down

0 comments on commit 8e57a6d

Please sign in to comment.