/** * Test successful track creation. */ public function test_success() { global $DB; // Create custom field. $fieldcat = new field_category(); $fieldcat->name = 'Test'; $fieldcat->save(); $field = new field(); $field->categoryid = $fieldcat->id; $field->shortname = 'testfield'; $field->name = 'Test Field'; $field->datatype = 'text'; $field->save(); $fieldctx = new field_contextlevel(); $fieldctx->fieldid = $field->id; $fieldctx->contextlevel = CONTEXT_ELIS_TRACK; $fieldctx->save(); $this->give_permissions(array('local/elisprogram:track_create')); // Setup program. $datagen = new elis_program_datagenerator($DB); $program = $datagen->create_program(array('idnumber' => 'test_program_idnumber', 'name' => 'ProgramName')); $track = array('idnumber' => 'testtrack', 'name' => 'testtrackname', 'assignment' => $program->idnumber, 'field_testfield' => 'Test field'); $response = local_datahub_elis_track_create::track_create($track); $this->assertNotEmpty($response); $this->assertInternalType('array', $response); $this->assertArrayHasKey('messagecode', $response); $this->assertArrayHasKey('message', $response); $this->assertArrayHasKey('record', $response); $this->assertEquals(get_string('ws_track_create_success_code', 'local_datahub'), $response['messagecode']); $this->assertEquals(get_string('ws_track_create_success_msg', 'local_datahub'), $response['message']); $this->assertInternalType('array', $response['record']); $this->assertArrayHasKey('id', $response['record']); // Get Track. $createdtrk = new track($response['record']['id']); $createdtrk->load(); $createdtrk = $createdtrk->to_array(); $expecttrk = array('id' => $response['record']['id'], 'idnumber' => 'testtrack', 'name' => 'testtrackname', 'curid' => $program->id, 'description' => '', 'field_testfield' => 'Test field'); foreach ($expecttrk as $param => $val) { $this->assertArrayHasKey($param, $createdtrk); $this->assertEquals($val, $createdtrk[$param]); } }
/** * Performs track update * @throws moodle_exception If there was an error in passed parameters. * @throws data_object_exception If there was an error creating the entity. * @param array $data The incoming data parameter. * @return array An array of parameters, if successful. */ public static function track_update(array $data) { global $USER, $DB; if (static::require_elis_dependencies() !== true) { throw new moodle_exception('ws_function_requires_elis', 'local_datahub'); } // Parameter validation. $params = self::validate_parameters(self::track_update_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; $record = new stdClass(); $record = $data; // need all custom fields, etc. // Initialize version1elis importplugin for utility functions. $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); // Validate if (empty($data->idnumber) || !($trkid = $DB->get_field(track::TABLE, 'id', array('idnumber' => $data->idnumber)))) { throw new data_object_exception('ws_track_update_fail_invalid_idnumber', 'local_datahub', '', $data); } unset($record->idnumber); // Capability checking. require_capability('local/elisprogram:track_edit', \local_elisprogram\context\track::instance($trkid)); if (isset($data->startdate)) { $startdate = $importplugin->parse_date($data->startdate); if (empty($startdate)) { throw new data_object_exception('ws_track_update_fail_invalid_startdate', 'local_datahub', '', $data); } else { $record->startdate = $startdate; } } if (isset($data->enddate)) { $enddate = $importplugin->parse_date($data->enddate); if (empty($enddate)) { throw new data_object_exception('ws_track_update_fail_invalid_enddate', 'local_datahub', '', $data); } else { $record->enddate = $enddate; } } $track = new track($trkid); $track->load(); $track->set_from_data($record); $track->save(); // Respond. if (!empty($track->id)) { $trackrec = (array) $DB->get_record(track::TABLE, array('id' => $track->id)); $trackobj = $track->to_array(); // convert multi-valued custom field arrays to comma-separated listing $fields = self::get_track_custom_fields(); foreach ($fields as $field) { // Generate name using custom field prefix. $fullfieldname = data_object_with_custom_fields::CUSTOM_FIELD_PREFIX . $field->shortname; if ($field->multivalued && isset($trackobj[$fullfieldname]) && is_array($trackobj[$fullfieldname])) { $trackobj[$fullfieldname] = implode(',', $trackobj[$fullfieldname]); } } return array('messagecode' => get_string('ws_track_update_success_code', 'local_datahub'), 'message' => get_string('ws_track_update_success_msg', 'local_datahub'), 'record' => array_merge($trackrec, $trackobj)); } else { throw new data_object_exception('ws_track_update_fail', 'local_datahub'); } }
} } return $timestamp; } header("Content-type: application/rss+xml"); include 'config.php'; $redis = new Predis\Client(); $last_build_date = $redis->get("last_build_date"); $podcastFiles = scandir("./" . $podcast_directory); date_default_timezone_set("Europe/Berlin"); foreach ($podcastFiles as $podcast) { if (substr($podcast, 0, 1) == ".") { continue; } $current_track = new track(); if ($current_track->load($podcast)) { $tracks[] = $current_track; continue; } // not in database $last_build_date = time(); $timestamp = timestamp_from_preg('/(\\d\\d-\\d\\d-\\d\\d\\d\\d)/', $podcast); if ($timestamp == 0) { $timestamp = filemtime($podcast_directory . "/" . $podcast); } $getID3 = new getID3(); $ThisFileInfo = $getID3->analyze("./" . $podcast_directory . "/" . $podcast); $duration = @$ThisFileInfo['playtime_string']; $filename = str_replace("_", " ", $podcast); if (preg_match('/.*?-(.*?)-(.*?)-.*?/', $filename, $matches)) { $current_track->content["author"] = trim($matches[1]);
/** * Test to ensure that the auto-generated class ID number values do not overflow the maximum length of the * local_elisprogram_cls.idnumber field when multiple copies of the same class are created which require an incrementing iterator * to be appended to the idnumber value are used. */ public function test_trackautocreatevalidation_doesnotoverflowidnumberfieldwithiterators() { global $DB; $this->load_csv_data(); // Need track and userset. $userset = new stdClass(); $userset->id = 1; $userset->name = 'test'; // Set values required for auto create. $track = new track(5); $track->load(); // Testing track auto create. $track->track_auto_create(); // Force duplicate classes to be created which should have a unique iterator added to the idnumber field and // still be within the allowable field size. $track->track_auto_create(); $track->track_auto_create(); // Get most recent class records created. $records = $DB->get_records('local_elisprogram_cls', array(), "id DESC", 'id, idnumber', 0, 3); // We want to test in the order they were created. $records = array_reverse($records); $expectedvalue = substr('test-' . $track->idnumber, 0, 95); $iterator = 0; foreach ($records as $record) { $this->assertEquals($expectedvalue . ($iterator > 0 ? '.' . $iterator : ''), $record->idnumber); $iterator++; } }