/**
  *
  */
 public static function update_event_timedue($data)
 {
     global $DB;
     if (!empty($data->timedue)) {
         $event = new \stdClass();
         $event->name = $data->name;
         $event->description = format_module_intro('dataform', $data, $data->coursemodule);
         $event->timestart = $data->timedue;
         if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'dataform', 'instance' => $data->id))) {
             $calendarevent = \calendar_event::load($event->id);
             $calendarevent->update($event);
         } else {
             $event->courseid = $data->course;
             $event->groupid = 0;
             $event->userid = 0;
             $event->modulename = 'dataform';
             $event->instance = $data->id;
             $event->eventtype = 'due';
             $event->timeduration = 0;
             $event->visible = $DB->get_field('course_modules', 'visible', array('module' => $data->module, 'instance' => $data->id));
             \calendar_event::create($event);
         }
     } else {
         $DB->delete_records('event', array('modulename' => 'dataform', 'instance' => $data->id, 'eventtype' => 'due'));
     }
 }
Beispiel #2
0
/**
 * Performs upgrade of the database structure and data
 *
 * Workshop supports upgrades from version 1.9.0 and higher only. During 1.9 > 2.0 upgrade,
 * there are significant database changes.
 *
 * @param int $oldversion the version we are upgrading from
 * @return bool result
 */
function xmldb_workshop_upgrade($oldversion)
{
    global $CFG, $DB, $OUTPUT;
    $dbman = $DB->get_manager();
    // Moodle v2.2.0 release upgrade line
    if ($oldversion < 2012033100) {
        // add the field 'phaseswitchassessment' to the 'workshop' table
        $table = new xmldb_table('workshop');
        $field = new xmldb_field('phaseswitchassessment', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'assessmentend');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2012033100, 'workshop');
    }
    /**
     * Remove all workshop calendar events
     */
    if ($oldversion < 2012041700) {
        require_once $CFG->dirroot . '/calendar/lib.php';
        $events = $DB->get_records('event', array('modulename' => 'workshop'));
        foreach ($events as $event) {
            $event = calendar_event::load($event);
            $event->delete();
        }
        upgrade_mod_savepoint(true, 2012041700, 'workshop');
    }
    /**
     * Recreate all workshop calendar events
     */
    if ($oldversion < 2012041701) {
        require_once dirname(dirname(__FILE__)) . '/lib.php';
        $sql = "SELECT w.id, w.course, w.name, w.intro, w.introformat, w.submissionstart,\n                       w.submissionend, w.assessmentstart, w.assessmentend,\n                       cm.id AS cmid\n                  FROM {workshop} w\n                  JOIN {modules} m ON m.name = 'workshop'\n                  JOIN {course_modules} cm ON (cm.module = m.id AND cm.course = w.course AND cm.instance = w.id)";
        $rs = $DB->get_recordset_sql($sql);
        foreach ($rs as $workshop) {
            $cmid = $workshop->cmid;
            unset($workshop->cmid);
            workshop_calendar_update($workshop, $cmid);
        }
        $rs->close();
        upgrade_mod_savepoint(true, 2012041701, 'workshop');
    }
    // Moodle v2.3.0 release upgrade line
    // Put any upgrade step following this
    return true;
}
function geogebra_after_add_or_update($geogebra, $mform)
{
    global $DB;
    if ($mform->get_data()->filetype === GEOGEBRA_FILE_TYPE_LOCAL) {
        $filename = geogebra_set_mainfile($geogebra);
        $geogebra->url = $filename;
        $result = $DB->update_record('geogebra', $geogebra);
    }
    if ($geogebra->timedue) {
        $event = new stdClass();
        if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'geogebra', 'instance' => $geogebra->id))) {
            $event->name = $geogebra->name;
            $event->description = format_module_intro('geogebra', $geogebra, $geogebra->coursemodule);
            $event->timestart = $geogebra->timedue;
            $calendarevent = calendar_event::load($event->id);
            $calendarevent->update($event);
        } else {
            $event = new stdClass();
            $event->name = $geogebra->name;
            $event->description = format_module_intro('geogebra', $geogebra, $geogebra->coursemodule);
            $event->courseid = $geogebra->course;
            $event->groupid = 0;
            $event->userid = 0;
            $event->modulename = 'geogebra';
            $event->instance = $geogebra->id;
            $event->eventtype = 'due';
            $event->timestart = $geogebra->timedue;
            $event->timeduration = 0;
            calendar_event::create($event);
        }
    } else {
        $DB->delete_records('event', array('modulename' => 'geogebra', 'instance' => $geogebra->id));
    }
    // get existing grade item
    geogebra_grade_item_update($geogebra);
    return true;
}
/**
 * Call this function to update an event in the calendar table
 * the event will be identified by the id field of the $event object.
 *
 * @param object $event An object representing an event from the calendar table. The event will be identified by the id field.
 * @return bool Success
 * @deprecated please calendar_event->update() instead.
 */
function update_event($event)
{
    global $CFG;
    require_once $CFG->dirroot . '/calendar/lib.php';
    debugging('update_event() is deprecated, please use calendar_event->update() instead.', DEBUG_DEVELOPER);
    $event = (object) $event;
    $calendarevent = calendar_event::load($event->id);
    return $calendarevent->update($event);
}
Beispiel #5
0
/**
 * Updates the calendar events associated to the given workshop
 *
 * @param stdClass $workshop the workshop instance record
 * @param int $cmid course module id
 */
function workshop_calendar_update(stdClass $workshop, $cmid) {
    global $DB;

    // get the currently registered events so that we can re-use their ids
    $currentevents = $DB->get_records('event', array('modulename' => 'workshop', 'instance' => $workshop->id));

    // the common properties for all events
    $base = new stdClass();
    $base->description  = format_module_intro('workshop', $workshop, $cmid, false);
    $base->courseid     = $workshop->course;
    $base->groupid      = 0;
    $base->userid       = 0;
    $base->modulename   = 'workshop';
    $base->eventtype    = 'pluginname';
    $base->instance     = $workshop->id;
    $base->visible      = instance_is_visible('workshop', $workshop);
    $base->timeduration = 0;

    if ($workshop->submissionstart) {
        $event = clone($base);
        $event->name = get_string('submissionstartevent', 'mod_workshop', $workshop->name);
        $event->timestart = $workshop->submissionstart;
        if ($reusedevent = array_shift($currentevents)) {
            $event->id = $reusedevent->id;
        } else {
            // should not be set but just in case
            unset($event->id);
        }
        // update() will reuse a db record if the id field is set
        $eventobj = new calendar_event($event);
        $eventobj->update($event, false);
    }

    if ($workshop->submissionend) {
        $event = clone($base);
        $event->name = get_string('submissionendevent', 'mod_workshop', $workshop->name);
        $event->timestart = $workshop->submissionend;
        if ($reusedevent = array_shift($currentevents)) {
            $event->id = $reusedevent->id;
        } else {
            // should not be set but just in case
            unset($event->id);
        }
        // update() will reuse a db record if the id field is set
        $eventobj = new calendar_event($event);
        $eventobj->update($event, false);
    }

    if ($workshop->assessmentstart) {
        $event = clone($base);
        $event->name = get_string('assessmentstartevent', 'mod_workshop', $workshop->name);
        $event->timestart = $workshop->assessmentstart;
        if ($reusedevent = array_shift($currentevents)) {
            $event->id = $reusedevent->id;
        } else {
            // should not be set but just in case
            unset($event->id);
        }
        // update() will reuse a db record if the id field is set
        $eventobj = new calendar_event($event);
        $eventobj->update($event, false);
    }

    if ($workshop->assessmentend) {
        $event = clone($base);
        $event->name = get_string('assessmentendevent', 'mod_workshop', $workshop->name);
        $event->timestart = $workshop->assessmentend;
        if ($reusedevent = array_shift($currentevents)) {
            $event->id = $reusedevent->id;
        } else {
            // should not be set but just in case
            unset($event->id);
        }
        // update() will reuse a db record if the id field is set
        $eventobj = new calendar_event($event);
        $eventobj->update($event, false);
    }

    // delete any leftover events
    foreach ($currentevents as $oldevent) {
        $oldevent = calendar_event::load($oldevent);
        $oldevent->delete();
    }
}
Beispiel #6
0
/**
 * Updates chat records so that the next chat time is correct
 *
 * @global object
 * @param int $chatid
 * @return void
 */
