Search This Blog

Thursday, December 13, 2012

Only show block to certain user

I wanted to hide my "Settings" block from all other users, except the admin role.

1)  I went into the file called:  mymoodleroot/blocks/settings/block_settings.php
2)  Inside two functions, I simply placed a role check at the beginning:

 function get_content() 
 {
    // only shows the settings block to admins
    global $USER, $CFG, $DB;
    if(!is_siteadmin($USER)) {return;}
        ...
}


function init() 
    {
    global $USER, $CFG, $DB;
    if(!is_siteadmin($USER)) {return;}

      ...
}



I'm sure that code could be modified to display to different roles and possibilities. This is just a simple example and it works!

Monday, July 23, 2012

Make a custom login page

Inside your theme folder, you need to change a couple files:

1) Inside the main theme folder, open the config.php file. Scroll down to where you see "login" and you can change that to read your own custom page. In my case, I made this change:
       From:
       ...
        ),
       'login' => array(
        'file' => 'general.php',
        'regions' => array(),
    ),
    ...

    To:
     ...
     ),
    'login' => array(
        'file' => 'login.php',
        'regions' => array(),
    ),
    ....

2) Next, I copied the general.php file, so I can use the same theme settings for my login.php page. However, I took out all the unnecessary sections: langmenu, custom menu, and side blocks. So basically I just show my logo, the main content, and the footer.

3) Lastly, I added some custom CSS to my core.php page to make the login elements look the way I wanted to. I centered all the elements on screen with a nice background like this:

Here is the CSS I used to make this:

