Commit 0f3fe4b6 authored by stronk7's avatar stronk7
Browse files

First Official realease of the "Blocks System".

Integrated with Moodle:
    - Install automatically.
    - Update course->blockinfo for each course at installation time.
    - Modified course creation to insert into blockinfo field.
    - Modified restore to insert into blockinfo field.
    - Admin options (hide, show, delete, config) from admin page.
    - Lang strings inserted (en only).
    - Database support to mysql and postgresql (not tested!!).
Moodle, backup and block versions updated to 2004041800.
Tested with mysql: Install fresh and upgrade from previous.
section_activities block renamed to social_activities and created
its own lang file to support name "Social Activities". This can
be changed at any time.

TODO:
=====
Support it really in backup/restore.
????

Enjoy!! Ciao :-)
parent e8f1458c
<?PHP // $Id$
// block.php - allows admin to edit all local configuration variables for a block
require_once('../config.php');
require_once($CFG->libdir.'/blocklib.php');
require_login();
if (!isadmin()) {
error('Only an admin can use this page');
}
if (!$site = get_site()) {
error("Site isn't defined!");
}
require_variable($_REQUEST['block']);
$blockid = intval($_REQUEST['block']);
if(($blockrecord = get_record('blocks', 'id', $blockid)) === false) {
error('This block does not exist');
}
$block = block_instance($blockrecord->name, NULL);
if($block === false) {
error('Problem in instantiating block object');
}
/// If data submitted, then process and store.
if ($config = data_submitted()) {
unset($config['block']); // This will always be set if we have reached this point
$block->handle_config($config);
print_header();
redirect("$CFG->wwwroot/$CFG->admin/blocks.php", get_string("changessaved"), 1);
exit;
}
/// Otherwise print the form.
$stradmin = get_string('administration');
$strconfiguration = get_string('configuration');
$strmanageblocks = get_string('manageblocks');
$strblockname = $block->get_title();
print_header($site->shortname.': '.$strblockname.": $strconfiguration", $site->fullname,
"<a href=\"index.php\">$stradmin</a> -> ".
"<a href=\"configure.php\">$strconfiguration</a> -> ".
"<a href=\"blocks.php\">$strmanageblocks</a> -> ".$strblockname);
print_heading($strblockname);
print_simple_box('<center>'.get_string('configwarning').'</center>', 'center', '50%');
echo '<br />';
$block->print_config();
print_footer();
?>
<?PHP // $Id$
// Allows the admin to configure blocks (hide/show, delete and configure)
require_once('../config.php');
require_once($CFG->libdir.'/blocklib.php');
optional_variable($_GET['hide']);
optional_variable($_GET['show']);
optional_variable($_GET['delete']);
optional_variable($_GET['confirm'], 0);
$delete = $_GET['delete']; // Dependency remover
require_login();
if (!isadmin()) {
error("Only administrators can use this page!");
}
if (!$site = get_site()) {
error("Site isn't defined!");
}
/// Print headings
$stradministration = get_string('administration');
$strconfiguration = get_string('configuration');
$strmanageblocks = get_string('manageblocks');
$strdelete = get_string('delete');
$strversion = get_string('version');
$strhide = get_string('hide');
$strshow = get_string('show');
$strsettings = get_string('settings');
$strcourses = get_string('courses');
$strname = get_string('name');
print_header("$site->shortname: $strmanageblocks", "$site->fullname",
"<a href=\"index.php\">$stradministration</a> -> ".
"<a href=\"configure.php\">$strconfiguration</a> -> $strmanageblocks");
print_heading($strmanageblocks);
/// If data submitted, then process and store.
if (!empty($_GET['hide'])) {
if (!$block = get_record('blocks', 'id', $_GET['hide'])) {
error("Block doesn't exist!");
}
set_field('blocks', 'visible', '0', 'id', $block->id); // Hide block
// [pj] There is no need to do this, since print_course_blocks()
// will not display blocks which are disabled. In fact, the
// comment "This block is hidden. Don't show it." there
// suggests that this db update SHOULDN'T be needed.
///I'M NOT SURE IF THIS IS THE CORRECT APPROACH
//blocks_update_every_block_by_id($block->id, "hide"); // Hide blocks in all courses by id
}
if (!empty($_GET['show'])) {
if (!$block = get_record('blocks', 'id', $_GET['show'])) {
error("Block doesn't exist!");
}
set_field('blocks', 'visible', '1', 'id', $block->id); // Show block
// [pj] See note above
///I'M NOT SURE IF THIS IS THE CORRECT APPROACH
//blocks_update_every_block_by_id($block->id,"show"); // Show blocks in all courses by id
}
if (!empty($delete)) {
if (!$block = get_record('blocks', 'id', $delete)) {
error("Block doesn't exist!");
}
$strblockname = get_string('modulename', 'block_'.$block->name);
if (!$_GET['confirm']) {
notice_yesno(get_string('blockdeleteconfirm', '', $strblockname),
'blocks.php?delete='.$block->id.'&amp;confirm=1',
'blocks.php');
print_footer();
exit;
} else {
// Delete block
if (!delete_records('blocks', 'id', $block->id)) {
notify("Error occurred while deleting the $strblockname record from blocks table");
}
blocks_update_every_block_by_id($block->id, 'delete'); // Delete blocks in all courses by id
// Then the tables themselves
if ($tables = $db->Metatables()) {
$prefix = $CFG->prefix.$block->name;
foreach ($tables as $table) {
if (strpos($table, $prefix) === 0) {
if (!execute_sql("DROP TABLE $table", false)) {
notify("ERROR: while trying to drop table $table");
}
}
}
}
$a->block = $strblockname;
$a->directory = $CFG->dirroot.'/blocks/'.$block->name;
notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
}
}
/// Main display starts here
/// Get and sort the existing blocks
if (!$blocks = get_records('blocks')) {
error('No blocks found!'); // Should never happen
}
foreach ($blocks as $block) {
if(($blockobject = block_instance($block->name, NULL)) === false) {
// Failed to load
continue;
}
$blockbyname[$blockobject->get_title()] = $block->id;
$blockobjects[$block->id] = $blockobject;
}
ksort($blockbyname);
/// Print the table of all blocks
if (empty($THEME->custompix)) {
$pixpath = '../pix';
// [pj] This is not used anywhere, but I'm leaving it in for the future
//$modpixpath = '../mod';
} else {
$pixpath = '../theme/'.$CFG->theme.'/pix';
// [pj] This is not used anywhere, but I'm leaving it in for the future
//$modpixpath = '../theme/'.$CFG->theme.'/pix/mod';
}
$table->head = array ($strname, $strcourses, $strversion, $strhide.'/'.$strshow, $strdelete, $strsettings);
$table->align = array ('LEFT', 'RIGHT', 'LEFT', 'CENTER', 'CENTER', 'CENTER');
$table->wrap = array ("NOWRAP", "", "", "", "","");
$table->size = array ("100%", "10", "10", "10", "10","12");
$table->width = "100";
foreach ($blockbyname as $blockname => $blockid) {
// [pj] This is not used anywhere, but I'm leaving it in for the future
//$icon = "<img src=\"$modpixpath/$block->name/icon.gif\" hspace=10 height=16 width=16 border=0>";
$blockobject = $blockobjects[$blockid];
$delete = '<a href="blocks.php?delete='.$blockid.'">'.$strdelete.'</a>';
$settings = ''; // By default, no configuration
if($blockobject->has_config()) {
$settings = '<a href="block.php?block='.$blockid.'">'.$strsettings.'</a>';
}
$count = blocks_get_courses_using_block_by_id($blockid);
$class = ''; // Nothing fancy, by default
if ($block->visible) {
$visible = '<a href="blocks.php?hide='.$blockid.'" title="'.$strhide.'">'.
'<img src="'.$pixpath.'/i/hide.gif" style="height: 16px; width: 16px;" /></a>';
} else {
$visible = '<a href="blocks.php?show='.$blockid.'" title="'.$strshow.'">'.
'<img src="'.$pixpath.'/i/show.gif" style="height: 16px; width: 16px;" /></a>';
$class = ' class="dimmed_text"'; // Leading space required!
}
$table->data[] = array ('<p'.$class.'>'.$blockobject->get_title().'</p>', $count, $blockobject->get_version(), $visible, $delete, $settings);
}
echo '<p>';
print_table($table);
echo '</p>';
print_footer();
?>
...@@ -194,11 +194,16 @@ ...@@ -194,11 +194,16 @@
/// Upgrade backup/restore system if necessary /// Upgrade backup/restore system if necessary
require_once("$CFG->dirroot/backup/lib.php"); require_once("$CFG->dirroot/backup/lib.php");
upgrade_backup_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards upgrade_backup_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
/// Upgrade blocks system if necessary
require_once("$CFG->dirroot/lib/blocklib.php");
upgrade_blocks_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
/// Check all blocks and load (or upgrade them if necessary)
upgrade_blocks_plugins("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
/// Find and check all modules and load them up or upgrade them if necessary /// Find and check all modules and load them up or upgrade them if necessary
if (!$mods = get_list_of_plugins("mod") ) { if (!$mods = get_list_of_plugins("mod") ) {
...@@ -374,6 +379,8 @@ ...@@ -374,6 +379,8 @@
get_string("adminhelplanguage")."</font><br />"; get_string("adminhelplanguage")."</font><br />";
$configdata .= "<font size=+1>&nbsp;</font><a href=\"modules.php\">".get_string("managemodules")."</a> - <font size=1>". $configdata .= "<font size=+1>&nbsp;</font><a href=\"modules.php\">".get_string("managemodules")."</a> - <font size=1>".
get_string("adminhelpmanagemodules")."</font><br />"; get_string("adminhelpmanagemodules")."</font><br />";
$configdata .= "<font size=+1>&nbsp;</font><a href=\"blocks.php\">".get_string("manageblocks")."</a> - <font size=1>".
get_string("adminhelpmanageblocks")."</font><br />";
$configdata .= "<font size=+1>&nbsp;</font><a href=\"filters.php\">".get_string("managefilters")."</a> - <font size=1>". $configdata .= "<font size=+1>&nbsp;</font><a href=\"filters.php\">".get_string("managefilters")."</a> - <font size=1>".
get_string("adminhelpmanagefilters")."</font><br />"; get_string("adminhelpmanagefilters")."</font><br />";
if (!isset($CFG->disablescheduledbackups)) { if (!isset($CFG->disablescheduledbackups)) {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
require_once ("../config.php"); require_once ("../config.php");
require_once ("lib.php"); require_once ("lib.php");
require_once ("backuplib.php"); require_once ("backuplib.php");
require_once ("$CFG->libdir/blocklib.php");
optional_variable($id); // course id optional_variable($id); // course id
optional_variable($cancel); optional_variable($cancel);
......
...@@ -339,7 +339,7 @@ ...@@ -339,7 +339,7 @@
$db->debug = false; $db->debug = false;
if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) { if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
notify(get_string("databasesuccess"), "green"); notify(get_string("databasesuccess"), "green");
notify(get_string("databaseupgradebackups", "", $backup_release)); notify(get_string("databaseupgradebackups", "", $backup_version));
print_continue($continueto); print_continue($continueto);
exit; exit;
} else { } else {
...@@ -362,7 +362,7 @@ ...@@ -362,7 +362,7 @@
$db->debug=false; $db->debug=false;
if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) { if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
notify(get_string("databasesuccess"), "green"); notify(get_string("databasesuccess"), "green");
notify(get_string("databaseupgradebackups", "", $backup_release)); notify(get_string("databaseupgradebackups", "", $backup_version));
print_continue($continueto); print_continue($continueto);
exit; exit;
} else { } else {
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
require_once ("../course/lib.php"); require_once ("../course/lib.php");
require_once ("lib.php"); require_once ("lib.php");
require_once ("restorelib.php"); require_once ("restorelib.php");
require_once("$CFG->libdir/blocklib.php");
//Optional //Optional
optional_variable($id); optional_variable($id);
......
...@@ -363,6 +363,7 @@ ...@@ -363,6 +363,7 @@
$course->summary = addslashes($course_header->course_summary); $course->summary = addslashes($course_header->course_summary);
$course->format = addslashes($course_header->course_format); $course->format = addslashes($course_header->course_format);
$course->showgrades = addslashes($course_header->course_showgrades); $course->showgrades = addslashes($course_header->course_showgrades);
$course->blockinfo = addslashes($course_header->blockinfo);
$course->newsitems = addslashes($course_header->course_newsitems); $course->newsitems = addslashes($course_header->course_newsitems);
$course->teacher = addslashes($course_header->course_teacher); $course->teacher = addslashes($course_header->course_teacher);
$course->teachers = addslashes($course_header->course_teachers); $course->teachers = addslashes($course_header->course_teachers);
...@@ -381,6 +382,18 @@ ...@@ -381,6 +382,18 @@
$course->visible = addslashes($course_header->course_visible); $course->visible = addslashes($course_header->course_visible);
$course->timecreated = addslashes($course_header->course_timecreated); $course->timecreated = addslashes($course_header->course_timecreated);
$course->timemodified = addslashes($course_header->course_timemodified); $course->timemodified = addslashes($course_header->course_timemodified);
//Adjust blockinfo field.
//NOTE: For now, it's imposible to find it in backup files because it isn't saved,
// so, we always rebuid it with defaults.
if (!$course->blockinfo) {
//Create blockinfo default content
if ($course->format == "social") {
$course->blockinfo = blocks_get_default_blocks (NULL,"participants,search_forums,calendar_month,calendar_upcoming,social_activities,recent_activity,admin,course_list");
} else {
//For topics and weeks formats (default built in the function)
$course->blockinfo = blocks_get_default_blocks();
}
}
//Now insert the record //Now insert the record
$newid = insert_record("course",$course); $newid = insert_record("course",$course);
if ($newid) { if ($newid) {
......
...@@ -5,6 +5,6 @@ ...@@ -5,6 +5,6 @@
// database (backup_version) to determine whether upgrades should // database (backup_version) to determine whether upgrades should
// be performed (see db/backup_*.php) // be performed (see db/backup_*.php)
$backup_version = 2004041000; // The current version is a date (YYYYMMDDXX) $backup_version = 2004041800; // The current version is a date (YYYYMMDDXX)
$backup_release = "1.3 development"; // User-friendly version number $backup_release = "1.3 development"; // User-friendly version number
CREATING NEW BLOCKS
-------------------------------------------------------------------------------
You have to derive a class that extends MoodleBlock.
The derived class MUST:
* Implement a constructor that:
1. Sets $this->content_type (BLOCK_TYPE_LIST or BLOCK_TYPE_TEXT)
2. Sets $this->header (BLOCK_SHOW_HEADER or BLOCK_HIDE_HEADER)
3. Sets $this->title
4. Sets $this->version
5. Sets $this->course equal to its only argument
The derived class MAY:
* Declare that the block has a configuration interface.
To do so:
1. Define a method has_config() {return true;}
2. Define a method print_config() that prints whatever
configuration interface you want to have.
3. Define a method handle_config($data) that does what
is needed. $data comes straight from data_submitted().
* Limit the course formats it can be displayed in.
To do so:
1. Define a method applicable_formats() which returns a
bitwise AND of one or more COURSE_FORMAT_XXX defined
constants.
* Select a "preferred" width which the course format will try to honor.
To do so:
1. Define a method preferred_width() which returns a number
measured in pixels.
* Declare that the block is going to hide its header.
To do so:
1. Define a method hide_header() {return true;}
<?PHP //$Id$
class CourseBlock_activity_modules extends MoodleBlock {
function CourseBlock_activity_modules($course) {
$this->title = get_string('activities');
$this->content_type = BLOCK_TYPE_LIST;
$this->course = $course;
$this->version = 2004041000;
}
function get_content() {
global $USER, $CFG;
// This is really NOT pretty, but let's do it simple for now...
global $modnamesused, $modnamesplural;
if($this->content !== NULL) {
return $this->content;
}
$this->content = New object;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
if ($modnamesused) {
foreach ($modnamesused as $modname => $modfullname) {
if ($modname != 'label') {
$this->content->items[] = '<a href="../mod/'.$modname.'/index.php?id='.$this->course->id.'">'.$modnamesplural[$modname].'</a>';
$this->content->icons[] = '<img src="'.$CFG->modpixpath.'/'.$modname.'/icon.gif" height="16" width="16" alt="">';
}
}
}
return $this->content;
}
}
?>
<?PHP //$Id$
//
// This file keeps track of upgrades to Moodle's
// blocks system.
//
// Sometimes, changes between versions involve
// alterations to database structures and other
// major things that may break installations.
//
// The upgrade function in this file will attempt
// to perform all the necessary actions to upgrade
// your older installtion to the current version.
//
// If there's something it cannot do itself, it
// will tell you what you need to do.
//
// Versions are defined by backup_version.php
//
// This file is tailored to MySQL
function activity_modules_upgrade($oldversion=0) {
global $CFG;
$result = true;
if ($oldversion < 2004041000 and $result) {
$result = true; //Nothing to do
}
//Finally, return result
return $result;
}
<?PHP //$Id$
class CourseBlock_admin extends MoodleBlock {
function CourseBlock_admin($course) {
$this->title = get_string('administration');
$this->content_type = BLOCK_TYPE_LIST;
$this->course = $course;
$this->version = 2004041000;
}
function get_content() {
global $USER, $CFG, $THEME;
require_once($CFG->dirroot.'/mod/forum/lib.php');
if($this->content !== NULL) {
return $this->content;
}
$this->content = New object;
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
if (isguest()) {
return $this->content;
}
if (isteacher($this->course->id)) {
$isteacheredit = isteacheredit($this->course->id);
if ($isteacheredit) {
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/edit.gif" height="16" width="16" alt="">';
if (isediting($this->course->id)) {
$this->content->items[]='<a href="view.php?id='.$this->course->id.'&amp;edit=off">'.get_string('turneditingoff').'</a>';
} else {
$this->content->items[]='<a href="view.php?id='.$this->course->id.'&amp;edit=on">'.get_string('turneditingon').'</a>';
}
$this->content->items[]='<a href="edit.php?id='.$this->course->id.'">'.get_string('settings').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/settings.gif" height="16" width="16" alt="">';
if (iscreator() or !empty($CFG->teacherassignteachers)) {
if (!$this->course->teachers) {
$this->course->teachers = get_string('defaultcourseteachers');
}
$this->content->items[]='<a href="teacher.php?id='.$this->course->id.'">'.$this->course->teachers.'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/users.gif" height="16" width="16" alt="">';
}
if (!$this->course->students) {
$this->course->students = get_string('defaultcoursestudents');
}
$this->content->items[]='<a href="student.php?id='.$this->course->id.'">'.$this->course->students.'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/users.gif" height="16" width="16" alt="">';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/backup/backup.php?id='.$this->course->id.'">'.get_string('backup').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/backup.gif" height="16" width="16" alt="">';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->course->id.'&amp;wdir=/backupdata">'.get_string('restore').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/restore.gif" height="16" width="16" alt="">';
$this->content->items[]='<a href="scales.php?id='.$this->course->id.'">'.get_string('scales').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/scales.gif" height="16" width="16" alt="">';
}
$this->content->items[]='<a href="grades.php?id='.$this->course->id.'">'.get_string('grades').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/grades.gif" height="16" width="16" alt="">';
$this->content->items[]='<a href="log.php?id='.$this->course->id.'">'.get_string('logs').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/log.gif" height="16" width="16" alt="">';
if ($isteacheredit) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->course->id.'">'.get_string('files').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/files.gif" height="16" width="16" alt="">';
}
$this->content->items[]='<a href="'.$CFG->wwwroot.'/doc/view.php?id='.$this->course->id.'&amp;file=teacher.html">'.get_string('help').'...</a>';
$this->content->icons[]='<img src="'.$CFG->modpixpath.'/resource/icon.gif" height="16" width="16" alt="">';
if ($teacherforum = forum_get_course_forum($this->course->id, 'teacher')) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/mod/forum/view.php?f='.$teacherforum->id.'">'.get_string('nameteacher', 'forum').'</a>';
$this->content->icons[]='<img src="'.$CFG->modpixpath.'/forum/icon.gif" height="16" width="16" alt="">';
}
} else if (!isguest()) { // Students menu
if ($this->course->showgrades) {
$this->content->items[]='<a href="grade.php?id='.$this->course->id.'">'.get_string('grades').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/grades.gif" height="16" width="16" alt="">';
}
if ($this->course->showreports) {
$this->content->items[]='<a href="user.php?id='.$this->course->id.'&amp;user='.$USER->id.'">'.get_string('activityreport').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/report.gif" height="16" width="16" alt="">';
}
if (is_internal_auth()) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/login/change_password.php?id='.$this->course->id.'">'.get_string('changepassword').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" height="16" width="16" alt="">';
} else if ($CFG->changepassword) {
$this->content->items[]='<a href="'.$CFG->changepassword.'">'.get_string('changepassword').'...</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" height="16" width="16" alt="">';
}
if ($CFG->allowunenroll) {
$this->content->items[]='<a href="unenrol.php?id='.$this->course->id.'">'.get_string('unenrolme', '', $this->course->shortname).'...</a>';
$this->content->