Moodle course content from renderer function "coursecat_coursebox_content" - moodle

I hope you will understand my english clearly. So my question is :
I'm new to moodle, I just start testing in local. So when the course is show on the first page, its appear with TITLE and CONTENT (Description and Teachers).
And what I want to do is to seperate the CONTENT to have a Description seperate from the Teachers when rendering. The idea is to use microdata from schema so that I can apply a different itemprop to "Description" and a different itemprop to "Teachers".
Right now when I applied microdata to the renderer its grabs the Description and Teachers and put all together.
Currently structure:
$content .= html_writer::start_tag('div', array('class' => 'content'));
$content .= html_writer::start_tag('span', array('itemprop' => 'description'));
$content .= $this->coursecat_coursebox_content($chelper, $course);
$content .= html_writer::end_tag('span'); // .span description
$content .= html_writer::end_tag('div'); // .content
now what I want is something like this:
$content .= html_writer::start_tag('div', array('class' => 'content'));
$content .= html_writer::start_tag('span', array('itemprop' => 'description'));
$content .= $this->coursecat_coursebox_content("description of a course");
$content .= html_writer::end_tag('span'); // .span description
$content .= html_writer::start_tag('span', array('itemprop' => 'creator'));
$content .= $this->coursecat_coursebox_content("Teachers of a course");
$content .= html_writer::end_tag('span'); // .span creator
$content .= html_writer::end_tag('div'); // .content
Thank you in advance, I'm using moodle 2.8. And this microdata I'm applying it in the file renderer.php locate at http://localhost/moodle/course/renderer.php

Related

How to create HTML tables dynamically using Perl?

I am working on a project where I need to access CSV file form a web URL. I am able access the file and print the content from CSV file in the terminal, but I'm unable to produce HTML table (then I'll later send email using MIME).
Here is my code - I need complete CSV file as HTML table delivered to my email.
#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use POSIX qw(strftime);
use MIME::Lite;
use Getopt::Long;
my $to="pankajdustbin\#gmail.com";
my $from="pankajdustbin\#gmail.com";
$subject="CSV File";
my $content = `curl -s "https:csvfile.com"`;
#output = split(/\n/,$content);
foreach my $line (#output) {
my ($col1, $col2, $col3, $col4, $col5, $col6) = split(/,/, $line);
#print "\n$col1, $col2, $col3, $col4, $col5, $col6\n";
$message = "<tr> <td>$col1</td> <td>$col2</td> <td>$col3</td> <td>$col4</td> <td>$col5</td> <td>$col6</td></tr>";
}
my $msg=MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message
);
$msg->attr('content-type' => 'text/html');
#MIME::Lite->send("smtp");
$msg->send;
With this code, the HTML table contains only the last row of the CSV. Can someone help me how I should do?
CSV has around 100 rows, and the sample output that I see in terminal as below:
1.2.3.4 03-04-2022. 03-08-2022. Red. 1%. Positive
5.2.3.4 03-05-2022. 04-08-2022. Blue. 1%. Neutral
and so on...
The problem is that you overwrite the contents of the $message variable each time through the foreach loop. This means that $message will only have the last value that you assign to it.
You could append to the contents of the variable using the .= operator:
my $message;
foreach my $line (#output) {
my ($col1, $col2, $col3, $col4, $col5, $col6) = split(/,/, $line);
$message .= "<tr> <td>$col1</td> <td>$col2</td> <td>$col3</td> <td>$col4</td> <td>$col5</td> <td>$col6</td></tr>";
}
Previous answer covered that you overwrite $message in the loop what is not you have intended.
Following snippet code demonstrates slightly different approach to build html table utilizing split and for loop.
Then table can be utilized anyway you desire -- send it by mail or generate html page. In this demo code complete html page generated.
Note #1: \n and \t optional and added for html readability only
Note #2: as no sample input CVS file was provided the content was assumed based on provided output in terminal
use strict;
use warnings;
use feature 'say';
my $table = '<table border=1>';
while( my $line = <DATA>) {
chomp $line;
$table .= "\n\t\t\t<tr>";
$table .= "\n\t\t\t\t<td>" . $_ . '</td>' for split(/,/,$line);
$table .= "\n\t\t\t</tr>";
}
$table .= "\n\t\t</table>";
my $html =
"<html lang='en'>
<head>
<meta charset='utf8' />
<link rel='stylesheet' href='css/styles.css'>
<title>
CVS table
</title>
</head>
<body>
$table
</body>
</html>
";
say $html;
__DATA__
1.2.3.4,03-04-2022,03-08-2022,Red,1%,Positive
5.2.3.4,03-05-2022,04-08-2022,Blue,1%,Neutral
1.2.3.4,03-04-2022,03-08-2022,Red,1%,Positive
5.2.3.4,03-05-2022,04-08-2022,Blue,1%,Neutral
1.2.3.4,03-04-2022,03-08-2022,Red,1%,Positive
5.2.3.4,03-05-2022,04-08-2022,Blue,1%,Neutral

