institutionpages.php 6.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
/**
 *
 * @package    mahara
 * @subpackage admin
 * @author     Catalyst IT Ltd
 * @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.
 *
 */

define('INTERNAL', 1);
define('INSTITUTIONALADMIN', 1);
define('MENUITEM', 'manageinstitutions/sitepages');
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
17
define('SECTION_PAGE', 'institutionstaticpages');
18 19

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
20
define('TITLE', get_string('institutionstaticpages', 'admin'));
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
require_once('pieforms/pieform.php');
require_once('license.php');
define('DEFAULTPAGE', 'home');
require_once('institution.php');

$sitepages = array();
$corepagenames = site_content_pages();
$localpagenames = function_exists('local_site_content_pages') ? local_site_content_pages() : array();
if ($pagenames = array_merge($corepagenames, $localpagenames)) {
    $sitepages = get_records_select_array(
        'site_content', 'name IN (' . join(',', array_fill(0, count($pagenames), '?')) . ')', $pagenames
    );
}
$pageoptions = array();

Naomi Guyer's avatar
Naomi Guyer committed
36

Naomi Guyer's avatar
Naomi Guyer committed
37
$institutionelement = get_institution_selector(false);
Naomi Guyer's avatar
Naomi Guyer committed
38

39 40
if (!empty($institutionelement['options']) && sizeof($institutionelement['options']) > 1) {
    $institutionelement['defaultvalue'] = key($institutionelement['options']);
41
}
42
else if (!empty($institutionelement['options']) && sizeof($institutionelement['options']) == 1) {
43 44
    // Institutional admins with only 1 institution do not get institution dropdown
    // Same with admins and only one institution exists
45 46 47
    $institutionelement = array('type' => 'hidden',
                                 'value' => key($institutionelement['options']),
                                 'defaultvalue' => key($institutionelement['options']),
48 49
                                );
}
50
else if (empty($institutionelement['options'])) {
51
    // Only the 'no institution' institution exists so we need to display this fact
52
    $smarty = smarty();
53 54
    $smarty->assign('noinstitutionsadmin', (($USER->admin) ? get_string('noinstitutionstaticpagesadmin', 'admin', get_config('wwwroot') . 'admin/site/pages.php') : false));
    $smarty->assign('noinstitutions', get_string('noinstitutionstaticpages', 'admin'));
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    $smarty->assign('PAGEHEADING', TITLE);
    $smarty->display('admin/site/pages.tpl');
    exit;
}


foreach ($sitepages as $page) {
    $section = in_array($page->name, $localpagenames) ? 'local' : 'admin';
    $pageoptions[$page->name] = get_string($page->name, $section);
    $pagecontents[$page->name] = $page->content;
}
asort($pageoptions);

$getstring = array('discardpageedits' => json_encode(get_string('discardpageedits', 'admin')));

$form = pieform(array(
    'name'              => 'editsitepage',
    'jsform'            => true,
    'jssuccesscallback' => 'contentSaved',
    'elements'          => array(
Naomi Guyer's avatar
Naomi Guyer committed
75
        'pageinstitution' => $institutionelement,
76 77 78 79 80 81 82
        'pagename'    => array(
            'type'    => 'select',
            'title'   => get_string('pagename', 'admin'),
            'defaultvalue' => DEFAULTPAGE,
            'options' => $pageoptions
        ),
        'pageusedefault' => array(
83
            'type'    => 'switchbox',
84
            'title'   => get_string('usedefault', 'admin'),
85
            'description'  => get_string('usedefaultdescription2', 'admin'),
86
            'defaultvalue' => (get_config_institution($institutionelement['defaultvalue'], 'sitepages_' . DEFAULTPAGE) == 'mahara' ? 1 : 0),
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        ),
        'pagetext' => array(
            'name'        => 'pagetext',
            'type'        => 'wysiwyg',
            'rows'        => 25,
            'cols'        => 100,
            'title'       => get_string('pagetext', 'admin'),
            'defaultvalue' => $pagecontents[DEFAULTPAGE],
            'rules'       => array(
                'maxlength' => 65536,
                'required' => true
            )
        ),
        'submit' => array(
            'type'  => 'submit',
102
            'class' => 'btn-primary',
103 104 105 106 107 108 109 110
            'value' => get_string('savechanges', 'admin')
        ),
    )
));

function editsitepage_validate(Pieform $form, $values) {
    $allowedinstitutions = get_institution_selector(false);
    if (array_search($values['pageinstitution'], array_flip($allowedinstitutions['options'])) === false) {
111
        $form->set_error(null, get_string('staticpageinstitutionbad', 'admin', $values['pageinstitution']));
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    }
}

function editsitepage_submit(Pieform $form, $values) {
    global $USER;
    $data = new StdClass;
    $data->name    = $values['pagename'];
    if (empty($values['pageusedefault'])) {
        $data->content = $values['pagetext'];
    }
    $data->mtime   = db_format_timestamp(time());
    $data->mauthor = $USER->get('id');
    $data->institution = $values['pageinstitution'];
    // update the institution config if needed
    if (isset($values['pageusedefault'])) {
        $configdata = new StdClass;
        $configdata->institution = $data->institution;
        $configdata->field = 'sitepages_' . $data->name;
        $whereobj = clone $configdata;
        $configdata->value = !empty($values['pageusedefault']) ? 'mahara' : $data->institution;
        ensure_record_exists('institution_config', $whereobj, $configdata);
    }
    if (get_record('site_content', 'name', $data->name, 'institution', $data->institution)) {
        try {
            update_record('site_content', $data, array('name', 'institution'));
        }
        catch (SQLException $e) {
            $form->reply(PIEFORM_ERR, get_string('savefailed', 'admin'));
        }
    }
    else {
        // local site page doesn't exist for this institution so we shall add it
        $data->ctime = db_format_timestamp(time());
        try {
            insert_record('site_content', $data);
        }
        catch (SQLException $e) {
            $form->reply(PIEFORM_ERR, get_string('savefailed', 'admin'));
        }
    }
    $form->reply(PIEFORM_OK, get_string('pagesaved', 'admin'));
}

$smarty = smarty(array('adminsitepages'), array(), array('admin' => array('discardpageedits')));
156 157
setpageicon($smarty, 'icon-university');

158
$smarty->assign('noinstitutionsadmin', (($USER->admin) ? get_string('noinstitutionstaticpagesadmin', 'admin', get_config('wwwroot') . 'admin/site/pages.php') : false));
159 160 161
$smarty->assign('pageeditform', $form);
$smarty->assign('PAGEHEADING', TITLE);
$smarty->display('admin/site/pages.tpl');