Commit a253ba9c authored by Son Nguyen's avatar Son Nguyen Committed by Robert Lyon

Refactor site templates (Bug 1397110)

- Change ownership of default system templates
 from user 0 to institution 'mahara'
 and set template to 2
- Enable to add site files into site templates

behatnotneeded - existing tests for system templates are ok

Change-Id: Idfd5fca423e6f6a5f66d6cf944f7c5f88112584c
Signed-off-by: 's avatarSon Nguyen <son.nguyen@catalyst.net.nz>
parent d879f296
......@@ -24,7 +24,7 @@ define('TITLE', $title);
$offset = param_integer('offset', 0);
$templateviews = View::get_template_views();
$templateviews = View::get_site_template_views();
list($searchform, $data, $pagination) = View::views_by_owner(null, 'mahara');
if ($data->data) {
$views = array_merge($templateviews, $data->data);
......
......@@ -1031,7 +1031,8 @@ abstract class ArtefactType implements IArtefactType {
'childreninstances' => 1,
'childrenmetadata' => 1,
'parentinstance' => 1,
'parentmetadata' => 1
'parentmetadata' => 1,
'path' => 1 // the path value will be updated later
);
$data = new StdClass;
foreach (get_object_vars($this) as $k => $v) {
......
......@@ -507,11 +507,11 @@ class User {
static $systemprofileviewid = null;
db_begin();
require_once(get_config('libroot') . 'view.php');
if (is_null($systemprofileviewid)) {
$systemprofileviewid = get_field('view', 'id', 'owner', 0, 'type', 'profile');
$systemprofileviewid = get_field('view', 'id', 'institution', 'mahara', 'template', View::SITE_TEMPLATE, 'type', 'profile');
}
require_once(get_config('libroot') . 'view.php');
list($view) = View::create_from_template(array(
'owner' => $this->get('id'),
'title' => get_field('view', 'title', 'id', $systemprofileviewid),
......@@ -561,11 +561,11 @@ class User {
static $systemdashboardviewid = null;
db_begin();
require_once(get_config('libroot') . 'view.php');
if (is_null($systemdashboardviewid)) {
$systemdashboardviewid = get_field('view', 'id', 'owner', 0, 'type', 'dashboard');
$systemdashboardviewid = get_field('view', 'id', 'institution', 'mahara', 'template', View::SITE_TEMPLATE, 'type', 'dashboard');
}
require_once(get_config('libroot') . 'view.php');
list($view) = View::create_from_template(array(
'owner' => $this->get('id'),
'title' => get_field('view', 'title', 'id', $systemdashboardviewid),
......@@ -1181,9 +1181,6 @@ class User {
if ($owner > 0 && $owner == $this->get('id')) {
return true;
}
if ($owner == "0" && $this->get('admin')) {
return true;
}
$institution = $v->get('institution');
if ($institution && $this->can_edit_institution($institution)) {
return true;
......
......@@ -43,7 +43,9 @@ class PluginBlocktypeGroupMembers extends MaharaCoreBlocktype {
public static function render_instance (BlockInstance $instance, $editing = false) {
global $USER;
if ($instance->get_view()->get('owner') == "0") {
// Not render if the block is in a template
require_once(get_config('libroot') . 'view.php');
if ($instance->get_view()->get('template') == View::SITE_TEMPLATE) {
return '';
}
......
......@@ -379,6 +379,13 @@ function watchlist_process_notifications() {
if (isset($view->owner) && empty($view->owner)) {
continue;
}
// Ignore system templates, institution = 'mahara' and template = 2
require_once(get_config('libroot') . 'view.php');
if (isset($view->institution)
&& $view->institution == 'mahara'
&& $view->template == View::SITE_TEMPLATE) {
continue;
}
foreach ($blockinstance_ids as $blockinstance_id) {
if (empty($blockinstance_id)) {
......
......@@ -4412,5 +4412,16 @@ function xmldb_core_upgrade($oldversion=0) {
}
}
if ($oldversion < 2016033108) {
log_debug('Making site pages owned by "mahara" institution');
// Change ownership of system templates: profile, dashboard, and group homepage
// to institution = 'mahara' and template = 2
execute_sql('
UPDATE {view}
SET owner = NULL, institution = ?, template = ?
WHERE owner = ? AND template = ?'
, array('mahara', 2, 0, 1));
}
return $status;
}
......@@ -289,6 +289,11 @@ function group_user_can_assess_submitted_views($groupid, $userid) {
* ...
* )
* @return int The ID of the created group
* @throws InvalidArgumentException
* UserException
* SystemException
* NotFoundException
* AccessDeniedException
*/
function group_create($data) {
if (!is_array($data)) {
......@@ -506,8 +511,8 @@ function group_create($data) {
}
$data['id'] = $id;
// install the homepage
if ($t = get_record('view', 'type', 'grouphomepage', 'template', 1, 'owner', 0)) {
require_once('view.php');
require_once('view.php');
if ($t = get_record('view', 'type', 'grouphomepage', 'template', View::SITE_TEMPLATE, 'institution', 'mahara')) {
$template = new View($t->id, (array)$t);
list($homepage) = View::create_from_template(array(
'group' => $id,
......@@ -516,6 +521,9 @@ function group_create($data) {
'type' => 'grouphomepage',
), $t->id, 0, false);
}
else {
throw new NotFoundException("group_create: group homepage is not found");
}
insert_record('view_access', (object) array(
'view' => $homepage->get('id'),
'accesstype' => $data['public'] ? 'public' : 'loggedin',
......@@ -2344,10 +2352,11 @@ function install_system_grouphomepage_view() {
require_once(get_config('libroot') . 'view.php');
$view = View::create(array(
'type' => 'grouphomepage',
'owner' => 0,
'owner' => null,
'institution' => 'mahara',
'template' => View::SITE_TEMPLATE,
'numrows' => 1,
'columnsperrow' => array((object)array('row' => 1, 'columns' => 1)),
'template' => 1,
'title' => get_string('grouphomepage', 'view'),
));
$view->set_access(array(array(
......
......@@ -751,6 +751,7 @@ function core_install_lastcoredata_defaults() {
$user->email = 'root@example.org';
$user->quota = get_config_plugin('artefact', 'file', 'defaultquota');
$user->authinstance = $auth_instance->id;
$user->admin = 1;
if (is_mysql()) { // gratuitous mysql workaround
$newid = insert_record('usr', $user, 'id', true);
......@@ -777,6 +778,10 @@ function core_install_lastcoredata_defaults() {
require_once('skin.php');
install_skins_default();
// Remove admin privs from root user as it doesn't need it now
$user->admin = 0;
update_record('usr', $user, array('id' => 0));
// Insert the admin user
$user = new StdClass;
$user->username = 'admin';
......
......@@ -2598,22 +2598,22 @@ function add_user_to_autoadd_groups($eventdata) {
* @throws SystemException if the system profile view is already installed
*/
function install_system_profile_view() {
$viewid = get_field('view', 'id', 'owner', 0, 'type', 'profile');
require_once(get_config('libroot') . 'view.php');
$viewid = get_field('view', 'id', 'institution', 'mahara', 'template', View::SITE_TEMPLATE, 'type', 'profile');
if ($viewid) {
throw new SystemException('A system profile view already seems to be installed');
}
require_once(get_config('libroot') . 'view.php');
require_once(get_config('docroot') . 'blocktype/lib.php');
$view = View::create(array(
'type' => 'profile',
'owner' => 0,
'institution' => 'mahara',
'template' => View::SITE_TEMPLATE,
'numrows' => 1,
'columnsperrow' => array((object)array('row' => 1, 'columns' => 2)),
'ownerformat' => FORMAT_NAME_PREFERREDNAME,
'title' => get_string('profileviewtitle', 'view'),
'description' => get_string('profiledescription'),
'template' => 1,
));
), 0);
$view->set_access(array(array(
'type' => 'loggedin'
)));
......@@ -2644,21 +2644,21 @@ function install_system_profile_view() {
* @throws SystemException if the system dashboard view is already installed
*/
function install_system_dashboard_view() {
$viewid = get_field('view', 'id', 'owner', 0, 'type', 'dashboard');
require_once(get_config('libroot') . 'view.php');
$viewid = get_field('view', 'id', 'institution', 'mahara', 'template', View::SITE_TEMPLATE, 'type', 'dashboard');
if ($viewid) {
throw new SystemException('A system dashboard view already seems to be installed');
}
require_once(get_config('libroot') . 'view.php');
require_once(get_config('docroot') . 'blocktype/lib.php');
$view = View::create(array(
'type' => 'dashboard',
'owner' => 0,
'institution' => 'mahara',
'template' => View::SITE_TEMPLATE,
'numrows' => 1,
'columnsperrow' => array((object)array('row' => 1, 'columns' => 2)),
'ownerformat' => FORMAT_NAME_PREFERREDNAME,
'title' => get_string('dashboardviewtitle', 'view'),
'template' => 1,
));
), 0);
$view->set_access(array(array(
'type' => 'loggedin'
)));
......
......@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/wiki/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2016033107;
$config->version = 2016033108;
$config->series = '16.04';
$config->release = '16.04.1testing';
$config->minupgradefrom = 2008040200;
......
......@@ -68,6 +68,10 @@ class View {
const SUBMITTED = 1;
const PENDING_RELEASE = 2;
// constansts view templates
const USER_TEMPLATE = 1;
const SITE_TEMPLATE = 2;
/**
* Which view layout is considered the "default" for views with the given
* number of columns. Must be present in $layouts of course.
......@@ -718,7 +722,7 @@ class View {
if (empty($this->id)) {
// users are only allowed one profile view
if ($this->type == 'profile' && record_exists('view', 'owner', $this->owner, 'type', 'profile')) {
if (!$this->template && $this->type == 'profile' && record_exists('view', 'owner', $this->owner, 'type', 'profile')) {
throw new SystemException(get_string('onlonlyyoneprofileviewallowed', 'error'));
}
$this->id = insert_record('view', $fordb, 'id', true);
......@@ -3313,7 +3317,7 @@ class View {
else if ($owner instanceof User) {
$user = $owner;
}
else if (intval($owner) != 0 || $owner == "0") {
else if (intval($owner) != 0) {
$user = new User();
$user->find_by_id(intval($owner));
}
......@@ -3557,6 +3561,11 @@ class View {
$where = '
WHERE ' . self::owner_sql((object) array('owner' => $userid, 'group' => $groupid, 'institution' => $institution));
// We use institution='mahara' and template=2 for the default site template
if (isset($institution) && $institution === 'mahara') {
$where .= ' AND v.template != ' . self::SITE_TEMPLATE;
}
$order = '';
$groupby = '';
if (!empty($orderby)) {
......@@ -3842,11 +3851,11 @@ class View {
return array();
}
public function get_template_views() {
public function get_site_template_views() {
$views = get_records_sql_array(
"SELECT v.*
FROM {view} v
WHERE v.owner = 0
WHERE v.institution = 'mahara' AND v.template = " . self::SITE_TEMPLATE . "
ORDER BY v.title, v.id", array());
$results = array();
if ($views) {
......@@ -4017,6 +4026,7 @@ class View {
else {
$where .= ' WHERE (v.owner IS NULL OR v.owner > 0)';
}
$where .= ' AND v.template != ' . self::SITE_TEMPLATE;
$where .= '
AND (v.group IS NULL OR v.group NOT IN (SELECT id FROM {group} WHERE deleted = 1))
AND (qu.suspendedctime is null OR v.owner = ?)';
......@@ -5392,7 +5402,7 @@ class View {
define('GROUP', $this->group);
define('NOGROUPMENU', 1);
}
else if ($this->institution == 'mahara' || $this->owner == "0") {
else if ($this->institution == 'mahara') {
define('ADMIN', 1);
define('MENUITEM', 'configsite/siteviews');
}
......@@ -5806,8 +5816,9 @@ class View {
* @return string
*/
public function get_url($full=true, $useid=false) {
if ($this->owner == "0") {
return null;
// No url for a default site template
if ($this->template == self::SITE_TEMPLATE) {
return '';
}
else if ($this->type == 'profile') {
if (!$useid) {
......@@ -5965,12 +5976,13 @@ class View {
/**
* Determine whether the current view is of a type which can be themed.
* Certain view types do not respect themes when displayed.
* Templates do not respect themes as well
*
* @return boolean whether the view type may be themed
*/
function is_themeable() {
$unthemable_types = array('grouphomepage', 'dashboard');
return !in_array($this->type, $unthemable_types);
return !$view->get('template') && !in_array($this->type, $unthemable_types);
}
/**
......@@ -5990,10 +6002,6 @@ class View {
function get_views_and_collections($owner=null, $group=null, $institution=null, $matchconfig=null, $includeprofile=true, $submittedgroup=null, $sort=null) {
$excludelocked = $group && group_user_access($group) != 'admin';
// Anonymous public viewing of a group with 'Allow submissions' checked needs to avoid including the dummy root profile page.
if ($owner == '0') {
$includeprofile = false;
}
$sql = "
SELECT v.id, v.type, v.title, v.accessconf, v.ownerformat, v.startdate, v.stopdate, v.template,
v.owner, v.group, v.institution, v.urlid, v.submittedgroup, v.submittedhost, " .
......@@ -6460,7 +6468,7 @@ function createview_submit(Pieform $form, $values) {
}
else {
// Use the site default portfolio page to create a new page
$sitedefaultviewid = get_field('view', 'id', 'owner', 0, 'type', 'portfolio');
$sitedefaultviewid = get_field('view', 'id', 'institution', 'mahara', 'template', View::SITE_TEMPLATE, 'type', 'portfolio');
if (!empty($sitedefaultviewid)) {
list($view, $template, $copystatus) = View::create_from_template($values, $sitedefaultviewid);
if (isset($copystatus['quotaexceeded'])) {
......@@ -6663,18 +6671,18 @@ function view_group_submission_form_submit(Pieform $form, $values) {
*
*/
function install_system_portfolio_view() {
$viewid = get_field('view', 'id', 'owner', 0, 'type', 'portfolio');
$viewid = get_field('view', 'id', 'institution', 'mahara', 'template', View::SITE_TEMPLATE, 'type', 'portfolio');
if ($viewid) {
log_info('A site default portfolio page already seems to be installed');
return $viewid;
}
$view = View::create(array(
'type' => 'portfolio',
'owner' => 0,
'template' => 1,
'institution' => 'mahara',
'template' => 2,
'title' => get_string('templateportfoliotitle', 'view'),
'description' => get_string('templateportfoliodescription', 'view'),
));
), 0);
$view->set_access(array(array(
'type' => 'loggedin'
)));
......
......@@ -44,7 +44,7 @@
<div id="view-wizard-controls" class="col-collapse-offset col-with-collapse">
{if !$issitetemplate}
{if !$template}
<a class="btn btn-default" href="{$displaylink}">
{str tag=displayview section=view}
<span class="icon icon-arrow-circle-right right" role="presentation" aria-hidden="true"></span>
......
......@@ -19,7 +19,7 @@
<div class="detail">{str tag=grouphomepagedescription section=view}</div>
{elseif $view.description}
<div class="detail">
{if $view.owner == 0 && $view.type == 'portfolio'}
{if $view.issitetemplate && $view.type == 'portfolio'}
{$view.description|strip_tags|safe}
{else}
{$view.description|str_shorten_html:110:true|strip_tags|safe}
......
......@@ -47,7 +47,7 @@ $group = $view->get('group');
$institution = $view->get('institution');
View::set_nav($group, $institution, true);
if (!$USER->can_edit_view($view) || $view->get('owner') == "0") {
if (!$USER->can_edit_view($view)) {
throw new AccessDeniedException();
}
if ($group && !group_within_edit_window($group)) {
......
......@@ -88,7 +88,7 @@ else if ($view->get('type') == 'dashboard') {
}
else if ($view->get('type') == 'grouphomepage') {
$title = get_string('grouphomepage', 'view');
if ($view->get('owner') != "0") {
if ($view->get('template') !== View::SITE_TEMPLATE) {
$groupurl = group_homepage_url(get_record('group', 'id', $view->get('group')), false);
}
define('TITLE', $title);
......@@ -268,7 +268,7 @@ if (isset($groupurl)) {
}
$smarty->assign('institution', $institution);
if (get_config('userscanchooseviewthemes') && $view->is_themeable() && $view->get('owner') != "0") {
if (get_config('userscanchooseviewthemes') && $view->is_themeable()) {
$smarty->assign('viewtheme', $viewtheme);
$smarty->assign('viewthemes', $allowedthemes);
}
......@@ -292,8 +292,6 @@ else {
}
$smarty->assign('issiteview', isset($institution) && ($institution == 'mahara'));
if ($view->get('owner') == "0") {
$smarty->assign('issitetemplate', true);
}
$smarty->assign('isssitetemplate', ($view->get('template') === View::SITE_TEMPLATE ? true : false));
$smarty->assign('PAGEHEADING', $state);
$smarty->display('view/blocks.tpl');
......@@ -16,7 +16,7 @@ $viewid = param_integer('id');
$view = new View($viewid, null);
if (!$view || $view->get('owner') == "0" || !$USER->can_edit_view($view) || $view->is_submitted()) {
if (!$view || !$USER->can_edit_view($view) || $view->is_submitted()) {
throw new AccessDeniedException(get_string('cantdeleteview', 'view'));
}
$groupid = $view->get('group');
......
......@@ -189,9 +189,7 @@ $smarty->assign('edittitle', $view->can_edit_title());
$smarty->assign('displaylink', $view->get_url());
$smarty->assign('new', $new);
$smarty->assign('issiteview', $view->get('institution') == 'mahara');
if ($view->get('owner') == "0") {
$smarty->assign('issitetemplate', true);
}
$smarty->assign('issitetemplate', ($view->get('template') === View::SITE_TEMPLATE ? true : false));
$smarty->assign('PAGEHEADING', $state);
$smarty->display('view/layout.tpl');
......
......@@ -70,7 +70,7 @@ $favorskins = Skin::get_favorite_skins();
$siteskins = Skin::get_site_skins();
$defaultskin = Skin::get_default_skin();
if (!$USER->can_edit_view($view) || $view->get('owner') == "0") {
if (!$USER->can_edit_view($view)) {
throw new AccessDeniedException();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment