Commit 94533acc authored by Maria Sorica's avatar Maria Sorica Committed by Robert Lyon
Browse files

Bug 1746259: Display and edit the T&C of an institution

behatnotneeded

Change-Id: I71f21f76129bc160904cffd1ff62dc6c7fd83c27
parent 4e67c147
Loading
Loading
Loading
Loading
+62 −44
Original line number Diff line number Diff line
@@ -19,8 +19,8 @@ define('MENUITEM', 'manageinstitutions/privacy');
require_once('institution.php');

define('TITLE', get_string('legal', 'admin'));
define('SUBSECTIONHEADING', get_string('privacy', 'admin'));
$versionid = param_integer('id', null);
$fs = param_alpha('fs', 'privacy');

if (!is_logged_in()) {
    throw new AccessDeniedException();
@@ -51,19 +51,38 @@ $institutionselector = pieform(array(
$wwwroot = get_config('wwwroot');

// The "Add one" link displayed when an institution has no privay statement of its own.
$href = $wwwroot . 'admin/users/institutionprivacy.php?institution=' . $institution . '&id=0';
$href = $wwwroot . 'admin/users/institutionprivacy.php?institution=' . $institution . '&id=0&fs=' . $fs;

$privacies = get_records_sql_assoc("
    SELECT  s.id, s.version, u.firstname, u.lastname, u.id AS userid, s.content, s.ctime
    FROM {site_content_version} s
    LEFT JOIN {usr} u ON s.author = u.id
    WHERE s.type = 'privacy' AND s.institution = ?
    ORDER BY s.id DESC", array($institution));
// Get the institution's privacy statements and T&Cs.
$privacies = get_institution_versioned_content($institution);

// Add to an array the latest versions of both T&C and privacy statement.
$latestVersions = array(); $types = array();
if ($privacies) {
    foreach ($privacies as $key => $content) {
        if ($content->current != null) {
            array_push($latestVersions, $key);
        }
        if (!in_array($content->type, $types)) {
            // Useful in case an institution has just one type of content.
            // Will use the $types to know on which tab to display the versions table.
            array_push($types, $content->type);
        }
        $content->displayname = display_name($content->userid, null, true);
    }
}
// Add 0 to $latestVersions, to allow the creation of a first privacy/T&C
if (count($types) <= 1) {
    array_push($latestVersions, 0);
}
// Site privacy and T&C to display in an expandable panel.
$sitecontent = get_latest_privacy_versions(array('mahara'));
$selectedtab = $fs;
$form = false;
if ($versionid !== null) {
    $pageoptions = get_record('site_content_version', 'id', $versionid, 'institution', $institution);
    if ($versionid === 0 || $pageoptions) {
        $selectedtab = ($versionid === 0) ? $selectedtab : $pageoptions->type;
        $form = pieform(array(
            'name'              => 'editsitepage',
            'jsform'            => false,
@@ -72,7 +91,7 @@ if ($versionid !== null) {
                'version' => array(
                    'type'         => 'text',
                    'title'        => get_string('version', 'admin'),
                    'description'  => $pageoptions ? get_string('lastversion', 'admin', $pageoptions->version) : '',
                    'description'  => $pageoptions ? get_string($privacies[$versionid]->type . 'lastversion', 'admin', $pageoptions->version) : '',
                    'defaultvalue' => '',
                    'rules' => array(
                        'required'    => true,
@@ -80,6 +99,7 @@ if ($versionid !== null) {
                    )
                ),
                'pageinstitution' => array('type' => 'hidden', 'value' => $institution),
                'activetab' => array('type' => 'hidden', 'value' => $selectedtab),
                'pagetext' => array(
                    'name'        => 'pagetext',
                    'type'        => 'wysiwyg',
@@ -96,7 +116,7 @@ if ($versionid !== null) {
                    'class' => 'btn-primary',
                    'type'  => 'submitcancel',
                    'value' => array(get_string('savechanges', 'admin'), get_string('cancel')),
                    'goto'  => get_config('wwwroot') . 'admin/users/institutionprivacy.php?institution=' . $institution,
                    'goto'  => get_config('wwwroot') . 'admin/users/institutionprivacy.php?institution=' . $institution . '&fs=' . $selectedtab,
                ),
            )
        ));
@@ -107,69 +127,67 @@ if ($versionid !== null) {
}

function editsitepage_validate(Pieform $form, $values) {
    // Check if the version entered by the user already exists
    if (record_exists('site_content_version', 'institution', $values['pageinstitution'], 'version', $values['version'])) {
        $form->set_error('version', get_string('versionalreadyexist', 'admin', $values['version']));
    // Check if the version entered by the user already exists for a specific content type.
    if (record_exists('site_content_version', 'institution', $values['pageinstitution'], 'version', $values['version'], 'type', $values['activetab'])) {
        $form->set_error('version', get_string('versionalreadyexist', 'admin', get_string($values['activetab'] . 'lowcase', 'admin'), $values['version']));
    }
}

function editsitepage_submit(Pieform $form, $values) {
    global $USER, $SESSION;

    $id = get_field('site_content_version', 'id', 'version', $values['version']);
    require_once('embeddedimage.php');
    // Update the pagetext with any embedded image info
    $pagetext = EmbeddedImage::prepare_embedded_images($values['pagetext'], 'staticpages', $id);

    $data = new StdClass;
    $data->content = $pagetext;
    $data->content = $values['pagetext'];
    $data->author = $USER->get('id');
    $data->institution = $values['pageinstitution'];
    $data->ctime = db_format_timestamp(time());
    $data->version = $values['version'];
    $data->type = 'privacy';
    $data->type = $values['activetab'];

    try {
        insert_record('site_content_version', $data);
        $id = insert_record('site_content_version', $data, 'id', true);
        if ($id) {
            require_once('embeddedimage.php');
            $pagetext = EmbeddedImage::prepare_embedded_images($values['pagetext'], 'staticpages', $id);
            // If there is an embedded image, update the src so users can have visibility
            if ($values['pagetext'] != $pagetext) {
                // Update the pagetext with any embedded image info
                $updated = new stdClass();
                $updated->id = $id;
                $updated->content = $pagetext;
                update_record('site_content_version', $updated, 'id');
            }
            // Auto accept the PS/T&C to avoid situation in which
            // the admin is asked to agree to the PS/T&C he has just created.
            save_user_reply_to_agreement($USER->get('id'), $id, 1);
        }
        $SESSION->add_ok_msg(get_string('pagesaved', 'admin'));
    }
    catch (SQLException $e) {
        $SESSION->add_ok_msg(get_string('savefailed', 'admin'));
    }
    redirect(get_config('wwwroot').'admin/users/institutionprivacy.php?institution=' . $values['pageinstitution']);
    redirect(get_config('wwwroot').'admin/users/institutionprivacy.php?institution=' . $values['pageinstitution'] . '&fs=' . $values['activetab']);
}

// Site privacy to display in an expandable panel
$siteprivacycontent = get_record_sql("
    SELECT s.content, s.ctime
    FROM {site_content_version} s
    WHERE s.type = 'privacy' AND s.institution = ?
    ORDER BY s.id DESC
    LIMIT 1", array('mahara'));

$js = <<< EOF
jQuery(function($) {
  function reloadUsers() {
      window.location.href = '{$wwwroot}admin/users/institutionprivacy.php?institution=' + $('#usertypeselect_institution').val();
  }

$(document).ready(function() {
  checkActiveTab('$selectedtab');
  $('#usertypeselect_institution').on('change', reloadUsers);
});
EOF;

$smarty = smarty();
$smarty = smarty(array('privacy'));
setpageicon($smarty, 'icon-umbrella');

$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->assign('href', $href);
$smarty->assign('siteprivacycontent', $siteprivacycontent);
$smarty->assign('lastupdated', get_string('lastupdatedon', 'blocktype.externalfeed', format_date(strtotime($siteprivacycontent->ctime))));
$smarty->assign('sitecontent', $sitecontent);
$smarty->assign('versionid', $versionid);
$smarty->assign('privacies', $privacies);
$smarty->assign('results', $privacies);
$smarty->assign('pageeditform', $form);
$smarty->assign('institution', $institution);
$smarty->assign('latestversion', $privacies ? reset($privacies)->version : 0);
$smarty->assign('latestprivacyid', $privacies ? reset($privacies)->id : 0);
$smarty->assign('version', $versionid && $pageoptions ? $pageoptions->version : '');
$smarty->assign('latestVersions', $latestVersions);
$smarty->assign('institutionselector', $institutionselector);
$smarty->assign('types', implode(' ', $types));
$smarty->assign('link', "admin/users/institutionprivacy.php?institution={$institution}&id=");
$smarty->display('admin/users/institutionprivacy.tpl');
+17 −12
Original line number Diff line number Diff line
@@ -931,6 +931,7 @@ function fetch_graph_data(opts) {
function updateUrlParameter(url, param, value) {
    var found = false;
    var vars = url.split("?");
    if (typeof(vars[1]) !== 'undefined') {
        varparams = vars[1].split("&");

        for (var i = 0; i < varparams.length; i++) {
@@ -946,6 +947,10 @@ function updateUrlParameter(url, param, value) {
        if (!found) {
            url = url + '&' + param + '=' + value;
        }
    }
    else {
        url = url + '?' + param + '=' + value;
    }
    return url;
}

+37 −0
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@ function checkActiveTab(activeTab) {
}

function showTab(el) {
    var newurl = updateUrlParameter(location.href, 'fs', el.replace("#", ""));
    history.pushState(null, null, newurl);

    var i;
    var x = $(".tab");
    for (i = 0; i < x.length; i++) {
@@ -22,4 +25,38 @@ function showTab(el) {
    }
    $(el).removeClass("js-hidden");
    $(el + '-text').removeClass("js-hidden");
    // Useful in case an institution has just one type of content
    if (typeof types != 'undefined') {
        showNoContentAddOne(el);
    }
}

function showNoContentAddOne(el) {
    $('.nocontent').addClass("js-hidden");
    var activetab = el.replace("#", "");
    if (types.indexOf(activetab) != -1) {
        $('#results').removeClass("js-hidden");
        $('#no-results').addClass("js-hidden");
        $('#no-' + activetab).addClass("js-hidden");
    }
    else {
        $('#results').addClass("js-hidden");
        $('#no-results').removeClass("js-hidden");
        $('#no-' + activetab).removeClass("js-hidden");
        var url = $('#no-results').find('a').prop('href');
        if (url) {
            if (getUrlParameter('fs', url)) {
                var newurl = updateUrlParameter(url, 'fs', activetab);
                $('#no-results').find('a').prop('href', newurl);
            }
        }
    }
}

function reloadUsers() {
    var appendfs = '';
    if (url = getUrlParameter('fs', location.href)) {
        appendfs = '&fs=' + url;
    }
    window.location.href = config.wwwroot + 'admin/users/institutionprivacy.php?institution=' + $('#usertypeselect_institution').val() + appendfs;
}
+3 −0
Original line number Diff line number Diff line
@@ -89,7 +89,9 @@ $string['usedefault'] = 'Use site default';
$string['usedefaultdescription3'] = 'Use the site\'s default text for the selected page type.';
$string['staticpagesdescription'] = 'Edit the content of static pages in Mahara (Home, Terms and Conditions, etc.)';
$string['institutionprivacypagedescription'] = 'Edit the privacy statement for your institution, which is displayed in addition to the site privacy statement. The version you edited last becomes the current privacy statement automatically. You see the current site privacy statement as reference.';
$string['institutiontermspagedescription'] = 'Edit the terms and conditions for your institution, which is displayed in addition to the site terms and conditions. The version you edited last becomes the current terms and conditions automatically. You see the current site terms and conditions as reference.';
$string['noinstitutionprivacy'] = 'There is no privacy statement for this institution yet.';
$string['noinstitutionterms'] = 'There are no terms and conditions for this institution yet.';
$string['menus'] = 'Menus';
$string['menusdescription'] = 'Manage the links and files within the "Links and resources" and footer menus';
$string['sitefiles']          = 'Site files';
@@ -1344,6 +1346,7 @@ $string['privacylastversion'] = 'Privacy statements must have unique version nam
$string['termsandconditionslastversion'] = 'Terms and conditions must have unique version names of up to 15 characters. The latest version for this privacy statement is "%s".';
$string['versionalreadyexist'] = 'A %s version with the name "%s" already exists.';
$string['siteprivacystatement'] = 'Site privacy statement';
$string['sitetermsstatement'] = 'Site terms and conditions';
$string['addoneversionlink']   = '<a href="%s">Add one</a> if you need it.';
$string['privacyversionfor'] = 'Privacy statement for version "%s" is as follows:';
$string['termsversionfor'] = 'Terms and conditions for version "%s" is as follows:';
+8 −9
Original line number Diff line number Diff line
@@ -3244,18 +3244,17 @@ function get_latest_privacy_versions($institutions = array(), $ignoreagreevalue
        $useragreementsql = $joinsql . " {usr_agreement} u ON s2.current = u.sitecontentid AND u.usr = ? AND u.agreed = 1";
        $params = array($USER->get('id'));
    }

    $latestversions = get_records_sql_array("
        SELECT s.id, s.version, s.content, s.ctime, s.institution, " . $userdetails . "
            CASE s.institution WHEN 'mahara' THEN 1 ELSE 2 END as type
    $select = count($institutions) == 1 ? 's.type, s.id' : 's.id, s.type';
    $latestversions = get_records_sql_assoc("
        SELECT " . $select . ", s.id, s.version, s.content, s.ctime, s.institution, " . $userdetails . "
            CASE s.institution WHEN 'mahara' THEN 1 ELSE 2 END AS site
        FROM {site_content_version} s
        INNER JOIN (SELECT MAX(id) as current, institution
        INNER JOIN (SELECT MAX(id) AS current, institution, type
            FROM {site_content_version}
            WHERE type = 'privacy'
            GROUP BY institution) s2 ON s.institution = s2.institution AND s.id = s2.current
            GROUP BY institution, type) s2 ON s.institution = s2.institution AND s.id = s2.current
            " . $useragreementsql . "
        WHERE s.type = 'privacy' AND s.institution IN (" . join(',',array_map('db_quote',$institutions)) . ")
        ORDER BY type", $params);
        WHERE s.institution IN (" . join(',',array_map('db_quote',$institutions)) . ")
        ORDER BY site", $params);

    return $latestversions;
}
Loading