"theme switcher/gallery" block

"theme switcher/gallery" block

by An Re -
Number of replies: 14

Hi

Where can I find a block that allows you to change theme directly from the homepage?

Here is an example: http://theme.classroomrevolution.com/

Thanks in advance

Average of ratings: -
In reply to An Re

Re: "theme switcher/gallery" block

by David Jackson -
Hi An,

The block at http://theme.classroomrevolution.com/ is just an HTML block with a list of links that have ?theme=xxx appended to the ends of the URLs. This changes the theme (provided $CFG->allowthemechangeonurl is defined as true in /config.php) but the downside to this approach is that the theme is only changed for your current session.

I'm working on a block (my first one) to allow users to permanently change their preferred theme. Based on a copy of the login block (because it already had a simple form) and parts of user/edit.html, it gets a list of avaliable themes and displays them as a drop down menu.

I'm a bit stuck at where/how to send the form output though. I want the user to click 'save' and be returned to their current location showing the new theme.

I've tried posting the data to user/edit.php along with hidden fields to specify which profile to edit, but all I can return is a new user form with several fields highlighted as being incomplete.

I am new at this but learning fast. I've attached my work so far if any one can help or offer advice. What I really want to know is;
  • Is there a simpler way of updating a users preference than sending data to user/edit.php?
  • Where should the data be sent?
  • What hidden fields should be included?
  • How does one get a form to send its data off, have it processed, then return to the current location?
Cheers
In reply to David Jackson

Re: "theme switcher/gallery" block

by Ray Lawrence -
Hi David,

Just checking that you're aware of user themes. This allows users to choose their own theme (from those you allow) in their profile. User themes don't override course themes.
In reply to Ray Lawrence

Re: "theme switcher/gallery" block

by David Jackson -
Yes thanks, I am aware of user themes...our problem is that our users aren't.

Basically, I'm trying to create a block that emulates the theme selection drop down menu in a users profile so as to make this feature more accessible.

I've copied the code from the edit profile form to get the menu to appear, but I'm just not sure where I can point form action to, what hidden fields should be included and how to get the result to return to the page where the theme was selected.

I could maybe create a custom script to recieve the data, update the users profile and hard code it to return to the my moodle page (where we plan to place the block) but I'm hoping for a more elegant solution.

Cheers
In reply to David Jackson

Re: "theme switcher/gallery" block

by David Jackson -
I've created a custom script to handle the data sent by the form and return the user to their original location. Next step, saving the selected theme to the database instead of just setting a session theme via url.

Here's the code for the script so far:

<?php
/*///////////////////////////////////////////////////////////
NAME:savetheme
PURPOSE: This script recieves data from the theme_switcher block,
saves the selected theme to the users profile,
and returns the user to their previous location
AUTHOR: David Jackson
CONTACT: davidj@stcuthberts.school.nz
///////////////////////////////////////////////////////////*/

//INCOMING POST VARIABLES: theme, id, location

//include relevant libraries
require_once("../../config.php");
include($CFG->wwwroot."/lib/datalib.php");
include($CFG->wwwroot."/lib/weblib.php");
include($CFG->wwwroot."/lib/moodlelib.php");

//localise post variables
if($_POST['theme']){$theme = $_POST['theme'];} else {$theme = "default";}
if($_POST['id']){$id = $_POST['id'];} else {echo "No user specified";}
if($_POST['location']){$location = $_POST['location'];} else {$location = $CFG->wwwroot.'/';}

//update user profile
//UPDATE mdl_user
//SET preferredtheme = $theme
//WHERE uid = $id
//query($query)
//update_record("user", $user);

//display the input (testing only)
echo "<b>Output:</b><br />";
echo $theme.'<br />';
echo $id.'<br />';
echo $location.'<br />';
echo '<a href="'.$location.'">Click here to return</a>';

//return to original location (testing - and set session theme)
redirect($location.'?theme='.$theme);

?>
In reply to David Jackson

Re: "theme switcher/gallery" block

by David Jackson -
Getting somewhere. The following code now updates the database and returns to the page of origin BUT, the theme does not change untill you log out and back in. This leads me to believe moodle checks a session variable for preferred theme rather than querying the users profile every time they load a new page. The theme needs to be updated in the SESSION variable?

Am I right and if so how is this done?

Here's the code:

<?php
/*///////////////////////////////////////////////////////////
NAME:savetheme
PURPOSE: This script recieves data from the theme_switcher block,
saves the selected theme to the users profile,
and returns the user to their previous location
AUTHOR: David Jackson
CONTACT: davidj@stcuthberts.school.nz
///////////////////////////////////////////////////////////*/

//INCOMING POST VARIABLES: theme, location

//include relevant libraries
require_once("../../config.php");
//these libs don't seem to be needed
//include($CFG->wwwroot."/lib/datalib.php");
//include($CFG->wwwroot."/lib/weblib.php");
//include($CFG->wwwroot."/lib/moodlelib.php");
//include($CFG->wwwroot."/lib/dmlib.php");

//define variables
if($_POST['theme']){$theme=$_POST['theme'];}else{$theme="intranet";}
if($_POST['location']){$location=$_POST['location'];}else{$location=$CFG->wwwroot.'/';}
$id = $USER->id;

