A tiny script: jumping to a course by shortname

A tiny script: jumping to a course by shortname

Toshihiro KITA-mit -
Antal besvarelser: 6
Plugin developers-ip assinga Translators-ip assinga
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...
Gennemsnitsbedømmelse:Useful (1)
I svar til Toshihiro KITA

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

Frank Ralf-mit -
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
I svar til Toshihiro KITA

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

Toshihiro KITA-mit -
Plugin developers-ip assinga Translators-ip assinga
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();
//-------------------------------------------
I svar til Toshihiro KITA

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

Tim Hunt-mit -
Core developers-ip assinga Documentation writers-ip assinga Particularly helpful Moodlers-ip assinga Peer reviewers-ip assinga Plugin developers-ip assinga

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

I svar til Tim Hunt

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

Toshihiro KITA-mit -
Plugin developers-ip assinga Translators-ip assinga
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.
I svar til Tim Hunt

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

Lucien Stals-mit -

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.