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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions course/moodleform_mod.php
Original file line number Diff line number Diff line change
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)) {
// 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'];
Expand Down Expand Up @@ -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);
Expand Down
35 changes: 15 additions & 20 deletions lib/conditionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
17 changes: 11 additions & 6 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
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('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) {
Expand Down
14 changes: 5 additions & 9 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
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
// 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
Expand Down
2 changes: 2 additions & 0 deletions user/profile/definelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit 33e657c

Please sign in to comment.