Skip to content
Browse files

MDL-29538 core_condition: Pre-integration merge cleanup

* Fixed up database installation and upgrade code
* Reverted some whitespace optimisations to minimise conflicts
* Optimised commits made by Mark to reduce complexity and add tracker issue numbers
  • Loading branch information...
1 parent 95a9bc0 commit 33e657c7e7d9c881143091d9de2a0215d71ec0f2 Sam Hemelryk committed with markn86 May 18, 2012
View
12 backup/moodle2/restore_stepslib.php
@@ -523,12 +523,10 @@ protected function define_execution() {
foreach($rs as $availrec) {
$allmatchesok = true;
// Get the complete availabilityobject
- $availability = restore_dbops::get_backup_ids_record($this->get_restoreid(),
- 'module_availability', $availrec->itemid)->info;
+ $availability = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'module_availability', $availrec->itemid)->info;
// Map the sourcecmid if needed and possible
if (!empty($availability->sourcecmid)) {
- $newcm = restore_dbops::get_backup_ids_record($this->get_restoreid(),
- 'course_module', $availability->sourcecmid);
+ $newcm = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'course_module', $availability->sourcecmid);
if ($newcm) {
$availability->sourcecmid = $newcm->newitemid;
} else {
@@ -537,8 +535,7 @@ protected function define_execution() {
}
// Map the gradeitemid if needed and possible
if (!empty($availability->gradeitemid)) {
- $newgi = restore_dbops::get_backup_ids_record($this->get_restoreid(),
- 'grade_item', $availability->gradeitemid);
+ $newgi = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'grade_item', $availability->gradeitemid);
if ($newgi) {
$availability->gradeitemid = $newgi->newitemid;
} else {
@@ -554,8 +551,7 @@ protected function define_execution() {
$params = array('backupid' => $this->get_restoreid(), 'itemname' => 'module_availability_field');
$rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid');
foreach($rs as $availrec) {
- $availability = restore_dbops::get_backup_ids_record($this->get_restoreid(),
- 'module_availability_field', $availrec->itemid)->info;
+ $availability = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'module_availability_field', $availrec->itemid)->info;
$DB->insert_record('course_modules_avail_fields', $availability);
}
$rs->close();
View
12 course/moodleform_mod.php
@@ -358,18 +358,19 @@ function validation($data, $files) {
}
}
- // Conditions: Verify that the user profile field has not been declared more than one
+ // Conditions: Verify that the user profile field has not been declared more than once
if (array_key_exists('conditionfieldgroup', $data)) {
// Array to store the existing fields
$arrcurrentfields = array();
- // Error message displayed if any condition is declared more than once
- $stralreadydeclaredwarning = get_string('fielddeclaredmultipletimes', 'condition');
+ // Error message displayed if any condition is declared more than once. We use lang string because
+ // this way we don't actually generate the string unless there is an error.
+ $stralreadydeclaredwarning = new lang_string('fielddeclaredmultipletimes', 'condition');
foreach ($data['conditionfieldgroup'] as $i => $fielddata) {
if ($fielddata['conditionfield'] == 0) { // Don't need to bother if none is selected
continue;
}
if (in_array($fielddata['conditionfield'], $arrcurrentfields)) {
- $errors["conditionfieldgroup[{$i}]"] = $stralreadydeclaredwarning;
+ $errors["conditionfieldgroup[{$i}]"] = $stralreadydeclaredwarning->out();
}
// Add the field to the array
$arrcurrentfields[] = $fielddata['conditionfield'];
@@ -572,8 +573,7 @@ function standard_coursemodule_elements(){
$grouparray[] =& $mform->createElement('select', 'conditionfieldoperator', '', $operators);
$grouparray[] =& $mform->createElement('text', 'conditionfieldvalue');
$mform->setType('conditionfieldvalue', PARAM_RAW);
- $group = $mform->createElement('group', 'conditionfieldgroup',
- get_string('userfield', 'condition'), $grouparray);
+ $group = $mform->createElement('group', 'conditionfieldgroup', get_string('userfield', 'condition'), $grouparray);
$this->repeat_elements(array($group), $fieldcount, array(), 'conditionfieldrepeats', 'conditionfieldadds', 2,
get_string('adduserfields', 'condition'), true);
View
35 lib/conditionlib.php
@@ -505,10 +505,10 @@ protected static function fill_availability_conditions_inner($item, $tableprefix
}
// For user fields
$sql = "SELECT cma.id as cmaid, cma.*, uf.*
- FROM {course_modules_avail_fields} cma
- LEFT JOIN {user_info_field} uf
- ON cma.customfieldid = uf.id
- WHERE coursemoduleid = :cmid";
+ FROM {course_modules_avail_fields} cma
+ LEFT JOIN {user_info_field} uf
+ ON cma.customfieldid = uf.id
+ WHERE coursemoduleid = :cmid";
if ($conditions = $DB->get_records_sql($sql, array('cmid' => $cm->id))) {
foreach ($conditions as $condition) {
// If the custom field is not empty, then
@@ -646,14 +646,12 @@ public function add_completion_condition($cmid, $requiredcompletion) {
/**
* Adds user fields condition
*
- * @global object
* @param mixed $field numeric if it is a user profile field, character
* if it is a column in the user table
* @param int $operator specifies the relationship between field and value
* @param char $value the value of the field
*/
public function add_user_field_condition($field, $operator, $value) {
- // Add to DB
global $DB;
$objavailfield = new stdClass;
@@ -1245,10 +1243,9 @@ private function get_cached_user_profile_field($userid, $fieldid, $grabthelot) {
if ($grabthelot) {
// Get all custom profile field values for user
$sql = "SELECT uf.id, ud.data
- FROM {user_info_field} uf
- LEFT JOIN {user_info_data} ud
- ON uf.id = ud.fieldid
- WHERE ud.userid = :userid";
+ FROM {user_info_field} uf
+ LEFT JOIN {user_info_data} ud ON uf.id = ud.fieldid
+ WHERE ud.userid = :userid";
if ($records = $DB->get_records_sql($sql, array('userid' => $USER->id))) {
foreach ($records as $r) {
$SESSION->userfieldcache[$r->id] = $r->data;
@@ -1257,11 +1254,10 @@ private function get_cached_user_profile_field($userid, $fieldid, $grabthelot) {
} else {
// Just get specified user field
$sql = "SELECT ud.data
- FROM {user_info_data} ud
- INNER JOIN {user_info_field} uf
- ON ud.fieldid = uf.id
- WHERE uf.id = :fieldid
- AND ud.userid = :userid";
+ FROM {user_info_data} ud
+ INNER JOIN {user_info_field} uf ON ud.fieldid = uf.id
+ WHERE uf.id = :fieldid
+ AND ud.userid = :userid";
if ($record = $DB->get_record_sql($sql, array('fieldid' => $fieldid, 'userid' => $USER->id))) {
$field = $record->data;
} else {
@@ -1281,11 +1277,10 @@ private function get_cached_user_profile_field($userid, $fieldid, $grabthelot) {
} else {
if ($iscustomprofilefield) {
$sql = "SELECT ud.data
- FROM {user_info_data} ud
- INNER JOIN {user_info_field} uf
- ON ud.fieldid = uf.id
- WHERE uf.id = :fieldid
- AND ud.userid = :userid";
+ FROM {user_info_data} ud
+ INNER JOIN {user_info_field} uf ON ud.fieldid = uf.id
+ WHERE uf.id = :fieldid
+ AND ud.userid = :userid";
if ($record = $DB->get_record_sql($sql, array('fieldid' => $fieldid, 'userid' => $userid))) {
return $record->data;
}
View
10 lib/db/install.xml
@@ -347,12 +347,12 @@
<KEY NAME="gradeitemid" TYPE="foreign" FIELDS="gradeitemid" REFTABLE="grade_items" REFFIELDS="id" PREVIOUS="sourcecmid"/>
</KEYS>
</TABLE>
- <TABLE NAME="course_modules_avail_fields" COMMENT="Table stores user field conditions that affect whether a module/activity is currently available to students or not." PREVIOUS="course_modules_availability" NEXT="course_modules_completion">
+ <TABLE NAME="course_modules_avail_fields" COMMENT="Stores user field conditions that affect whether an activity is currently available to students." PREVIOUS="course_modules_availability" NEXT="course_modules_completion">
<FIELDS>
- <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="coursemoduleid"/>
- <FIELD NAME="coursemoduleid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" COMMENT="ID of the module whose availability is being restricted by this condition." PREVIOUS="id" NEXT="userfield"/>
- <FIELD NAME="userfield" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false" COMMENT="The required value of the field" PREVIOUS="coursemoduleid" NEXT="customfieldid"/>
- <FIELD NAME="customfieldid" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" COMMENT="The required value of the user profile field" PREVIOUS="userfield" NEXT="operator"/>
+ <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="coursemoduleid"/>
+ <FIELD NAME="coursemoduleid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="ID of the module whose availability is being restricted by this condition." PREVIOUS="id" NEXT="userfield"/>
+ <FIELD NAME="userfield" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false" COMMENT="The user profile field this record relates to if it is not a custom profile field" PREVIOUS="coursemoduleid" NEXT="customfieldid"/>
+ <FIELD NAME="customfieldid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="ID for the custom field if this relates to one" PREVIOUS="userfield" NEXT="operator"/>
<FIELD NAME="operator" TYPE="char" LENGTH="20" NOTNULL="true" SEQUENCE="false" COMMENT="The operator, such as less than or equal to, between the field and the value" PREVIOUS="customfieldid" NEXT="value"/>
<FIELD NAME="value" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="The required value of the field" PREVIOUS="operator"/>
</FIELDS>
View
17 lib/db/upgrade.php
@@ -586,28 +586,33 @@ function xmldb_main_upgrade($oldversion) {
$table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
$table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
- // Define table course_modules_availability to be created
+ // Main savepoint reached
+ upgrade_main_savepoint(true, 2012051100.03);
+ }
+
+ if ($oldversion < 2012051700.02) {
+ // Define table course_modules_avail_fields to be created
$table = new xmldb_table('course_modules_avail_fields');
// Adding fields to table course_modules_avail_fields
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
- $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
+ $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
$table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
// Adding keys to table course_modules_avail_fields
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
- // Conditionally launch create table for course_modules_availability
+ // Conditionally launch create table for course_modules_avail_fields
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Main savepoint reached
- upgrade_main_savepoint(true, 2012051100.03);
+ upgrade_main_savepoint(true, 2012051700.02);
}
if ($oldversion < 2012052100.00) {
View
14 lib/moodlelib.php
@@ -4518,15 +4518,11 @@ function remove_course_contents($courseid, $showfeedback = true, array $options
// Remove all data from availability and completion tables that is associated
// with course-modules belonging to this course. Note this is done even if the
// features are not enabled now, in case they were enabled previously
- $DB->delete_records_select('course_modules_completion',
- 'coursemoduleid IN (SELECT id from {course_modules} WHERE course=?)',
- array($courseid));
- $DB->delete_records_select('course_modules_availability',
- 'coursemoduleid IN (SELECT id from {course_modules} WHERE course=?)',
- array($courseid));
- $DB->delete_records_select('course_modules_avail_fields',
- 'coursemoduleid IN (SELECT id from {course_modules} WHERE course = ?)',
- array($courseid));
+ $subquery = 'coursemoduleid IN (SELECT id from {course_modules} WHERE course = ?)';
+ $subqueryparam = array($courseid);
+ $DB->delete_records_select('course_modules_completion', $subquery, $subqueryparam);
+ $DB->delete_records_select('course_modules_availability', $subquery, $subqueryparam);
+ $DB->delete_records_select('course_modules_avail_fields', $subquery, $subqueryparam);
// Remove all data from gradebook - this needs to be done before course modules
// because while deleting this information, the system may need to reference
View
2 user/profile/definelib.php
@@ -291,8 +291,10 @@ function profile_delete_field($id) {
// Delete any module dependencies for this field
$DB->delete_records('course_modules_avail_fields', array('field' => $id));
+
// Need to rebuild course cache to update the info
rebuild_course_cache();
+
/// Try to remove the record from the database
$DB->delete_records('user_info_field', array('id'=>$id));

0 comments on commit 33e657c

Please sign in to comment.
Something went wrong with that request. Please try again.