/** * Helper method user_can_unenrol checks if user can unenrol from selected CI * @param object $data the data object from db query, i.e. user::get_user_course_curriculum() * @param int $cuserid optional ELIS/crlm userid * @param object &$studetrec optional param to return student record if found * @param object &$waitlist optional param to return waitlist record if found * @return bool true if user can unenrol, false otherwise * @uses $DB * @uses $USER */ static function user_can_unenrol($data, $cuserid = 0, &$studentrec = null, &$waitlist = null) { global $DB, $USER; if (!get_config('enrol_elis', 'unenrol_from_course_catalog')) { return false; } if ($data->completionid != STUSTATUS_NOTCOMPLETE || !empty($data->prereqcount) || empty($data->classcount)) { return false; } if (empty($cuserid) && !($cuserid = pm_get_crlmuserid($USER->id))) { return false; } $classes = $DB->get_recordset(pmclass::TABLE, array('courseid' => $data->courseid)); if (!$classes->valid()) { unset($classes); return false; } foreach ($classes as $pmclass) { if ($waitrec = $DB->get_record(waitlist::TABLE, array('classid' => $pmclass->id, 'userid' => $cuserid))) { if ($waitlist !== null) { $waitlist = $waitrec; } break; } if ($sturec = $DB->get_record(student::TABLE, array('classid' => $pmclass->id, 'userid' => $cuserid))) { if ($studentrec !== null) { $studentrec = $sturec; } // check for any grade data if ($sturec->grade && $sturec->grade > 0.0 || $DB->record_exists(student_grade::TABLE, array('classid' => $pmclass->id, 'userid' => $cuserid))) { // error_log("user_can_unenrol: already have grade data ({$sturec->grade}) => false"); unset($classes); return false; } break; // TBD: should only ever be in one class instance (?) } } unset($classes); return true; }
/** * Handle a role assignment by creating an equivalent class enrolment or * instructor assignment (if applicable). */ static function role_assigned($data) { //include dependencies require_once elispm::lib('data/student.class.php'); require_once elispm::lib('data/instructor.class.php'); $student_roleid = get_config('elisprogram_enrolrolesync', 'student_role'); $instructor_roleid = get_config('elisprogram_enrolrolesync', 'instructor_role'); global $DB; $context = $DB->get_record('context', array('id' => $data->contextid)); if (!empty($context) && $context->contextlevel == CONTEXT_ELIS_CLASS) { //assignment is on a PM class instance //need the PM userid to create an association $cmuserid = pm_get_crlmuserid($data->userid); if (!empty($student_roleid) && $data->roleid == $student_roleid) { // add enrolment record $student = new student(); $student->userid = $cmuserid; $student->classid = $context->instanceid; $student->enrolmenttime = time(); try { $student->save(); } catch (Exception $e) { //validation failed } } else { if (!empty($instructor_roleid) && $data->roleid == $instructor_roleid) { // add instructor record $instructor = new instructor(); $instructor->userid = $cmuserid; $instructor->classid = $context->instanceid; $instructor->assigntime = time(); try { $instructor->save(); } catch (Exception $e) { //validation failed } } } } return true; }
/** * Update all PM information for the provided user * * @param int $mdluserid the id of the Moodle user we want to migrate * @return boolean true on success, otherwise false */ function pm_update_user_information($mdluserid) { $status = true; //create the PM user if necessary, regardless of time modified $status = pm_migrate_moodle_users(false, 0, $mdluserid) && $status; //sync enrolments and pass ones with sufficient grades and passed LOs $status = pm_update_student_progress($mdluserid) && $status; $pmuserid = pm_get_crlmuserid($mdluserid); if ($pmuserid != false) { //delete orphaned class - Moodle course associations the user is enrolled in $status = pmclass::check_for_moodle_courses($pmuserid) && $status; //fail users who took to long to complete classes $status = pm_update_student_enrolment($pmuserid) && $status; } return $status; }
/** * Returns an array of cluster ids that are associated to the supplied class through tracks and * the current user has access to enrol users into * * @param int $clsid The class whose association ids we care about * @return int array The array of accessible cluster ids */ public static function get_allowed_clusters($clsid) { global $USER; $context = pm_context_set::for_user_with_capability('cluster', 'local/elisprogram:assign_userset_user_class_instructor', $USER->id); $allowed_clusters = array(); // TODO: Ugly, this needs to be overhauled $cpage = new pmclasspage(); if ($cpage->_has_capability('local/elisprogram:assign_userset_user_class_instructor', $clsid)) { require_once elispm::lib('data/clusterassignment.class.php'); $cmuserid = pm_get_crlmuserid($USER->id); $userclusters = clusterassignment::find(new field_filter('userid', $cmuserid)); foreach ($userclusters as $usercluster) { $allowed_clusters[] = $usercluster->clusterid; } } //we first need to go through tracks to get to clusters $track_listing = new trackassignment(array('classid' => $clsid)); $tracks = $track_listing->get_assigned_tracks(); //iterate over the track ides, which are the keys of the array if (!empty($tracks)) { foreach (array_keys($tracks) as $track) { //get the clusters and check the context against them $clusters = clustertrack::get_clusters($track); $allowed_track_clusters = $context->get_allowed_instances($clusters, 'cluster', 'clusterid'); //append all clusters that are allowed by the available clusters contexts foreach ($allowed_track_clusters as $allowed_track_cluster) { $allowed_clusters[] = $allowed_track_cluster; } } } return $allowed_clusters; }
public function send_notification($message = '', $userto = null, $userfrom = null, $logevent = false) { global $DB, $USER; /// Handle parameters: if (!empty($userto)) { $this->userto = $userto; } else { if (empty($this->userto)) { if (in_cron()) { mtrace(get_string('message_nodestinationuser', 'local_elisprogram')); } else { print_error('message_nodestinationuser', 'local_elisprogram'); } return false; } } if (!empty($userfrom)) { $this->userfrom = $userfrom; } else { if (empty($this->userfrom)) { $this->userfrom = $USER; } } if ($message != '') { $this->fullmessage = $message; } /// Check for the user object type. If a PM User was sent in, need to get the Moodle object. //todo: convert this code to use "is_a" $topmuserid = false; if (get_class($this->userto) == 'user') { $topmuserid = $this->userto->id; if (!($this->userto = $this->userto->get_moodleuser())) { // TODO log here. return false; } } if (empty($this->userto)) { // TODO log here. return false; } if (get_class($this->userfrom) == 'user') { // if (!($this->userfrom = cm_get_moodleuser($this->userfrom->id))) { if (!($this->userfrom = $this->userfrom->get_moodleuser())) { if (in_cron()) { mtrace(get_string('nomoodleuser', 'local_elisprogram')); } else { debugging(get_string('nomoodleuser', 'local_elisprogram')); } } } if (empty($this->userfrom)) { // ELIS-3632: prevent DB errors downstream $this->userfrom = $USER; // TBD } /// Handle unset variables: $this->name = $this->name == '' ? $this->defname : $this->name; $this->subject = $this->subject == '' ? $this->defsubject : $this->subject; //this call performs the core work involved in notifying users pm_notify_send_handler(clone $this); /// Insert a notification log if we have data for it. if ($logevent !== false) { if (!empty($logevent->event)) { $newlog = new Object(); $newlog->event = $logevent->event; if (isset($logevent->userid)) { $newlog->userid = $logevent->userid; } else { if ($topmuserid === false) { $newlog->userid = pm_get_crlmuserid($this->userto->id); } else { $newlog->userid = $topmuserid; } } //if the log entry specifies which user triggered the event, //store that info //NOTE: Do not use $userfrom because that is the message sender //but not necessarily the user whose criteria triggered the event if (isset($logevent->fromuserid)) { $newlog->fromuserid = $logevent->fromuserid; } if (isset($logevent->instance)) { $newlog->instance = $logevent->instance; } if (isset($logevent->data)) { $newlog->data = $logevent->data; } if (isset($logevent->timecreated)) { $newlog->timecreated = $logevent->timecreated; } else { $newlog->timecreated = time(); } $DB->insert_record('local_elisprogram_notifylog', $newlog); } } }
function cluster_profile_update_handler($userdata) { global $DB, $CFG; if (!empty($userdata->deleted)) { return true; } // make sure a CM user exists pm_moodle_user_to_pm($userdata); if (!isset($userdata->id)) { return true; } $cuid = pm_get_crlmuserid($userdata->id); if (empty($cuid)) { // not a curriculum user -- (guest?) return true; } // the cluster assignments that the plugin wants to exist // we figure this out by counting the number of profile fields that the // user has that matches the values for the cluster, and comparing that // with the number of profile values set for the cluster $new_assignments = "(SELECT DISTINCT ? as userid, cp.clusterid\n FROM {" . userset_profile::TABLE . "} cp\n WHERE (SELECT COUNT(*)\n FROM {" . userset_profile::TABLE . "} cp1\n JOIN (SELECT i.fieldid, i.data FROM {user_info_data} i\n WHERE i.userid = ?\n UNION\n SELECT uif.id as fieldid, uif.defaultdata as data\n FROM {user_info_field} uif\n LEFT JOIN {user_info_data} i ON i.userid = ? AND uif.id = i.fieldid\n WHERE i.id IS NULL\n ) inf ON inf.fieldid = cp1.fieldid AND inf.data = cp1.value\n WHERE cp.clusterid=cp1.clusterid)\n = (SELECT COUNT(*) FROM {" . userset_profile::TABLE . "} cp1 WHERE cp.clusterid = cp1.clusterid))"; $new_assignments_params = array($cuid, $userdata->id, $userdata->id); // delete existing assignments that should not be there any more if ($CFG->dbfamily == 'postgres') { $delete = "DELETE FROM {" . clusterassignment::TABLE . "}\n WHERE id IN (\n SELECT id FROM {" . clusterassignment::TABLE . "} a\n LEFT OUTER JOIN {$new_assignments} b ON a.clusterid = b.clusterid AND a.userid = b.userid\n WHERE a.userid = ? AND b.clusterid IS NULL\n ) AND plugin='moodleprofile'"; } else { $delete = "DELETE a FROM {" . clusterassignment::TABLE . "} a\n LEFT OUTER JOIN {$new_assignments} b ON a.clusterid = b.clusterid AND a.userid = b.userid\n WHERE a.userid = ? AND b.clusterid IS NULL AND a.plugin='moodleprofile'"; } $DB->execute($delete, array_merge($new_assignments_params, array($cuid))); // add new assignments $insert = "INSERT INTO {" . clusterassignment::TABLE . "}\n (clusterid, userid, plugin)\n SELECT a.clusterid, a.userid, 'moodleprofile'\n FROM {$new_assignments} a\n LEFT OUTER JOIN {" . clusterassignment::TABLE . "} b ON a.clusterid = b.clusterid AND a.userid = b.userid AND b.plugin='moodleprofile'\n WHERE a.userid = ? AND b.clusterid IS NULL"; $DB->execute($insert, array_merge($new_assignments_params, array($cuid))); clusterassignment::update_enrolments($cuid); return true; }
/** * Find a list of userset folders that the user has access to. * * @param $CFG * @param array $opts The current drop down list to which to add userset folders * @return array Alfresco repository folder names. */ function find_userset_folders(&$opts, $createonly) { global $CFG, $DB, $USER; require_once($CFG->libdir . '/ddllib.php'); // Ensure that the cluster table actually exists before we query it. $manager = $DB->get_manager(); if (!$manager->table_exists('local_elisprogram_uset')) { return false; } if (!file_exists($CFG->dirroot.'/local/elisprogram/plugins/usetclassify/usersetclassification.class.php')) { return false; } require_once($CFG->dirroot.'/local/elisprogram/plugins/usetclassify/usersetclassification.class.php'); require_once($CFG->dirroot.'/local/elisprogram/lib/data/userset.class.php'); // Get cm userid $cmuserid = pm_get_crlmuserid($USER->id); $timenow = time(); $capability = 'repository/elisfiles:viewusersetcontent'; $child_path = $DB->sql_concat('c_parent.path', "'/%'"); $like = $DB->sql_like('c.path',':child_path'); // Select clusters and sub-clusters for the current user // to which they have the vieworganization capability $sql = "SELECT DISTINCT clst.id AS instanceid FROM {local_elisprogram_uset} clst WHERE EXISTS ( SELECT 'x' FROM {context} c JOIN {context} c_parent ON {$like} OR c.path = c_parent.path JOIN {role_assignments} ra ON ra.contextid = c_parent.id AND ra.userid = :muserid JOIN {role_capabilities} rc ON ra.roleid = rc.roleid WHERE c_parent.instanceid = clst.id AND c.contextlevel = :contextlevelnum AND c_parent.contextlevel = :contextlevelnum2 AND rc.capability = :capability ) OR EXISTS ( SELECT 'x' FROM {local_elisprogram_uset_asign} ca WHERE ca.clusterid = clst.id AND ca.userid = :cmuserid) "; $params = array( 'capability' => $capability, 'child_path' => $child_path, 'contextlevelnum' => CONTEXT_ELIS_USERSET, 'contextlevelnum2' => CONTEXT_ELIS_USERSET, 'muserid' => $USER->id, 'cmuserid' => $cmuserid ); $viewable_clusters = $DB->get_recordset_sql($sql, $params); // Get user clusters $cluster_info = array(); if ($viewable_clusters) { foreach ($viewable_clusters as $cluster) { if (!$new_cluster_info = $this->load_cluster_info($cluster->instanceid)) { continue; } else { $cluster_info[$cluster->instanceid] = $new_cluster_info; } } } else { return false; } if (empty($cluster_info)) { return false; } // There may be multiple clusters that this user is assigned to... foreach ($cluster_info as $cluster) { // Get the extra cluster data and ensure it is present before proceeding. $clusterdata = usersetclassification::get_for_cluster($cluster); if (empty($clusterdata->params)) { continue; } $clusterparams = unserialize($clusterdata->params); // Make sure this cluster has the Alfresco shared folder property defined if (empty($clusterparams['elis_files_shared_folder'])) { continue; } // Make sure we can get the storage space from Alfresco for this userset. if (!$uuid = $this->get_userset_store($cluster->id)) { continue; } // Add to opts array $cluster_context = \local_elisprogram\context\userset::instance($cluster->id); $system_context = context_system::instance(); $viewalfuserset = has_capability('repository/elisfiles:viewusersetcontent', $cluster_context) || has_capability('repository/elisfiles:viewsitecontent', $system_context); $editalfuserset = has_capability('repository/elisfiles:createusersetcontent', $cluster_context) || has_capability('repository/elisfiles:createsitecontent', $system_context); if ($editalfuserset) { if (!elis_files_has_permission($uuid, $USER->username, true)) { $this->allow_edit($USER->username, $uuid); } } else if ($viewalfuserset) { // viewalfuserset if (!elis_files_has_permission($uuid, $USER->username)) { $this->allow_read($USER->username, $uuid); } } if ((!$createonly && ($editalfuserset || $viewalfuserset)) || ($createonly && $editalfuserset)) { $params = array('path'=>$uuid, 'shared'=>(boolean)0, 'oid'=>(int)$cluster->id, 'cid'=>0, 'uid'=>0); $encodedpath = base64_encode(serialize($params)); $unbiasedparams = array( 'path' => $uuid, 'shared' => false, 'oid' => 0, 'cid' => 0, 'uid' => 0 ); $unbiasedpath = base64_encode(serialize($unbiasedparams)); $opts[] = array('name'=> $cluster->name, 'path'=> $encodedpath, 'unbiasedpath'=> $unbiasedpath ); } } return true; }
function can_do_edit() { global $USER; $id = $this->required_param('id', PARAM_INT); return $this->_has_capability('local/elisprogram:class_edit') || instructor::user_is_instructor_of_class(pm_get_crlmuserid($USER->id), $id); }