/* ////////////////////////////////////////////////////  LOGIN PANEL //////////////////////////////////////////// */
.twocolumns div.loginpanel 
{
     border-right: 1px dashed #dadada !important; 
}
.onecolumn div.loginpanel
{
    position:relative;
    width:35%;
    margin: 0 auto;
    
    background:#ececec;
    background: -webkit-gradient(linear, 0 100, 0 bottom, from(#ececec), to(#bfbfbf)); 
    background: -moz-linear-gradient(#ececec, #bfbfbf);
    background: -ms-linear-gradient(#ececec, #bfbfbf);
    background: -o-linear-gradient(#ececec, #bfbfbf);
    background: linear-gradient(#ececec, #bfbfbf);
    -pie-background: linear-gradient(#ececec, #bfbfbf);
    
    border: 1px solid #a2a2a2;
    -webkit-border-radius:10px;
    -moz-border-radius:10px;
    border-radius:10px;
    behavior: url(PIE.htc);
}
div.loginpanel h2
{
    text-align:center;
    text-transform:none;
}

.loginbox .loginform
{
   width: 100%;
   height:20em;
   vertical-align:middle;
}
.loginbox .loginform .form-input
{
     vertical-align:middle;
     text-align:center;
     float:none;
     clear:both;
     width:100%;
     padding-bottom:1em;
}
.loginbox .loginform .form-label
{
    padding: 0em 0em 0.3em 2em;
    text-align:center;
}
.loginbox .loginform .form-input #username, 
.loginbox .loginform .form-input #password 
{
    position:relative;
    min-height:1.7em;
    width:60%;
    border-size:1px inset #d9d9d9;
    
    -webkit-border-radius:10px;
    -moz-border-radius:10px;
    border-radius:10px;
    behavior: url(PIE.htc);
}
.loginbox .loginform .form-input #loginbtn
{
    vertical-align:middle;
    display:block;
    padding:1em;
    width:10em;
    border:1px solid #bfbfbf;
    
    position:relative;
    -webkit-border-radius:5px;
    -moz-border-radius:5px;
    border-radius:5px;
    behavior: url(PIE.htc);
}
.loginbox .loginform .form-input #loginbtn:hover
{
    color:white;
    background:#5b2d7e;
}

div.forgetpass, 
div.centeredcookies 
{
   text-align:center;
}

/* ////////////////////////////////////////////////////  END OF LOGIN PANEL //////////////////////////////////////////// */


Adding search bar in top menu bar

Using the new Aardvark theme (2.2 blackbird version), I was able to insert a search bar in the top menu by making some modifications:

1) In styles/general.php, I added the header information I needed between the <head> tags
2) In layout/profileblock.php, I added my own code around line 62:

....

<?php
echo '</div>'; // end of headerwrap
?>
<!--/////////// This is the search bar  ////////////-->
<div id="mysearchbar"  >
<input type="text" class="autosuggest_search" />
</div>

</div>
<div class="profilebar" id="profilebar" style="display: none;">
...

Now my searchbar is inline with the custom menu and other elements. Yippee!

Wednesday, May 16, 2012

Easily finding an entry in your database activity

Here is a nice sql query to find an entry in your database activity, while searching for a specific text string:


$query = "SELECT * FROM {data_content} c JOIN {data_records} r ON r.id = c.recordid WHERE r.dataid=? AND c.content=?";
if($results = $DB->get_records_sql($query, array($data->id, 'my search string')))
{
    // do something here with results
}

It's a nice snippet!

Wednesday, April 25, 2012

Create an empty page for Moodle

I came across this code in the forum today and I thought it's a nice snippet to keep. This shows how to create a new, blank page within the moodle system.


<?php


// The number of lines in front of config file determine the // hierarchy of files.
require_once(dirname(dirname(__FILE__)).'/../config.php');


$PAGE->set_context(get_system_context());
$PAGE->set_pagelayout('admin');
$PAGE->set_title("Your title");
$PAGE->set_heading("Blank page");
$PAGE->set_url($CFG->wwwroot.'/blank_page.php');


echo $OUTPUT->header();


// Actual content goes here
echo "Hello World";


echo $OUTPUT->footer();
?>

Simple as that!

Adding custom message for users with "nologin" status

I have to share this because it's an awesome addition to the Moodle system. In my particular case, I want to suspend a particular user from entering the site, and also give them a specific reason why.


  1. Go to their profile, as admin, and set their authentication as "No Login." Make sure this option is also enabled in the admin panel -> plugins ->authentication.
  2. Now we need to modify some code. Thanks to the patch from " tiagojn", it's easy to create specific messages on the login panel. 
  3. Create a new file inside root/auth/nologin  called "config.html" This allows us to create some form elements on the settings panel of "nologin" from the admin -> plugins -> authentication section.
  4. Here is the code for config.html:
<?php
    // set to defaults if undefined
    if (!isset($config->enable_specific_message)) {
        $config->enable_specific_message = false;
    }

    if( !isset($config->specific_message_text)){
        $config->specific_message_text = get_string('invalidlogin');
    }
?>

<table cellspacing="0" cellpadding="5" border="0">
<tr>
    <td align="right">
        <label for=checkbox_enable_specific_message"><?php print_string('auth_nologin_specificmessage_select_key', 'auth_nologin'); ?></label>
    </td>
    <td><?php
        global $OUTPUT;
        echo html_writer::checkbox('enable_specific_message', 1, $config->enable_specific_message,'',array(id => checkbox_enable_specific_message));
        ?></td>
    <td><?php print_string('auth_nologin_specificmessage_description', 'auth_nologin'); ?></td>
</tr>
<tr>
    <td align="right">
        <label for="specific_message_text"><?php print_string('auth_nologin_specificmessage_text_key', 'auth_nologin'); ?></label>
    </td>
    <td>
        <input id="specific_message_text" name="specific_message_text" type="text" size="100" value="<?php echo $config->specific_message_text; ?>" />
    </td>
    <td><?php print_string('auth_nologin_specificmessage_text_description', 'auth_nologin'); ?></td>
</tr>
</table>
  1. Next we are going to modify an existing file called "auth.php"  - should be in the same folder that you are currently in.
  2. We are going to add two functions to this class. So right before the ending bracket, place this code:
 /**
     * Prints a form for configuring this authentication plugin.
     *
     * This function is called from admin/auth.php, and outputs a full page with
     * a form for configuring this plugin.
     */
    function config_form($config, $err, $user_fields) 
    {
        include "config.html";
    }

    /**
     * Processes and stores configuration data for this authentication plugin.
     */
    function process_config($config) {
        // set to defaults if undefined
        if (!isset($config->enable_specific_message)) 
        {
            $config->enable_specific_message = false;
        }
        if (!isset($config->specific_message_text)) 
        {
            $config->specific_message_text = get_string('invalidlogin');
        }

        // save settings
        set_config('enable_specific_message',   $config->enable_specific_message,   'auth/nologin');
        set_config('specific_message_text',     $config->specific_message_text,     'auth/nologin');

        return true;
    }

  1. Next we go inside the lang folder -> en -> and we'll open up "auth_nologin.php" to modify it. 
  2. We are going to add the following strings to the file:
$string['auth_nologin_specificmessage_select_key'] = 'Enable specific "invalid login" message';
$string['auth_nologin_specificmessage_description'] = 'Presents a specific message to users marked as "nologin" when they try to authenticate. <strong>Security Warning: The message is displayed even if the password is wrong, allowing attackers to find out existing usernames.</strong>';
$string['auth_nologin_specificmessage_text_key'] = 'Message to be presented';
$string['auth_nologin_specificmessage_text_description'] = 'Only considered when the above option is selected.';

  1. There is only one file left to modify. Go to root/login/index.php
  2. Around line 239, you should see the beginning of an "if" statment:  if (empty($errormsg)) {
  3. We are going to replace all content between the brackets of the "if" statement like this:
if (empty($errormsg)) 
{
            // check if user is set as 'nologin' and if a specific message is desired
            $user_auth = $DB->get_field('user', 'auth', array('username' => $frm->username), IGNORE_MISSING);
            $nologin_cfgs = get_config('auth/nologin');

            if (!empty($user_auth) && $user_auth == 'nologin' && !empty($nologin_cfgs->enable_specific_message)) {
                $errormsg = $nologin_cfgs->specific_message_text;
            } else {
                $errormsg = get_string("invalidlogin");
            }
            $errorcode = 3;
 }

Now all your specific messages will show up on the login panel! There is one warning to this patch though:
Security Warning: The message is displayed even if the password is wrong, allowing attackers to find out existing usernames.

Monday, April 16, 2012

Change the default tab of the Kaltura uploader

To change the default, opening tab of the Kaltura uploader, go to: local/kaltura/js/kaltura-edit.js and change this line from:

// Default opening is the "My Video" tab

$this.renderables.videotabs.selectChild($this.renderables.videotabs._items.length-1);


to

// This puts the default opening tab to "Upload" 

$this.renderables.videotabs.selectChild($this.renderables.videotabs._items[0]);