-
Notifications
You must be signed in to change notification settings - Fork 1
Example DAO's
philcali edited this page Dec 12, 2011
·
2 revisions
In the example extension, we made a very simple extension that sends a greeting to a student who is enrolled in a class for the first time.
Let's make a custom DAO that represents a custom greeting message.
Create a enrol_ues_greetings
table with the following fields:
- id -> int(11)
- subject -> varchar(255)
- fullmessage -> text
- fullmessageformat -> varchar(20)
- sectionid -> int(11) foreign key (enrol_ues_sections -> id)
- userfrom -> int(11) foreign key (user -> id)
In example/classes/lib.php
:
<?php
require_once $CFG->dirroot . "/enrol/ues/publiclib.php";
ues::require_daos();
class ues_greeting extends ues_external {
function get_or_default(array $params) {
$greeting = self::get($params);
if (empty($greeting)) {
$section = ues_section::get(array("id" => $params["sectionid"]));
$greeting = new stdClass;
$greeting->userfrom = 1;
$greeting->subject = "Welcome";
$greeting->fullmessage = "Welcome to " . $section->moodle()->fullname;
$greeting->fullmessageformat = FORMAT_MOODLE;
}
return $greeting;
}
}
Going back to the example extension, let's modify the event handler definition:
<?php
require_once $CFG->dirroot . "/blocks/example/classes/lib.php";
abstract class ues_greeting_events {
function greet($user) {
$by_section = array("sectionid" => $user->sectionid);
$greeting = ues_greeting::get_or_default($by_section);
$greeting->userto = $user->userid;
$greeting->component = "moodle";
return $greeting;
}
function ues_student_enroll($params) {
extract($params);
$message = self::greet($ues_user);
message_send($message);
}
}