/** * Generate certificate codes for each applicable user and insert a record in in the certificate_issued table * @param int $certsettingid Certificate setting id (foreign key) * @param recordset $users A record set of users eligible to recevie certificates * @param object $dataclass local_elisprogram_certissued data ojbect * @return bool True if the record was inserted or false is something went wrong */ function pm_issue_user_certificate($certsettingid, $users, $dataclass) { $data = new stdClass(); $time = time(); if (empty($certsettingid)) { if (debugging('', DEBUG_ALL)) { error_log('local/elisprogram/lib/lib.php::pm_issue_user_certificate() - certificate setting is empty '); } return false; } if (!$dataclass instanceof certificateissued) { if (debugging('', DEBUG_ALL)) { error_log('local/elisprogram/lib/lib.php::pm_issue_user_certificate() - data_class is not an instance of certificateissued'); } return false; } foreach ($users as $user) { /* Initalize the data for the data class */ $code = cm_certificate_get_code(); $data->cm_userid = $user->userid; $data->cert_setting_id = $certsettingid; $data->cert_code = $code; $data->timeissued = $user->completetime; $data->timecreated = $time; $dataclass->set_from_data($data); $dataclass->save(); /* Unset the id field to force an insert operation */ unset($dataclass->id); } return true; }
/** * Perform all actions to mark this student record complete. * * @param mixed $time Student's curriculum completion time (ignored if equal to FALSE) * @param mixed $credits The number of credits awarded (ignored if false) * @param boolean $locked TRUE if the curriculum enrolment should be locked, otherwise false */ function complete($time = false, $credits = false, $locked = false) { global $CFG; require_once elispm::lib('notifications.php'); $this->completed = STUSTATUS_PASSED; if ($time !== false) { $this->timecompleted = $time; } if ($this->timecompleted <= 0 || !is_numeric($this->timecompleted)) { $this->timecompleted = time(); } // Check for $this->curriculum and create it if it is not included if (!isset($this->curriculum) && isset($this->curriculumid)) { $this->curriculum = new curriculum($this->curriculumid); $this->curriculum->load(); } else { $this->curriculum->load(); } // Handle a curriculum with an expiry date defined (ELIS-1172): if (!empty(elis::$config->local_elisprogram->enable_curriculum_expiration) && !empty($this->curriculum->frequency)) { $this->timeexpired = calculate_curriculum_expiry($this); } if ($credits !== false) { $this->credits = $credits; } if ($locked !== false) { $this->locked = $locked ? 1 : 0; } // Get the certificate code. This batch of code tries to ensure // that the random string is unique trying if (empty($this->certificatecode)) { $this->certificatecode = null; $this->certificatecode = cm_certificate_get_code(); } // Doesn't return true/false, so just assume it worked $this->save(); // Send notifications /// Does the user receive a notification? $sendtouser = !empty(elis::$config->local_elisprogram->notify_curriculumcompleted_user) ? true : false; $sendtorole = !empty(elis::$config->local_elisprogram->notify_curriculumcompleted_role) ? true : false; $sendtosupervisor = !empty(elis::$config->local_elisprogram->notify_curriculumcompleted_supervisor) ? true : false; /// If nobody receives a notification, we're done. if (!$sendtouser && !$sendtorole && !$sendtosupervisor) { return true; } $context = context_system::instance(); /// Make sure this is a valid user. $enroluser = new user($this->userid); if (!$enroluser) { print_error('nouser', 'local_elisprogram'); return true; } // Due to lazy loading, we need to pre-load this object $enroluser->load(); if (empty($enroluser->id)) { print_error('nouser', 'local_elisprogram'); return true; } $message = new notification(); /// Set up the text of the message $text = empty(elis::$config->local_elisprogram->notify_curriculumcompleted_message) ? get_string('notifycurriculumcompletedmessagedef', 'local_elisprogram') : elis::$config->local_elisprogram->notify_curriculumcompleted_message; $search = array('%%userenrolname%%', '%%programname%%'); // Get course info $program = $this->_db->get_record(curriculum::TABLE, array('id' => $this->curriculumid)); $replace = array($enroluser->moodle_fullname(), $program->name); $text = str_replace($search, $replace, $text); $eventlog = new Object(); $eventlog->event = 'curriculum_completed'; $eventlog->instance = $this->id; /// Store the assignment id. if ($sendtouser) { //todo: figure out why a log object is passed in here $message->send_notification($text, $enroluser, null, $eventlog); } $users = array(); if ($sendtorole) { /// Get all users with the notify_classenrol capability. if ($roleusers = get_users_by_capability($context, 'local/elisprogram:notify_programcomplete')) { $users = $users + $roleusers; } } if ($sendtosupervisor) { /// Get parent-context users. if ($supervisors = pm_get_users_by_capability('user', $this->userid, 'local/elisprogram:notify_programcomplete')) { $users = $users + $supervisors; } } foreach ($users as $user) { $message->send_notification($text, $user, $enroluser); } }