/** * Adds a database instance to the provided course + a text field + adds all attached entries. * * @param stdClass $course * @param array $entries A list of entry names. * @return void */ protected function add_simple_database_instance($course, $entries = false) { global $DB; $database = $this->getDataGenerator()->create_module('data', array('course' => $course->id)); // A database field. $field = data_get_field_new('text', $database); $fielddetail = new stdClass(); $fielddetail->d = $database->id; $fielddetail->mode = 'add'; $fielddetail->type = 'text'; $fielddetail->sesskey = sesskey(); $fielddetail->name = 'Name'; $fielddetail->description = 'Some name'; $fielddetail->param1 = '1'; $field->define_field($fielddetail); $field->insert_field(); $recordid = data_add_record($database); // Database entries. foreach ($entries as $entrytext) { $datacontent = array(); $datacontent['fieldid'] = $field->field->id; $datacontent['recordid'] = $recordid; $datacontent['content'] = $entrytext; $contentid = $DB->insert_record('data_content', $datacontent); } }
/** * Test get_comments */ public function test_get_comments() { global $DB, $CFG; $this->resetAfterTest(true); $CFG->usecomments = true; $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(array('enablecomment' => 1)); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); $record = new stdClass(); $record->course = $course->id; $record->name = "Mod data test"; $record->intro = "Some intro of some sort"; $record->comments = 1; $module = $this->getDataGenerator()->create_module('data', $record); $field = data_get_field_new('text', $module); $fielddetail = new stdClass(); $fielddetail->name = 'Name'; $fielddetail->description = 'Some name'; $field->define_field($fielddetail); $field->insert_field(); $recordid = data_add_record($module); $datacontent = array(); $datacontent['fieldid'] = $field->field->id; $datacontent['recordid'] = $recordid; $datacontent['content'] = 'Asterix'; $contentid = $DB->insert_record('data_content', $datacontent); $cm = get_coursemodule_from_instance('data', $module->id, $course->id); $context = context_module::instance($module->cmid); $this->setUser($user); // We need to add the comments manually, the comment API uses the global OUTPUT and this is going to make the WS to fail. $newcmt = new stdClass(); $newcmt->contextid = $context->id; $newcmt->commentarea = 'database_entry'; $newcmt->itemid = $recordid; $newcmt->content = 'New comment'; $newcmt->format = 0; $newcmt->userid = $user->id; $newcmt->timecreated = time(); $cmtid1 = $DB->insert_record('comments', $newcmt); $newcmt->content = 'New comment 2'; $newcmt->timecreated = time() + 1; $cmtid2 = $DB->insert_record('comments', $newcmt); $contextlevel = 'module'; $instanceid = $cm->id; $component = 'mod_data'; $itemid = $recordid; $area = 'database_entry'; $page = 0; $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page); // We need to execute the return values cleaning process to simulate the web service server. $result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(2, $result['comments']); $this->assertEquals($user->id, $result['comments'][0]['userid']); $this->assertEquals($user->id, $result['comments'][1]['userid']); $this->assertEquals($cmtid2, $result['comments'][0]['id']); $this->assertEquals($cmtid1, $result['comments'][1]['id']); }
function test_data_delete_record() { global $DB; $this->resetAfterTest(); // Create a record for deleting. $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); $record = new stdClass(); $record->course = $course->id; $record->name = "Mod data delete test"; $record->intro = "Some intro of some sort"; $module = $this->getDataGenerator()->create_module('data', $record); $field = data_get_field_new('text', $module); $fielddetail = new stdClass(); $fielddetail->d = $module->id; $fielddetail->mode = 'add'; $fielddetail->type = 'text'; $fielddetail->sesskey = sesskey(); $fielddetail->name = 'Name'; $fielddetail->description = 'Some name'; $field->define_field($fielddetail); $field->insert_field(); $recordid = data_add_record($module); $datacontent = array(); $datacontent['fieldid'] = $field->field->id; $datacontent['recordid'] = $recordid; $datacontent['content'] = 'Asterix'; $contentid = $DB->insert_record('data_content', $datacontent); $cm = get_coursemodule_from_instance('data', $module->id, $course->id); // Check to make sure that we have a database record. $data = $DB->get_records('data', array('id' => $module->id)); $this->assertEquals(1, count($data)); $datacontent = $DB->get_records('data_content', array('id' => $contentid)); $this->assertEquals(1, count($datacontent)); $datafields = $DB->get_records('data_fields', array('id' => $field->field->id)); $this->assertEquals(1, count($datafields)); $datarecords = $DB->get_records('data_records', array('id' => $recordid)); $this->assertEquals(1, count($datarecords)); // Test to see if a failed delete returns false. $result = data_delete_record(8798, $module, $course->id, $cm->id); $this->assertFalse($result); // Delete the record. $result = data_delete_record($recordid, $module, $course->id, $cm->id); // Check that all of the record is gone. $datacontent = $DB->get_records('data_content', array('id' => $contentid)); $this->assertEquals(0, count($datacontent)); $datarecords = $DB->get_records('data_records', array('id' => $recordid)); $this->assertEquals(0, count($datarecords)); // Make sure the function returns true on a successful deletion. $this->assertTrue($result); }
/** * Tests for mod_data_rating_can_see_item_ratings(). * * @throws coding_exception * @throws rating_exception */ public function test_mod_data_rating_can_see_item_ratings() { global $DB; $this->resetAfterTest(); // Setup test data. $course = new stdClass(); $course->groupmode = SEPARATEGROUPS; $course->groupmodeforce = true; $course = $this->getDataGenerator()->create_course($course); $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id)); $cm = get_coursemodule_from_instance('data', $data->id); $context = context_module::instance($cm->id); // Create users. $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $user3 = $this->getDataGenerator()->create_user(); $user4 = $this->getDataGenerator()->create_user(); // Groups and stuff. $role = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST); $this->getDataGenerator()->enrol_user($user1->id, $course->id, $role->id); $this->getDataGenerator()->enrol_user($user2->id, $course->id, $role->id); $this->getDataGenerator()->enrol_user($user3->id, $course->id, $role->id); $this->getDataGenerator()->enrol_user($user4->id, $course->id, $role->id); $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); groups_add_member($group1, $user1); groups_add_member($group1, $user2); groups_add_member($group2, $user3); groups_add_member($group2, $user4); // Add data. $field = data_get_field_new('text', $data); $fielddetail = new stdClass(); $fielddetail->name = 'Name'; $fielddetail->description = 'Some name'; $field->define_field($fielddetail); $field->insert_field(); // Add a record with a group id of zero (all participants). $recordid1 = data_add_record($data, 0); $datacontent = array(); $datacontent['fieldid'] = $field->field->id; $datacontent['recordid'] = $recordid1; $datacontent['content'] = 'Obelix'; $DB->insert_record('data_content', $datacontent); $recordid = data_add_record($data, $group1->id); $datacontent = array(); $datacontent['fieldid'] = $field->field->id; $datacontent['recordid'] = $recordid; $datacontent['content'] = 'Asterix'; $DB->insert_record('data_content', $datacontent); // Now try to access it as various users. unassign_capability('moodle/site:accessallgroups', $role->id); // Eveyone should have access to the record with the group id of zero. $params1 = array('contextid' => 2, 'component' => 'mod_data', 'ratingarea' => 'entry', 'itemid' => $recordid1, 'scaleid' => 2); $params = array('contextid' => 2, 'component' => 'mod_data', 'ratingarea' => 'entry', 'itemid' => $recordid, 'scaleid' => 2); $this->setUser($user1); $this->assertTrue(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); $this->setUser($user2); $this->assertTrue(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); $this->setUser($user3); $this->assertFalse(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); $this->setUser($user4); $this->assertFalse(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); // Now try with accessallgroups cap and make sure everything is visible. assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role->id, $context->id); $this->setUser($user1); $this->assertTrue(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); $this->setUser($user2); $this->assertTrue(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); $this->setUser($user3); $this->assertTrue(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); $this->setUser($user4); $this->assertTrue(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); // Change group mode and verify visibility. $course->groupmode = VISIBLEGROUPS; $DB->update_record('course', $course); unassign_capability('moodle/site:accessallgroups', $role->id); $this->setUser($user1); $this->assertTrue(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); $this->setUser($user2); $this->assertTrue(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); $this->setUser($user3); $this->assertTrue(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); $this->setUser($user4); $this->assertTrue(mod_data_rating_can_see_item_ratings($params)); $this->assertTrue(mod_data_rating_can_see_item_ratings($params1)); }
///get the list of possible fields (plugins) $directories = get_list_of_plugins('mod/data/field/'); $menufield = array(); foreach ($directories as $directory) { $menufield[$directory] = get_string($directory, 'data'); //get from language files } asort($menufield); //sort in alphabetical order $PAGE->set_title(get_string('course') . ': ' . $course->fullname); $PAGE->set_heading($course->fullname); $PAGE->set_pagetype('mod-data-field-' . $newtype); if ($mode == 'new' && !empty($newtype) && confirm_sesskey()) { /// Adding a new field data_print_header($course, $cm, $data, 'fields'); $field = data_get_field_new($newtype, $data); $field->display_edit_field(); } else { if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field data_print_header($course, $cm, $data, 'fields'); $field = data_get_field_from_id($fid, $data); $field->display_edit_field(); } else { /// Display the main listing of all fields data_print_header($course, $cm, $data, 'fields'); if (!$DB->record_exists('data_fields', array('dataid' => $data->id))) { echo $OUTPUT->notification(get_string('nofieldindatabase', 'data')); // nothing in database echo $OUTPUT->notification(get_string('pleaseaddsome', 'data', 'preset.php?id=' . $cm->id)); // link to presets
/** * Test the record deleted event. */ public function test_record_deleted() { global $DB; // Create a course we are going to add a data module to. $course = $this->getDataGenerator()->create_course(); // The generator used to create a data module. $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); // Create a data module. $data = $generator->create_instance(array('course' => $course->id)); // Now we want to create a field. $field = data_get_field_new('text', $data); $fielddata = new stdClass(); $fielddata->name = 'Test'; $fielddata->description = 'Test description'; $field->define_field($fielddata); $field->insert_field(); // Create data record. $datarecords = new stdClass(); $datarecords->userid = '2'; $datarecords->dataid = $data->id; $datarecords->id = $DB->insert_record('data_records', $datarecords); // Create data content. $datacontent = new stdClass(); $datacontent->fieldid = $field->field->id; $datacontent->recordid = $datarecords->id; $datacontent->id = $DB->insert_record('data_content', $datacontent); // Trigger and capture the event for deleting the data record. $sink = $this->redirectEvents(); data_delete_record($datarecords->id, $data, $course->id, $data->cmid); $events = $sink->get_events(); $event = reset($events); // Check that the event data is valid. $this->assertInstanceOf('\\mod_data\\event\\record_deleted', $event); $this->assertEquals(context_module::instance($data->cmid), $event->get_context()); $expected = array($course->id, 'data', 'record delete', 'view.php?id=' . $data->cmid, $data->id, $data->cmid); $this->assertEventLegacyLogData($expected, $event); $this->assertEventContextNotUsed($event); $url = new moodle_url('/mod/data/view.php', array('d' => $data->id)); $this->assertEquals($url, $event->get_url()); }
/** * Test getting a list of files with and without a context ID. */ public function test_get_files() { global $USER, $DB; $this->resetAfterTest(); // Set the current user to be the administrator. $this->setAdminUser(); $USER->email = '*****@*****.**'; // Create a course. $course = $this->getDataGenerator()->create_course(); $record = new stdClass(); $record->course = $course->id; $record->name = "Mod data upload test"; $record->intro = "Some intro of some sort"; // Create a database module. $module = $this->getDataGenerator()->create_module('data', $record); // Create a new field in the database activity. $field = data_get_field_new('file', $module); // Add more detail about the field. $fielddetail = new stdClass(); $fielddetail->d = $module->id; $fielddetail->mode = 'add'; $fielddetail->type = 'file'; $fielddetail->sesskey = sesskey(); $fielddetail->name = 'Upload file'; $fielddetail->description = 'Some description'; $fielddetail->param3 = '0'; $field->define_field($fielddetail); $field->insert_field(); $recordid = data_add_record($module); // File information for the database module record. $datacontent = array(); $datacontent['fieldid'] = $field->field->id; $datacontent['recordid'] = $recordid; $datacontent['content'] = 'Simple4.txt'; // Insert the information about the file. $contentid = $DB->insert_record('data_content', $datacontent); // Required information for uploading a file. $context = context_module::instance($module->cmid); $usercontext = context_user::instance($USER->id); $component = 'mod_data'; $filearea = 'content'; $itemid = $contentid; $filename = $datacontent['content']; $filecontent = base64_encode("Let us create a nice simple file."); $filerecord = array(); $filerecord['contextid'] = $context->id; $filerecord['component'] = $component; $filerecord['filearea'] = $filearea; $filerecord['itemid'] = $itemid; $filerecord['filepath'] = '/'; $filerecord['filename'] = $filename; // Create an area to upload the file. $fs = get_file_storage(); // Create a file from the string that we made earlier. $file = $fs->create_file_from_string($filerecord, $filecontent); $timemodified = $file->get_timemodified(); $timecreated = $file->get_timemodified(); $filesize = $file->get_filesize(); // Use the web service function to return the information about the file that we just uploaded. // The first time is with a valid context ID. $filename = ''; $testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename); $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting); // With the information that we have provided we should get an object exactly like the one below. $coursecontext = context_course::instance($course->id); $testdata = array(); $testdata['parents'] = array(); $testdata['parents']['0'] = array('contextid' => 1, 'component' => null, 'filearea' => null, 'itemid' => null, 'filepath' => null, 'filename' => 'System'); $testdata['parents']['1'] = array('contextid' => 3, 'component' => null, 'filearea' => null, 'itemid' => null, 'filepath' => null, 'filename' => 'Miscellaneous'); $testdata['parents']['2'] = array('contextid' => $coursecontext->id, 'component' => null, 'filearea' => null, 'itemid' => null, 'filepath' => null, 'filename' => 'Test course 1'); $testdata['parents']['3'] = array('contextid' => $context->id, 'component' => null, 'filearea' => null, 'itemid' => null, 'filepath' => null, 'filename' => 'Mod data upload test (Database)'); $testdata['parents']['4'] = array('contextid' => $context->id, 'component' => 'mod_data', 'filearea' => 'content', 'itemid' => null, 'filepath' => null, 'filename' => 'Fields'); $testdata['files'] = array(); $testdata['files']['0'] = array('contextid' => $context->id, 'component' => 'mod_data', 'filearea' => 'content', 'itemid' => $itemid, 'filepath' => '/', 'filename' => 'Simple4.txt', 'url' => 'http://www.example.com/moodle/pluginfile.php/' . $context->id . '/mod_data/content/' . $itemid . '/Simple4.txt', 'isdir' => false, 'timemodified' => $timemodified, 'timecreated' => $timecreated, 'filesize' => $filesize, 'author' => null, 'license' => null); // Make sure that they are the same. $this->assertEquals($testdata, $testfilelisting); // Try again but without the context. Minus one signals the function to use other variables to obtain the context. $nocontext = -1; $modified = 0; // Context level and instance ID are used to determine what the context is. $contextlevel = 'module'; $instanceid = $module->cmid; $testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid); $testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting); $this->assertEquals($testfilelisting, $testdata); }
/** * Test comment_deleted event. */ public function test_data_comment_deleted_event() { global $CFG, $DB; require_once $CFG->dirroot . '/comment/lib.php'; $this->resetAfterTest(); // Create a record for deleting. $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); $record = new stdClass(); $record->course = $course->id; $record->name = "Mod data delete test"; $record->intro = "Some intro of some sort"; $record->comments = 1; $module = $this->getDataGenerator()->create_module('data', $record); $field = data_get_field_new('text', $module); $fielddetail = new stdClass(); $fielddetail->name = 'Name'; $fielddetail->description = 'Some name'; $field->define_field($fielddetail); $field->insert_field(); $recordid = data_add_record($module); $datacontent = array(); $datacontent['fieldid'] = $field->field->id; $datacontent['recordid'] = $recordid; $datacontent['content'] = 'Asterix'; $contentid = $DB->insert_record('data_content', $datacontent); $cm = get_coursemodule_from_instance('data', $module->id, $course->id); $context = context_module::instance($module->cmid); $cmt = new stdClass(); $cmt->context = $context; $cmt->course = $course; $cmt->cm = $cm; $cmt->area = 'database_entry'; $cmt->itemid = $recordid; $cmt->showcount = true; $cmt->component = 'mod_data'; $comment = new comment($cmt); $newcomment = $comment->add('New comment 1'); // Triggering and capturing the event. $sink = $this->redirectEvents(); $comment->delete($newcomment->id); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_data\\event\\comment_deleted', $event); $this->assertEquals($context, $event->get_context()); $url = new moodle_url('/mod/data/view.php', array('id' => $module->cmid)); $this->assertEquals($url, $event->get_url()); $this->assertEventContextNotUsed($event); }