CGI::Application, hidden form value overwritten

I am going through the basic example for CGI::Application but when I try to add a 3rd mode, it seems the query object is refusing to use my supplied value.
webapp.cgi:
#!/usr/bin/perl
use webapp;
my $webapp = WebApp->new();
$webapp->run();
webapp.pm:
package WebApp;
use base 'CGI::Application';
sub setup {
my $self = shift;
$self->start_mode('mode1');
$self->mode_param('rm');
$self->run_modes(
'mode1' => 'do_stuff',
'mode2' => 'do_more_stuff',
'mode3' => 'do_something_else'
);
}
sub do_stuff {
my $self = shift;
my $q = $self->query();
my $output = '';
$output .= $q->start_html(-title => 'Widget Search Form');
$output .= $q->start_form();
$output .= $q->textfield(-name => 'widgetcode');
$output .= $q->hidden(-name => 'rm', -value => 'mode2');
$output .= $q->submit();
$output .= $q->end_form();
$output .= $q->end_html();
return $output;
}
sub do_more_stuff {
my $self = shift;
my $q = $self->query();
my $widgetcode = $q->param("widgetcode");
my $output = '';
$output .= $q->start_html(-title => 'List of Matching Widgets');
$output .= $q->start_form();
$output .= $q->textfield(-name => 'widgetcode');
$output .= $q->hidden(-name => 'rm', -value => 'mode3');
# ^^^^^^
# this value is being ignored
$output .= $q->submit();
$output .= $q->end_form();
$output .= $q->end_html();
return $output;
}
sub do_something_else {
my $self = shift;
my $q = $self->query();
my $widgetcode = $q->param("widgetcode");
my $output = '';
$output .= $q->start_html(-title => 'Widgets details');
$output .= $q->start_form();
$output .= $q->textfield(-name => 'widgetcode');
$output .= $q->hidden(-name => 'rm', -value => 'mode4');
$output .= $q->submit();
$output .= $q->end_form();
$output .= $q->end_html();
return $output;
}
1;
So it works fine to load the first page (mode1), it gives me the form, and I can submit it and reach the second page (mode2), but I cannot reach mode3, because the rm param is being set to "mode2", despite the fact that, as you can read above, I am setting it to "mode3". That means I am sent back to mode2 again. I can change the rm to be rm2 or something else and then the right value gets picked up, but obviously that's not helpful, since the rm variable is what is used to set the mode.
I don't have experience with CGI.pm (which supplies the query object) and as you can tell, I am only just starting to learn CGI::Application, so I don't know what is going on or how to solve this.
It seems the perlmonks had the wisdom: Hidden fields using CGI
You can use the -override parameter to force it to use the default value.
Which in my case would be used as follows:
$output .= $q->hidden(-name => 'rm', -value => 'mode3' , -override => 1);
Hope that helps whoever finds this question through a search, since this isn't obvious at all.
Yes, it appears that the hidden method will use the current form value if one exists instead of what you specify as the default. This could be observed with the following code when accessing a view with ?rm=mode2:
$output .= $q->hidden(-name => 'rm', -value => 'mode3'); # Prints mode2
$q->param('rm' => 'mode3');
$output .= $q->hidden(-name => 'rm'); # Print mode3
As you found, the best solution is to use the override flag as documented in CGI #Form Elements
$output .= $q->hidden(-name => 'rm', -value => 'mode3', -override => 1); # Print mode3

