A tiny script: jumping to a course by shortname

A tiny script: jumping to a course by shortname

by Toshihiro KITA -
Number of replies: 6
Picture of Plugin developers Picture of 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)
In reply to Toshihiro KITA

Re: A tiny script: jumping to a course by shortname

by Frank Ralf -
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
In reply to Frank Ralf

Re: A tiny script: jumping to a course by shortname

by Toshihiro KITA -
Picture of Plugin developers Picture of Translators
Thank you for your nice comment.

BTW, the license of this script is public domain.
Feel free to use it for any purpose.
In reply to Toshihiro KITA

Re: A tiny script: jumping to a course by shortname

by Toshihiro KITA -
Picture of Plugin developers Picture of 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();
//-------------------------------------------
In reply to Toshihiro KITA

Re: A tiny script: jumping to a course by shortname

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

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

Average of ratings: Useful (2)
In reply to Tim Hunt

Re: A tiny script: jumping to a course by shortname (or some field in mdl_course table)

by Toshihiro KITA -
Picture of Plugin developers Picture of 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.
In reply to Tim Hunt

Re: A tiny script: jumping to a course by shortname

by Lucien Stals -

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.