/**
 * Provides a duration chooser, with a text box for a number and a
 * select box to choose the units, in days, weeks, months, years, or 'no end date'.
 *
 * @param Pieform $form    The form to render the element for
 * @param array   $element The element to render
 * @return string          The HTML for the element
 */
function pieform_element_expiry(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    $result = '';
    $name = Pieform::hsc($element['name']);
    if (!isset($element['defaultvalue'])) {
        $element['defaultvalue'] = null;
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    // Get the value of the element for rendering.
    if (isset($element['value'])) {
        $seconds = $element['value'];
        $values = pieform_element_expiry_get_expiry_from_seconds($element['value']);
    } else {
        if ($form->is_submitted() && isset($global[$element['name']]) && isset($global[$element['name'] . '_units'])) {
            $values = array('number' => $global[$element['name']], 'units' => $global[$element['name'] . '_units']);
            $seconds = $values['number'] * pieform_element_expiry_seconds_in($values['units']);
        } else {
            if (isset($element['defaultvalue'])) {
                $seconds = $element['defaultvalue'];
                $values = pieform_element_expiry_get_expiry_from_seconds($seconds);
            } else {
                $values = array('number' => '', 'units' => 'noenddate');
                $seconds = null;
            }
        }
    }
    // @todo probably create with an actual input element, as tabindex doesn't work here for one thing
    // Same with the select. And do the events using mochikit signal instead of dom events
    $numberinput = '<input';
    $numberinput .= $values['units'] == 'noenddate' && empty($element['rules']['required']) ? ' disabled="disabled"' : '';
    $numberinput .= ' type="text" size="4" name="' . $name . '"';
    $numberinput .= ' id="' . $formname . '_' . $name . '" value="' . Pieform::hsc($values['number']) . '" tabindex="' . Pieform::hsc($element['tabindex']) . '"';
    $numberinput .= (isset($element['error']) ? ' class="error"' : '') . ">\n";
    $uselect = '<select onchange="' . $name . '_change()" ';
    $uselect .= 'name="' . $name . '_units" id="' . $formname . '_' . $name . '_units"' . ' tabindex="' . Pieform::hsc($element['tabindex']) . "\">\n";
    foreach (pieform_element_expire_get_expiry_units() as $u) {
        // Don't allow 'no end date' if the element is required
        if ($u == 'noenddate' && !empty($element['rules']['required'])) {
            continue;
        }
        $uselect .= "\t<option value=\"{$u}\"" . ($values['units'] == $u ? ' selected="selected"' : '') . '>' . $form->i18n('element', 'expiry', $u, $element) . "</option>\n";
    }
    $uselect .= "</select>\n";
    // Make sure the input is disabled if "no end date" is selected
    $script = <<<EOJS
<script type="text/javascript" language="javascript">
function {$name}_change() {
    if (\$('{$formname}_{$name}_units').value == 'noenddate') {
        \$('{$formname}_{$name}').disabled = true;
    }
    else {
        \$('{$formname}_{$name}').disabled = false;
    }
}
</script>
EOJS;
    return $numberinput . $uselect . $script;
}
Exemple #2
0
/**
 * Returns the color value of the color selector element from the request or transparent
 *
 * @param Pieform $form    The form the element is attached to
 * @param array   $element The element to get the value for
 * @return string A 6-digit hex color value, or the string "transparent"
 */
function pieform_element_color_get_value(Pieform $form, $element)
{
    $name = $element['name'];
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if ($form->is_submitted() && isset($global[$name . '_color']) && !isset($global[$name . '_optional'])) {
        $color = $global[$name . '_color'];
        // Whitelist for a 6-digit hex color
        $color = preg_replace('/[^a-f0-9]/i', '', $color);
        if (strlen($color) >= 6) {
            $color = substr($color, 0, 6);
        } else {
            if (strlen($color) >= 3) {
                // If they provided a 3-digit color string, convert it into a 6-digit one by doubling each digit
                $color = substr($color, 0, 3);
                $color = $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
            } else {
                $color = '';
            }
        }
        if ($color === '') {
            return 'transparent';
        }
        $color = "#{$color}";
        return $color;
    }
    return 'transparent';
}
Exemple #3
0
/**
 * Returns the value for a hidden element. Hidden elements only listen to the
 * 'value' index, and not to GET/POST, unless the 'sesskey' property is set
 * on the element. Or, if the element has the "dynamic" tag set, which indicates
 * that it's a hidden field that is meant to be updated by Javascript.
 */
function pieform_element_hidden_get_value(Pieform $form, $element)
{
    /*{{{*/
    if (!empty($element['dynamic']) || !empty($element['sesskey']) && $form->is_submitted()) {
        return isset($_POST[$element['name']]) ? $_POST[$element['name']] : null;
    }
    return $element['value'];
}
function pieform_element_checkboxes_get_value(Pieform $form, $element)
{
    /*{{{*/
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if (isset($element['value'])) {
        $values = (array) $element['value'];
    } else {
        if ($form->is_submitted() && isset($global[$element['name']])) {
            $values = (array) $global[$element['name']];
        } else {
            if (!$form->is_submitted() && isset($element['defaultvalue'])) {
                $values = (array) $element['defaultvalue'];
            } else {
                $values = array();
            }
        }
    }
    return $values;
}
function pieform_element_trafficlights_get_value(Pieform $form, $element)
{
    /*{{{*/
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if (isset($element['value'])) {
        $value = $element['value'];
    } else {
        if ($form->is_submitted() && isset($global[$element['name']])) {
            $value = $global[$element['name']];
        } else {
            if (!$form->is_submitted() && isset($element['defaultvalue'])) {
                $value = $element['defaultvalue'];
            } else {
                $value = null;
            }
        }
    }
    return $value;
}
Exemple #6
0
/**
 * Provides a size chooser, with a text box for a number and a
 * select box to choose the units, in bytes, kilobytes, megabytes or gigabytes
 *
 * @param Pieform $form    The form to render the element for
 * @param array   $element The element to render
 * @return string          The HTML for the element
 */
function pieform_element_bytes(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    $result = '';
    $name = Pieform::hsc($element['name']);
    if (!isset($element['defaultvalue'])) {
        $element['defaultvalue'] = null;
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    // Get the value of the element for rendering.
    if (isset($element['value'])) {
        $bytes = $element['value'];
        $values = pieform_element_bytes_get_bytes_from_bytes($element['value']);
    } else {
        if ($form->is_submitted() && isset($global[$element['name']]) && isset($global[$element['name'] . '_units'])) {
            $values = array('number' => $global[$element['name']], 'units' => $global[$element['name'] . '_units']);
            $bytes = $values['number'] * pieform_element_bytes_in($values['units']);
        } else {
            if (isset($element['defaultvalue'])) {
                $bytes = $element['defaultvalue'];
                $values = pieform_element_bytes_get_bytes_from_bytes($bytes);
            } else {
                $values = array('number' => '0', 'units' => 'bytes');
                $bytes = 0;
            }
        }
    }
    // @todo probably create with an actual input element, as tabindex doesn't work here for one thing
    // Same with the select. And do the events using mochikit signal instead of dom events
    $numberinput = '<div class="with-dropdown js-with-dropdown text">';
    $numberinput .= '<label for="' . $formname . '_' . $name . '">' . Pieform::hsc($element['title']) . ': </label><input';
    $numberinput .= ' type="text" size="6" name="' . $name . '"';
    $numberinput .= ' id="' . $formname . '_' . $name . '" value="' . Pieform::hsc($values['number']) . '" tabindex="' . Pieform::hsc($element['tabindex']) . '"';
    $numberinput .= 'class="with-dropdown js-with-dropdown form-control text' . (isset($element['error']) ? ' error"' : '') . '"';
    if (isset($element['description'])) {
        $numberinput .= ' aria-describedby="' . $form->element_descriptors($element) . '"';
    }
    $numberinput .= "></div>\n";
    $uselect = '<div class="dropdown-connect js-dropdown-connect select">';
    $uselect .= '<label for="' . $formname . '_' . $name . '_units" class="accessible-hidden sr-only">' . get_string('units') . '</label>';
    $uselect .= '<span class="picker"><select class="form-control dropdown-connect js-dropdown-connect select" name="' . $name . '_units" id="' . $formname . '_' . $name . '_units"' . ' tabindex="' . Pieform::hsc($element['tabindex']) . '"';
    if (isset($element['description'])) {
        $uselect .= ' aria-describedby="' . $form->element_descriptors($element) . '"';
    }
    $uselect .= ">\n";
    foreach (pieform_element_bytes_get_bytes_units() as $u) {
        $uselect .= "\t<option value=\"{$u}\"" . ($values['units'] == $u ? ' selected="selected"' : '') . '>' . $form->i18n('element', 'bytes', $u, $element) . "</option>\n";
    }
    $uselect .= "</select></span></div>\n";
    $fieldset = '<div id="' . $formname . '_' . $name . '_fieldset" class="dropdown-group js-dropdown-group form-group">' . '<fieldset class="pieform-fieldset dropdown-group js-dropdown-group">' . $numberinput . $uselect . '</fieldset></div>';
    return $fieldset;
}
Exemple #7
0
function pieform_element_image_get_value(Pieform $form, $element)
{
    /*{{{*/
    if (isset($element['value'])) {
        return $element['value'];
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if ($form->is_submitted() && isset($global[$element['name'] . '_x'])) {
        return true;
    }
    return null;
}
function pieform_element_password_get_value(Pieform $form, $element)
{
    /*{{{*/
    if (isset($element['value'])) {
        return $element['value'];
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if ($form->is_submitted() && isset($global[$element['name']])) {
        return $global[$element['name']];
    }
    if (isset($element['defaultvalue'])) {
        return $element['defaultvalue'];
    }
    return null;
}
function pieform_element_multitext_get_value(Pieform $form, $element)
{
    if (isset($element['value'])) {
        return $element['value'];
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    $name = $form->get_name() . '_' . $element['name'];
    if ($form->is_submitted() && isset($global[$name]) && is_array($global[$name])) {
        return array_values($global[$name]);
    }
    if (isset($element['defaultvalue'])) {
        return $element['defaultvalue'];
    }
    return null;
}
function pieform_element_textarea_get_value(Pieform $form, $element)
{
    /*{{{*/
    if (isset($element['value'])) {
        return $element['value'];
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if ($form->is_submitted() && isset($global[$element['name']])) {
        return str_replace("\r\n", "\n", $global[$element['name']]);
    }
    if (isset($element['defaultvalue'])) {
        return $element['defaultvalue'];
    }
    return '';
}
/**
 * Return the value of the element. This returns an array with the defaultvalue (if supplied),
 * and the value newly submitted in this form (if supplied)
 *
 * @param Pieform $form
 * @param array $element
 * @return array with two keys, 'defaultvalue' and 'submittedvalue'
 */
function pieform_element_passwordnoread_get_value(Pieform $form, $element)
{
    $ret = array();
    if (isset($element['defaultvalue'])) {
        $ret['defaultvalue'] = $element['defaultvalue'];
    } else {
        $ret['defaultvalue'] = null;
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if ($form->is_submitted() && isset($global[$element['name']])) {
        $ret['submittedvalue'] = $global[$element['name']];
    } else {
        $ret['submittedvalue'] = null;
    }
    return $ret;
}
Exemple #12
0
function pieform_element_checkbox_get_value(Pieform $form, $element)
{
    /*{{{*/
    $name = $element['name'];
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if (isset($element['value'])) {
        return $element['value'];
    }
    if ($form->is_submitted()) {
        if (isset($global[$name])) {
            return true;
        }
        return false;
    }
    if (isset($element['defaultvalue'])) {
        return $element['defaultvalue'];
    }
    return false;
}
/**
 * Provides a size chooser, with a text box for a number and a
 * select box to choose the units, in bytes, kilobytes, megabytes or gigabytes
 *
 * @param Pieform $form    The form to render the element for
 * @param array   $element The element to render
 * @return string          The HTML for the element
 */
function pieform_element_bytes(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    $result = '';
    $name = Pieform::hsc($element['name']);
    if (!isset($element['defaultvalue'])) {
        $element['defaultvalue'] = null;
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    // Get the value of the element for rendering.
    if (isset($element['value'])) {
        $bytes = $element['value'];
        $values = pieform_element_bytes_get_bytes_from_bytes($element['value']);
    } else {
        if ($form->is_submitted() && isset($global[$element['name']]) && isset($global[$element['name'] . '_units'])) {
            $values = array('number' => $global[$element['name']], 'units' => $global[$element['name'] . '_units']);
            $bytes = $values['number'] * pieform_element_bytes_in($values['units']);
        } else {
            if (isset($element['defaultvalue'])) {
                $bytes = $element['defaultvalue'];
                $values = pieform_element_bytes_get_bytes_from_bytes($bytes);
            } else {
                $values = array('number' => '0', 'units' => 'bytes');
                $bytes = 0;
            }
        }
    }
    // @todo probably create with an actual input element, as tabindex doesn't work here for one thing
    // Same with the select. And do the events using mochikit signal instead of dom events
    $numberinput = '<input';
    $numberinput .= ' type="text" size="6" name="' . $name . '"';
    $numberinput .= ' id="' . $formname . '_' . $name . '" value="' . Pieform::hsc($values['number']) . '" tabindex="' . Pieform::hsc($element['tabindex']) . '"';
    $numberinput .= (isset($element['error']) ? ' class="error"' : '') . ">\n";
    $uselect = '<select name="' . $name . '_units" id="' . $formname . '_' . $name . '_units"' . ' tabindex="' . Pieform::hsc($element['tabindex']) . "\">\n";
    foreach (pieform_element_bytes_get_bytes_units() as $u) {
        $uselect .= "\t<option value=\"{$u}\"" . ($values['units'] == $u ? ' selected="selected"' : '') . '>' . $form->i18n('element', 'bytes', $u, $element) . "</option>\n";
    }
    $uselect .= "</select>\n";
    return $numberinput . $uselect;
}
function pieform_element_rolepermissions_get_value(Pieform $form, $element)
{
    /*{{{*/
    if (isset($element['value'])) {
        return $element['value'];
    }
    if (isset($element['defaultvalue'])) {
        $value = $element['defaultvalue'];
    } else {
        $value = group_get_default_artefact_permissions($element['group']);
    }
    if ($form->is_submitted()) {
        $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
        $prefix = $form->get_name() . '_' . $element['name'] . '_p';
        foreach ($value as $r => $perms) {
            foreach (array_keys(get_object_vars($perms)) as $p) {
                if ($r != 'admin') {
                    $value[$r]->{$p} = param_boolean($prefix . '_' . $r . '_' . $p);
                }
            }
        }
    }
    return $value;
}
/**
 * Translates the raw form data into PHP variables. Basically it just needs to
 * decide whether we should return an array (if this is a multi-select) or a
 * scalar (if this is not a multi-select)
 *
 * @param Pieform $form
 * @param array $element
 * @return mixed
 */
function pieform_element_autocomplete_get_value(Pieform $form, $element)
{
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if (isset($element['value'])) {
        $values = $element['value'];
    } else {
        if ($form->is_submitted() && isset($global[$element['name']])) {
            $values = $global[$element['name']];
        } else {
            if (!$form->is_submitted() && isset($element['defaultvalue'])) {
                $values = $element['defaultvalue'];
            } else {
                if (!empty($element['disabled']) && isset($element['defaultvalue'])) {
                    $values = $element['defaultvalue'];
                } else {
                    $values = null;
                }
            }
        }
    }
    if (empty($element['multiple'])) {
        return $values;
    } else {
        // Defaultvalue will already be an array
        if (is_array($values)) {
            return $values;
        }
        // Values returned form the form will be a comma-separated list
        $r = explode(',', $values);
        if ($r === false) {
            return array();
        } else {
            return $r;
        }
    }
}
Exemple #16
0
 /**
  * Builds the configuration pieform for this blockinstance
  *
  * @return array Array with two keys: 'html' for raw html, 'javascript' for
  *               javascript to run, 'css' for dynamic css to add to header
  */
 public function build_configure_form($new = false)
 {
     static $renderedform;
     if (!empty($renderedform)) {
         return $renderedform;
     }
     safe_require('blocktype', $this->get('blocktype'));
     $blocktypeclass = generate_class_name('blocktype', $this->get('blocktype'));
     $elements = call_static_method($blocktypeclass, 'instance_config_form', $this, $this->get_view()->get('template'));
     // Block types may specify a method to generate a default title for a block
     $hasdefault = method_exists($blocktypeclass, 'get_instance_title');
     $title = $this->get('title');
     $configdata = $this->get('configdata');
     $retractable = isset($configdata['retractable']) ? $configdata['retractable'] : false;
     $retractedonload = isset($configdata['retractedonload']) ? $configdata['retractedonload'] : $retractable;
     if (call_static_method($blocktypeclass, 'override_instance_title', $this)) {
         $titleelement = array('type' => 'hidden', 'value' => $title);
     } else {
         $titleelement = array('type' => 'text', 'title' => get_string('blocktitle', 'view'), 'description' => $hasdefault ? get_string('defaulttitledescription', 'blocktype.' . blocktype_name_to_namespaced($this->get('blocktype'))) : null, 'defaultvalue' => $title, 'rules' => array('maxlength' => 255), 'hidewhenempty' => $hasdefault, 'expandtext' => get_string('setblocktitle'));
     }
     $elements = array_merge(array('title' => $titleelement, 'blockconfig' => array('type' => 'hidden', 'value' => $this->get('id')), 'id' => array('type' => 'hidden', 'value' => $this->get('view')), 'change' => array('type' => 'hidden', 'value' => 1), 'new' => array('type' => 'hidden', 'value' => $new)), $elements, array('retractable' => array('type' => 'select', 'title' => get_string('retractable', 'view'), 'description' => get_string('retractabledescription', 'view'), 'options' => array(BlockInstance::RETRACTABLE_NO => get_string('no'), BlockInstance::RETRACTABLE_YES => get_string('yes'), BlockInstance::RETRACTABLE_RETRACTED => get_string('retractedonload', 'view')), 'defaultvalue' => $retractable + $retractedonload)));
     if ($new) {
         $cancel = get_string('remove');
         $elements['removeoncancel'] = array('type' => 'hidden', 'value' => 1);
         $elements['sure'] = array('type' => 'hidden', 'value' => 1);
     } else {
         $cancel = get_string('cancel');
     }
     // Add submit/cancel buttons
     $elements['action_configureblockinstance_id_' . $this->get('id')] = array('type' => 'submitcancel', 'class' => 'btn-default', 'value' => array(get_string('save'), $cancel), 'goto' => View::make_base_url());
     $configdirs = array(get_config('libroot') . 'form/');
     if ($this->get('artefactplugin')) {
         $configdirs[] = get_config('docroot') . 'artefact/' . $this->get('artefactplugin') . '/form/';
     }
     $form = array('name' => 'instconf', 'renderer' => 'div', 'validatecallback' => array(generate_class_name('blocktype', $this->get('blocktype')), 'instance_config_validate'), 'successcallback' => array($this, 'instance_config_store'), 'jsform' => true, 'jssuccesscallback' => 'blockConfigSuccess', 'jserrorcallback' => 'blockConfigError', 'elements' => $elements, 'viewgroup' => $this->get_view()->get('group'), 'group' => $this->get_view()->get('group'), 'viewinstitution' => $this->get_view()->get('institution'), 'institution' => $this->get_view()->get('institution'), 'configdirs' => $configdirs, 'plugintype' => 'blocktype', 'pluginname' => $this->get('blocktype'));
     if (param_variable('action_acsearch_id_' . $this->get('id'), false)) {
         $form['validate'] = false;
     }
     require_once 'pieforms/pieform.php';
     $pieform = new Pieform($form);
     if ($pieform->is_submitted()) {
         global $SESSION;
         $SESSION->add_error_msg(get_string('errorprocessingform'));
     }
     $html = $pieform->build();
     // We probably need a new version of $pieform->build() that separates out the js
     // Temporary evil hack:
     if (preg_match('/<script type="(text|application)\\/javascript">(new Pieform\\(.*\\);)<\\/script>/', $html, $matches)) {
         $js = "var pf_{$form['name']} = " . $matches[2] . "pf_{$form['name']}.init();";
     } else {
         $js = '';
     }
     // We need to load any javascript required for the pieform. We do this
     // by checking for an api function that has been added especially for
     // the purpose, but that is not part of Pieforms. Maybe one day later
     // it will be though
     foreach ($elements as $key => $element) {
         $element['name'] = $key;
         $function = 'pieform_element_' . $element['type'] . '_views_js';
         if (is_callable($function)) {
             $js .= call_user_func_array($function, array($pieform, $element));
         }
     }
     $configjs = call_static_method($blocktypeclass, 'get_instance_config_javascript', $this);
     if (is_array($configjs)) {
         $js .= $this->get_get_javascript_javascript($configjs);
     } else {
         if (is_string($configjs)) {
             $js .= $configjs;
         }
     }
     // We need to load any dynamic css required for the pieform. We do this
     // by checking for an api function that has been added especially for
     // the purpose, but that is not part of Pieforms. Maybe one day later
     // it will be though
     $css = array();
     foreach ($elements as $key => $element) {
         $element['name'] = $key;
         $function = 'pieform_element_' . $element['type'] . '_views_css';
         if (is_callable($function)) {
             $css[] = call_user_func_array($function, array($pieform, $element));
         }
     }
     $renderedform = array('html' => $html, 'javascript' => $js, 'css' => $css);
     return $renderedform;
 }
Exemple #17
0
/**
 * Gets the value of the date element from the request and converts it into a
 * unix timestamp.
 *
 * @param Pieform $form    The form the element is attached to
 * @param array   $element The element to get the value for
 */
function pieform_element_date_get_value(Pieform $form, $element)
{
    /*{{{*/
    $name = $element['name'];
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if ($form->is_submitted() && isset($global[$name . '_day']) && isset($global[$name . '_month']) && isset($global[$name . '_year'])) {
        if (isset($global[$name . '_minute']) && isset($global[$name . '_hour'])) {
            $time = mktime($global[$name . '_hour'], $global[$name . '_minute'], 0, $global[$name . '_month'], $global[$name . '_day'], $global[$name . '_year']);
        } else {
            $time = mktime(0, 0, 0, $global[$name . '_month'], $global[$name . '_day'], $global[$name . '_year']);
        }
        if (false === $time) {
            return null;
        }
        return $time;
    }
    return null;
}
function pieform_element_wysiwyg_get_value(Pieform $form, $element)
{
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if (isset($element['value'])) {
        return clean_html($element['value']);
    } else {
        if ($form->is_submitted() && isset($global[$element['name']])) {
            $value = $global[$element['name']];
            if (!is_html_editor_enabled()) {
                $value = format_whitespace($value);
            }
            return $value;
        } else {
            if (isset($element['defaultvalue'])) {
                return clean_html($element['defaultvalue']);
            }
        }
    }
    return null;
}
/**
 * Retrieves the value of the calendar as a unix timestamp
 *
 * @param Pieform $form    The form the element is attached to
 * @param array   $element The element to get the value for
 * @return int             The unix timestamp represented by the calendar
 */
function pieform_element_calendar_get_value(Pieform $form, $element)
{
    /*{{{*/
    $name = $element['name'];
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if (isset($element['value'])) {
        return $element['value'];
    }
    if ($form->is_submitted() && isset($global[$name])) {
        if (trim($global[$name]) == '') {
            return null;
        }
        $value = strtotime($global[$name]);
        if ($value === false) {
            $form->set_error($name, $form->i18n('element', 'calendar', 'invalidvalue', $element));
            return null;
        }
        return $value;
    }
    if (isset($element['defaultvalue'])) {
        return $element['defaultvalue'];
    }
    return null;
}
Exemple #20
0
function pieform_element_select_get_value(Pieform $form, $element)
{
    if (empty($element['multiple'])) {
        $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
        if (isset($element['value'])) {
            $values = (array) $element['value'];
        } else {
            if ($form->is_submitted() && isset($global[$element['name']])) {
                $values = (array) $global[$element['name']];
                if (!empty($element['allowother']) and $values[0] === 'other') {
                    if (isset($global[$element['name'] . '_other'])) {
                        $values = (array) $global[$element['name'] . '_other'];
                    } else {
                        $values = (array) $element['defaultvalue'];
                    }
                }
            } else {
                if (isset($element['defaultvalue'])) {
                    $values = (array) $element['defaultvalue'];
                } else {
                    $values = array(null);
                }
            }
        }
        if (count($values) != 1) {
            Pieform::info('The select element "' . $element['name'] . '" has ' . 'more than one value, but has not been declared multiple');
        }
        return $values[0];
    } else {
        $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
        if (isset($element['value'])) {
            $values = (array) $element['value'];
        } else {
            if ($form->is_submitted() && isset($global[$element['name']])) {
                $values = (array) $global[$element['name']];
            } else {
                if (!$form->is_submitted() && isset($element['defaultvalue'])) {
                    $values = (array) $element['defaultvalue'];
                } else {
                    $values = array();
                }
            }
        }
    }
    return $values;
}
Exemple #21
0
function pieform_element_viewacl_get_value(Pieform $form, $element)
{
    $values = null;
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    if (isset($element['value'])) {
        $values = $element['value'];
    } else {
        if (isset($global[$element['name']])) {
            $value = $global[$element['name']];
            $values = $value;
        } else {
            if (isset($element['defaultvalue'])) {
                $values = $element['defaultvalue'];
            }
        }
    }
    // Convert dates to epochs
    if ($form->is_submitted() && $values) {
        foreach ($values as &$value) {
            if (!empty($value['startdate'])) {
                $value['startdate'] = pieform_element_calendar_convert_to_epoch($value['startdate']);
            }
            if (!empty($value['stopdate'])) {
                $value['stopdate'] = pieform_element_calendar_convert_to_epoch($value['stopdate']);
            }
        }
    }
    return $values;
}
 /**
  * Builds the configuration pieform for this blockinstance
  *
  * @return array Array with two keys: 'html' for raw html, 'javascript' for
  *               javascript to run
  */
 public function build_configure_form($new = false)
 {
     static $renderedform;
     if (!empty($renderedform)) {
         return $renderedform;
     }
     safe_require('blocktype', $this->get('blocktype'));
     $elements = call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'instance_config_form', $this, $this->get_view()->get('template'));
     $blocktypeclass = generate_class_name('blocktype', $this->get('blocktype'));
     if ($this->get('title') != call_static_method($blocktypeclass, 'get_title')) {
         // If the title for this block has been set to something other than
         // the block title, use it unconditionally
         $title = $this->get('title');
     } else {
         if (method_exists($blocktypeclass, 'get_instance_title')) {
             // Block types can specify a default title for a block
             $title = call_static_method($blocktypeclass, 'get_instance_title', $this);
         } else {
             // A block that doesn't have a method for setting an instance
             // title, and hasn't had its title changed (e.g. a new textbox)
             $title = $this->get('title');
         }
     }
     $elements = array_merge(array('title' => array('type' => 'text', 'title' => get_string('blocktitle', 'view'), 'description' => method_exists($blocktypeclass, 'get_instance_title') ? get_string('defaulttitledescription', 'blocktype.' . blocktype_name_to_namespaced($this->get('blocktype'))) : null, 'defaultvalue' => $title), 'blockconfig' => array('type' => 'hidden', 'value' => $this->get('id')), 'id' => array('type' => 'hidden', 'value' => $this->get('view')), 'change' => array('type' => 'hidden', 'value' => 1), 'new' => array('type' => 'hidden', 'value' => $new)), $elements);
     if ($new) {
         $cancel = get_string('remove');
         $elements['removeoncancel'] = array('type' => 'hidden', 'value' => 1);
         $elements['sure'] = array('type' => 'hidden', 'value' => 1);
     } else {
         $cancel = get_string('cancel');
     }
     // Add submit/cancel buttons
     $elements['action_configureblockinstance_id_' . $this->get('id')] = array('type' => 'submitcancel', 'value' => array(get_string('save'), $cancel), 'goto' => View::make_base_url());
     $configdirs = array(get_config('libroot') . 'form/');
     if ($this->get('artefactplugin')) {
         $configdirs[] = get_config('docroot') . 'artefact/' . $this->get('artefactplugin') . '/form/';
     }
     $form = array('name' => 'instconf', 'renderer' => 'maharatable', 'validatecallback' => array(generate_class_name('blocktype', $this->get('blocktype')), 'instance_config_validate'), 'successcallback' => array($this, 'instance_config_store'), 'jsform' => true, 'jssuccesscallback' => 'blockConfigSuccess', 'jserrorcallback' => 'blockConfigError', 'elements' => $elements, 'viewgroup' => $this->get_view()->get('group'), 'group' => $this->get_view()->get('group'), 'viewinstitution' => $this->get_view()->get('institution'), 'institution' => $this->get_view()->get('institution'), 'configdirs' => $configdirs, 'plugintype' => 'blocktype', 'pluginname' => $this->get('blocktype'));
     if (param_variable('action_acsearch_id_' . $this->get('id'), false)) {
         $form['validate'] = false;
     }
     require_once 'pieforms/pieform.php';
     $pieform = new Pieform($form);
     if ($pieform->is_submitted()) {
         global $SESSION;
         $SESSION->add_error_msg(get_string('errorprocessingform'));
     }
     $html = $pieform->build();
     // We probably need a new version of $pieform->build() that separates out the js
     // Temporary evil hack:
     if (preg_match('/<script type="text\\/javascript">(new Pieform\\(.*\\);)<\\/script>/', $html, $matches)) {
         $js = "var pf_{$form['name']} = " . $matches[1] . "pf_{$form['name']}.init();";
     } else {
         $js = '';
     }
     // We need to load any javascript required for the pieform. We do this
     // by checking for an api function that has been added especially for
     // the purpose, but that is not part of Pieforms. Maybe one day later
     // it will be though
     // $js = '';
     foreach ($elements as $key => $element) {
         $element['name'] = $key;
         $function = 'pieform_element_' . $element['type'] . '_views_js';
         if (is_callable($function)) {
             $js .= call_user_func_array($function, array($pieform, $element));
         }
     }
     $renderedform = array('html' => $html, 'javascript' => $js);
     return $renderedform;
 }
Exemple #23
0
/**
 * Provides an element to manage a view ACL
 *
 * @param array    $element The element to render
 * @param Pieform  $form    The form to render the element for
 * @return string           The HTML for the element
 */
function pieform_element_viewacl(Pieform $form, $element)
{
    global $USER, $SESSION, $LANGDIRECTION;
    $strlen = function_exists('mb_strlen') ? 'mb_strlen' : 'strlen';
    $smarty = smarty_core();
    $smarty->left_delimiter = '{{';
    $smarty->right_delimiter = '}}';
    $value = $form->get_value($element);
    // Look for the presets and split them into two groups
    require_once get_config('libroot') . 'antispam.php';
    $public = false;
    if (is_probationary_user()) {
        $public = false;
    } else {
        if (get_config('allowpublicviews') && $USER->institution_allows_public_views()) {
            $public = true;
        } else {
            if (get_config('allowpublicprofiles') && $element['viewtype'] == 'profile') {
                $public = true;
            }
        }
    }
    $allpresets = array('public', 'loggedin', 'friends');
    $allowedpresets = array();
    $loggedinindex = 0;
    if ($public) {
        $allowedpresets[] = 'public';
        $loggedinindex = 1;
    }
    $allowedpresets[] = 'loggedin';
    if ($form->get_property('userview')) {
        $allowedpresets[] = 'friends';
    }
    $accesslist = array();
    if ($value) {
        foreach ($value as $item) {
            if (is_array($item)) {
                if ($item['type'] == 'public') {
                    $item['publicallowed'] = (int) $public;
                }
                if (in_array($item['type'], $allpresets)) {
                    $item['name'] = get_string($item['type'] == 'loggedin' ? 'registeredusers' : $item['type'], 'view');
                    $item['preset'] = true;
                } else {
                    $item['name'] = pieform_render_viewacl_getvaluebytype($item['type'], $item['id']);
                }
                if ($strlen($item['name']) > 30) {
                    $item['shortname'] = str_shorten_text($item['name'], 30, true);
                }
                // only show access that is still current. Expired access will be deleted if the form is saved
                if ($form->is_submitted() || empty($item['stopdate']) || time() <= strtotime($item['stopdate'])) {
                    $accesslist[] = $item;
                }
            }
        }
    }
    $defaultaccesslist = $accesslist ? 0 : 1;
    $myinstitutions = array();
    foreach ($USER->get('institutions') as $i) {
        $myinstitutions[] = array('type' => 'institution', 'id' => $i->institution, 'start' => null, 'end' => null, 'name' => hsc($i->displayname), 'preset' => false);
    }
    foreach ($allowedpresets as &$preset) {
        $preset = array('type' => $preset, 'id' => $preset, 'start' => null, 'end' => null, 'name' => get_string($preset == 'loggedin' ? 'registeredusers' : $preset, 'view'), 'preset' => true);
    }
    $allgroups = array('type' => 'allgroups', 'id' => 'allgroups', 'start' => null, 'end' => null, 'name' => get_string('allmygroups', 'group'), 'preset' => true);
    $mygroups = array();
    foreach (group_get_user_groups($USER->get('id')) as $g) {
        $group = array('type' => 'group', 'id' => $g->id, 'start' => null, 'end' => null, 'name' => $g->name, 'preset' => false);
        if ($strlen($g->name) > 30) {
            $group['shortname'] = str_shorten_text($g->name, 30, true);
        }
        $mygroups[] = $group;
    }
    $faves = array();
    foreach (get_user_favorites($USER->get('id')) as $u) {
        $fave = array('type' => 'user', 'id' => $u->id, 'start' => null, 'end' => null, 'name' => $u->name, 'preset' => false);
        if ($strlen($u->name) > 30) {
            $fave['shortname'] = str_shorten_text($u->name, 30, true);
        }
        $faves[] = $fave;
    }
    require_once get_config('libroot') . 'pieforms/pieform/elements/calendar.php';
    $options = array('dateFormat' => get_string('calendar_dateFormat', 'langconfig'), 'timeFormat' => get_string('calendar_timeFormat', 'langconfig'), 'stepHour' => 1, 'stepMinute' => 5);
    $options = pieform_element_calendar_get_lang_strings($options, $LANGDIRECTION);
    $datepickeroptionstr = '';
    foreach ($options as $key => $option) {
        if (is_numeric($option)) {
            $datepickeroptionstr .= $key . ': ' . $option . ',';
        } else {
            if (is_array($option)) {
                foreach ($option as $k => $v) {
                    if (!is_numeric($v)) {
                        if (preg_match('/^\'(.*)\'$/', $v, $match)) {
                            $v = $match[1];
                        }
                        $option[$k] = json_encode($v);
                    }
                }
                $option = '[' . implode(',', $option) . ']';
                $datepickeroptionstr .= $key . ': ' . $option . ',';
            } else {
                $datepickeroptionstr .= $key . ': ' . json_encode($option) . ',';
            }
        }
    }
    $smarty->assign('datepickeroptions', $datepickeroptionstr);
    $smarty->assign('viewtype', $element['viewtype']);
    $smarty->assign('potentialpresets', json_encode($allowedpresets));
    $smarty->assign('loggedinindex', $loggedinindex);
    $smarty->assign('accesslist', json_encode($accesslist));
    $smarty->assign('defaultaccesslist', $defaultaccesslist);
    $smarty->assign('viewid', $form->get_property('viewid'));
    $smarty->assign('formname', $form->get_property('name'));
    $smarty->assign('myinstitutions', json_encode($myinstitutions));
    $smarty->assign('allowcomments', $element['allowcomments']);
    $smarty->assign('allgroups', json_encode($allgroups));
    $smarty->assign('mygroups', json_encode($mygroups));
    $smarty->assign('faves', json_encode($faves));
    return $smarty->fetch('form/viewacl.tpl');
}
Exemple #24
0
 /**
  * Builds the configuration pieform for the image browser
  *
  * @return array Array with two keys: 'html' for raw html, 'javascript' for
  *               javascript to run
  */
 public function build_image_browser_form()
 {
     require_once 'view.php';
     static $renderedform;
     if (!empty($renderedform)) {
         return $renderedform;
     }
     safe_require('artefact', 'file');
     $this->set('artefactplugin', 'file');
     $elements['url'] = array('type' => 'text', 'title' => get_string('url'), 'size' => 50);
     $elements['artefactfieldset'] = array('type' => 'fieldset', 'collapsible' => true, 'collapsed' => true, 'legend' => get_string('image'), 'class' => 'last select-file mtl', 'elements' => array('artefactid' => self::config_filebrowser_element($this, null)));
     $configdata = $this->get('configdata');
     $elements['sure'] = array('type' => 'hidden', 'value' => 1);
     // use these to determine which space to display to upload files to
     $elements['post'] = array('type' => 'hidden', 'value' => $this->post);
     $elements['group'] = array('type' => 'hidden', 'value' => $this->group);
     $elements['institution'] = array('type' => 'hidden', 'value' => $this->institution);
     $elements['view'] = array('type' => 'hidden', 'value' => $this->view);
     // tinymce specific elements
     $alignoptions = array('none' => '--', 'top' => 'Top', 'middle' => 'Middle', 'bottom' => 'Bottom', 'left' => 'Left', 'right' => 'Right');
     $elements['toggleformatting'] = array('type' => 'html', 'class' => 'toggleablecontainer', 'value' => '<div id="formattingoptionstoggle" class="retracted arrow"><label>Image formatting options</label></div>');
     $elements['formattingoptions'] = array('type' => 'container', 'name' => 'formattingoptions', 'class' => 'js-hidden', 'elements' => array('width' => array('type' => 'text', 'title' => get_string('dimensions'), 'size' => 3, 'rules' => array('maxlength' => 4)), 'height' => array('type' => 'text', 'size' => 3, 'rules' => array('maxlength' => 4)), 'constrain' => array('type' => 'switchbox', 'title' => get_string('constrain'), 'defaultvalue' => true), 'vspace' => array('type' => 'text', 'title' => get_string('vspace'), 'size' => 3, 'rules' => array('maxlength' => 2)), 'hspace' => array('type' => 'text', 'title' => get_string('hspace'), 'size' => 3, 'rules' => array('maxlength' => 2)), 'border' => array('type' => 'text', 'title' => get_string('border'), 'size' => 3, 'rules' => array('maxlength' => 2)), 'align' => array('defaultvalue' => 'none', 'type' => 'select', 'title' => get_string('alignment'), 'options' => $alignoptions), 'style' => array('type' => 'text', 'title' => get_string('style'), 'size' => 50)));
     $wwwroot = get_config('wwwroot');
     $goto = "";
     if ($this->view) {
         $goto = $wwwroot . 'view/blocks.php' . View::make_base_url();
         // editing forum topic
     } else {
         if ($this->post) {
             $goto = $wwwroot . "interaction/forum/edittopic.php?id=" . $this->post;
         } else {
             if ($this->group) {
                 // editing forum itself
                 $goto = $wwwroot . "interaction/edit.php?id=" . $this->group;
             }
         }
     }
     // Add submit/cancel buttons
     // goto should not be used by those with javascript - cancel is handled by js function which simply removes the image browser
     $elements['action_submitimage'] = array('type' => 'submitcancel', 'class' => 'btn-default', 'value' => array(get_string('submit'), get_string('cancel')), 'goto' => $goto);
     $configdirs = array(get_config('libroot') . 'form/');
     if ($this->get('artefactplugin')) {
         $configdirs[] = get_config('docroot') . 'artefact/' . $this->get('artefactplugin') . '/form/';
     }
     $form = array('name' => 'imgbrowserconf', 'action' => get_config('wwwroot') . 'json/imagebrowser.json.php', 'renderer' => 'div', 'validatecallback' => array($this, 'config_validate'), 'successcallback' => array($this, 'config_success'), 'jsform' => true, 'jssuccesscallback' => 'imageBrowserConfigSuccess', 'jserrorcallback' => 'imageBrowserConfigError', 'elements' => $elements, 'viewgroup' => $this->get_view()->get('group'), 'group' => $this->get('group'), 'viewinstitution' => $this->get_view()->get('institution'), 'institution' => $this->get('institution'), 'configdirs' => $configdirs, 'plugintype' => 'blocktype', 'pluginname' => $this->get('blocktype'));
     require_once 'pieforms/pieform.php';
     $pieform = new Pieform($form);
     if ($pieform->is_submitted()) {
         global $SESSION;
         $SESSION->add_error_msg(get_string('errorprocessingform'));
     }
     $html = $pieform->build();
     // We probably need a new version of $pieform->build() that separates out the js
     // Temporary evil hack:
     if (preg_match('/<script type="(text|application)\\/javascript">(new Pieform\\(.*\\);)<\\/script>/', $html, $matches)) {
         $js = "var pf_{$form['name']} = " . $matches[2] . "pf_{$form['name']}.init();";
     } else {
         $js = '';
     }
     // We need to load any javascript required for the pieform. We do this
     // by checking for an api function that has been added especially for
     // the purpose, but that is not part of Pieforms. Maybe one day later
     // it will be though
     // $js = '';
     foreach ($elements as $key => $element) {
         $element['name'] = $key;
         $function = 'pieform_element_' . $element['type'] . '_views_js';
         if (is_callable($function)) {
             $js .= call_user_func_array($function, array($pieform, $element));
         }
     }
     $renderedform = array('html' => $html, 'javascript' => $js);
     return $renderedform;
 }
Exemple #25
0
/**
 * Browser for files area.
 *
 * @param Pieform  $form    The form to render the element for
 * @param array    $element The element to render
 * @return string           The HTML for the element
 */
function pieform_element_filebrowser(Pieform $form, $element)
{
    require_once 'license.php';
    global $USER, $_PIEFORM_FILEBROWSERS;
    $smarty = smarty_core();
    // See if the filebrowser has indicated it's a group element
    if (!empty($element['group'])) {
        $group = $element['group'];
    } else {
        // otherwise check if the form knows it's in a group setting
        $group = $form->get_property('group');
    }
    // See if the filebrowser has indicated it's an institution element
    if (!empty($element['institution'])) {
        $institution = $element['institution'];
    } else {
        // otherwise check if the form knows it's in an institution setting
        $institution = $form->get_property('institution');
    }
    $formid = $form->get_name();
    $prefix = $formid . '_' . $element['name'];
    if (!empty($element['tabs'])) {
        $tabdata = pieform_element_filebrowser_configure_tabs($element['tabs'], $prefix);
        $smarty->assign('tabs', $tabdata);
        if (!$group && $tabdata['owner'] == 'group') {
            $group = $tabdata['ownerid'];
        } else {
            if (!$institution) {
                if ($tabdata['owner'] == 'institution') {
                    $institution = $tabdata['ownerid'];
                } else {
                    if ($tabdata['owner'] == 'site') {
                        $institution = 'mahara';
                    }
                }
            }
        }
    }
    $userid = $group || $institution ? null : $USER->get('id');
    // refresh quotas
    if ($userid) {
        $USER->quota_refresh();
    }
    $folder = $element['folder'];
    if ($group && !pieform_element_filebrowser_view_group_folder($group, $folder)) {
        $folder = null;
    }
    $path = pieform_element_filebrowser_get_path($folder);
    $smarty->assign('folder', $folder);
    $smarty->assign('foldername', $path[0]->title);
    $smarty->assign('path', array_reverse($path));
    $smarty->assign('highlight', $element['highlight'][0]);
    $smarty->assign('edit', !empty($element['edit']) ? $element['edit'] : -1);
    if (isset($element['browse'])) {
        $smarty->assign('browse', (int) $element['browse']);
    }
    $config = array_map('intval', $element['config']);
    if ($group && $config['edit']) {
        $smarty->assign('groupinfo', pieform_element_filebrowser_get_groupinfo($group));
    }
    if ($config['select']) {
        if (function_exists($element['selectlistcallback'])) {
            if ($form->is_submitted() && $form->has_errors() && isset($_POST[$prefix . '_selected']) && is_array($_POST[$prefix . '_selected'])) {
                $value = array_keys($_POST[$prefix . '_selected']);
            } else {
                if (isset($element['defaultvalue'])) {
                    $value = $element['defaultvalue'];
                } else {
                    $value = null;
                }
            }
            // check to see if attached artefact items in $value array are actually allowed
            // to be seen by this user
            if (!empty($value)) {
                foreach ($value as $k => $v) {
                    $file = artefact_instance_from_id($v);
                    if (!$file instanceof ArtefactTypeFile && !$file instanceof ArtefactTypeFolder || !$USER->can_view_artefact($file)) {
                        unset($value[$k]);
                    }
                }
            }
            $selected = $element['selectlistcallback']($value);
        }
        $smarty->assign('selectedlist', $selected);
        $selectedliststr = json_encode($selected);
    }
    if ($config['uploadagreement']) {
        if (get_config_plugin('artefact', 'file', 'usecustomagreement')) {
            $smarty->assign('agreementtext', get_field('site_content', 'content', 'name', 'uploadcopyright'));
        } else {
            $smarty->assign('agreementtext', get_string('uploadcopyrightdefaultcontent', 'install'));
        }
    } else {
        if (!isset($config['simpleupload'])) {
            $config['simpleupload'] = 1;
        }
    }
    $licensing = '<div class="fileuploadlicense">' . license_form_files($prefix) . '</div>';
    $smarty->assign('licenseform', $licensing);
    if ($config['resizeonuploaduseroption'] == 1) {
        $smarty->assign('resizeonuploadenable', get_config_plugin('artefact', 'file', 'resizeonuploadenable'));
        $smarty->assign('resizeonuploadmaxwidth', get_config_plugin('artefact', 'file', 'resizeonuploadmaxwidth'));
        $smarty->assign('resizeonuploadmaxheight', get_config_plugin('artefact', 'file', 'resizeonuploadmaxheight'));
    }
    if ($config['upload']) {
        $maxuploadsize = display_size(get_max_upload_size(!$institution && !$group));
        $smarty->assign('maxuploadsize', $maxuploadsize);
        $smarty->assign('phpmaxfilesize', get_max_upload_size(false));
        if ($group) {
            $smarty->assign('uploaddisabled', !pieform_element_filebrowser_edit_group_folder($group, $folder));
        }
    }
    if (!empty($element['browsehelp'])) {
        $config['plugintype'] = $form->get_property('plugintype');
        $config['pluginname'] = $form->get_property('pluginname');
        $config['browsehelp'] = $element['browsehelp'];
    }
    $config['showtags'] = !empty($config['tag']) ? (int) $userid : 0;
    $config['editmeta'] = (int) ($userid && !$config['edit'] && !empty($config['tag']));
    $smarty->assign('config', $config);
    $filters = isset($element['filters']) ? $element['filters'] : null;
    $filedata = ArtefactTypeFileBase::get_my_files_data($folder, $userid, $group, $institution, $filters);
    $smarty->assign('filelist', $filedata);
    $configstr = json_encode($config);
    $fileliststr = json_encode($filedata);
    $smarty->assign('prefix', $prefix);
    $accepts = isset($element['accept']) ? 'accept="' . Pieform::hsc($element['accept']) . '"' : '';
    $smarty->assign('accepts', $accepts);
    $initjs = "{$prefix} = new FileBrowser('{$prefix}', {$folder}, {$configstr}, config);\n{$prefix}.filedata = {$fileliststr};";
    if ($config['select']) {
        $initjs .= "{$prefix}.selecteddata = {$selectedliststr};";
    }
    if (isset($tabdata)) {
        $initjs .= "{$prefix}.tabdata = " . json_encode($tabdata) . ';';
    }
    $_PIEFORM_FILEBROWSERS[$prefix]['views_js'] = $initjs;
    $initjs .= "addLoadEvent({$prefix}.init);";
    $initjs .= "upload_max_filesize = '" . get_real_size(ini_get('upload_max_filesize')) . "';";
    $smarty->assign('initjs', $initjs);
    $smarty->assign('querybase', $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&'));
    $params = 'folder=' . $folder;
    if ($group) {
        $params .= '&group=' . $group;
    }
    if ($institution) {
        $params .= '&institution=' . $institution;
    }
    $switchwidth = ArtefactTypeFileBase::get_switch_width();
    $smarty->assign('switchwidth', $switchwidth);
    $smarty->assign('folderparams', $params);
    return $smarty->fetch('artefact:file:form/filebrowser.tpl');
}
/**
 * Browser for files area.
 *
 * @param Pieform  $form    The form to render the element for
 * @param array    $element The element to render
 * @return string           The HTML for the element
 */
function pieform_element_filebrowser(Pieform $form, $element)
{
    global $USER, $_PIEFORM_FILEBROWSERS;
    $smarty = smarty_core();
    $group = $form->get_property('group');
    $institution = $form->get_property('institution');
    if (!empty($element['tabs'])) {
        $tabdata = pieform_element_filebrowser_configure_tabs($element['tabs']);
        $smarty->assign('tabs', $tabdata);
        if (!$group && $tabdata['owner'] == 'group') {
            $group = $tabdata['ownerid'];
        } else {
            if (!$institution) {
                if ($tabdata['owner'] == 'institution') {
                    $institution = $tabdata['ownerid'];
                } else {
                    if ($tabdata['owner'] == 'site') {
                        $institution = 'mahara';
                    }
                }
            }
        }
    }
    $userid = $group || $institution ? null : $USER->get('id');
    // refresh quotas
    if ($userid) {
        $USER->quota_refresh();
    }
    $folder = $element['folder'];
    $path = pieform_element_filebrowser_get_path($folder);
    $smarty->assign('folder', $folder);
    $smarty->assign('foldername', $path[0]->title);
    $smarty->assign('path', array_reverse($path));
    $smarty->assign('highlight', $element['highlight'][0]);
    $smarty->assign('edit', !empty($element['edit']) ? $element['edit'] : -1);
    if (isset($element['browse'])) {
        $smarty->assign('browse', (int) $element['browse']);
    }
    $config = array_map('intval', $element['config']);
    if ($group && $config['edit']) {
        $smarty->assign('groupinfo', pieform_element_filebrowser_get_groupinfo($group));
    }
    $formid = $form->get_name();
    $prefix = $formid . '_' . $element['name'];
    if ($config['select']) {
        if (function_exists($element['selectlistcallback'])) {
            if ($form->is_submitted() && $form->has_errors() && isset($_POST[$prefix . '_selected']) && is_array($_POST[$prefix . '_selected'])) {
                $value = array_keys($_POST[$prefix . '_selected']);
            } else {
                if (isset($element['defaultvalue'])) {
                    $value = $element['defaultvalue'];
                } else {
                    $value = null;
                }
            }
            $selected = $element['selectlistcallback']($value);
        }
        $smarty->assign('selectedlist', $selected);
        $selectedliststr = json_encode($selected);
    }
    if ($config['uploadagreement']) {
        if (get_config_plugin('artefact', 'file', 'usecustomagreement')) {
            $smarty->assign('agreementtext', get_field('site_content', 'content', 'name', 'uploadcopyright'));
        } else {
            $smarty->assign('agreementtext', get_string('uploadcopyrightdefaultcontent', 'install'));
        }
    }
    if ($config['upload']) {
        $maxuploadsize = min(get_real_size(ini_get('post_max_size')), get_real_size(ini_get('upload_max_filesize')));
        if (!$institution && !$group) {
            $userquotaremaining = $USER->get('quota') - $USER->get('quotaused');
            $maxuploadsize = min($maxuploadsize, $userquotaremaining);
        }
        $maxuploadsize = display_size($maxuploadsize);
        $smarty->assign('maxuploadsize', $maxuploadsize);
    }
    if (!empty($element['browsehelp'])) {
        $config['plugintype'] = $form->get_property('plugintype');
        $config['pluginname'] = $form->get_property('pluginname');
        $config['browsehelp'] = $element['browsehelp'];
    }
    $config['showtags'] = !empty($config['tag']) ? (int) $userid : 0;
    $config['editmeta'] = (int) ($userid && !$config['edit'] && !empty($config['tag']));
    $smarty->assign('config', $config);
    $filters = isset($element['filters']) ? $element['filters'] : null;
    $filedata = ArtefactTypeFileBase::get_my_files_data($folder, $userid, $group, $institution, $filters);
    $smarty->assign('filelist', $filedata);
    $configstr = json_encode($config);
    $fileliststr = json_encode($filedata);
    $smarty->assign('prefix', $prefix);
    $initjs = "{$prefix} = new FileBrowser('{$prefix}', {$folder}, {$configstr}, config);\n{$prefix}.filedata = {$fileliststr};";
    if ($config['select']) {
        $initjs .= "{$prefix}.selecteddata = {$selectedliststr};";
    }
    $_PIEFORM_FILEBROWSERS[$prefix]['views_js'] = $initjs;
    $initjs .= "addLoadEvent({$prefix}.init);";
    $smarty->assign('initjs', $initjs);
    $smarty->assign('querybase', $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&'));
    return $smarty->fetch('artefact:file:form/filebrowser.tpl');
}