send a notification when entering and leaving a course

send a notification when entering and leaving a course

by Jose Chapa -
Number of replies: 0

Hello

I need to send a notification to an external site to inform the students' attendance to the course, when they enter and when they leave a course

like this: http://elearning.sence.cl/api/CursoApi?username=studentusername&courseid=4231233213

version: moodle 3.4.2

i edit a code to do it, and call it with

include_once ("$CFG->libdir/sence.lib.php");
$kk = registerSence($courseid,$USER->id,$CFG->sence_pass);

in moodlelib.php, but I'm not sure that the call is right on the line 2713 before "return;"

// Set accesstime or the user will appear offline which messes up messaging.
        user_accesstime_log($course->id);
        return;

thanks

sence.lib.php file is:

function registerSence ($moodle_course_id,$moodle_user_id,$sence_pass) {

    global $CFG, $SESSION;
    $status = true;
    if (!isset($SESSION->sence[$moodle_course_id])) {
        global $DB;
        $course_rec = $DB->get_records("course", array('id'=>$moodle_course_id));
        $user_rec = $DB->get_records("user",array('id'=>$moodle_user_id));
        if ($course_rec && $user_rec) {
            $host = "elearning.sence.cl";
            $method = "GET";
            $path = "/api/CursoApi";
            $data = "rutAlumno=".$user_rec->username."&claveAlumno=".$user_rec->idnumber."&rutOtec=999999999&claveOtec=999999999&codigoSence=".$course_rec->idnumber."&estadoActividad=1";
            $status = sendUrl($host,$method,$path,$data);
            if ($CFG->debug > 7) {
                echo "Invocada URL: ".$host.$path."?".$data."<br>";
                echo "Respuesta: ".$status;
            }
        }
        $SESSION->sence[$moodle_course_id] = true;
    }
    return $status;
}

function unregisterSence ($moodle_user_id,$sence_pass) {

    global $CFG, $SESSION;
    $status = true;
    if (isset($SESSION->sence)) {
        foreach($SESSION->sence as $key => $value) {
            if ($value) {
                $course_rec = $DB->get_records("course", array('id'=>$moodle_course_id));
        $user_rec = $DB->get_records("user",array('id'=>$moodle_user_id));
                if ($course_rec && $user_rec) {
                    $host = "elearning.sence.cl";
                    $method = "GET";
                    $path = "/api/CursoApi";
                    $data = "rutAlumno=".$user_rec->username."&claveAlumno=".$user_rec->idnumber."&rutOtec=76301845&claveOtec=otecmr2014&codigoSence=".$course_rec->idnumber."&estadoActividad=2";
                    $status = sendUrl($host,$method,$path,$data);
                    if ($CFG->debug > 7) {
                        echo "Invocada URL: ".$host.$path."?".$data."<br>";
                        echo "Respuesta: ".$status;
                    }
                }
            }
        }
        return $status;
    }
}

function sendUrl($host,$method,$path,$data,$useragent=0) {

    $buf = "";
   
    // Supply a default method of GET if the one passed was empty
    if (empty($method)) {
        $method = 'GET';
    }
    $method = strtoupper($method);
    $fp = fsockopen($host, 80);
    if ($method == 'GET') {
      $path .= '?' . $data;
    }
    fputs($fp, "$method $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    if ($useragent) {
      fputs($fp, "User-Agent: MSIE\r\n");
    }   
    if ($method == 'POST') {
      fputs($fp,"Content-type: application/x-www-form-urlencoded\r\n");
      fputs($fp, "Content-length: " . strlen($data) . "\r\n");
    }
    fputs($fp, "Connection: close\r\n\r\n");
    if ($method == 'POST') {
        fputs($fp, $data);
    }

    while (!feof($fp)) {
        $buf .= fgets($fp,128);
    }
    fclose($fp);
    return $buf;
}

Average of ratings: -