Inspect cookies using Perl

I've written a short Perl script that lists the current cookies for the website, but somehow it reports the cookie's domain and expires empty. What am I doing wrong or am I misunderstanding the mechanics behind cookies?
My ultimate goal is to be able to delete exsisting cookies with a button.
The script is live here, but it may help if you visit my blog first so there are actually some cookies set. Here is my source code:
use warnings;
use strict;
use CGI::Cookie;
my $table;
my %cookies = CGI::Cookie->fetch;
if ( keys %cookies ) {
$table .= "<table border=\"3\" cellpadding=\"5\">";
$table .= "<caption>COOKIES</caption>";
$table .= "<tr><th>Name</th><th>Domain</th><th>Path</th><th>Expires</th>
<th align=\"left\">Value</th></tr>";
foreach my $cookie ( keys %cookies ) {
$table .= "<tr>";
$table .= "<td>$cookie</td>";
$table .= "<td>" . $cookies{ $cookie }->domain() . "</td>";
$table .= "<td>" . $cookies{ $cookie }->path . "</td>";
$table .= "<td>" . $cookies{ $cookie }->expires . "</td>";
$table .= "<td>" . $cookies{ $cookie }->value . "</td>";
$table .= "</tr>";
}
$table .= "</table>";
}
print "Content-Type: text/html\n\n";
print "<html>\n";
print "<head></head>\n";
print "<body>\n";
print "$table";
print "</body>\n";
print "</html>\n";
Regardless the various cookies on various websites where I install the script, the output looks like this and Domain and Expires is always empty:
+-----------------------------------------------------------------------------------------+
| Name | Domain | Path | Expires | Value |
|---------------+--------+------+---------+-----------------------------------------------|
| bb2_screener_ | | / | | 1379007156 2001:980:1b7f:1:a00:27ff:fea6:a2e7 |
+-----------------------------------------------------------------------------------------+
Browsers does not provide any meta information to the server about the cookies they send. There isn't even a way for them to send it. What the browser sends looks like the following:
Cookie: a=b; c=d; e=f

Identifying a standard gettext pot file

Within my php framework (CakePHP), is a i18n tool for generating POT files. The header of the file is generated like so:
protected function _writeHeader() {
$output = "# LANGUAGE translation of CakePHP Application\n";
$output .= "# Copyright YEAR NAME <EMAIL#ADDRESS>\n";
$output .= "#\n";
$output .= "#, fuzzy\n";
$output .= "msgid \"\"\n";
$output .= "msgstr \"\"\n";
$output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
$output .= "\"POT-Creation-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
$output .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n";
$output .= "\"Last-Translator: NAME <EMAIL#ADDRESS>\\n\"\n";
$output .= "\"Language-Team: LANGUAGE <EMAIL#ADDRESS>\\n\"\n";
$output .= "\"MIME-Version: 1.0\\n\"\n";
$output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
$output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
$output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
return $output;
}
I am curious to know if the following:
$output .= "#, fuzzy\n";
$output .= "msgid \"\"\n";
$output .= "msgstr \"\"\n";
breaks some sort of standards of gettext. If it does not, I would love an explanation as to why one would put that in the header of the file.
I suppose it's 'fuzzy' because the headers are not complete. i.e. the template entries will be filled in when the PO files are generated from the POT.
The official Gettext xgettext tool that generates POT files from source code also adds the fuzzy flag to the header. By that token it is certainly not against the standard.

php mail sending attachment the can't be opened

