output records in the form of html table from moodle database , custom block development - moodle

Please check this code for the custom block to be placed in the dashboard. We want to display an HTML table for the records. But it does not add up to the custom block, rather it appears on the top page.
enter image description here
class block_scorecard extends block_base {
function init() {
$this->title = get_string('pluginname', 'block_scorecard');
}
function get_content() {
global $DB;
if ($this->content !== NULL) {
return $this->content;
}
$content = '';
$courses = $DB->get_records('scorm_scoes_track', ['element' => 'cmi.core.total_time']);
$table=new html_table();
$table->head=array('No of attempts','Time modified','status');
foreach ($courses as $course) {
$attempt=$course->attempt;
$timemodified=userdate($course->timemodified, get_string('strftimerecentfull'));
$status=$course->value;
$table->data[]=array($attempt, $timemodified, $status);
}
echo html_writer::table($table);
$this->content = new stdClass;
$this->content->text = $content;
}}

echo html_writer::table($table);
Should be
$content .= html_writer::table($table);

Related

Custom Joomla 1.5 View cannot find the layout default.php file

I have been developing templates for Joomla 1.5 (obsolete one, yes I knew it. Unfortunately, I've been spending some time working with it and move on to newer version will be like starting from zero).
I tried to understand how Joomla 1.5 components work, especially for those using MVC principle, and somehow I managed to make things work, except now I have been stuck with the layout of a customized view. For unknown reason, Joomla could not read or find the layout (default.php) inside the folder com_hello2/views/viewed/tmpl and give '500 error Layout default not found'.
I tried to move the default.php file from folder to folder, looking if I could figure out where Joomla want me to put the default.php. It DOES work for the default view, located on com_hello2/views/hello2/tmpl.
Could anyone please help me with this problem?
Here are the codes from the controllers:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
class Hello2Controller extends JController
{
function display($tpl = null)
{
$task = JRequest::getVar('task', null, 'default', 'cmd');
switch ($task){
case 'view':
$model = &$this->getModel('Viewed');
$view = &$this->getView('viewed','html');
$mylayout = 'default';
break;
case 'newstatus':
break;
default:
$view = &$this->getView('hello2','html');
$model = &$this->getModel();
$mylayout = 'default';
break;
}
$view->setModel($model,true);
$view->setLayout($mylayout);
$view->display();
}
}
?>
I created two models, one is the default Hello2ModelHello2, the new one is Hello2ModelViewed. I seperated the two models based on the tasks in the controllers.
the Hello2ModelHello2:
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );
jimport('joomla.application.component.model');
class Hello2ModelHello2 extends JModel
{
//add CC
function getComments()
{
global $mainframe;
$db = JFactory::getDBO();
$id = JRequest::getVar('id', 0, '', 'int');
$query = 'SELECT a.*'.
' FROM #__hello1 AS a '.
' LEFT JOIN #__content AS b ON a.article_id = b.id'.
' WHERE a.state = 1';
$db->setQuery( $query );
$comments = $db->loadObjectList();
return $comments;
}
}
?>
now the Hello2ModelViewed
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );
jimport('joomla.application.component.model');
class Hello2ModelViewed extends JModel
{
function __construct()
{
parent::__construct();
$id = JRequest::getVar('id', 0, '', 'int');
$this->setId((int)$id);
}
function setId($id)
{
// Set new article ID and wipe data
$this->_id = $id;
$this->_article = null;
}
//get certain comment only.
function getSingle()
{
global $mainframe;
$db = JFactory::getDBO();
$id = JRequest::getVar('id', 0, '', 'int');
$query = 'SELECT a.*'.
' FROM #__hello1 AS a' .
' LEFT JOIN #__content AS b ON a.article_id = b.id'.
' WHERE a.id = '.$id.
' AND a.state = 1';
$db->setQuery( $query );
$comments = $db->loadObjectList();
return $comments;
}
}
?>
the view.html of Viewed View is
<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.view');
jimport('joomla.environment.request');
class Hello2View extends JView
{
function __construct($config = array())
{
parent::__construct($config);
}
}
class Hello2ViewViewed extends Hello2View{
function display($tpl = null)
{
$model = &$this->getModel();
$id = JRequest::getVar('id', 0, '', 'int');
$comments = $model->getSingle();
JRequest::setVar('row',$comments);
parent::display($tpl);
}
}
?>
and view.html of Hello2 View is
<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.view');
jimport('joomla.environment.request');
class Hello2ViewHello2 extends JView{
function display($tpl = null)
{
$model = &$this->getModel();
$comments = $model->getComments();
JRequest::setVar('rows',$comments);
parent::display($tpl);
if($this->getLayout() == 'view') {
$this->view($tpl);
return;
}
}
}
?>
It works for me to execute index.php?option=com_hello2&task=display
but gets error when execute index.php?option=com_hello2&task=view
eventhough the default.php is already inside the directory views/viewed/
any input appreciated! :)

Prevent render template in fuelphp

