From: vyshane Date: Wed, 20 Sep 2006 16:57:01 +0000 (+0000) Subject: Upgrade to roles code, as well as restoring from 1.6 on a 1.7 install code. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=7f2586640e776de8d4ea4db10ec1c4b87029d86b;p=moodle.git Upgrade to roles code, as well as restoring from 1.6 on a 1.7 install code. --- diff --git a/mod/data/db/access.php b/mod/data/db/access.php index 90625ba5d2..c9d39796aa 100644 --- a/mod/data/db/access.php +++ b/mod/data/db/access.php @@ -47,20 +47,6 @@ $mod_data_capabilities = array( ) ), - 'mod/data:viewentriesfromallgrous' => array( - - 'captype' => 'read', - 'contextlevel' => CONTEXT_MODULE, - 'legacy' => array( - 'guest' => CAP_PREVENT, - 'student' => CAP_PREVENT, - 'teacher' => CAP_ALLOW, - 'editingteacher' => CAP_ALLOW, - 'coursecreator' => CAP_ALLOW, - 'admin' => CAP_ALLOW - ) - ), - 'mod/data:writeentry' => array( 'riskbitmask' => RISK_SPAM, diff --git a/mod/data/db/mysql.php b/mod/data/db/mysql.php index b5079fca70..69eaa359e9 100644 --- a/mod/data/db/mysql.php +++ b/mod/data/db/mysql.php @@ -71,7 +71,43 @@ function data_upgrade($oldversion) { if ($oldversion < 2006081700) { table_column('data', '', 'jstemplate', 'text', '', '', '', 'not null', 'csstemplate'); } + /* + if ($oldversion < 2006092000) { + // Upgrades for new roles and capabilities support. + require_once($CFG->dirroot.'/mod/data/lib.php'); + $datamod = get_record('modules', 'name', 'data'); + + if ($data = get_records('data')) { + + if (!$teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) { + notify('Default teacher role was not found. Roles and permissions '. + 'for all your forums will have to be manually set after '. + 'this upgrade.'); + } + if (!$studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) { + notify('Default student role was not found. Roles and permissions '. + 'for all your forums will have to be manually set after '. + 'this upgrade.'); + } + foreach ($data as $d) { + if (!data_convert_to_roles($d, $teacherroles, $studentroles)) { + notify('Data with id '.$d->id.' was not upgraded'); + } + } + // We need to rebuild all the course caches to refresh the state of + // the forum modules. + include_once( "$CFG->dirroot/course/lib.php" ); + rebuild_course_cache(); + + } // End if. + + modify_database('', 'ALTER TABLE prefix_data DROP COLUMN participants;'); + modify_database('', 'ALTER TABLE prefix_data DROP COLUMN assesspublic;'); + modify_database('', 'ALTER TABLE prefix_data DROP COLUMN groupmode;'); + + } + */ return true; } diff --git a/mod/data/db/postgres7.php b/mod/data/db/postgres7.php index c923c360aa..7c883c5622 100644 --- a/mod/data/db/postgres7.php +++ b/mod/data/db/postgres7.php @@ -155,8 +155,44 @@ function data_upgrade($oldversion) { if ($oldversion < 2006081700) { table_column('data', '', 'jstemplate', 'text', '', '', '', 'not null', 'csstemplate'); } - + /* + if ($oldversion < 2006092000) { + // Upgrades for new roles and capabilities support. + require_once($CFG->dirroot.'/mod/data/lib.php'); + + $datamod = get_record('modules', 'name', 'data'); + + if ($data = get_records('data')) { + + if (!$teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) { + notify('Default teacher role was not found. Roles and permissions '. + 'for all your forums will have to be manually set after '. + 'this upgrade.'); + } + if (!$studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) { + notify('Default student role was not found. Roles and permissions '. + 'for all your forums will have to be manually set after '. + 'this upgrade.'); + } + foreach ($data as $d) { + if (!data_convert_to_roles($d $teacherroles, $studentroles)) { + notify('Data with id '.$d->id.' was not upgraded'); + } + } + // We need to rebuild all the course caches to refresh the state of + // the forum modules. + include_once( "$CFG->dirroot/course/lib.php" ); + rebuild_course_cache(); + + } // End if. + + modify_database('', 'ALTER TABLE prefix_data DROP COLUMN participants;'); + modify_database('', 'ALTER TABLE prefix_data DROP COLUMN assesspublic;'); + modify_database('', 'ALTER TABLE prefix_data DROP COLUMN groupmode;'); + + } + */ return true; } -?> +?> \ No newline at end of file diff --git a/mod/data/lib.php b/mod/data/lib.php index d494f00f55..05422278dd 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -1290,4 +1290,147 @@ function data_fields_print_header($course,$cm,$data,$showtabs=true) { } } -?> + +/** + * Converts a database (module instance) to use the Roles System + * @param $data - a data object with the same attributes as a record + * from the data database table + * @param $datamodid - the id of the data module, from the modules table + * @param $teacherroles - array of roles that have moodle/legacy:teacher + * @param $studentroles - array of roles that have moodle/legacy:student + * @param $guestroles - array of roles that have moodle/legacy:guest + * @param $cmid - the course_module id for this data instance + * @return boolean - data module was converted or not + */ +function data_convert_to_roles($data, $teacherroles=array(), $studentroles=array(), $cmid=NULL) { + + global $CFG; + + if (!isset($data->participants) && !isset($data->assesspublic) + && !isset($data->groupmode)) { + // We assume that this database has already been converted to use the + // Roles System. above fields get dropped the data module has been + // upgraded to use Roles. + return false; + } + + if (empty($cmid)) { + // We were not given the course_module id. Try to find it. + if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) { + notify('Could not get the course module for the forum'); + return false; + } else { + $cmid = $cm->id; + } + } + $context = get_context_instance(CONTEXT_MODULE, $cmid); + + + // $data->participants: + // 1 - Only teachers can add entries + // 3 - Teachers and students can add entries + switch ($data->participants) { + case 1: + foreach ($studentroles as $studentrole) { + assign_capability('mod/data:writeentry', CAP_PREVENT, $studentrole->id, $context->id); + } + foreach ($teacherroles as $teacherrole) { + assign_capability('mod/data:writeentry', CAP_ALLOW, $teacherrole->id, $context->id); + } + break; + case 3: + foreach ($studentroles as $studentrole) { + assign_capability('mod/data:writeentry', CAP_ALLOW, $studentrole->id, $context->id); + } + foreach ($teacherroles as $teacherrole) { + assign_capability('mod/data:writeentry', CAP_ALLOW, $teacherrole->id, $context->id); + } + break; + } + + // $data->assessed: + // 2 - Only teachers can rate posts + // 1 - Everyone can rate posts + // 0 - No one can rate posts + switch ($data->assessed) { + case 0: + foreach ($studentroles as $studentrole) { + assign_capability('mod/data:rate', CAP_PREVENT, $studentrole->id, $context->id); + } + foreach ($teacherroles as $teacherrole) { + assign_capability('mod/data:rate', CAP_PREVENT, $teacherrole->id, $context->id); + } + break; + case 1: + foreach ($studentroles as $studentrole) { + assign_capability('mod/data:rate', CAP_ALLOW, $studentrole->id, $context->id); + } + foreach ($teacherroles as $teacherrole) { + assign_capability('mod/data:rate', CAP_ALLOW, $teacherrole->id, $context->id); + } + break; + case 2: + foreach ($studentroles as $studentrole) { + assign_capability('mod/data:rate', CAP_PREVENT, $studentrole->id, $context->id); + } + foreach ($teacherroles as $teacherrole) { + assign_capability('mod/data:rate', CAP_ALLOW, $teacherrole->id, $context->id); + } + break; + } + + // $data->assesspublic: + // 0 - Students can only see their own ratings + // 1 - Students can see everyone's ratings + switch ($data->assesspublic) { + case 0: + foreach ($studentroles as $studentrole) { + assign_capability('mod/data:viewrating', CAP_PREVENT, $studentrole->id, $context->id); + } + foreach ($teacherroles as $teacherrole) { + assign_capability('mod/data:viewrating', CAP_ALLOW, $teacherrole->id, $context->id); + } + break; + case 1: + foreach ($studentroles as $studentrole) { + assign_capability('mod/data:viewrating', CAP_ALLOW, $studentrole->id, $context->id); + } + foreach ($teacherroles as $teacherrole) { + assign_capability('mod/data:viewrating', CAP_ALLOW, $teacherrole->id, $context->id); + } + break; + } + + if (empty($cm)) { + $cm = get_record('course_modules', 'id', $cmid); + } + + // $cm->groupmode: + // 0 - No groups + // 1 - Separate groups + // 2 - Visible groups + switch ($cm->groupmode) { + case 0: + break; + case 1: + foreach ($studentroles as $studentrole) { + assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $studentrole->id, $context->id); + } + foreach ($teacherroles as $teacherrole) { + assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $teacherrole->id, $context->id); + } + break; + case 2: + foreach ($studentroles as $studentrole) { + assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $studentrole->id, $context->id); + } + foreach ($teacherroles as $teacherrole) { + assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $teacherrole->id, $context->id); + } + break; + } + return true; +} + + +?> \ No newline at end of file diff --git a/mod/data/restorelib.php b/mod/data/restorelib.php index 4a830a3f73..e3933dc755 100644 --- a/mod/data/restorelib.php +++ b/mod/data/restorelib.php @@ -106,7 +106,7 @@ function data_restore_mods($mod,$restore) { $database->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']); $database->assesspublic = backup_todb($info['MOD']['#']['ASSESSPUBLIC']['0']['#']); - $newid = insert_record ("data",$database); + $newid = insert_record ('data', $database); //Do some output if (!defined('RESTORE_SILENTLY')) { @@ -131,6 +131,30 @@ function data_restore_mods($mod,$restore) { $status = $status and data_records_restore_mods ($mod->id, $newid, $info, $restore); } + + // If the backup contained $data->participants, $data->assesspublic + // and $data->groupmode, we need to convert the data to use Roles. + // It means the backup was made pre Moodle 1.7. We check the + // backup_version to make sure. + if (isset($database->participants) && isset($database->assesspublic)) { + + if (!$teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) { + notice('Default teacher role was not found. Roles and permissions '. + 'for your database modules will have to be manually set.'); + } + if (!$studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) { + notice('Default student role was not found. Roles and permissions '. + 'for all your database modules will have to be manually set.'); + } + if (!$guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) { + notice('Default guest role was not found. Roles and permissions '. + 'for all your database modules will have to be manually set.'); + } + require_once($CFG->dirroot.'/mod/data/lib.php'); + data_convert_to_roles($database, $teacherroles, $studentroles, + $restore->mods['data']->instances[$mod->id]->restored_as_course_module); + } + } else { $status = false; }