Skip to content

Commit

Permalink
MDL-29538 core_condition: Pre-integration merge cleanup
Browse files Browse the repository at this point in the history
* 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
Sam Hemelryk authored and mdjnelson committed Jun 26, 2012
1 parent 95a9bc0 commit 33e657c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 54 deletions.
12 changes: 4 additions & 8 deletions backup/moodle2/restore_stepslib.php
Expand Up @@ -523,12 +523,10 @@ protected function define_execution() {
foreach($rs as $availrec) { foreach($rs as $availrec) {
$allmatchesok = true; $allmatchesok = true;
// Get the complete availabilityobject // Get the complete availabilityobject
$availability = restore_dbops::get_backup_ids_record($this->get_restoreid(), $availability = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'module_availability', $availrec->itemid)->info;
'module_availability', $availrec->itemid)->info;
// Map the sourcecmid if needed and possible // Map the sourcecmid if needed and possible
if (!empty($availability->sourcecmid)) { if (!empty($availability->sourcecmid)) {
$newcm = restore_dbops::get_backup_ids_record($this->get_restoreid(), $newcm = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'course_module', $availability->sourcecmid);
'course_module', $availability->sourcecmid);
if ($newcm) { if ($newcm) {
$availability->sourcecmid = $newcm->newitemid; $availability->sourcecmid = $newcm->newitemid;
} else { } else {
Expand All @@ -537,8 +535,7 @@ protected function define_execution() {
} }
// Map the gradeitemid if needed and possible // Map the gradeitemid if needed and possible
if (!empty($availability->gradeitemid)) { if (!empty($availability->gradeitemid)) {
$newgi = restore_dbops::get_backup_ids_record($this->get_restoreid(), $newgi = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'grade_item', $availability->gradeitemid);
'grade_item', $availability->gradeitemid);
if ($newgi) { if ($newgi) {
$availability->gradeitemid = $newgi->newitemid; $availability->gradeitemid = $newgi->newitemid;
} else { } else {
Expand All @@ -554,8 +551,7 @@ protected function define_execution() {
$params = array('backupid' => $this->get_restoreid(), 'itemname' => 'module_availability_field'); $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'module_availability_field');
$rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid'); $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid');
foreach($rs as $availrec) { foreach($rs as $availrec) {
$availability = restore_dbops::get_backup_ids_record($this->get_restoreid(), $availability = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'module_availability_field', $availrec->itemid)->info;
'module_availability_field', $availrec->itemid)->info;
$DB->insert_record('course_modules_avail_fields', $availability); $DB->insert_record('course_modules_avail_fields', $availability);
} }
$rs->close(); $rs->close();
Expand Down
12 changes: 6 additions & 6 deletions course/moodleform_mod.php
Expand Up @@ -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)) { if (array_key_exists('conditionfieldgroup', $data)) {
// Array to store the existing fields // Array to store the existing fields
$arrcurrentfields = array(); $arrcurrentfields = array();
// Error message displayed if any condition is declared more than once // Error message displayed if any condition is declared more than once. We use lang string because
$stralreadydeclaredwarning = get_string('fielddeclaredmultipletimes', 'condition'); // 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) { foreach ($data['conditionfieldgroup'] as $i => $fielddata) {
if ($fielddata['conditionfield'] == 0) { // Don't need to bother if none is selected if ($fielddata['conditionfield'] == 0) { // Don't need to bother if none is selected
continue; continue;
} }
if (in_array($fielddata['conditionfield'], $arrcurrentfields)) { if (in_array($fielddata['conditionfield'], $arrcurrentfields)) {
$errors["conditionfieldgroup[{$i}]"] = $stralreadydeclaredwarning; $errors["conditionfieldgroup[{$i}]"] = $stralreadydeclaredwarning->out();
} }
// Add the field to the array // Add the field to the array
$arrcurrentfields[] = $fielddata['conditionfield']; $arrcurrentfields[] = $fielddata['conditionfield'];
Expand Down Expand Up @@ -572,8 +573,7 @@ function standard_coursemodule_elements(){
$grouparray[] =& $mform->createElement('select', 'conditionfieldoperator', '', $operators); $grouparray[] =& $mform->createElement('select', 'conditionfieldoperator', '', $operators);
$grouparray[] =& $mform->createElement('text', 'conditionfieldvalue'); $grouparray[] =& $mform->createElement('text', 'conditionfieldvalue');
$mform->setType('conditionfieldvalue', PARAM_RAW); $mform->setType('conditionfieldvalue', PARAM_RAW);
$group = $mform->createElement('group', 'conditionfieldgroup', $group = $mform->createElement('group', 'conditionfieldgroup', get_string('userfield', 'condition'), $grouparray);
get_string('userfield', 'condition'), $grouparray);


$this->repeat_elements(array($group), $fieldcount, array(), 'conditionfieldrepeats', 'conditionfieldadds', 2, $this->repeat_elements(array($group), $fieldcount, array(), 'conditionfieldrepeats', 'conditionfieldadds', 2,
get_string('adduserfields', 'condition'), true); get_string('adduserfields', 'condition'), true);
Expand Down
35 changes: 15 additions & 20 deletions lib/conditionlib.php
Expand Up @@ -505,10 +505,10 @@ protected static function fill_availability_conditions_inner($item, $tableprefix
} }
// For user fields // For user fields
$sql = "SELECT cma.id as cmaid, cma.*, uf.* $sql = "SELECT cma.id as cmaid, cma.*, uf.*
FROM {course_modules_avail_fields} cma FROM {course_modules_avail_fields} cma
LEFT JOIN {user_info_field} uf LEFT JOIN {user_info_field} uf
ON cma.customfieldid = uf.id ON cma.customfieldid = uf.id
WHERE coursemoduleid = :cmid"; WHERE coursemoduleid = :cmid";
if ($conditions = $DB->get_records_sql($sql, array('cmid' => $cm->id))) { if ($conditions = $DB->get_records_sql($sql, array('cmid' => $cm->id))) {
foreach ($conditions as $condition) { foreach ($conditions as $condition) {
// If the custom field is not empty, then // If the custom field is not empty, then
Expand Down Expand Up @@ -646,14 +646,12 @@ public function add_completion_condition($cmid, $requiredcompletion) {
/** /**
* Adds user fields condition * Adds user fields condition
* *
* @global object
* @param mixed $field numeric if it is a user profile field, character * @param mixed $field numeric if it is a user profile field, character
* if it is a column in the user table * if it is a column in the user table
* @param int $operator specifies the relationship between field and value * @param int $operator specifies the relationship between field and value
* @param char $value the value of the field * @param char $value the value of the field
*/ */
public function add_user_field_condition($field, $operator, $value) { public function add_user_field_condition($field, $operator, $value) {
// Add to DB
global $DB; global $DB;


$objavailfield = new stdClass; $objavailfield = new stdClass;
Expand Down Expand Up @@ -1245,10 +1243,9 @@ private function get_cached_user_profile_field($userid, $fieldid, $grabthelot) {
if ($grabthelot) { if ($grabthelot) {
// Get all custom profile field values for user // Get all custom profile field values for user
$sql = "SELECT uf.id, ud.data $sql = "SELECT uf.id, ud.data
FROM {user_info_field} uf FROM {user_info_field} uf
LEFT JOIN {user_info_data} ud LEFT JOIN {user_info_data} ud ON uf.id = ud.fieldid
ON uf.id = ud.fieldid WHERE ud.userid = :userid";
WHERE ud.userid = :userid";
if ($records = $DB->get_records_sql($sql, array('userid' => $USER->id))) { if ($records = $DB->get_records_sql($sql, array('userid' => $USER->id))) {
foreach ($records as $r) { foreach ($records as $r) {
$SESSION->userfieldcache[$r->id] = $r->data; $SESSION->userfieldcache[$r->id] = $r->data;
Expand All @@ -1257,11 +1254,10 @@ private function get_cached_user_profile_field($userid, $fieldid, $grabthelot) {
} else { } else {
// Just get specified user field // Just get specified user field
$sql = "SELECT ud.data $sql = "SELECT ud.data
FROM {user_info_data} ud FROM {user_info_data} ud
INNER JOIN {user_info_field} uf INNER JOIN {user_info_field} uf ON ud.fieldid = uf.id
ON ud.fieldid = uf.id WHERE uf.id = :fieldid
WHERE uf.id = :fieldid AND ud.userid = :userid";
AND ud.userid = :userid";
if ($record = $DB->get_record_sql($sql, array('fieldid' => $fieldid, 'userid' => $USER->id))) { if ($record = $DB->get_record_sql($sql, array('fieldid' => $fieldid, 'userid' => $USER->id))) {
$field = $record->data; $field = $record->data;
} else { } else {
Expand All @@ -1281,11 +1277,10 @@ private function get_cached_user_profile_field($userid, $fieldid, $grabthelot) {
} else { } else {
if ($iscustomprofilefield) { if ($iscustomprofilefield) {
$sql = "SELECT ud.data $sql = "SELECT ud.data
FROM {user_info_data} ud FROM {user_info_data} ud
INNER JOIN {user_info_field} uf INNER JOIN {user_info_field} uf ON ud.fieldid = uf.id
ON ud.fieldid = uf.id WHERE uf.id = :fieldid
WHERE uf.id = :fieldid AND ud.userid = :userid";
AND ud.userid = :userid";
if ($record = $DB->get_record_sql($sql, array('fieldid' => $fieldid, 'userid' => $userid))) { if ($record = $DB->get_record_sql($sql, array('fieldid' => $fieldid, 'userid' => $userid))) {
return $record->data; return $record->data;
} }
Expand Down
10 changes: 5 additions & 5 deletions lib/db/install.xml
Expand Up @@ -347,12 +347,12 @@
<KEY NAME="gradeitemid" TYPE="foreign" FIELDS="gradeitemid" REFTABLE="grade_items" REFFIELDS="id" PREVIOUS="sourcecmid"/> <KEY NAME="gradeitemid" TYPE="foreign" FIELDS="gradeitemid" REFTABLE="grade_items" REFFIELDS="id" PREVIOUS="sourcecmid"/>
</KEYS> </KEYS>
</TABLE> </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> <FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="coursemoduleid"/> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="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="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 required value of the field" PREVIOUS="coursemoduleid" NEXT="customfieldid"/> <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" UNSIGNED="true" SEQUENCE="false" COMMENT="The required value of the user profile field" PREVIOUS="userfield" NEXT="operator"/> <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="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"/> <FIELD NAME="value" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="The required value of the field" PREVIOUS="operator"/>
</FIELDS> </FIELDS>
Expand Down
17 changes: 11 additions & 6 deletions lib/db/upgrade.php
Expand Up @@ -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('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
$table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', 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'); $table = new xmldb_table('course_modules_avail_fields');


// Adding fields to 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('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 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('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('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
$table->add_field('value', XMLDB_TYPE_CHAR, '255', 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 // Adding keys to table course_modules_avail_fields
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', 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)) { if (!$dbman->table_exists($table)) {
$dbman->create_table($table); $dbman->create_table($table);
} }


// Main savepoint reached // Main savepoint reached
upgrade_main_savepoint(true, 2012051100.03); upgrade_main_savepoint(true, 2012051700.02);
} }


if ($oldversion < 2012052100.00) { if ($oldversion < 2012052100.00) {
Expand Down
14 changes: 5 additions & 9 deletions lib/moodlelib.php
Expand Up @@ -4518,15 +4518,11 @@ function remove_course_contents($courseid, $showfeedback = true, array $options
// Remove all data from availability and completion tables that is associated // 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 // 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 // features are not enabled now, in case they were enabled previously
$DB->delete_records_select('course_modules_completion', $subquery = 'coursemoduleid IN (SELECT id from {course_modules} WHERE course = ?)';
'coursemoduleid IN (SELECT id from {course_modules} WHERE course=?)', $subqueryparam = array($courseid);
array($courseid)); $DB->delete_records_select('course_modules_completion', $subquery, $subqueryparam);
$DB->delete_records_select('course_modules_availability', $DB->delete_records_select('course_modules_availability', $subquery, $subqueryparam);
'coursemoduleid IN (SELECT id from {course_modules} WHERE course=?)', $DB->delete_records_select('course_modules_avail_fields', $subquery, $subqueryparam);
array($courseid));
$DB->delete_records_select('course_modules_avail_fields',
'coursemoduleid IN (SELECT id from {course_modules} WHERE course = ?)',
array($courseid));


// Remove all data from gradebook - this needs to be done before course modules // Remove all data from gradebook - this needs to be done before course modules
// because while deleting this information, the system may need to reference // because while deleting this information, the system may need to reference
Expand Down
2 changes: 2 additions & 0 deletions user/profile/definelib.php
Expand Up @@ -291,8 +291,10 @@ function profile_delete_field($id) {


// Delete any module dependencies for this field // Delete any module dependencies for this field
$DB->delete_records('course_modules_avail_fields', array('field' => $id)); $DB->delete_records('course_modules_avail_fields', array('field' => $id));

// Need to rebuild course cache to update the info // Need to rebuild course cache to update the info
rebuild_course_cache(); rebuild_course_cache();

/// Try to remove the record from the database /// Try to remove the record from the database
$DB->delete_records('user_info_field', array('id'=>$id)); $DB->delete_records('user_info_field', array('id'=>$id));


Expand Down

0 comments on commit 33e657c

Please sign in to comment.