From 33e657c7e7d9c881143091d9de2a0215d71ec0f2 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Fri, 18 May 2012 14:40:33 +1200 Subject: [PATCH] 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 --- backup/moodle2/restore_stepslib.php | 12 ++++------ course/moodleform_mod.php | 12 +++++----- lib/conditionlib.php | 35 +++++++++++++---------------- lib/db/install.xml | 10 ++++----- lib/db/upgrade.php | 17 +++++++++----- lib/moodlelib.php | 14 +++++------- user/profile/definelib.php | 2 ++ 7 files changed, 48 insertions(+), 54 deletions(-) diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 58cd26b10f0..27afd285df9 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -523,12 +523,10 @@ class restore_process_course_modules_availability extends restore_execution_step 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 @@ class restore_process_course_modules_availability extends restore_execution_step } // 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 @@ class restore_process_course_modules_availability extends restore_execution_step $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(); diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php index 50c62e9e6b6..376283a4004 100644 --- a/course/moodleform_mod.php +++ b/course/moodleform_mod.php @@ -358,18 +358,19 @@ abstract class moodleform_mod extends moodleform { } } - // 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 @@ abstract class moodleform_mod extends moodleform { $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); diff --git a/lib/conditionlib.php b/lib/conditionlib.php index 3d618a586f1..68e34dcbe94 100644 --- a/lib/conditionlib.php +++ b/lib/conditionlib.php @@ -505,10 +505,10 @@ abstract class condition_info_base { } // 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 @@ abstract class condition_info_base { /** * 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 @@ abstract class condition_info_base { 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 @@ abstract class condition_info_base { } 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 @@ abstract class condition_info_base { } 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; } diff --git a/lib/db/install.xml b/lib/db/install.xml index 7f2bb6c6cfc..24576c21352 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -347,12 +347,12 @@ - +
- - - - + + + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 6c8be26eb8e..af8e3603b6d 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -586,14 +586,19 @@ 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); @@ -601,13 +606,13 @@ function xmldb_main_upgrade($oldversion) { $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) { diff --git a/lib/moodlelib.php b/lib/moodlelib.php index b6601543ca5..bb5b5b07b53 100644 --- a/lib/moodlelib.php +++ b/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 diff --git a/user/profile/definelib.php b/user/profile/definelib.php index 5ff99855377..eb9c801c8e6 100644 --- a/user/profile/definelib.php +++ b/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));