Moosh\Command\Moodle29\Course\CourseInfo::execute PHP Method

execute() public method

public execute ( )
    public function execute()
    {
        global $CFG, $DB;
        require_once $CFG->libdir . '/accesslib.php';
        //some variables you may want to use
        //$this->cwd - the directory where moosh command was executed
        //$this->mooshDir - moosh installation directory
        //$this->expandedOptions - commandline provided options, merged with defaults
        //$this->topDir - top Moodle directory
        //$this->arguments[0] - first argument passed
        //$this->pluginInfo - array with information about the current plugin (based on cwd), keys:'type','name','dir'
        $options = $this->expandedOptions;
        $courseid = $this->arguments[0];
        $this->course = get_course($courseid);
        $coursecontext = \context_course::instance($courseid, MUST_EXIST);
        // Get # of contexts with break-down.
        $sql = "SELECT * FROM {context} WHERE path LIKE '{$coursecontext->path}/%'";
        $dbcontexts = $DB->get_records_sql($sql);
        foreach ($dbcontexts as $dbcontext) {
            $context = \context::instance_by_id($dbcontext->id, MUST_EXIST);
            $this->contexts[$dbcontext->id] = $context;
            $this->inc($this->contextbylevel, $context->contextlevel);
            /** @var \context $context */
            if ($this->verbose) {
                echo $context->get_context_name();
            }
            if (is_a($context, "context_module")) {
                $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
                $this->inc($this->contextbymodule, $cm->module);
            }
        }
        ksort($this->contextbylevel);
        ksort($this->contextbymodule);
        // Get # of role permission overwrites for this course.
        foreach ($dbcontexts as $dbcontext) {
            $capabilities = $DB->get_records('role_capabilities', array('contextid' => $dbcontext->id));
            if ($capabilities) {
                $this->capabilityoverwrites[$dbcontext->id] = count($capabilities);
            }
        }
        // Locally assigned roles.
        foreach ($dbcontexts as $dbcontext) {
            $assignments = $DB->get_records('role_assignments', array('contextid' => $dbcontext->id));
            if ($assignments) {
                $this->rolesassigned[$dbcontext->id] = count($assignments);
            }
        }
        // Get users enrolled.
        $enrolledtotal = $DB->get_record_sql("SELECT COUNT(DISTINCT userid) AS c FROM {role_assignments} WHERE contextid = ? ", array($coursecontext->id));
        $this->enrolledtotal = $enrolledtotal->c;
        $usersbyrole = $DB->get_records_sql("SELECT roleid, COUNT(*) AS c FROM {role_assignments} WHERE contextid = ?", array($coursecontext->id));
        foreach ($usersbyrole as $u) {
            if ($u->c > 0) {
                $this->usersbyrole[$u->roleid] = $u->c;
            }
        }
        // Get # of groups. Get min, max and avg number of users per group.
        $groups = $DB->get_records_sql("SELECT g.id, COUNT(m.id) AS c FROM {groups} g LEFT JOIN {groups_members} m ON g.id = m.groupid WHERE g.courseid = ? GROUP BY g.id", array($courseid));
        $this->groupsnumber = count($groups);
        $sum = 0;
        foreach ($groups as $group) {
            $sum += $group->c;
            if ($this->groupsmax < $group->c) {
                $this->groupsmax = $group->c;
            }
            if ($this->groupsmin === NULL || $this->groupsmin > $group->c) {
                $this->groupsmin = $group->c;
            }
        }
        if ($this->groupsnumber > 0) {
            $this->groupsavg = intval($sum / $this->groupsnumber);
        } else {
            $this->groupsavg = 0;
        }
        if ($this->groupsmin === NULL) {
            $this->groupsmin = 0;
        }
        // Get size of modinfo course structure.
        $modinfo = get_fast_modinfo($this->course);
        $this->modinfosize = strlen(serialize($modinfo));
        // Get # of sections.
        $sections = $DB->get_records('course_sections', array('course' => $courseid));
        $this->sectionsnumber = count($sections);
        $modstotal = 0;
        foreach ($sections as $section) {
            $this->sectionsvisible += $section->visible;
            if (!$section->sequence) {
                $mods = 0;
            } else {
                $mods = substr_count($section->sequence, ',') + 1;
            }
            $modstotal += $mods;
            if ($mods > $this->sectionsmax) {
                $this->sectionsmax = $mods;
            }
            if ($this->sectionsmin === NULL || $mods < $this->sectionsmin) {
                $this->sectionsmin = $mods;
            }
        }
        $this->sectionsavg = intval($modstotal / $this->sectionsnumber);
        $this->sectionshidden = $this->sectionsnumber - $this->sectionsvisible;
        // Get # of grades.
        $this->gradesnumber = $DB->get_record_sql("SELECT COUNT(*) c FROM {grade_items} i JOIN {grade_grades} g ON i.id = g.itemid WHERE i.courseid = ?", array($courseid));
        $this->gradesnumber = $this->gradesnumber->c;
        // Get # of log entries.
        $this->logsnumber = $DB->get_record("logstore_standard_log", array('courseid' => $courseid), 'COUNT(*) c');
        $this->logsnumber = $this->logsnumber->c;
        // Get # and size of files.
        $results = $DB->get_records_sql("SELECT * FROM {context} WHERE path LIKE '" . $context->get_course_context()->path . "/%'");
        foreach ($results as $result) {
            $contexts[] = $result->id;
        }
        list($sql, $params) = $DB->get_in_or_equal($contexts);
        $files = $DB->get_record_sql("SELECT COUNT(*) c FROM {files} WHERE filename <> '.' AND contextid IN (SELECT id FROM {context} WHERE path LIKE '{$coursecontext->path}/%' )");
        $this->filesnumber = $files->c;
        $files = $DB->get_record_sql("SELECT SUM(filesize) s FROM {files} WHERE filename <> '.' AND contextid IN (SELECT id FROM {context} WHERE path LIKE '{$coursecontext->path}/%' )");
        $this->filesize = $files->s;
        $this->aggregateData();
        // Cache build time.
        $start = microtime(true);
        rebuild_course_cache($courseid);
        $this->data['Cache build time'] = microtime(true) - $start;
        $this->display();
    }