Ejemplo n.º 1
0
 /**
  * Method to create ELIS field & owner objects given test data array.
  *
  * @param array $inputarray The test data array with params to build elis field object & owner
  *        input array format:
  *        array('field' => array(fieldparam => fieldparam_value [,...]),
  *              'context' => contextlevel,
  *              'manual' => array(fieldowners_manual_param => fomp_value [,...]),
  *              'moodle_profile' => array(fieldowners_moodleprofile_param => fompp_value [,...]),
  *        )
  * @return object The ELIS field object created
  */
 public function build_elis_field_data($inputarray)
 {
     $field = new field((object) $inputarray['field']);
     $field->save();
     $fieldcontext = new field_contextlevel();
     $fieldcontext->fieldid = $field->id;
     $fieldcontext->contextlevel = $inputarray['context'];
     $fieldcontext->save();
     if (isset($inputarray['manual'])) {
         $manual = new field_owner();
         $manual->fieldid = $field->id;
         $manual->plugin = 'manual';
         foreach ($inputarray['manual'] as $key => $val) {
             $manual->{'param_' . $key} = $val;
         }
         $manual->save();
     }
     if (isset($inputarray['moodle_profile'])) {
         $moodleprofile = new field_owner();
         $moodleprofile->fieldid = $field->id;
         $moodleprofile->plugin = 'moodle_profile';
         foreach ($inputarray['moodle_profile'] as $key => $val) {
             $moodleprofile->{$key} = $val;
         }
         $moodleprofile->save();
     }
     $field->load();
     // TDB.
     return $field;
 }
 /**
  * Set up necessary data
  *
  * @param int $numfields The number of custom fields used in auto-association
  */
 private function init_required_data($numfields = 1)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elis::file('eliscore/fields/moodleprofile/custom_fields.php');
     require_once elis::lib('data/customfield.class.php');
     require_once elispm::file('accesslib.php');
     require_once elispm::lib('data/userset.class.php');
     require_once $CFG->dirroot . '/user/profile/definelib.php';
     require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php';
     // Set up the category only once.
     $fieldcategory = new field_category(array('name' => 'testcategoryname'));
     $fieldcategory->save();
     // Ste up the target userset only once.
     $userset = new userset(array('name' => 'testusersetname'));
     $userset->save();
     for ($i = 1; $i <= $numfields; $i++) {
         // Custom field.
         $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'testfieldshortname' . $i, 'name' => 'testfieldname' . $i, 'datatype' => 'bool'));
         $field->save();
         // Ensure manual field owner exists for syncing.
         field_owner::ensure_field_owner_exists($field, 'manual');
         $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual'));
         $owner = new field_owner($ownerid);
         $owner->param_control = 'checkbox';
         $owner->save();
         // Ensure moodle profile field owner exists.
         field_owner::ensure_field_owner_exists($field, 'moodle_profile');
         $DB->execute("UPDATE {" . field_owner::TABLE . "} SET exclude = ?", array(pm_moodle_profile::sync_to_moodle));
         // Field context level assocation.
         $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER));
         $fieldcontextlevel->save();
         // The associated Moodle user profile field.
         $profiledefinecheckbox = new profile_define_checkbox();
         $data = new stdClass();
         $data->datatype = 'checkbox';
         $data->categoryid = 99999;
         $data->shortname = 'testfieldshortname' . $i;
         $data->name = 'testfieldname' . $i;
         $profiledefinecheckbox->define_save($data);
         $mfield = $DB->get_record('user_info_field', array('shortname' => 'testfieldshortname' . $i));
         // The "cluster-profile" association.
         $usersetprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $mfield->id, 'value' => 1));
         $usersetprofile->save();
     }
 }
Ejemplo n.º 3
0
 /**
  * Test sync-ing an ELIS User Profile field to a DELETED Moodle User Profile field
  */
 public function test_syncpmuserfieldtodeletedmoodleprofilefield()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/user/profile/definelib.php';
     $this->load_csv_data();
     // Set PM Custom User field(s) to Sync to Moodle.
     $ctxlvl = CONTEXT_ELIS_USER;
     $fields = field::get_for_context_level($ctxlvl);
     foreach ($fields as $field) {
         $fieldobj = new field($field);
         if (!isset($fieldobj->owners['moodle_profile'])) {
             $fieldobj->owners['moodle_profile'] = new stdClass();
         }
         $owner = new field_owner($fieldobj->owners['moodle_profile']);
         $owner->exclude = pm_moodle_profile::sync_from_moodle;
         $owner->save();
         $fieldobj->save();
     }
     // Read a record.
     $src = new user(103, null, array(), false, array());
     $src->reset_custom_field_list();
     // Modify the data.
     $src->firstname = 'Testuser';
     $src->lastname = 'One';
     $src->field_sometext = 'boo';
     $src->field_sometextfrompm = 'bla';
     $src->save();
     // Delete some custom Moodle Profile field(s) to cause old error (pre ELIS-4499).
     $fields = field::get_for_context_level($ctxlvl);
     foreach ($fields as $field) {
         $fieldobj = new field($field);
         if ($moodlefield = $DB->get_record('user_info_field', array('shortname' => $fieldobj->shortname))) {
             profile_delete_field($moodlefield->id);
         }
     }
     // Run the library sync - throws errors not exceptions :(.
     $CFG->mnet_localhost_id = 1;
     // ???
     $mu = cm_get_moodleuser(103);
     try {
         $result = pm_moodle_user_to_pm($mu);
         $this->assertTrue($result);
     } catch (Exception $ex) {
         $this->assertTrue(false, $ex->message);
     }
 }
Ejemplo n.º 4
0
/**
 * Run all upgrade steps from before elis 2.6.
 *
 * @param int $oldversion The currently installed version of the old component.
 * @return bool Success/Failure.
 */
