예제 #1
0
파일: mahara.php 프로젝트: kienv/mahara
function progressbar_sideblock($preview = false)
{
    global $USER;
    // TODO: Remove this URL param from here, and when previewing pass institution
    // by function param instead
    $institution = param_alphanum('i', null);
    if (is_array($USER->institutions) && count($USER->institutions) > 0) {
        // Get all institutions where user is member
        $institutions = array();
        foreach ($USER->institutions as $inst) {
            if (empty($inst->suspended)) {
                $institutions = array_merge($institutions, array($inst->institution => $inst->displayname));
            }
        }
        // Set user's first institution in case that institution isn't
        // set yet or user is not member of currently set institution.
        if (!$institution || !array_key_exists($institution, $institutions)) {
            $institution = key(array_slice($institutions, 0, 1));
        }
    } else {
        $institutions = array();
        $institution = 'mahara';
    }
    // Set appropriate preview according to institution, if the institutio is selected
    // If the institution isn't selected then show preview for first institution, which
    // is also selected as a default value in institution selection box
    if ($preview) {
        $default = get_column('institution', 'name');
        // TODO: Remove this URL param from here, and when previewing pass institution
        // by function param instead
        $institution = param_alphanum('institution', $default[0]);
    }
    // We need to check to see if any of the institutions have profile completeness to allow
    // the select box to work correctly for users with more than one institution
    $multiinstitutionprogress = false;
    $counting = null;
    if (!empty($institutions)) {
        foreach ($institutions as $key => $value) {
            if ($result = get_records_select_assoc('institution_config', 'institution=? and field like \'progressbaritem_%\'', array($key), 'field', 'field, value')) {
                $multiinstitutionprogress = true;
                if ($key == $institution) {
                    $counting = $result;
                    break;
                }
            }
        }
    } else {
        $counting = get_records_select_assoc('institution_config', 'institution=? and field like \'progressbaritem_%\'', array($institution), 'field', 'field, value');
    }
    // Get artefacts that count towards profile completeness
    if ($counting) {
        // Without locked ones (site locked and institution locked)
        $sitelocked = (array) get_column('institution_locked_profile_field', 'profilefield', 'name', 'mahara');
        $instlocked = (array) get_column('institution_locked_profile_field', 'profilefield', 'name', $institution);
        $locked = $sitelocked + $instlocked;
        foreach ($locked as $l) {
            unset($counting["progressbaritem_internal_{$l}"]);
        }
        $totalcounting = 0;
        foreach ($counting as $c) {
            $totalcounting = $totalcounting + $c->value;
        }
        // Get all artefacts for progressbar and create data structure
        $data = array();
        // For the artefact_get_progressbar_items function, we want them indexed by plugin
        // and then subindexed by artefact. For most other purposes, having them indexed
        // by config name is sufficient
        $onlytheseplugins = array();
        foreach ($counting as $key => $obj) {
            // This one has no value. So remove it from the list.
            if (!$obj->value) {
                unset($counting[$key]);
                continue;
            }
            $parts = explode('_', $obj->field);
            $plugin = $parts[1];
            $item = $parts[2];
            if (empty($onlytheseplugins[$plugin])) {
                $onlytheseplugins[$plugin] = array();
            }
            $onlytheseplugins[$plugin][$item] = $item;
        }
        $progressbaritems = artefact_get_progressbar_items($onlytheseplugins);
        // Get the data link about every item
        foreach ($progressbaritems as $pluginname => $itemlist) {
            foreach ($itemlist as $artefactname => $item) {
                $itemname = "progressbaritem_{$pluginname}_{$artefactname}";
                $c = $counting[$itemname];
                $target = $c->value;
                $completed = 0;
                $data[$itemname] = array('artefact' => $artefactname, 'link' => progressbar_artefact_link($pluginname, $artefactname), 'counting' => $target, 'completed' => $completed, 'display' => (bool) $c->value, 'label' => progressbar_artefact_task_label($pluginname, $artefactname, $target, $completed));
            }
        }
        if ($preview) {
            $percent = 0;
        } else {
            // Since this is not a preview, gather data about the users' actual progress,
            // and update the records we placed in $data.
            // Get a list of all the basic artefact types in this progress bar.
            $nonmeta = array();
            foreach ($progressbaritems as $plugin => $pluginitems) {
                foreach ($pluginitems as $itemname => $item) {
                    if (!$item->ismeta) {
                        $nonmeta[] = $itemname;
                    }
                }
            }
            if ($nonmeta) {
                // To reduce the number of queries, we gather data about all the user's artefacts
                // at once. (Metaartefacts are handled separately, below)
                $insql = "'" . implode("','", $nonmeta) . "'";
                $sql = "SELECT artefacttype, (select plugin from {artefact_installed_type} ait where ait.name=a.artefacttype) as plugin, COUNT(*) AS completed\n                        FROM {artefact} a\n                        WHERE owner = ?\n                        AND artefacttype in ({$insql})\n                        GROUP BY artefacttype";
                $normalartefacts = get_records_sql_array($sql, array($USER->get('id')));
                if (!$normalartefacts) {
                    $normalartefacts = array();
                }
            } else {
                // No basic artefacts in this one, so we just use an empty array for this.
                $normalartefacts = array();
            }
            $totalcompleted = 0;
            $metaartefacts = array();
            foreach ($progressbaritems as $plugin => $pluginitems) {
                if (is_array($records = artefact_get_progressbar_metaartefacts($plugin, $pluginitems))) {
                    foreach ($records as $record) {
                        $record->plugin = $plugin;
                        array_push($metaartefacts, $record);
                    }
                }
            }
            foreach (array_merge($normalartefacts, $metaartefacts) as $record) {
                $itemname = "progressbaritem_{$record->plugin}_{$record->artefacttype}";
                // It's not an item we're tracking, so skip it.
                if (!array_key_exists($itemname, $counting)) {
                    continue;
                }
                $target = $counting[$itemname]->value;
                $remaining = max(0, $target - $record->completed);
                // Override the data for this item
                $data[$itemname]['completed'] = $record->completed;
                $data[$itemname]['display'] = $remaining > 0;
                $data[$itemname]['label'] = $label = get_string('progress_' . $record->artefacttype, 'artefact.' . $record->plugin, $remaining);
                if ($target > 0) {
                    $totalcompleted = $totalcompleted + min($target, $record->completed);
                }
            }
            $percent = round($totalcompleted / $totalcounting * 100);
            if ($percent > 100) {
                $percent = 100;
            }
        }
        return array('data' => $data, 'percent' => $percent, 'preview' => $preview, 'count' => $preview ? 1 : count($institutions), 'institutions' => $institutions, 'institution' => $institution, 'totalcompleted' => !empty($totalcompleted) ? $totalcompleted : 0, 'totalcounting' => $totalcounting);
    } else {
        if ($multiinstitutionprogress) {
            return array('data' => null, 'percent' => 0, 'preview' => $preview, 'count' => $preview ? 1 : count($institutions), 'institutions' => $institutions, 'institution' => $institution, 'totalcompleted' => 0, 'totalcounting' => 0);
        }
    }
    return array('data' => null, 'percent' => 0, 'preview' => $preview, 'count' => 1, 'institutions' => null, 'institution' => 'mahara');
}
예제 #2
0
        // Check format
        if (count($parts) < 3) {
            continue;
        }
        $selected[$rec->field] = $rec->value;
    }
} else {
    $selected = array();
}
// Locked artefacts (site locked and institution locked)
$sitelocked = (array) get_column('institution_locked_profile_field', 'profilefield', 'name', 'mahara');
$instlocked = (array) get_column('institution_locked_profile_field', 'profilefield', 'name', $institution);
$locked = array_merge($sitelocked, $instlocked);
// Figure out the form elements in the configuration form
$elements = array();
$possibleitems = artefact_get_progressbar_items();
$possibleitemscount = count($possibleitems);
$i = 0;
foreach ($possibleitems as $plugin => $itemlist) {
    $subelements = array();
    $fscollapsed = true;
    $class = $i === $possibleitemscount - 1 ? 'last' : '';
    $i++;
    foreach ($itemlist as $artefact) {
        $pbname = "progressbaritem_{$artefact->plugin}_{$artefact->name}";
        // Check if this one is a locked profile field.
        if ($plugin == 'internal' && in_array($artefact->name, $locked)) {
            $islocked = true;
        } else {
            $islocked = false;
        }