function chat_update_chat_times($chatid = 0)
{
    // Updates chat records so that the next chat time is correct.
    global $DB;
    $timenow = time();
    $params = array('timenow' => $timenow, 'chatid' => $chatid);
    if ($chatid) {
        if (!($chats[] = $DB->get_record_select("chat", "id = :chatid AND chattime <= :timenow AND schedule > 0", $params))) {
            return;
        }
    } else {
        if (!($chats = $DB->get_records_select("chat", "chattime <= :timenow AND schedule > 0", $params))) {
            return;
        }
    }
    foreach ($chats as $chat) {
        switch ($chat->schedule) {
            case 1:
                // Single event - turn off schedule and disable.
                $chat->chattime = 0;
                $chat->schedule = 0;
                break;
            case 2:
                // Repeat daily.
                while ($chat->chattime <= $timenow) {
                    $chat->chattime += 24 * 3600;
                }
                break;
            case 3:
                // Repeat weekly.
                while ($chat->chattime <= $timenow) {
                    $chat->chattime += 7 * 24 * 3600;
                }
                break;
        }
        $DB->update_record("chat", $chat);
        $event = new stdClass();
        // Update calendar too.
        $cond = "modulename='chat' AND instance = :chatid AND timestart <> :chattime";
        $params = array('chattime' => $chat->chattime, 'chatid' => $chatid);
        if ($event->id = $DB->get_field_select('event', 'id', $cond, $params)) {
            $event->timestart = $chat->chattime;
            $calendarevent = calendar_event::load($event->id);
            $calendarevent->update($event, false);
        }
    }
}
/**
 * Update the event if it exists, else create
 */
