General plugins

A tiny script: jumping to a course by shortname

 
This discussion has been locked because a year has elapsed since the last post. Please start a new discussion topic.
Picture of Toshihiro KITA
A tiny script: jumping to a course by shortname
Group Particularly helpful MoodlersGroup Plugin developersGroup Translators
just FYI: This script enables you to use a URI including a course shortname like
http://moodle.foo.bar/sn2cs.php?id=CF101
to jump to the corresponding course like
http://moodle.foo.bar/course/view.php?id=3
(And it jumps to a parent course (meta course) if the course is invisible.)

// ---------- sn2cs.php ------------
<?php
$dirroot="./";  // this script should be in the Moodle top directory

require_once($dirroot."/config.php");

$coursenum= $_GET['id'];

if ($coursenum == ""){
  $uri= "http://moodle.foo.bar/";
}else{
  $course = get_record("course", "shortname", $coursenum);
  if ($course->visible == 0){  // if the course is invisible, jump to the parent course
    $course_meta = get_record("course_meta", "child_course", $course->id);
    $moodle_cid= $course_meta->parent_course;
  }else{
    $moodle_cid= $course->id;
  }
  $uri= "http://moodle.foo.bar/course/view.php?id=$moodle_cid";
}
header("HTTP/1.1 301 Moved Permanently");
header("Location: $uri");
exit();
?>
//-------------------------------------------

# I am not sure this is the appropriate place to post like this...
 
Average of ratings: Useful (1)
Frank Ralf
Re: A tiny script: jumping to a course by shortname
Group Documentation writersGroup Plugin developers
Hi Toshihiro,

This is a nice little script, especially if you want to manually insert links to specific courses into your material.

Reminds me a bit of Drupal's URL Alias feature (http://api.drupal.org/api/search/6/alias).

Cheers,
Frank
 
Average of ratings: -
Picture of Toshihiro KITA
Re: A tiny script: jumping to a course by shortname
Group Particularly helpful MoodlersGroup Plugin developersGroup Translators
Thank you for your nice comment.

BTW, the license of this script is public domain.
Feel free to use it for any purpose.
 
Average of ratings: -
Picture of Toshihiro KITA
Re: A tiny script: jumping to a course by shortname
Group Particularly helpful MoodlersGroup Plugin developersGroup Translators
For 2.x, the script should be modified like this:

// ---------- sn2cs.php ------------
<?php
$dirroot="./";  // this script should be in the Moodle top directory

require_once($dirroot."/config.php");

$coursenum= $_GET['id'];

if ($coursenum == ""){
  $uri= "http://moodle.foo.bar/";
}else{
  $course = $DB->get_record("course",array('shortname'=>$coursenum));
  if ($course->visible == 0){  // if the course is invisible, jump to the parent course
    $course_meta = $DB->get_record("enrol", array('enrol'=>'meta','customint1'=>$course->id));
    $moodle_cid= $course_meta->courseid;
  }else{
    $moodle_cid= $course->id;
  }
  $uri= "http://moodle.foo.bar/course/view.php?id=$moodle_cid";
}
// redirect
header("HTTP/1.1 301 Moved Permanently");
header("Location: $uri");
exit();
//-------------------------------------------
 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: A tiny script: jumping to a course by shortname
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

This is not necessary. Just use the URL .../course/view.php?name=shortname

 
Average of ratings: Useful (2)
Picture of Toshihiro KITA
Re: A tiny script: jumping to a course by shortname (or some field in mdl_course table)
Group Particularly helpful MoodlersGroup Plugin developersGroup Translators
Hi TIm,
Thank you for the information. i did not know that.

OK, so i emphasize
> (And it jumps to a parent course (meta course) if the course is invisible.)
is what the script can do.

That is useful for users who want to integrate courses into one parent meta course like our university (We use it as an alternative to cross-listed course mechanisms for IMS Enterprise feature).

And you can change the line
$course = $DB->get_record("course",array('shortname'=>$coursenum));
to use other fields than shortname in mdl_course table as a key to select a course.
 
Average of ratings: -
Picture of Lucien Stals
Re: A tiny script: jumping to a course by shortname
 

Is there a setting in Moodle (I'm using 2.6.10) that enables this by default sitewide?

Or do I just need to manually create these URLS?

I'm looking at my Google Analytics reports and all my traffic is showing up as "/course/view.php?id=152" and the like.

I'd prefer if it showed up as "/course/view.php?name=shortname" by default, or even "/course/shortname"

I could probably apply filters in Analytics to make things easier for me, but I was wondering if this can be addressed from the Moodle end as well?

Cheers,

Lucien.

PS, I know this is an old post, but after some Googling, I can't seem to find a solution to this.

 
Average of ratings: -