]> git.mjollnir.org Git - moodle.git/commitdiff
Upgrade to roles code, as well as restoring from 1.6 on a 1.7 install code.
authorvyshane <vyshane>
Wed, 20 Sep 2006 16:57:01 +0000 (16:57 +0000)
committervyshane <vyshane>
Wed, 20 Sep 2006 16:57:01 +0000 (16:57 +0000)
mod/data/db/access.php
mod/data/db/mysql.php
mod/data/db/postgres7.php
mod/data/lib.php
mod/data/restorelib.php

index 90625ba5d24c385f6026f5bb2c4eb8d9977bebf1..c9d39796aa0cda5d07f5c457bf7008f7810e56de 100644 (file)
@@ -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,
index b5079fca70ba3ece39d892b509f91e9810eee81f..69eaa359e97ff31cf2d0b2b84f9887befd25c518 100644 (file)
@@ -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;
 }
 
index c923c360aad93b658088e48fd883b58ad499c747..7c883c56224528e6629edb36e379f76ddcced66c 100644 (file)
@@ -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
index d494f00f55bd94abb1a010ac4f5b3b6052095e71..05422278ddfaafab6ce274c22e0b4b308cce791d 100755 (executable)
@@ -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
index 4a830a3f73f680ed4fcda24791f50c0e4da8c0fd..e3933dc755d59d8b7a3ebd3d389f3d6be80d72e7 100644 (file)
@@ -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;
         }