diff --git a/block_panopto.php b/block_panopto.php index 0095e30..3c6d260 100644 --- a/block_panopto.php +++ b/block_panopto.php @@ -76,7 +76,6 @@ public function instance_allow_config() { * @param bool $nolongerused depcrecated variable */ public function instance_config_save($data, $nolongerused = false) { - if (!empty($data->course)) { panopto_data::set_panopto_course_id($this->page->course->id, $data->course); @@ -123,6 +122,7 @@ public function cron() { $panoptodata->moodlecourseid = $course->moodleid; $provisioningdata = $panoptodata->get_provisioning_info(); $provisioneddata = $panoptodata->provision_course($provisioningdata); + if (!empty($provisioneddata)) { mtrace("Successfully provisioned course for $provisioneddata->ExternalCourseID"); } else { diff --git a/classes/rollingsync.php b/classes/rollingsync.php index 06704b1..da7448a 100644 --- a/classes/rollingsync.php +++ b/classes/rollingsync.php @@ -44,52 +44,6 @@ */ class block_panopto_rollingsync { - /** - * Called when a user has been logged in as. - * - * @param \core\event\user_loggedinas $event - */ - public static function userloggedinas(\core\event\user_loggedinas $event) { - if (!\panopto_data::is_main_block_configured() || - !\panopto_data::has_minimum_version()) { - return; - } - - $task = new \block_panopto\task\sync_user(); - $task->set_custom_data(array( - 'userid' => $event->relateduserid - )); - - if (get_config('block_panopto', 'async_tasks')) { - \core\task\manager::queue_adhoc_task($task); - } else { - $task->execute(); - } - } - - /** - * Called when a user has been logged in. - * - * @param \core\event\user_loggedin $event - */ - public static function userloggedin(\core\event\user_loggedin $event) { - if (!\panopto_data::is_main_block_configured() || - !\panopto_data::has_minimum_version()) { - return; - } - - $task = new \block_panopto\task\sync_user(); - $task->set_custom_data(array( - 'userid' => $event->userid - )); - - if (get_config('block_panopto', 'async_tasks')) { - \core\task\manager::queue_adhoc_task($task); - } else { - $task->execute(); - } - } - /** * Called when a course has been created. * diff --git a/classes/task/sync_user.php b/classes/task/sync_user.php deleted file mode 100644 index b867cff..0000000 --- a/classes/task/sync_user.php +++ /dev/null @@ -1,54 +0,0 @@ -. - -/** - * the sync user class for Panopto, syncs user permissions with Panopto server, called on login - * - * @package block_panopto - * @copyright Panopto 2009 - 2016 - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -namespace block_panopto\task; - -defined('MOODLE_INTERNAL') || die(); - -require_once(dirname(__FILE__) . '/../../lib/panopto_data.php'); - -/** - * Panopto "sync user" task. - * @copyright Panopto 2009 - 2016 /With contributions - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class sync_user extends \core\task\adhoc_task { - - /** - * the the parent component for this class - */ - public function get_component() { - return 'block_panopto'; - } - - /** - * the main execution function of the class - */ - public function execute() { - global $DB; - - $eventdata = (array) $this->get_custom_data(); - \panopto_data::sync_external_user($eventdata['userid']); - } -} diff --git a/db/events.php b/db/events.php index 43ec248..d1c93ce 100644 --- a/db/events.php +++ b/db/events.php @@ -26,14 +26,6 @@ $observers = array( // User enrolled event. - array( - 'eventname' => '\core\event\user_loggedinas', - 'callback' => 'block_panopto_rollingsync::userloggedinas', - ), - array( - 'eventname' => '\core\event\user_loggedin', - 'callback' => 'block_panopto_rollingsync::userloggedin', - ), array( 'eventname' => '\core\event\course_created', 'callback' => 'block_panopto_rollingsync::coursecreated', diff --git a/db/upgrade.php b/db/upgrade.php index 3821352..1d60806 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -288,7 +288,9 @@ function xmldb_block_panopto_upgrade($oldversion = 0) { update_upgrade_progress($currindex, $totalupgradesteps, $upgradestep); $panoptocourseobjects = array(); - $errorstring = get_string('upgrade_provision_access_error', 'block_panopto'); + + $getunamepanopto = new panopto_data(null); + $errorstring = get_string('upgrade_provision_access_error', 'block_panopto', $getunamepanopto->panopto_decorate_username($getunamepanopto->uname)); $versionerrorstring = get_string('upgrade_panopto_required_version', 'block_panopto'); $usercanupgrade = true; @@ -306,8 +308,7 @@ function xmldb_block_panopto_upgrade($oldversion = 0) { isset($oldpanoptocourse->panopto->applicationkey) && !empty($oldpanoptocourse->panopto->applicationkey); if ($moodlecourseexists && $hasvalidpanoptodata) { - if (isset($oldpanoptocourse->panopto->uname) && !empty($oldpanoptocourse->panopto->uname) && - $oldpanoptocourse->panopto->uname !== 'guest') { + if (isset($oldpanoptocourse->panopto->uname) && !empty($oldpanoptocourse->panopto->uname)) { $oldpanoptocourse->panopto->ensure_auth_manager(); $activepanoptoserverversion = $oldpanoptocourse->panopto->authmanager->get_server_version(); if (!version_compare($activepanoptoserverversion, \panopto_data::$requiredpanoptoversion, '>=')) { diff --git a/lang/en/block_panopto.php b/lang/en/block_panopto.php index 8dd9dd1..0cd8c0e 100644 --- a/lang/en/block_panopto.php +++ b/lang/en/block_panopto.php @@ -47,16 +47,12 @@ $string['block_panopto_non_editing_teacher_provision_desc'] = 'Enable this option to allow non-editing teachers to provision Panopto course folders.'; $string['block_panopto_auto_sync_imports'] = 'Automatically grant permissions when importing a course'; $string['block_panopto_auto_sync_imports_desc'] = 'Enable this option to allow Panopto to automatically grant viewer permissions when importing a course.'; -$string['block_panopto_auto_add_admins'] = 'Grant creator permissions to Moodle admins for all Panopto course folders'; -$string['block_panopto_auto_add_admins_desc'] = 'Enable this option to allow Panopto to automatically grant creator permissions to Moodle admins to all Panopto folders that are provisioned for Moodle courses. (changes take effect on User login).'; $string['block_global_hostname'] = 'Panopto Server Hostname'; $string['block_global_hostname_desc'] = 'Enter the FQDN of your Panopto server.'; $string['block_global_instance_desc'] = 'Enter the Instance Name from the Panopto Identity Providers page.'; $string['block_global_instance_name'] = 'Moodle Instance Name'; $string['block_panopto_server_number_desc'] = 'Click \'Save Changes\' to update number of servers.'; $string['block_panopto_server_number_name'] = 'Number of Panopto Servers'; -$string['block_panopto_async_tasks'] = 'Asynchronous enrollment sync'; -$string['block_panopto_async_tasks_desc'] = 'Enable this option to allow the sync user task that happens during login to run in the background. Check this option if the login operation is taking longer than desired.'; $string['block_panopto_auto_provision'] = 'Automatically provision newly created courses'; $string['block_panopto_auto_provision_desc'] = 'Enable this option to automatically provision a Panopto course folder when a course is created.'; $string['block_panopto_prefix_new_folder_shortnames'] = 'Prefix new Panopto folder display names with the Moodle course\'s shortname.'; @@ -121,6 +117,7 @@ $string['select_server'] = 'Select a Panopto server'; $string['select_server_help'] = 'Choose the Panopto server where the course folders will be provisioned.'; $string['server_name'] = 'Server name'; +$string['server_not_available'] = 'The Panopto server {$a} was not available. Server may be down'; $string['server_info_not_valid'] = 'The server name or application key are not valid, below are attempted values.'; $string['show_all'] = 'Show All'; $string['show_less'] = 'Show Less'; @@ -129,7 +126,7 @@ $string['unconfigured'] = 'Global configuration incomplete. Please contact your system administrator.'; $string['unknown_provisioning_error'] = "An unknown error has occurred."; $string['unprovisioned'] = 'This course has not yet been provisioned.'; -$string['upgrade_provision_access_error'] = 'UPGRADE BLOCKED: The user does not have access to a provisioned Panopto course folder. Upgrading user must have at least viewer access to all Panopto course folders. It is highly reccommended that the upgrading user is an Administrator on Panopto.'; +$string['upgrade_provision_access_error'] = 'UPGRADE BLOCKED: The user {$a} does not have access to a provisioned Panopto course folder. Upgrading user must have at least viewer access to all Panopto course folders. It is highly reccommended that the upgrading user is an Administrator on Panopto.'; $string['upgrade_panopto_required_version'] = 'A Panopto server you are using does not meet the minimum required version to support this version of the Moodle Panopto Block. The upgrade will be blocked from succeeding until all Panopto servers in use meet miniumum version requirements.'; $string['watch_live'] = 'Watch Live'; /* End of file block_panopto.php */ diff --git a/lib/panopto_data.php b/lib/panopto_data.php index b4b470f..9640963 100644 --- a/lib/panopto_data.php +++ b/lib/panopto_data.php @@ -279,9 +279,6 @@ public function provision_course($provisioninginfo) { $provisioninginfo->externalcourseid ); - // Sync access of the privisioning user so they don't need to relog to view the folder. - self::sync_external_user($USER->id); - $this->ensure_auth_manager(); $currentblockversion = $DB->get_record( @@ -297,6 +294,11 @@ public function provision_course($provisioninginfo) { $CFG->version ); + // uname will be guest is provisioning/upgrading through cli, no need to sync this 'user'. + if ($this->uname !== 'guest') { + // Update permissions so user can see everything they should. + $this->sync_external_user($USER->id); + } } else { // Give the user some basic info they can use to debug or send to AE. $courseinfo = new stdClass; @@ -444,9 +446,17 @@ public function init_and_sync_import($newimportid) { return $importinfo; } + /** + * Attempts to get a folder by it's external id + * + */ public function get_folders_by_external_id() { + global $USER; $ret = false; + // Update permissions so user can see everything they should. + $this->sync_external_user($USER->id); + if (isset($this->sessiongroupid)) { $this->ensure_session_manager(); @@ -457,9 +467,18 @@ public function get_folders_by_external_id() { return $ret; } + /** + * Attempts to get a folder by it's public Guid + * + */ public function get_folders_by_id() { + global $USER; + $ret = false; + // Update permissions so user can see everything they should. + $this->sync_external_user($USER->id); + if (isset($this->sessiongroupid)) { $this->ensure_session_manager(); @@ -474,9 +493,18 @@ public function get_folders_by_id() { return $ret; } + /** + * Attempts to get all folders the user has access to. + * + */ public function get_folders_list() { + global $USER; $ret = false; + + // Update permissions so user can see everything they should. + $this->sync_external_user($USER->id); + if (isset($this->sessiongroupid)) { $this->ensure_session_manager(); @@ -487,162 +515,66 @@ public function get_folders_list() { } /** - * Sync a user with all of the courses he is enrolled in + * Sync a user with all of the courses he is enrolled in on the current Panopto server * - * @param int $userid the Moodle id of the user we are trying to sync. */ - public static function sync_external_user($userid) { - global $DB, $CFG, $USER; - - if (isset($USER->username)) { - $username = $USER->username; - $userisguest = false; - } else { - $userisguest = true; - $username = 'guest'; - } - - $setupasadmin = false; - $servergroupidlist = array(); - $instancename = get_config('block_panopto', 'instance_name'); - - $numservers = get_config('block_panopto', 'server_number'); - $numservers = isset($numservers) ? $numservers : 0; - - // Increment numservers by 1 to take into account starting at 0. - ++$numservers; - - for ($serverwalker = 1; $serverwalker <= $numservers; ++$serverwalker) { - - // Generate strings corresponding to potential servernames in the config. - $thisservername = get_config('block_panopto', 'server_name' . $serverwalker); - $thisappkey = get_config('block_panopto', 'application_key' . $serverwalker); + public function sync_external_user($userid) { + global $DB, $CFG; - $hasservername = !(!isset($thisservername) || trim($thisservername) === ''); - $hasappkey = !(!isset($thisappkey) || trim($thisappkey) === ''); - - if ($hasservername && $hasappkey) { - $panoptodataobj = new panopto_data(null); - $panoptodataobj->uname = $username; - $panoptodataobj->servername = $thisservername; - $panoptodataobj->applicationkey = $thisappkey; - - $servergroupidlist[$panoptodataobj->servername] = array( - 'panopto' => $panoptodataobj, - 'externalgroupids' => array() - ); - } - } - - if (get_config('block_panopto', 'auto_add_admins') && !$userisguest) { - // All Moodle administrators should get access to all Panopto course folders as teachers, since they can all provision in the config page. - // Whitespace after id is required. - $sql = "SELECT id " . - "FROM {user} " . - "WHERE"; - $siteadmins = explode(",", $CFG->siteadmins); - list($usql, $params) = $DB->get_in_or_equal($siteadmins); - $sql .= " id $usql "; - $superadmins = $DB->get_records_sql($sql, $params); - foreach ($superadmins as $possibleuser) { - if ($possibleuser->id === $userid) { - $setupasadmin = true; - break; - } - } - } - - if ($setupasadmin) { - $coursestosync = $DB->get_records( - 'block_panopto_foldermap', - null, - 'moodleid' - ); + $userinfo = $DB->get_record('user', array('id' => $userid)); - $currentcourses = array(); - foreach ($coursestosync as $course) { - $moodlecourse = $DB->get_record('course', array('id' => $course->moodleid)); + // Only sync if we find an existing user with the given id. + if (isset($userinfo) && ($userinfo !== false)) { + $instancename = get_config('block_panopto', 'instance_name'); - if (isset($moodlecourse) && !empty($moodlecourse) && $moodlecourse !== false) { - $courseobj = new stdClass; - $courseobj->id = $course->moodleid; - $currentcourses[] = $courseobj; - } else { - // The course does not exist in Moodle, move it to the old table for cleanup. - error_log(get_string('moodle_course_not_exist', 'block_panopto')); - self::delete_panopto_relation($course->moodleid, true); - } - } - } else { $currentcourses = enrol_get_users_courses($userid, true); - } - // Go through each course. - foreach ($currentcourses as $course) { - $coursecontext = context_course::instance($course->id); - $coursegroups = array(); + // Go through each course. + $groupstosync = array(); + foreach ($currentcourses as $course) { + $coursecontext = context_course::instance($course->id); - $coursepanopto = new panopto_data($course->id); + $coursepanopto = new panopto_data($course->id); - // Check to see if we are already going to provision a specific Panopto server, if we are just add the groups to the already made array - // If not add the server to the list of servers. - if (isset($coursepanopto->servername) && !empty($coursepanopto->servername) && - isset($coursepanopto->applicationkey) && !empty($coursepanopto->applicationkey) && - isset($coursepanopto->sessiongroupid) && !empty($coursepanopto->sessiongroupid)) { + // Check to see if we are already going to provision a specific Panopto server, if we are just add the groups to the already made array + // If not add the server to the list of servers. + if (isset($coursepanopto->servername) && !empty($coursepanopto->servername) && $coursepanopto->servername === $this->servername && + isset($coursepanopto->applicationkey) && !empty($coursepanopto->applicationkey) && + isset($coursepanopto->sessiongroupid) && !empty($coursepanopto->sessiongroupid)) { - $role = self::get_role_from_context($coursecontext, $userid); + $role = self::get_role_from_context($coursecontext, $userid); - // If the admin is already a creator or publisher do nothing. - if ($setupasadmin && $role === 'Viewer') { - $role = 'Creator'; - } + // Build a list of ExternalGroupIds using a specific format. + // E.g. moodle31:courseId_viewers/moodle31:courseId_creators. + $groupname = $coursepanopto->instancename . ':' . $course->id; + if (strpos($role, 'Viewer') !== false) { + $groupstosync[] = $groupname . "_viewers"; + } - // Build a list of ExternalGroupIds using a specific format. - // E.g. moodle31:courseId_viewers/moodle31:courseId_creators. - $groupname = $coursepanopto->instancename . ':' . $course->id; - if (strpos($role, 'Viewer') !== false) { - $coursegroups[] = $groupname . "_viewers"; - } + if (strpos($role, 'Creator') !== false) { + $groupstosync[] = $groupname . "_creators"; + } - if (strpos($role, 'Creator') !== false) { - $coursegroups[] = $groupname . "_creators"; - } - - if (strpos($role, 'Publisher') !== false) { - $coursegroups[] = $groupname . "_publishers"; - } - - if (!array_key_exists($coursepanopto->servername, $servergroupidlist)) { - $servergroupidlist[$coursepanopto->servername] = array( - 'panopto' => $coursepanopto, - 'externalgroupids' => array_merge(array(), $coursegroups) - ); - } else { - $servergroupidlist[$coursepanopto->servername]['externalgroupids'] = array_merge( - $servergroupidlist[$coursepanopto->servername]['externalgroupids'], - $coursegroups - ); + if (strpos($role, 'Publisher') !== false) { + $groupstosync[] = $groupname . "_publishers"; + } } } - } - $userinfo = $DB->get_record('user', array('id' => $userid)); - // For each Panopto server that has groups we need to provision. - foreach ($servergroupidlist as $servername => $servergroup) { // Only try to sync the users if he Panopto server is up. - if (is_server_alive('https://' . $servername . '/Panopto') || is_server_alive('http://' . $servername . '/Panopto')) { + if (self::is_server_alive('https://' . $this->servername . '/Panopto')) { - $servergroup['panopto']->ensure_user_manager(); + $this->ensure_user_manager(); - $servergroup['panopto']->usermanager->sync_external_user( + $this->usermanager->sync_external_user( $userinfo->firstname, $userinfo->lastname, $userinfo->email, - $servergroup['externalgroupids'] + $groupstosync ); } else { - error_log(get_string('panopto_server_error', 'block_panopto', $servername)); + error_log(get_string('panopto_server_error', 'block_panopto', $this->servername)); } } @@ -1082,6 +1014,7 @@ public static function delete_panopto_relation($moodlecourseid, $movetoinactivet * Get list of available courses from db based on user's access level on course. */ public function get_course_options() { + $panoptocourses = $this->get_folders_list(); if (!empty($panoptocourses)) { $options = array(); @@ -1233,27 +1166,27 @@ public static function clear_capabilities_for_course($courseid) { $coursecontext->mark_dirty(); } } -} -function is_server_alive($url = null) { - if ($url == null) { - return false; - } - $ch = curl_init($url); + public static function is_server_alive($url = null) { + if ($url == null) { + return false; + } + $ch = curl_init($url); - curl_setopt($ch, CURLOPT_TIMEOUT, 10); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $data = curl_exec($ch); - $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $data = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); + curl_close($ch); - if (($httpcode >= 200 && $httpcode < 300) || $httpcode == 302) { - return true; - } else { - return false; + if (($httpcode >= 200 && $httpcode < 300) || $httpcode == 302) { + return true; + } else { + return false; + } } } /* End of file panopto_data.php */ diff --git a/panopto_content.php b/panopto_content.php index 0612264..1b02298 100644 --- a/panopto_content.php +++ b/panopto_content.php @@ -53,7 +53,7 @@ $content->text = get_string('unprovisioned', 'block_panopto'); if ($panoptodata->can_user_provision($courseid)) { - $content->text .= '

' . + $content->text .= '
' . "" . get_string('provision_course_link_text', 'block_panopto') . ''; } @@ -66,9 +66,10 @@ try { if (!$panoptodata->sessiongroupid) { $content->text = get_string('no_course_selected', 'block_panopto'); + } else if (!\panopto_data::is_server_alive('https://' . $panoptodata->servername . '/Panopto')) { + error_log(get_string('server_not_available', 'block_panopto', $panoptodata->servername)); + $content->text .= "" . get_string('error_retrieving', 'block_panopto') . ''; } else { - // Get course info from SOAP service. - // We can get by external_id but there is no point because atm it calls this method redundantly anyway. $courseinfo = $panoptodata->get_folders_by_id(); @@ -212,7 +213,7 @@ } } } catch (Exception $e) { - $content->text .= "

" . get_string('error_retrieving', 'block_panopto') . ''; + $content->text .= "
" . get_string('error_retrieving', 'block_panopto') . ''; } $content->footer = ''; diff --git a/provision_course_internal.php b/provision_course_internal.php index d1cc9a9..48c9461 100644 --- a/provision_course_internal.php +++ b/provision_course_internal.php @@ -75,8 +75,7 @@ class panopto_provision_form extends moodleform { */ public function definition() { - global $DB; - global $aserverarray; + global $DB, $aserverarray; $mform = & $this->_form; diff --git a/settings.php b/settings.php index dd533d8..45a7dad 100644 --- a/settings.php +++ b/settings.php @@ -72,15 +72,6 @@ ) ); } - - $settings->add( - new admin_setting_configcheckbox( - 'block_panopto/async_tasks', - get_string('block_panopto_async_tasks', 'block_panopto'), - get_string('block_panopto_async_tasks_desc', 'block_panopto'), - 1 - ) - ); $settings->add( new admin_setting_configcheckbox( 'block_panopto/prefix_new_folder_names', @@ -105,14 +96,6 @@ 1 ) ); - $settings->add( - new admin_setting_configcheckbox( - 'block_panopto/auto_add_admins', - get_string('block_panopto_auto_add_admins', 'block_panopto'), - get_string('block_panopto_auto_add_admins_desc', 'block_panopto'), - 1 - ) - ); $systemcontext = context_system::instance(); $systemrolearray = get_assignable_roles($systemcontext, ROLENAME_BOTH); diff --git a/version.php b/version.php index a919c8d..2ace1d2 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ // Plugin version should normally be the same as the internal version. // If an admin wants to install with an older version number, however, set that here. -$plugin->version = 2017071000; +$plugin->version = 2017080100; // Requires this Moodle version - 2.7. $plugin->requires = 2014051200;