0
require_once 'edit_form.php';
require_once __DIR__ . '/../../config.php';
class block_add extends block_base
{

public function init()
{
    $this->title = 'Your Block';
}
public function get_content()
{
    global $DB, $OUTPUT, $USER;


    $this->content = new stdClass();

    $mform = new edit_form();
    if ($fromform = $mform->get_data()) {
        $record = new stdClass();
        $messages = $DB->get_records('blocks_table');

        $record->id = $fromform->id;
        $a = $fromform->message['text'];
        $templatecontext = (object) [
            'show' => array_values($messages),
        ];

        // $templatecontext1=(object)[
        //     'mform' => $mform->render(),
        // ];
        $get = $DB->get_records('blocks_table');
        foreach ($get as $value) {
            // var_dump($value->message_attribute);
            if ($value->message_attribute=='2') {
                $this->content->text = $OUTPUT->render_from_template('block_add/manage', $templatecontext);
            } else {
                echo 'doesnt match';
            }
        }

        $b = preg_match_all('#src="([^\s]+)"#', $a, $matches);
        $c = implode(' ', $matches[1]);
        
        $record->message = $c;
        $record->message_type = $fromform->message_type;
        $record->message_attribute = $fromform->message_attribute;
        $DB->insert_record('blocks_table', $record);

    } else {
        $this->content->text = $mform->render();
    }

    return $this->content;

}

public function has_config()
{return true;}
}

I have mustache file too but on mustache only pictures are displaying, I am trying but stuck on this, trying to show those pictures whose values are 2, Actually to be honest i am an entry level coder, i am new to php, please do help me as i am stuck on this.

enter image description here

1 Answers1

0
    <?php /** @noinspection ALL */
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 *
 * @package    block_add
 * @copyright  Mohammad Ahmad
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once('edit_form.php');

class block_add extends block_base
{
    

    public function init(){
        $this->title = 'Your Block';
    }
    public function get_content(){
        global $DB,$OUTPUT,$USER;
        $this->content = new stdClass();
        
        $mform = new edit_form();
         if ($fromform = $mform->get_data()) {
            $record= new stdClass();
            $record->id = $fromform->id;
            $a=$fromform->message['text'];
            $b=preg_match_all('#src="([^\s]+)"#', $a, $matches);
            $c=implode(' ',$matches[1]);
            $record->message=$c;
            $record->message_type=$fromform->message_type;
            $record->message_attribute=$fromform->message_attribute;
            $record->roleid=$fromform->roleid;
            $DB->insert_record('blocks_table', $record);
            
            $a=$DB->get_records('role_assignments');
            foreach($a as $data){
             $roles = $DB->get_records('role_assignments',['id'=>$data->id]);
             var_dump($roles);
             foreach ($roles as $role) {
                $this->content->footer = "userid  = ".$role->userid."</br>" ;
             }
         }
            $sql="SELECT * FROM {blocks_table} WHERE roleid=$role->userid";
            $get=$DB->get_records_sql($sql);
            var_dump($get);
            $templatecontext=(object)[
                'show'=>array_values($get),
            ];
            $this->content->text=$OUTPUT->render_from_template('block_add/manage', $templatecontext);

        }else{
            $this->content->text = $mform->render();
        }

        return $this->content;

    }




    function has_config() {return true;}
}

This is the code that is design by me.