Moodle Links/Browser PHP Syntax

Moodle Links/Browser PHP Syntax

by Troy May -
Number of replies: 5

Hi,

I am trying to create a custom html link that will take my users directly to their "Edit Profile" page. So far, I have this:


"/moodle/user/edit.php?id='.$user->id.'&course='.$course1->id.'"


Can anyone tell me what is incorrect about this syntax? It bounces back this:

"You are trying to use an invalid course ID"

Obviously, the syntax needed to determine the correct course is wrong. But I do not know what syntax to use to add additional parameters.

I also could use the correct syntax for the Logs page:

"moodle/report/log/user.php?id='.$user->id.'&course='.$course1->id.'"


PS:

I got the idea from this link, which opens my users' main profile page. I created a custom nav link at the top pf my LMS:

"http://digitalliteracyproject.com/lms/user/view.php?id='.$user->id.'"

This link was originally posted by Grant Swaim in 2008 after claiming he had tweaked the following PHP script(by John Isner) to work as an html link:

global $CFG,$USER;
$url = $CFG->wwwroot . '/user/edit.php?id=' . $USER->id . '&course=1';

What is the language Grant used to create his link to a user's profile page?

And second, what is the syntax needed to create a link to go to their Edit Profile" page?

Average of ratings: -
In reply to Troy May

Re: Moodle Links/Browser PHP Syntax

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

For specifying a course argument to the script, there's nothing wrong with the syntax. The error is telling you that the value you specified is not a valid course. So whatever is in the variable "$course1" in the script you are using does not have an "id" property that corresponds to a valid course id on your site.

Specifying the URL in the way you are will have limited use though. You should use "$CFG->wwwroot" to define the first part of the URL.

And, you don't really need to have a "course" parameter passed into the user editing script. Its optional, and will default to the system id.

mike

In reply to Troy May

Re: Moodle Links/Browser PHP Syntax

by Simey Lameze -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Please note that you should use moodle_url class to generate url's instead of manually create your url. For example:
$url = new moodle_url('/user/edit.php', array('id' => $USER->id, 'course' => $courseid));

And then, you print using out():
echo $url->out();
In reply to Troy May

Re: Moodle Links/Browser PHP Syntax

by Troy May -

Well, I solved one link.smile

I simply removed the syntax and am left with this:

"/moodle/user/edit.php"

As a button on my navbar, this link takes any user directly to their Edit profile page. Ye ha!!smile


But this does not solve my greater issues. I still really need to know how to write additional parameters to a href link like this one uses:

/moodle/user/view.php?id='.$user->id.'


This syntax  ?id='.$user->id.'  when placed at the end says to Moodle, "use the id of the person trying to process this href link & show data that is pertinent to them only". When you run in a browser, the link automatically changes in the address bar to the actual user's id like so:

/moodle/user/view.php?id=110


My problem is I do not know how to write a second parameter, such as the course id.

I really need a "href" link, meaning a link which can be added as html code, NOT PHP, into my header for use in my navbar so my students can access their own activity logs.

This is an example of the link after it is processed in a browser for Student 110:

/moodle/report/log/user.php?id=110&course=1&mode=all

You see what I mean?

How can I rewrite this link using the syntax so it works on any user who clicks on it?



In reply to Troy May

Re: Moodle Links/Browser PHP Syntax

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

The language you asked about in your original post (in the tiny writing) is just php, although your custom link in that original post appears to mix html and php together, so wont work.

So your first query should be something along the lines of (and following Simey's earlier advice about using the moodle_url function)

<php
global $USER;
$url = new moodle_url('/user/edit.php', array('id' => $USER->id));
echo "<a href =". $url .">";
?>

and for the second one will be something like

<?php
global $USER, $COURSE;
$url = new moodle_url('/report/log/user.php', array('id' => $USER->id, 'course' => $COURSE->id, 'mode' => 'all'));
echo "<a href =". $url .">";
?>

 

In reply to Richard Oelmann

Re: Moodle Links/Browser PHP Syntax

by Troy May -
I appreciate all your help guys! Thanks for taking the time to provide some options.cool You are the best!