function simplecertificate_send_event($certificate)
{
    global $DB, $CFG;
    require_once $CFG->dirroot . '/calendar/lib.php';
    if ($event = $DB->get_record('event', array('modulename' => 'simplecertificate', 'instance' => $certificate->id))) {
        $calendarevent = calendar_event::load($event->id);
        $calendarevent->name = $certificate->name;
        $calendarevent->update($calendarevent);
    } else {
        $event = new stdClass();
        $event->name = $certificate->name;
        $event->description = '';
        $event->courseid = $certificate->course;
        $event->groupid = 0;
        $event->userid = 0;
        $event->modulename = 'simplecertificate';
        $event->instance = $certificate->id;
        calendar_event::create($event);
    }
}
Beispiel #8
0
 /**
  * Update or create an event within the database
  *
  * Pass in a object containing the event properties and this function will
  * insert it into the database and deal with any associated files
  *
  * @see add_event()
  * @see update_event()
  *
  * @param stdClass $data object of event
  * @param bool $checkcapability if moodle should check calendar managing capability or not
  * @return bool event updated
  */
 public function update($data, $checkcapability = true)
 {
     global $CFG, $DB, $USER;
     foreach ($data as $key => $value) {
         $this->properties->{$key} = $value;
     }
     $this->properties->timemodified = time();
     $usingeditor = !empty($this->properties->description) && is_array($this->properties->description);
     if (empty($this->properties->id) || $this->properties->id < 1) {
         if ($checkcapability) {
             if (!calendar_add_event_allowed($this->properties)) {
                 print_error('nopermissiontoupdatecalendar');
             }
         }
         if ($usingeditor) {
             switch ($this->properties->eventtype) {
                 case 'user':
                     $this->properties->courseid = 0;
                     $this->properties->course = 0;
                     $this->properties->groupid = 0;
                     $this->properties->userid = $USER->id;
                     break;
                 case 'site':
                     $this->properties->courseid = SITEID;
                     $this->properties->course = SITEID;
                     $this->properties->groupid = 0;
                     $this->properties->userid = $USER->id;
                     break;
                 case 'course':
                     $this->properties->groupid = 0;
                     $this->properties->userid = $USER->id;
                     break;
                 case 'group':
                     $this->properties->userid = $USER->id;
                     break;
                 default:
                     // Ewww we should NEVER get here, but just incase we do lets
                     // fail gracefully
                     $usingeditor = false;
                     break;
             }
             // If we are actually using the editor, we recalculate the context because some default values
             // were set when calculate_context() was called from the constructor.
             if ($usingeditor) {
                 $this->properties->context = $this->calculate_context($this->properties);
                 $this->editorcontext = $this->properties->context;
             }
             $editor = $this->properties->description;
             $this->properties->format = $this->properties->description['format'];
             $this->properties->description = $this->properties->description['text'];
         }
         // Insert the event into the database
         $this->properties->id = $DB->insert_record('event', $this->properties);
         if ($usingeditor) {
             $this->properties->description = file_save_draft_area_files($editor['itemid'], $this->editorcontext->id, 'calendar', 'event_description', $this->properties->id, $this->editoroptions, $editor['text'], $this->editoroptions['forcehttps']);
             $DB->set_field('event', 'description', $this->properties->description, array('id' => $this->properties->id));
         }
         // Log the event entry.
         add_to_log($this->properties->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id=' . $this->properties->id, $this->properties->name);
         $repeatedids = array();
         if (!empty($this->properties->repeat)) {
             $this->properties->repeatid = $this->properties->id;
             $DB->set_field('event', 'repeatid', $this->properties->repeatid, array('id' => $this->properties->id));
             $eventcopy = clone $this->properties;
             unset($eventcopy->id);
             for ($i = 1; $i < $eventcopy->repeats; $i++) {
                 $eventcopy->timestart = $eventcopy->timestart + WEEKSECS + dst_offset_on($eventcopy->timestart) - dst_offset_on($eventcopy->timestart + WEEKSECS);
                 // Get the event id for the log record.
                 $eventcopyid = $DB->insert_record('event', $eventcopy);
                 // If the context has been set delete all associated files
                 if ($usingeditor) {
                     $fs = get_file_storage();
                     $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id);
                     foreach ($files as $file) {
                         $fs->create_file_from_storedfile(array('itemid' => $eventcopyid), $file);
                     }
                 }
                 $repeatedids[] = $eventcopyid;
                 // Log the event entry.
                 add_to_log($eventcopy->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id=' . $eventcopyid, $eventcopy->name);
             }
         }
         // Hook for tracking added events
         self::calendar_event_hook('add_event', array($this->properties, $repeatedids));
         return true;
     } else {
         if ($checkcapability) {
             if (!calendar_edit_event_allowed($this->properties)) {
                 print_error('nopermissiontoupdatecalendar');
             }
         }
         if ($usingeditor) {
             if ($this->editorcontext !== null) {
                 $this->properties->description = file_save_draft_area_files($this->properties->description['itemid'], $this->editorcontext->id, 'calendar', 'event_description', $this->properties->id, $this->editoroptions, $this->properties->description['text'], $this->editoroptions['forcehttps']);
             } else {
                 $this->properties->format = $this->properties->description['format'];
                 $this->properties->description = $this->properties->description['text'];
             }
         }
         $event = $DB->get_record('event', array('id' => $this->properties->id));
         $updaterepeated = !empty($this->properties->repeatid) && !empty($this->properties->repeateditall);
         if ($updaterepeated) {
             // Update all
             if ($this->properties->timestart != $event->timestart) {
                 $timestartoffset = $this->properties->timestart - $event->timestart;
                 $sql = "UPDATE {event}\n                               SET name = ?,\n                                   description = ?,\n                                   timestart = timestart + ?,\n                                   timeduration = ?,\n                                   timemodified = ?\n                             WHERE repeatid = ?";
                 $params = array($this->properties->name, $this->properties->description, $timestartoffset, $this->properties->timeduration, time(), $event->repeatid);
             } else {
                 $sql = "UPDATE {event} SET name = ?, description = ?, timeduration = ?, timemodified = ? WHERE repeatid = ?";
                 $params = array($this->properties->name, $this->properties->description, $this->properties->timeduration, time(), $event->repeatid);
             }
             $DB->execute($sql, $params);
             // Log the event update.
             add_to_log($this->properties->courseid, 'calendar', 'edit all', 'event.php?action=edit&amp;id=' . $this->properties->id, $this->properties->name);
         } else {
             $DB->update_record('event', $this->properties);
             $event = calendar_event::load($this->properties->id);
             $this->properties = $event->properties();
             add_to_log($this->properties->courseid, 'calendar', 'edit', 'event.php?action=edit&amp;id=' . $this->properties->id, $this->properties->name);
         }
         // Hook for tracking event updates
         self::calendar_event_hook('update_event', array($this->properties, $updaterepeated));
         return true;
     }
 }
Beispiel #9
0
 /**
  * Update the calendar entries for this assignment.
  *
  * @param int $coursemoduleid - Required to pass this in because it might
  *                              not exist in the database yet.
  * @return bool
  */
 public function update_calendar($coursemoduleid)
 {
     global $DB, $CFG;
     require_once $CFG->dirroot . '/calendar/lib.php';
     // Special case for add_instance as the coursemodule has not been set yet.
     $instance = $this->get_instance();
     if ($instance->duedate) {
         $event = new stdClass();
         $params = array('modulename' => 'assign', 'instance' => $instance->id);
         $event->id = $DB->get_field('event', 'id', $params);
         $event->name = $instance->name;
         $event->timestart = $instance->duedate;
         // Convert the links to pluginfile. It is a bit hacky but at this stage the files
         // might not have been saved in the module area yet.
         $intro = $instance->intro;
         if ($draftid = file_get_submitted_draft_itemid('introeditor')) {
             $intro = file_rewrite_urls_to_pluginfile($intro, $draftid);
         }
         // We need to remove the links to files as the calendar is not ready
         // to support module events with file areas.
         $intro = strip_pluginfile_content($intro);
         if ($this->show_intro()) {
             $event->description = array('text' => $intro, 'format' => $instance->introformat);
         } else {
             $event->description = array('text' => '', 'format' => $instance->introformat);
         }
         if ($event->id) {
             $calendarevent = calendar_event::load($event->id);
             $calendarevent->update($event);
         } else {
             unset($event->id);
             $event->courseid = $instance->course;
             $event->groupid = 0;
             $event->userid = 0;
             $event->modulename = 'assign';
             $event->instance = $instance->id;
             $event->eventtype = 'due';
             $event->timeduration = 0;
             calendar_event::create($event);
         }
     } else {
         $DB->delete_records('event', array('modulename' => 'assign', 'instance' => $instance->id));
     }
 }
Beispiel #10
0
/**
 * Runs any processes that must be run
 * after a lesson insert/update
 *
 * @global object
 * @param object $lesson Lesson form data
 * @return void
 **/
function lesson_process_post_save(&$lesson)
{
    global $DB, $CFG;
    require_once $CFG->dirroot . '/calendar/lib.php';
    require_once $CFG->dirroot . '/mod/lesson/locallib.php';
    if ($events = $DB->get_records('event', array('modulename' => 'lesson', 'instance' => $lesson->id))) {
        foreach ($events as $event) {
            $event = calendar_event::load($event->id);
            $event->delete();
        }
    }
    $event = new stdClass();
    $event->description = $lesson->name;
    $event->courseid = $lesson->course;
    $event->groupid = 0;
    $event->userid = 0;
    $event->modulename = 'lesson';
    $event->instance = $lesson->id;
    $event->eventtype = 'open';
    $event->timestart = $lesson->available;
    $event->visible = instance_is_visible('lesson', $lesson);
    $event->timeduration = $lesson->deadline - $lesson->available;
    if ($lesson->deadline and $lesson->available and $event->timeduration <= LESSON_MAX_EVENT_LENGTH) {
        // Single event for the whole lesson.
        $event->name = $lesson->name;
        calendar_event::create(clone $event);
    } else {
        // Separate start and end events.
        $event->timeduration = 0;
        if ($lesson->available) {
            $event->name = $lesson->name . ' (' . get_string('lessonopens', 'lesson') . ')';
            calendar_event::create(clone $event);
        }
        if ($lesson->deadline) {
            $event->name = $lesson->name . ' (' . get_string('lessoncloses', 'lesson') . ')';
            $event->timestart = $lesson->deadline;
            $event->eventtype = 'close';
            calendar_event::create(clone $event);
        }
    }
}
Beispiel #11
0
/**
 * This function will handles the whole deletion process of a module. This includes calling
 * the modules delete_instance function, deleting files, events, grades, conditional data,
 * the data in the course_module and course_sections table and adding a module deletion
 * event to the DB.
 *
 * @param int $cmid the course module id
 * @since 2.5
 */
function course_delete_module($cmid)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/gradelib.php';
    require_once $CFG->dirroot . '/blog/lib.php';
    require_once $CFG->dirroot . '/calendar/lib.php';
    // Get the course module.
    if (!($cm = $DB->get_record('course_modules', array('id' => $cmid)))) {
        return true;
    }
    // Get the module context.
    $modcontext = context_module::instance($cm->id);
    // Get the course module name.
    $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module), MUST_EXIST);
    // Get the file location of the delete_instance function for this module.
    $modlib = "{$CFG->dirroot}/mod/{$modulename}/lib.php";
    // Include the file required to call the delete_instance function for this module.
    if (file_exists($modlib)) {
        require_once $modlib;
    } else {
        throw new moodle_exception('cannotdeletemodulemissinglib', '', '', null, "Cannot delete this module as the file mod/{$modulename}/lib.php is missing.");
    }
    $deleteinstancefunction = $modulename . '_delete_instance';
    // Ensure the delete_instance function exists for this module.
    if (!function_exists($deleteinstancefunction)) {
        throw new moodle_exception('cannotdeletemodulemissingfunc', '', '', null, "Cannot delete this module as the function {$modulename}_delete_instance is missing in mod/{$modulename}/lib.php.");
    }
    // Call the delete_instance function, if it returns false throw an exception.
    if (!$deleteinstancefunction($cm->instance)) {
        throw new moodle_exception('cannotdeletemoduleinstance', '', '', null, "Cannot delete the module {$modulename} (instance).");
    }
    // Remove all module files in case modules forget to do that.
    $fs = get_file_storage();
    $fs->delete_area_files($modcontext->id);
    // Delete events from calendar.
    if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename))) {
        foreach ($events as $event) {
            $calendarevent = calendar_event::load($event->id);
            $calendarevent->delete();
        }
    }
    // Delete grade items, outcome items and grades attached to modules.
    if ($grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename, 'iteminstance' => $cm->instance, 'courseid' => $cm->course))) {
        foreach ($grade_items as $grade_item) {
            $grade_item->delete('moddelete');
        }
    }
    // Delete completion and availability data; it is better to do this even if the
    // features are not turned on, in case they were turned on previously (these will be
    // very quick on an empty table).
    $DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id));
    $DB->delete_records('course_modules_availability', array('coursemoduleid' => $cm->id));
    $DB->delete_records('course_modules_avail_fields', array('coursemoduleid' => $cm->id));
    $DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id, 'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));
    // Delete the context.
    context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
    // Delete the module from the course_modules table.
    $DB->delete_records('course_modules', array('id' => $cm->id));
    // Delete module from that section.
    if (!delete_mod_from_section($cm->id, $cm->section)) {
        throw new moodle_exception('cannotdeletemodulefromsection', '', '', null, "Cannot delete the module {$modulename} (instance) from section.");
    }
    // Trigger event for course module delete action.
    $event = \core\event\course_module_deleted::create(array('courseid' => $cm->course, 'context' => $modcontext, 'objectid' => $cm->id, 'other' => array('modulename' => $modulename, 'instanceid' => $cm->instance)));
    $event->add_record_snapshot('course_modules', $cm);
    $event->trigger();
    rebuild_course_cache($cm->course, true);
}
Beispiel #12
0
/**
 * Given an ID of an instance of this module,
 * this function will permanently delete the instance
 * and any data that depends on it.
 *
 * @param int $id Id of the module instance
 * @return boolean Success/Failure
 */
