forked from djplaner/moodle-booktool_github
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpush.php
122 lines (88 loc) · 4.07 KB
/
push.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?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/>.
/**
* Book GitHub export plugin
*
* @package booktool_github
* @copyright 2015 David Jones {@link http://djone.es}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(dirname(__FILE__).'/../../../../config.php');
require_once(dirname(__FILE__).'/locallib.php');
require_once($CFG->dirroot.'/mod/book/locallib.php');
require_once( __DIR__ . '/push_form.php' );
// Can this be put into a support function?
$cmid = required_param('id', PARAM_INT); // Course Module ID.
$cm = get_coursemodule_from_id('book', $cmid, 0, false, MUST_EXIST);
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
$book = $DB->get_record('book', ['id' => $cm->instance], '*', MUST_EXIST);
$toolurl = new moodle_url( '/mod/book/tool/github/index.php', ['id' => $cmid]);
$bookurl = new moodle_url( '/mod/book/view.php', ['id' => $cmid]);
$PAGE->set_url('/mod/book/tool/github/push.php');
require_login($course, false, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/book:read', $context);
require_capability('mod/book:edit', $context);
require_capability('mod/book:viewhiddenchapters', $context);
require_capability( 'booktool/github:export', $context );
$PAGE->navbar->add( 'GitHub tool', $toolurl );
$PAGE->navbar->add( get_string('push_form_crumb', 'booktool_github'),
new moodle_url( '/mod/book/tool/github/push.php',
['id' => $cmid]));
// Need to think about what events get added.
// \booktool_exportimscp\event\book_exported::create_from_book($book, $context)->trigger();!
// Show the header and initial display.
// Has this book been configured to use github?
$repodetails = booktool_github_get_repo_details( $book->id );
echo $OUTPUT->header();
// Get github client and github user details via oauth.
list( $githubclient, $githubuser ) = booktool_github_get_client( $cmid );
// Couldn't authenticate with github, probably never happen.
// TIDY UP.
if ( ! $githubclient ) {
print '<h1> Cannot authenticate with github</h1>';
echo $OUTPUT->footer();
die;
}
// Add the "owner" of this connection as the username from oAuth.
$repodetails['owner'] = $githubuser->getLogin();
// Start showing the form.
$form = new push_form( null, ['id' => $cmid ]);
// Build params for messages.
$giturl = 'http://github.com/' . $repodetails['owner'] . '/' .
$repodetails['repo'] . '/blob/master/' . $repodetails['path'];
$repourl = 'http://github.com/' . $repodetails['owner'] . '/' .
$repodetails['repo'] . '//';
$gituserurl = 'http://github.com/' . $repodetails['owner'];
$urls = ['book_url' => $bookurl->out(), 'tool_url' => $toolurl->out(),
'git_url' => $giturl, 'repo_url' => $repourl,
'git_user_url' => $gituserurl ];
if ( $fromform = $form->get_data() ) {
// User has submitted the form, they want to do the push.
// Grab the book content and combine into a single file.
// Commit the file.
if ( booktool_github_push_book( $githubclient, $repodetails,
$fromform->message ) ) {
print get_string('push_success', 'booktool_github', $urls);
} else {
print get_string('push_failure', 'booktool_github', $urls);
}
} else {
// Just display the initial warning.
print get_string( 'push_warning', 'booktool_github', $urls );
$form->display();
}
echo $OUTPUT->footer();