/**
  * 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, $CURMAN;
     require_once CURMAN_DIRLOCATION . '/lib/notifications.php';
     $this->completed = STUSTATUS_PASSED;
     if ($time !== false) {
         $this->timecompleted = $time;
     }
     if ($this->timecompleted <= 0 || !is_numeric($this->timecompleted)) {
         $this->timecompleted = time();
     }
     // Handle a curriculum with an expiry date defined (ELIS-1172):
     if (!empty($CURMAN->config->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;
         $counter = 0;
         $attempts = 10;
         $maximumchar = 15;
         $addchar = 0;
         // This loop will try to generate a unique string 11 times.  On the 11th attempt
         // if string is still not unique then it will add to the length of the string
         // If the length of the string exceed the maximum length set by $maximumchar
         // then stop the loop and return an error
         do {
             $code = cm_certificate_generate_code($addchar);
             $exists = curriculum_code_exists($code);
             if (!$exists) {
                 $this->certificatecode = $code;
                 break;
             }
             // If the counter is equal to the number of attempts
             if ($counter == $attempts) {
                 // Set counter back to zero and add a character to the string
                 $counter = 0;
                 $addchar++;
             }
             // increment counter otherwise this is an infinite loop
             $counter++;
         } while ($maximumchar >= $addchar);
         // Check if the length has exceeded the maximum length
         if ($maximumchar < $addchar) {
             if (!cm_certificate_email_random_number_fail($this)) {
                 $message = get_string('certificate_email_fail', 'block_curr_admin');
                 notify($message);
             }
             print_error('certificate_code_error', 'block_curr_admin');
         }
     }
     if ($this->update()) {
         /// Does the user receive a notification?
         $sendtouser = $CURMAN->config->notify_curriculumcompleted_user;
         $sendtorole = $CURMAN->config->notify_curriculumcompleted_role;
         $sendtosupervisor = $CURMAN->config->notify_curriculumcompleted_supervisor;
         /// If nobody receives a notification, we're done.
         if (!$sendtouser && !$sendtorole && !$sendtosupervisor) {
             return true;
         }
         $context = get_system_context();
         /// Make sure this is a valid user.
         $enroluser = new user($this->userid);
         if (empty($enroluser->id)) {
             print_error('nouser', 'block_curr_admin');
             return true;
         }
         $message = new notification();
         /// Set up the text of the message
         $text = empty($CURMAN->config->notify_curriculumcompleted_message) ? get_string('notifycurriculumcompletedmessagedef', 'block_curr_admin') : $CURMAN->config->notify_curriculumcompleted_message;
         $search = array('%%userenrolname%%', '%%curriculumname%%');
         $replace = array(fullname($this->user), $this->curriculum->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, $this->user, null, $eventlog);
         }
         $users = array();
         if ($sendtorole) {
             /// Get all users with the notify_classenrol capability.
             if ($roleusers = get_users_by_capability($context, 'block/curr_admin:notify_curriculumcomplete')) {
                 $users = $users + $roleusers;
             }
         }
         if ($sendtosupervisor) {
             /// Get parent-context users.
             if ($supervisors = cm_get_users_by_capability('user', $this->userid, 'block/curr_admin:notify_curriculumcomplete')) {
                 $users = $users + $supervisors;
             }
         }
         foreach ($users as $user) {
             $message->send_notification($text, $user, $enroluser);
         }
     }
 }
Example #2
0
/**
 * Make multiple attempts to get a unique certificate code.
 *
 * @param none
 * @return string A unique certificate code.
 */
function cm_certificate_get_code()
{
    $counter = 0;
    $attempts = 10;
    $maximumchar = 15;
    $addchar = 0;
    // This loop will try to generate a unique string 11 times.  On the 11th attempt
    // if string is still not unique then it will add to the length of the string
    // If the length of the string exceed the maximum length set by $maximumchar
    // then stop the loop and return an error
    do {
        $code = cm_certificate_generate_code($addchar);
        $exists = curriculum_code_exists($code);
        $exists = $exists && entity_certificate_code_exists($code);
        if (!$exists) {
            return $code;
        }
        // If the counter is equal to the number of attempts
        if ($counter == $attempts) {
            // Set counter back to zero and add a character to the string
            $counter = 0;
            $addchar++;
        }
        // increment counter otherwise this is an infinite loop
        $counter++;
    } while ($maximumchar >= $addchar);
    // Check if the length has exceeded the maximum length
    if ($maximumchar < $addchar) {
        if (!cm_certificate_email_random_number_fail($this)) {
            $message = get_string('certificate_email_fail', 'local_elisprogram');
            $OUTPUT->notification($message);
        }
        print_error('certificate_code_error', 'local_elisprogram');
    }
}