Esempio n. 1
0
 /**
  * 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']);
 }
Esempio n. 2
0
 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);
 }
 public function setUp()
 {
     global $DB, $USER;
     parent::setUp();
     $settings = array('quiet' => 1, 'pre_cleanup' => 0, 'modules_list' => array($this->module_type), 'number_of_students' => 5, 'students_per_course' => 5, 'number_of_sections' => 1, 'number_of_modules' => 1, 'questions_per_course' => 0);
     generator_generate_data($settings);
     $this->modules = $DB->get_records($this->module_type);
     $first_module = reset($this->modules);
     $cm = get_coursemodule_from_instance($this->module_type, $first_module->id);
     $fields = $DB->get_records('data_fields', array('dataid' => $first_module->id));
     $recordid = data_add_record($first_module);
     foreach ($fields as $field) {
         $content->recordid = $recordid;
         $content->fieldid = $field->id;
         $content->content = 'test content';
         $content->content1 = 'test content 1';
         $content->content2 = 'test content 2';
         $DB->insert_record('data_content', $content);
     }
     // Callback args required: id, record, delimiter_name, exporttype
     $this->caller_single = parent::setup_caller('data_portfolio_caller', array('id' => $cm->id, 'record' => $recordid));
     $this->caller = parent::setup_caller('data_portfolio_caller', array('id' => $cm->id));
 }
Esempio n. 4
0
         if (empty($field->field) || $namearr[1] != $field->field->id) {
             // Try to reuse classes
             $field = data_get_field_from_id($namearr[1], $data);
         }
         if ($field->notemptyfield($value, $name)) {
             $emptyform = false;
             break;
             // if anything has content, this form is not empty, so stop now!
         }
     }
 }
 if ($emptyform) {
     //nothing gets written to database
     echo $OUTPUT->notification(get_string('emptyaddform', 'data'));
 }
 if (!$emptyform && ($recordid = data_add_record($data, $currentgroup))) {
     //add instance to data_record
     /// Insert a whole lot of empty records to make sure we have them
     $fields = $DB->get_records('data_fields', array('dataid' => $data->id));
     foreach ($fields as $field) {
         $content->recordid = $recordid;
         $content->fieldid = $field->id;
         $DB->insert_record('data_content', $content);
     }
     /// For each field in the add form, add it to the data_content.
     foreach ($datarecord as $name => $value) {
         if (!in_array($name, $ignorenames)) {
             $namearr = explode('_', $name);
             // Second one is the field id
             if (empty($field->field) || $namearr[1] != $field->field->id) {
                 // Try to reuse classes
Esempio n. 5
0
 /**
  * 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);
     }
 }
Esempio n. 6
0
                $namearr = explode('_', $name);  // Second one is the field id
                if (empty($field->field) || ($namearr[1] != $field->field->id)) {  // Try to reuse classes
                    $field = data_get_field_from_id($namearr[1], $data);
                }
                if ($field->notemptyfield($value, $name)) {
                    $emptyform = false;
                    break;             // if anything has content, this form is not empty, so stop now!
                }
            }
        }

        if ($emptyform){    //nothing gets written to database
            echo $OUTPUT->notification(get_string('emptyaddform','data'));
        }

        if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) {    //add instance to data_record

            /// Insert a whole lot of empty records to make sure we have them
            $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
            foreach ($fields as $field) {
                $content->recordid = $recordid;
                $content->fieldid = $field->id;
                $DB->insert_record('data_content',$content);
            }

            /// For each field in the add form, add it to the data_content.
            foreach ($datarecord as $name => $value){
                if (!in_array($name, $ignorenames)) {
                    $namearr = explode('_', $name);  // Second one is the field id
                    if (empty($field->field) || ($namearr[1] != $field->field->id)) {  // Try to reuse classes
                        $field = data_get_field_from_id($namearr[1], $data);
 /**
  * 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));
 }
Esempio n. 8
0
 }
 // check the fieldnames are valid
 $fields = $DB->get_records('data_fields', array('dataid' => $data->id), '', 'name, id, type');
 $errorfield = '';
 foreach ($fieldnames as $name) {
     if (!isset($fields[$name])) {
         $errorfield .= "'{$name}' ";
     }
 }
 if (!empty($errorfield)) {
     print_error('fieldnotmatched', 'data', "{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}", $errorfield);
 }
 $cir->init();
 $recordsadded = 0;
 while ($record = $cir->next()) {
     if ($recordid = data_add_record($data, 0)) {
         // add instance to data_record
         $fields = $DB->get_records('data_fields', array('dataid' => $data->id), '', 'name, id, type');
         // Insert new data_content fields with NULL contents:
         foreach ($fields as $field) {
             $content = new stdClass();
             $content->recordid = $recordid;
             $content->fieldid = $field->id;
             $DB->insert_record('data_content', $content);
         }
         // Fill data_content with the values imported from the CSV file:
         foreach ($record as $key => $value) {
             $name = $fieldnames[$key];
             $field = $fields[$name];
             $content = new stdClass();
             $content->fieldid = $field->id;
Esempio n. 9
0
 /**
  * Test the record created event.
  */
 public function test_record_created()
 {
     // 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));
     // Trigger and capture the event for creating the record.
     $sink = $this->redirectEvents();
     $recordid = data_add_record($data);
     $events = $sink->get_events();
     $event = reset($events);
     // Check that the event data is valid.
     $this->assertInstanceOf('\\mod_data\\event\\record_created', $event);
     $this->assertEquals(context_module::instance($data->cmid), $event->get_context());
     $expected = array($course->id, 'data', 'add', 'view.php?d=' . $data->id . '&rid=' . $recordid, $data->id, $data->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $this->assertEventContextNotUsed($event);
     $url = new moodle_url('/mod/data/view.php', array('d' => $data->id, 'rid' => $recordid));
     $this->assertEquals($url, $event->get_url());
 }
Esempio n. 10
0
 /**
  * Creates a field for a mod_data instance.
  * Currently, the field types in the ignoredfieldtypes array aren't supported.
  * The developers using the generator must adhere to the following format :
  *
  *   Syntax : $contents[ fieldid ] = fieldvalue
  *   $contents['checkbox'] = array('val1', 'val2', 'val3' .....)
  *   $contents['data'] = 'dd-mm-yyyy'
  *   $contents['menu'] = 'value';
  *   $contents['multimenu'] =  array('val1', 'val2', 'val3' .....)
  *   $contents['number'] = 'numeric value'
  *   $contents['radiobuton'] = 'value'
  *   $contents['text'] = 'text'
  *   $contents['textarea'] = 'text'
  *   $contents['url'] = 'example.url' or array('example.url', 'urlname')
  *
  * @param mod_data $data
  * @param array $contents
  * @return data_field_{type}
  */
 public function create_entry($data, array $contents)
 {
     global $DB;
     $this->databaserecordcount++;
     $recordid = data_add_record($data);
     $fields = $DB->get_records('data_fields', array('dataid' => $data->id));
     // Validating whether required field are filled.
     foreach ($fields as $field) {
         $fieldhascontent = true;
         if (in_array($field->type, $this->ignoredfieldtypes)) {
             continue;
         }
         $field = data_get_field($field, $data);
         $fieldid = $field->field->id;
         if ($field->type === 'date') {
             $values = array();
             $temp = explode('-', $contents[$fieldid], 3);
             $values['field_' . $fieldid . '_day'] = (int) trim($temp[0]);
             $values['field_' . $fieldid . '_month'] = (int) trim($temp[1]);
             $values['field_' . $fieldid . '_year'] = (int) trim($temp[2]);
             // Year should be less than 2038, so it can be handled by 32 bit windows.
             if ($values['field_' . $fieldid . '_year'] > 2038) {
                 throw new coding_exception('DateTime::getTimestamp resturns false on 32 bit win for year beyond ' . '2038. Please use year less than 2038.');
             }
             $contents[$fieldid] = $values;
             foreach ($values as $fieldname => $value) {
                 if (!$field->notemptyfield($value, $fieldname)) {
                     $fieldhascontent = false;
                 }
             }
         } else {
             if ($field->type === 'textarea') {
                 $values = array();
                 $values['field_' . $fieldid] = $contents[$fieldid];
                 $values['field_' . $fieldid . '_content1'] = 1;
                 $contents[$fieldid] = $values;
                 foreach ($values as $fieldname => $value) {
                     if (!$field->notemptyfield($value, $fieldname)) {
                         $fieldhascontent = false;
                     }
                 }
             } else {
                 if ($field->type === 'url') {
                     $values = array();
                     if (is_array($contents[$fieldid])) {
                         foreach ($contents[$fieldid] as $key => $value) {
                             $values['field_' . $fieldid . '_' . $key] = $value;
                         }
                     } else {
                         $values['field_' . $fieldid . '_0'] = $contents[$fieldid];
                     }
                     $contents[$fieldid] = $values;
                     foreach ($values as $fieldname => $value) {
                         if (!$field->notemptyfield($value, $fieldname)) {
                             $fieldhascontent = false;
                         }
                     }
                 } else {
                     if ($field->notemptyfield($contents[$fieldid], 'field_' . $fieldid . '_0')) {
                         continue;
                     }
                 }
             }
         }
         if ($field->field->required && !$fieldhascontent) {
             return false;
         }
     }
     foreach ($contents as $fieldid => $content) {
         $field = data_get_field_from_id($fieldid, $data);
         if (is_array($content)) {
             foreach ($content as $fieldname => $value) {
                 $field->update_content($recordid, $value, $fieldname);
             }
         } else {
             $field->update_content($recordid, $content);
         }
     }
     return $recordid;
 }
Esempio n. 11
0
         $event->add_record_snapshot('data', $data);
         $event->trigger();
         $viewurl = new moodle_url('/mod/data/view.php', array('d' => $data->id, 'rid' => $rid));
         redirect($viewurl);
     }
 } else {
     // No recordid was specified - creating a new entry.
     // Retrieve the format for the fields.
     $fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
     // Validate the form to ensure that enough data was submitted.
     $processeddata = data_process_submission($data, $fields, $datarecord);
     // Add the new notification data.
     $generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications);
     $fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications);
     // Add instance to data_record.
     if ($processeddata->validated && ($recordid = data_add_record($data, $currentgroup))) {
         // Insert a whole lot of empty records to make sure we have them.
         $records = array();
         foreach ($fields as $field) {
             $content = new stdClass();
             $content->recordid = $recordid;
             $content->fieldid = $field->id;
             $records[] = $content;
         }
         // Bulk insert the records now. Some records may have no data but all must exist.
         $DB->insert_records('data_content', $records);
         // Add all provided content.
         foreach ($processeddata->fields as $fieldname => $field) {
             $field->update_content($recordid, $datarecord->{$fieldname}, $fieldname);
         }
         // Trigger an event for updating this record.
Esempio n. 12
0
 /**
  * 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);
 }
Esempio n. 13
0
 /**
  * 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);
 }
Esempio n. 14
0
        $fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type');
        $errorfield = '';
        foreach ($fieldnames as $name) {
            if (!isset($fields[$name])) {
                $errorfield .= "'$name' ";
            }
        }

        if (!empty($errorfield)) {
            print_error('fieldnotmatched','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}",$errorfield);
        }

        $cir->init();
        $recordsadded = 0;
        while ($record = $cir->next()) {
            if ($recordid = data_add_record($data, 0)) {  // add instance to data_record
                $fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type');

                // Insert new data_content fields with NULL contents:
                foreach ($fields as $field) {
                    $content = new stdClass();
                    $content->recordid = $recordid;
                    $content->fieldid = $field->id;
                    $DB->insert_record('data_content', $content);
                }
                // Fill data_content with the values imported from the CSV file:
                foreach ($record as $key => $value) {
                    $name = $fieldnames[$key];
                    $field = $fields[$name];
                    $content = new stdClass();
                    $content->fieldid = $field->id;