// Get the list of videos
require_once 'require_openveo.php';
use Openveo\Client\Client as OpenveoClient;
// Retrieve block configuration
$wsserverhost = get_config('openveo_videos', 'wsserverhost');
$wsserverport = get_config('openveo_videos', 'wsserverport');
$clientid = get_config('openveo_videos', 'wsclientid');
$clientsecret = get_config('openveo_videos', 'wsclientsecret');
$properties;
$choices = array();
try {
    $url = 'http://' . $wsserverhost . ':' . $wsserverport . '/publish/properties';
    // Make an authentication to the OpenVeo Web Service
    $client = new OpenveoClient($clientid, $clientsecret, $wsserverhost, $wsserverport);
    // Get all videos associated to the course
    $response = $client->get($url);
    if (isset($response->entities)) {
        $properties = $response->entities;
        for ($i = 0; $i < count($properties); $i++) {
            $choices[$properties[$i]->id] = $properties[$i]->name;
        }
    }
} catch (Exception $e) {
}
// Application configuration fieldset
$settings->add(new admin_setting_heading('openveo_videos/headerconfig', get_string('genconfheader', 'block_openveo_videos'), get_string('genconfdesc', 'block_openveo_videos')));
// Server host
$settings->add(new admin_setting_configtext('openveo_videos/serverhost', get_string('genconfserverhostlabel', 'block_openveo_videos'), get_string('genconfserverhostdesc', 'block_openveo_videos'), '', PARAM_RAW));
// Server port
$settings->add(new admin_setting_configtext('openveo_videos/serverport', get_string('genconfserverportlabel', 'block_openveo_videos'), get_string('genconfserverportdesc', 'block_openveo_videos'), 3000, PARAM_INT));
// Web service configuration fieldset
 /**
  * Sets block content (title, text and footer).
  *
  * If current user does not have the permission to visualize the bloc it won't be displayed
  *
  * @return StdClass A PHP object with title, text and footer properties
  */
 public function get_content()
 {
     global $COURSE, $CFG, $DB;
     // Content already generated
     if ($this->content !== NULL) {
         return $this->content;
     }
     $this->content = null;
     $courseid = $COURSE->id;
     $context = context_course::instance($courseid);
     // Checks if user has the permission to see the block and its
     // content
     $isEnrolled = is_enrolled($context);
     $hasCapabilityToEdit = has_capability('block/openveo_videos:edit', $context);
     // User has the permission to see the block
     // All enrolled users can see the block
     // If a user is not enrolled, he can not see the block unless he can edit it
     if (!empty($courseid) && ($isEnrolled || $hasCapabilityToEdit)) {
         $this->content = new StdClass();
         // Retrieve block configuration
         $serverhost = get_config('openveo_videos', 'wsserverhost');
         $serverport = get_config('openveo_videos', 'wsserverport');
         $clientid = get_config('openveo_videos', 'wsclientid');
         $clientsecret = get_config('openveo_videos', 'wsclientsecret');
         $videoproperty = get_config('openveo_videos', 'videoproperty');
         try {
             $param = ['sortBy' => 'date', 'sortOrder' => 'asc', 'states' => 12, 'properties' => [$videoproperty => $COURSE->idnumber]];
             $query = http_build_query($param, '', '&');
             $url = 'http://' . $serverhost . ':' . $serverport . '/publish/videos?' . $query;
             // Make an authentication to the OpenVeo Web Service
             $client = new OpenveoClient($clientid, $clientsecret, $serverhost, $serverport);
             $response = $client->get($url);
             $validatedvideos = $DB->get_records('block_openveo_videos', array('isvalidated' => 1, 'courseid' => $COURSE->idnumber));
             // Got a list of videos
             if (isset($response->entities) && !empty($response->entities)) {
                 $videos = $response->entities;
                 $videovalidated = false;
                 $video = null;
                 // There is, at least, one validated video
                 if (!empty($validatedvideos)) {
                     // Iterate through videos
                     for ($i = 0; $i < sizeof($videos); $i++) {
                         // Search video in validated videos
                         foreach ($validatedvideos as $validatedvideo) {
                             if ($validatedvideo->isvalidated == 1 && $validatedvideo->videoid === $videos[$i]->id) {
                                 $video = $videos[$i];
                                 $videovalidated = true;
                                 break;
                             }
                         }
                         if ($videovalidated) {
                             break;
                         }
                     }
                 }
                 // Url for the list of videos associated to this course id
                 $videosurl = $CFG->wwwroot . '/blocks/openveo_videos/view.php?courseid=' . $courseid;
                 if (!empty($video) && $videovalidated) {
                     // Got a video for the block
                     // Display block
                     // Path to the video
                     $videopath = $CFG->wwwroot . '/blocks/openveo_videos/player.php?courseid=' . $courseid . '&videoid=' . $video->id;
                     // Video thumbnail
                     $thumbnailpath = isset($video->thumbnail) ? $video->thumbnail : null;
                     // Build video date
                     $videomoodledate = usergetdate($video->date / 1000);
                     $videodate = new StdClass();
                     $videodate->day = $videomoodledate['mday'] < 10 ? '0' . $videomoodledate['mday'] : $videomoodledate['mday'];
                     $videodate->month = $videomoodledate['mon'] < 10 ? '0' . $videomoodledate['mon'] : $videomoodledate['mon'];
                     $videodate->year = $videomoodledate['year'];
                     // Build content
                     $this->content->text = $this->render_block($video->title, $video->description, $videodate, $videosurl, $videopath, $thumbnailpath, $videovalidated);
                 } else {
                     if ($hasCapabilityToEdit) {
                         // No video for the block but there are videos associated to the course
                         // Display an empty block with a link to the list of videos
                         $this->content->text = $this->render_block(null, null, null, $videosurl);
                     }
                 }
             }
         } catch (Exception $e) {
             // TODO Log the error
         }
     }
     return $this->content;
 }