//update user profile
if(has_capability('moodle/user:editprofile',$USER)){
$sql="UPDATE mdl_user SET theme = '$theme' WHERE id = '$USER->id'";
$db->Execute($sql);
//any of the following functions also work ($db->Execute is most portable)
//pg_query($sql);
//execute_sql($sql);

//yay! it works but the theme does not get updated untill the user logs out and back in. Do we need to update the session/cookie or something?

/*
//output for testing
echo '<h2>Updating profile</h2>';
echo 'Running query: '.$sql.'<br />';
echo 'to update profile of '.$USER->username.' to use '.$theme;
echo ' and returning to <a href="'.$CFG->wwwroot.$location.'">'.$CFG->wwwroot.$location.'</a><br />';
echo '<a href="'.$location.'">Click here to return</a>';
*/
}else{error("You are not allowed to do that");}

//return to original location
redirect($location);

?>
In reply to David Jackson

Re: "theme switcher/gallery" block

by David Jackson -
Ok, never mind. I've cracked it myself big grin

<?php
/*///////////////////////////////////////////////////////////
NAME:savetheme
PURPOSE: This script recieves data from the theme_switcher block,
saves the selected theme to the users profile,
and returns the user to their previous location
AUTHOR: David Jackson
CONTACT: davidj@stcuthberts.school.nz
///////////////////////////////////////////////////////////*/

//INCOMING POST VARIABLES: theme, location

//include relevant libraries
require_once("../../config.php");
//these libs don't seem to be needed
//include($CFG->wwwroot."/lib/datalib.php");
//include($CFG->wwwroot."/lib/weblib.php");
//include($CFG->wwwroot."/lib/moodlelib.php");
//include($CFG->wwwroot."/lib/dmlib.php");

//define variables
if($_POST['theme']){$theme=$_POST['theme'];}else{$theme=$CFG->theme;}
if($_POST['location']){$location=$_POST['location'];}else{$location=$CFG->wwwroot.'/';}
$id = $USER->id;

//update user profile
if(has_capability('moodle/user:editprofile',$USER)){
$sql="UPDATE mdl_user SET theme = '$theme' WHERE id = '$USER->id'";
$db->Execute($sql);
//any of the following functions also work ($db->Execute is most portable)
//pg_query($sql);
//execute_sql($sql);

//output for testing
/*
echo '<h2>Updating profile</h2>';
echo 'Running query: '.$sql.'<br />';
echo 'to update profile of '.$USER->username.' to use '.$theme;
echo ' and returning to <a href="'.$CFG->wwwroot.$location.'">'.$CFG->wwwroot.$location.'</a><br /><br />';
*/

//update $SESSION with the preferred theme
$USER->theme=$theme;
$_SESSION['USER']=$USER;

//check contents of session
/*
foreach($_SESSION as $itemname => $itemvalue){
echo "<b>$itemname:</b><br /><br />";
foreach($itemvalue as $name => $value){
echo $name.': '.$value.'<br />';
}
echo "<br />";
}
echo '<a href="'.$location.'">Click here to return</a>';
*/

}else{error("You are not allowed to do that");}

//return to original location
redirect($location);

?>

Will post completed block once tested.
In reply to David Jackson

Re: "theme switcher/gallery" block

by Jamie Robe -
Hi David,
Your example has been helpful to me, as I work on another type of issue that requires database updating. I especially used the trick you have to update the session variable, avoiding the problem of those not updating until the next login.

However, I have a question:
Did you consider using this method below? The reason I am asking is that I am trying to find the proper and effective way to change field values.


$user = new object();
$user->id = $USER->id;
$user->theme = 'whatever'; // the name of it
if (!update_record('user', $user)) {
// error updating
}


I have not tested the above on theme, but it does work on firstname and similar fields. I am having a problem getting it to update fields that I have added using Administration - Users Accounts - User profile fields.
Jamie

In reply to An Re

Re: "theme switcher/gallery" block

by David Jackson -
I have created a block (my first ever, so be nice, haha) that lets you choose your preferred theme. Only allowed themes are displayed and the change is persistent between logins.

Just unzip it and copy the folder to http://yourmoodle/blocks
Visit /admin/blocks to confirm it has been recognised.

Any bugs, hints or advice gratefully recieved cool
In reply to David Jackson

Re: "theme switcher/gallery" block

by Ali Hastie -

Hi David

Cool block, which we were looking for to change our contrast themes.  How do we make the block available to guests? We would like this on our homepage (constantly) without a user logging into Moodle.

Thanks,

Ali.  

In reply to Ali Hastie

Re: "theme switcher/gallery" block

by David Jackson -

Hi Ali,

Line 32 of block_theme_switcher.php...

if ($USER->loggedin or !isguest()) {   // Show the block

...limits the visibility of the block to logged in users/users who are not guests. But I don't know if the guest account has the ability to set a preferred theme, and if it does, it will probably affect all guests simultaneously.

The other method you could try is to use session themes. Instead of the form which saves data to the database, session themes work via the URL eg.

http://www.yoursite/index.php?theme=cooltheme

The block could be modified something like...


$themes = get_list_of_themes();

...

foreach($themes as $theme){

    $this->content->text .= "<a href='http://yoursite/index.php?theme=$theme'>$theme</a><br />";

}


Of course, you dont really need a whole block to do that. Some hard coded links in your banner would suffice.

In reply to David Jackson

Re: "theme switcher/gallery" block

by Leang Chumsoben -
Hi David,

I already copied the folder but I can't see it at /admin/blocks.

What did I do wrong?

Regards,
Soben