progressbar.php 7.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
<?php
/**
 *
 * @package    mahara
 * @subpackge  admin
 * @author     Gregor Anzelj
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 * @copyright  (C) 2013 Gregor Anzelj <gregor.anzelj@gmail.com>
 *
 */

define('INTERNAL', 1);
define('INSTITUTIONALADMIN', 1);
define('MENUITEM', 'manageinstitutions/progressbar');
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'progressbar');

require(dirname(dirname(dirname(__FILE__))).'/init.php');
require_once('pieforms/pieform.php');
require_once('institution.php');
define('TITLE', get_string('progressbar', 'admin'));
define('DEFAULTPAGE', 'home');

if (!is_logged_in()) {
    throw new AccessDeniedException();
}

$institutionelement = get_institution_selector(true, false, true, false);

if (empty($institutionelement)) {
    $smarty = smarty();
    $smarty->display('admin/users/noinstitutions.tpl');
    exit;
}

$institution = param_alphanum('institution', false);
if (!$institution || !$USER->can_edit_institution($institution, true)) {
    $institution = empty($institutionelement['value']) ? $institutionelement['defaultvalue'] : $institutionelement['value'];
}
else if (!empty($institution)) {
    $institutionelement['defaultvalue'] = $institution;
}
$institutionselector = pieform(array(
    'name' => 'progressbarselect',
    'elements' => array(
        'institution' => $institutionelement,
    )
));


// Selected artefacts that count towards completing progress bar
54 55 56 57 58 59 60 61 62 63 64 65 66
$recs = get_records_select_array('institution_config', 'institution=? and field like \'progressbaritem_%\'', array($institution), 'field', 'field, value');
if ($recs) {
    $selected = array();
    foreach($recs as $rec) {
        $obj = new stdClass();
        $obj->raw = $rec->field;
        $parts = explode('_', $rec->field);
        // Check format
        if (count($parts) < 3) {
            continue;
        }
        $selected[$rec->field] = $rec->value;
    }
67 68 69 70 71 72 73 74 75 76
}
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);

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
// Figure out the form elements in the configuration form
$elements = array();
$possibleitems = artefact_get_progressbar_items();
foreach($possibleitems as $plugin => $itemlist) {
    $subelements = array();
    $fscollapsed = true;
    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;
        }
93

94 95 96 97 98 99
        // Check if this one has a default value (i.e. a value stored in the DB)
        if (!$islocked && array_key_exists($pbname, $selected)) {
            $defaultvalue = $selected[$pbname];
        }
        else {
            $defaultvalue = null;
100 101
        }

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        // If there are any selected elements in this fieldset, don't pre-collapse it.
        $fscollapsed = $fscollapsed && !$defaultvalue;

        if ($artefact->iscountable) {
            $options = array(
                0 => '0',
                1 => '1',
                2 => '2',
                3 => '3',
                4 => '4',
                5 => '5',
               10 => '10',
               15 => '15',
               20 => '20',
               25 => '25',
               50 => '50',
              100 => '100',
            );
            $subelements[$pbname] = array(
                'type' => 'select',
                'title' => $artefact->title,
                'disabled' => ($artefact->active && !$islocked ? false : true),
                'defaultvalue' => $defaultvalue,
                'options' => $options
            );
        }
        else {
            $subelements[$pbname] = array(
                'type' => 'checkbox',
                'title' => $artefact->title,
                'disabled' => ($artefact->active && !$islocked ? false : true),
                'defaultvalue' => $defaultvalue,
            );
        }
136
    }
137 138 139 140 141 142 143
    $elements["fs{$plugin}"] = array(
            'type' => 'fieldset',
            'collapsible' => true,
            'collapsed' => $fscollapsed,
            'legend' => get_string('pluginname', "artefact.{$plugin}"),
            'elements' => $subelements,
    );
144 145
}

146 147 148 149 150 151 152 153
$elements['institution'] = array(
    'type' => 'hidden',
    'value' => $institution,
);
$elements['submit'] = array(
    'type' => 'submit',
    'value' => get_string('submit')
);
154 155 156 157 158 159

$form = pieform(array(
    'name'        => 'progressbarform',
    'renderer'    => 'table',
    'plugintype'  => 'core',
    'pluginname'  => 'admin',
160
    'elements'    => $elements,
161 162
));

163
function progressbarform_validate(Pieform $form, $values) {
164
    global $SESSION, $USER;
165 166 167 168 169
    $inst = $values['institution'];
    if (empty($inst) || !$USER->can_edit_institution($inst)) {
        $SESSION->add_error_msg(get_string('notadminforinstitution', 'admin'));
        redirect('/admin/users/progressbar.php');
    }
170
}
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
function progressbarform_submit(Pieform $form, $values) {
    global $SESSION, $USER, $possibleitems;

    $institution = $values['institution'];

    // Pre-fetching the current settings to reduce SELECT queries
    $currentsettings = get_records_select_assoc('institution_config', 'institution=? and field like \'progressbaritem_%\'', array($institution), 'field', 'field, value');
    if (!$currentsettings) {
        $currentsettings = array();
    }

    foreach ($possibleitems as $plugin => $pluginitems) {
        foreach ($pluginitems as $artefact) {
            $itemname = "progressbaritem_{$plugin}_{$artefact->name}";

            // Format the value into an integer or 0/1
            $val = $values[$itemname];
            if ($artefact->iscountable) {
                $val = (int) $val;
            }
            else {
                $val = (int)((bool) $val);
            }

            // Update the record if it already exists, or create the record if it doesn't
            if (array_key_exists($itemname, $currentsettings)) {
                if ($val) {
                    set_field('institution_config', 'value', $val, 'institution', $institution, 'field', $itemname);
                }
                else {
                    delete_records('institution_config', 'institution', $institution, 'field', $itemname);
                }
            }
            else {
                if ($val) {
                    insert_record('institution_config', (object) array('institution'=>$institution, 'field'=>$itemname, 'value'=>$val));
                }
            }
        }
    }
212 213

    $SESSION->add_ok_msg(get_string('progressbarsaved', 'admin'));
214
    redirect('/admin/users/progressbar.php?institution=' . $institution);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
}


$wwwroot = get_config('wwwroot');
$js = <<< EOF
function reloadBar() {
    window.location.href = '{$wwwroot}admin/users/progressbar.php?institution='+$('progressbarselect_institution').value;
}
addLoadEvent(function() {
    connect($('progressbarselect_institution'), 'onchange', reloadBar);
});
EOF;

$smarty = smarty();
$smarty->assign('progressbarform', $form);
$smarty->assign('institution', $institution);
$smarty->assign('institutionselector', $institutionselector);
$smarty->assign('enabled', get_config('showprogressbar'));
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->assign('PAGEHEADING', TITLE);
$smarty->display('admin/users/progressbar.tpl');