Commit 4c5cde31 authored by Mark Nelson's avatar Mark Nelson Committed by Damyon Wiese
Browse files

MDL-57433 block_course_overview: removed block from core

Part of MDL-55611 epic.
parent 932f299b
<?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
*
* @package block_course_overview
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot.'/blocks/course_overview/locallib.php');
/**
* Course overview block
*
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_course_overview extends block_base {
/**
* If this is passed as mynumber then showallcourses, irrespective of limit by user.
*/
const SHOW_ALL_COURSES = -2;
/**
* Block initialization
*/
public function init() {
$this->title = get_string('pluginname', 'block_course_overview');
}
/**
* Return contents of course_overview block
*
* @return stdClass contents of block
*/
public function get_content() {
global $USER, $CFG, $DB;
require_once($CFG->dirroot.'/user/profile/lib.php');
if($this->content !== NULL) {
return $this->content;
}
$config = get_config('block_course_overview');
$this->content = new stdClass();
$this->content->text = '';
$this->content->footer = '';
$content = array();
$updatemynumber = optional_param('mynumber', -1, PARAM_INT);
if ($updatemynumber >= 0) {
block_course_overview_update_mynumber($updatemynumber);
}
profile_load_custom_fields($USER);
$showallcourses = ($updatemynumber === self::SHOW_ALL_COURSES);
list($sortedcourses, $sitecourses, $totalcourses) = block_course_overview_get_sorted_courses($showallcourses);
$overviews = block_course_overview_get_overviews($sitecourses);
$renderer = $this->page->get_renderer('block_course_overview');
if (!empty($config->showwelcomearea)) {
require_once($CFG->dirroot.'/message/lib.php');
$msgcount = message_count_unread_messages();
$this->content->text = $renderer->welcome_area($msgcount);
}
// Number of sites to display.
if ($this->page->user_is_editing() && empty($config->forcedefaultmaxcourses)) {
$this->content->text .= $renderer->editing_bar_head($totalcourses);
}
if (empty($sortedcourses)) {
$this->content->text .= get_string('nocourses','my');
} else {
// For each course, build category cache.
$this->content->text .= $renderer->course_overview($sortedcourses, $overviews);
$this->content->text .= $renderer->hidden_courses($totalcourses - count($sortedcourses));
}
return $this->content;
}
/**
* Allow the block to have a configuration page
*
* @return boolean
*/
public function has_config() {
return true;
}
/**
* Locations where block can be displayed
*
* @return array
*/
public function applicable_formats() {
return array('my' => true);
}
/**
* 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');
return !empty($config->showwelcomearea);
}
}
<?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 caps.
*
* @package block_course_overview
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
'block/course_overview:myaddinstance' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'user' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/my:manageblocks'
),
'block/course_overview:addinstance' => array(
'riskbitmask' => RISK_SPAM | RISK_XSS,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => array(
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/site:manageblocks'
)
);
<?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/>.
/**
* Lang strings for course_overview block
*
* @package block_course_overview
* @copyright 2012 Adam Olley <adam.olley@netspot.com.au>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['activityoverview'] = 'You have {$a}s that need attention';
$string['alwaysshowall'] = 'Always show all';
$string['collapseall'] = 'Collapse all course lists';
$string['configotherexpanded'] = 'If enabled, other courses will be expanded by default unless overridden by user preferences.';
$string['configpreservestates'] = 'If enabled, the collapsed/expanded states set by the user are stored and used on each load.';
$string['course_overview:addinstance'] = 'Add a new course overview block';
$string['course_overview:myaddinstance'] = 'Add a new course overview block to Dashboard';
$string['defaultmaxcourses'] = 'Default maximum courses';
$string['defaultmaxcoursesdesc'] = 'Maximum courses which should be displayed on course overview block, 0 will show all courses';
$string['expandall'] = 'Expand all course lists';
$string['forcedefaultmaxcourses'] = 'Force maximum courses';
$string['forcedefaultmaxcoursesdesc'] = 'If set then user will not be able to change his/her personal setting';
$string['fullpath'] = 'All categories and subcategories';
$string['hiddencoursecount'] = 'You have {$a} hidden course';
$string['hiddencoursecountplural'] = 'You have {$a} hidden courses';
$string['hiddencoursecountwithshowall'] = 'You have {$a->coursecount} hidden course ({$a->showalllink})';
$string['hiddencoursecountwithshowallplural'] = 'You have {$a->coursecount} hidden courses ({$a->showalllink})';
$string['message'] = 'message';
$string['messages'] = 'messages';
$string['movecourse'] = 'Move course: {$a}';
$string['movecoursehere'] = 'Move course here';
$string['movetofirst'] = 'Move {$a} course to top';
$string['moveafterhere'] = 'Move {$a->movingcoursename} course after {$a->currentcoursename}';
$string['movingcourse'] = 'You are moving: {$a->fullname} ({$a->cancellink})';
$string['none'] = 'None';
$string['numtodisplay'] = 'Number of courses to display: ';
$string['onlyparentname'] = 'Parent category only';
$string['otherexpanded'] = 'Other courses expanded';
$string['pluginname'] = 'Course overview';
$string['preservestates'] = 'Preserve expanded states';
$string['shortnameprefix'] = 'Includes {$a}';
$string['shortnamesufixsingular'] = ' (and {$a} other)';
$string['shortnamesufixprural'] = ' (and {$a} others)';
$string['showcategories'] = 'Categories to show';
$string['showcategoriesdesc'] = 'Should course categories be displayed below each course?';
$string['showchildren'] = 'Show children';
$string['showchildrendesc'] = 'Should child courses be listed underneath the main course title?';
$string['showwelcomearea'] = 'Show welcome area';
$string['showwelcomeareadesc'] = 'Show the welcome area above the course list?';
$string['view_edit_profile'] = '(View and edit your profile.)';
$string['welcome'] = 'Welcome {$a}';
$string['youhavemessages'] = 'You have {$a} unread ';
$string['youhavenomessages'] = 'You have no unread ';
<?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
* @copyright 2012 Adam Olley <adam.olley@netspot.com.au>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_NONE', '0');
define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_ONLY_PARENT_NAME', '1');
define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_FULL_PATH', '2');
/**
* Display overview for courses
*
* @param array $courses courses for which overview needs to be shown
* @return array html overview
*/
function block_course_overview_get_overviews($courses) {
$htmlarray = array();
if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
// Split courses list into batches with no more than MAX_MODINFO_CACHE_SIZE courses in one batch.
// Otherwise we exceed the cache limit in get_fast_modinfo() and rebuild it too often.
if (defined('MAX_MODINFO_CACHE_SIZE') && MAX_MODINFO_CACHE_SIZE > 0 && count($courses) > MAX_MODINFO_CACHE_SIZE) {
$batches = array_chunk($courses, MAX_MODINFO_CACHE_SIZE, true);
} else {
$batches = array($courses);
}
foreach ($batches as $courses) {
foreach ($modules as $fname) {
$fname($courses, $htmlarray);
}
}
}
return $htmlarray;
}
/**
* Sets user preference for maximum courses to be displayed in course_overview block
*
* @param int $number maximum courses which should be visible
*/
function block_course_overview_update_mynumber($number) {
set_user_preference('course_overview_number_of_courses', $number);
}
/**
* Sets user course sorting preference in course_overview block
*
* @param array $sortorder list of course ids
*/
function block_course_overview_update_myorder($sortorder) {
$value = implode(',', $sortorder);
if (core_text::strlen($value) > 1333) {
// The value won't fit into the user preference. Remove courses in the end of the list (mostly likely user won't even notice).
$value = preg_replace('/,[\d]*$/', '', core_text::substr($value, 0, 1334));
}
set_user_preference('course_overview_course_sortorder', $value);
}
/**
* Gets user course sorting preference in course_overview block
*
* @return array list of course ids
*/
function block_course_overview_get_myorder() {
if ($value = get_user_preferences('course_overview_course_sortorder')) {
return explode(',', $value);
}
// If preference was not found, look in the old location and convert if found.
$order = array();
if ($value = get_user_preferences('course_overview_course_order')) {
$order = unserialize_array($value);
block_course_overview_update_myorder($order);
unset_user_preference('course_overview_course_order');
}
return $order;
}
/**
* 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_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', $diff);
} else {
$suffix = get_string('shortnamesufixsingular', 'block_course_overview', $diff);
}
}
$shortnames = get_string('shortnameprefix', 'block_course_overview', implode('; ', $shortnames));
$shortnames .= $suffix;
}
return isset($shortnames) ? $shortnames : false;
}
/**
* Returns maximum number of courses which will be displayed in course_overview block
*
* @param bool $showallcourses if set true all courses will be visible.
* @return int maximum number of courses
*/
function block_course_overview_get_max_user_courses($showallcourses = false) {
// Get block configuration
$config = get_config('block_course_overview');
$limit = $config->defaultmaxcourses;
// If max course is not set then try get user preference
if (empty($config->forcedefaultmaxcourses)) {
if ($showallcourses) {
$limit = 0;
} else {
$limit = get_user_preferences('course_overview_number_of_courses', $limit);
}
}
return $limit;
}
/**
* Return sorted list of user courses
*
* @param bool $showallcourses if set true all courses will be visible.
* @return array list of sorted courses and count of courses.
*/
function block_course_overview_get_sorted_courses($showallcourses = false) {
global $USER;
$limit = block_course_overview_get_max_user_courses($showallcourses);
$courses = enrol_get_my_courses();
$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;
}
}
// Get remote courses.
$remotecourses = array();
if (is_enabled_auth('mnet')) {
$remotecourses = get_my_remotecourses();
}
// Remote courses will have -ve remoteid as key, so it can be differentiated from normal courses
foreach ($remotecourses as $id => $val) {
$remoteid = $val->remoteid * -1;
$val->id = $remoteid;
$courses[$remoteid] = $val;
}
$order = block_course_overview_get_myorder();
$sortedcourses = array();
$counter = 0;
// Get courses in sort order into list.
foreach ($order as $key => $cid) {
if (($counter >= $limit) && ($limit != 0)) {
break;
}
// Make sure user is still enroled.
if (isset($courses[$cid])) {
$sortedcourses[$cid] = $courses[$cid];
$counter++;
}
}
// Append unsorted courses if limit allows
foreach ($courses as $c) {
if (($limit != 0) && ($counter >= $limit)) {
break;
}
if (!in_array($c->id, $order)) {
$sortedcourses[$c->id] = $c;
$counter++;
}
}
// From list extract site courses for overview
$sitecourses = array();
foreach ($sortedcourses as $key => $course) {
if ($course->id > 0) {
$sitecourses[$key] = $course;
}
}
return array($sortedcourses, $sitecourses, count($courses));
}
M.block_course_overview = {}
M.block_course_overview.add_handles = function(Y) {
M.block_course_overview.Y = Y;
var MOVEICON = {
pix: "i/move_2d",
component: 'moodle'
};
YUI().use('dd-constrain', 'dd-proxy', 'dd-drop', 'dd-plugin', function(Y) {
//Static Vars
var goingUp = false, lastY = 0;
var list = Y.Node.all('.course_list .coursebox');
list.each(function(v, k) {
// Replace move link and image with move_2d image.
var imagenode = v.one('.course_title .move a img');
imagenode.setAttribute('src', M.util.image_url(MOVEICON.pix, MOVEICON.component));
imagenode.addClass('cursor');
v.one('.course_title .move a').replace(imagenode);
var dd = new Y.DD.Drag({
node: v,
target: {
padding: '0 0 0 20'
}
}).plug(Y.Plugin.DDProxy, {
moveOnEnd: false
}).plug(Y.Plugin.DDConstrained, {
constrain2node: '.course_list'
});
dd.addHandle('.course_title .move');
});
Y.DD.DDM.on('drag:start', function(e) {
//Get our drag object
var drag = e.target;
//Set some styles here
drag.get('node').setStyle('opacity', '.25');
drag.get('dragNode').addClass('block_course_overview');
drag.get('dragNode').set('innerHTML', drag.get('node').get('innerHTML'));
drag.get('dragNode').setStyles({
opacity: '.5',
borderColor: drag.get('node').getStyle('borderColor'),
backgroundColor: drag.get('node').getStyle('backgroundColor')
});
});
Y.DD.DDM.on('drag:end', function(e) {
var drag = e.target;
//Put our styles back
drag.get('node').setStyles({
visibility: '',
opacity: '1'
});
M.block_course_overview.save(Y);
});
Y.DD.DDM.on('drag:drag', function(e) {
//Get the last y point
var y = e.target.lastXY[1];
//is it greater than the lastY var?
if (y < lastY) {
//We are going up
goingUp = true;
} else {
//We are going down.
goingUp = false;
}
//Cache for next check
lastY = y;
});
Y.DD.DDM.on('drop:over', function(e) {
//Get a reference to our drag and drop nodes
var drag = e.drag.get('node'),
drop = e.drop.get('node');
//Are we dropping on a li node?
if (drop.hasClass('coursebox')) {
//Are we not going up?
if (!goingUp) {
drop = drop.get('nextSibling');
}
//Add the node to this list
e.drop.get('node').get('parentNode').insertBefore(drag, drop);
//Resize this nodes shim, so we can drop on it later.
e.drop.sizeShim();
}
});
Y.DD.DDM.on('drag:drophit', function(e) {
var drop = e.drop.get('node'),
drag = e.drag.get('node');
//if we are not on an li, we must have been dropped on a ul
if (!drop.hasClass('coursebox')) {
if (!drop.contains(drag)) {
drop.appendChild(drag);
}
}
});
});
}
M.block_course_overview.save = function() {
var Y = M.block_course_overview.Y;
var sortorder = Y.one('.course_list').get('children').getAttribute('id');
for (var i = 0; i < sortorder.length; i++) {
sortorder[i] = sortorder[i].substring(7);
}
var params = {
sesskey : M.cfg.sesskey,
sortorder : sortorder
};
Y.io(M.cfg.wwwroot+'/blocks/course_overview/save.php', {
method: 'POST',
data: build_querystring(params),
context: this
});
}
/**