Skip to content

Commit

Permalink
MDL-44725 Availability: Replace groupmembersonly - upgrade, core (1)
Browse files Browse the repository at this point in the history
* Upgrades existing data so that the groupmembersonly option is
  replaced with equivalent data for the new availability API.
* Removes the database field and admin setting used for
  groupmembersonly.
* Changes core modinfo library to remove/deprecate groupmembersonly
  data and functions.
* Changes other core library files to remove groupmembersonly
  references.

Includes deprecation of several functions, listed in lib/upgrade.txt.
  • Loading branch information
sammarshallou committed Sep 2, 2014
1 parent 5474b8b commit 061e6b2
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 263 deletions.
1 change: 0 additions & 1 deletion admin/settings/development.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//TODO: Re-enable cc-import once re-implemented in 2.0.x
//$temp->add(new admin_setting_configcheckbox('enableimsccimport', new lang_string('enable_cc_import', 'imscc'), new lang_string('enable_cc_import_description', 'imscc'), 0));
$temp->add(new admin_setting_configcheckbox('enablesafebrowserintegration', new lang_string('enablesafebrowserintegration', 'admin'), new lang_string('configenablesafebrowserintegration', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('enablegroupmembersonly', new lang_string('enablegroupmembersonly', 'admin'), new lang_string('configenablegroupmembersonly', 'admin'), 0));

$temp->add(new admin_setting_configcheckbox('dndallowtextandlinks', new lang_string('dndallowtextandlinks', 'admin'), new lang_string('configdndallowtextandlinks', 'admin'), 0));
// The CSS optimiser setting. When changed we need to reset the theme caches in order to ensure they are regenerated through the optimiser.
Expand Down
15 changes: 6 additions & 9 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,6 @@ function get_array_of_activities($courseid) {
// name - the name of the instance
// visible - is the instance visible or not
// groupingid - grouping id
// groupmembersonly - is this instance visible to group members only
// extra - contains extra string to include in any link
global $CFG, $DB;
if(!empty($CFG->enableavailability)) {
Expand Down Expand Up @@ -1048,7 +1047,6 @@ function get_array_of_activities($courseid) {
$mod[$seq]->visibleold = $rawmods[$seq]->visibleold;
$mod[$seq]->groupmode = $rawmods[$seq]->groupmode;
$mod[$seq]->groupingid = $rawmods[$seq]->groupingid;
$mod[$seq]->groupmembersonly = $rawmods[$seq]->groupmembersonly;
$mod[$seq]->indent = $rawmods[$seq]->indent;
$mod[$seq]->completion = $rawmods[$seq]->completion;
$mod[$seq]->extra = "";
Expand Down Expand Up @@ -1127,13 +1125,12 @@ function get_array_of_activities($courseid) {
$mod[$seq]->name = $DB->get_field($rawmods[$seq]->modname, "name", array("id"=>$rawmods[$seq]->instance));
}

// Minimise the database size by unsetting default options when they are
// 'empty'. This list corresponds to code in the cm_info constructor.
foreach (array('idnumber', 'groupmode', 'groupingid', 'groupmembersonly',
'indent', 'completion', 'extra', 'extraclasses', 'iconurl', 'onclick', 'content',
'icon', 'iconcomponent', 'customdata', 'availability',
'completionview', 'completionexpected', 'score', 'showdescription')
as $property) {
// Minimise the database size by unsetting default options when they are
// 'empty'. This list corresponds to code in the cm_info constructor.
foreach (array('idnumber', 'groupmode', 'groupingid',
'indent', 'completion', 'extra', 'extraclasses', 'iconurl', 'onclick', 'content',
'icon', 'iconcomponent', 'customdata', 'availability', 'completionview',
'completionexpected', 'score', 'showdescription') as $property) {
if (property_exists($mod[$seq], $property) &&
empty($mod[$seq]->{$property})) {
unset($mod[$seq]->{$property});
Expand Down
4 changes: 0 additions & 4 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ private function create_specific_module_test($modulename) {
$moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
$moduleinfo->course = $course->id;
$moduleinfo->groupingid = $grouping->id;
$moduleinfo->groupmembersonly = 0;
$moduleinfo->visible = true;

// Sometimes optional generic values for some modules.
Expand Down Expand Up @@ -276,7 +275,6 @@ private function create_specific_module_test($modulename) {
$this->assertEquals($moduleinfo->section, $section->section);
$this->assertEquals($moduleinfo->course, $dbcm->course);
$this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
$this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
$this->assertEquals($moduleinfo->visible, $dbcm->visible);
$this->assertEquals($moduleinfo->completion, $dbcm->completion);
$this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
Expand Down Expand Up @@ -438,7 +436,6 @@ private function update_specific_module_test($modulename) {
$moduleinfo->modulename = $modulename;
$moduleinfo->course = $course->id;
$moduleinfo->groupingid = $grouping->id;
$moduleinfo->groupmembersonly = 0;
$moduleinfo->visible = true;

// Sometimes optional generic values for some modules.
Expand Down Expand Up @@ -509,7 +506,6 @@ private function update_specific_module_test($modulename) {
$this->assertEquals($moduleinfo->modulename, $dbcm->modname);
$this->assertEquals($moduleinfo->course, $dbcm->course);
$this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
$this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
$this->assertEquals($moduleinfo->visible, $dbcm->visible);
$this->assertEquals($moduleinfo->completion, $dbcm->completion);
$this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
Expand Down
2 changes: 0 additions & 2 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@
$string['configenablecalendarexport'] = 'Enable exporting or subscribing to calendars.';
$string['configenablecomments'] = 'Enable comments';
$string['configenablecourserequests'] = 'This will allow any user to request a course be created.';
$string['configenablegroupmembersonly'] = 'If enabled, access to activities can be restricted to group members only. This may result in an increased server load. In addition, gradebook categories must be set up in a certain way to ensure that activities are hidden from non-group members.';
$string['configenablemobilewebservice'] = 'Enable mobile service for the official Moodle app or other app requesting it. For more information, read the {$a}';
$string['configenablerssfeeds'] = 'If enabled, RSS feeds are generated by various features across the site, such as blogs, forums, database activities and glossaries. Note that RSS feeds also need to be enabled for the particular activity modules.';
$string['configenablerssfeedsdisabled'] = 'It is not available because RSS feeds are disabled in all the Site. To enable them, go to the Variables settings under Admin Configuration.';
Expand Down Expand Up @@ -472,7 +471,6 @@
$string['enabledevicedetection'] = 'Enable device detection';
$string['enablegravatar'] = 'Enable Gravatar';
$string['enablegravatar_help'] = 'When enabled Moodle will attempt to fetch a user profile picture from Gravatar if the user has not uploaded an image.';
$string['enablegroupmembersonly'] = 'Enable group members only';
$string['enablemobilewebservice'] = 'Enable web services for mobile devices';
$string['enablerecordcache'] = 'Enable record cache';
$string['enablerssfeeds'] = 'Enable RSS feeds';
Expand Down
10 changes: 5 additions & 5 deletions lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ function get_coursemodules_in_course($modulename, $courseid, $extrafields='') {
* in the course. Returns an empty array on any errors.
*
* The returned objects includle the columns cw.section, cm.visible,
* cm.groupmode and cm.groupingid, cm.groupmembersonly, and are indexed by cm.id.
* cm.groupmode, and cm.groupingid, and are indexed by cm.id.
*
* @global object
* @global object
Expand Down Expand Up @@ -1470,7 +1470,7 @@ function get_all_instances_in_courses($modulename, $courses, $userid=NULL, $incl
$params['modulename'] = $modulename;

if (!$rawmods = $DB->get_records_sql("SELECT cm.id AS coursemodule, m.*, cw.section, cm.visible AS visible,
cm.groupmode, cm.groupingid, cm.groupmembersonly
cm.groupmode, cm.groupingid
FROM {course_modules} cm, {course_sections} cw, {modules} md,
{".$modulename."} m
WHERE cm.course $coursessql AND
Expand Down Expand Up @@ -1515,7 +1515,7 @@ function get_all_instances_in_courses($modulename, $courses, $userid=NULL, $incl
* in the course. Returns an empty array on any errors.
*
* The returned objects includle the columns cw.section, cm.visible,
* cm.groupmode and cm.groupingid, cm.groupmembersonly, and are indexed by cm.id.
* cm.groupmode, and cm.groupingid, and are indexed by cm.id.
*
* Simply calls {@link all_instances_in_courses()} with a single provided course
*
Expand All @@ -1536,7 +1536,7 @@ function get_all_instances_in_course($modulename, $course, $userid=NULL, $includ
*
* Given a valid module object with info about the id and course,
* and the module's type (eg "forum") returns whether the object
* is visible or not, groupmembersonly visibility not tested
* is visible or not according to the 'eye' icon only.
*
* NOTE: This does NOT take into account visibility to a particular user.
* To get visibility access for a specific user, use get_fast_modinfo, get a
Expand All @@ -1554,7 +1554,7 @@ function instance_is_visible($moduletype, $module) {

if (!empty($module->id)) {
$params = array('courseid'=>$module->course, 'moduletype'=>$moduletype, 'moduleid'=>$module->id);
if ($records = $DB->get_records_sql("SELECT cm.instance, cm.visible, cm.groupingid, cm.id, cm.groupmembersonly, cm.course
if ($records = $DB->get_records_sql("SELECT cm.instance, cm.visible, cm.groupingid, cm.id, cm.course
FROM {course_modules} cm, {modules} m
WHERE cm.course = :courseid AND
cm.module = m.id AND
Expand Down
1 change: 0 additions & 1 deletion lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@
<FIELD NAME="visibleold" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="groupmode" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="groupingid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="groupmembersonly" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="only members of any group are allowed to access the activity"/>
<FIELD NAME="completion" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Whether the completion-tracking facilities are enabled for this activity. 0 = not enabled (database default) 1 = manual tracking, user can tick this activity off (UI default for most activity types) 2 = automatic tracking, system should mark completion according to rules specified in course_moduleS_completion"/>
<FIELD NAME="completiongradeitemnumber" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Grade-item number used to track automatic completion, if applicable."/>
<FIELD NAME="completionview" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Controls whether a page view is part of the automatic completion requirements for this activity. 0 = view not required 1 = view required"/>
Expand Down
47 changes: 47 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3743,5 +3743,52 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2014082900.01);
}

if ($oldversion < 2014082900.02) {
// Replace groupmembersonly usage with new availability system.
$transaction = $DB->start_delegated_transaction();
if ($CFG->enablegroupmembersonly) {
// If it isn't already enabled, we need to enable availability.
if (!$CFG->enableavailability) {
set_config('enableavailability', 1);
}

// Count all course-modules with groupmembersonly set (for progress
// bar).
$total = $DB->count_records('course_modules', array('groupmembersonly' => 1));
$pbar = new progress_bar('upgradegroupmembersonly', 500, true);

// Get all these course-modules, one at a time.
$rs = $DB->get_recordset('course_modules', array('groupmembersonly' => 1),
'course, id');
$i = 0;
foreach ($rs as $cm) {
// Calculate and set new availability value.
$availability = upgrade_group_members_only($cm->groupingid, $cm->availability);
$DB->set_field('course_modules', 'availability', $availability,
array('id' => $cm->id));

// Update progress.
$i++;
$pbar->update($i, $total, "Upgrading groupmembersonly settings - $i/$total.");
}
$rs->close();
}

// Define field groupmembersonly to be dropped from course_modules.
$table = new xmldb_table('course_modules');
$field = new xmldb_field('groupmembersonly');

// Conditionally launch drop field groupmembersonly.
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}

// Unset old config variable.
unset_config('enablegroupmembersonly');
$transaction->allow_commit();

upgrade_main_savepoint(true, 2014082900.02);
}

return true;
}
54 changes: 54 additions & 0 deletions lib/db/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,60 @@ function upgrade_availability_item($groupmembersonly, $groupingid,
}
}

/**
* Using data for a single course-module that has groupmembersonly enabled,
* returns the new availability value that incorporates the correct
* groupmembersonly option.
*
* Included as a function so that it can be shared between upgrade and restore,
* and unit-tested.
*
* @param int $groupingid Grouping id for the course-module (0 if none)
* @param string $availability Availability JSON data for the module (null if none)
* @return string New value for availability for the module
*/
function upgrade_group_members_only($groupingid, $availability) {
// Work out the new JSON object representing this option.
if ($groupingid) {
// Require specific grouping.
$condition = (object)array('type' => 'grouping', 'id' => (int)$groupingid);
} else {
// No grouping specified, so require membership of any group.
$condition = (object)array('type' => 'group');
}

if (is_null($availability)) {
// If there are no conditions using the new API then just set it.
$tree = (object)array('op' => '&', 'c' => array($condition), 'showc' => array(false));
} else {
// There are existing conditions.
$tree = json_decode($availability);
switch ($tree->op) {
case '&' :
// For & conditions we can just add this one.
$tree->c[] = $condition;
$tree->showc[] = false;
break;
case '!|' :
// For 'not or' conditions we can add this one
// but negated.
$tree->c[] = (object)array('op' => '!&', 'c' => array($condition));
$tree->showc[] = false;
break;
default:
// For the other two (OR and NOT AND) we have to add
// an extra level to the tree.
$tree = (object)array('op' => '&', 'c' => array($tree, $condition),
'showc' => array($tree->show, false));
// Inner trees do not have a show option, so remove it.
unset($tree->c[0]->show);
break;
}
}

return json_encode($tree);
}

/**
* Updates the mime-types for files that exist in the database, based on their
* file extension.
Expand Down
47 changes: 47 additions & 0 deletions lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,53 @@ function get_current_group($courseid, $full = false) {
}
}

/**
* Filter a user list and return only the users that can see the course module based on
* groups/permissions etc. It is assumed that the users are pre-filtered to those who are enrolled in the course.
*
* @category group
* @param stdClass|cm_info $cm The course module
* @param array $users An array of users, indexed by userid
* @return array A filtered list of users that can see the module, indexed by userid.
* @deprecated Since Moodle 2.8
*/
function groups_filter_users_by_course_module_visible($cm, $users) {
debugging('groups_filter_users_by_course_module_visible() is deprecated. ' .
'Replace with a call to \core_availability\info_module::filter_user_list(), ' .
'which does basically the same thing but includes other restrictions such ' .
'as profile restrictions.', DEBUG_DEVELOPER);
if (empty($users)) {
return $users;
}
// Since this function allows stdclass, let's play it safe and ensure we
// do have a cm_info.
if (!($cm instanceof cm_info)) {
$modinfo = get_fast_modinfo($cm->course);
$cm = $modinfo->get_cm($cm->id);
}
$info = new \core_availability\info_module($cm);
return $info->filter_user_list($users);
}

/**
* Determine if a course module is currently visible to a user
*
* Deprecated (it was never very useful as it only took into account the
* groupmembersonly option and no other way of hiding activities). Always
* returns true.
*
* @category group
* @param stdClass|cm_info $cm The course module
* @param int $userid The user to check against the group.
* @return bool True
* @deprecated Since Moodle 2.8
*/
function groups_course_module_visible($cm, $userid=null) {
debugging('groups_course_module_visible() is deprecated and always returns ' .
'true; use $cm->uservisible to decide whether the current user can ' .
'access an activity.', DEBUG_DEVELOPER);
return true;
}

/**
* Inndicates fatal error. This function was originally printing the
Expand Down
Loading

0 comments on commit 061e6b2

Please sign in to comment.