Skip to content

Commit febbb46

Browse files
committed
fix #19: Git statistics data page
1 parent 806fa08 commit febbb46

File tree

6 files changed

+210
-6
lines changed

6 files changed

+210
-6
lines changed

assignment.class.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,15 @@ private function view_repos() {
155155

156156
echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
157157
echo html_writer::tag('h3', get_string('githubreposetting', 'assignment_github'), array('class' => 'git_h3'));
158+
158159
echo $OUTPUT->box_start('generalbox boxaligncenter git_box');
159160

161+
// Statistics link
162+
$url = new moodle_url("/mod/assignment/type/github/log.php?id={$this->cm->id}");
163+
$link = html_writer::link($url, get_string('statistics', 'assignment_github'));
164+
echo '<div class="git_checklink reportlink">'.$link.'</div>';
165+
echo '<div class="clearer"></div>';
166+
160167
$mform = new mod_assignment_github_edit_form(null, array('group' => $this->group, 'repo' => null, 'submission' => null));
161168
if (!$mform->is_cancelled() && $github_info = $mform->get_submitted_data()) {
162169
try {
@@ -414,6 +421,10 @@ public function print_student_answer($userid, $return=false) {
414421
$service =& $this->git->get_api_service($repo->server);
415422
$url = $service->generate_http_from_git($repo->url);
416423
$link = html_writer::link($url['repo'], $repo->repo, array('target' => '_blank'));
424+
$output .= '<span>'.$link.'</span> ';
425+
426+
$url = new moodle_url("/mod/assignment/type/github/log.php?id={$this->cm->id}&group={$group->id}");
427+
$link = html_writer::link($url, get_string('statistics', 'assignment_github'));
417428
$output .= '<span>'.$link.'</span><br/>';
418429
}
419430
}
@@ -423,6 +434,10 @@ public function print_student_answer($userid, $return=false) {
423434
$service =& $this->git->get_api_service($repo->server);
424435
$url = $service->generate_http_from_git($repo->url);
425436
$link = html_writer::link($url['repo'], $repo->repo, array('target' => '_blank'));
437+
$output .= '<span>'.$link.'</span>';
438+
439+
$url = new moodle_url("/mod/assignment/type/github/log.php?id={$this->cm->id}&group={$group->id}");
440+
$link = html_writer::link($url, get_string('statistics', 'assignment_github'));
426441
$output .= '<span>'.$link.'</span><br/>';
427442
}
428443
}

lang/en/assignment_github.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
$string['emailnotset'] = 'Email is not set.';
88
$string['githubreposetting'] = 'Git Repo';
99
$string['githubreposettinglist'] = 'All repo list';
10+
$string['latestcommits'] = 'Latest commits';
1011
$string['memberemail'] = '{$a}\'s email';
1112
$string['memberemail_help'] = 'Email which the member use to commit code into this repository';
1213
$string['memberlist'] = 'Member List';
@@ -21,6 +22,8 @@
2122
$string['repositoryrourl'] = 'Repository Read-only URL';
2223
$string['repositoryrourl_help'] = 'Your git repository\'s read-only url. Such as git://github.com/username/projectname.git';
2324
$string['serviceerror'] = 'Service Error. Please check the url. If this error still happens, please contact the administrator.';
25+
$string['statistics'] = 'Statistics Data';
26+
$string['statisticsdata'] = '{$a}\'s Statistics Data';
2427
$string['typegithub'] = 'Github Project';
2528
$string['unknownserver'] = 'Unknown Server. Please check the url.';
2629
$string['unknowntype'] = 'Unknown type parameter';

lang/zh_cn/assignment_github.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
$string['emailnotset'] = '邮箱未设置';
88
$string['githubreposetting'] = 'Git容器';
99
$string['githubreposettinglist'] = '所有容器列表';
10+
$string['latestcommits'] = '最近的提交';
1011
$string['memberemail'] = '{$a}的email';
1112
$string['memberemail_help'] = '此用户在此git容器上提交代码所用的邮箱';
1213
$string['memberlist'] = '成员列表';
@@ -21,6 +22,8 @@
2122
$string['repositoryrourl'] = '容器只读URL';
2223
$string['repositoryrourl_help'] = 'Git容器的只读链接。例如:git://github.com/username/projectname.git';
2324
$string['serviceerror'] = '服务出错。请检查链接是否输入正确。如果仍旧发生此错误,请联系站点管理员。';
25+
$string['statistics'] = '统计数据';
26+
$string['statisticsdata'] = '{$a} 的统计数据';
2427
$string['typegithub'] = 'Github项目';
2528
$string['unknownserver'] = '无法识别的托管服务器。请检查链接是否输入正确。';
2629
$string['unknowntype'] = '未知的类型参数';

log.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
3+
require("../../../../config.php");
4+
require("assignment.class.php");
5+
6+
$id = required_param('id', PARAM_INT); // Course Module ID
7+
8+
if (! $cm = get_coursemodule_from_id('assignment', $id)) {
9+
print_error('invalidcoursemodule');
10+
}
11+
12+
if (! $assignment = $DB->get_record("assignment", array("id"=>$cm->instance))) {
13+
print_error('invalidid', 'assignment');
14+
}
15+
16+
if (! $course = $DB->get_record("course", array("id"=>$assignment->course))) {
17+
print_error('coursemisconf', 'assignment');
18+
}
19+
20+
require_login($course->id, false, $cm);
21+
22+
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
23+
if (!has_capability('mod/assignment:view', $context)) {
24+
print_error('cannotviewassignment', 'assignment');
25+
}
26+
27+
if ($assignment->assignmenttype != 'github') {
28+
print_error('invalidtype', 'assignment');
29+
}
30+
31+
$PAGE->set_url('/mod/assignment/type/github/log.php', array('id'=>$id));
32+
$PAGE->set_title(get_string('statistics', 'assignment_github'));
33+
$PAGE->set_heading($course->fullname);
34+
$PAGE->set_context($context);
35+
$PAGE->set_cm($cm);
36+
$PAGE->requires->css('/mod/assignment/type/'.$assignment->assignmenttype.'/styles.css');
37+
38+
$groupmode = groups_get_activity_groupmode($cm);
39+
if ($groupmode) {
40+
$aag = has_capability('moodle/site:accessallgroups', $context);
41+
42+
if ($groupmode == VISIBLEGROUPS or $aag) {
43+
$allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
44+
} else {
45+
$allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid);
46+
}
47+
48+
$groupid = groups_get_activity_group($cm, true, $allowedgroups);
49+
50+
// Group 0 (all groups) is not allowed to use
51+
// change groupid to the first allowed group's id
52+
if (!$groupid) {
53+
$group = array_shift($allowedgroups);
54+
$groupid = $group->id;
55+
$allowedgroups[$groupid] = $group;
56+
}
57+
$name = $allowedgroups[$groupid]->name;
58+
} else {
59+
$name = fullname($USER);
60+
}
61+
62+
$assignmentinstance = new assignment_github($cm->id, $assignment, $cm, $course);
63+
$git = new git($course->id, $assignment->id);
64+
$logger = new git_logger($assignment->id);
65+
66+
// Load emails, repo, logs and statistics infomation
67+
if ($groupmode) {
68+
$members = groups_get_members($groupid, 'u.*', 'lastname ASC');
69+
$emails = array();
70+
foreach($members as $userid => $member) {
71+
$submission = $assignmentinstance->get_submission($userid);
72+
if (!empty($submission->data1)) {
73+
$emails[$submission->data1] = $userid;
74+
}
75+
}
76+
$repo = $git->get_by_group($groupid);
77+
$statistics = $logger->get_statistics_by_group($groupid);
78+
$logs = $logger->get_by_group($groupid, '', '', 0, 10);
79+
} else {
80+
$submission = $assignmentinstance->get_submission($USER->id);
81+
$emails = array();
82+
if (!empty($submission->data1)) {
83+
$emails[$submission->data1] = $USER->id;
84+
}
85+
$repo = $git->get_by_user($USER->id);
86+
$statistics = $logger->get_statistics_by_user($USER->id);
87+
$logs = $logger->get_by_user($USER->id, '', '', 0, 10);
88+
}
89+
90+
$service =& $git->get_api_service($repo->server);
91+
92+
echo $OUTPUT->header();
93+
groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/assignment/type/github/log.php?id=' . $cm->id, false, true);
94+
echo $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
95+
echo html_writer::tag('h3', get_string('statisticsdata', 'assignment_github', $name), array('class' => 'git_h3'));
96+
echo $OUTPUT->box_start('generalbox boxaligncenter git_log');
97+
98+
// Statistics
99+
echo html_writer::tag('h4', get_string('statistics', 'assignment_github'));
100+
$statistics_title = array('Author', 'Commits', 'Files', '+', '-', 'Total');
101+
$statistics_table = html_writer::start_tag('table', array('class' => 'generaltable'));
102+
$statistics_table .= '<tr>';
103+
foreach($statistics_title as $title) {
104+
$statistics_table .= '<th>' . $title . '</th>';
105+
}
106+
$statistics_table .= '</tr>';
107+
if ($statistics) {
108+
$total = array_pop($statistics);
109+
if (!$statistics) {
110+
array_push($statistics, $total);
111+
}
112+
113+
foreach($statistics as $line) {
114+
$statistics_table .= '<tr>';
115+
if (empty($emails[$line->email])) {
116+
$author = $line->author;
117+
} else {
118+
$author = fullname($members[$emails[$line->email]]);
119+
}
120+
$statistics_table .= '<td>'.$author.'</td>';
121+
$statistics_table .= '<td>'.$line->commits.' ('.round($line->commits/$total->commits * 100, 2).'%)</td>';
122+
$statistics_table .= '<td>'.$line->files.' ('.round($line->files/$total->files * 100, 2).'%)</td>';
123+
$statistics_table .= '<td class="green">'.$line->insertions.' ('.round($line->insertions/$total->insertions * 100, 2).'%)</td>';
124+
$statistics_table .= '<td class="red">'.$line->deletions.' ('.round($line->deletions/$total->deletions * 100, 2).'%)</td>';
125+
$statistics_table .= '<td>'.$line->total.' ('.round($line->total/$total->total * 100, 2).'%)</td>';
126+
$statistics_table .= '</tr>';
127+
}
128+
129+
$statistics_table .= '<tr>';
130+
$statistics_table .= '<td>'.get_string('total').'</td>';
131+
$statistics_table .= '<td>'.$total->commits.'</td>';
132+
$statistics_table .= '<td>'.$total->files.'</td>';
133+
$statistics_table .= '<td class="green">'.$total->insertions.'</td>';
134+
$statistics_table .= '<td class="red">'.$total->deletions.'</td>';
135+
$statistics_table .= '<td>'.$total->total.'</td>';
136+
$statistics_table .= '</tr>';
137+
}
138+
echo $statistics_table .= html_writer::end_tag('table');
139+
140+
// Log
141+
echo html_writer::tag('h4', get_string('latestcommits', 'assignment_github'));
142+
$log_title = array('Commit', 'Author', 'Subject', 'Files', '+', '-', 'Date');
143+
$log_table = html_writer::start_tag('table', array('class' => 'generaltable'));
144+
$log_table .= '<tr>';
145+
foreach($log_title as $title) {
146+
$log_table .= '<th>' . $title . '</th>';
147+
}
148+
$log_table .= '</tr>';
149+
if ($logs) {
150+
foreach($logs as $commit => $log) {
151+
$log_table .= '<tr>';
152+
$commit_link = html_writer::link($service->generate_commit_url($repo->url, $log->commit),
153+
shorten_text($log->commit, 11), array('target' => '_blank'));
154+
$log_table .= '<td>'.$commit_link.'</td>';
155+
156+
if (empty($emails[$log->email])) {
157+
$author = $log->author;
158+
} else {
159+
$author = fullname($members[$emails[$log->email]]);
160+
}
161+
$log_table .= '<td>'.$author.'</td>';
162+
163+
$log_table .= '<td class="subject">'.$log->subject.'</td>';
164+
$log_table .= '<td>'.$log->files.'</td>';
165+
$log_table .= '<td class="green">'.$log->insertions.'</td>';
166+
$log_table .= '<td class="red">'.$log->deletions.'</td>';
167+
$log_table .= '<td>'.userdate($log->date).'</td>';
168+
$log_table .= '</tr>';
169+
}
170+
}
171+
echo $log_table .= html_writer::end_tag('table');
172+
173+
echo $OUTPUT->box_end();
174+
echo $OUTPUT->box_end();
175+
echo $OUTPUT->footer();

