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.


3 comments:

  1. Hi
    I also did a same thing,

    I want to create block like "Course overview" and customise the content with some additional information.

    I duplicated "Course Overview" block. Rename and save as a new block and installed.

    My issue is how I can show this block in the center location (like "Course Overview" in the center)

    I installed the my new block properly but I can't see in the block list

    Please can you help me to slove this is.

    Thank you
    Ranil

    ReplyDelete
    Replies
    1. If you can't see it in the block list, then this is probably an installation error. Perhaps you forgot to rename something properly or edit the name in the lang file. Just give everything a good look once more, making sure it's all renamed.

      Delete
  2. I have two questions, the first one is, how do I customize my course cards? And how do I add a page that I have created in my local directory to my Moodle site?

    ReplyDelete