After creating my Moodle newmodule, I want to be able to select from the list of current users in the system and assign them some other data. It looks like the following:
Add user manually
User [CHOOSE]
Car hire station [text field]
Car type [textfield]
Price per day [list of prices available]
I builded the database, added the textfields, the forms for them and the button for choosing the users, but how to build the functionality for getting the users's list and add the chosen user?
I did not find such example in Moodle in order use it.
If you anticipate a lot of users in the system then extend the user_selector_base class
https://docs.moodle.org/dev/Ajax_user_selector
From the information given, I think what you're looking for is a way to filter against a user ID to save your custom information against that user.
There is a widget that you might find useful called single_select, which creates a drop-down menu that sends POST data when the selected value is changed.
For example:
global $DB, $OUTPUT;
$posturl = new moodle_url('/local/newmodule');
$options = $DB->get_records_menu('user', array(), '', "id, CONCAT(firstname, ' ', lastname)");
$singleselect = new single_select($posturl, 'userid', $options);
echo $OUTPUT->render($singleselect);
That will output the drop down box and send 'userid' to the page when it reloads. You can then grab that user ID:
$userid = optional_param('userid', 0, PARAM_INT);
And use that when you retrieve the data from your form to save the data.
if ($data = $myform->get_data()) {
$data->userid = $userid;
$DB->update('mytable', $data);
}
Related
Here is what I am trying to acheive on my wordpress site.
I would like to compare 2 data sources from 1 database.
For example when a random wordpress user logs in, I would like PHP to get his user ID and then check if that ID matches with other table's ID and then if they match display content in frontend according to matched ID.
Table 1 is wordpress wp_users and table 2 is a table which has its own id and contains data. Data which table 2 contains is related to wordpress users. Each wordpress has its own data.
I need to display this data in frontend of the website after matching validation is done.
I managed to display data in front end but stuck in comparing two tables.
Here is what I got:
$user = get_userdata($user_id);
$h5p = $wpdb->get_var("SELECT id FROM wp_h5p_results ");
if ($user == $h5p) {
$total_score = $wpdb->get_var("SELECT score FROM wp_h5p_results ");
echo "<p>Your Score {$total_score}</p>";
} else {
echo 'No user with such ID';
}
I have a php website. The first page contains a list of products and I'm currently passing the ID (picked up from mysql database) for the product within the URL to the items page i.e. localhost/item.php?4
I don't want to show any parameters in the URL so have investigated another option which is using a session.
The issue with this is that the link to each of my items is in a while loop retrieving ID and product name from the database so I'm having issues making the session mirror the ID when an item/link has been clicked.
Here's a snippet of my code (I've removed the session code):
$stmt = $con->prepare("SELECT pid, product_name FROM persons where deleted = ? order by order_age desc");
$stmt->bind_param("i", $del);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr><td>';
$phn = $row["pid"];
echo "<span><a class='storage' href='item.php'>" . $rows["product_name"] . "</a></span>";
}
echo "</td></tr>";
}
I guess I have two questions:
Is it possible to achieve what I need to do
What is the correct way of achieving this
Thanks in advance,
Pete
Options, briefly
You could first load /item.php?id=4 then redirect to /item-hidden.php & use $_SERVER['HTTP_REFERER'] & parse_url & process the GET portion of the referrer url.
You could also use session for this. Set the session variables when the page loads to the long-url, then redirect to the short url, load the session & clear the session.
If you just want to shorten the url, then you could use uniqid() And put the unique id in the url & save the paramaters to a session variable with that unique id.
You could use a pre-made url shortener.
You could roll your own url shortener using a reference file that holds an array or a database.
There are surely other creative solutions that I haven't thought of
My thoughts:
Hiding the url altogether will make for a poor user experience - inability to bookmark, using the back-button will be funky, hard to share an item on social media or a blog
Shortening the url is nice but not necessary
Depending on the options you're working with, you might be able to create shorthands that are more friendly to look at in the url bar or db-references for sets of options that are extremely common
What you're trying to do seems like a great learning project - learn about sessions, http_referer, databasing & whatnot. I think by doing what you're wanting, you'll learn that you don't really like how it feels - or you might come up with a clever way to make your URLs prettier & make the UX really nice.
I have a database which when inserted into should create a notification in the webpage. What I did here was polling through ajax. I execute a query which selects for a row whose timestamp is less than 3 seconds from current time.
Here is my code-
HTML file:
<html>
<head>
<title>Dashboard</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3 /jquery.min.js"></script>
</head>
<body>
<div id="responsecontainer"></div>
<script type="text/javascript">
$( document ).ready(function() {
setInterval(ajaxcall, 500);
function ajaxcall() {
$.ajax({
type: "GET",
url: "maintdashsubmit.php",
dataType: "html", //expect html to be returned
success: function(response){
$("#responsecontainer").append(response);
}
});}
});
</script>
</body>
</html>
PHP file:
<?php
session_start();
$link = mysqli_connect("localhost", "root", "*****", "DMRC");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM Ticket, Station, user_auth WHERE TIME(ticket_open_datetime) BETWEEN curtime()-3 AND curtime() AND Ticket.ticket_station_id = Station.station_id AND Ticket.ticket_open_emp_id = user_auth.emp_id AND Ticket.ticket_close_open='open" ;
$result = $link->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
echo "<tr><td style='width:15%''>".$row['ticket_id']."</td><td><table><tr><td>".$row['ticket_equipment']."</td></tr><tr><td>".$row['ticket_equipment_id']."</td></tr></table></td><td>".$row['station_name']."</td><td><table><tr><td>".$row['emp_firstname']." ".$row['emp_lastname']."</td></tr><tr><td> Sec:".$row['emp_section']."</td></tr><tr><td>".$row['emp_id']."</td></tr></table></td><td>".$row['ticket_close_open']."</td></tr>";
}
}
$link->close();
?>
This gives me the most recent notification that I want to see. But it displays multiple times as long as it remains within the 0.5 seconds time limit.
I read that I can keep a column in the database that can be set 1 if the notification has been displayed and 0 if not.
But I have a doubt. Suppose this notification has to be received by many users. What if displaying a notification to one user sets the column entry 1 before it was viewed by another? This is all so confusing. If anyone can suggest a way?
What if displaying a notification to one user sets the column entry 1 before it was viewed by another?
It sounds like what you're looking for is called a "many to many" relationship. Suppose you have tables for Users and Notifications:
Users
----------
ID
Username
etc.
Notifications
----------
ID
MessageBody
etc.
If each notification can be seen by many users, and each user can see many notifications, and you want to track some piece of information about that interaction (whether a user has seen a notification), then you want a linking table between them. Something like this:
UserNotifications
----------
UserID
NotificationID
HasBeenSeen
etc.
(You can give this table its own ID, or you can use the combination of UserID and NotificationID as a compound primary key. Either way is valid, it's up to you.)
With a many-to-many relationship you need to have such a "linking table" between the two entities which are related. In that table you'd track information about the relationship itself, which in this case would include whether the user has seen the notification.
If you expect this table to grow significantly over time, there are a variety of things you can do to modify the approach slightly. For example, do you need to permanently retain the history of these things? Maybe delete records after they've been viewed? Do notifications themselves get deleted? If so, you would also delete these linking records along with them. Or perhaps the table doesn't store whether a user has seen a notification, but instead stores the notifications a user still needs to see. Then as it's seen, the record is deleted.
There are a variety of approaches you can take to solve the overall problem. But to address the specific question asked here, what you need to track the information is a many-to-many linking table.
I'm building a Joomla module that can get an array of all articles on the site -- regardless of category -- then identify the category, retrieve intro text and title, and display the titles, categories, and intro text of the most recent two articles in each category in a tiled layout. I have the layout done, but I don't know where to start on the rest. Is it possible?
I'm not averse to getting the articles from category blogs, but I'm not sure if that's possible.
There is the best way to retrieve the articles:
$jcontent=JControllerLegacy::getInstance('Content');
$jarticles=$jcontent->getModel('Articles');
$jarticles->getState();
$jarticles->setState('filter.article_id', $ids);
$jarticles->setState('list.limit', count($ids));
$jarticles->setState('filter.published', 1);
$articles=$jarticles->getItems();
This code was tested and, as for me, this is the best way - it uses Joomla abstractions to retrieve the articles, it uses Joomla caching and is not dependent on database structure.
This is code is not tested, you may need to check and make minor modifications.
Method 1: In this you need to query the category details again from the category id you get inside the loop.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__content');
$db->setQuery((string)$query);
$res = $db->loadObjectList();
foreach($res as $r){
//query category details also here
echo '<h3>'.$r->title.'</h3>';
echo $r->introtext;
}
Method 2: In this method you are supposed to get both content and category details in one query. In the select query you need to include the field names which you need.
$db = JFactory::getDbo();
$db->setQuery('SELECT #__content.title as contentTitle, #__categories.title as catTitle FROM #__content, #__categories WHERE #__content.catid = #__categories.id');
$details = $db->loadObjectList();
print_r(details);
I have a table with the following fields: wall_posts, group_id, id, and user_id.
Wall_posts is user entries, group_id is imported from another table and is just a unique group id, id is just a simple counter, and user_id gets the user id from the session.
My code gets rid of all the wall_posts if you press the delete button by comparing the user id to the user in session. I'm trying to find a way to delete individual posts and not all the posts by the user.
Here is the code:
if (isset($_POST['delete'])) {
$current_user = $_SESSION['user_id'];
$result = mysql_query("SELECT * FROM group_posts");
while ($user_id = mysql_fetch_array($result)) {
$id = $user_id['user_id'];
}
if ($current_user == $id) {
mysql_query("DELETE FROM group_posts WHERE $current_user = $id") or die(mysql_error());
}
}
How can I bound the delete button to individual posts instead of deleting all the posts made by the user currently in session?
Your SQL query above doesn't make sense - the WHERE statement should be in the form WHERE column_name = value.
Assuming id is the primary key for group_posts, as you're displaying posts, create a link for each post created by the author, e.g. Delete This Post for post with id 3. Then you'd do a query like this:
DELETE FROM group_posts WHERE id = postIdValueHere
Using the code pattern you have above:
if (isset($_POST['delete']) && $_POST['delete'] > 0) {
$current_user = $_SESSION['user_id'];
$post_id = (int) $_POST['delete'];
if ($current_user == $id) {
mysql_query("DELETE FROM group_posts WHERE id = $post_id AND user_id = $id") or die(mysql_error());
}
That query ensures that only posts with a given ID, created by the current author, can be deleted.
Does that answer your question?
Once you get more comfortable with SQL, you might also want to look into using prepared statements with mysqli or PDO. That will help your code clean and secure.
I'm assuming your delete buttons are simple links. Your links must contain all the information to delete a post. One way would be to pass a post id as a GET variable (e.g. link="myurl.com/posts/delete?id=#").
Your script would then at first make sure the current user is allowed to delete the post. For example:
$user_id = $_SESSION['user_id'];
$post_id = (int) $_GET['id'];
if(canDelete($user_id, $post_id))
{
// assuming post_id is unique for every post
$sql = sprintf("DELETE FROM group_posts WHERE id = %d", $post_id);
mysql_query($sql);
}
Of course, you'd have to implement canDelete($user_id) yourself.
By the way, "DELETE FROM group_posts WHERE $current_user = $id" always deletes every record in your table. At first you're comparing if $current_user equals $id and if they do happen to be equal, your query would look something like WHERE 1 = 1. I think you mean "DELETE FROM group_posts WHERE user_id = '$id'"
EDIT: It seems you want to use ajax for deleting your posts. I recommend using jQuery or any other proper javascript framework as it saves you time. Here is a link from the jQuery documentation describing how to make an ajax call to the server and a similar question to help you understand better.