function adobeconnect_delete_instance($id)
{
    global $DB;
    $param = array('id' => $id);
    if (!($adobeconnect = $DB->get_record('adobeconnect', $param))) {
        return false;
    }
    $result = true;
    // Remove meeting from Adobe connect server
    $param = array('instanceid' => $adobeconnect->id);
    $adbmeetings = $DB->get_records('adobeconnect_meeting_groups', $param);
    if (!empty($adbmeetings)) {
        $aconnect = aconnect_login();
        foreach ($adbmeetings as $meeting) {
            // Update calendar event
            $param = array('courseid' => $adobeconnect->course, 'instance' => $adobeconnect->id, 'groupid' => $meeting->groupid, 'modulename' => 'adobeconnect');
            $eventid = $DB->get_field('event', 'id', $param);
            if (!empty($eventid)) {
                $event = calendar_event::load($eventid);
                $event->delete();
            }
            aconnect_remove_meeting($aconnect, $meeting->meetingscoid);
        }
        aconnect_logout($aconnect);
    }
    $param = array('id' => $adobeconnect->id);
    $result &= $DB->delete_records('adobeconnect', $param);
    $param = array('instanceid' => $adobeconnect->id);
    $result &= $DB->delete_records('adobeconnect_meeting_groups', $param);
    return $result;
}
/**
 * This function updates the events associated to the offlinequiz.
 * If $override is non-zero, then it updates only the events
 * associated with the specified override.
 *
 * @uses OFFLINEQUIZ_MAX_EVENT_LENGTH
 * @param object $offlinequiz the offlinequiz object.
 * @param object optional $override limit to a specific override
 */
function offlinequiz_update_events($offlinequiz)
{
    global $DB, $CFG;
    require_once $CFG->dirroot . '/calendar/lib.php';
    // Load the old events relating to this offlinequiz.
    $conds = array('modulename' => 'offlinequiz', 'instance' => $offlinequiz->id);
    if (!empty($override)) {
        // Only load events for this override.
        $conds['groupid'] = isset($override->groupid) ? $override->groupid : 0;
        $conds['userid'] = isset($override->userid) ? $override->userid : 0;
    }
    $oldevents = $DB->get_records('event', $conds);
    $groupid = 0;
    $userid = 0;
    $timeopen = $offlinequiz->timeopen;
    $timeclose = $offlinequiz->timeclose;
    if ($offlinequiz->time) {
        $timeopen = $offlinequiz->time;
    }
    // Only add open/close events if they differ from the offlinequiz default.
    if (!empty($offlinequiz->coursemodule)) {
        $cmid = $offlinequiz->coursemodule;
    } else {
        $cmid = get_coursemodule_from_instance('offlinequiz', $offlinequiz->id, $offlinequiz->course)->id;
    }
    if (!empty($timeopen)) {
        $event = new stdClass();
        $event->name = $offlinequiz->name;
        $event->description = format_module_intro('offlinequiz', $offlinequiz, $cmid);
        // Events module won't show user events when the courseid is nonzero.
        $event->courseid = $userid ? 0 : $offlinequiz->course;
        $event->groupid = $groupid;
        $event->userid = $userid;
        $event->modulename = 'offlinequiz';
        $event->instance = $offlinequiz->id;
        $event->timestart = $timeopen;
        $event->timeduration = max($timeclose - $timeopen, 0);
        $event->visible = instance_is_visible('offlinequiz', $offlinequiz);
        if ($timeopen == $offlinequiz->time) {
            $event->name = $offlinequiz->name;
        }
        if ($timeopen == $offlinequiz->timeopen) {
            $event->name = $offlinequiz->name . ' (' . get_string('reportstarts', 'offlinequiz') . ')';
        }
        calendar_event::create($event);
    }
    // Delete any leftover events.
    foreach ($oldevents as $badevent) {
        $badevent = calendar_event::load($badevent);
        $badevent->delete();
    }
}
Beispiel #14
0
/**
 * Performs upgrade of the database structure and data
 *
 * Workshop supports upgrades from version 1.9.0 and higher only. During 1.9 > 2.0 upgrade,
 * there are significant database changes.
 *
 * @param int $oldversion the version we are upgrading from
 * @return bool result
 */
function xmldb_workshop_upgrade($oldversion) {
    global $CFG, $DB, $OUTPUT;

    $dbman = $DB->get_manager();

    // Moodle v2.2.0 release upgrade line

    if ($oldversion < 2012033100) {
        // add the field 'phaseswitchassessment' to the 'workshop' table
        $table = new xmldb_table('workshop');
        $field = new xmldb_field('phaseswitchassessment', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'assessmentend');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        upgrade_mod_savepoint(true, 2012033100, 'workshop');
    }

    /**
     * Remove all workshop calendar events
     */
    if ($oldversion < 2012041700) {
        require_once($CFG->dirroot . '/calendar/lib.php');
        $events = $DB->get_records('event', array('modulename' => 'workshop'));
        foreach ($events as $event) {
            $event = calendar_event::load($event);
            $event->delete();
        }
        upgrade_mod_savepoint(true, 2012041700, 'workshop');
    }

    /**
     * Recreate all workshop calendar events
     */
    if ($oldversion < 2012041701) {
        require_once(dirname(dirname(__FILE__)) . '/lib.php');

        $sql = "SELECT w.id, w.course, w.name, w.intro, w.introformat, w.submissionstart,
                       w.submissionend, w.assessmentstart, w.assessmentend,
                       cm.id AS cmid
                  FROM {workshop} w
                  JOIN {modules} m ON m.name = 'workshop'
                  JOIN {course_modules} cm ON (cm.module = m.id AND cm.course = w.course AND cm.instance = w.id)";

        $rs = $DB->get_recordset_sql($sql);

        foreach ($rs as $workshop) {
            $cmid = $workshop->cmid;
            unset($workshop->cmid);
            workshop_calendar_update($workshop, $cmid);
        }
        $rs->close();
        upgrade_mod_savepoint(true, 2012041701, 'workshop');
    }

    // Moodle v2.3.0 release upgrade line

    /**
     * Add new fields conclusion and conclusionformat
     */
    if ($oldversion < 2012102400) {
        $table = new xmldb_table('workshop');

        $field = new xmldb_field('conclusion', XMLDB_TYPE_TEXT, null, null, null, null, null, 'phaseswitchassessment');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $field = new xmldb_field('conclusionformat', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '1', 'conclusion');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        upgrade_mod_savepoint(true, 2012102400, 'workshop');
    }


    // Moodle v2.4.0 release upgrade line
    // Put any upgrade step following this

    /**
     * Add overall feedback related fields into the workshop table.
     */
    if ($oldversion < 2013032500) {
        $table = new xmldb_table('workshop');

        $field = new xmldb_field('overallfeedbackmode', XMLDB_TYPE_INTEGER, '3', null, null, null, '1', 'conclusionformat');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $field = new xmldb_field('overallfeedbackfiles', XMLDB_TYPE_INTEGER, '3', null, null, null, '0', 'overallfeedbackmode');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $field = new xmldb_field('overallfeedbackmaxbytes', XMLDB_TYPE_INTEGER, '10', null, null, null, '100000', 'overallfeedbackfiles');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        upgrade_mod_savepoint(true, 2013032500, 'workshop');
    }

    /**
     * Add feedbackauthorattachment field into the workshop_assessments table.
     */
    if ($oldversion < 2013032501) {
        $table = new xmldb_table('workshop_assessments');
        $field = new xmldb_field('feedbackauthorattachment', XMLDB_TYPE_INTEGER, '3', null, null, null, '0', 'feedbackauthorformat');
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        upgrade_mod_savepoint(true, 2013032501, 'workshop');
    }


    return true;
}
Beispiel #15
0
 /**
  * Deletes a lesson override from the database and clears any corresponding calendar events
  *
  * @param int $overrideid The id of the override being deleted
  * @return bool true on success
  */
 public function delete_override($overrideid)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/calendar/lib.php';
     $cm = get_coursemodule_from_instance('lesson', $this->properties->id, $this->properties->course);
     $override = $DB->get_record('lesson_overrides', array('id' => $overrideid), '*', MUST_EXIST);
     // Delete the events.
     $conds = array('modulename' => 'lesson', 'instance' => $this->properties->id);
     if (isset($override->userid)) {
         $conds['userid'] = $override->userid;
     } else {
         $conds['groupid'] = $override->groupid;
     }
     $events = $DB->get_records('event', $conds);
     foreach ($events as $event) {
         $eventold = calendar_event::load($event);
         $eventold->delete();
     }
     $DB->delete_records('lesson_overrides', array('id' => $overrideid));
     // Set the common parameters for one of the events we will be triggering.
     $params = array('objectid' => $override->id, 'context' => context_module::instance($cm->id), 'other' => array('lessonid' => $override->lessonid));
     // Determine which override deleted event to fire.
     if (!empty($override->userid)) {
         $params['relateduserid'] = $override->userid;
         $event = \mod_lesson\event\user_override_deleted::create($params);
     } else {
         $params['other']['groupid'] = $override->groupid;
         $event = \mod_lesson\event\group_override_deleted::create($params);
     }
     // Trigger the override deleted event.
     $event->add_record_snapshot('lesson_overrides', $override);
     $event->trigger();
     return true;
 }
