Ejemplo n.º 1
0
 /**
  * Attach to $element (usually attempts) the needed backup structures
  * for question_usages and all the associated data.
  *
  * @param backup_nested_element $element the element that will contain all the question_usages data.
  * @param string $usageidname the name of the element that holds the usageid.
  *      This must be child of $element, and must be a final element.
  * @param string $nameprefix this prefix is added to all the element names we create.
  *      Element names in the XML must be unique, so if you are using usages in
  *      two different ways, you must give a prefix to at least one of them. If
  *      you only use one sort of usage, then you can just use the default empty prefix.
  *      This should include a trailing underscore. For example "myprefix_"
  */
 protected function add_question_usages($element, $usageidname, $nameprefix = '')
 {
     global $CFG;
     require_once $CFG->dirroot . '/question/engine/lib.php';
     // Check $element is one nested_backup_element
     if (!$element instanceof backup_nested_element) {
         throw new backup_step_exception('question_states_bad_parent_element', $element);
     }
     if (!$element->get_final_element($usageidname)) {
         throw new backup_step_exception('question_states_bad_question_attempt_element', $usageidname);
     }
     $quba = new backup_nested_element($nameprefix . 'question_usage', array('id'), array('component', 'preferredbehaviour'));
     $qas = new backup_nested_element($nameprefix . 'question_attempts');
     $qa = new backup_nested_element($nameprefix . 'question_attempt', array('id'), array('slot', 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged', 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified'));
     $steps = new backup_nested_element($nameprefix . 'steps');
     $step = new backup_nested_element($nameprefix . 'step', array('id'), array('sequencenumber', 'state', 'fraction', 'timecreated', 'userid'));
     $response = new backup_nested_element($nameprefix . 'response');
     $variable = new backup_nested_element($nameprefix . 'variable', null, array('name', 'value'));
     // Build the tree
     $element->add_child($quba);
     $quba->add_child($qas);
     $qas->add_child($qa);
     $qa->add_child($steps);
     $steps->add_child($step);
     $step->add_child($response);
     $response->add_child($variable);
     // Set the sources
     $quba->set_source_table('question_usages', array('id' => '../' . $usageidname));
     $qa->set_source_table('question_attempts', array('questionusageid' => backup::VAR_PARENTID), 'slot ASC');
     $step->set_source_table('question_attempt_steps', array('questionattemptid' => backup::VAR_PARENTID), 'sequencenumber ASC');
     $variable->set_source_table('question_attempt_step_data', array('attemptstepid' => backup::VAR_PARENTID));
     // Annotate ids
     $qa->annotate_ids('question', 'questionid');
     $step->annotate_ids('user', 'userid');
     // Annotate files
     $fileareas = question_engine::get_all_response_file_areas();
     foreach ($fileareas as $filearea) {
         $step->annotate_files('question', $filearea, 'id');
     }
 }
