General developer forum

Generate URL in Block

 
Picture of Hernán García
Generate URL in Block
 

Hi everybody,

I'm trying to develop a custom block for courses. The main idea is having featured links to several sities: inside/outside the course - blocks and external links.


I think im having rookie issues with PHP code. For example, im using JMAIL plugin for inbox and trying to automatically generate the url this way without success:


	public function get_content() {
if ($this->content !== null) {
  return $this->content;
}  
$course_id = $COURSE->id;
$this->content         =  new stdClass;
$this->content->text   = '<a href="blocks/jmail/mailbox.php?id="'.$course_id.'">E-Mail</a>';
return $this->content;
}


I dont truly know how to achieve/generate url depending the block. 


Thanks in advanced,

4aLifeTime

 
Average of ratings: -
Picture of Darko Miletić
Re: Generate URL in Block
Core developersParticularly helpful Moodlers

Your code is not correct.

This should work better:

    public function get_content() {
        global $COURSE;
        if ($this->content) {
            return $this->content;
        }

        $this->content = new stdClass();
        $this->content->text = html_writer::link(
            new moodle_url('/blocks/jmail/mailbox.php', ['id' => $COURSE->id]),
            "E-mail"
        );
        $this->content->footer = '';

        return $this->content;
    }





 
Average of ratings: Useful (1)
Picture of Hernán García
Re: Generate URL in Block
 

Drako, thanks for your quick reply. Works perfectly. 

Now im dealing with a similiar issue. I want to recreated the same situation, adding a new button to my featured links block, to redirect to the default news forum course. The situation here is that each forum has it own ID, and changes randomly. 


Is there a way to show or access the course elements? Independently of the element id? I think it goes a similar code but i dont know exactly how to access course elements.

    public function get_content() {
        global $COURSE;
        if ($this->content) {
            return $this->content;
        }

        $this->content = new stdClass();
        $this->content->text = html_writer::link(
            new moodle_url('/mod/forum/view.php, ['id' => $COURSE->????]),
            "E-mail"
        );
        $this->content->footer = '';

        return $this->content;
    }

Thanks in advanced,

4aLifeTime

 
Average of ratings: -
Picture of Darko Miletić
Re: Generate URL in Block
Core developersParticularly helpful Moodlers

This is how it should be done:

class myblockclass {

    protected function newsforumid() {
        global $CFG, $COURSE;
        $result = false;
        $plugins = \core_plugin_manager::instance()->get_enabled_plugins('mod');
        if (isset($plugins['forum'])) {
            require_once($CFG->dirroot . '/mod/forum/lib.php');
            if ($forum = forum_get_course_forum($COURSE->id, 'news')) {
                $result = $forum->id;
            }
        }
        return $result;
    }

    /**
     * @return bool
     */
    protected function newsforumurl() {
        $result = false;
        if ($forumid = $this->newsforumid()) {
            $result = html_writer::link(
                new moodle_url('/mod/forum/view.php', ['id' => $forumid]),
                "E-mail"
            );
        }
        return $result;
    }

    public function get_content() {
        if ($this->content) {
            return $this->content;
        }

        $this->content = new stdClass();
        $this->content->text = $this->newsforumurl();
        $this->content->footer = '';

        return $this->content;
    }

}





 
Average of ratings: -
Picture of Hernán García
Re: Generate URL in Block
 

Hi Drako: Thanks. I tried your code, but without success. The built url path in your code includes the course ID and if you click on the news forum doesnt. The resourse forum path has it own ID. Do i make myself clear? Could it be that the problem?

4aLifeTime

 
Average of ratings: -
Picture of Darko Miletić
Re: Generate URL in Block
Core developersParticularly helpful Moodlers

Ah yes, I forgot that id parameter is actually course module id. So you have two options here:

Alter the link generator to use f parameter instead of id and it would work as is.

    protected function newsforumurl() {
        $result = false;
        if ($forumid = $this->newsforumid()) {
            $result = html_writer::link(
                new moodle_url('/mod/forum/view.php', ['f' => $forumid]),
                "E-mail"
            );
        }
        return $result;
    }


Obtain the course module id.

    protected function newsforumid() {
        global $CFG, $COURSE;
        $result = false;
        $plugins = \core_plugin_manager::instance()->get_enabled_plugins('mod');
        if (isset($plugins['forum'])) {
            require_once($CFG->dirroot . '/mod/forum/lib.php');
            if ($forum = forum_get_course_forum($COURSE->id, 'news')) {
                if ($cm = get_coursemodule_from_id('forum', $forum->id)) {
                    $result = $cm->id;
                }
            }
        }
        return $result;
    }




 
Average of ratings: Useful (1)