In fuelphp, we can render template from controller. But I want prevent render template from package.
Example:
Step 1: fuelphp run controlelr -> render template
Step 2: run package -> have a command to clear all data in step 1. and
render blank page.
Result with a blank page
$this->template->content = ...
\Package::removeTemplate();
I tried with
\Event::forge(array('shutdown'));
\Fuel::finish();
But it is not success. How can I do it?
You can always modify your template, inside the controller in every function just use
$this->template
Example
class Controller_lorem extends Controller_Main {
public $template = 'template_default';
public function action_ipsum()
{
//use customize template other than default
$this->template = View::forge('template_custom');
}
I found a solution. Rewrite \Fuel::finish()
public static function finishS()
{
if (\Config::get('caching', false))
{
\Finder::instance()->write_cache('FuelFileFinder');
}
if (static::$profiling and ! static::$is_cli)
{
// Grab the output buffer and flush it, we will rebuffer later
$output = ob_get_clean();
$headers = headers_list();
$show = true;
foreach ($headers as $header)
{
if (stripos($header, 'content-type') === 0 and stripos($header, 'text/html') === false)
{
$show = false;
}
}
if ($show)
{
\Profiler::mark('End of Fuel Execution');
if (preg_match("|</body>.*?</html>|is", $output))
{
$output = preg_replace("|</body>.*?</html>|is", '', $output);
$output .= \Profiler::output();
$output .= '</body></html>';
}
else
{
$output .= \Profiler::output();
}
}
// Restart the output buffer and send the new output
ob_start();
**/// Remove this line echo $output;**
}
}

ZfcUser - flash messages after user registration

I'm trying to add some flash messages after user registered
$sharedManager->attach('ZfcUser\Service\User', 'register.post', function($e) use($serviceManager) {
$user = $e->getParam('user');
$mail = new Mail\ActivateAccount($serviceManager, $user);
$mail->send($user->getEmail());
$flash = $serviceManager->get('ControllerPluginManager')->get('flashMessenger');
$flash->addSuccessMessage('Check your mailbox, please.');
});
However, after I was redirected to login page I didn't see any message. Anybody knows the reason?
There is view helper that displays messages
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\View\Helper\FlashMessenger;
class ShowMessages extends AbstractHelper
{
public function __invoke()
{
$messenger = new FlashMessenger();
$error_messages = $messenger->getErrorMessages();
$messages = $messenger->getSuccessMessages();
$result = '';
if (count($error_messages)) {
foreach ($error_messages as $message) {
$result .= '<p class="alert alert-danger">' . $message . '</p>';
}
}
if (count($messages)) {
foreach ($messages as $message) {
$result .= '<p class="alert alert-success">' . $message . '</p>';
}
}
return $result;
}
}
On other pages flash messages works fine.
Thanks!
I found my mistake. I created new messenger instead of getting it throuhg service locator.
$messenger = new FlashMessenger();
should be
$messenger = $this->sm->getServiceLocator()->get('ControllerPluginManager')->get('flashMessenger');

Jomsocial Extra Field

I am trying to create extra fields on the Jomsocial Groups Create New Group page, its suggested on the Jomsocial docs that the best way is to creat a plugin to do this.
As I have never created such a complex plugin do anyone have a working example to start with?
Here is the code that I have tried with
<?php
defined('_JEXEC') or die('Restricted access');
if (! class_exists ( 'plgSpbgrouppostcode' )) {
class plgSpbgrouppostcode extends JPlugin {
/**
* Method construct
*/
function plgSystemExample($subject, $config) {
parent::__construct($subject, $config);
// JPlugin::loadLanguage ( 'plg_system_example', JPATH_ADMINISTRATOR ); // only use if theres any language file
include_once( JPATH_ROOT .'/components/com_community/libraries/core.php' ); // loading the core library now
}
function onFormDisplay( $form_name )
{
/*
Add additional form elements at the bottom privacy page
*/
$elements = array();
if( $form_name == 'jsform-groups-forms' )
{
$obj = new CFormElement();
$obj->label = 'Labe1 1';
$obj->position = 'after';
$obj->html = '<input name="custom1" type="text">';
$elements[] = $obj;
$obj = new CFormElement();
$obj->label = 'Labe1 2';
$obj->position = 'after';
$obj->html = '<input name="custom2" type="text">';
$elements[] = $obj;
}
return $elements;

Zend Framework view render takes a lot of memory

I try to create emails for next sending with my cron php script.
And I use Zend_View for rendering email template.
I have 50k users but 3000 emails was created with 64MB memory limit and 7200 with 128MB.
Code of rendering emails
public function prepareEmailBody($template, $templates)
{
$view = new Zend_View(array('basePath' => './application/views'));
$template_file_name = $template . '.phtml';
foreach ($templates as $key => $value) {
$view->$key = $value;
}
$body = $view->render('mails/' . $template_file_name);
return $body
}
And use this method in
foreach ($users as $user) {
.....
$text = Mailer::getInstance()->prepareEmailBody($template, $vars);
.....
}
Please advice how optimize code.
You could try using one View object and the partial helper instead, this might improve things (or might make it slower).
public function getView()
{
if (!$this->_view) {
$this->_view = new Zend_View(array('basePath' => './application/views'));
}
return $this->_view;
}
public function prepareEmailBody($template, $templates)
{
$template_file_name = $template . '.phtml';
$body = $this->getView()->partial($template_file_name, $templates);
return $body
}