Sunday, March 17, 2013

Redirect user to course page upon login

This is a feature I've been wanting for a while, especially for teachers. I would like that teachers are re-directed to their course page immediately after login. I've created some code inside the "login/index.php" file to accomplish this feature.

NOTE:  This code will only redirect the teacher to the FIRST course they are enrolled in. For me, this is okay because each teacher is only enrolled in one course anyway. If you want the user to be redirected to a specific course (out of many), you will need to modify this code and search for the course by name or id.

1)  Go to login/index.php on your moodle server.

2)  Around line 222, you will see some code that determines if the user will be redirected to the site home or the my moodle page. (In a previous post, I set up my system that teachers were sent to the my moodle page, and students were sent to the site home)

3)  Now we will add some code to get the user's list of courses and redirect the page to a specific course page.


// Go to my-moodle page instead of site homepage if defaulthomepage set to homepage_my
if ($home_page == HOMEPAGE_MY && !is_siteadmin() && !isguestuser()) {
         if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') 
                {
                     $urltogo = $CFG->wwwroot.'/my/';

                    // I set up the page to redirect teachers to the 'my' page. 
                    // So here, instead of looking for user's role, I simply see if they
                    // are directed to the mymoodle page. Then I know it's a teacher and 
                   // I redirect to their course.
   if ($courses = enrol_get_my_courses(NULL, 'visible DESC, fullname ASC')) 
   {
           foreach($courses as $course)
                          {
                                $urltogo = $CFG->wwwroot.'/course/view.php?id='.$course->id;
                                break;
                          }
                    }
          }
 } 


4)  If you wanted to search for a specific course, then add logic inside of the foreach loop to look at the $course->id, $course->name, etc.

Sunday, March 10, 2013

Create new block for "My Courses" including activities

I want to make a custom block that will show the courses I'm enrolled in and the activities included within.
I don't want to show anything extra: no reports, participants, or grades.
So the structure I'm looking for will be similar to this:

Block Name:  My Courses

  • Course 1
    • Topic 1
    • Topic 2
      • Activity 1
      • Activity 2
      • Activity 3
    • Topic 3
  • Course 2
I'm going to duplicated the "Courses" block and modify it to create a new, custom block for myself. Then I'm going to use the "My courses" branch within the Navigation block as inspiration to make the list of content that I want to have.

Duplicate the "courses_list" block

1)  You will need access to your moodle server. Go inside /blocks and find the folder called "course_list." Copy the folder and rename it to....whatever name you want to give the new block.
For example, I will name my folder "course_outline."

2)  Now we'll need to rename some files:
-- block_course_outline.php
-- lang/en/block_course_outline.php

3)  Modify data in those files:
-- db/access.php:  rename all instances of "block_course_list"  with "block_course_outline"
-- lang/en/block_course_outline.php:  Give the pluginname = your name. ("Course Outline")
-- In all the remaining files, rename all instances of "block_course_list"  with  "block_course_outline"

4)  Go to your moodle site and login as admin. Click on "Site Administration -> Notifications."  You should now see your new block ready to be installed. 
Choose "upgrade moodle database" and install the block. 

Now we customize our new block

1)  The next thing we need to do is populate the course list to show activities.
2)  Go into the blocks/course_outline/block_course_outline.php file. Around line 35, you will see code like this:
foreach ($courses as $course) 
{
       $coursecontext = context_course::instance($course->id);
       $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
       $this->content->items[]="<a $linkcss title=\"" . format_string($course->shortname, true, array('context' => $coursecontext)) . "\" "."href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".$icon.format_string($course->fullname). "</a>";
}

Right now, that code is only printing out the course name. So we need to insert some new code that also prints out the activities of that course. Here is the new code we will add:

foreach ($courses as $course) 
{
       $coursecontext = context_course::instance($course->id);
       $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
       $this->content->items[]="<a $linkcss title=\"" . format_string($course->shortname, true, array('context' => $coursecontext)) . "\" "."href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".$icon.format_string($course->fullname). "</a>";

         $myactivities = get_array_of_activities($course->id);
         if(!empty($myactivities))
         {     
                 $activityList = "<ul>";
                  foreach($myactivities as $activity)
                  {
                        $activityList .= '<li><a $linkcss 
                         href="'.$CFG->wwwroot.'/mod/'.$activity->mod.'/view.php?id='.$activity->cm.'" 
                         title="'.$activity->name.'" >'.$activity->name.'</a></li>';
                   }     
                   $activityList .= "</ul>";
                   $this->content->items[]=$activityList; 
          }
}

3)  You can see that we get the activities within the course and save it in an array variable called "$myactivities."  Then we are going to make a list of the activities and add that to the content of the block.


PATCH: Fix default homepage for users when using "force login"

At the moment, a certain feature is broken in the stable release of Moodle 2.4.1.
If you enable the "force login" setting on the site, users will always be directed to the site home.
This is bad if you have also allowed users to choose their own homepage -- even if they have chosen the "my moodle" page as a default homepage, they will never be redirected there upon login.

Just as a recap for those settings:

  •  "site administration -> appearance -> navigation". Set the default homepage to:  user preference.
  • "site administration -> security -> site policies. Enable the setting called: Force users to login

I have followed the trackers on moodle.org for a solution and a patch has recently been uploaded. AND it works!
https://tracker.moodle.org/browse/MDL-37983