Ejemplo n.º 2
0
 /**
  * Backup structures tests (construction, definition and execution)
  */
 function test_backup_structure_construct()
 {
     global $DB;
     $backupid = 'Testing Backup ID';
     // Official backupid for these tests
     // Create all the elements that will conform the tree
     $forum = new backup_nested_element('forum', array('id'), array('type', 'name', 'intro', 'introformat', 'assessed', 'assesstimestart', 'assesstimefinish', 'scale', 'maxbytes', 'maxattachments', 'forcesubscribe', 'trackingtype', 'rsstype', 'rssarticles', 'timemodified', 'warnafter', 'blockafter', new backup_final_element('blockperiod'), new mock_skip_final_element('completiondiscussions'), new mock_modify_final_element('completionreplies'), new mock_final_element_interceptor('completionposts')));
     $discussions = new backup_nested_element('discussions');
     $discussion = new backup_nested_element('discussion', array('id'), array('forum', 'name', 'firstpost', 'userid', 'groupid', 'assessed', 'timemodified', 'usermodified', 'timestart', 'timeend'));
     $posts = new backup_nested_element('posts');
     $post = new backup_nested_element('post', array('id'), array('discussion', 'parent', 'userid', 'created', 'modified', 'mailed', 'subject', 'message', 'messageformat', 'messagetrust', 'attachment', 'totalscore', 'mailnow'));
     $ratings = new backup_nested_element('ratings');
     $rating = new backup_nested_element('rating', array('id'), array('userid', 'itemid', 'time', 'post_rating'));
     $reads = new backup_nested_element('readposts');
     $read = new backup_nested_element('read', array('id'), array('userid', 'discussionid', 'postid', 'firstread', 'lastread'));
     $inventeds = new backup_nested_element('invented_elements', array('reason', 'version'));
     $invented = new backup_nested_element('invented', null, array('one', 'two', 'three'));
     $one = $invented->get_final_element('one');
     $one->add_attributes(array('attr1', 'attr2'));
     // Build the tree
     $forum->add_child($discussions);
     $discussions->add_child($discussion);
     $discussion->add_child($posts);
     $posts->add_child($post);
     $post->add_child($ratings);
     $ratings->add_child($rating);
     $forum->add_child($reads);
     $reads->add_child($read);
     $forum->add_child($inventeds);
     $inventeds->add_child($invented);
     // Let's add 1 optigroup with 4 elements
     $alternative1 = new backup_optigroup_element('alternative1', array('name', 'value'), '../../id', 1);
     $alternative2 = new backup_optigroup_element('alternative2', array('name', 'value'), backup::VAR_PARENTID, 2);
     $alternative3 = new backup_optigroup_element('alternative3', array('name', 'value'), '/forum/discussions/discussion/posts/post/id', 3);
     $alternative4 = new backup_optigroup_element('alternative4', array('forumtype', 'forumname'));
     // Alternative without conditions
     // Create the optigroup, adding one element
     $optigroup = new backup_optigroup('alternatives', $alternative1, false);
     // Add second opti element
     $optigroup->add_child($alternative2);
     // Add optigroup to post element
     $post->add_optigroup($optigroup);
     // Add third opti element, on purpose after the add_optigroup() line above to check param evaluation works ok
     $optigroup->add_child($alternative3);
     // Add 4th opti element (the one without conditions, so will be present always)
     $optigroup->add_child($alternative4);
     /// Create some new nested elements, both named 'dupetest1', and add them to alternative1 and alternative2
     /// (not problem as far as the optigroup in not unique)
     $dupetest1 = new backup_nested_element('dupetest1', null, array('field1', 'field2'));
     $dupetest2 = new backup_nested_element('dupetest2', null, array('field1', 'field2'));
     $dupetest3 = new backup_nested_element('dupetest3', null, array('field1', 'field2'));
     $dupetest4 = new backup_nested_element('dupetest1', null, array('field1', 'field2'));
     $dupetest1->add_child($dupetest3);
     $dupetest2->add_child($dupetest4);
     $alternative1->add_child($dupetest1);
     $alternative2->add_child($dupetest2);
     // Define sources
     $forum->set_source_table('forum', array('id' => backup::VAR_ACTIVITYID));
     $discussion->set_source_sql('SELECT *
                                    FROM {forum_discussions}
                                   WHERE forum = ?', array('/forum/id'));
     $post->set_source_table('forum_posts', array('discussion' => '/forum/discussions/discussion/id'));
     $rating->set_source_sql('SELECT *
                                FROM {rating}
                               WHERE itemid = ?', array(backup::VAR_PARENTID));
     $read->set_source_table('forum_read', array('id' => '../../id'));
     $inventeds->set_source_array(array((object) array('reason' => 'I love Moodle', 'version' => '1.0'), (object) array('reason' => 'I love Moodle', 'version' => '2.0')));
     // 2 object array
     $invented->set_source_array(array((object) array('one' => 1, 'two' => 2, 'three' => 3), (object) array('one' => 11, 'two' => 22, 'three' => 33)));
     // 2 object array
     // Set optigroup_element sources
     $alternative1->set_source_array(array((object) array('name' => 'alternative1', 'value' => 1)));
     // 1 object array
     // Skip alternative2 source definition on purpose (will be tested)
     // $alternative2->set_source_array(array((object)array('name' => 'alternative2', 'value' => 2))); // 1 object array
     $alternative3->set_source_array(array((object) array('name' => 'alternative3', 'value' => 3)));
     // 1 object array
     // Alternative 4 source is the forum type and name, so we'll get that in ALL posts (no conditions) that
     // have not another alternative (post4 in our testing data in the only not matching any other alternative)
     $alternative4->set_source_sql('SELECT type AS forumtype, name AS forumname
                                      FROM {forum}
                                     WHERE id = ?', array('/forum/id'));
     // Set children of optigroup_element source
     $dupetest1->set_source_array(array((object) array('field1' => '1', 'field2' => 1)));
     // 1 object array
     $dupetest2->set_source_array(array((object) array('field1' => '2', 'field2' => 2)));
     // 1 object array
     $dupetest3->set_source_array(array((object) array('field1' => '3', 'field2' => 3)));
     // 1 object array
     $dupetest4->set_source_array(array((object) array('field1' => '4', 'field2' => 4)));
     // 1 object array
     // Define some aliases
     $rating->set_source_alias('rating', 'post_rating');
     // Map the 'rating' value from DB to 'post_rating' final element
     // Mark to detect files of type 'forum_intro' in forum (and not item id)
     $forum->annotate_files('mod_forum', 'intro', null);
     // Mark to detect file of type 'forum_post' and 'forum_attachment' in post (with itemid being post->id)
     $post->annotate_files('mod_forum', 'post', 'id');
     $post->annotate_files('mod_forum', 'attachment', 'id');
     // Mark various elements to be annotated
     $discussion->annotate_ids('user1', 'userid');
     $post->annotate_ids('forum_post', 'id');
     $rating->annotate_ids('user2', 'userid');
     $rating->annotate_ids('forum_post', 'itemid');
     // Create the backup_ids_temp table
     backup_controller_dbops::create_backup_ids_temp_table($backupid);
     // Instantiate in memory xml output
     $xo = new memory_xml_output();
     // Instantiate xml_writer and start it
     $xw = new xml_writer($xo);
     $xw->start();
     // Instantiate the backup processor
     $processor = new backup_structure_processor($xw);
     // Set some variables
     $processor->set_var(backup::VAR_ACTIVITYID, $this->forumid);
     $processor->set_var(backup::VAR_BACKUPID, $backupid);
     $processor->set_var(backup::VAR_CONTEXTID, $this->contextid);
     // Process the backup structure with the backup processor
     $forum->process($processor);
     // Stop the xml_writer
     $xw->stop();
     // Check various counters
     $this->assertEquals($forum->get_counter(), $DB->count_records('forum'));
     $this->assertEquals($discussion->get_counter(), $DB->count_records('forum_discussions'));
     $this->assertEquals($rating->get_counter(), $DB->count_records('rating'));
     $this->assertEquals($read->get_counter(), $DB->count_records('forum_read'));
     $this->assertEquals($inventeds->get_counter(), 2);
     // Array
     // Perform some validations with the generated XML
     $dom = new DomDocument();
     $dom->loadXML($xo->get_allcontents());
     $xpath = new DOMXPath($dom);
     // Some more counters
     $query = '/forum/discussions/discussion/posts/post';
     $posts = $xpath->query($query);
     $this->assertEquals($posts->length, $DB->count_records('forum_posts'));
     $query = '/forum/invented_elements/invented';
     $inventeds = $xpath->query($query);
     $this->assertEquals($inventeds->length, 2 * 2);
     // Check ratings information against DB
     $ratings = $dom->getElementsByTagName('rating');
     $this->assertEquals($ratings->length, $DB->count_records('rating'));
     foreach ($ratings as $rating) {
         $ratarr = array();
         $ratarr['id'] = $rating->getAttribute('id');
         foreach ($rating->childNodes as $node) {
             if ($node->nodeType != XML_TEXT_NODE) {
                 $ratarr[$node->nodeName] = $node->nodeValue;
             }
         }
         $this->assertEquals($ratarr['userid'], $DB->get_field('rating', 'userid', array('id' => $ratarr['id'])));
         $this->assertEquals($ratarr['itemid'], $DB->get_field('rating', 'itemid', array('id' => $ratarr['id'])));
         $this->assertEquals($ratarr['post_rating'], $DB->get_field('rating', 'rating', array('id' => $ratarr['id'])));
     }
     // Check forum has "blockeperiod" with value 0 (was declared by object instead of name)
     $query = '/forum[blockperiod="0"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check forum is missing "completiondiscussions" (as we are using mock_skip_final_element)
     $query = '/forum/completiondiscussions';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check forum has "completionreplies" with value "original was 0, now changed" (because of mock_modify_final_element)
     $query = '/forum[completionreplies="original was 0, now changed"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check forum has "completionposts" with value "intercepted!" (because of mock_final_element_interceptor)
     $query = '/forum[completionposts="intercepted!"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check there isn't any alternative2 tag, as far as it hasn't source defined
     $query = '//alternative2';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check there are 4 "field1" elements
     $query = '/forum/discussions/discussion/posts/post//field1';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 4);
     // Check first post has one name element with value "alternative1"
     $query = '/forum/discussions/discussion/posts/post[@id="1"][name="alternative1"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check there are two "dupetest1" elements
     $query = '/forum/discussions/discussion/posts/post//dupetest1';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 2);
     // Check second post has one name element with value "dupetest2"
     $query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check element "dupetest2" of second post has one field1 element with value "2"
     $query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2[field1="2"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check forth post has no name element
     $query = '/forum/discussions/discussion/posts/post[@id="4"]/name';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check 1st, 2nd and 3rd posts have no forumtype element
     $query = '/forum/discussions/discussion/posts/post[@id="1"]/forumtype';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     $query = '/forum/discussions/discussion/posts/post[@id="2"]/forumtype';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     $query = '/forum/discussions/discussion/posts/post[@id="3"]/forumtype';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check 4th post has one forumtype element with value "general"
     // (because it doesn't matches alternatives 1, 2, 3, then alternative 4,
     // the one without conditions is being applied)
     $query = '/forum/discussions/discussion/posts/post[@id="4"][forumtype="general"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check annotations information against DB
     // Count records in original tables
     $c_postsid = $DB->count_records_sql('SELECT COUNT(DISTINCT id) FROM {forum_posts}');
     $c_dissuserid = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {forum_discussions}');
     $c_ratuserid = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {rating}');
     // Count records in backup_ids_table
     $f_forumpost = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'forum_post'));
     $f_user1 = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'user1'));
     $f_user2 = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'user2'));
     $c_notbackupid = $DB->count_records_select('backup_ids_temp', 'backupid != ?', array($backupid));
     // Peform tests by comparing counts
     $this->assertEquals($c_notbackupid, 0);
     // there isn't any record with incorrect backupid
     $this->assertEquals($c_postsid, $f_forumpost);
     // All posts have been registered
     $this->assertEquals($c_dissuserid, $f_user1);
     // All users coming from discussions have been registered
     $this->assertEquals($c_ratuserid, $f_user2);
     // All users coming from ratings have been registered
     // Check file annotations against DB
     $fannotations = $DB->get_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'file'));
     $ffiles = $DB->get_records('files', array('contextid' => $this->contextid));
     $this->assertEquals(count($fannotations), count($ffiles));
     // Same number of recs in both (all files have been annotated)
     foreach ($fannotations as $annotation) {
         // Check ids annotated
         $this->assertTrue($DB->record_exists('files', array('id' => $annotation->itemid)));
     }
     // Drop the backup_ids_temp table
     backup_controller_dbops::drop_backup_ids_temp_table('testingid');
 }