Looking for download (PDF, CSV) the data from moodle custompage. Are there API function, need to call for downloading the data from custompage. The custompage has a table data:
custompage
$table = new html_table();
$table->head = array('course', 'users');
$table->attributes['class'] = 'table';
$sql = "SELECT c.fullname, count(cc.userid) AS 'completed'
FROM {course_completions} cc JOIN {course} ON c.id = cc.course WHERE cc.timestarted > 0
GROUP BY c.fullname ";
$mds = $DB->get_records_sql($sql);
foreach ($mds as $m) {
$table->data[] = new html_table_row(array(implode(array($m->fullname. $m->lastname)),
$m->completed ));
}
echo html_writer::table($table);
For a basic PDF
require_once($CFG->libdir . '/pdflib.php');
$doc = new pdf();
$doc->AddPage();
$output = html_writer::table($table);
$doc->writeHTML($output);
$doc->Output('filename.pdf');
There is a better example in the Moodle source here - /lib/tests/other/pdflibtestpage.php
For CSV
require_once($CFG->libdir . '/csvlib.class.php');
csv_export_writer::download_array('filename.csv, $mds);
Both of the above should be before printing anything to the screen so they are downloaded.
Also in your SQL, you should use GROUP BY c.id because the course fullname isn't unique.
Related
In my Joomla website, I need to execute some custom SQL queries, that have to select different titles from related categories.
Problem I have it works like option Prepare Content is turned off, so all of my content is outside HTML tags.
Module content looks like this:
{source}
<?php
$var_result = '';
$var_categories = array();
$var_category_list = array();
$db =& JFactory::getDBO();
$query = 'select * from jneg_categories where parent_id = 9';
$db->setQuery($query,0,300);
$res = $db->loadObjectList();
foreach ( $res as $row ) {
$var_categories[($row->id)] = $row->title;
$var_category_list[] = $row->id;
}
$var_category_list = implode($var_category_list, ', ');
$sql = "select * from jneg_content where catid IN (".$var_category_list.") order by `catid`";
$db->setQuery($sql,0,30000);
$res = $db->loadObjectList();
$var_current_cat = 0;
foreach ( $res as $row ) {
if ($current_cat != $row->catid) {
$current_cat = $row->catid;
echo '<h2>'.$categories[($row->catid)] . '</h2>';
echo '<br>';
}
echo $row->title;
echo '<br>';
}
?>
{/source}
Can you help me how to get proper HTML as a result of this code please.
Sourcer or other php rendering plugins don't run in html modules unless you go under the module 'options' and select 'prepare content'...
...or you can use this module and just include your php file directly:
https://github.com/idea34/mod_show
Ok, I did it with Jumi plugin - http://2glux.com/projects/jumi/usage-for-j15
Thank you anyway.
$condgrade = $DB->get_recordset_sql('SELECT c.fullname, gi.itemtype, gi.itemname,
gg.userid, gg.finalgrade FROM
'.$CFG->prefix.'grade_items gi
JOIN '.$CFG->prefix.'grade_grades gg ON gi.id =
gg.itemid JOIN '.$CFG->prefix.'course c ON c.id =
gi.courseid WHERE
gi.itemtype IN ("course") AND
gg.userid = '.$USER->id.'');
foreach($condgrade as $cgg)
{
$this->content->text .= html_writer::div($cgg->fullname.' : '.round($cgg->finalgrade).'%', array('class' => 'cours'));
}
Is there any other way of retrieving data directly from the query, rather using foreach loop everytime something like in mysql : mysql_fetch_object($users);
$records = $DB->get_records_sql('SELECT c.fullname, gi.itemtype, gi.itemname,
gg.userid, gg.finalgrade FROM
{grade_items} gi
JOIN {grade_grades} gg ON gi.id =
gg.itemid JOIN {course} c ON c.id =
gi.courseid WHERE
gi.itemtype IN ("course") AND
gg.userid = :userid', array('userid' => $USER->id));
This will return an array of records.
Note also, the use of {tablename} notation, rather than manually inserting the $CFG->prefix and the use of ':userid' bound parameter + parameter list passed as "array('userid' => $USER->id)", which helps to protect you from SQL injection attacks.
Get a single record?
$user = $DB->get_record('user', array('id' => 1));
http://docs.moodle.org/dev/Data_manipulation_API#Getting_a_single_record
EDIT : As well as the recommendations from davosmith, you also need to have a unique id in the first column. Also you can just use $DB->get_records_sql() - use $DB->get_recordset_sql() if you expect a large amount of data. If you do use get_recordset_sql() then you will also need to check the query is valid with $condgrade->valid() and close it after $condgrade->close();
$sql = "SELECT gg.id,
c.fullname,
gi.itemtype,
gi.itemname,
gg.userid,
gg.finalgrade
FROM {grade_items} gi
JOIN {grade_grades} gg ON gi.id = gg.itemid
JOIN {course} c ON c.id = gi.courseid
WHERE gi.itemtype = :itemtype
AND gg.userid = :userid";
$params = array('itemtype' => 'course', 'userid' => $USER->id);
$condgrade = $DB->get_records_sql($sql, $params);
This will return an array of record objects. The array key will be whatever is in the first column. eg:
$condgrade[1] = $recordobject;
So you can access a record object directly eg:
with $condgrade[1]->fullname
But you won't know what the id is unless you retrieve it from the database. So I'm not clear why you want to access the object directly? If you can give me an example of how you would use this directly then I can try to give an answer.
I am working on a facebook application, and some of the data that I want are the groups of that user and the other members of that group as shown in this code:
$groups = $facebook->api('/me/groups?since=0');
mysqlc();
foreach($groups['data'] as $group){
$gid=GetSQLValueString($group['id'], "text");
$name=GetSQLValueString($group['name'], "text");
//echo $gid." -> ".$name;
$query = sprintf("SELECT * FROM `group` WHERE gid = %s", $gid);
$res = mysql_query($query) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
if(mysql_num_rows($res) == 0){
$iquery = sprintf("INSERT INTO `group`(gid, name) values(%s,%s)", $gid, $name);
$ires = mysql_query($iquery) or die('Query failed: ' . mysql_error() . "huhuhu<br />\n$sql");
}
$c = explode("'", $gid);
$params = "/".$c[1]."/members";
echo "'".$params."'";
$members = $facebook->api("'".$params."'");
print_r($members);
}
what I wanted to have after this code are the members of each group that the user has, but this code is not working properly.. So my main query is what is the proper way or replacement to this line:
$members = $facebook->api("'".$params."'");
where I can use the fetched group Id. thanks a lot to those who will answer my query.
I'v done this query to look for opportunities based on account name and name:
public function getOpportunity($session_id, $client, $email, $name){
$acc = $this->getAccount($session_id, $client, $email);
$acc_id = $acc->entry_list[0]->id;
$query2 = "
opportunities.id in
(select accounts_opportunities.opportunity_id from
accounts_opportunities inner join
accounts where
accounts_opportunities.account_id = accounts.id and
accounts_opportunities.deleted = 0 and
accounts.deleted = 0 and
accounts.id = '{$acc_id}')
and opportunities.deleted=0
and opportunities.name = '{$name}'";
$op = $client->get_entry_list($session_id, 'Opportunities', $query2);
return $op;
}
For some reason, it is returning me an error with erro 40 access denied. I don't know why this query is not working. I did a similar one, to find Accounts by email, and it successfully return me the results i want without any error. That query is the following:
public function getAccount($session_id, $client, $email){
$query = "accounts.id in
( select bean_id from
email_addr_bean_rel inner join
email_addresses where
email_addr_bean_rel.email_address_id = email_addresses.id
and email_addr_bean_rel.deleted = 0 and
email_addresses.deleted = 0 and
bean_module = 'Accounts' and
email_addresses.email_address = '{$email}' )";
$acc = $client->get_entry_list($session_id, 'Accounts', $query);
return $acc;
}
Any help would be appreciated. Thank you.
EDITED: I'm using SOAP API and PHP.
See this blog post for more details...
http://developers.sugarcrm.com/wordpress/2012/03/19/howto-avoiding-subqueries-with-our-web-services/
I want to get the contents from a row in the Postgresql database and compare the lowercase version of it to a lowercase version of a user input to check if it exists in the database.
i tried:
"SELECT LOWER(name) FROM user_names WHERE name LIKE '%$search%' ORDER BY name ASC"
but that make query not working at all.
EDIT
I am trying to implement an autocomplete Jquery UI like here:
http://jqueryui.com/demos/autocomplete/#remote
for search box (for names)
using javascript and php.
php code:
$search = ($_GET['term']);
if (!$con)
{ die('Could not connect: ' . pg_last_error ());}
else
{
$sql = "SELECT name FROM users_table WHERE name LIKE '%$search%' ORDER BY name ASC";
$result = pg_query($sql);
$json = '[';
$first = true;
while ($row = pg_fetch_array($result))
{
if (!$first) { $json .= ','; } else { $first = false; }
$json .= '{"value":"'.$row['name'].'"}';
}
$json .= ']';
echo $json;
exit();
}
JavaScript code:
$(document).ready(function()
{
$('#auto').autocomplete(
{
source: "./file.php",
minLength: 3
})
})
all above work great.. exactly like in Demo here: http://jqueryui.com/demos/autocomplete/#remote
my problem is that the names in database stored in Uppercase (e.g. LORI)
and of course the user prefers to insert a lowercase in search box to search for name (e.g. lori). but since it stored in uppercase, i need to convert it.
i tried as your suggestion :
$sql = "SELECT LOWER(name) FROM users_table WHERE name ILIKE '%$search%' ORDER BY name ASC";
then i got an empty drop down list!
pretty weird!
thanks in advance.
Google is your friend:
SELECT LOWER(name) FROM user_names
WHERE name ILIKE '%$search%' ORDER BY name ASC