I'm trying to write a page where users can send an email along with an attachment. I think I'm almost there. Right now I get the email, but the attachment is either empty in filesize, or when I try to open it, I get a message that it cannot be opened. Any help is appreciated.
function valid_email($Email)
{
//new regex, didn't give me any errors...might be a bit more exact
if (ereg('^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$', $Email) != false)
return true;
else
return false;
}
function sendEmail($email){
return mail ($email['to'], $email['subject'], $email['message'], $email['headers']);
}
if ( strlen($_FILES['Resume_File']['name']) > 0 )
{ // If a file was uploaded, do some processing
$filename = preg_replace('/[^a-zA-Z0-9._-]/', '', $_FILES['Resume_File']['name']);
$filetype = $_FILES["Resume_File"]["type"];
$filesize = $_FILES["Resume_File"]["size"];
$filetemp = $_FILES["Resume_File"]["tmp_name"];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if ( !preg_match('/\.(doc|docx|odt|pdf|rtf|txt)/i', $ext) )
{ $errors++;
$errorLog .= "Upload filetype not supported.";
}
else if ( $filesize > 2000000 )
{ $errors++;
$errorLog .= "File size too high, up to 2MB is allowed.";
}
else
{ // Looks like the file is good, send it with the email
//$fp = fopen($filetemp, "rb");
//$file = fread($fp, $filesize);
//$file = chunk_split(base64_encode($file));
//$email['headers'] .= "\n--{$num}\n";
//$email['headers'] .= "Content-Type:{$filetype}";
//$email['headers'] .= "name={$filename}r\n";
//$email['headers'] .= "Content-Disposition: attachment; ";
//$email['headers'] .= "filename={$filename}\n";
//$email['headers'] .= "{$file}";
}
}
// get posted data into local variables
$fname = trim(stripslashes($_POST['fname']));
$lname = trim(stripslashes($_POST['lname']));
$emailAddress = trim(stripslashes($_POST['email']));
$company = trim(stripslashes($_POST['company']));
$information = trim(stripslashes($_POST['information']));
$subject = trim(stripslashes($_POST['subject']));
$title = trim(stripslashes($_POST['title']));
//setup email
$to = 'me#me.com';
$subject = "Resume Submission";
$headers = "From: {$fname} {$lname} <{$emailAddress}>\r\n";
// prepare email body text
$message = "First Name: {$fname} <br>";
$message .= "Last Name: {$lname} <br>";
$message .= "Email: {$emailAddress} <br>";
$message .= "Title: {$title} <br><br>";
$message .= "Comments: {$information}";
if ( $errors == 0 ) {
// Attachment headers
//$to = "myemail#mydomain.com";
//$from = "Website <website#mydomain.com>";
//$subject = "Test Attachment Email";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
//$filename = "document.pdf";
//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers .= "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
}
//sendEmail($email);
// validation
if ( $errors == 0 ) {
if (valid_email($emailAddress) && $fname != "" && $lname != "") { //if return is true...
mail($to, $subject, $body, $headers);
echo 0; //Success
}else { //otherwise
echo 1; //Error
}
} else {
echo 1; //Error
}
I am using portions of code which I found here
PHP mail() attachment problems
Thank you!
PHP's mail() function is really poor, particularly when trying to do advanced stuff like adding attachments. It's really only worth using for the most basic "send a notification email to the site admin" type emails, and even then it's not always the best solution.
I would suggest ditching any attempt to work with the mail() function itself, and instead switch to using a decent PHP mailer class such as the appropriately named phpMailer.
phpMailer makes creating emails from PHP dead easy. I has all the features you could want, including making it very easy to add attachments, write HTML emails, and plenty more.
But the best thing about phpMailer is that it removes all the need to waste dozens of lines of code formatting the email headers. All that stuff with the separators and mime types becomes reduced to a few simple lines of code. Easier to read, easier to maintain, and less likely to have bugs. You win all round.
//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));
$pdfdoc is not mentioned any where in the page. I presume that is the issue:|