function elisprogram_usetthemes_pre26upgradesteps($oldversion)
{
    global $CFG, $THEME, $DB;
    $dbman = $DB->get_manager();
    $result = true;
    if ($oldversion < 2011071300) {
        // Rename fields.
        $fieldnames = array('theme', 'themepriority');
        foreach ($fieldnames as $fieldname) {
            $field = field::find(new field_filter('shortname', 'cluster_' . $fieldname));
            if ($field->valid()) {
                $field = $field->current();
                $field->shortname = '_elis_userset_' . $fieldname;
                $field->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011071300, 'pmplugins', 'userset_themes');
    }
    if ($result && $oldversion < 2011101800) {
        // Userset -> 'User Set'.
        $fieldnames = array('theme', 'themepriority');
        foreach ($fieldnames as $fieldname) {
            $fname = '_elis_userset_' . $fieldname;
            $field = field::find(new field_filter('shortname', $fname));
            if ($field->valid()) {
                $field = $field->current();
                // Add help file.
                if ($owner = new field_owner(!isset($field->owners) || !isset($field->owners['manual']) ? false : $field->owners['manual'])) {
                    $owner->fieldid = $field->id;
                    $owner->plugin = 'manual';
                    //$owner->exclude = 0; // TBD
                    $owner->param_help_file = "elisprogram_usetthemes/{$fname}";
                    $owner->save();
                }
                $category = $field->category;
                if (stripos($category->name, 'Userset') !== false) {
                    $category->name = str_ireplace('Userset', 'User Set', $category->name);
                    $category->save();
                }
            }
        }
        upgrade_plugin_savepoint($result, 2011101800, 'pmplugins', 'userset_themes');
    }
    if ($oldversion < 2013020400) {
        // Rename field if it is still 'Cluster Theme'.
        $field = field::find(new field_filter('shortname', '_elis_userset_theme'));
        if ($field->valid()) {
            $field = $field->current();
            $category = $field->category;
            if ($category->name == 'Cluster Theme') {
                // The field name hasn't been changed from the old default.
                $category->name = get_string('userset_theme_category', 'elisprogram_usetthemes');
                $category->save();
            }
        }
        upgrade_plugin_savepoint($result, 2013020400, 'pmplugins', 'userset_themes');
    }
    return $result;
}
Ejemplo n.º 5
0
/**
 * moodle_profile_field_save_form_data - saves form specific sync settings for moodle profile fields
 *
 * @param object $form        the custom field form object submitted
 * @param object $field       the field object to save moodle_profile field_owner object for
 * @param object $data        the submitted form data to save
 * @uses  $DB
 */
function moodleprofile_field_save_form_data($form, $field, $data)
{
    global $DB;
    $level = required_param('level', PARAM_ACTION);
    if ($level != 'user' || !isset($data->moodle_profile_exclusive)) {
        return;
        // not user context or sync was disabled
    }
    if ($data->moodle_profile_exclusive == pm_moodle_profile::sync_to_moodle || $data->moodle_profile_exclusive == pm_moodle_profile::sync_from_moodle) {
        if (isset($field->owners['moodle_profile'])) {
            $owner = new field_owner($field->owners['moodle_profile']);
            $owner->exclude = $data->moodle_profile_exclusive;
        } else {
            $owner = new field_owner();
            $owner->fieldid = $field->id;
            $owner->plugin = 'moodle_profile';
            $owner->exclude = $data->moodle_profile_exclusive;
        }
        $owner->save();
        unset($field->owners);
        // force reload of owners field
        sync_profile_field_with_moodle($field);
    } else {
        $DB->delete_records(field_owner::TABLE, array('fieldid' => $field->id, 'plugin' => 'moodle_profile'));
    }
}
Ejemplo n.º 6
0
/**
 * Upgrade old ELIS tables.
 *
 * @param int $oldversion The old ELIS version.
 * @return bool Success/Failure.
 */
function local_elisprogram_upgrade_old_tables($oldversion)
{
    global $DB, $CFG;
    $dbman = $DB->get_manager();
    $result = true;
    if ($result && $oldversion < 2013031400) {
        // ELIS-8066: remove blank/empty menu options from custom field menu/checkbox and defaults using them.
        $customfields = $DB->get_recordset('local_eliscore_field', null, '', 'id');
        foreach ($customfields as $id => $unused) {
            $field = new field($id);
            $field->load();
            if (isset($field->owners['manual'])) {
                $manual = new field_owner($field->owners['manual']);
                $control = $manual->param_control;
                $options = $manual->param_options;
                if (!empty($options) && empty($manual->param_options_source) && ($control == 'menu' || $control == 'checkbox')) {
                    $options = str_replace("\r", '', $options);
                    // Strip CRs.
                    $options = preg_replace("/\n+/", "\n", $options);
                    $manual->param_options = rtrim($options, "\n");
                    $manual->save();
                    // Remove any empty defaults.
                    $DB->delete_records_select($field->data_table(), "contextid IS NULL AND fieldid = ? AND data = ''", array($id));
                }
            }
        }
        upgrade_plugin_savepoint($result, 2013031400, 'elis', 'program');
    }
    // ELIS-7780: remove deprecated capabilites.
    if ($result && $oldversion < 2013041900) {
        $capstodelete = array('elis/program:viewgroupreports', 'elis/program:viewreports');
        list($inorequal, $params) = $DB->get_in_or_equal($capstodelete);
        $where = "capability {$inorequal}";
        $DB->delete_records_select('role_capabilities', $where, $params);
        $where = "name {$inorequal}";
        $DB->delete_records_select('capabilities', $where, $params);
        upgrade_plugin_savepoint($result, 2013041900, 'elis', 'program');
    }
    // Remove any duplicate user track records before attempting to apply an index.
    pm_fix_duplicate_usertrack_records('crlm_user_track');
    if ($result && $oldversion < 2013042900) {
        // Add indexes to {crlm_user_track} table.
        $table = new xmldb_table('crlm_user_track');
        if ($dbman->table_exists($table)) {
            // Array of indexes to drop.
            $dropindexes = array(new xmldb_index('any_userid_ix', XMLDB_INDEX_UNIQUE, array('userid')), new xmldb_index('any_trackid_ix', XMLDB_INDEX_UNIQUE, array('trackid')), new xmldb_index('any_userid_trackid_ix', XMLDB_INDEX_NOTUNIQUE, array('userid', 'trackid')));
            foreach ($dropindexes as $index) {
                // Drop unwanted indexes if they exist.
                if ($dbman->index_exists($table, $index)) {
                    $dbman->drop_index($table, $index);
                }
            }
            // Array of indexes to create.
            $createindexes = array(new xmldb_index('userid_ix', XMLDB_INDEX_NOTUNIQUE, array('userid')), new xmldb_index('trackid_ix', XMLDB_INDEX_NOTUNIQUE, array('trackid')), new xmldb_index('userid_trackid_ix', XMLDB_INDEX_UNIQUE, array('userid', 'trackid')));
            foreach ($createindexes as $index) {
                // Create desired indexes as required.
                if (!$dbman->index_exists($table, $index)) {
                    $dbman->add_index($table, $index);
                }
            }
        }
        upgrade_plugin_savepoint($result, 2013042900, 'elis', 'program');
    }
    if ($result && $oldversion < 2013051500) {
        // Change results engine action min/max fields from integer to float.
        $table = new xmldb_table('crlm_results_action');
        if ($dbman->table_exists($table)) {
            $field = new xmldb_field('minimum', XMLDB_TYPE_NUMBER, '10,5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'actiontype');
            $dbman->change_field_type($table, $field);
            $field = new xmldb_field('maximum', XMLDB_TYPE_NUMBER, '10,5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'minimum');
            $dbman->change_field_type($table, $field);
        }
        upgrade_plugin_savepoint($result, 2013051500, 'elis', 'program');
    }
    if ($result && $oldversion < 2013051502) {
        // Define table crlm_certificate_settings to be created
        // Conditionally launch create table for crlm_certificate_settings.
        $table = new xmldb_table('crlm_certificate_settings');
        if (!$dbman->table_exists($table)) {
            // Adding fields to table crlm_certificate_settings.
            $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
            $table->add_field('entity_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
            $table->add_field('entity_type', XMLDB_TYPE_CHAR, '9', null, XMLDB_NOTNULL, null, null);
            $table->add_field('cert_border', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
            $table->add_field('cert_seal', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
            $table->add_field('cert_template', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
            $table->add_field('disable', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');
            $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
            $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
            // Adding keys to table crlm_certificate_settings.
            $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
            // Adding indexes to table crlm_certificate_settings.
            $table->add_index('ent_id_type_ix', XMLDB_INDEX_UNIQUE, array('entity_id', 'entity_type'));
            $dbman->create_table($table);
        }
        // Define table crlm_certificate_issued to be created.
        $table = new xmldb_table('crlm_certificate_issued');
        // Conditionally launch create table for crlm_certificate_issued.
        if (!$dbman->table_exists($table)) {
            // Adding fields to table crlm_certificate_issued.
            $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
            $table->add_field('cm_userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
            $table->add_field('cert_setting_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
            $table->add_field('cert_code', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
            $table->add_field('timeissued', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
            $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
            // Adding keys to table crlm_certificate_issued.
            $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
            // Adding indexes to table crlm_certificate_issued.
            $table->add_index('cert_code_ix', XMLDB_INDEX_UNIQUE, array('cert_code'));
            $table->add_index('cm_userid_ix', XMLDB_INDEX_NOTUNIQUE, array('cm_userid'));
            $dbman->create_table($table);
        }
        // ELIS savepoint reached.
        upgrade_plugin_savepoint($result, 2013051502, 'elis', 'program');
    }
    // ELIS-8528: remove orphaned LOs.
    if ($result && $oldversion < 2013051503) {
        $where = 'NOT EXISTS (SELECT \'x\' FROM {crlm_course} cc WHERE cc.id = {crlm_course_completion}.courseid)';
        $DB->delete_records_select('crlm_course_completion', $where);
        upgrade_plugin_savepoint($result, 2013051503, 'elis', 'program');
    }
    if ($result && $oldversion < 2013082100) {
        // Change password field length to 255.
        $table = new xmldb_table('crlm_user');
        $field = new xmldb_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'username');
        $dbman->change_field_precision($table, $field);
        upgrade_plugin_savepoint($result, 2013082100, 'elis', 'program');
    }
    if ($result && $oldversion < 2013082101) {
        require_once elis::file('eliscore/fields/manual/custom_fields.php');
        require_once elis::file('eliscore/fields/moodleprofile/custom_fields.php');
        $fieldcat = field_category::ensure_exists_for_contextlevel(get_string('moodlefields', 'local_elisprogram'), CONTEXT_ELIS_USER);
        $sql = 'SELECT m.id
                  FROM {user_info_field} m
                 WHERE NOT EXISTS(
                           SELECT *
                             FROM {local_eliscore_field} e
                            WHERE e.shortname = m.shortname
                       )';
        $fieldstosync = $DB->get_records_sql($sql);
        foreach ($fieldstosync as $mfield) {
            // Create field.
            $efield = field::make_from_moodle_field($mfield->id, $fieldcat, pm_moodle_profile::sync_from_moodle);
            // Sync profile field information from Moodle into ELIS.
            sync_profile_field_from_moodle($efield);
        }
        upgrade_plugin_savepoint($result, 2013082101, 'elis', 'program');
    }
    if ($result && $oldversion < 2013082103) {
        // ELIS-8441 & ELIS-8569: Fix Program timetocomplete & frequency defaults of '0y'.
        $table = new xmldb_table('crlm_curriculum');
        if ($dbman->table_exists($table)) {
            $field = new xmldb_field('timetocomplete', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null, 'timemodified');
            $dbman->change_field_default($table, $field);
            $field = new xmldb_field('frequency', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null, 'timetocomplete');
            $dbman->change_field_default($table, $field);
            $sql = 'UPDATE {crlm_curriculum} SET timetocomplete = "" WHERE timetocomplete = "0y"';
            $DB->execute($sql);
            $sql = 'UPDATE {crlm_curriculum} SET frequency = "" WHERE frequency = "0y"';
            $DB->execute($sql);
        }
        upgrade_plugin_savepoint($result, 2013082103, 'elis', 'program');
    }
    if ($result && $oldversion < 2013082104) {
        require_once dirname(__FILE__) . '/../../../lib/lib.php';
        pm_set_config('notify_addedtowaitlist_user', 1);
        pm_set_config('notify_enroledfromwaitlist_user', 1);
        pm_set_config('notify_incompletecourse_user', 1);
        upgrade_plugin_savepoint($result, 2013082104, 'elis', 'program');
    }
    return $result;
}
Ejemplo n.º 7
0
/**
 * Run all upgrade steps from before elis 2.6.
 *
 * @param int $oldversion The currently installed version of the old component.
 * @return bool Success/Failure.
 */
function elisprogram_usetdisppriority_pre26upgradesteps($oldversion)
{
    global $CFG, $THEME, $DB;
    $dbman = $DB->get_manager();
    $result = true;
    if ($oldversion < 2011071200) {
        // Rename field.
        $field = field::find(new field_filter('shortname', '_elis_cluster_display_priority'));
        if ($field->valid()) {
            $field = $field->current();
            $field->shortname = USERSET_DISPLAY_PRIORITY_FIELD;
            $field->save();
            $category = $field->category;
            if ($category->name == 'Cluster Display Settings') {
                // The field name hasn't been changed from the old default.
                $category->name = get_string('display_settings_category_name', 'elisprogram_usetdisppriority');
                $category->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011071200, 'pmplugins', 'userset_display_priority');
    }
    if ($result && $oldversion < 2011101200) {
        $field = field::find(new field_filter('shortname', USERSET_DISPLAY_PRIORITY_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            if ($owner = new field_owner(!isset($field->owners) || !isset($field->owners['manual']) ? false : $field->owners['manual'])) {
                $owner->fieldid = $field->id;
                $owner->plugin = 'manual';
                //$owner->exclude = 0; // TBD
                $owner->param_help_file = 'elisprogram_usetdisppriority/display_priority';
                $owner->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011101200, 'pmplugins', 'userset_display_priority');
    }
    if ($result && $oldversion < 2011101800) {
        // Userset -> 'User Set'.
        $field = field::find(new field_filter('shortname', USERSET_DISPLAY_PRIORITY_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            $category = $field->category;
            if (stripos($category->name, 'Userset') !== false) {
                $category->name = str_ireplace('Userset', 'User Set', $category->name);
                $category->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011101800, 'pmplugins', 'userset_display_priority');
    }
    return $result;
}
Ejemplo n.º 8
0
/**
 * Run all upgrade steps from before elis 2.6.
 *
 * @param int $oldversion The currently installed version of the old component.
 * @return bool Success/Failure.
 */
function elisprogram_usetclassify_pre26upgradesteps($oldversion)
{
    global $CFG, $THEME, $DB;
    $dbman = $DB->get_manager();
    $result = true;
    if ($oldversion < 2011071400) {
        // Rename field.
        $field = field::find(new field_filter('shortname', '_elis_cluster_classification'));
        if ($field->valid()) {
            $field = $field->current();
            $field->shortname = USERSET_CLASSIFICATION_FIELD;
            if ($field->name == 'Cluster classification') {
                // Rhe field name hasn't been changed from the old default.
                $field->name = get_string('classification_field_name', 'elisprogram_usetclassify');
            }
            $field->save();
            $category = $field->category;
            if ($category->name == 'Cluster classification') {
                // The field name hasn't been changed from the old default.
                $category->name = get_string('classification_category_name', 'elisprogram_usetclassify');
                $category->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011071400, 'pmplugins', 'userset_classification');
    }
    if ($result && $oldversion < 2011101200) {
        $field = field::find(new field_filter('shortname', USERSET_CLASSIFICATION_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            if ($owner = new field_owner(!isset($field->owners) || !isset($field->owners['manual']) ? false : $field->owners['manual'])) {
                $owner->fieldid = $field->id;
                $owner->plugin = 'manual';
                //$owner->exclude = 0; // TBD
                $owner->param_help_file = 'elisprogram_usetclassify/cluster_classification';
                $owner->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011101200, 'pmplugins', 'userset_classification');
    }
    if ($result && $oldversion < 2011101800) {
        // Userset -> 'User Set'.
        $field = field::find(new field_filter('shortname', USERSET_CLASSIFICATION_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            if (stripos($field->name, 'Userset') !== false) {
                $field->name = str_ireplace('Userset', 'User Set', $field->name);
                $field->save();
            }
            $category = $field->category;
            if (stripos($category->name, 'Userset') !== false) {
                $category->name = str_ireplace('Userset', 'User Set', $category->name);
                $category->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011101800, 'pmplugins', 'userset_classification');
    }
    if ($result && $oldversion < 2011110300) {
        // Make sure to rename the default classification name from "Cluster" to "User set".
        require_once elispm::file('plugins/usetclassify/usersetclassification.class.php');
        // Make sure there are no custom fields with invalid categories.
        pm_fix_orphaned_fields();
        $field = field::find(new field_filter('shortname', USERSET_CLASSIFICATION_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            $category = $field->category;
            $default = usersetclassification::find(new field_filter('shortname', 'regular'));
            if ($default->valid()) {
                $default = $default->current();
                $default->name = get_string('cluster', 'local_elisprogram');
                $default->save();
            }
            // Upgrade field owner data for the default User Set field.
            $field = field::ensure_field_exists_for_context_level($field, CONTEXT_ELIS_USERSET, $category);
            $owners = field_owner::find(new field_filter('fieldid', $field->id));
            if ($owners->valid()) {
                foreach ($owners as $owner) {
                    if ($owner->plugin == 'cluster_classification') {
                        $owner->plugin = 'userset_classification';
                        $owner->save();
                    } else {
                        if ($owner->plugin == 'manual') {
                            $owner->param_options_source = 'userset_classifications';
                            $owner->param_help_file = 'elisprogram_usetclassify/cluster_classification';
                            $owner->save();
                        }
                    }
                }
            }
            upgrade_plugin_savepoint($result, 2011110300, 'pmplugins', 'userset_classification');
        }
    }
    return $result;
}
Ejemplo n.º 9
0
 /**
  * Create a moodle user custom field.
  * @param field_category $cat The category to create the field in.
  * @return field The created field.
  */
 public function create_user_field(field_category $cat)
 {
     global $DB;
     // Create category.
     $mcat = (object) array('name' => $cat->name, 'sortorder' => 1);
     $mcat->id = $DB->insert_record('user_info_category', $mcat);
     // Create field.
     $field = (object) array('shortname' => 'user_testfield', 'name' => 'User Test Field', 'datatype' => 'text', 'description' => '', 'descriptionformat' => 1, 'categoryid' => $mcat->id, 'sortorder' => 1, 'required' => 0, 'locked' => 0, 'visible' => 1, 'forceunique' => 0, 'signup' => 0, 'defaultdata' => '', 'defaultdataformat' => 0);
     $field->id = $DB->insert_record('user_info_field', $field);
     $manualowneroptions = array('required' => 0, 'edit_capability' => '', 'view_capability' => '', 'control' => 'text', 'columns' => 30, 'rows' => 10, 'maxlength' => 100);
     $field = new field();
     $field->shortname = 'user_testfield';
     $field->name = 'User Test Field';
     $field->datatype = 'char';
     field::ensure_field_exists_for_context_level($field, CONTEXT_ELIS_USER, $cat);
     field_owner::ensure_field_owner_exists($field, 'manual', $manualowneroptions);
     $owner = new field_owner();
     $owner->fieldid = $field->id;
     $owner->plugin = 'moodleprofile';
     $owner->exclude = pm_moodle_profile::sync_to_moodle;
     $owner->save();
     return $field;
 }
Ejemplo n.º 10
0
/**
 * Run all upgrade steps from before elis 2.6.
 *
 * @param int $oldversion The currently installed version of the old component.
 * @return bool Success/Failure.
 */
function elisprogram_preposttest_pre26upgradesteps($oldversion)
{
    $result = true;
    if ($result && $oldversion < 2011101200) {
        $field = field::find(new field_filter('shortname', PRE_TEST_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            if ($owner = new field_owner(!isset($field->owners) || !isset($field->owners['manual']) ? false : $field->owners['manual'])) {
                $owner->fieldid = $field->id;
                $owner->plugin = 'manual';
                //$owner->exclude = 0; // TBD
                $owner->param_help_file = 'elisprogram_preposttest/pre_test';
                $owner->save();
            }
        }
        $field = field::find(new field_filter('shortname', POST_TEST_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            if ($owner = new field_owner(!isset($field->owners) || !isset($field->owners['manual']) ? false : $field->owners['manual'])) {
                $owner->fieldid = $field->id;
                $owner->plugin = 'manual';
                //$owner->exclude = 0; // TBD
                $owner->param_help_file = 'elisprogram_preposttest/post_test';
                $owner->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011101200, 'pmplugins', 'pre_post_test');
    }
    return $result;
}
Ejemplo n.º 11
0
 /**
  * Set up an ELIS custom field from a Moodle custom field.
  * @param object $mfield The moodle custom field object to use as reference.
  * @param field_category $cat The ELIS custom field category to put the field in.
  * @return field The created ELIS custom field.
  */
 protected function set_up_elis_custom_field($mfield, field_category $cat)
 {
     if (empty($cat)) {
         $cat = $this->set_up_elis_field_category();
     }
     $user = new user();
     $user->reset_custom_field_list();
     $field = new field();
     $field->shortname = $mfield->shortname;
     $field->name = $mfield->name;
     $field->datatype = $mfield->datatype;
     field::ensure_field_exists_for_context_level($field, CONTEXT_ELIS_USER, $cat);
     $owner = new field_owner();
     $owner->fieldid = $field->id;
     $owner->plugin = 'manual';
     $owner->params = serialize(array('required' => false, 'edit_capability' => '', 'view_capability' => '', 'control' => 'text', 'columns' => 30, 'rows' => 10, 'maxlength' => 2048, 'startyear' => '1970', 'stopyear' => '2038', 'inctime' => '0'));
     $owner->save();
     $owner = new field_owner();
     $owner->fieldid = $field->id;
     $owner->plugin = 'moodle_profile';
     $owner->exclude = pm_moodle_profile::sync_to_moodle;
     $owner->save();
     return $field;
 }
Ejemplo n.º 12
0
 /**
  * Validate that the import performs user synchronization on user update
  */
 public function test_version1importsyncsusertoelisonupdate()
 {
     global $CFG, $DB;
     if (!file_exists($CFG->dirroot . '/local/elisprogram/lib/setup.php')) {
         $this->markTestIncomplete('This test depends on the PM system');
     }
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once $CFG->dirroot . '/user/profile/definelib.php';
     require_once elis::lib('data/customfield.class.php');
     require_once elispm::lib('data/user.class.php');
     // Make sure we are not auto-assigning idnumbers.
     set_config('auto_assign_user_idnumber', 0, 'local_elisprogram');
     elis::$config = new elis_config();
     // Create Moodle custom field category.
     $category = new stdClass();
     $category->sortorder = $DB->count_records('user_info_category') + 1;
     $category->id = $DB->insert_record('user_info_category', $category);
     // Create Moodle custom profile field.
     $this->create_profile_field('rliptext', 'text', $category->id);
     // Obtain the PM user context level.
     $contextlevel = CONTEXT_ELIS_USER;
     // Make sure the PM category and field exist.
     $category = new field_category(array('name' => 'rlipcategory'));
     $field = new field(array('shortname' => 'rliptext', 'name' => 'rliptext', 'datatype' => 'text', 'multivalued' => 0));
     $field = field::ensure_field_exists_for_context_level($field, $contextlevel, $category);
     // Make sure the field owner is setup.
     field_owner::ensure_field_owner_exists($field, 'manual');
     $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual'));
     $owner = new field_owner($ownerid);
     $owner->param_control = 'text';
     $owner->save();
     // Make sure the field is set up for synchronization.
     field_owner::ensure_field_owner_exists($field, 'moodle_profile');
     $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'moodle_profile'));
     $owner = new field_owner($ownerid);
     $owner->exclude = pm_moodle_profile::sync_from_moodle;
     $owner->save();
     // Update the user class's static cache of define user custom fields.
     $tempuser = new user();
     $tempuser->reset_custom_field_list();
     // Create the user.
     $this->run_core_user_import(array('idnumber' => 'rlipidnumber', 'profile_field_rliptext' => 'rliptext'));
     // Make sure PM user was created correctly.
     $this->assert_record_exists(user::TABLE, array('username' => 'rlipusername', 'idnumber' => 'rlipidnumber'));
     // Run the import, updating the user.
     $this->run_core_user_import(array('action' => 'update', 'username' => 'rlipusername', 'profile_field_rliptext' => 'rliptextupdated'));
     // Make sure the PM custom field data was set.
     $sql = "SELECT 'x'\n                  FROM {" . field::TABLE . "} f\n                  JOIN {" . field_data_text::TABLE . "} d ON f.id = d.fieldid\n                 WHERE f.shortname = ? AND d.data = ?";
     $params = array('rliptext', 'rliptextupdated');
     $exists = $DB->record_exists_sql($sql, $params);
     $this->assertEquals($exists, true);
 }
Ejemplo n.º 13
0
function manual_field_save_form_data($form, $field, $data)
{
    if (isset($data->manual_field_enabled) && $data->manual_field_enabled) {
        if (isset($field->owners['manual'])) {
            $manual = new field_owner($field->owners['manual']);
        } else {
            $manual = new field_owner();
            $manual->fieldid = $field->id;
            $manual->plugin = 'manual';
        }
        if (isset($data->manual_field_required)) {
            $manual->param_required = $data->manual_field_required;
        } else {
            $manual->param_required = false;
        }
        $parameters = array('edit_capability', 'view_capability', 'control', 'options_source', 'options', 'columns', 'rows', 'maxlength', 'help_file', 'startyear', 'stopyear', 'inctime');
        foreach ($parameters as $param) {
            $dataname = "manual_field_{$param}";
            if (isset($data->{$dataname})) {
                $manual->{"param_{$param}"} = $data->{$dataname};
            }
        }
        $manual->save();
    } else {
        global $DB;
        $DB->delete_records(field_owner::TABLE, array('fieldid' => $field->id, 'plugin' => 'manual'));
    }
}
Ejemplo n.º 14
0
/**
 * Run all upgrade steps from before elis 2.6.
 *
 * @param int $oldversion The currently installed version of the old component.
 * @return bool Success/Failure.
 */
function elisprogram_archive_pre26upgradesteps($oldversion)
{
    $result = true;
    if ($result && $oldversion < 2011100700) {
        // rename field
        $field = field::find(new field_filter('shortname', '_elis_curriculum_archive'));
        if ($field->valid()) {
            $field = $field->current();
            $field->shortname = ARCHIVE_FIELD;
            $field->name = get_string('archive_field_name', 'elisprogram_archive');
            $field->save();
        }
        upgrade_plugin_savepoint($result, 2011100700, 'pmplugins', 'archive');
    }
    if ($result && $oldversion < 2011101200) {
        $field = field::find(new field_filter('shortname', ARCHIVE_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            if ($owner = new field_owner(!isset($field->owners) || !isset($field->owners['manual']) ? false : $field->owners['manual'])) {
                $owner->fieldid = $field->id;
                $owner->plugin = 'manual';
                //$owner->exclude = 0; // TBD
                $owner->param_help_file = 'elisprogram_archive/archive_program';
                $owner->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011101200, 'pmplugins', 'archive');
    }
    return $result;
}
Ejemplo n.º 15
0
 /**
  * Creates the owner record corresponding to the supplied field if it does not already exist
  *
  * @param   field   $field   The field to create the owner for
  * @param   string  $plugin  The plugin used for the owner field
  * @param   array   $params  Any additional parameters to pass to the owner record
  */
 public static function ensure_field_owner_exists(field $field, $plugin, array $params = array())
 {
     $owners = $field->owners;
     if (!empty($owners[$plugin])) {
         return;
     }
     $owner = new field_owner();
     $owner->fieldid = $field->id;
     $owner->plugin = $plugin;
     $owner->params = serialize($params);
     $owner->save();
 }
Ejemplo n.º 16
0
 /**
  * Set up data that is needed for testing
  *
  * @param boolean $setcustomfielddata Specify whether the userset's custom fields should be set
  * @param boolean $assignuser Specify whether the user should be directly assigned to the user set
  * @param boolean $setautoassociatefields Specity whether we should set up fields that allow userset autoassociation
  */
 private function set_up_required_data($setcustomfielddata = true, $assignuser = true, $setautoassociatefields = false)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elis::lib('data/customfield.class.php');
     require_once elispm::file('accesslib.php');
     require_once elispm::file('enrol/userset/moodleprofile/userset_profile.class.php');
     require_once elispm::lib('data/clusterassignment.class.php');
     require_once elispm::lib('data/user.class.php');
     require_once elispm::lib('data/usermoodle.class.php');
     require_once elispm::lib('data/userset.class.php');
     $fieldcategoryid = $DB->get_field(field_category::TABLE, 'id', array('name' => 'Associated Group'));
     $this->assertNotEquals(false, $fieldcategoryid);
     $fieldcategory = new field_category($fieldcategoryid);
     $fieldcategory->load();
     // Set up the test user.
     $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA'));
     $user->save();
     $user->synchronize_moodle_user();
     // We need a system-level role assignment.
     $roleid = create_role('systemrole', 'systemrole', 'systemrole');
     $userid = $DB->get_field('user', 'id', array('username' => 'testuserusername'));
     $context = context_system::instance();
     role_assign($roleid, $userid, $context->id);
     // Set up the userset.
     $userset = new userset();
     $usersetdata = array('name' => 'testusersetname');
     if ($setcustomfielddata) {
         $usersetdata['field_userset_group'] = 1;
         $usersetdata['field_userset_groupings'] = 1;
     }
     $userset->set_from_data((object) $usersetdata);
     $userset->save();
     if ($setautoassociatefields) {
         // Set up a file we can use to auto-associate users to a userset.
         $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'autoassociate', 'name' => 'autoassociate', 'datatype' => 'bool'));
         $field->save();
         // Ensure manual field owner exists for syncing.
         field_owner::ensure_field_owner_exists($field, 'manual');
         $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual'));
         $owner = new field_owner($ownerid);
         $owner->param_control = 'checkbox';
         $owner->save();
         field_owner::ensure_field_owner_exists($field, 'moodle_profile');
         $DB->execute("UPDATE {" . field_owner::TABLE . "} SET exclude = ?", array(pm_moodle_profile::sync_to_moodle));
         $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER));
         $fieldcontextlevel->save();
         // The associated Moodle user profile field.
         require_once $CFG->dirroot . '/user/profile/definelib.php';
         require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php';
         $profiledefinecheckbox = new profile_define_checkbox();
         $data = new stdClass();
         $data->datatype = 'checkbox';
         $data->categoryid = 99999;
         $data->shortname = 'autoassociate';
         $data->name = 'autoassociate';
         $profiledefinecheckbox->define_save($data);
         $mfield = $DB->get_record('user_info_field', array('shortname' => 'autoassociate'));
         // The "cluster-profile" association.
         $usersetprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $mfield->id, 'value' => 1));
         $usersetprofile->save();
     }
     if ($assignuser) {
         // Assign the user to the user set.
         $clusterassignment = new clusterassignment(array('clusterid' => $userset->id, 'userid' => $user->id, 'plugin' => 'manual'));
         $clusterassignment->save();
     }
 }
Ejemplo n.º 17
0
/**
 * Run all upgrade steps from before elis 2.6.
 *
 * @param int $oldversion The currently installed version of the old component.
 * @return bool Success/Failure.
 */
function elisprogram_usetgroups_pre26upgradesteps($oldversion)
{
    global $CFG, $THEME, $DB;
    $dbman = $DB->get_manager();
    $result = true;
    if ($oldversion < 2011072600) {
        // Rename fields.
        $fieldnames = array('group', 'groupings');
        foreach ($fieldnames as $fieldname) {
            $field = field::find(new field_filter('shortname', 'cluster_' . $fieldname));
            if ($field->valid()) {
                $field = $field->current();
                $field->shortname = 'userset_' . $fieldname;
                $field->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011072600, 'pmplugins', 'userset_groups');
    }
    if ($result && $oldversion < 2011101300) {
        // Rename field help.
        $fieldmap = array('userset_group' => 'elisprogram_usetgroups/userset_group', 'userset_groupings' => 'elisprogram_usetgroups/autoenrol_groupings');
        foreach ($fieldmap as $key => $val) {
            $field = field::find(new field_filter('shortname', $key));
            if ($field->valid()) {
                $field = $field->current();
                if ($owner = new field_owner(!isset($field->owners) || !isset($field->owners['manual']) ? false : $field->owners['manual'])) {
                    $owner->fieldid = $field->id;
                    $owner->plugin = 'manual';
                    //$owner->exclude = 0; // TBD
                    $owner->params = serialize(array('required' => 0, 'edit_capability' => '', 'view_capability' => '', 'control' => 'checkbox', 'columns' => 30, 'rows' => 10, 'maxlength' => 2048, 'help_file' => $val));
                    $owner->save();
                }
            }
        }
        upgrade_plugin_savepoint($result, 2011101300, 'pmplugins', 'userset_groups');
    }
    return $result;
}
Ejemplo n.º 18
0
 /**
  * Test rlipexport_version1elis_extrafieldsetcustomfieldbase::get_columns()
  */
 public function test_rlipexport_version1elis_extrafieldsetcustomfieldbase_getdata()
 {
     $this->load_csv_data();
     // Create Fields.
     // Normal Field.
     $fieldid1 = $this->create_custom_field('testcf1', 0, true, CONTEXT_ELIS_USER);
     $this->create_field_mapping('testcustomfields', 'field_' . $fieldid1, '');
     $this->create_field_data($fieldid1, 'option1');
     // Second Normal Field.
     $fieldid2 = $this->create_custom_field('testcf2', 0, true, CONTEXT_ELIS_USER);
     $this->create_field_mapping('testcustomfields', 'field_' . $fieldid2, '');
     $this->create_field_data($fieldid2, 'option2');
     // Multivalued field with multiple values.
     $fieldid3 = $this->create_custom_field('testcf3', 1, true, CONTEXT_ELIS_USER);
     $this->create_field_mapping('testcustomfields', 'field_' . $fieldid3, '');
     $this->create_field_data($fieldid3, array('option1', 'option2'));
     // Multivalued field with single value.
     $fieldid4 = $this->create_custom_field('testcf4', 1, true, CONTEXT_ELIS_USER);
     $this->create_field_mapping('testcustomfields', 'field_' . $fieldid4, '');
     $this->create_field_data($fieldid4, array('option1'));
     // Historical multivalued field.
     $fieldid5 = $this->create_custom_field('testcf5', 1, true, CONTEXT_ELIS_USER);
     $this->create_field_mapping('testcustomfields', 'field_' . $fieldid5, '');
     $this->create_field_data($fieldid5, array('option1', 'option2'));
     // Update field to not multivalued to create historical multivalue state.
     $field = new field($fieldid5);
     $field->load();
     $field->multivalued = 0;
     $field->save();
     // Datetime field.
     $fieldid6 = $this->create_custom_field('testcf6', 0, false, CONTEXT_ELIS_USER, 'datetime');
     $this->create_field_mapping('testcustomfields', 'field_' . $fieldid6, '');
     $this->create_field_data($fieldid6, mktime(0, 0, 0, 7, 24, 2012));
     // Datetime with time field.
     $fieldid7 = $this->create_custom_field('testcf7', 0, false, CONTEXT_ELIS_USER, 'datetime');
     $this->create_field_mapping('testcustomfields', 'field_' . $fieldid7, '');
     $this->create_field_data($fieldid7, mktime(10, 20, 30, 7, 24, 2012));
     $field = new field($fieldid7);
     $field->load();
     $fieldowner = new field_owner($field->owners['manual']->id);
     $fieldowner->load();
     $fieldowner->param_inctime = 1;
     $fieldowner->save();
     // Fields with HTML.
     $fieldid8 = $this->create_custom_field('testcf8', 0, false, CONTEXT_ELIS_USER, 'text');
     $this->create_field_mapping('testcustomfields', 'field_' . $fieldid8, '');
     $this->create_field_data($fieldid8, '<b>Test Text One</b>');
     $fieldid9 = $this->create_custom_field('testcf9', 0, false, CONTEXT_ELIS_USER, 'textarea');
     $this->create_field_mapping('testcustomfields', 'field_' . $fieldid9, '');
     $this->create_field_data($fieldid9, '<i>Test Text Two</i>');
     // Mock database record.
     $record = new stdClass();
     $record->userid = 200;
     $record->{'custom_field_' . $fieldid1} = 'option1';
     $record->{'custom_field_' . $fieldid2} = 'option2';
     $record->{'custom_field_' . $fieldid3} = '';
     $record->{'custom_field_' . $fieldid4} = 'option1';
     $record->{'custom_field_' . $fieldid5} = '';
     $record->{'custom_field_' . $fieldid6} = mktime(0, 0, 0, 7, 24, 2012);
     $record->{'custom_field_' . $fieldid7} = mktime(10, 20, 30, 7, 24, 2012);
     $record->{'custom_field_' . $fieldid8} = '<b>Test Text One</b>';
     $record->{'custom_field_' . $fieldid9} = '<i>Test Text Two</i>';
     // Perform Test.
     $enabledfields = rlipexport_version1elis_extrafields::get_enabled_fields();
     $fieldset = new rlipexport_version1elis_extrafieldset_testcustomfields($enabledfields['testcustomfields']);
     $data = $fieldset->get_data($record);
     // Verify Result.
     $expecteddata = array('field_' . $fieldid1 => 'option1', 'field_' . $fieldid2 => 'option2', 'field_' . $fieldid3 => 'option1 / option2', 'field_' . $fieldid4 => 'option1', 'field_' . $fieldid5 => 'option1', 'field_' . $fieldid6 => 'Jul/24/2012', 'field_' . $fieldid7 => 'Jul/24/2012:10:20', 'field_' . $fieldid8 => 'TEST TEXT ONE', 'field_' . $fieldid9 => '_Test Text Two_');
     $this->assertEquals($expecteddata, $data);
 }
Ejemplo n.º 19
0
 /**
  * Initialize a custom field and field owner record.
  *
  * @param string $editcapability The capability configured as needed for editing
  *                               the custom field (empty string for context-specific capability)
  * @param string $viewcapability The capability configured as needed for viewing
  *                               the custom field (empty string for context-specific capability)
  * @return object The custom field object
  */
 private function init_field_and_owner($editcapability, $viewcapability)
 {
     // Set up the custom field.
     $field = new field(array('shortname' => 'field', 'name' => 'field', 'datatype' => 'bool', 'categoryid' => 99999));
     $field->save();
     // Set up the field owner.
     $params = array('control' => 'checkbox', 'edit_capability' => $editcapability, 'view_capability' => $viewcapability);
     $fieldowner = new field_owner(array('fieldid' => $field->id, 'plugin' => 'manual', 'params' => serialize($params)));
     $fieldowner->save();
     return $field;
 }
 /**
  * Validate that the version 1 export includes custom field datetime data in the output.
  *
  * @dataProvider entity_provider
  */
 public function test_exportincludes_customfield_datetime_data($entityname, $entitytable)
 {
     global $CFG, $DB;
     // Set the export to be incremental.
     set_config('nonincremental', 0, 'dhexport_version1elis');
     $this->load_csv_data();
     // Set up necessary custom field information in the database.
     // Create category.
     $categoryid = $this->create_custom_field_category();
     // Create custom field.
     $field = $this->create_test_field($entityname, 'rlipdate', 'int', 'datetime', $categoryid);
     $this->create_field_mapping('testcustomfields', 'field_' . $field->id);
     // Create data record for custom field.
     $entity = $DB->get_records($entitytable);
     $entity = current($entity);
     $result = $this->update_data_record($entityname, $entity, $field, mktime(0, 0, 0, 1, 1, 2012));
     // Obtain our export data based on the current DB state.
     $data = $this->get_export_data();
     // Data validation.
     $this->assertEquals(2, count($data));
     $row = $data[1];
     $this->assertEquals(11, count($row));
     $this->assertEquals('Jan/01/2012', $row[10]);
     // Set inctime to true.
     $fieldowner = new field_owner($field->owners['manual']->id);
     $fieldowner->load();
     $fieldowner->param_inctime = 1;
     $fieldowner->save();
     // Test with other value.
     $value = mktime(10, 10, 0, 1, 1, 2012);
     $result = $this->update_data_record($entityname, $entity, $field, $value);
     $data = $this->get_export_data();
     $this->assertEquals(2, count($data));
     $row = $data[1];
     $this->assertEquals(11, count($row));
     $this->assertEquals('Jan/01/2012:10:10', $row[10]);
 }
 /**
  * Set up data that is needed for testing
  */
 private function set_up_required_data($assignusertouserset = true, $assigncoursetoclass = true, $assigntracktoclass = true, $initclusterprofile = false, $initusersetfielddata = true)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once $CFG->dirroot . '/course/lib.php';
     require_once $CFG->dirroot . '/lib/enrollib.php';
     require_once elis::lib('data/customfield.class.php');
     require_once elispm::file('accesslib.php');
     require_once elispm::lib('data/classmoodlecourse.class.php');
     require_once elispm::lib('data/clusterassignment.class.php');
     require_once elispm::lib('data/clustertrack.class.php');
     require_once elispm::lib('data/course.class.php');
     require_once elispm::lib('data/curriculum.class.php');
     require_once elispm::lib('data/pmclass.class.php');
     require_once elispm::lib('data/track.class.php');
     require_once elispm::lib('data/user.class.php');
     require_once elispm::lib('data/userset.class.php');
     require_once elispm::lib('data/usertrack.class.php');
     $fieldcategoryid = $DB->get_field(field_category::TABLE, 'id', array('name' => 'Associated Group'));
     $this->assertNotEquals(false, $fieldcategoryid);
     $fieldcategory = new field_category($fieldcategoryid);
     $fieldcategory->load();
     // Set up the test user.
     $user = new user(array('idnumber' => 'testuseridnumber', 'username' => 'testuserusername', 'firstname' => 'testuserfirstname', 'lastname' => 'testuserlastname', 'email' => '*****@*****.**', 'country' => 'CA'));
     $user->save();
     // Set up the test course description and class instance.
     $course = new course(array('name' => 'testcoursename', 'idnumber' => 'testcourseidnumber', 'syllabus' => ''));
     $course->save();
     $pmclass = new pmclass(array('courseid' => $course->id, 'idnumber' => 'testclassidnumber'));
     $pmclass->save();
     $category = new stdClass();
     $category->name = 'testcategoryname';
     $category->id = $DB->insert_record('course_categories', $category);
     // Set up the test Moodle course.
     set_config('enrol_plugins_enabled', 'manual');
     set_config('defaultenrol', 1, 'enrol_manual');
     set_config('status', ENROL_INSTANCE_ENABLED, 'enrol_manual');
     $course = new stdClass();
     $course->category = $category->id;
     $course->shortname = 'testcourseshortname';
     $course->fullname = 'testcoursefullname';
     $course = create_course($course);
     if ($assigncoursetoclass) {
         // Assign the Moodle course to a class instance.
         $classmoodlecourse = new classmoodlecourse(array('classid' => $pmclass->id, 'moodlecourseid' => $course->id));
         $classmoodlecourse->save();
     }
     // Set up the test program and track.
     $curriculum = new curriculum(array('idnumber' => 'testcurriculumidnumber'));
     $curriculum->save();
     $track = new track(array('curid' => $curriculum->id, 'idnumber' => 'testtrackidnumber'));
     $track->save();
     if ($assigntracktoclass) {
         // Assign the test track to the test class instance.
         $trackassignment = new trackassignment(array('trackid' => $track->id, 'classid' => $pmclass->id, 'autoenrol' => 1));
         $trackassignment->save();
     }
     // Set up the test userset.
     $userset = new userset();
     $usersetdata = array('name' => 'testusersetname');
     if ($initusersetfielddata) {
         $usersetdata['field_userset_group'] = 1;
         $usersetdata['field_userset_groupings'] = 1;
     }
     $userset->set_from_data((object) $usersetdata);
     $userset->save();
     // Assign the test user to the test track.
     $usertrack = new usertrack(array('userid' => $user->id, 'trackid' => $track->id));
     $usertrack->save();
     $clustertrack = new clustertrack(array('clusterid' => $userset->id, 'trackid' => $track->id));
     $clustertrack->save();
     if ($assignusertouserset) {
         // Assign the test user to the test userset.
         $clusterassignment = new clusterassignment(array('userid' => $user->id, 'clusterid' => $userset->id, 'plugin' => 'manual'));
         $clusterassignment->save();
     }
     if ($initclusterprofile) {
         // Set up a file we can use to auto-associate users to a userset.
         $field = new field(array('categoryid' => $fieldcategory->id, 'shortname' => 'autoassociate', 'name' => 'autoassociate', 'datatype' => 'bool'));
         $field->save();
         // Ensure manual field owner exists for syncing.
         field_owner::ensure_field_owner_exists($field, 'manual');
         $ownerid = $DB->get_field(field_owner::TABLE, 'id', array('fieldid' => $field->id, 'plugin' => 'manual'));
         $owner = new field_owner($ownerid);
         $owner->param_control = 'checkbox';
         $owner->save();
         field_owner::ensure_field_owner_exists($field, 'moodle_profile');
         $DB->execute("UPDATE {" . field_owner::TABLE . "} SET exclude = ?", array(pm_moodle_profile::sync_to_moodle));
         $fieldcontextlevel = new field_contextlevel(array('fieldid' => $field->id, 'contextlevel' => CONTEXT_ELIS_USER));
         $fieldcontextlevel->save();
         // The associated Moodle user profile field.
         require_once $CFG->dirroot . '/user/profile/definelib.php';
         require_once $CFG->dirroot . '/user/profile/field/checkbox/define.class.php';
         $profiledefinecheckbox = new profile_define_checkbox();
         $data = new stdClass();
         $data->datatype = 'checkbox';
         $data->categoryid = 99999;
         $data->shortname = 'autoassociate';
         $data->name = 'autoassociate';
         $profiledefinecheckbox->define_save($data);
         $mfield = $DB->get_record('user_info_field', array('shortname' => 'autoassociate'));
         // The "cluster-profile" association.
         $usersetprofile = new userset_profile(array('clusterid' => $userset->id, 'fieldid' => $mfield->id, 'value' => true));
         $usersetprofile->save();
     }
     // Enrol the user in the Moodle course.
     $mdluserid = $DB->get_field('user', 'id', array('username' => 'testuserusername'));
     $roleid = create_role('testrole', 'testrole', 'testrole');
     enrol_try_internal_enrol($course->id, $mdluserid, $roleid);
     // Set up the necessary config data.
     set_config('userset_groups', 1, 'elisprogram_usetgroups');
     set_config('siteguest', '');
     // Validate setup.
     $this->assertEquals(0, $DB->count_records('groups'));
 }