Moodle Course ID always equals to 1 - php

I am trying to get a Moodle Course ID in a php script shown below.
I have tested it in different courses and different accounts and it always gives me the ID of 1. Why is that and how to get the correct ID?
<?php
require('./config.php');
global $PAGE;
echo $PAGE->course->id;
?>

Moodle only knows which course your page is meant to be part of if you tell it (you could have arrived on your page by following a link from any course on the site). When you create a link to your page you need to include a parameter of some kind that will tell your page what course it should be considered part of, e.g. $url = new moodle_url('/blocks/myblock/mypage.php', ['id' => $PAGE->course->id]); echo html_writer($url, 'Look at my page');
Then, once you follow the link, gather the param using required_param() and use require_login() to set up the page. E.g
$courseid = required_param('id', PARAM_INT);
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST);
require_login($course);
After that, $PAGE->course will be configured.

Related

PHP PDO MySql query results not updated

hopefully I can explain this clearly enough.
On my profile page (let's call it profile.php), I am including a header file (header.php) that contains all the HTML header code and menu, as well as some queries to show stats to a user (how many surveys they have completed, and rewards earned). There are multiple queries on this page, but all work as expected, such as
header.php
<h3>
<?php
$whereCnt = array(
array('svy_end_status','=',1),
array('svy_end_mem_id','=',$member_id)
);
$svycnt = DB::getInstance()->get( 'COUNT(*) AS Count', 'survey_end', $whereCnt );
echo $svycnt->first()->Count;
?>
</h3>
Back in profile.php I have another query to get all the user profile information:
$wheremem = array(
array( 'member-uid', '=', $member_id ));
$completeprofileqry = DB::getInstance()->get('*','members',$wheremem);
NOTE: This is using a DB class based from Codecourse's Login/Register tutorial but is using PDO as the method.
Now, when I do a
if ($completeprofileqry->count()){
echo '<pre>';
print_r($completeprofileqry->first());
echo '</pre>';
I get as the output:
stdClass Object
(
[Count] => 2
)
This output directly correlates with the first query from the included header file, rather than the new query.
Is this due to the 'instance' still being the same?
I should also note, that on my index.php page, I am doing other queries, as well as using the same included header.php, but I am having NO issues with these query results.
Can anyone help narrow down where the problem might be?
EDIT: I am an idiot
It was meant to be 'member_uid' NOT 'member-uid' (underscore not hyphen !!!)

Laravel 5 user defined settings only needs to be called once

For my personal website (online administration) I have an appointments page, with certain settings, which is just for me, so I don't need it to be secure.
For example, I can change my view (to show all appointments or to sort them by label, or status). I can also exclude a status to make sure it's not being shown. Everything works, no problem there.
My issue is this. I have a simple field in my user database called "view". When I go to my appointments page, I check the value of my "view" field and if it is "status" for example, in my controller I set "$view = status", to return my "status" view. This works, with the following simple check:
$getUserView = \Auth::user()->view;
if($getUserView){
$view = $getUserView;
}
In my view itself I have a dropdown to change the view. Now, when I go to my view, it shows the "status view" just fine. But when I want to change the view to "default" or "label" using my dropdown, it should change the view to what I selected. So basically what I want to achieve is, when I go to my appointments page for the first time, it should show the view that I have set in my database, but only that one time. I could set it in a session maybe for that but I am just not sure how to accomplish this. Any pointers would be helpful!
Edit:
Still struggling with this, because I am using GET for everything, also the dropdown. Example, when I change the value in the dropdown, a javascript simply calls the URL again, but with the status that was selected in the dropdown. So, for example, my default URL is the following:
http://example.com/appointments/status/default
Now, I select "completed" in the dropdown, the following URL is called:
http://example.com/appointments/status/completed
In my appointments controller I put the following:
$status = session()->get("status", \Auth::user()->status);
In my routes I have the following:
Route::get('appointments/status/{status}', array('as' => 'appointments', 'uses' => 'Appointments\AppointmentsController#index'));
Maybe I should change "{status}" in the route to "$status" and use the put method to set the "$status"? Not sure what the best method would be.
When using the get method on the Session, the second argument is intended for a default value, which will be returned if the session key is not found.
You could do something like this:
$user = \Auth::user();
$view = session()->get("appointments_view", $user->view);
That will get the view set in the session, and if that is not set, it'll return $user->view. Now, when the users picks another view in the dropdown, just do:
session()->put("appointments_view", $dropDownValue);

Laravel ajax pagination set page

In my laravel project i use hashes in the url to keep track on selections of the user, so they are able to go back to a filter result. I dont want the page to refresh so I DONT UPDATE THE URL with Page=someid (except the hash) the response of the server looks like.
if ($request->ajax()) {
//The page I want to select posted with ajax
$paginaId = $request->input('page');
//some query like
$query = Table::Query();
//get 9 items
$rooms= $query->paginate(9);
//Return the response
return Response::json(View::make('front.rooms.render', array('rooms' => $rooms))->render());
}
The $paginaId represents the selected pagination page. Everything works fine except one part. I cant set the current page of the pagination. I viewed the documentation but couldn't find any solutions.
Try the following:
$rooms = $query->paginate(9, ['*'], 'page', $paginaId);
In this case you can set $paginaId manualy

Vtiger custom link with $current_user variable

I am trying to add a custom link to a custom page using the vtiger_link table, with 2 parameters; 1 is the trouble ticket id which works fine:
This is in the URL field in the database:
modules/Helpdesk%20Info/index.php?ticketid=$RECORD$"target='helpdeskinfo'
and 2 is the id of the logged in user ($current_user) but it doesn't work:
modules/Helpdesk%20Info/index.php?ticketid=$RECORD$&$user=$current_user"target='helpdeskinfo'
It shows the page just as it should, but the variable $user gets the value '$current_user':
http://localhost:8888/modules/Helpdesk%20Info/index.php?ticketid=135409&$user=$current_user
I wanted to post a picture of the table, but I don't have enough reputation for that.
I have tried many things, but I am clearly doing something wrong.
Any help would be much appreciated.
I did not need to put the variable in the URL,
the reason I could not use the $current_user variable is because it didn't exist in "detailview.php" which is where the link ends up.
I used:
echo "<pre>";
print_r($GLOBALS);
echo "</pre>";
to view all globals, there I found the authenticated_user_id
Knowing I could use this I was able to access the global from my file without the URL:
$current_user = $_SESSION["authenticated_user_id"];
I would like to thank STT LCU for his advice, as he was the only one who helped me through this problem, which took me a full week in total.

Php - Multiple Pages Same Url Different Ids?

I've been working on a project for which i want to create a page leading to different pages but i want to use same URL i have created all the pages but now i am stuck...
the Thing i want is.
i have pages **
index324.php , index34.php , index378.php , index345.php ,
index365.php , index334.php
What i want is to use a url -
index.php?product_id=21
index.php?product_id=23
Something Like Shown Above.
On your index.php, check if $_GET['product_id'] is set. If it is, use it to retrieve the desired ID which in turn you will use for checking against a database/file/whatever.
if (isset($_GET['product_id']))
$product_id = $_GET['product_id'];
Of course, you'll want to do some validation on the fetched ID to be sure it won't harm anything in your system since it can be altered by anyone issuing a GET request to your page.
Suppose that you have product detail pages for each product like: product1.php, product2.php etc ... You can include your desired file in index.php by passing GET parameter. e.g. (index.php?product_id=21) :
if(isset($_GET['product_id'])){
$product_id = $_GET['product_id'];
include('product' .$product_id. '.php');
}
else{
// product_id is not set ...
// do more stuff...
}

Categories