|
| 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(); |
0 commit comments