Here are the steps to implement the patch:
1)  Go to /login/index.php file.
2)  Around line 209, we are going to add some code.
This is the original code:


            // no wantsurl stored or external - go to homepage
            $urltogo = $CFG->wwwroot.'/';
            unset($SESSION->wantsurl);

            $home_page = get_home_page();

            // Go to my-moodle page instead of site homepage if defaulthomepage set to homepage_my
            if ($home_page == HOMEPAGE_MY && !is_siteadmin() && !isguestuser()) {
                if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') 
                {
                    $urltogo = $CFG->wwwroot.'/my/';
                }
       } 
}

AND here is the new code. I will highlight all the new parts of code you must add.


           // no wantsurl stored or external - go to homepage
            $urltogo = $CFG->wwwroot.'/';
            unset($SESSION->wantsurl);
        }

        // If the url to go to is the same as the site page, check for default homepage.
        if ($urltogo == ($CFG->wwwroot . '/')) 
        {
            $home_page = get_home_page();

            // Go to my-moodle page instead of site homepage if defaulthomepage set to homepage_my
            if ($home_page == HOMEPAGE_MY && !is_siteadmin() && !isguestuser()) {
                if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') 
                {
                    $urltogo = $CFG->wwwroot.'/my/';
                }
       } 
}

3) Now the site will save and display the user's preferred homepage. Yay!

Thursday, March 7, 2013

Create RSS Feed block for database activity

I really liked how the Glossary activity had a menu block on moodle, but the database activity did not. By using the RSS feeds block, you actually can achieve the same effect.

Enable RSS on your moodle site
1)  Enable RSS settings within the administration. Go to:  Site Administration -> Advanced Features
2)  Scroll down and enable the option called "Enable RSS feeds"
3)  Now we need to enable RSS for the database activity itself. Go to:  Site Administration -> Plugins -> Activity Modules-> Database.
4)  Enable RSS Feeds and click the "Save" button

Generate the RSS file for your database activity
1)  Open up your database activity so you can see the database settings. Choose "Edit Settings"
2)  Now you see a new option called "RSS Articles" --here you can choose the amount of entries that should be stored in the RSS feed. I think 5 is a pretty good number for myself. 
Make sure you save the new setting.

3)  We need a link to the newly generated RSS file. Look at the database settings again:
At the bottom of the list, a new option appears called "RSS feed for this activity." 
 -- Right-click on the link and say "Copy link address."
 -- You can also click the link and it will open in a new window. Highlight and copy the url inside the browser window.

Create the RSS Feed Block
Now we simply need to create a new RSS block and put it on whichever page we like. For myself, I'm going to put the RSS Feed block on the database activity page.
1)  Go to your desired page and turn editing on.
2)  If block editing is enabled on your site, you should see a new block appear called "Add a Block." From the drop-down list, choose "RSS Feed Block"
3)  Once the new rss block appears, click on it's pencil icon to edit the settings. 
Here is where you will paste the url of the database activity rss feed.
4)  If the url was correct, then your RSS block should now be configured. You can choose additional settings to change the name of the block, etc.
NOTE: It is possible that you'll need to add an entry to your database activity and run your cron job before the RSS block will display entries properly.

Move blocks to the left by default

I've had a couple issues with blocks appearing at different sides of the page, depending on the page I'm at. There are a couple options that will set default actions for blocks.


Choose the region that blocks appear on your course page:

1) Navigate to your current theme and open up the config.php file.
theme/mythemename/config.php

2)  Scroll down until you see a line of code that describes "in-course"

'incourse' => array(
'file' => 'general.php',
'regions' => array('side-pre', 'side-post'),
'defaultregion' => 'side-pre',
    ),
This code sets the regions for the blocks within a course page. This applies to pages used by any activities as well.
Side-pre:  left side of the page
Side-post:  right side of the page

Setting the "regions" to include both, means that when moving a block, you can choose either the right or left side.
Setting the "defaultregion"  means all blocks appear on the left side by default.



Decide how many default blocks appear on a course page:
This setting decides which default blocks appear automatically on a course page when it's created for the first time. Some people like myself, don't need 5 blocks spawning each time.

1)  Go to your moodle server and located the course format you are currently using. In my case, I'm using the "Collapsed Topics" format for my courses.
course/format/topcoll

2)  Open the config.php file.
course/format/topcoll/config.php

3)  You should see a line of code that looks like this:
$format['defaultblocks'] = ':...';
Here you can delete or add the blocks you want to automatically appear on all course pages.

Wednesday, March 6, 2013

Include javascript file in your theme

Including javascript inside my moodle theme gave me soooo much headache.
Now I've finally figured out how to do it, and this post will be my proof!

1)  Go inside your "/theme/mythemename"   folder. If you don't have one already, create a new folder called "javascript."


2)  Inside the new javascript folder, upload or create your actual javascript file. We could call it "text.js" for example purposes. 

3) Next, go inside your  "theme/mythemename/config.php."  At the very bottom of the file, we will add a new line of code to include your javascript file. 
$THEME->javascripts_footer = array('test');  // Recommended:  This loads the file in the html footer

OR you could load the file in the html header:
$THEME->javascripts = array('test');

4)  In order to see the changes, the caches of the site must be purged. What I normally do:
-- Turn on "Theme designer mode" while I'm working, to automatically prevent caching
-- Go to "Admin -> Development -> Purge all caches" (if not in designer mode)
-- Refresh the page you want to see

Just a word of warning:  jQuery does funky things to Moodle and half of the time, including it will give you more headache than not. Try to use the YUI libraries and find equivalent functions instead of jQuery.

Here is the offical documentation from Moodle about jQuery and including javascript files: