diff --git a/README.txt b/README.txt index 124ed5c..7c18675 100644 --- a/README.txt +++ b/README.txt @@ -18,6 +18,8 @@ Hence, use this block at your own risk. * Changes * 2022-03-19 - PHP 7+, 8+ compatibility fixes; Moodle 3.8+ compatibility fixes. +2017-11-17 - Make multiple instances available in MRBS +2017-11-17 - Make MRBS honor course roles: student <-> Viewer, editingteacher <-> Editor, manager <-> Admin 2014-06-09 - Fixed compatibility with M2.7 2014-06-09 - Confirmed compatibility with M2.6, fixed roomsearch with periods disabled, removal of deprecated 'ereg_replace' funcion (minor warning message). 2013-06-23 - New capability 'block/mrbs:ignoremaxadvancedays' - allows admin users to bypass the 'max_advance_days' restriction. @@ -63,6 +65,9 @@ Because of changes in the way that MRBS handles database calls (they switched in In April 2011, Davo Smith ( http://www.davodev.co.uk ), commissioned by Synergy Learning ( http://www.synergy-learning.com/ ) and Landesmedienzentrum Baden-Württemberg (http://www.lmz-bw.de/ ), updated this block to work with Moodle 2.0. This built upon some earlier work by Stephen Bourget, but included updating the block to make use of the Moodle database functionality (for increased security and compatibility with all Moodle supported databases), as well as a number of minor bug fixes, security improvements and general code-cleaning. +In November 2017, Frank Schütte ( http://www.gymnasium-himmelsthuer.de ), added code to host multiple instances of MRBS and the ability to +assign manager role locally to MRBS instance to administer it. + * MRBS Block - Installation * 1) Save the zip file somewhere onto your local computer and extract all the files @@ -78,7 +83,12 @@ In April 2011, Davo Smith ( http://www.davodev.co.uk ), commissioned by Synergy Details about making use of these capabilities is found at: http://docs.moodle.org/en/MRBS_block#Installation Three roles are automatically created during installation - 'mrbsviewer', 'mrbseditor' and 'mrbsadmin'. Assigning users these roles at the system level will allow them the appropriate level of access to the MRBS system. -6) The MRBS block is primarily intended for use on the Moodle front page (it works on other pages, but note that each instance of the block links to the same set of bookings - it is not possible to create independent sets of bookings by adding the block to multiple pages) +In the multiple instance code, there is a change to use moodle roles assigned locally to each instance of the MRBS. So course students are +capable of viewing MRBS, editing teachers are capable of editing MRBS, and manager (assigned locally to block) are able to administer +the MRBS. + +6) The MRBS block is now useful not only on the front page but also in course pages. Each instance of the block links to it's own set +of areas, rooms and bookings and can have it's own viewers, editors and administrators. * MRBS Block - unconfirmed capability * diff --git a/block_mrbs.php b/block_mrbs.php index 2c035ee..c76e4aa 100644 --- a/block_mrbs.php +++ b/block_mrbs.php @@ -14,7 +14,6 @@ // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . - class block_mrbs extends block_base { function init() { @@ -30,6 +29,20 @@ function applicable_formats() { return array('all' => true); } + function specialization() { + if($this->config == null) { + $this->config = get_config('block/mrbs'); + $this->config->title = get_string('newmrbsblock','block_mrbs'); + $this->config->linkname = get_string('accessmrbs','blockmrbs'); + $this->instance_config_commit(); + } + $this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newmrbsblock', 'block_mrbs')); + } + + function instance_allow_multiple() { + return true; + } + function get_content() { global $CFG, $OUTPUT; @@ -37,35 +50,63 @@ function get_content() { return $this->content; } - $cfg_mrbs = get_config('block/mrbs'); - - $context = context_system::instance(); + $context = context_block::instance($this->instance->id); if (has_capability('block/mrbs:viewmrbs', $context) or has_capability('block/mrbs:editmrbs', $context) or has_capability('block/mrbs:administermrbs', $context)) { - if (isset($CFG->block_mrbs_serverpath)) { - $serverpath = $CFG->block_mrbs_serverpath; + if (isset($this->instance->linkname)) { + $go = $this->instance->linkname; } else { - $serverpath = $CFG->wwwroot.'/blocks/mrbs/web'; + $go = get_string('accessmrbs', 'block_mrbs'); } - $go = get_string('accessmrbs', 'block_mrbs'); $icon = $OUTPUT->pix_icon('web', '', 'block_mrbs'); $target = ''; - if ($cfg_mrbs->newwindow) { + if (isset($this->config->newwindow) and $this->config->newwindow) { $target = ' target="_blank" '; } - $this->content = new stdClass(); - $this->content->text = ''.$icon.'  '.$go.''; + if (isset($this->config->serverpath) && $this->config->serverpath != "") { + $serverpath = $this->config->serverpath; + } else { + $serverpath = $CFG->wwwroot.'/blocks/mrbs/web'; + } + if (isset($this->instance->id)) { + $instance = $this->instance->id; + } else { + throw new \coding_error("Instance id must be set
\n"); + } + $this->content = new stdClass; + $this->content->text = ''.$icon.'  '.$go.''; $this->content->footer = ''; return $this->content; } return null; } - - function cron() { - global $CFG; - include($CFG->dirroot.'/blocks/mrbs/import.php'); - - return true; + + function instance_config_save($data, $nolongerused = false) { + // modify type of select fields to be int + $data->newwindow = intval($data->newwindow); + $data->enable_periods = intval($data->enable_periods); + if ($data->enable_periods == 0) { + $data->morningstarts = intval($data->morningstarts); + $data->morningstarts_min = intval($data->morningstarts_min); + $data->eveningends = intval($data->eveningends); + $data->eveningends_min = intval($data->eveningends_min); + } + $data->weekstarts = intval($data->weekstarts); + $data->dateformat = intval($data->dateformat); + $data->timeformat = intval($data->timeformat); + $data->view_week_number = intval($data->view_week_number); + $data->times_right_side = intval($data->times_right_side); + $data->javascript_cursor = intval($data->javascript_cursor); + $data->show_plus_link = intval($data->show_plus_link); + $data->mail_admin_on_bookings = intval($data->mail_admin_on_bookings); + $data->mail_area_admin_on_bookings = intval($data->mail_area_admin_on_bookings); + $data->mail_room_admin_on_bookings = intval($data->mail_room_admin_on_bookings); + $data->mail_admin_on_delete = intval($data->mail_admin_on_delete); + $data->mail_admin_all = intval($data->mail_admin_all); + $data->mail_details = intval($data->mail_details); + $data->mail_booker = intval($data->mail_booker); + parent::instance_config_save($data, $nolongerused); } + } diff --git a/classes/event/booking_created.php b/classes/event/booking_created.php index 855ddd3..9631202 100644 --- a/classes/event/booking_created.php +++ b/classes/event/booking_created.php @@ -63,7 +63,7 @@ public static function get_name() { * @return string */ public function get_description() { - return "The user with id '$this->userid' has updated a new booking in '{$this->other['room']}' for '{$this->other['name']}'"; + return "The user with id '$this->userid' has updated a new booking for '{$this->other['instance']}' in '{$this->other['room']}' for '{$this->other['name']}'"; } /** @@ -72,7 +72,7 @@ public function get_description() { * @return \moodle_url */ public function get_url() { - return new \moodle_url('/blocks/mrbs/web/view_entry.php', array('id' => $this->objectid)); + return new \moodle_url('/blocks/mrbs/web/view_entry.php', array('instance' => $this->other['instance'], 'id' => $this->objectid)); } /** @@ -82,11 +82,14 @@ public function get_url() { */ protected function get_legacy_logdata() { global $CFG; - return array(SITEID, 'mrbs', 'add booking', $CFG->wwwroot.'blocks/mrbs/web/view_entry.php?id='.$this->objectid, + return array(SITEID, 'mrbs', 'add booking', $CFG->wwwroot.'blocks/mrbs/web/view_entry.php?instance='.$this->other['instance'].'&id='.$this->objectid, $this->other['name']); } protected function validate_data() { + if (!isset($this->other['instance'])) { + throw new \coding_exception('Must specify the instance of the booking as \'other[\'instance\']\''); + } if (!isset($this->other['name'])) { throw new \coding_exception('Must specify the name of the booking as \'other[\'name\']\''); } diff --git a/classes/event/booking_updated.php b/classes/event/booking_updated.php index 6298e6a..a5afc8c 100644 --- a/classes/event/booking_updated.php +++ b/classes/event/booking_updated.php @@ -63,7 +63,7 @@ public static function get_name() { * @return string */ public function get_description() { - return "The user with id '$this->userid' has updated a booking in '{$this->other['room']}' for '{$this->other['name']}'"; + return "The user with id '$this->userid' has updated a booking for '{$this->other['instance']}'in '{$this->other['room']}' for '{$this->other['name']}'"; } /** @@ -72,7 +72,7 @@ public function get_description() { * @return \moodle_url */ public function get_url() { - return new \moodle_url('/blocks/mrbs/web/view_entry.php', array('id' => $this->objectid)); + return new \moodle_url('/blocks/mrbs/web/view_entry.php', array('instance' => $this->other['instance'], 'id' => $this->objectid)); } /** @@ -82,11 +82,14 @@ public function get_url() { */ protected function get_legacy_logdata() { global $CFG; - return array(SITEID, 'mrbs', 'edit booking', $CFG->wwwroot.'blocks/mrbs/web/view_entry.php?id='.$this->objectid, + return array(SITEID, 'mrbs', 'edit booking', $CFG->wwwroot.'blocks/mrbs/web/view_entry.php?instance='.$this->other['instance'].'&id='.$this->objectid, $this->other['name']); } protected function validate_data() { + if (!isset($this->other['instance'])) { + throw new \coding_exception('Must specify the instance of the booking as \'other[\'instance\']\''); + } if (!isset($this->other['name'])) { throw new \coding_exception('Must specify the name of the booking as \'other[\'name\']\''); } diff --git a/classes/task/mrbs_import.php b/classes/task/mrbs_import.php new file mode 100644 index 0000000..b015919 --- /dev/null +++ b/classes/task/mrbs_import.php @@ -0,0 +1,38 @@ +. + +/** + * The block_mrbs booking created event. + * + * @package block_mrbs + * @copyright 2014 Davo Smith + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace block_mrbs\task; + +class mrbs_import extends \core\task\scheduled_task { + public function get_name() { + return get_string('mrbs:import', 'block_mrbs'); + } + + public function execute() { + global $CFG; + include($CFG->dirroot.'/blocks/mrbs/import.php'); + + return true; + } +} diff --git a/db/access.php b/db/access.php index bf30907..a99d5bd 100644 --- a/db/access.php +++ b/db/access.php @@ -48,6 +48,19 @@ ) ), + 'block/mrbs:viewmrbs' => array( + + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + 'student' => CAP_ALLOW, + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ) + ), + 'block/mrbs:editmrbs' => array( 'captype' => 'write', @@ -60,6 +73,18 @@ ) ), + 'block/mrbs:editmrbs' => array( + + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ) + ), + 'block/mrbs:administermrbs' => array( 'captype' => 'write', @@ -69,6 +94,15 @@ ) ), + 'block/mrbs:administermrbs' => array( + + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + 'manager' => CAP_ALLOW + ) + ), + 'block/mrbs:viewalltt' => array( 'captype' => 'view', @@ -81,6 +115,18 @@ ) ), + 'block/mrbs:viewalltt' => array( + + 'captype' => 'view', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ) + ), + 'block/mrbs:forcebook' => array( 'captype' => 'write', @@ -90,6 +136,15 @@ ) ), + 'block/mrbs:forcebook' => array( + + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + 'manager' => CAP_ALLOW + ) + ), + 'block/mrbs:doublebook' => array( 'captype' => 'write', @@ -99,6 +154,15 @@ ) ), + 'block/mrbs:doublebook' => array( + + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + 'manager' => CAP_ALLOW + ) + ), + // Limits users to only creating 'unconfirmed' bookings // (unless they are the room administrator) 'block/mrbs:editmrbsunconfirmed' => array( @@ -108,6 +172,13 @@ 'archetypes' => array() ), + 'block/mrbs:editmrbsunconfirmed' => array( + + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array() + ), + 'block/mrbs:myaddinstance' => array( 'captype' => 'write', 'contextlevel' => CONTEXT_SYSTEM, @@ -115,6 +186,13 @@ ), ), + 'block/mrbs:myaddinstance' => array( + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + ), + ), + 'block/mrbs:addinstance' => array( 'riskbitmask' => RISK_SPAM | RISK_XSS, @@ -122,6 +200,7 @@ 'contextlevel' => CONTEXT_BLOCK, 'archetypes' => array( 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, 'manager' => CAP_ALLOW ), @@ -136,5 +215,12 @@ ), 'clonepermissionsfrom' => 'block/mrbs:administermrbs', ), + 'block/mrbs:ignoremaxadvancedays' => array( + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + 'manager' => CAP_ALLOW + ), + 'clonepermissionsfrom' => 'block/mrbs:administermrbs', + ), ); - diff --git a/db/install.php b/db/install.php index 412e265..b666873 100644 --- a/db/install.php +++ b/db/install.php @@ -53,6 +53,20 @@ function xmldb_block_mrbs_install() { assign_capability('block/mrbs:doublebook', CAP_ALLOW, $mrbsadminid, $context->id, true); } + // Add context level block to manager, student, editingteacher archetype + $roles = array('student', 'editingteacher','manager'); + foreach($roles as $role) { + $roleid = $DB->get_field('role', 'id', array('shortname' => $role), MUST_EXIST); + if(! $roleid) { + throw new \coding_exception("The \'".$role."\' role must exist
\n"); + } + $levels = get_role_contextlevels($roleid); + if(! in_array(CONTEXT_BLOCK, $levels)) { + array_push($levels, CONTEXT_BLOCK); + set_role_contextlevels($roleid, $levels); + } + } + // Clear any capability caches $context->mark_dirty(); } diff --git a/db/install.xml b/db/install.xml index 02b2df1..609cbf5 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -7,6 +7,7 @@ + @@ -17,6 +18,7 @@
+ @@ -36,6 +38,7 @@
+ @@ -56,6 +59,7 @@
+ diff --git a/db/tasks.php b/db/tasks.php new file mode 100644 index 0000000..5576d7a --- /dev/null +++ b/db/tasks.php @@ -0,0 +1,11 @@ +$tasks = array( + array( + 'classname' => 'block_mrbs\task\mrbs_import', + 'blocking' => 0, + 'minute' => '*/15', + 'hour' => '*', + 'day' => '*', + 'dayofweek' => '*', + 'month' => '*' + ) +); diff --git a/db/upgrade.php b/db/upgrade.php index 123e866..f6d37f3 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -194,5 +194,95 @@ function xmldb_block_mrbs_upgrade($oldversion=0) { upgrade_block_savepoint(true, 2016101700, 'mrbs'); } + if ($oldversion < 2017112601) { + // Create system context instance from global config and move data to that instance. + echo "Moving first instance to block in system context on site-index page
\n"; + + // Add this block to the blocks on site-index. + $blockname = 'mrbs'; + $page = new moodle_page(); + $page->set_context(context_system::instance()); + + // Check to see if this block is already on the site-index page. + $criteria = array( + 'blockname' => $blockname, + 'pagetypepattern' => 'site-index', + ); + + if (!$DB->record_exists('block_instances', $criteria)) { + // Add the block to site-index. + $page->blocks->add_region(BLOCK_POS_LEFT); + $page->blocks->add_block($blockname, BLOCK_POS_LEFT, -8, false, 'site-index'); + + // Move configuration from global config to instance + $cfg_mrbs = get_config('block/mrbs'); + $cfg_mrbs->name = 'Imported MRBS'; + $tmp = $DB->get_record('block_instances', $criteria, '*', MUST_EXIST); + if(!isset($tmp)) { + throw new \coding_exception('block_instance \'block_mrbs\' with id '.$instance_id.' must exist on \'site-index\'.'); + } + $instance_id = $tmp->id; + set_config('default_instance', $instance_id, 'block/mrbs'); + $tmp->configdata = base64_encode(serialize($cfg_mrbs)); + $DB->update_record('block_instances', $tmp); + } else { + $instance_id = $DB->get_field('block_instances', 'id', $criteria, MUST_EXIST); + } + if(!isset($instance_id)) { + throw new \coding_exception('block instance \'block_mrbs\' with id '.$instance_id.' must exist on \'site-index\''); + } + + // Add instance field to each table. + echo "Adding instance field to each mrbs table
\n"; + + $table = new xmldb_table('block_mrbs_area'); + $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, $instance_id, 'id'); + // Conditionally launch add field instance + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + echo "Added instance field to mrbs table block_mrbs_area
\n"; + } + $table = new xmldb_table('block_mrbs_entry'); + $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, $instance_id, 'id'); + // Conditionally launch add field instance + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + echo "Added instance field to mrbs table block_mrbs_entry
\n"; + } + $table = new xmldb_table('block_mrbs_repeat'); + $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, $instance_id, 'id'); + // Conditionally launch add field instance + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + echo "Added instance field to mrbs table block_mrbs_repeat
\n"; + } + $table = new xmldb_table('block_mrbs_room'); + $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, $instance_id, 'id'); + // Conditionally launch add field instance + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + echo "Added instance field to mrbs table block_mrbs_room
\n"; + } + // Add context level block to manager, student, editingteacher archetype + $roles = array('student', 'editingteacher','manager'); + foreach($roles as $role) { + $roleid = $DB->get_field('role', 'id', array('shortname' => $role), MUST_EXIST); + if(! $roleid) { + throw new \coding_exception("The \'".$role."\' role must exist
\n"); + } + $levels = get_role_contextlevels($roleid); + if(! in_array(CONTEXT_BLOCK, $levels)) { + array_push($levels, CONTEXT_BLOCK); + set_role_contextlevels($roleid, $levels); + } + } + echo "Added context level block to manager, student, editingteacher roles.
\n
\n"; + + echo "Imported global mrbs to instance \'Imported MRBS\' with id '.$instance_id.'
\n"; + echo "Please add \'?instance='.$instance_id.'\' to point your links to this instance.
\n"; + + // mrbs savepoint reached + upgrade_block_savepoint(true, 2017112601, 'mrbs'); + } return true; } diff --git a/edit_form.php b/edit_form.php new file mode 100644 index 0000000..4390f13 --- /dev/null +++ b/edit_form.php @@ -0,0 +1,304 @@ +. + +/** + * Form for editing MRBS block instances. + * + * @package block_mrbs + * @copyright 2017 Frank Schütte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +class block_mrbs_edit_form extends block_edit_form { + protected function specific_definition($mform) { + global $CFG; + + // Fields for editing MRBS block title and contents. + $mform->addElement('header', 'configheader', get_string('blocksettings', 'block')); + + $mform->addElement('text', 'config_title', get_string('configtitle', 'block_html')); + $mform->setType('config_title', PARAM_TEXT); + + $mform->addElement('text', 'config_linkname', get_string('config_linkname', 'block_html')); + $mform->addElement('static', '', '', get_string('config_linkname2', 'block_mrbs')); + $mform->setType('config_linkname', PARAM_TEXT); + $mform->setDefault('config_linkname', get_string('accessmrbs', 'block_mrbs')); + + $options = array(0 => get_string('pagewindow', 'block_mrbs'), 1 => get_string('newwindow', 'block_mrbs')); + $mform->addElement('select', 'config_newwindow', get_string('config_new_window', 'block_mrbs'), $options); + $mform->setDefault('config_newwindow',1); + $mform->addElement('static', '', '', get_string('config_new_window2', 'block_mrbs')); + + $mform->addElement('text', 'config_serverpath', get_string('serverpath', 'block_mrbs')); + $mform->setType('config_serverpath', PARAM_URL); + $mform->setDefault('config_serverpath',$CFG->wwwroot.'/blocks/mrbs/web'); + $mform->addElement('static', '', '', get_string('adminview','block_mrbs')); + + $mform->addElement('text', 'config_admin', get_string('config_admin', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_admin2', 'block_mrbs')); + $mform->setDefault('config_admin', $CFG->supportname); + $mform->setType('config_admin', PARAM_TEXT); + + $mform->addElement('text', 'config_admin_email', get_string('config_admin_email', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_admin_email2', 'block_mrbs')); + $mform->setDefault('config_admin_email', $CFG->supportemail); + $mform->setType('config_admin_email', PARAM_TEXT); + + $options = array(0 => get_string('no'), 1 => get_string('yes')); + $mform->addElement('selectyesno', 'config_enable_periods', get_string('config_enable_periods', 'block_mrbs')); + $mform->addElement('static','','',get_string('config_enable_periods2','block_mrbs')); + $mform->setDefault('config_enable_periods', 1); + + // Resolution + unset($options); + $strunits = get_string('resolution_units', 'block_mrbs'); + $options = array( + '900' => '15'.$strunits, '1800' => '30'.$strunits, '2700' => '45'.$strunits, '3600' => '60'.$strunits, + '4500' => '75'.$strunits, '5400' => '90'.$strunits, '6300' => '105'.$strunits, '7200' => '120'.$strunits + ); + $mform->addElement('select', 'config_resolution', get_string('config_resolution', 'block_mrbs'), $options); + $mform->addElement('static', '', '', get_string('config_resolution2', 'block_mrbs')); + $mform->setDefault('config_resolution', '1800'); + $mform->disabledIf('config_resolution', 'config_enable_periods', 'eq', '1'); + // Start Time (Hours) + unset($options); + $options = array( + 1 => '01', 2 => '02', 3 => '03', 4 => '04', 5 => '05', 6 => '06', 7 => '07', 8 => '08', 9 => '09', 10 => '10', + 11 => '11', 12 => '12', 13 => '13', 14 => '14', 15 => '15', 16 => '16', 17 => '17', 18 => '18', 19 => '19', 20 => '20', + 21 => '21', 22 => '22', 23 => '23' + ); + $mform->addElement('select', 'config_morningstarts', get_string('config_morningstarts', 'block_mrbs'), $options); + $mform->addElement('static', '', '', get_string('config_morningstarts2', 'block_mrbs')); + $mform->setDefault('config_morningstarts', 7); + $mform->disabledIf('config_morningstarts', 'config_enable_periods', 'eq', '1'); + // Start Time (Min) + unset($options); + $options = array( + 0 => '00', 5 => '05', 10 => '10', 15 => '15', 20 => '20', 25 => '25', 30 => '30', 35 => '35', 40 => '40', 45 => '45', + 50 => '50', 55 => '55' + ); + $mform->addElement('select', 'config_morningstarts_min', get_string('config_morningstarts_min', 'block_mrbs'), $options); + $mform->addElement('static', '', '', get_string('config_morningstarts_min2', 'block_mrbs')); + $mform->setDefault('config_morningstarts_min', 0); + $mform->disabledIf('config_morningstarts_min', 'config_enable_periods', 'eq', '1'); + // End Time (Hours) + unset($options); + $options = array( + 1 => '01', 2 => '02', 3 => '03', 4 => '04', 5 => '05', 6 => '06', 7 => '07', 8 => '08', 9 => '09', 10 => '10', + 11 => '11', 12 => '12', 13 => '13', 14 => '14', 15 => '15', 16 => '16', 17 => '17', 18 => '18', 19 => '19', 20 => '20', + 21 => '21', 22 => '22', 23 => '23' + ); + $mform->addElement('select', 'config_eveningends', get_string('config_eveningends', 'block_mrbs'), $options); + $mform->addElement('static', '', '', get_string('config_eveningends2', 'block_mrbs')); + $mform->setDefault('config_eveningends', 19); + $mform->disabledIf('config_eveningends', 'config_enable_periods', 'eq', '1'); + // End Time Time (Min) + unset($options); + $options = array( + 0 => '00', 5 => '05', 10 => '10', 15 => '15', 20 => '20', 25 => '25', 30 => '30', 35 => '35', 40 => '40', 45 => '45', + 50 => '50', 55 => '55' + ); + $mform->addElement('select', 'config_eveningends_min', get_string('config_eveningends_min', 'block_mrbs'), $options); + $mform->addElement('static', '', '', get_string('config_eveningends_min2', 'block_mrbs')); + $mform->setDefault('config_eveningends_min', 0); + $mform->disabledIf('config_eveningends_min', 'config_enable_periods', 'eq', '1'); + //Use Custom Periods + $mform->addElement('textarea', 'config_periods', get_string('config_periods', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_periods2', 'block_mrbs')); + $mform->setType('config_periods', PARAM_TEXT); + $mform->disabledIf('config_periods', 'config_enable_periods', 'neq', '1'); + + // Date Information + //Start of Week + unset($options); + $options = array( + 0 => get_string('sunday', 'calendar'), 1 => get_string('monday', 'calendar'), 2 => get_string('tuesday', 'calendar'), + 3 => get_string('wednesday', 'calendar'), 4 => get_string('thursday', 'calendar'), 5 => get_string('friday', 'calendar'), + 6 => get_string('saturday', 'calendar') + ); + $mform->addElement('select', 'config_weekstarts', get_string('config_weekstarts', 'block_mrbs'), $options); + $mform->setDefault('config_weekstarts', 0); + $mform->addElement('static', '', '', get_string('config_weekstarts2', 'block_mrbs')); + + //Length of week + $mform->addElement('text', 'config_weeklength', get_string('config_weeklength', 'block_mrbs')); + $mform->setDefault('config_weeklength', 7); + $mform->setType('config_weeklength', PARAM_INT); + $mform->addElement('static', '', '', get_string('config_weeklength2', 'block_mrbs')); + + //Date Format + unset($options); + $options = array(0 => get_string('config_date_mmddyy', 'block_mrbs'), 1 => get_string('config_date_ddmmyy', 'block_mrbs')); + $mform->addElement('select', 'config_dateformat', get_string('config_dateformat', 'block_mrbs'), $options); + $mform->setDefault('config_dateformat', 0); + $mform->addElement('static', '', '', get_string('config_dateformat2', 'block_mrbs')); + + //Time format + unset($options); + $options = array(0 => get_string('timeformat_12', 'calendar'), 1 => get_string('timeformat_24', 'calendar')); + $mform->addElement('select', 'config_timeformat', get_string('config_timeformat', 'block_mrbs'), $options); + $mform->setDefault('config_timeformat', 1); + $mform->addElement('static', '', '', get_string('config_timeformat2', 'block_mrbs')); + + // Misc Settings + $mform->addElement('text', 'config_max_rep_entrys', get_string('config_max_rep_entrys', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_max_rep_entrys2', 'block_mrbs')); + $mform->setDefault('config_max_rep_entrys', 365); + $mform->setType('config_max_rep_entrys', PARAM_INT); + + $mform->addElement('text', 'config_max_advance_days', get_string('config_max_advance_days', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_max_advance_days2', 'block_mrbs')); + $mform->setDefault('config_max_advance_days', -1); + $mform->setType('config_max_advance_days', PARAM_INT); + + $mform->addElement('text', 'config_default_report_days', get_string('config_default_report_days', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_default_report_days2', 'block_mrbs')); + $mform->setDefault('config_default_report_days', 60); + $mform->setType('config_default_report_days', PARAM_INT); + + $mform->addElement('text', 'config_search_count', get_string('config_search_count', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_search_count2', 'block_mrbs')); + $mform->setDefault('config_search_count', 20); + $mform->setType('config_search_count', PARAM_INT); + /* + $mform->addElement('text', 'config_refresh_rate', get_string('config_refresh_rate', 'block_mrbs'), get_string('config_refresh_rate2', 'block_mrbs'), 0, PARAM_INT); + + */ + + $options = array('list' => get_string('list'), 'select' => get_string('select')); + $mform->addElement('select', 'config_area_list_format', get_string('config_area_list_format', 'block_mrbs'), $options); + $mform->setDefault('config_area_list_format', 'list'); + $mform->addElement('static', '', '', get_string('config_area_list_format2', 'block_mrbs')); + + $options = array( + 'both' => get_string('both', 'block_mrbs'), 'description' => get_string('description'), + 'slot' => get_string('slot', 'block_mrbs') + ); + $mform->addElement('select', 'config_monthly_view_entries_details', get_string('config_monthly_view_entries_details', 'block_mrbs'), $options); + $mform->setDefault('config_monthly_view_entries_details', 'both'); + $mform->addElement('static', '', '', get_string('config_monthly_view_entries_details2', 'block_mrbs')); + + $mform->addElement('selectyesno', 'config_view_week_number', get_string('config_view_week_number', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_view_week_number2', 'block_mrbs')); + + $mform->addElement('selectyesno', 'config_times_right_side', get_string('config_times_right_side', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_times_right_side2', 'block_mrbs')); + + $mform->addElement('selectyesno', 'config_javascript_cursor', get_string('config_javascript_cursor', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_javascript_cursor2', 'block_mrbs')); + $mform->setDefault('config_javascript_cursor', 1); + + $mform->addElement('selectyesno', 'config_show_plus_link', get_string('config_show_plus_link', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_show_plus_link2', 'block_mrbs')); + $mform->setDefault('config_show_plus_link', 1); + + $options = array( + 'bgcolor' => get_string('bgcolor', 'block_mrbs'), 'class' => get_string('class', 'block_mrbs'), + 'hybrid' => get_string('hybrid', 'block_mrbs') + ); + $mform->addElement('select', 'config_highlight_method', get_string('config_highlight_method', 'block_mrbs'), $options); + $mform->setDefault('config_highlight_method', 'hybrid'); + $mform->addElement('static', '', '', get_string('config_highlight_method2', 'block_mrbs')); + + $options = array('day' => get_string('day'), 'month' => get_string('month', 'block_mrbs'), 'week' => get_string('week')); + $mform->addElement('select', 'config_default_view', get_string('config_default_view', 'block_mrbs'), $options); + $mform->setDefault('config_default_view', 'day'); + $mform->addElement('static', '', '', get_string('config_default_view2', 'block_mrbs')); + + $mform->addElement('text', 'config_default_room', get_string('config_default_room', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_default_room2', 'block_mrbs')); + $mform->setType('config_default_room', PARAM_INT); + + // should this be the same as the Moodle Site cookie path? + // $mform->addElement('text', 'cookie_path_override', get_string('config_cookie_path_override', 'block_mrbs'), get_string('config_cookie_path_override2', 'block_mrbs'), '', PARAM_LOCALURL); + // + /* + //select + $options = array('' => get_string('', 'block_mrbs'), '' => get_string('', 'block_mrbs')); + $mform->addElement('advcheckbox', '', get_string('config_', 'block_mrbs'), get_string('config_2', 'block_mrbs'), '', $options); + //text or int + $mform->addElement('text', '', get_string('config_', 'block_mrbs'), get_string('config_2', 'block_mrbs'), 0, PARAM_INT); + */ + + $mform->addElement('text', 'config_entry_type_a', get_string('config_entry_type', 'block_mrbs', 'A')); + $mform->addElement('static', '', '', get_string('config_entry_type2', 'block_mrbs', 'A')); + $mform->setType('config_entry_type_a', PARAM_TEXT); + $mform->addElement('text', 'config_entry_type_b', get_string('config_entry_type', 'block_mrbs', 'B')); + $mform->addElement('static', '', '', get_string('config_entry_type2', 'block_mrbs', 'B')); + $mform->setType('config_entry_type_b', PARAM_TEXT); + $mform->addElement('text', 'config_entry_type_c', get_string('config_entry_type', 'block_mrbs', 'C')); + $mform->addElement('static', '', '', get_string('config_entry_type2', 'block_mrbs', 'C')); + $mform->setType('config_entry_type_c', PARAM_TEXT); + $mform->addElement('text', 'config_entry_type_d', get_string('config_entry_type', 'block_mrbs', 'D')); + $mform->addElement('static', '', '', get_string('config_entry_type2', 'block_mrbs', 'D')); + $mform->setType('config_entry_type_d', PARAM_TEXT); + $mform->addElement('text', 'config_entry_type_e', get_string('config_entry_type', 'block_mrbs', 'E')); + $mform->addElement('static', '', '', get_string('config_entry_type2', 'block_mrbs', 'E')); + $mform->setType('config_entry_type_e', PARAM_TEXT); + $mform->addElement('text', 'config_entry_type_f', get_string('config_entry_type', 'block_mrbs', 'F')); + $mform->addElement('static', '', '', get_string('config_entry_type2', 'block_mrbs', 'F')); + $mform->setType('config_entry_type_f', PARAM_TEXT); + $mform->addElement('text', 'config_entry_type_g', get_string('config_entry_type', 'block_mrbs', 'G')); + $mform->addElement('static', '', '', get_string('config_entry_type2', 'block_mrbs', 'G')); + $mform->setType('config_entry_type_g', PARAM_TEXT); + $mform->addElement('text', 'config_entry_type_h', get_string('config_entry_type', 'block_mrbs', 'H')); + $mform->addElement('static', '', '', get_string('config_entry_type2', 'block_mrbs', 'H')); + $mform->setType('config_entry_type_h', PARAM_TEXT); + $mform->addElement('text', 'config_entry_type_i', get_string('config_entry_type', 'block_mrbs', 'I')); + $mform->addElement('static', '', '', get_string('config_entry_type2', 'block_mrbs', 'I')); + $mform->setType('config_entry_type_i', PARAM_TEXT); + $mform->addElement('text', 'config_entry_type_j', get_string('config_entry_type', 'block_mrbs', 'J')); + $mform->addElement('static', '', '', get_string('config_entry_type2', 'block_mrbs', 'J')); + $mform->setType('config_entry_type_j', PARAM_TEXT); + + $mform->addElement('selectyesno', 'config_mail_admin_on_bookings', get_string('config_mail_admin_on_bookings', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_mail_admin_on_bookings2', 'block_mrbs')); + + $mform->addElement('selectyesno', 'config_mail_area_admin_on_bookings', get_string('config_mail_area_admin_on_bookings', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_mail_area_admin_on_bookings2', 'block_mrbs')); + + $mform->addElement('selectyesno', 'config_mail_room_admin_on_bookings', get_string('config_mail_room_admin_on_bookings', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_mail_room_admin_on_bookings2', 'block_mrbs')); + + $mform->addElement('selectyesno', 'config_mail_admin_on_delete', get_string('config_mail_admin_on_delete', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_mail_admin_on_delete2', 'block_mrbs')); + + $mform->addElement('selectyesno', 'config_mail_admin_all', get_string('config_mail_admin_all', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_mail_admin_all2', 'block_mrbs')); + + $mform->addElement('selectyesno', 'config_mail_details', get_string('config_mail_details', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_mail_details2', 'block_mrbs')); + + $mform->addElement('selectyesno', 'config_mail_booker', get_string('config_mail_booker', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_mail_booker2', 'block_mrbs')); + + $mform->addElement('text', 'config_mail_from', get_string('config_mail_from', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_mail_from2', 'block_mrbs')); + $mform->setDefault('config_mail_from', $CFG->supportemail); + $mform->setType('config_mail_from', PARAM_TEXT); + $mform->addElement('text', 'config_mail_recipients', get_string('config_mail_recipients', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_mail_recipients2', 'block_mrbs')); + $mform->setDefault('config_mail_recipients', $CFG->supportemail); + $mform->setType('config_mail_recipients', PARAM_TEXT); + $mform->addElement('text', 'config_mail_cc', get_string('config_mail_cc', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('config_mail_cc2', 'block_mrbs')); + $mform->setType('config_mail_cc', PARAM_TEXT); + $mform->addElement('text', 'config_cronfile', get_string('cronfile', 'block_mrbs')); + $mform->addElement('static', '', '', get_string('cronfiledesc', 'block_mrbs')); + $mform->setType('config_cronfile', PARAM_TEXT); + } + +} diff --git a/import.php b/import.php index 7a3c4ae..7dcfceb 100644 --- a/import.php +++ b/import.php @@ -21,7 +21,6 @@ * regularly, it will replace any non-edited imported bookings with a new copy but * not any that have been edited. * - * It is included by the blocks cron() function each time it runs */ //TODO:maybe set it up like tutorlink etc so that it can take uploaded files directly? @@ -32,147 +31,153 @@ //record time for time taken stat $script_start_time = time(); -$cfg_mrbs = get_config('block/mrbs'); //get Moodle config settings for the MRBS block -if (!isset($cfg_mrbs->periods) or empty($cfg_mrbs->periods)) { - $cfg_mrbs->periods = array(); - $cfg_mrbs->periods[] = "Period 1"; - $cfg_mrbs->periods[] = "Period 2"; - $cfg_mrbs->periods[] = "Period 3"; - $cfg_mrbs->periods[] = "Period 4"; - $cfg_mrbs->periods[] = "Period 5"; - $cfg_mrbs->periods[] = "Period 6"; - $cfg_mrbs->periods[] = "Period 7"; - $cfg_mrbs->periods[] = "Period 8"; - $cfg_mrbs->periods[] = "Period 9"; - $cfg_mrbs->periods[] = "Period 10"; - $cfg_mrbs->periods[] = "Period 11"; - $cfg_mrbs->periods[] = "Period 12"; -} else { - $pds = explode("\n", $cfg_mrbs->periods); - $cfg_mrbs->periods = array(); - foreach ($pds as $pd) { - $pd = trim($pd); - $cfg_mrbs->periods[] = $pd; - } -} -$output = ''; -if (!empty($cfg_mrbs->cronfile) && file_exists($cfg_mrbs->cronfile)) { - if ($mrbs_sessions = fopen($cfg_mrbs->cronfile, 'r')) { - $output .= get_string('startedimport', 'block_mrbs')."\n"; - $now = time(); - $DB->set_field_select('block_mrbs_entry', 'type', 'M', 'type=\'K\' and start_time > ?', array($now)); // Change old imported (type K) records to temporary type M - while ($array = fgetcsv($mrbs_sessions)) { //import timetable into mrbs - $csvrow = new stdClass(); - $csvrow->start_time = clean_param($array[0], PARAM_TEXT); - $csvrow->end_time = clean_param($array[1], PARAM_TEXT); - $csvrow->first_date = clean_param($array[2], PARAM_TEXT); - $csvrow->weekpattern = clean_param($array[3], PARAM_TEXT); - $csvrow->room_name = clean_param($array[4], PARAM_TEXT); - $csvrow->username = clean_param($array[5], PARAM_TEXT); - $csvrow->name = clean_param($array[6], PARAM_TEXT); - $csvrow->description = clean_param($array[7], PARAM_TEXT); - - list($year, $month, $day) = explode('/', $csvrow->first_date); - $date = mktime(00, 00, 00, $month, $day, $year); - $room = room_id_lookup($csvrow->room_name); - $weeks = str_split($csvrow->weekpattern); - foreach ($weeks as $week) { - if (($week == 1) and ($date > $now)) { - $start_time = time_to_datetime($date, $csvrow->start_time); - $end_time = time_to_datetime($date, $csvrow->end_time); - if (!is_timetabled($csvrow->name, $start_time)) { ////only timetable class if it isn't already timetabled elsewhere (class been moved) - $entry = new stdClass(); - $entry->start_time = $start_time; - $entry->end_time = $end_time; - $entry->room_id = $room; - $entry->timestamp = $now; - $entry->create_by = $csvrow->username; - $entry->name = $csvrow->name; - $entry->type = 'K'; - $entry->description = $csvrow->description; - $newentryid = $DB->insert_record('block_mrbs_entry', $entry); - - //If there is another non-imported booking there, send emails. It is assumed that simultanious imported classes are intentional - $sql = "SELECT * - FROM {block_mrbs_entry} AS e - WHERE - ((e.start_time < ? AND e.end_time > ?) - OR (e.start_time < ? AND e.end_time > ?) - OR (e.start_time >= ? AND e.end_time <= ? )) - AND e.room_id = ? AND type <>'K'"; - - //limit to 1 to keep this simpler- if there is a 3-way clash it will be noticed by one of the 2 teachers notified - if ($existingclass = $DB->get_record_sql($sql, array( - $start_time, $start_time, $end_time, - $end_time, $start_time, $end_time, $room - )) - ) { - $hr_start_time = date("j F, Y", $start_time).", ".to_hr_time($start_time); - $a = new stdClass(); - $a->oldbooking = $existingclass->description.'('.$existingclass->id.')'; - $a->newbooking = $csvrow->description.'('.$newentryid.')'; - $a->time = $hr_start_time; - $a->room = $csvrow->room_name; - $a->admin = $cfg_mrbs->admin.' ('.$cfg_mrbs->admin_email.')'; - $output .= get_string('clash', 'block_mrbs', $a); - - $existingteacher = $DB->get_record('user', array('username' => $existingclass->create_by)); - $newteacher = $DB->get_record('user', array('username' => $csvrow->username)); - - $body = get_string('clashemailbody', 'block_mrbs', $a); - - if (email_to_user($existingteacher, $newteacher, get_string('clashemailsub', 'block_mrbs', $a), $body)) { - $output .= ', '.get_string('clashemailsent', 'block_mrbs').' '.$existingteacher->firstname.' '.$existingteacher->lastname.'<'.$existingteacher->email.'>'; - } else { - $output .= get_string('clashemailnotsent', 'block_mrbs').$existingclass->description.'('.$existingclass->id.')'; - } - if (email_to_user($newteacher, $existingteacher, get_string('clashemailsub', 'block_mrbs', $a), $body)) { - $output .= ', '.get_string('clashemailsent', 'block_mrbs').' '.$newteacher->firstname.' '.$newteacher->lastname.'<'.$newteacher->email.'>'; - } else { - $output .= get_string('clashemailnotsent', 'block_mrbs').$csvrow->description.'('.$newentryid.')'; - } - $output .= "\n"; - } - } - } - $date += 604800; - - //checks for being an hour out due to BST/GMT change and corrects - if (date('G', $date) == 01) { - $date = $date + 3600; - } - if (date('G', $date) == 23) { - $date = $date - 3600; - } - } - } +$instances = $DB->get_records('block_instances', array('blockname' => 'mrbs')); +foreach($instances as $instance){ + $instance_id = $instance->id; + $cfg_mrbs = unserialize(base64_decode($instance->configdata)); //get Moodle config settings for this instance of the MRBS block + if (!isset($cfg_mrbs->periods) or empty($cfg_mrbs->periods)) { + $cfg_mrbs->periods = array(); + $cfg_mrbs->periods[] = "Period 1"; + $cfg_mrbs->periods[] = "Period 2"; + $cfg_mrbs->periods[] = "Period 3"; + $cfg_mrbs->periods[] = "Period 4"; + $cfg_mrbs->periods[] = "Period 5"; + $cfg_mrbs->periods[] = "Period 6"; + $cfg_mrbs->periods[] = "Period 7"; + $cfg_mrbs->periods[] = "Period 8"; + $cfg_mrbs->periods[] = "Period 9"; + $cfg_mrbs->periods[] = "Period 10"; + $cfg_mrbs->periods[] = "Period 11"; + $cfg_mrbs->periods[] = "Period 12"; + } else { + $pds = explode("\n", $cfg_mrbs->periods); + $cfg_mrbs->periods = array(); + foreach ($pds as $pd) { + $pd = trim($pd); + $cfg_mrbs->periods[] = $pd; + } + } + $output = ''; + if (!empty($cfg_mrbs->cronfile) && file_exists($cfg_mrbs->cronfile)) { + if ($mrbs_sessions = fopen($cfg_mrbs->cronfile, 'r')) { + $output .= get_string('startedimport', 'block_mrbs')." for instance ".$instance_id."\n"; + $now = time(); + $DB->set_field_select('block_mrbs_entry', 'type', 'M', 'type=\'K\' and instance = ? and start_time > ?', array($instance_id, $now)); // Change old imported (type K) records to temporary type M + while ($array = fgetcsv($mrbs_sessions)) { //import timetable into mrbs + $csvrow = new stdClass(); + $csvrow->instance = $instance_id; + $csvrow->start_time = clean_param($array[0], PARAM_TEXT); + $csvrow->end_time = clean_param($array[1], PARAM_TEXT); + $csvrow->first_date = clean_param($array[2], PARAM_TEXT); + $csvrow->weekpattern = clean_param($array[3], PARAM_TEXT); + $csvrow->room_name = clean_param($array[4], PARAM_TEXT); + $csvrow->username = clean_param($array[5], PARAM_TEXT); + $csvrow->name = clean_param($array[6], PARAM_TEXT); + $csvrow->description = clean_param($array[7], PARAM_TEXT); - // any remaining type M records are no longer in the import file, so delete - $DB->delete_records_select('block_mrbs_entry', 'type=\'M\''); + list($year, $month, $day) = explode('/', $csvrow->first_date); + $date = mktime(00, 00, 00, $month, $day, $year); + $room = room_id_lookup($csvrow->instance, $csvrow->room_name); + $weeks = str_split($csvrow->weekpattern); + foreach ($weeks as $week) { + if (($week == 1) and ($date > $now)) { + $start_time = time_to_datetime($cfg_mrbs->enable_periods, $cfg_mrbs->periods, $date, $csvrow->start_time); + $end_time = time_to_datetime($cfg_mrbs->enable_periods, $cfg_mrbs->periods, $date, $csvrow->end_time); + if (!is_timetabled($csvrow->name, $start_time)) { ////only timetable class if it isn't already timetabled elsewhere (class been moved) + $entry = new stdClass(); + $entry->instance = $instance_id; + $entry->start_time = $start_time; + $entry->end_time = $end_time; + $entry->room_id = $room; + $entry->timestamp = $now; + $entry->create_by = $csvrow->username; + $entry->name = $csvrow->name; + $entry->type = 'K'; + $entry->description = $csvrow->description; + $newentryid = $DB->insert_record('block_mrbs_entry', $entry); - //move the processed file to prevent wasted time re-processing TODO: option for how long to keep these- I've found them useful for debugging but obviously can't keep them for ever - $date = date('Ymd'); - if (rename($cfg_mrbs->cronfile, $cfg_mrbs->cronfile.'.'.$date)) { - $output .= $cfg_mrbs->cronfile.get_string('movedto', 'block_mrbs').$cfg_mrbs->cronfile.'.'.$date."\n"; - } - $script_time_taken = time() - $script_start_time; - $output .= get_string('finishedimport', 'block_mrbs', $script_time_taken); + //If there is another non-imported booking there, send emails. It is assumed that simultanious imported classes are intentional + $sql = "SELECT * + FROM {block_mrbs_entry} AS e + WHERE + ((e.start_time < ? AND e.end_time > ?) + OR (e.start_time < ? AND e.end_time > ?) + OR (e.start_time >= ? AND e.end_time <= ? )) + AND e.room_id = ? AND e.instance = ? AND type <>'K'"; - echo $output; //will only show up if being run via apache + //limit to 1 to keep this simpler- if there is a 3-way clash it will be noticed by one of the 2 teachers notified + if ($existingclass = $DB->get_record_sql($sql, array( + $start_time, $start_time, $end_time, + $end_time, $start_time, $end_time, $room, $instance_id + )) + ) { + $hr_start_time = date("j F, Y", $start_time).", ".to_hr_time($cfg_mrbs->enable_periods, $cfg_mrbs->periods, $start_time); + $a = new stdClass(); + $a->oldbooking = $existingclass->description.'('.$existingclass->id.')'; + $a->newbooking = $csvrow->description.'('.$newentryid.')'; + $a->time = $hr_start_time; + $a->room = $csvrow->room_name; + $a->admin = $cfg_mrbs->admin.' ('.$cfg_mrbs->admin_email.')'; + $output .= get_string('clash', 'block_mrbs', $a); - //email output to admin - if ($mrbsadmin = $DB->get_record('user', array('email' => $cfg_mrbs->admin_email))) { - email_to_user($mrbsadmin, $mrbsadmin, get_string('importlog', 'block_mrbs'), $output); - } + $existingteacher = $DB->get_record('user', array('username' => $existingclass->create_by)); + $newteacher = $DB->get_record('user', array('username' => $csvrow->username)); + + $body = get_string('clashemailbody', 'block_mrbs', $a); + + if (email_to_user($existingteacher, $newteacher, get_string('clashemailsub', 'block_mrbs', $a), $body)) { + $output .= ', '.get_string('clashemailsent', 'block_mrbs').' '.$existingteacher->firstname.' '.$existingteacher->lastname.'<'.$existingteacher->email.'>'; + } else { + $output .= get_string('clashemailnotsent', 'block_mrbs').$existingclass->description.'('.$existingclass->id.')'; + } + if (email_to_user($newteacher, $existingteacher, get_string('clashemailsub', 'block_mrbs', $a), $body)) { + $output .= ', '.get_string('clashemailsent', 'block_mrbs').' '.$newteacher->firstname.' '.$newteacher->lastname.'<'.$newteacher->email.'>'; + } else { + $output .= get_string('clashemailnotsent', 'block_mrbs').$csvrow->description.'('.$newentryid.')'; + } + $output .= "\n"; + } + } + } + $date += 604800; + + //checks for being an hour out due to BST/GMT change and corrects + if (date('G', $date) == 01) { + $date = $date + 3600; + } + if (date('G', $date) == 23) { + $date = $date - 3600; + } + } + } + + // any remaining type M records are no longer in the import file, so delete + $DB->delete_records_select('block_mrbs_entry', 'type=\'M\' and instance=?', array($instance_id)); + + //move the processed file to prevent wasted time re-processing TODO: option for how long to keep these- I've found them useful for debugging but obviously can't keep them for ever + $date = date('Ymd'); + if (rename($cfg_mrbs->cronfile, $cfg_mrbs->cronfile.'.'.$date)) { + $output .= $cfg_mrbs->cronfile.get_string('movedto', 'block_mrbs').$cfg_mrbs->cronfile.'.'.$date."\n"; + } + $script_time_taken = time() - $script_start_time; + $output .= get_string('finishedimport', 'block_mrbs', $script_time_taken).' instance: '.$instance_id."\n"; + + echo $output; //will only show up if being run via apache + + //email output to admin + if ($mrbsadmin = $DB->get_record('user', array('email' => $cfg_mrbs->admin_email))) { + email_to_user($mrbsadmin, $mrbsadmin, get_string('importlog', 'block_mrbs'), $output); + } + } } } //==========================================FUNCTIONS============================================================== //looks up the room id from the name -function room_id_lookup($name) { +function room_id_lookup($instance_id, $name) { global $DB; - if (!$room = $DB->get_record('block_mrbs_room', array('room_name' => $name))) { - $error = "ERROR: failed to return id from database (room $name probably doesn't exist)"; + if (!$room = $DB->get_record('block_mrbs_room', array('instance' => $instance_id, 'room_name' => $name))) { + $error = "ERROR: failed to return id from database (room $name in instanced $instance_id probably doesn't exist)"; echo $error."\n"; return 'error'; } else { @@ -190,15 +195,16 @@ function room_id_lookup($name) { * @param $time int start time of the booking in unix timestamp format * @return bool does a previous booking exist? */ -function is_timetabled($name, $time) { +function is_timetabled($instance_id, $name, $time) { global $DB; - if ($DB->get_record('block_mrbs_entry', array('name' => $name, 'start_time' => $time, 'type' => 'L'))) { + if ($DB->get_record('block_mrbs_entry', array('instance' => $instance_id, 'name' => $name, 'start_time' => $time, 'type' => 'L'))) { return true; } else if ($record = $DB->get_record('block_mrbs_entry', array( - 'name' => $name, 'start_time' => $time, 'type' => 'M' + 'instance' => $instance_id, 'name' => $name, 'start_time' => $time, 'type' => 'M' )) ) { $upd = new stdClass; + $upd->instance =$instance_id; $upd->id = $record->id; $upd->type = 'K'; if ($DB->update_record('block_mrbs_entry', $upd)) { @@ -214,16 +220,17 @@ function is_timetabled($name, $time) { /** * Adds together a date (unixtime) and a time (hh:mm) * + * @param $enable_periods bool custom periods enabled + * @param $periods array custom periods * @param $date integer date in seconds since epoch * @param $time string time in hh:mm format * @return integer date/time in seconds since epoch */ -function time_to_datetime($date, $time) { - global $cfg_mrbs; +function time_to_datetime($enable_periods, $periods, $date, $time) { list($hours, $mins) = explode(':', $time); $hours = intval($hours); $mins = intval($mins); - if ($cfg_mrbs->enable_periods && $hours == 0 && $mins < count($cfg_mrbs->periods)) { + if ($enable_periods && $hours == 0 && $mins < count($periods)) { $hours = 12; // Periods are imported as P1 - 00:00, P2 - 00:01, P3 - 00:02, etc. // but stored internally as P1 - 12:00, P2 - 12:01, P3 - 12:02, etc. } @@ -235,14 +242,15 @@ function time_to_datetime($date, $time) { * If periods are enabled then gives the name of the period starting at this time * Will probably break is some idiot has more than 59 periods per day (seems very unlikely though) * + * @param $enable_periods bool custom periods enabled + * @param $periods array custom periods * @param $time integer unix timestamp * @return string either the time formatted as hh:mm or the name of the period starting at this time */ -function to_hr_time($time) { - $cfg_mrbs = get_config('block/mrbs'); - if ($cfg_mrbs->enable_periods) { +function to_hr_time($enable_periods, $periods, $time) { + if ($enable_periods) { $period = intval(date('i', $time)); - return $cfg_mrbs->periods[$period]; + return $periods[$period]; } else { return date('G:i', $time); } diff --git a/lang/en/block_mrbs.php b/lang/en/block_mrbs.php index 62e5fa6..9976880 100644 --- a/lang/en/block_mrbs.php +++ b/lang/en/block_mrbs.php @@ -50,6 +50,8 @@ $string['config_date_mmddyy'] = 'July 10'; $string['config_dateformat'] = 'Date format'; $string['config_dateformat2'] = 'Date format to be used by MRBS.'; +$string['config_default_instance'] = 'Default Instance'; +$string['config_default_instance2'] = 'Instance ID to use, if none is given in url. Used for compatibility on old configurations'; $string['config_default_report_days'] = 'Report span (days)'; $string['config_default_report_days2'] = 'Default report span in days'; $string['config_default_room'] = 'Default room'; @@ -68,6 +70,8 @@ $string['config_highlight_method2'] = 'Choose one of the highlight methods: bgcolor, class, or hybrid.'; $string['config_javascript_cursor'] = 'Javascript cursor'; $string['config_javascript_cursor2'] = 'Change to false if clients have old browsers incompatible with JavaScript.'; +$string['config_linkname'] = 'Link name'; +$string['config_linknname2'] = 'Set the name of the MRBS access link.'; $string['config_mail_admin_on_bookings'] = 'Mail admin bookings'; $string['config_mail_admin_on_bookings2'] = 'Send email to admin notifying of a new booking'; $string['config_mail_area_admin_on_bookings'] = 'Mail area admin'; @@ -217,6 +221,7 @@ $string['mrbs:doublebook'] = 'Double Book Rooms'; $string['mrbs:editmrbsunconfirmed'] = 'Only create \'unconfirmed\' bookings (overriden by \'editmrbs\')'; $string['mrbs:forcebook'] = 'Force Book Rooms (auto move existing bookings)'; +$string['mrbs:import'] = 'MRBS Import'; $string['mrbs:ignoremaxadvancedays'] = 'Ignore the \'max_advance_days\' setting'; $string['mrbs:myaddinstance'] = 'Add new MRBS block'; $string['mrbs:viewalltt'] = 'View All Users\' Timetables'; @@ -232,6 +237,8 @@ $string['must_set_description'] = 'You must set a description'; $string['must_set_name'] = 'You must set a name'; $string['namebooker'] = 'Reservation for'; +$string['newbooking'] = 'New Booking'; +$string['newmrbsblock'] = 'New MRBS'; $string['newwindow'] = 'New window'; //see MDL-15952 $string['no_rooms_for_area'] = 'No rooms defined for this area'; $string['no_user_with_email'] = 'No Moodle users found with an email address of: {$a}. All MRBS related email(s) must be associated with a Moodle user account.'; @@ -297,6 +304,8 @@ $string['search_results'] = 'Search Results for'; $string['seconds'] = 'seconds'; $string['serverpath'] = 'MRBS Installation path'; +$string['settings_heading_default'] = 'Default settings'; +$string['settings_default_comment'] = 'This settings are system defaults used for new MRBS instances.'; $string['show_my_entries'] = 'Click to display all my upcoming entries'; $string['slot'] = 'Slot'; $string['sort_rep_time'] = 'Start Date/Time'; diff --git a/settings.php b/settings.php index c152f52..e488c4f 100644 --- a/settings.php +++ b/settings.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . defined('MOODLE_INTERNAL') || die(); -global $CFG; +global $CFG,$DB; // The following couple of lines stop a warning message when setting up PHPUnit. if (!isset($CFG->supportname)) { @@ -25,7 +25,16 @@ $CFG->supportemail = ''; } -$cfg_mrbs = get_config('block/mrbs'); +$default_instance = get_config('block/mrbs', 'default_instance'); +if(!$default_instance) { + $conditions = array(); + $default_instance = $DB->get_field('block_instances', 'id', $conditions, IGNORE_MISSING | IGNORE_MULTIPLE); + if(!$default_instance) { + $default_instance = 0; + } +} +$settings->add(new admin_setting_heading('mrbs_default_settings', get_string('settings_heading_default','block_mrbs'), + get_string('settings_default_comment','block_mrbs'))); $options = array(0 => get_string('pagewindow', 'block_mrbs'), 1 => get_string('newwindow', 'block_mrbs')); $settings->add(new admin_setting_configselect('newwindow', get_string('config_new_window', 'block_mrbs'), get_string('config_new_window2', 'block_mrbs'), 1, $options)); @@ -35,6 +44,10 @@ get_string('adminview', 'block_mrbs'), $CFG->wwwroot.'/blocks/mrbs/web', PARAM_URL)); $settings->settings->serverpath->plugin = 'block/mrbs'; +$settings->add(new admin_setting_configtext('default_instance', get_string('config_default_instance', 'block_mrbs'), + get_string('config_default_instance2', 'block_mrbs'), $default_instance, PARAM_INT)); +$settings->settings->default_instance->plugin = 'block/mrbs'; + $settings->add(new admin_setting_configtext('admin', get_string('config_admin', 'block_mrbs'), get_string('config_admin2', 'block_mrbs'), $CFG->supportname, PARAM_TEXT)); $settings->settings->admin->plugin = 'block/mrbs'; diff --git a/version.php b/version.php index e4dbb6a..0d486b8 100644 --- a/version.php +++ b/version.php @@ -16,7 +16,6 @@ $plugin->version = 2022031900; $plugin->requires = 2019111800; // Moodle 3.8+ -$plugin->cron = 300; $plugin->component = 'block_mrbs'; $plugin->maturity = MATURITY_STABLE; -$plugin->release = '2.x (Build: 2022031900)'; +$plugin->release = '3.x (Build: 2022031900)'; diff --git a/web/add.php b/web/add.php index d8c1df6..430d5a8 100644 --- a/web/add.php +++ b/web/add.php @@ -27,7 +27,7 @@ $capacity = optional_param('capacity', 0, PARAM_INT); $area = optional_param('area', 0, PARAM_INT); -$thisurl = new moodle_url('/blocks/mrbs/web/add.php', array('type' => $type, 'name' => $name)); +$thisurl = new moodle_url('/blocks/mrbs/web/add.php', array('instance' =>$instance_id, 'type' => $type, 'name' => $name)); if (!empty($description)) { $thisurl->param('description', $description); } @@ -40,8 +40,8 @@ $PAGE->set_url($thisurl); require_login(); -if (!getAuthorised(2)) { - showAccessDenied($day, $month, $year, $area); +if (!getAuthorised($instance_id, 2)) { + showAccessDenied($day, $month, $year, $instance_id, $area); exit(); } require_sesskey(); @@ -54,16 +54,18 @@ if ($type == "area") { $newarea = new stdClass; $newarea->area_name = $name; + $newarea->instance = $instance_id; $area = $DB->insert_record('block_mrbs_area', $newarea); } if ($type == "room") { $newroom = new stdClass; $newroom->room_name = $name; + $newroom->instance = $instance_id; $newroom->description = $description; $newroom->capacity = $capacity; $newroom->area_id = $area; $DB->insert_record('block_mrbs_room', $newroom); } -redirect(new moodle_url('/blocks/mrbs/web/admin.php', array('area' => $area))); +redirect(new moodle_url('/blocks/mrbs/web/admin.php', array('instance' => $instance_id, 'area' => $area))); diff --git a/web/admin.php b/web/admin.php index cfb7e86..895463d 100644 --- a/web/admin.php +++ b/web/admin.php @@ -14,9 +14,8 @@ // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . - require_once(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php'); //for Moodle integration -global $PAGE, $CFG, $DB; +global $PAGE, $CFG, $DB, $COURSE; require "config.inc.php"; require "functions.php"; require_once("mrbs_auth.php"); @@ -34,27 +33,27 @@ $year = date("Y"); } -$thisurl = new moodle_url('/blocks/mrbs/web/admin.php', array('day' => $day, 'month' => $month, 'year' => $year)); +$thisurl = new moodle_url('/blocks/mrbs/web/admin.php', array('instance' => $instance_id, 'day' => $day, 'month' => $month, 'year' => $year)); if ($area) { $thisurl->param('area', $area); } else { - $area = get_default_area(); + $area = get_default_area($instance_id); } $PAGE->set_url($thisurl); require_login(); -if (!getAuthorised(2)) { - showAccessDenied($day, $month, $year, $area); +if (!getAuthorised($instance_id, 2)) { + showAccessDenied($day, $month, $year, $instance_id, $area); exit(); } -print_header_mrbs($day, $month, $year, isset($area) ? $area : ""); +print_header_mrbs($day, $month, $year, $instance_id, isset($area) ? $area : ""); // If area is set but area name is not known, get the name. if ($area) { if (empty($area_name)) { - $dbarea = $DB->get_record('block_mrbs_area', array('id' => $area), 'area_name', MUST_EXIST); + $dbarea = $DB->get_record('block_mrbs_area', array('instance' => $instance_id, 'id' => $area), 'area_name', MUST_EXIST); $area_name = $dbarea->area_name; } } @@ -69,7 +68,7 @@ echo '
\n"; //insert the goto room form - $gotoroom = new moodle_url('/blocks/mrbs/web/gotoroom.php'); + $gotoroom = new moodle_url('/blocks/mrbs/web/gotoroom.php', array('instance' => $instance_id)); $gostr = get_string('goroom', 'block_mrbs'); $gotoval = ''; $gotomsg = ''; @@ -121,7 +120,8 @@ } echo ""; //Draw the three month calendars - minicals($year, $month, $day, $area, '', 'day'); + minicals($year, $month, $day, $instance_id, $area, '', 'day'); echo "
'; // This cell has the areas -$areas = $DB->get_records('block_mrbs_area', null, 'area_name'); +$areas = $DB->get_records('block_mrbs_area', array('instance' => $instance_id), 'area_name'); if (empty($areas)) { echo get_string('noareas', 'block_mrbs'); @@ -78,11 +77,11 @@ foreach ($areas as $dbarea) { $area_name_q = urlencode($dbarea->area_name); $adminurl = new moodle_url('/blocks/mrbs/web/admin.php', array( - 'area' => $dbarea->id, 'area_name' => $area_name_q, 'sesskey' => sesskey() + 'instance' => $instance_id, 'area' => $dbarea->id, 'area_name' => $area_name_q, 'sesskey' => sesskey() )); - $editroomurl = new moodle_url('/blocks/mrbs/web/edit_area_room.php', array('area' => $dbarea->id, 'sesskey' => sesskey())); + $editroomurl = new moodle_url('/blocks/mrbs/web/edit_area_room.php', array('instance' => $instance_id, 'area' => $dbarea->id, 'sesskey' => sesskey())); $delareaurl = new moodle_url('/blocks/mrbs/web/del.php', array( - 'area' => $dbarea->id, 'type' => 'area', 'sesskey' => sesskey() + 'instance' => $instance_id, 'area' => $dbarea->id, 'type' => 'area', 'sesskey' => sesskey() )); echo '
  • '.s($dbarea->area_name).' ('.get_string('edit').') ('.get_string('delete').")\n"; } @@ -92,7 +91,7 @@ // This one has the rooms if ($area) { - $rooms = $DB->get_records('block_mrbs_room', array('area_id' => $area), 'room_name'); + $rooms = $DB->get_records('block_mrbs_room', array('instance' => $instance_id, 'area_id' => $area), 'room_name'); if (empty($rooms)) { // $res = sql_query("select id, room_name, description, capacity from $tbl_room where area_id=$area order by room_name"); echo get_string('norooms', 'block_mrbs'); @@ -100,10 +99,10 @@ echo '
      '; foreach ($rooms as $dbroom) { $editroomurl = new moodle_url('/blocks/mrbs/web/edit_area_room.php', array( - 'room' => $dbroom->id, 'sesskey' => sesskey() + 'instance' => $instance_id, 'room' => $dbroom->id, 'sesskey' => sesskey() )); $delroomurl = new moodle_url('/blocks/mrbs/web/del.php', array( - 'area' => $area, 'room' => $dbroom->id, 'type' => 'room', 'sesskey' => sesskey() + 'instance' => $instance_id, 'area' => $area, 'room' => $dbroom->id, 'type' => 'room', 'sesskey' => sesskey() )); $info = array(); $desc = trim(s($dbroom->description)); @@ -126,7 +125,7 @@ echo get_string('noarea', 'block_mrbs'); } -$addareaurl = new moodle_url('/blocks/mrbs/web/add.php', array('type' => 'area', 'sesskey' => sesskey())); +$addareaurl = new moodle_url('/blocks/mrbs/web/add.php', array('instance' => $instance_id, 'type' => 'area', 'sesskey' => sesskey())); $addroomurl = new moodle_url($addareaurl, array('type' => 'room', 'area' => $area)); echo '
  • '.get_string('addarea', 'block_mrbs').'

    '; diff --git a/web/auth_moodle.php b/web/auth_moodle.php index d3c9d4f..d82fa69 100644 --- a/web/auth_moodle.php +++ b/web/auth_moodle.php @@ -39,16 +39,15 @@ * $auth["session"] = "moodle"; * */ - require_once(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php'); //for Moodle integration function authValidateUser($user, $pass) { return 1; } -function authGetUserLevel($user) { +function authGetUserLevel($instance_id, $user) { // HACK For Moodle 1.7 With Roles Block... - $context = context_system::instance(); + $context = context_block::instance($instance_id); // Set Access leve for users via MRBS block and Moodle 1.7 roles if (has_capability('block/mrbs:administermrbs', $context)) { diff --git a/web/config.inc.php b/web/config.inc.php index 9c52e5e..4a68726 100644 --- a/web/config.inc.php +++ b/web/config.inc.php @@ -19,10 +19,23 @@ # MRBS Configuration File # You shouldn't have to modify this file as all options can be set via Moode - see CONTRIB-422 ########################################################################### - //For integration with Moodle require_once(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php'); -$cfg_mrbs = get_config('block/mrbs'); //get Moodle config settings for the MRBS block +global $DB,$CFG; + +$cfg_mrbs = get_config('block/mrbs'); +$instance_id = optional_param('instance', $cfg_mrbs->default_instance, PARAM_INT); +if(! isset($instance_id)) { + throw new \coding_exception('Either instance_id or mrbs->default_instance is required.'); +} +if( $instance_id != $cfg_mrbs->default_instance ){ //get Moodle config settings for this instance of the MRBS block + $tmp = $DB->get_record('block_instances', array('id' => $instance_id), '*', MUST_EXIST); + if(! isset($tmp)) { + throw new \coding_exception('block_instance with id '.$instance_id.' must exist.'); + } + $cfg_mrbs = unserialize(base64_decode($tmp->configdata)); +} + ################### # Database settings ################### diff --git a/web/day.php b/web/day.php index e2a9d99..9e80480 100644 --- a/web/day.php +++ b/web/day.php @@ -21,7 +21,6 @@ include "functions.php"; require_once('mrbs_auth.php'); include "mincals.php"; - $day = optional_param('day', 0, PARAM_INT); $month = optional_param('month', 0, PARAM_INT); $year = optional_param('year', 0, PARAM_INT); @@ -55,13 +54,13 @@ } $baseurl = new moodle_url('/blocks/mrbs/web/day.php', array( - 'day' => $day, 'month' => $month, 'year' => $year + 'instance' => $instance_id, 'day' => $day, 'month' => $month, 'year' => $year )); // Used as basis for URLs throughout this file $thisurl = new moodle_url($baseurl); if ($area > 0) { $thisurl->param('area', $area); } else { - $area = get_default_area(); + $area = get_default_area($instance_id); } if ($morningstarts_minutes > 0) { $thisurl->param('morningstarts_minutes', $morningstarts_minutes); @@ -74,7 +73,7 @@ require_login(); // print the page header -print_header_mrbs($day, $month, $year, $area); +print_header_mrbs($day, $month, $year, $instance_id, $area); // Define the start and end of each day in a way which is not affected by // daylight saving... @@ -95,10 +94,10 @@ // need to show either a select box or a normal html list, // depending on the settings in config.inc.php if ($area_list_format == "select") { - echo make_area_select_html(new moodle_url('/blocks/mrbs/web/day.php'), $area, $year, $month, $day); // from functions.php + echo make_area_select_html(new moodle_url('/blocks/mrbs/web/day.php'), $area, $instance_id, $year, $month, $day); // from functions.php } else { // show the standard html list - $areas = $DB->get_records('block_mrbs_area', null, 'area_name'); + $areas = $DB->get_records('block_mrbs_area', array('instance' => $instance_id), 'area_name'); foreach ($areas as $dbarea) { echo ''; if ($dbarea->id == $area) { @@ -111,7 +110,7 @@ echo "

    ".get_string('findroom', 'block_mrbs')."

    - + + @@ -129,7 +129,7 @@
    "; } @@ -168,11 +168,12 @@ $sql = "SELECT e.id AS eid, r.id AS rid, e.start_time, e.end_time, e.name, e.type, e.description FROM {block_mrbs_entry} e, {block_mrbs_room} r - WHERE e.room_id = r.id + WHERE e.instance = ? + AND e.room_id = r.id AND r.area_id = ? AND e.start_time <= ? AND e.end_time > ?"; - $entries = $DB->get_records_sql($sql, array($area, $pm7, $am7)); + $entries = $DB->get_records_sql($sql, array($instance_id, $area, $pm7, $am7)); foreach ($entries as $entry) { // $today is a map of the screen that will be displayed @@ -242,7 +243,7 @@ // We need to know what all the rooms area called, so we can show them all // pull the data from the db and store it. Convienently we can print the room // headings and capacities at the same time - $rooms = $DB->get_records('block_mrbs_room', array('area_id' => $area), 'room_name'); + $rooms = $DB->get_records('block_mrbs_room', array('instance' => $instance_id, 'area_id' => $area), 'room_name'); foreach ($rooms as $room) { $room->allowedtobook = allowed_to_book($USER, $room); } @@ -286,7 +287,7 @@ $room_column_width = (int)(95 / count($rooms)); $weekurl = new moodle_url('/blocks/mrbs/web/week.php', array( - 'year' => $year, 'month' => $month, 'day' => $day, 'area' => $area + 'instance' => $instance_id, 'year' => $year, 'month' => $month, 'day' => $day, 'area' => $area )); foreach ($rooms as $room) { echo " @@ -401,6 +402,7 @@ echo "
    "; $editurl = new moodle_url('/blocks/mrbs/web/edit_entry.php', array( + 'instance' => $instance_id, 'room' => $room->id, 'area' => $area, 'year' => $year, 'month' => $month, 'day' => $day )); @@ -433,6 +435,7 @@ for ($i = 0; $i < count($descrs); $i++) { $viewentry = new moodle_url('/blocks/mrbs/web/view_entry.php', array( + 'instance' => $instance_id, 'id' => $ids[$i], 'area' => $area, 'day' => $day, 'month' => $month, 'year' => $year )); diff --git a/web/del.php b/web/del.php index a416f5a..1f8dceb 100644 --- a/web/del.php +++ b/web/del.php @@ -37,11 +37,11 @@ $year = date("Y"); } -$thisurl = new moodle_url('/blocks/mrbs/web/del.php', array('day' => $day, 'month' => $month, 'year' => $year, 'type' => $type)); +$thisurl = new moodle_url('/blocks/mrbs/web/del.php', array('instance' => $instance_id, 'day' => $day, 'month' => $month, 'year' => $year, 'type' => $type)); if ($area) { $thisurl->param('area', $area); } else { - $area = get_default_area(); + $area = get_default_area($instance_id); } if ($room) { $thisurl->param('room', $room); @@ -52,13 +52,13 @@ $PAGE->set_url($thisurl); require_login(); -if (!getAuthorised(2)) { - showAccessDenied($day, $month, $year, $area); +if (!getAuthorised($instance_id, 2)) { + showAccessDenied($day, $month, $year, $instance_id, $area); exit(); } require_sesskey(); -$adminurl = new moodle_url('/blocks/mrbs/web/admin.php'); +$adminurl = new moodle_url('/blocks/mrbs/web/admin.php', array('instance' => $instance_id)); // This is gonna blast away something. We want them to be really // really sure that this is what they want to do. @@ -69,20 +69,20 @@ // We are supposed to delete a room if ($confirm) { // Delete bookings - $DB->delete_records('block_mrbs_entry', array('room_id' => $room)); + $DB->delete_records('block_mrbs_entry', array('instance' => $instance_id, 'room_id' => $room)); // Delete the room - $DB->delete_records('block_mrbs_room', array('id' => $room)); + $DB->delete_records('block_mrbs_room', array('instance' => $instance_id, 'id' => $room)); // Go back to the admin page redirect($adminurl); } else { - print_header_mrbs($day, $month, $year, $area); + print_header_mrbs($day, $month, $year, $instance_id, $area); // We tell them how bad what theyre about to do is // Find out how many appointments would be deleted - $bookings = $DB->get_records('block_mrbs_entry', array('room_id' => $room)); + $bookings = $DB->get_records('block_mrbs_entry', array('instance' => $instance_id, 'room_id' => $room)); if (!empty($bookings)) { echo get_string('deletefollowing', 'block_mrbs').":