modules/git_logger.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ function get_statistics_by_group($groupid) {
5050

5151
$sql = "SELECT
5252
email, author, COUNT(*) AS commits, SUM(files) AS files,
53-
SUM(insertions) AS insertions, SUM(deletions) AS deletions
53+
SUM(insertions) AS insertions, SUM(deletions) AS deletions,
54+
SUM(insertions)+SUM(deletions) AS total
5455
FROM `{$CFG->prefix}{$this->_table}`
5556
WHERE `assignment`={$this->assignment} AND `groupid`={$groupid}
5657
GROUP BY `email`
5758
UNION
5859
SELECT
5960
'total' email, '' author, COUNT(*) AS commits, SUM(files) AS files,
60-
SUM(insertions) AS insertions, SUM(deletions) AS deletions
61+
SUM(insertions) AS insertions, SUM(deletions) AS deletions,
62+
SUM(insertions)+SUM(deletions) AS total
6163
FROM `{$CFG->prefix}{$this->_table}`
6264
WHERE `assignment`={$this->assignment} AND `groupid`={$groupid}
6365
GROUP BY `assignment`";
@@ -74,7 +76,8 @@ function get_statistics_by_user($userid) {
7476

7577
$sql = "SELECT
7678
userid, author, COUNT(*) AS commits, SUM(files) AS files,
77-
SUM(insertions) AS insertions, SUM(deletions) AS deletions
79+
SUM(insertions) AS insertions, SUM(deletions) AS deletions,
80+
SUM(insertions)+SUM(deletions) AS total
7881
FROM `{$CFG->prefix}{$this->_table}`
7982
WHERE `assignment`={$this->assignment} AND `userid`={$userid}
8083
AND `groupid`=0
@@ -91,7 +94,8 @@ function get_statistics_by_email($email) {
9194

9295
$sql = "SELECT
9396
email, author, COUNT(*) AS commits, SUM(files) AS files,
94-
SUM(insertions) AS insertions, SUM(deletions) AS deletions
97+
SUM(insertions) AS insertions, SUM(deletions) AS deletions,
98+
SUM(insertions)+SUM(deletions) AS total
9599
FROM `{$CFG->prefix}{$this->_table}`
96100
WHERE `assignment`={$this->assignment} AND `email`='{$email}'
97101
AND `groupid`=0
@@ -109,7 +113,8 @@ function get_statistics_by_group_email($groupid, $email) {
109113

110114
$sql = "SELECT
111115
email, author, COUNT(*) AS commits, SUM(files) AS files,
112-
SUM(insertions) AS insertions, SUM(deletions) AS deletions
116+
SUM(insertions) AS insertions, SUM(deletions) AS deletions,
117+
SUM(insertions)+SUM(deletions) AS total
113118
FROM `{$CFG->prefix}{$this->_table}`
114119
WHERE `assignment`={$this->assignment} AND `groupid`={$groupid}
115120
AND `email`='{$email}'

styles.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
.git_h3 {text-align: center; margin: 10px 0 10px 0;}
2-
.git_box {margin: 20px 10px 0 10px;}
2+
.git_box, .git_log {margin: 20px 10px 0 10px;}
33
.git_box .generaltable th {min-width: 125px; text-align: center;}
44
.git_box .generaltable td.c1 {min-width: 100px; text-align: center; font-weight: bold;}
5+
.git_log .generaltable th {text-align: center;}
6+
.git_log .generaltable .subject {max-width: 350px;}
7+
.git_log .red {color: red;}
58
.git_box .fitemtitle {min-width: 140px;}
69
.git_box .felement input {min-width: 240px;}
710
.git_error {text-align: center; background-color: #FFF; border: 1px red solid; margin-bottom: 15px;}

0 commit comments

Comments
 (0)