// (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/>.
/**
 * Aggregates activity data from all supported sources
 *
 * @package     local_dev
 * @copyright   2012 David Mudrak <*****@*****.**>
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
define('CLI_SCRIPT', 1);
require dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php';
require_once $CFG->libdir . '/clilib.php';
require_once $CFG->dirroot . '/local/dev/locallib.php';
list($options, $unrecognized) = cli_get_params(array('help' => false), array('h' => 'help'));
if ($options['help']) {
    fputs(STDOUT, "Aggregates activity data from all supported sources\n\n--help                  Display this help\n\n");
    exit(0);
}
fputs(STDOUT, 'AGGREGATE JOB STARTED ON ' . date('Y-m-d H:i', time()) . PHP_EOL);
$aggregator = new dev_aggregator();
$aggregator->add_source('gitcommits');
$aggregator->add_source('gitmerges');
$aggregator->execute();
fputs(STDOUT, 'AGGREGATE JOB FINISHED ON ' . date('Y-m-d H:i', time()) . PHP_EOL);
if (!is_null($version)) {
    if ($version !== 'x.x.x') {
        $version = preg_replace('/[^x0-9\\.]/', '', $version);
    }
}
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
$PAGE->set_url(new local_dev_url('/local/dev/contributions.php', array('version' => $version)));
$PAGE->add_body_class('path-local-dev');
$PAGE->set_title(get_string('pluginname', 'local_dev'));
$PAGE->set_heading(get_string('pluginname', 'local_dev'));
$output = $PAGE->get_renderer('local_dev');
// prepare the drop down box with versions
$options = array('x.x.x' => get_string('allversions', 'local_dev'));
$validversion = is_null($version);
$branches = dev_aggregator::get_branches();
foreach ($branches as $branch => $vers) {
    if ($version === 'x.x.x') {
        $validversion = true;
    } else {
        if ($version === $branch) {
            $validversion = true;
        }
    }
    $optgroup = array($branch => get_string('allversionsonbranch', 'local_dev', $branch));
    foreach ($vers as $ver) {
        $optgroup[$ver] = $ver;
        if ($version === $ver) {
            $validversion = true;
        }
    }
 /**
  * Aggregate commits
  */
 public function on_execute()
 {
     global $DB;
     // aggregate the number of commits into a big in-memory array first
     $sql = $this->get_sql();
     $rs = $DB->get_recordset_sql($sql);
     $commits = array();
     $unknownusers = array();
     foreach ($rs as $record) {
         $userid = $this->calculate_user_id($record);
         if (!is_numeric($userid) and !isset($unknownusers[$userid])) {
             $unknownusers[$userid] = $this->calculate_user_details($record);
         }
         $version = $this->tag_to_version($record->tag);
         if (!isset($commits[$userid][$version])) {
             $commits[$userid][$version] = 0;
         }
         $commits[$userid][$version] += $record->commits;
     }
     $rs->close();
     // update the aggregated values in the database
     foreach ($commits as $userid => $versions) {
         foreach ($versions as $version => $metric) {
             if (is_numeric($userid)) {
                 $user = $userid;
             } else {
                 $user = new stdClass();
                 $user->firstname = $unknownusers[$userid]->firstname;
                 $user->lastname = $unknownusers[$userid]->lastname;
                 $user->email = $unknownusers[$userid]->email;
             }
             dev_aggregator::update_activity($user, $version, $this->get_metric_column(), $metric);
         }
     }
 }