Skip to content
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 the Table

Create a enrol_ues_greetings table with the following fields:

  1. id -> int(11)
  2. subject -> varchar(255)
  3. fullmessage -> text
  4. fullmessageformat -> varchar(20)
  5. sectionid -> int(11) foreign key (enrol_ues_sections -> id)
  6. userfrom -> int(11) foreign key (user -> id)

Create the DAO

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;
  }
}

Using the DAO

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);
  }
}
Clone this wiki locally