/** * Sends out scheduled messages to badge creators * */ function badge_message_cron() { global $DB; mtrace('Sending scheduled badge notifications.'); $scheduled = $DB->get_records_select('badge', 'notification > ? AND (status != ?) AND nextcron < ?', array(BADGE_MESSAGE_ALWAYS, BADGE_STATUS_ARCHIVED, time()), 'notification ASC', 'id, name, notification, usercreated as creator, timecreated'); foreach ($scheduled as $sch) { // Send messages. badge_assemble_notification($sch); // Update next cron value. $nextcron = badges_calculate_message_schedule($sch->notification); $DB->set_field('badge', 'nextcron', $nextcron, array('id' => $sch->id)); } }
/** * Creates and saves a clone of badge with all its properties. * Clone is not active by default and has 'Copy of' attached to its name. * * @return int ID of new badge. */ public function make_clone() { global $DB, $USER; $fordb = new stdClass(); foreach (get_object_vars($this) as $k => $v) { $fordb->{$k} = $v; } $fordb->name = get_string('copyof', 'badges', $this->name); $fordb->status = BADGE_STATUS_INACTIVE; $fordb->usercreated = $USER->id; $fordb->usermodified = $USER->id; $fordb->timecreated = time(); $fordb->timemodified = time(); unset($fordb->id); if ($fordb->notification > 1) { $fordb->nextcron = badges_calculate_message_schedule($fordb->notification); } $criteria = $fordb->criteria; unset($fordb->criteria); if ($new = $DB->insert_record('badge', $fordb, true)) { $newbadge = new badge($new); // Copy badge image. $fs = get_file_storage(); if ($file = $fs->get_file($this->get_context()->id, 'badges', 'badgeimage', $this->id, '/', 'f1.png')) { if ($imagefile = $file->copy_content_to_temp()) { badges_process_badge_image($newbadge, $imagefile); } } // Copy badge criteria. foreach ($this->criteria as $crit) { $crit->make_clone($new); } return $new; } else { throw new moodle_exception('error:clone', 'badges'); return false; } }
// Need to unset message_editor options to avoid errors on form edit. unset($badge->messageformat); unset($badge->message_editor); if ($badge->save()) { badges_process_badge_image($badge, $form->save_temp_file('image')); $form->set_data($badge); $statusmsg = get_string('changessaved'); } else { $errormsg = get_string('error:save', 'badges'); } } else { if ($action == 'message') { // Calculate next message cron if form data is different from original badge data. if ($data->notification != $badge->notification) { if ($data->notification > BADGE_MESSAGE_ALWAYS) { $badge->nextcron = badges_calculate_message_schedule($data->notification); } else { $badge->nextcron = null; } } $badge->message = clean_text($data->message_editor['text'], FORMAT_HTML); $badge->messagesubject = $data->messagesubject; $badge->notification = $data->notification; $badge->attachment = $data->attachment; unset($badge->messageformat); unset($badge->message_editor); if ($badge->save()) { $statusmsg = get_string('changessaved'); } else { $errormsg = get_string('error:save', 'badges'); }