Skip to content

Commit

Permalink
Merge pull request #13 from KQMATH/backup
Browse files Browse the repository at this point in the history
Backup and restore added
  • Loading branch information
sebastsg committed Jul 5, 2018
2 parents acd5c81 + 86290fd commit 07845b5
Show file tree
Hide file tree
Showing 10 changed files with 440 additions and 54 deletions.
69 changes: 21 additions & 48 deletions ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,41 +498,22 @@ function session_info($session) {
*/
function handle_instructor_request($action, $session) {
switch ($action) {
case 'start_quiz':
return start_quiz($session);
case 'get_question_form':
return get_question_form($session);
case 'save_question':
return save_question($session);
case 'list_improvise_questions':
return show_all_improvise_questions($session->jazzquiz);
case 'list_jump_questions':
return show_all_jump_questions($session->jazzquiz);
case 'run_voting':
return run_voting($session);
case 'get_vote_results':
return get_vote_results($session);
case 'get_results':
return get_results($session);
case 'start_question':
return start_question($session);
case 'end_question':
return end_question($session);
case 'get_right_response':
return get_right_response($session);
case 'merge_responses':
return merge_responses($session);
case 'undo_merge':
return undo_merge($session);
case 'close_session':
return close_session($session);
case 'info':
return session_info($session);
default:
return [
'status' => 'error',
'message' => 'Invalid action'
];
case 'start_quiz': return start_quiz($session);
case 'get_question_form': return get_question_form($session);
case 'save_question': return save_question($session);
case 'list_improvise_questions': return show_all_improvise_questions($session->jazzquiz);
case 'list_jump_questions': return show_all_jump_questions($session->jazzquiz);
case 'run_voting': return run_voting($session);
case 'get_vote_results': return get_vote_results($session);
case 'get_results': return get_results($session);
case 'start_question': return start_question($session);
case 'end_question': return end_question($session);
case 'get_right_response': return get_right_response($session);
case 'merge_responses': return merge_responses($session);
case 'undo_merge': return undo_merge($session);
case 'close_session': return close_session($session);
case 'info': return session_info($session);
default: return ['status' => 'error', 'message' => 'Invalid action'];
}
}

Expand All @@ -544,19 +525,11 @@ function handle_instructor_request($action, $session) {
*/
function handle_student_request($action, $session) {
switch ($action) {
case 'save_question':
return save_question($session);
case 'save_vote':
return save_vote($session);
case 'get_question_form':
return get_question_form($session);
case 'info':
return session_info($session);
default:
return [
'status' => 'error',
'message' => 'Invalid action'
];
case 'save_question': return save_question($session);
case 'save_vote': return save_vote($session);
case 'get_question_form': return get_question_form($session);
case 'info': return session_info($session);
default: return ['status' => 'error', 'message' => 'Invalid action'];
}
}

Expand Down
59 changes: 59 additions & 0 deletions backup/moodle2/backup_jazzquiz_activity_task.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/mod/jazzquiz/backup/moodle2/backup_jazzquiz_stepslib.php');

/**
* Backup task that provides all the settings and steps to perform one complete backup.
*/
class backup_jazzquiz_activity_task extends backup_activity_task {

/**
* This should define settings. Not used at the moment.
*/
protected function define_my_settings() {

}

/**
* Define (add) particular steps this activity can have.
*/
protected function define_my_steps() {
$this->add_step(new backup_jazzquiz_activity_structure_step('jazzquiz_structure', 'jazzquiz.xml'));
// TODO: This might not be necessary in future Moodle versions, if discussed subclass is added.
$this->add_step(new backup_calculate_question_categories('activity_question_categories'));
$this->add_step(new backup_delete_temp_questions('clean_temp_questions'));
}

/**
* Code the transformations to perform in the activity in order to get transportable (encoded) links.
* @param string $content
* @return string of content with the URLs encoded
*/
static public function encode_content_links($content) {
global $CFG;
$base = preg_quote($CFG->wwwroot,"/");
// Link to the list of JazzQuizes.
$search = "/(" . $base . "\/mod\/jazzquiz\/index.php\?id\=)([0-9]+)/";
$content = preg_replace($search, '$@JAZZQUIZINDEX*$2@$', $content);
// Link to JazzQuiz view by moduleid.
$search = "/(" . $base . "\/mod\/jazzquiz\/view.php\?id\=)([0-9]+)/";
$content = preg_replace($search, '$@JAZZQUIZVIEWBYID*$2@$', $content);
return $content;
}
}
144 changes: 144 additions & 0 deletions backup/moodle2/backup_jazzquiz_stepslib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

defined('MOODLE_INTERNAL') || die();

/**
* Define the complete choice structure for backup, with file and id annotations.
*/
class backup_jazzquiz_activity_structure_step extends backup_questions_activity_structure_step {

protected function define_structure() {

// To know if we are including userinfo.
$userinfo = $this->get_setting_value('userinfo');

// Define each element separated.
$jazzquiz = new backup_nested_element('jazzquiz', ['id'], [
'name',
'intro',
'introformat',
'timecreated',
'timemodified',
'defaultquestiontime',
'waitforquestiontime'
]);

$questions = new backup_nested_element('questions');
$question = new backup_nested_element('question', ['id'], [
'questionid',
'questiontime',
'slot'
]);

$sessions = new backup_nested_element('sessions');
$session = new backup_nested_element('session', ['id'], [
'name',
'sessionopen',
'showfeedback',
'status',
'slot',
'currentquestiontime',
'nextstarttime',
'created'
]);

$sessionquestions = new backup_nested_element('sessionquestions');
$sessionquestion = new backup_nested_element('sessionquestion', ['id'], [
'sessionid',
'questionid',
'questiontime',
'slot'
]);

$attempts = new backup_nested_element('attempts');
$attempt = new backup_nested_element('attempt', ['id'], [
'sessionid',
'userid',
'questionengid',
'status',
'preview',
'responded',
'timestart',
'timefinish',
'timemodified'
]);
$this->add_question_usages($attempt, 'questionengid');

$merges = new backup_nested_element('merges');
$merge = new backup_nested_element('merge', ['id'], [
'sessionid',
'slot',
'ordernum',
'original',
'merged'
]);

$votes = new backup_nested_element('votes');
$vote = new backup_nested_element('vote', ['id'], [
'sessionid',
'attempt',
'initialcount',
'finalcount',
'userlist',
'qtype',
'slot'
]);

// Build the tree.
$jazzquiz->add_child($questions);
$jazzquiz->add_child($sessions);

$questions->add_child($question);

$sessions->add_child($session);
$session->add_child($sessionquestions);
$session->add_child($attempts);
$session->add_child($merges);
$session->add_child($votes);

$sessionquestions->add_child($sessionquestion);
$attempts->add_child($attempt);
$merges->add_child($merge);
$votes->add_child($vote);

// Define sources.
$jazzquiz->set_source_table('jazzquiz', ['id' => backup::VAR_ACTIVITYID]);
$question->set_source_table('jazzquiz_questions', ['jazzquizid' => backup::VAR_PARENTID]);
if ($userinfo) {
$session->set_source_table('jazzquiz_sessions', ['jazzquizid' => backup::VAR_PARENTID]);
$sessionquestion->set_source_table('jazzquiz_session_questions', ['sessionid' => backup::VAR_PARENTID]);
$attempt->set_source_table('jazzquiz_attempts', ['sessionid' => backup::VAR_PARENTID]);
$merge->set_source_table('jazzquiz_merges', ['sessionid' => backup::VAR_PARENTID]);
$vote->set_source_table('jazzquiz_votes', [
'jazzquizid' => backup::VAR_ACTIVITYID,
'sessionid' => backup::VAR_PARENTID
]);
}

// Define id annotations.
$attempt->annotate_ids('user', 'userid');
$question->annotate_ids('question', 'questionid');
$sessionquestion->annotate_ids('question', 'questionid');

// Define file annotations.
$jazzquiz->annotate_files('mod_jazzquiz', 'intro', null);

// Return the root element (choice), wrapped into standard activity structure.
return $this->prepare_activity_structure($jazzquiz);
}

}
74 changes: 74 additions & 0 deletions backup/moodle2/restore_jazzquiz_activity_task.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/mod/jazzquiz/backup/moodle2/restore_jazzquiz_stepslib.php');

/**
* JazzQuiz restore task that provides all the settings and steps to perform one
* complete restore of the activity
*/
class restore_jazzquiz_activity_task extends restore_activity_task {

/**
* This should define settings. Not used at the moment.
*/
protected function define_my_settings() {

}

/**
* Define the structure steps.
*/
protected function define_my_steps() {
$this->add_step(new restore_jazzquiz_activity_structure_step('jazzquiz_structure', 'jazzquiz.xml'));
}

/**
* @return restore_decode_content[]
*/
static public function define_decode_contents() {
return [
new restore_decode_content('jazzquiz', ['intro'])
];
}

/**
* @return restore_decode_rule[]
*/
static public function define_decode_rules() {
return [
new restore_decode_rule('JAZZQUIZVIEWBYID', '/mod/jazzquiz/view.php?id=$1', 'course_module'),
new restore_decode_rule('JAZZQUIZINDEX', '/mod/jazzquiz/index.php?id=$1', 'course')
];
}

/**
* @return restore_log_rule[]
*/
static public function define_restore_log_rules() {
return [];
}

/**
* @return restore_log_rule[]
*/
static public function define_restore_log_rules_for_course() {
return [];
}

}
Loading

0 comments on commit 07845b5

Please sign in to comment.