Beispiel #16
0
if (!empty($SESSION->cal_course_referer)) {
    // TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
    $course = $DB->get_record('course', array('id' => $SESSION->cal_course_referer));
} else {
    $course = $site;
}
require_login($course, false);
$calendar = new calendar_information($cal_d, $cal_m, $cal_y);
$calendar->courseid = $courseid;
$strcalendar = get_string('calendar', 'calendar');
$link = clone $viewcalendarurl;
$link->param('view', 'upcoming');
$formoptions = new stdClass();
if ($eventid !== 0) {
    $title = get_string('editevent', 'calendar');
    $event = calendar_event::load($eventid);
    if (!calendar_edit_event_allowed($event)) {
        print_error('nopermissions');
    }
    $event->action = $action;
    $event->course = $courseid;
    $event->timedurationuntil = $event->timestart + $event->timeduration;
    $event->count_repeats();
    if (!calendar_add_event_allowed($event)) {
        print_error('nopermissions');
    }
} else {
    $title = get_string('newevent', 'calendar');
    calendar_get_allowed_types($formoptions->eventtypes, $USER->id);
    $event = new stdClass();
    $event->action = $action;
Beispiel #17
0
    /**
     * Deletes this lesson from the database
     */
    public function delete() {
        global $CFG, $DB;
        require_once($CFG->libdir.'/gradelib.php');
        require_once($CFG->dirroot.'/calendar/lib.php');

        $DB->delete_records("lesson", array("id"=>$this->properties->id));;
        $DB->delete_records("lesson_pages", array("lessonid"=>$this->properties->id));
        $DB->delete_records("lesson_answers", array("lessonid"=>$this->properties->id));
        $DB->delete_records("lesson_attempts", array("lessonid"=>$this->properties->id));
        $DB->delete_records("lesson_grades", array("lessonid"=>$this->properties->id));
        $DB->delete_records("lesson_timer", array("lessonid"=>$this->properties->id));
        $DB->delete_records("lesson_branch", array("lessonid"=>$this->properties->id));
        $DB->delete_records("lesson_high_scores", array("lessonid"=>$this->properties->id));
        if ($events = $DB->get_records('event', array("modulename"=>'lesson', "instance"=>$this->properties->id))) {
            foreach($events as $event) {
                $event = calendar_event::load($event);
                $event->delete();
            }
        }

        grade_update('mod/lesson', $this->properties->course, 'mod', 'lesson', $this->properties->id, 0, NULL, array('deleted'=>1));
        return true;
    }
 /**
  *
  * Updates a calendar event with new details
  * @param object $event a object containing details of an event tot be saved into a users calendar
  */
 function update_event($event)
 {
     global $CFG, $USER;
     if (stripos($CFG->release, "2.") !== false) {
         require_once $CFG->dirroot . '/calendar/lib.php';
         $calevent = calendar_event::load($event->id);
         return $calevent->update($event, false);
     } else {
         return update_event($event);
     }
 }
Beispiel #19
0
/**
 * deletes an instance of a data
 *
 * @global object
 * @param int $id
 * @return bool
 */
function data_delete_instance($id)
{
    // takes the dataid
    global $DB, $CFG;
    if (!($data = $DB->get_record('data', array('id' => $id)))) {
        return false;
    }
    $cm = get_coursemodule_from_instance('data', $data->id);
    $context = context_module::instance($cm->id);
    /// Delete all the associated information
    // files
    $fs = get_file_storage();
    $fs->delete_area_files($context->id, 'mod_data');
    // get all the records in this data
    $sql = "SELECT r.id\n              FROM {data_records} r\n             WHERE r.dataid = ?";
    $DB->delete_records_select('data_content', "recordid IN ({$sql})", array($id));
    // delete all the records and fields
    $DB->delete_records('data_records', array('dataid' => $id));
    $DB->delete_records('data_fields', array('dataid' => $id));
    // Remove old calendar events.
    $events = $DB->get_records('event', array('modulename' => 'data', 'instance' => $id));
    foreach ($events as $event) {
        $event = calendar_event::load($event);
        $event->delete();
    }
    // Delete the instance itself
    $result = $DB->delete_records('data', array('id' => $id));
    // cleanup gradebook
    data_grade_item_delete($data);
    return $result;
}
/**
 * Deletes a booking option and the associated user answers
 * @param $bookingid the booking instance
 * @param $optionid the booking option
 * @return false if not successful, true on success
 */
function booking_delete_booking_option($booking, $optionid)
{
    global $DB;
    $event = new stdClass();
    if (!($option = $DB->get_record("booking_options", array("id" => $optionid)))) {
        return false;
    }
    $result = true;
    $params = array('bookingid' => $booking->id, 'optionid' => $optionid);
    $userids = $DB->get_fieldset_select('booking_answers', 'userid', 'bookingid = :bookingid AND optionid = :optionid', $params);
    foreach ($userids as $userid) {
        booking_check_unenrol_user($option, $booking, $userid);
        // Unenrol any users enroled via this option.
    }
    if (!$DB->delete_records("booking_answers", array("bookingid" => $booking->id, "optionid" => $optionid))) {
        $result = false;
    }
    // Delete calendar entry, if any
    $event->id = $DB->get_field('booking_options', 'calendarid', array('id' => $optionid));
    if ($event->id > 0) {
        // Delete event if exist
        $event = calendar_event::load($event->id);
        $event->delete(true);
    }
    if (!$DB->delete_records("booking_options", array("id" => $optionid))) {
        $result = false;
    }
    return $result;
}
Beispiel #21
0
/**
 * Processes the data passed by the part update form from the summary page
 *
 * @param object $cm The moodle course module object for this instance
 * @param object $turnitintool The turnitintool object is for this activity
 * @param array $post The post array from the part update form
 * @return array A notice array contains error details for display on page load in the case of an error nothing returned if no errors occur
 */
function turnitintool_update_partnames($cm, $turnitintool, $post)
{
    global $CFG, $USER;
    if (has_capability('mod/turnitintool:grade', turnitintool_get_context('MODULE', $cm->id))) {
        $notice['message'] = '';
        $error = false;
        $dtstart = make_timestamp($post["dtstart"]["year"], $post["dtstart"]["month"], $post["dtstart"]["day"], $post["dtstart"]["hour"], $post["dtstart"]["minute"], 0, get_user_timezone());
        $dtdue = make_timestamp($post["dtdue"]["year"], $post["dtdue"]["month"], $post["dtdue"]["day"], $post["dtdue"]["hour"], $post["dtdue"]["minute"], 0, get_user_timezone());
        $dtpost = make_timestamp($post["dtpost"]["year"], $post["dtpost"]["month"], $post["dtpost"]["day"], $post["dtpost"]["hour"], $post["dtpost"]["minute"], 0, get_user_timezone());
        if ($dtstart >= $dtdue) {
            $notice['message'] .= get_string('partstarterror', 'turnitintool');
            $error = true;
        }
        if ($dtpost < $dtstart) {
            $notice['message'] .= get_string('partdueerror', 'turnitintool');
            $error = true;
        }
        if (empty($post['partname'])) {
            $notice['message'] .= get_string('partnameerror', 'turnitintool');
            $error = true;
        }
        if (strlen($post['partname']) > 35) {
            $input = new stdClass();
            $input->length = 35;
            $input->field = get_string('partname', 'turnitintool');
            $notice['message'] .= get_string('maxlength', 'turnitintool', $input);
            $error = true;
        }
        if (!preg_match("/^[-]?[0-9]+([\\.][0-9]+)?\$/", $post['maxmarks'])) {
            // ENTRY IS NOT A NUMBER
            $notice['message'] .= get_string('partmarkserror', 'turnitintool');
            $error = true;
        }
        if ($error) {
            $notice['error'] = $post['submitted'];
            $notice['post'] = $post;
        }
        if (!$error) {
            // Update Turnitin Assignment via the API [[[[
            $tiipost = new stdClass();
            $tiipost->ctl = turnitintool_getCTL($turnitintool->course);
            $tiipost->dtstart = $dtstart;
            $tiipost->dtdue = $dtdue;
            $tiipost->dtpost = $dtpost;
            if (turnitintool_is_owner($turnitintool->course)) {
                $owner = $USER;
            } else {
                $owner = turnitintool_get_owner($turnitintool->course);
            }
            if (!($course = turnitintool_get_record('course', 'id', $turnitintool->course))) {
                turnitintool_print_error('coursegeterror', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
                exit;
            }
            $loaderbar = new turnitintool_loaderbarclass(3);
            $tii = new turnitintool_commclass(turnitintool_getUID($owner), $owner->firstname, $owner->lastname, $owner->email, 2, $loaderbar);
            $tii->startSession();
            turnitintool_usersetup($owner, get_string('userprocess', 'turnitintool'), $tii, $loaderbar);
            turnitintool_classsetup($course, $owner, get_string('classprocess', 'turnitintool'), $tii, $loaderbar);
            if ($tii->getRerror()) {
                if ($tii->getAPIunavailable()) {
                    turnitintool_print_error('apiunavailable', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
                } else {
                    turnitintool_print_error($tii->getRmessage(), NULL, NULL, NULL, __FILE__, __LINE__);
                }
                exit;
            }
            $tiipost->cid = turnitintool_getCID($turnitintool->course);
            $tiipost->assignid = turnitintool_getAID($post['submitted']);
            $tiipost->s_view_report = $turnitintool->studentreports;
            $tiipost->max_points = $post['maxmarks'];
            $tiipost->anon = $turnitintool->anon;
            $tiipost->report_gen_speed = $turnitintool->reportgenspeed;
            $tiipost->late_accept_flag = $turnitintool->allowlate;
            $tiipost->submit_papers_to = $turnitintool->submitpapersto;
            $tiipost->s_paper_check = $turnitintool->spapercheck;
            $tiipost->internet_check = $turnitintool->internetcheck;
            $tiipost->journal_check = $turnitintool->journalcheck;
            // Add in Exclude small matches, biblio, quoted etc 20102009
            $tiipost->exclude_biblio = $turnitintool->excludebiblio;
            $tiipost->exclude_quoted = $turnitintool->excludequoted;
            $tiipost->exclude_value = $turnitintool->excludevalue;
            $tiipost->exclude_type = $turnitintool->excludetype;
            // Add in the erater settings
            $tiipost->erater = $turnitintool->erater;
            $tiipost->erater_handbook = $turnitintool->erater_handbook;
            $tiipost->erater_dictionary = $turnitintool->erater_dictionary;
            $tiipost->erater_spelling = $turnitintool->erater_spelling;
            $tiipost->erater_grammar = $turnitintool->erater_grammar;
            $tiipost->erater_usage = $turnitintool->erater_usage;
            $tiipost->erater_mechanics = $turnitintool->erater_mechanics;
            $tiipost->erater_style = $turnitintool->erater_style;
            $tiipost->transmatch = $turnitintool->transmatch;
            $tiipost->name = $turnitintool->name . ' - ' . $post['partname'] . ' (Moodle ' . $tiipost->assignid . ')';
            $tiipost->currentassign = $turnitintool->name . ' - ' . turnitintool_partnamefromnum($post['submitted']) . ' (Moodle ' . $tiipost->assignid . ')';
            $tii->createAssignment($tiipost, 'UPDATE', get_string('assignmentupdate', 'turnitintool', ''));
            if ($tii->getRerror()) {
                if ($tii->getRcode() == TURNITINTOOL_DB_UNIQUEID_ERROR) {
                    $reason = get_string('assignmentdoesnotexist', 'turnitintool');
                } else {
                    $reason = $tii->getAPIunavailable() ? get_string('apiunavailable', 'turnitintool') : $tii->getRmessage();
                }
                turnitintool_print_error('<strong>' . get_string('updateerror', 'turnitintool') . '</strong><br />' . $reason);
                exit;
            }
            $part = new stdClass();
            $part->id = $post['submitted'];
            $part->dtstart = $dtstart;
            $part->dtdue = $dtdue;
            $part->dtpost = $dtpost;
            $part->partname = $post['partname'];
            $part->maxmarks = $post['maxmarks'];
            $part->deleted = 0;
            $event = new stdClass();
            $event->timestart = $part->dtdue;
            $event->name = $turnitintool->name . ' - ' . $part->partname;
            $currentevent = $turnitintool->name . ' - ' . turnitintool_partnamefromnum($post['submitted']);
            if (!($dbpart = turnitintool_update_record('turnitintool_parts', $part, false))) {
                turnitintool_print_error('partdberror', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
            }
            if ($events = turnitintool_get_record_select('event', "modulename='turnitintool' AND instance = ? AND name = ?", array($turnitintool->id, $currentevent))) {
                $event->id = $events->id;
                if (method_exists('calendar_event', 'update')) {
                    $calendarevent = calendar_event::load($event->id);
                    $calendarevent->update($event);
                } else {
                    update_event($event);
                }
            }
            @(include_once $CFG->dirroot . "/lib/gradelib.php");
            // Set the view time for Grade Book viewing
            if (function_exists('grade_update')) {
                $lastpart = turnitintool_get_record('turnitintool_parts', 'turnitintoolid', $turnitintool->id, '', '', '', '', 'max(dtpost)');
                $lastpart = current($lastpart);
                $params['hidden'] = $lastpart;
                grade_update('mod/turnitintool', $turnitintool->course, 'mod', 'turnitintool', $turnitintool->id, 0, NULL, $params);
            }
            $tii->endSession();
            turnitintool_redirect($CFG->wwwroot . '/mod/turnitintool/view.php' . '?id=' . $cm->id . '&do=intro');
            exit;
        } else {
            return $notice;
        }
    } else {
        turnitintool_print_error('permissiondeniederror', 'turnitintool', NULL, NULL, __FILE__, __LINE__);
    }
}
Beispiel #22
0
/**
 * Update a vpl instance
 *
 * @param object from the form in mod.html
 * @return boolean OK
 *
 **/
function vpl_update_instance($instance)
{
    global $CFG, $DB;
    require_once $CFG->dirroot . '/calendar/lib.php';
    vpl_truncate_VPL($instance);
    $instance->id = $instance->instance;
    //Update event
    $event = vpl_create_event($instance, $instance->id);
    if ($eventid = $DB->get_field('event', 'id', array('modulename' => VPL, 'instance' => $instance->id))) {
        $event->id = $eventid;
        $calendarevent = calendar_event::load($eventid);
        if ($instance->duedate) {
            $calendarevent->update($event);
        } else {
            $calendarevent->delete();
        }
    } else {
        if ($instance->duedate) {
            calendar_event::create($event);
        }
    }
    $cm = get_coursemodule_from_instance(VPL, $instance->id, $instance->course);
    vpl_update_grade_item($instance, $cm);
    return $DB->update_record(VPL, $instance);
}
Beispiel #23
0
/**
 * Call this function to delete the event with id $id from calendar table.
 *
 * @param int $id The id of an event from the 'event' table.
 * @return bool
 */
function delete_event($id) {
    global $CFG;
    require_once($CFG->dirroot.'/calendar/lib.php');
    $event = calendar_event::load($id);
    return $event->delete();
}
Beispiel #24
0
    /**
     * Updates a new assignment activity
     *
     * Given an object containing all the necessary data,
     * (defined by the form in mod_form.php) this function
     * will update the assignment instance and return the id number
     * The due date is updated in the calendar
     * This is common to all assignment types.
     *
     * @global object
     * @global object
     * @param object $assignment The data from the form on mod_form.php
     * @return bool success
     */
    function update_instance($assignment) {
        global $COURSE, $DB;

        $assignment->timemodified = time();

        $assignment->id = $assignment->instance;
        $assignment->courseid = $assignment->course;

        $DB->update_record('assignment', $assignment);

        if ($assignment->timedue) {
            $event = new stdClass();

            if ($event->id = $DB->get_field('event', 'id', array('modulename'=>'assignment', 'instance'=>$assignment->id))) {

                $event->name        = $assignment->name;
                $event->description = format_module_intro('assignment', $assignment, $assignment->coursemodule);
                $event->timestart   = $assignment->timedue;

                $calendarevent = calendar_event::load($event->id);
                $calendarevent->update($event);
            } else {
                $event = new stdClass();
                $event->name        = $assignment->name;
                $event->description = format_module_intro('assignment', $assignment, $assignment->coursemodule);
                $event->courseid    = $assignment->course;
                $event->groupid     = 0;
                $event->userid      = 0;
                $event->modulename  = 'assignment';
                $event->instance    = $assignment->id;
                $event->eventtype   = 'due';
                $event->timestart   = $assignment->timedue;
                $event->timeduration = 0;

                calendar_event::create($event);
            }
        } else {
            $DB->delete_records('event', array('modulename'=>'assignment', 'instance'=>$assignment->id));
        }

        // get existing grade item
        assignment_grade_item_update($assignment);

        return true;
    }
Beispiel #25
0
 /**
  * Get Calendar events
  *
  * @param array $events A list of events
  * @param array $options various options
  * @return array Array of event details
  * @since Moodle 2.5
  */
 public static function get_calendar_events($events = array(), $options = array())
 {
     global $SITE, $DB, $USER, $CFG;
     require_once $CFG->dirroot . "/calendar/lib.php";
     // Parameter validation.
     $params = self::validate_parameters(self::get_calendar_events_parameters(), array('events' => $events, 'options' => $options));
     $funcparam = array('courses' => array(), 'groups' => array());
     $hassystemcap = has_capability('moodle/calendar:manageentries', context_system::instance());
     $warnings = array();
     // Let us findout courses that we can return events from.
     if (!$hassystemcap) {
         $courses = enrol_get_my_courses();
         $courses = array_keys($courses);
         foreach ($params['events']['courseids'] as $id) {
             if (in_array($id, $courses)) {
                 $funcparam['courses'][] = $id;
             } else {
                 $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to access this course');
             }
         }
     } else {
         $courses = $params['events']['courseids'];
         $funcparam['courses'] = $courses;
     }
     // Let us findout groups that we can return events from.
     if (!$hassystemcap) {
         $groups = groups_get_my_groups();
         $groups = array_keys($groups);
         foreach ($params['events']['groupids'] as $id) {
             if (in_array($id, $groups)) {
                 $funcparam['groups'][] = $id;
             } else {
                 $warnings[] = array('item' => $id, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to access this group');
             }
         }
     } else {
         $groups = $params['events']['groupids'];
         $funcparam['groups'] = $groups;
     }
     // Do we need user events?
     if (!empty($params['options']['userevents'])) {
         $funcparam['users'] = array($USER->id);
     } else {
         $funcparam['users'] = false;
     }
     // Do we need site events?
     if (!empty($params['options']['siteevents'])) {
         $funcparam['courses'][] = $SITE->id;
     }
     // We treat 0 and null as no end.
     if (empty($params['options']['timeend'])) {
         $params['options']['timeend'] = PHP_INT_MAX;
     }
     $eventlist = calendar_get_events($params['options']['timestart'], $params['options']['timeend'], $funcparam['users'], $funcparam['groups'], $funcparam['courses'], true, $params['options']['ignorehidden']);
     // WS expects arrays.
     $events = array();
     foreach ($eventlist as $id => $event) {
         $events[$id] = (array) $event;
     }
     // We need to get events asked for eventids.
     $eventsbyid = calendar_get_events_by_id($params['events']['eventids']);
     foreach ($eventsbyid as $eventid => $eventobj) {
         $event = (array) $eventobj;
         if (isset($events[$eventid])) {
             continue;
         }
         if ($hassystemcap) {
             // User can see everything, no further check is needed.
             $events[$eventid] = $event;
         } else {
             if (!empty($eventobj->modulename)) {
                 $cm = get_coursemodule_from_instance($eventobj->modulename, $eventobj->instance);
                 if (\core_availability\info_module::is_user_visible($cm, 0, false)) {
                     $events[$eventid] = $event;
                 }
             } else {
                 // Can the user actually see this event?
                 $eventobj = calendar_event::load($eventobj);
                 if ($eventobj->courseid == $SITE->id || !empty($eventobj->groupid) && in_array($eventobj->groupid, $groups) || !empty($eventobj->courseid) && in_array($eventobj->courseid, $courses) || $USER->id == $eventobj->userid || calendar_edit_event_allowed($eventid)) {
                     $events[$eventid] = $event;
                 } else {
                     $warnings[] = array('item' => $eventid, 'warningcode' => 'nopermissions', 'message' => 'you do not have permissions to view this event');
                 }
             }
         }
     }
     return array('events' => $events, 'warnings' => $warnings);
 }
/**
 * Given an object containing all the necessary data,
 * (defined by the form in mod_form.php) this function
 * will update an existing instance with new data.
 *
 * @param object $bigbluebuttonbn An object from the form in mod_form.php
 * @return boolean Success/Fail
 */
function bigbluebuttonbn_update_instance($bigbluebuttonbn)
{
    global $DB;
    $bigbluebuttonbn->timemodified = time();
    $bigbluebuttonbn->id = $bigbluebuttonbn->instance;
    if (!isset($bigbluebuttonbn->newwindow)) {
        $bigbluebuttonbn->newwindow = 0;
    }
    if (!isset($bigbluebuttonbn->wait)) {
        $bigbluebuttonbn->wait = 0;
    }
    if (!isset($bigbluebuttonbn->record)) {
        $bigbluebuttonbn->record = 0;
    }
    if (!isset($bigbluebuttonbn->allmoderators)) {
        $bigbluebuttonbn->allmoderators = 0;
    }
    $returnid = $DB->update_record('bigbluebuttonbn', $bigbluebuttonbn);
    if (isset($bigbluebuttonbn->timeavailable) && $bigbluebuttonbn->timeavailable) {
        $event = new stdClass();
        $event->name = $bigbluebuttonbn->name;
        $event->courseid = $bigbluebuttonbn->course;
        $event->groupid = 0;
        $event->userid = 0;
        $event->modulename = 'bigbluebuttonbn';
        $event->instance = $bigbluebuttonbn->id;
        $event->timestart = $bigbluebuttonbn->timeavailable;
        if ($bigbluebuttonbn->timedue) {
            $event->timeduration = $bigbluebuttonbn->timedue - $bigbluebuttonbn->timeavailable;
        } else {
            $event->timeduration = 0;
        }
        if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id))) {
            $calendarevent = calendar_event::load($event->id);
            $calendarevent->update($event);
        } else {
            calendar_event::create($event);
        }
    } else {
        $DB->delete_records('event', array('modulename' => 'bigbluebuttonbn', 'instance' => $bigbluebuttonbn->id));
    }
    return $returnid;
}
Beispiel #27
0
 /**
  * Deletes this lesson from the database
  */
 public function delete()
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/gradelib.php';
     require_once $CFG->dirroot . '/calendar/lib.php';
     $cm = get_coursemodule_from_instance('lesson', $this->properties->id, $this->properties->course);
     $context = context_module::instance($cm->id);
     $DB->delete_records("lesson", array("id" => $this->properties->id));
     $DB->delete_records("lesson_pages", array("lessonid" => $this->properties->id));
     $DB->delete_records("lesson_answers", array("lessonid" => $this->properties->id));
     $DB->delete_records("lesson_attempts", array("lessonid" => $this->properties->id));
     $DB->delete_records("lesson_grades", array("lessonid" => $this->properties->id));
     $DB->delete_records("lesson_timer", array("lessonid" => $this->properties->id));
     $DB->delete_records("lesson_branch", array("lessonid" => $this->properties->id));
     $DB->delete_records("lesson_high_scores", array("lessonid" => $this->properties->id));
     if ($events = $DB->get_records('event', array("modulename" => 'lesson', "instance" => $this->properties->id))) {
         foreach ($events as $event) {
             $event = calendar_event::load($event);
             $event->delete();
         }
     }
     // Delete files associated with this module.
     $fs = get_file_storage();
     $fs->delete_area_files($context->id);
     grade_update('mod/lesson', $this->properties->course, 'mod', 'lesson', $this->properties->id, 0, null, array('deleted' => 1));
     return true;
 }
function questionnaire_set_events($questionnaire)
{
    // Adding the questionnaire to the eventtable.
    global $DB;
    if ($events = $DB->get_records('event', array('modulename' => 'questionnaire', 'instance' => $questionnaire->id))) {
        foreach ($events as $event) {
            $event = calendar_event::load($event);
            $event->delete();
        }
    }
    // The open-event.
    $event = new stdClass();
    $event->description = $questionnaire->name;
    $event->courseid = $questionnaire->course;
    $event->groupid = 0;
    $event->userid = 0;
    $event->modulename = 'questionnaire';
    $event->instance = $questionnaire->id;
    $event->eventtype = 'open';
    $event->timestart = $questionnaire->opendate;
    $event->visible = instance_is_visible('questionnaire', $questionnaire);
    $event->timeduration = $questionnaire->closedate - $questionnaire->opendate;
    if ($questionnaire->closedate && $questionnaire->opendate && $event->timeduration <= QUESTIONNAIRE_MAX_EVENT_LENGTH) {
        // Single event for the whole questionnaire.
        $event->name = $questionnaire->name;
        calendar_event::create($event);
    } else {
        // Separate start and end events.
        $event->timeduration = 0;
        if ($questionnaire->opendate) {
            $event->name = $questionnaire->name . ' (' . get_string('questionnaireopens', 'questionnaire') . ')';
            calendar_event::create($event);
            unset($event->id);
            // So we can use the same object for the close event.
        }
        if ($questionnaire->closedate) {
            $event->name = $questionnaire->name . ' (' . get_string('questionnairecloses', 'questionnaire') . ')';
            $event->timestart = $questionnaire->closedate;
            $event->eventtype = 'close';
            calendar_event::create($event);
        }
    }
}
Beispiel #29
0
 /**
  * Update the calendar entries for this assignment.
  *
  * @param int $coursemoduleid - Required to pass this in because it might
  *                              not exist in the database yet.
  * @return bool
  */
 public function update_calendar($coursemoduleid)
 {
     global $DB, $CFG;
     require_once $CFG->dirroot . '/calendar/lib.php';
     // Special case for add_instance as the coursemodule has not been set yet.
     $instance = $this->get_instance();
     if ($instance->duedate) {
         $event = new stdClass();
         $params = array('modulename' => 'assign', 'instance' => $instance->id);
         $event->id = $DB->get_field('event', 'id', $params);
         if ($event->id) {
             $event->name = $instance->name;
             $event->description = format_module_intro('assign', $instance, $coursemoduleid);
             $event->timestart = $instance->duedate;
             $calendarevent = calendar_event::load($event->id);
             $calendarevent->update($event);
         } else {
             $event = new stdClass();
             $event->name = $instance->name;
             $event->description = format_module_intro('assign', $instance, $coursemoduleid);
             $event->courseid = $instance->course;
             $event->groupid = 0;
             $event->userid = 0;
             $event->modulename = 'assign';
             $event->instance = $instance->id;
             $event->eventtype = 'due';
             $event->timestart = $instance->duedate;
             $event->timeduration = 0;
             calendar_event::create($event);
         }
     } else {
         $DB->delete_records('event', array('modulename' => 'assign', 'instance' => $instance->id));
     }
 }
Beispiel #30
0
/**
 * This function updates the events associated to the quiz.
 * If $override is non-zero, then it updates only the events
 * associated with the specified override.
 *
 * @uses QUIZ_MAX_EVENT_LENGTH
 * @param object $quiz the quiz object.
 * @param object optional $override limit to a specific override
 */
function quiz_update_events($quiz, $override = null) {
    global $DB;

    // Load the old events relating to this quiz.
    $conds = array('modulename'=>'quiz',
                   'instance'=>$quiz->id);
    if (!empty($override)) {
        // Only load events for this override.
        $conds['groupid'] = isset($override->groupid)?  $override->groupid : 0;
        $conds['userid'] = isset($override->userid)?  $override->userid : 0;
    }
    $oldevents = $DB->get_records('event', $conds);

    // Now make a todo list of all that needs to be updated.
    if (empty($override)) {
        // We are updating the primary settings for the quiz, so we
        // need to add all the overrides.
        $overrides = $DB->get_records('quiz_overrides', array('quiz' => $quiz->id));
        // As well as the original quiz (empty override).
        $overrides[] = new stdClass();
    } else {
        // Just do the one override.
        $overrides = array($override);
    }

    foreach ($overrides as $current) {
        $groupid   = isset($current->groupid)?  $current->groupid : 0;
        $userid    = isset($current->userid)? $current->userid : 0;
        $timeopen  = isset($current->timeopen)?  $current->timeopen : $quiz->timeopen;
        $timeclose = isset($current->timeclose)? $current->timeclose : $quiz->timeclose;

        // Only add open/close events for an override if they differ from the quiz default.
        $addopen  = empty($current->id) || !empty($current->timeopen);
        $addclose = empty($current->id) || !empty($current->timeclose);

        if (!empty($quiz->coursemodule)) {
            $cmid = $quiz->coursemodule;
        } else {
            $cmid = get_coursemodule_from_instance('quiz', $quiz->id, $quiz->course)->id;
        }

        $event = new stdClass();
        $event->description = format_module_intro('quiz', $quiz, $cmid);
        // Events module won't show user events when the courseid is nonzero.
        $event->courseid    = ($userid) ? 0 : $quiz->course;
        $event->groupid     = $groupid;
        $event->userid      = $userid;
        $event->modulename  = 'quiz';
        $event->instance    = $quiz->id;
        $event->timestart   = $timeopen;
        $event->timeduration = max($timeclose - $timeopen, 0);
        $event->visible     = instance_is_visible('quiz', $quiz);
        $event->eventtype   = 'open';

        // Determine the event name.
        if ($groupid) {
            $params = new stdClass();
            $params->quiz = $quiz->name;
            $params->group = groups_get_group_name($groupid);
            if ($params->group === false) {
                // Group doesn't exist, just skip it.
                continue;
            }
            $eventname = get_string('overridegroupeventname', 'quiz', $params);
        } else if ($userid) {
            $params = new stdClass();
            $params->quiz = $quiz->name;
            $eventname = get_string('overrideusereventname', 'quiz', $params);
        } else {
            $eventname = $quiz->name;
        }
        if ($addopen or $addclose) {
            if ($timeclose and $timeopen and $event->timeduration <= QUIZ_MAX_EVENT_LENGTH) {
                // Single event for the whole quiz.
                if ($oldevent = array_shift($oldevents)) {
                    $event->id = $oldevent->id;
                } else {
                    unset($event->id);
                }
                $event->name = $eventname;
                // The method calendar_event::create will reuse a db record if the id field is set.
                calendar_event::create($event);
            } else {
                // Separate start and end events.
                $event->timeduration  = 0;
                if ($timeopen && $addopen) {
                    if ($oldevent = array_shift($oldevents)) {
                        $event->id = $oldevent->id;
                    } else {
                        unset($event->id);
                    }
                    $event->name = $eventname.' ('.get_string('quizopens', 'quiz').')';
                    // The method calendar_event::create will reuse a db record if the id field is set.
                    calendar_event::create($event);
                }
                if ($timeclose && $addclose) {
                    if ($oldevent = array_shift($oldevents)) {
                        $event->id = $oldevent->id;
                    } else {
                        unset($event->id);
                    }
                    $event->name      = $eventname.' ('.get_string('quizcloses', 'quiz').')';
                    $event->timestart = $timeclose;
                    $event->eventtype = 'close';
                    calendar_event::create($event);
                }
            }
        }
    }

    // Delete any leftover events.
    foreach ($oldevents as $badevent) {
        $badevent = calendar_event::load($badevent);
        $badevent->delete();
    }
}